line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Direct::Directory::File; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
70
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
406
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Template::Direct::Directory::File - Objectified access to files |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Template::Direct::Directory::File; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $file1 = $directory->new( File => 'file1.txt' ); |
15
|
|
|
|
|
|
|
my $file2 = Directory::File->new( File => '/Root/lib/file1.txt' ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Loads a directory for use with FileDirectives |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 METHODS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $VERSION = "1.00"; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use overload |
28
|
4
|
|
|
4
|
|
14
|
'""' => sub { shift->autocontents(@_) }, |
29
|
6
|
|
|
6
|
|
20
|
'bool' => sub { shift->exist(@_) }, |
30
|
2
|
|
|
2
|
|
11
|
'eq' => sub { shift->autocontents(@_) }; |
|
2
|
|
|
0
|
|
5
|
|
|
2
|
|
|
|
|
23
|
|
|
0
|
|
|
|
|
0
|
|
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
2
|
|
148
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3200
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 I<$class>->new( $filename, %p ) |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Create a new file object. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
sub new |
40
|
|
|
|
|
|
|
{ |
41
|
2
|
|
|
2
|
1
|
9
|
my ($class, $file, %p) = @_; |
42
|
|
|
|
|
|
|
|
43
|
2
|
50
|
0
|
|
|
6
|
carp "Unable to create valid file object, missing File p" and return if not $file; |
44
|
2
|
|
|
|
|
11
|
my $self = bless { %p, File => $file }, $class; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#warn Carp::longmess("\nNew File -> $file (".$self->{'Parent'}.") + ".$p{'Create'}."\n"); |
47
|
|
|
|
|
|
|
|
48
|
2
|
50
|
|
|
|
46
|
if(not $self->{'Parent'}) { |
49
|
0
|
|
|
|
|
0
|
my $dir = $file; |
50
|
0
|
|
|
|
|
0
|
$dir =~ s/\/([^\/]*?)$//; |
51
|
0
|
|
|
|
|
0
|
warn "\nTurning file into $1! does this make sense?\n"; |
52
|
0
|
|
|
|
|
0
|
$self->{'File'} = $1; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
0
|
warn "Creating Directory Parent '$dir'\n" if $ENV{'FILE_DEBUG'}; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
0
|
$self->{'Parent'} = Template::Direct::Directory->new( $dir, Create => $p{'Create'} ); |
57
|
|
|
|
|
|
|
} |
58
|
2
|
50
|
|
|
|
10
|
if(not $self->{'Parent'}) { |
59
|
0
|
0
|
|
|
|
0
|
carp "File Warn: Unable to create file because directory does not exist" if $ENV{'FILE_DEBUG'}; |
60
|
0
|
|
|
|
|
0
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
2
|
50
|
33
|
|
|
11
|
if($p{'Create'} and not $self->exist()) { |
64
|
0
|
|
|
|
|
0
|
$self->save(''); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
2
|
50
|
|
|
|
9
|
if(my $cache = $self->parent->loadCache($self->path())) { |
68
|
0
|
|
|
|
|
0
|
return $cache; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2
|
50
|
33
|
|
|
13
|
$self->parent->saveCache($self->path(), $self) if $self->{'Cache'} and ($self->exist() or $p{'Create'}); |
|
|
|
33
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
10
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 OVERLOADED |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 I<$file>->autocontents( ) |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Return the contents of a file when used in string context. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
sub autocontents |
84
|
|
|
|
|
|
|
{ |
85
|
4
|
|
|
4
|
1
|
6
|
my ($self) = @_; |
86
|
|
|
|
|
|
|
|
87
|
4
|
100
|
66
|
|
|
24
|
if(not defined($self->{'Data'}) or $self->outofdate()) { |
88
|
2
|
|
|
|
|
7
|
return $self->load(); |
89
|
|
|
|
|
|
|
} |
90
|
2
|
|
|
|
|
38
|
return $self->{'Data'}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 I<$file>->save( $new_data, %options ) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Save $new_data as the new file contents. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Options: |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
* Append - Boolean to specifiy data is to be appended. |
100
|
|
|
|
|
|
|
* Text - Treat data as text and do CR/LF filtering |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
sub save |
104
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
0
|
1
|
0
|
my ($self, $data, %p) = @_; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
my $filename = $self->path(); |
108
|
0
|
0
|
|
|
|
0
|
if(not $filename) { |
109
|
0
|
|
|
|
|
0
|
carp "File Error: No file name in save file"; return; |
|
0
|
|
|
|
|
0
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
0
|
if(defined($data)) { |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
0
|
if($p{'Text'}) { |
115
|
0
|
|
|
|
|
0
|
$self->unix(\$filename); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Both save and Append functions |
119
|
0
|
0
|
|
|
|
0
|
my $method = ">".($p{'Append'} ? ">" : ""); |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
0
|
if(open( FILE, $method.$filename )) { |
122
|
0
|
0
|
|
|
|
0
|
$data = join('', $data) if ref($data) eq "Fh"; |
123
|
0
|
|
|
|
|
0
|
print FILE $data; |
124
|
0
|
|
|
|
|
0
|
close( FILE ); |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
0
|
if(not $p{'nocache'}) { |
127
|
0
|
|
|
|
|
0
|
$self->{'modtime'} = -M $self->path; |
128
|
0
|
|
|
|
|
0
|
$self->parent->saveCache($self->path, $self); |
129
|
0
|
|
|
|
|
0
|
$self->{'Data'} = $data; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
0
|
|
|
|
0
|
warn "File: Saving $filename\n" if $ENV{'FILE_DEBUG'}; |
132
|
|
|
|
|
|
|
} else { |
133
|
0
|
|
|
|
|
0
|
carp "File Error: could not save file: $filename, $!"; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} else { |
136
|
0
|
|
|
|
|
0
|
carp "File Error: could not save file: $filename, No data provided"; |
137
|
|
|
|
|
|
|
} |
138
|
0
|
|
|
|
|
0
|
return 1; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 I<$file>->append( $data, %p ) |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Same as save() but specify data is to be appended. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
0
|
|
|
0
|
1
|
0
|
sub append { my $self = shift; $self->save(@_, Append => 1 ); } |
|
0
|
|
|
|
|
0
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 I<$file>->load( %options ) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Load data from file with options: |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
* Quoting - Quote all data |
155
|
|
|
|
|
|
|
* Text - Treat data as text and filter CR/LF |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
sub load |
159
|
|
|
|
|
|
|
{ |
160
|
2
|
|
|
2
|
1
|
5
|
my ($self, %p) = @_; |
161
|
|
|
|
|
|
|
|
162
|
2
|
|
|
|
|
5
|
my $filename = $self->path(); |
163
|
2
|
|
|
|
|
6
|
my $data; |
164
|
|
|
|
|
|
|
|
165
|
2
|
50
|
|
|
|
6
|
if(not $filename) { |
166
|
0
|
|
|
|
|
0
|
carp "File Error: No filename to load file"; |
167
|
0
|
|
|
|
|
0
|
return -1; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
2
|
50
|
|
|
|
103
|
if(open( File, $filename )) { |
171
|
2
|
|
|
|
|
91
|
$data = join('', ); |
172
|
2
|
50
|
|
|
|
14
|
warn "File: Loading '$filename'\n" if $ENV{'FILE_DEBUG'}; |
173
|
2
|
|
|
|
|
29
|
close( File ); |
174
|
|
|
|
|
|
|
} else { |
175
|
0
|
|
|
|
|
0
|
carp "File Error: Unable to open file: $filename, $!"; |
176
|
0
|
|
|
|
|
0
|
return -1; |
177
|
|
|
|
|
|
|
} |
178
|
2
|
50
|
33
|
|
|
11
|
warn "File: NOT Caching $filename\n" if not $self->{'Cache'} and $ENV{'FILE_DEBUG'}; |
179
|
2
|
|
|
|
|
26
|
$self->{'modtime'} = -M $filename; |
180
|
|
|
|
|
|
|
|
181
|
2
|
50
|
|
|
|
9
|
if($p{'Quoting'}) { |
182
|
0
|
|
|
|
|
0
|
$self->quote(\$data); |
183
|
|
|
|
|
|
|
} |
184
|
2
|
50
|
|
|
|
4
|
if($p{'Text'}) { |
185
|
0
|
|
|
|
|
0
|
$self->unix(\$data); |
186
|
|
|
|
|
|
|
} |
187
|
2
|
|
|
|
|
7
|
$self->{'modtime'} = -M $self->path; |
188
|
2
|
|
|
|
|
8
|
$self->{'Data'} = $data; |
189
|
2
|
|
|
|
|
22
|
return $data; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 I<$file>->path( ) |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Return the full path to this file objects location. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
sub path { |
198
|
18
|
|
|
18
|
1
|
22
|
my ($self) = @_; |
199
|
18
|
|
|
|
|
30
|
return $self->parent()->path().$self->filename(); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 I<$file>->filename( ) |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 I<$file>->name( ) |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Return the files name without path. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
209
|
|
|
|
|
|
|
sub filename |
210
|
|
|
|
|
|
|
{ |
211
|
18
|
|
|
18
|
1
|
22
|
my ($self) = @_; |
212
|
18
|
|
|
|
|
215
|
return $self->{'File'}; |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
0
|
1
|
0
|
sub name { shift->filename(); } |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head2 I<$file>->parent( ) |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Return the parent Directory object to this file. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
sub parent |
222
|
|
|
|
|
|
|
{ |
223
|
22
|
|
|
22
|
1
|
25
|
my ($self) = @_; |
224
|
22
|
|
|
|
|
74
|
return $self->{'Parent'}; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 I<$file>->exist( ) |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Return true is this file exists on the disk. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
sub exist { |
233
|
8
|
|
|
8
|
1
|
11
|
my ($self) = @_; |
234
|
8
|
50
|
|
|
|
14
|
return -f $self->path() ? 1 : 0; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 I<$file>->clearCache( ) |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Clear this files cache (if it is cached) |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
242
|
|
|
|
|
|
|
sub clearCache { |
243
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
244
|
0
|
|
|
|
|
0
|
return $self->parent->clearCache( File => $self->filename ); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 I<$file>->fromCache( ) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Was this file loaded from cache? (used for testing) |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |
252
|
|
|
|
|
|
|
sub fromCache { |
253
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
254
|
0
|
|
|
|
|
0
|
return $self->{'fromCache'}; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 I<$file>->delete( ) |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Remove this file fromt he disk and close object. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
sub delete |
263
|
|
|
|
|
|
|
{ |
264
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
265
|
0
|
0
|
|
|
|
0
|
warn "Deleteing file '".$self->path."'\n" if $ENV{'DIR_DEBUG'}; |
266
|
0
|
|
|
|
|
0
|
my $result = unlink($self->path); |
267
|
0
|
0
|
|
|
|
0
|
$self->clearCache if $result; |
268
|
0
|
|
|
|
|
0
|
return $result; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head2 I<$file>->size( $h ) |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Returns size of file as number of bytes unless |
274
|
|
|
|
|
|
|
$h is true in which case it returns the most |
275
|
|
|
|
|
|
|
relivent size metric (i.e KB/MB/GB) |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
278
|
|
|
|
|
|
|
sub size |
279
|
|
|
|
|
|
|
{ |
280
|
0
|
|
|
0
|
1
|
0
|
my ($self, $h) = @_; |
281
|
0
|
|
|
|
|
0
|
my $unit = ''; |
282
|
0
|
|
|
|
|
0
|
my $filesize = -s $self->path; |
283
|
0
|
0
|
|
|
|
0
|
if($h) { |
284
|
0
|
|
|
|
|
0
|
$unit = 'Bytes'; |
285
|
0
|
0
|
0
|
|
|
0
|
$filesize / 1024 and $unit = 'KB' if $filesize > 1024; |
286
|
0
|
0
|
0
|
|
|
0
|
$filesize / 1024 and $unit = 'MB' if $filesize > 1024; |
287
|
0
|
0
|
0
|
|
|
0
|
$filesize / 1024 and $unit = 'GB' if $filesize > 1024; |
288
|
|
|
|
|
|
|
} |
289
|
0
|
|
|
|
|
0
|
return $filesize.$unit; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head2 I<$file>->outofdate( ) |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Returns true if the file is out of date (used internally) |
295
|
|
|
|
|
|
|
The file with automatically reload contents if it's out of date |
296
|
|
|
|
|
|
|
when used so there isn't a need to use this for content. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=cut |
299
|
|
|
|
|
|
|
sub outofdate |
300
|
|
|
|
|
|
|
{ |
301
|
2
|
|
|
2
|
1
|
4
|
my ($self) = @_; |
302
|
2
|
|
|
|
|
5
|
my $newfiletime = -M $self->path; |
303
|
|
|
|
|
|
|
#warn "return 1 if not ".length($self->modtime)." or $newfiletime < ".$self->modtime."\n"; |
304
|
2
|
50
|
33
|
|
|
9
|
return 1 if(not length($self->modtime) or $newfiletime < $self->modtime); |
305
|
2
|
|
|
|
|
9
|
return 0; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head2 I<$file>->modtime( ) |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
When was the last time this file was modified. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=cut |
313
|
|
|
|
|
|
|
sub modtime |
314
|
|
|
|
|
|
|
{ |
315
|
4
|
|
|
4
|
1
|
6
|
my ($self) = @_; |
316
|
4
|
50
|
|
|
|
66
|
return defined($self->{'modtime'}) ? $self->{'modtime'} : ''; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head2 I<$file>->isfile( ) |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
Returns true. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
324
|
0
|
|
|
0
|
1
|
|
sub isfile { 1 } |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head2 I<$file>->isdir( ) |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
Returns false |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=cut |
331
|
0
|
|
|
0
|
1
|
|
sub isdir { 0 } |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=head1 AUTHOR |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
Copyright, Martin Owens 2008, AGPL |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
1; |