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
|
|
587
|
use strict; |
|
2
|
|
|
|
|
50
|
|
|
2
|
|
|
|
|
64
|
|
25
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
26
|
2
|
|
|
2
|
|
9
|
use Cwd; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
101
|
|
27
|
2
|
|
|
2
|
|
8
|
use File::Spec; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
33
|
|
28
|
2
|
|
|
2
|
|
8
|
use File::Basename; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
126
|
|
29
|
2
|
|
|
2
|
|
9
|
use base 'Template::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
806
|
|
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
|
284
|
my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { }; |
47
|
104
|
|
|
|
|
168
|
my ($class, $context, $path) = @_; |
48
|
104
|
|
|
|
|
116
|
my ($root, $home, @stat, $abs); |
49
|
|
|
|
|
|
|
|
50
|
104
|
100
|
66
|
|
|
463
|
return $class->throw('no file specified') |
51
|
|
|
|
|
|
|
unless defined $path and length $path; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# path, dir, name, root, home |
54
|
|
|
|
|
|
|
|
55
|
103
|
100
|
|
|
|
790
|
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
|
|
|
|
|
114
|
$root = ''; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
103
|
|
|
|
|
2583
|
my ($name, $dir, $ext) = fileparse($path, '\.\w+'); |
67
|
|
|
|
|
|
|
# fixup various items |
68
|
103
|
|
|
|
|
375
|
$dir =~ s[/$][]; |
69
|
103
|
100
|
|
|
|
230
|
$dir = '' if $dir eq '.'; |
70
|
103
|
|
|
|
|
112
|
$name = $name . $ext; |
71
|
103
|
|
|
|
|
160
|
$ext =~ s/^\.//g; |
72
|
|
|
|
|
|
|
|
73
|
103
|
|
|
|
|
556
|
my @fields = File::Spec->splitdir($dir); |
74
|
103
|
100
|
100
|
|
|
529
|
shift @fields if @fields && ! length $fields[0]; |
75
|
103
|
|
|
|
|
259
|
$home = join('/', ('..') x @fields); |
76
|
103
|
100
|
|
|
|
804
|
$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
|
|
|
|
394
|
map { ($_ => '') } @STAT_KEYS, |
|
1339
|
|
|
|
|
3304
|
|
93
|
|
|
|
|
|
|
}; |
94
|
|
|
|
|
|
|
|
95
|
103
|
100
|
|
|
|
432
|
if ($self->{ stat }) { |
96
|
94
|
100
|
|
|
|
2095
|
(@stat = stat( $abs )) |
97
|
|
|
|
|
|
|
|| return $class->throw("$abs: $!"); |
98
|
|
|
|
|
|
|
|
99
|
93
|
|
|
|
|
648
|
@$self{ @STAT_KEYS } = @stat; |
100
|
|
|
|
|
|
|
|
101
|
93
|
50
|
|
|
|
240
|
unless ($config->{ noid }) { |
102
|
93
|
50
|
|
|
|
129
|
$self->{ user } = eval { getpwuid( $self->{ uid }) || $self->{ uid } }; |
|
93
|
|
|
|
|
8544
|
|
103
|
93
|
50
|
|
|
|
166
|
$self->{ group } = eval { getgrgid( $self->{ gid }) || $self->{ gid } }; |
|
93
|
|
|
|
|
5946
|
|
104
|
|
|
|
|
|
|
} |
105
|
93
|
|
|
|
|
1486
|
$self->{ isdir } = -d $abs; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
102
|
|
|
|
|
691
|
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
|
6
|
my ($self, $path) = @_; |
120
|
2
|
100
|
|
|
|
12
|
$path = $path->{ path } if ref $path eq ref $self; # assumes same root |
121
|
2
|
50
|
|
|
|
7
|
return $path if $path =~ m[^/]; |
122
|
2
|
50
|
|
|
|
134
|
return $path unless $self->{ home }; |
123
|
2
|
|
|
|
|
14
|
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
|
|
|
|
|
43
|
$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__ |