line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-Perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Template::Plugin::File |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Plugin for encapsulating information about a system file. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# AUTHOR |
9
|
|
|
|
|
|
|
# Originally written by Michael Stevens as the |
10
|
|
|
|
|
|
|
# Directory plugin, then mutilated by Andy Wardley |
11
|
|
|
|
|
|
|
# into separate File and Directory plugins, with some additional |
12
|
|
|
|
|
|
|
# code for working with views, etc. |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# COPYRIGHT |
15
|
|
|
|
|
|
|
# Copyright 2000-2007 Michael Stevens, Andy Wardley. |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
18
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
#============================================================================ |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
package Template::Plugin::File; |
23
|
|
|
|
|
|
|
|
24
|
2
|
|
|
2
|
|
725
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
79
|
|
25
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
52
|
|
26
|
2
|
|
|
2
|
|
10
|
use Cwd; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
110
|
|
27
|
2
|
|
|
2
|
|
9
|
use File::Spec; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
36
|
|
28
|
2
|
|
|
2
|
|
10
|
use File::Basename; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
140
|
|
29
|
2
|
|
|
2
|
|
8
|
use base 'Template::Plugin'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
835
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = 2.71; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
our @STAT_KEYS = qw( dev ino mode nlink uid gid rdev size |
34
|
|
|
|
|
|
|
atime mtime ctime blksize blocks ); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
38
|
|
|
|
|
|
|
# new($context, $file, \%config) |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# Create a new File object. Takes the pathname of the file as |
41
|
|
|
|
|
|
|
# the argument following the context and an optional |
42
|
|
|
|
|
|
|
# hash reference of configuration parameters. |
43
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub new { |
46
|
104
|
100
|
|
104
|
1
|
304
|
my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; |
47
|
104
|
|
|
|
|
154
|
my ($class, $context, $path) = @_; |
48
|
104
|
|
|
|
|
121
|
my ($root, $home, @stat, $abs); |
49
|
|
|
|
|
|
|
|
50
|
104
|
100
|
66
|
|
|
488
|
return $class->throw('no file specified') |
51
|
|
|
|
|
|
|
unless defined $path and length $path; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# path, dir, name, root, home |
54
|
|
|
|
|
|
|
|
55
|
103
|
100
|
|
|
|
792
|
if (File::Spec->file_name_is_absolute($path)) { |
|
|
100
|
|
|
|
|
|
56
|
2
|
|
|
|
|
4
|
$root = ''; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif (($root = $config->{ root })) { |
59
|
|
|
|
|
|
|
# strip any trailing '/' from root |
60
|
30
|
|
|
|
|
57
|
$root =~ s[/$][]; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
71
|
|
|
|
|
106
|
$root = ''; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
103
|
|
|
|
|
2727
|
my ($name, $dir, $ext) = fileparse($path, '\.\w+'); |
67
|
|
|
|
|
|
|
# fixup various items |
68
|
103
|
|
|
|
|
397
|
$dir =~ s[/$][]; |
69
|
103
|
100
|
|
|
|
266
|
$dir = '' if $dir eq '.'; |
70
|
103
|
|
|
|
|
133
|
$name = $name . $ext; |
71
|
103
|
|
|
|
|
159
|
$ext =~ s/^\.//g; |
72
|
|
|
|
|
|
|
|
73
|
103
|
|
|
|
|
645
|
my @fields = File::Spec->splitdir($dir); |
74
|
103
|
100
|
100
|
|
|
527
|
shift @fields if @fields && ! length $fields[0]; |
75
|
103
|
|
|
|
|
275
|
$home = join('/', ('..') x @fields); |
76
|
103
|
100
|
|
|
|
781
|
$abs = File::Spec->catfile($root ? $root : (), $path); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $self = { |
79
|
|
|
|
|
|
|
path => $path, |
80
|
|
|
|
|
|
|
name => $name, |
81
|
|
|
|
|
|
|
root => $root, |
82
|
|
|
|
|
|
|
home => $home, |
83
|
|
|
|
|
|
|
dir => $dir, |
84
|
|
|
|
|
|
|
ext => $ext, |
85
|
|
|
|
|
|
|
abs => $abs, |
86
|
|
|
|
|
|
|
user => '', |
87
|
|
|
|
|
|
|
group => '', |
88
|
|
|
|
|
|
|
isdir => '', |
89
|
|
|
|
|
|
|
stat => defined $config->{ stat } |
90
|
|
|
|
|
|
|
? $config->{ stat } |
91
|
|
|
|
|
|
|
: ! $config->{ nostat }, |
92
|
103
|
50
|
|
|
|
454
|
map { ($_ => '') } @STAT_KEYS, |
|
1339
|
|
|
|
|
3370
|
|
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
|
95
|
103
|
100
|
|
|
|
424
|
if ($self->{ stat }) { |
96
|
94
|
100
|
|
|
|
2350
|
(@stat = stat( $abs )) |
97
|
|
|
|
|
|
|
|| return $class->throw("$abs: $!"); |
98
|
|
|
|
|
|
|
|
99
|
93
|
|
|
|
|
674
|
@$self{ @STAT_KEYS } = @stat; |
100
|
|
|
|
|
|
|
|
101
|
93
|
50
|
|
|
|
293
|
unless ($config->{ noid }) { |
102
|
93
|
50
|
|
|
|
120
|
$self->{ user } = eval { getpwuid( $self->{ uid }) || $self->{ uid } }; |
|
93
|
|
|
|
|
18009
|
|
103
|
93
|
50
|
|
|
|
155
|
$self->{ group } = eval { getgrgid( $self->{ gid }) || $self->{ gid } }; |
|
93
|
|
|
|
|
5762
|
|
104
|
|
|
|
|
|
|
} |
105
|
93
|
|
|
|
|
1627
|
$self->{ isdir } = -d $abs; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
102
|
|
|
|
|
849
|
bless $self, $class; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
#------------------------------------------------------------------------- |
113
|
|
|
|
|
|
|
# rel($file) |
114
|
|
|
|
|
|
|
# |
115
|
|
|
|
|
|
|
# Generate a relative filename for some other file relative to this one. |
116
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub rel { |
119
|
2
|
|
|
2
|
1
|
7
|
my ($self, $path) = @_; |
120
|
2
|
100
|
|
|
|
10
|
$path = $path->{ path } if ref $path eq ref $self; # assumes same root |
121
|
2
|
50
|
|
|
|
7
|
return $path if $path =~ m[^/]; |
122
|
2
|
50
|
|
|
|
20
|
return $path unless $self->{ home }; |
123
|
2
|
|
|
|
|
333
|
return $self->{ home } . '/' . $path; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
128
|
|
|
|
|
|
|
# present($view) |
129
|
|
|
|
|
|
|
# |
130
|
|
|
|
|
|
|
# Present self to a Template::View. |
131
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub present { |
134
|
7
|
|
|
7
|
0
|
9
|
my ($self, $view) = @_; |
135
|
7
|
|
|
|
|
56
|
$view->view_file($self); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub throw { |
140
|
1
|
|
|
1
|
0
|
3
|
my ($self, $error) = @_; |
141
|
1
|
|
|
|
|
11
|
die (Template::Exception->new('File', $error)); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |