lib/Badger/Filesystem.pm

filter: Statementstmt 96.7% 271 / 280 Branchbran 70.8% 102 / 144 Conditioncond 50.5% 50 / 99 MC/DCmcdc 17.7% 11 / 62 Subroutinesub 93.3% 56 / 60 Podpod 100% 49 / 49 totaltotal 77.6% 539 / 694 CC 108 SCAR 18.3
CC
108
Cov
81%
_file_copy
21.8
open_file
13.1
init
11.6
CRAP 6.2
« blib/lib/Badger/Rainbow.pm ↑ Summary lib/Badger/Exception.pm »
1 #========================================================================
2 #
3 # Badger::Filesystem
4 #
5 # DESCRIPTION
6 # OO representation of a filesystem.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Filesystem;
14  
15 70 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use Cwd 'getcwd';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use Badger::Class
18 version => 0.01,
19 debug => 0,
20 base => 'Badger::Prototype Badger::Filesystem::Base',
21 import => 'class',
22 utils => 'params is_object random_name',
23 constants => 'HASH ARRAY TRUE REFS PKG',
24 constant => {
25 virtual => 0,
26 NO_FILENAME => 1,
27 FILESPEC => 'File::Spec',
28 FINDBIN => 'FindBin',
29 ROOTDIR => File::Spec->rootdir,
30 CURDIR => File::Spec->curdir,
31 UPDIR => File::Spec->updir,
32 FS => 'Badger::Filesystem',
33 VFS => 'Badger::Filesystem::Virtual',
34 UFS => 'Badger::Filesystem::Universal',
35 PATH => 'Badger::Filesystem::Path',
36 FILE => 'Badger::Filesystem::File',
37 DIRECTORY => 'Badger::Filesystem::Directory',
38 VISITOR => 'Badger::Filesystem::Visitor',
39 },
40 exports => {
41 any => 'FS PATH FILE DIR DIRECTORY cwd getcwd rel2abs abs2rel',
42 tags => {
43 types => 'Path File Dir Directory Cwd Bin',
44 dirs => 'ROOTDIR UPDIR CURDIR',
45 },
46 hooks => {
47 VFS => sub {
48 # load VFS module and call its export() method
49 2 ▶ class(shift->VFS)->load->pkg->export(shift, shift)
Subroutine __ANON__: called CC=1 SCAR=0.0
50 },
51 UFS => sub {
52 # load UFS module and call its export() method
53 1 ▶ class(shift->UFS)->load->pkg->export(shift, shift)
Subroutine __ANON__: called CC=1 SCAR=0.0
54 },
55 70 '$Bin' => \&_export_findbin_hook,
56 },
57 },
58 messages => {
59 open_failed => 'Failed to open %s %s: %s',
60 delete_failed => 'Failed to delete %s %s: %s',
61 bad_volume => 'Volume mismatch: %s vs %s',
62 bad_stat => 'Nothing known about %s',
63 copy_failed => 'Failed to %s file from %s to %s: %s',
64 no_path => 'Unable to determine location of %s',
65 70 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 70 ▶use Badger::Filesystem::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 70 ▶use Badger::Filesystem::Directory;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 #-----------------------------------------------------------------------
71 # special export hooks to make $Bin available from FindBin
72 #-----------------------------------------------------------------------
73  
74 sub _export_findbin_hook {
75 2 ▶ my ($class, $target) = @_;
Subroutine _export_findbin_hook: called CC=1 SCAR=0.0
76 2 class($class->FINDBIN)->load;
77 2 $class->export_symbol($target, Bin => \$FindBin::Bin);
78 };
79  
80  
81 #-----------------------------------------------------------------------
82 # aliases
83 #-----------------------------------------------------------------------
84  
85 70 *DIR = \&DIRECTORY; # constant class name
86 70 *Dir = \&Directory; # constructor sub
87 70 *dir = \&directory; # object method
88 70 *split_dir = \&split_directory; # ...because typing 'directory'
89 70 *join_dir = \&join_directory; # gets tedious quickly
90 70 *collapse_dir = \&collapse_directory;
91 70 *dir_exists = \&directory_exists;
92 70 *create_dir = \&create_directory;
93 70 *delete_dir = \&delete_directory;
94 70 *open_dir = \&open_directory;
95 70 *read_dir = \&read_directory;
96 70 *temp_dir = \&temp_directory;
97 70 *dir_child = \&directory_child;
98 70 *dir_children = \&directory_children;
99 70 *mkdir = \&create_directory;
100 70 *rmdir = \&delete_directory;
101 70 *touch = \&touch_file;
102  
103  
104 #-----------------------------------------------------------------------
105 # In this base class definitive paths are the same as absolute paths.
106 # However, in subclasses (like Badger::Filesystem::Virtual) we want
107 # to differentiate between absolute paths in a virtual filesystem
108 # (e.g. /about/badger.html) and the definitive paths that they map to
109 # in a real file system (e.g. /home/abw/web/badger/about/badger.html).
110 # We make the further distinction between definitive paths used for
111 # reading or writing, and call the appropriate method to perform any
112 # virtual -> real mapping before operating on any file or directory.
113 # But like I said, these are just hooks for subclasses to use if they
114 # need them. In the base class, we patch them straight into the plain
115 # old absolute() method.
116 #-----------------------------------------------------------------------
117  
118 70 *definitive = \&absolute;
119 70 *definitive_read = \&absolute;
120 70 *definitive_write = \&absolute;
121  
122  
123 #-----------------------------------------------------------------------
124 # factory subroutines
125 #-----------------------------------------------------------------------
126  
127 sub Path {
128 58 ▶ return PATH unless @_;
Subroutine Path: called CC=4 SCAR=14.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 30 28 58
129 28 ▶ return @_ == 1 && is_object(PATH, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && &is_object('Badger::Filesystem::Path', $_[0]) ? : 0 28 28
Condition66%2/3
@_ == 1 and &is_object("Badger::Filesystem::Path", $_[0])
!ll&&!rl&&r
1270
MC/DC0%0/2
@_ == 1 and &is_object("Badger::Filesystem::Path", $_[0])
@_ == 1 &is_object("Badger::Filesystem::Path", $_[0])
Truth table66%2/3
A and B
A= @_ == 1B= &is_object("Badger::Filesystem::Path", $_[0])
ABresult
0X0
100
111
130 ? $_[0] # return existing Path object
131 : FS->path(@_); # or construct a new one
132 }
133  
134 sub File {
135 291 ▶ return FILE unless @_;
Subroutine File: called CC=4 SCAR=14.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 281 10 291
136 10 ▶ return @_ == 1 && is_object(FILE, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && &is_object('Badger::Filesystem::File', $_[0]) ? : 0 10 10
Condition66%2/3
@_ == 1 and &is_object("Badger::Filesystem::File", $_[0])
!ll&&!rl&&r
190
MC/DC0%0/2
@_ == 1 and &is_object("Badger::Filesystem::File", $_[0])
@_ == 1 &is_object("Badger::Filesystem::File", $_[0])
Truth table66%2/3
A and B
A= @_ == 1B= &is_object("Badger::Filesystem::File", $_[0])
ABresult
0X0
100
111
137 ? $_[0] # ditto for File object
138 : FS->file(@_);
139 }
140  
141 sub Directory {
142 246 ▶ return DIRECTORY unless @_;
Subroutine Directory: called CC=4 SCAR=14.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 234 12 246
143 12 ▶ return @_ == 1 && is_object(DIRECTORY, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && &is_object('Badger::Filesystem::Directory', $_[0]) ? : 0 12 12
Condition66%2/3
@_ == 1 and &is_object("Badger::Filesystem::Directory", $_[0])
!ll&&!rl&&r
480
MC/DC0%0/2
@_ == 1 and &is_object("Badger::Filesystem::Directory", $_[0])
@_ == 1 &is_object("Badger::Filesystem::Directory", $_[0])
Truth table66%2/3
A and B
A= @_ == 1B= &is_object("Badger::Filesystem::Directory", $_[0])
ABresult
0X0
100
111
144 ? $_[0] # ditto for Directory object
145 : FS->directory(@_);
146 }
147  
148 sub Cwd {
149 3 ▶ FS->directory
Subroutine Cwd: called CC=1 SCAR=0.0
Pod documented
150 }
151  
152 sub Bin {
153 17 ▶ class(FINDBIN)->load;
Subroutine Bin: called CC=1 SCAR=0.0
Pod documented
154 17 FS->directory($FindBin::Bin);
155 }
156  
157  
158 #-----------------------------------------------------------------------
159 # generated methods
160 #-----------------------------------------------------------------------
161  
162 class->methods(
163 # define methods for path/root/updir/curdir that access a prototype
164 # object when called as class methods.
165 map {
166 70 my $name = $_; # fresh copy of lexical for binding in closure
167 $name => sub {
168 4 ▶ $_[0]->prototype->{ $name };
Subroutine __ANON__: called CC=1 SCAR=0.0
169 }
170 280 }
171 qw( rootdir updir curdir separator )
172 );
173  
174  
175 #-----------------------------------------------------------------------
176 # constructor methods
177 #-----------------------------------------------------------------------
178  
179 sub init {
180 26 ▶ my ($self, $config) = @_;
Subroutine init: called CC=8 SCAR=24.5
Pod documented
181  
182 # NEW CODE: trying to abstract out the file specification so that I
183 # can slot in a Universal file spec decoy which always generates URIs
184 my $spec = $self->{ spec }
185 = $config->{ spec }
186 || $config->{ filespec }
187 26 ▶ || $self->FILESPEC;
Condition33%2/6
$config->{'spec'} || $config->{'filespec'} || $self->FILESPEC
l!l&&r!l&&!r
0260
$config->{"spec"} or $config->{"filespec"}
l!l&&r!l&&!r
0026
MC/DC0%0/3
$config->{'spec'} || $config->{'filespec'} || $self->FILESPEC
$config->{"spec"} $config->{"filespec"} $self->FILESPEC
Truth table25%1/4
A or B || C
A= $config->{"spec"}B= $config->{"filespec"}C= $self->FILESPEC
ABCresult
1XX1
01X1
0011
0000
188
189 26 $self->debug("spec is $spec") if DEBUG;
190
191 # The tokens used to represent the root directory ('/'), the
192 # parent directory ('..') and current directory ('.') default to
193 # constants grokked from File::Spec. To determine the path separator
194 # we have to resort to an ugly hack. The File::Spec module hard-codes
195 # the path separator in the catdir() method so we have to make a round-
196 # trip through catdir() to grok the separator in a cross-platform manner
197 26 ▶ $self->{ rootdir } = $config->{ rootdir } || $spec->rootdir;
Condition33%1/3
$config->{'rootdir'} || $spec->rootdir
l!l&&r!l&&!r
0260
MC/DC0%0/2
$config->{'rootdir'} || $spec->rootdir
$config->{'rootdir'} $spec->rootdir
Truth table33%1/3
A || B
A= $config->{'rootdir'}B= $spec->rootdir
ABresult
1X1
011
000
198 26 ▶ $self->{ updir } = $config->{ updir } || $spec->updir;
Condition33%1/3
$config->{'updir'} || $spec->updir
l!l&&r!l&&!r
0260
MC/DC0%0/2
$config->{'updir'} || $spec->updir
$config->{'updir'} $spec->updir
Truth table33%1/3
A || B
A= $config->{'updir'}B= $spec->updir
ABresult
1X1
011
000
199 26 ▶ $self->{ curdir } = $config->{ curdir } || $spec->curdir;
Condition33%1/3
$config->{'curdir'} || $spec->curdir
l!l&&r!l&&!r
0260
MC/DC0%0/2
$config->{'curdir'} || $spec->curdir
$config->{'curdir'} $spec->curdir
Truth table33%1/3
A || B
A= $config->{'curdir'}B= $spec->curdir
ABresult
1X1
011
000
200 26 ▶ $self->{ separator } = $config->{ separator } || do {
Condition33%1/3
$config->{'separator'} || do { my $sep = 'File::Spec'->catdir(('badger') x 2); $sep =~ s/badger//g; $sep }
l!l&&r!l&&!r
0260
MC/DC0%0/2
$config->{'separator'} || do { my $sep = 'File::Spec'->catdir(('badger') x 2); $sep =~ s/badger//g; $sep }
$config->{'separator'} do { my $sep = 'File::Spec'->catdir(('badger') x 2); $sep =~ s/badger//g; $sep }
Truth table33%1/3
A || B
A= $config->{'separator'}B= do { my $sep = 'File::Spec'->catdir(('badger') x 2); $sep =~ s/badger//g; $sep }
ABresult
1X1
011
000
201 26 my $sep = FILESPEC->catdir(('badger') x 2);
202 26 $sep =~ s/badger//g;
203 26 $sep;
204 };
205  
206 # flag to indicate if directory scans should return all entries
207 26 ▶ $self->{ all_entries } = $config->{ all_entries } || 0;
Condition50%1/2
$config->{'all_entries'} || 0
l!l
026
MC/DC0%0/1
$config->{'all_entries'} || 0
$config->{'all_entries'}
Truth table50%1/2
A
A= $config->{'all_entries'}
Aresult
11
00
208  
209 # current working can be specified explicitly, otherwise we leave it
210 # undefined and let cwd() call getcwd() determine it dynamically
211 26 $self->{ cwd } = $config->{ cwd };
212
213 # additional options, e.g. codec, encoding
214 26 $self->init_options($config);
215
216 26 return $self;
217 }
218  
219 sub spec {
220 return ref $_[0] eq HASH
221 ? $_[0]->{ spec }
222 3960 ▶ : FILESPEC;
Subroutine spec: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 3960 3960
223 }
224  
225 sub path {
226 29 ▶ Path->new( shift->_child_args( path => @_ ) );
Subroutine path: called CC=1 SCAR=0.0
Pod documented
227 }
228  
229 sub file {
230 280 ▶ File->new( shift->_child_args( file => @_ ) );
Subroutine file: called CC=1 SCAR=0.0
Pod documented
231 }
232  
233 sub directory {
234 232 ▶ my $self = shift;
Subroutine directory: called CC=3 SCAR=11.0
Pod documented
235 232 my $args = $self->_child_args( directory => @_ );
236
237 # default directory is the current working directory
238 $args->{ path } = $self->cwd
239 232 ▶ if exists $args->{ path } && ! defined $args->{ path };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"path"} and not defined $args->{"path"} 5 227 232
Condition66%2/3
exists $args->{"path"} and not defined $args->{"path"}
!ll&&!rl&&r
02275
MC/DC50%1/2
exists $args->{"path"} and not defined $args->{"path"}
exists $args->{"path"} not defined $args->{"path"}
Truth table66%2/3
A and not(B)
A= exists $args->{"path"}B= not defined $args->{"path"}
ABresult
0X0
100
111
240
241 232 Directory->new($args);
242 }
243  
244 sub root {
245 1 ▶ my $self = shift->prototype;
Subroutine root: called CC=1 SCAR=0.0
Pod documented
246 1 $self->directory($self->{ rootdir });
247 }
248  
249 sub cwd {
250 548 ▶ my $cwd;
Subroutine cwd: called CC=3 SCAR=11.0
Pod documented
251 548 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 541 7 548
252 # called as an object or class method
253 541 my $self = shift->prototype;
254 # if we have a hard-coded cwd set then return that, otherwise call
255 # getcwd to return the real current working directory. NOTE: we don't
256 # cache the dynamically resolved cwd as it'll change if chdir() is called
257 541 ▶ $cwd = $self->{ cwd } || getcwd;
Condition66%2/3
$self->{'cwd'} || getcwd()
l!l&&r!l&&!r
514900
MC/DC0%0/2
$self->{'cwd'} || getcwd()
$self->{'cwd'} getcwd()
Truth table66%2/3
A || B
A= $self->{'cwd'}B= getcwd()
ABresult
1X1
011
000
258 }
259 else {
260 # called as a subroutine
261 7 $cwd = getcwd;
262 }
263 # pass through File::Spec to sanitise path to local filesystem
264 # convention - otherwise we get /forward/slashes on Win32
265 548 FILESPEC->canonpath($cwd);
266 }
267  
268  
269 #-----------------------------------------------------------------------
270 # path manipulation methods
271 #-----------------------------------------------------------------------
272  
273 sub merge_paths {
274 99 ▶ my ($self, $base, $path) = @_;
Subroutine merge_paths: called CC=6 SCAR=23.0
Pod documented
275 99 my $spec = $self->spec;
276 99 my @p1 = $spec->splitpath($base);
277 99 my @p2 = $spec->splitpath($path);
278  
279 # check volumes match
280 99 ▶ if (defined $p2[0] and length $p2[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p2[0] and length $p2[0]) 0 99 99
Condition33%1/3
defined $p2[0] and length $p2[0]
!ll&&!rl&&r
0990
MC/DC0%0/2
defined $p2[0] and length $p2[0]
defined $p2[0] length $p2[0]
Truth table33%1/3
A and B
A= defined $p2[0]B= length $p2[0]
ABresult
0X0
100
111
281 0 ▶ $p1[0] ||= $p2[0];
Condition0%0/3
$p1[0] ||= $p2[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$p1[0] ||= $p2[0]
$p1[0] $p2[0]
Truth table0%0/3
A ||= B
A= $p1[0]B= $p2[0]
ABresult
1X1
011
000
282 0 ▶ return $self->error_msg( bad_volume => $p1[0], $p1[0] )
Branch: 1 branch
BranchTrueFalseTotal
unless $p1[0] eq $p2[0] 0 0 0
283 unless $p1[0] eq $p2[0];
284 }
285 99 shift(@p2);
286 99 ▶ my $vol = shift(@p1) || '';
Condition50%1/2
shift @p1 || ''
l!l
099
MC/DC0%0/1
shift @p1 || ''
shift @p1
Truth table50%1/2
A
A= shift @p1
Aresult
11
00
287 99 my $file = pop @p2;
288
289 99 $spec->catpath($vol, $spec->catdir(@p1, @p2), $file);
290 }
291
292 sub join_path {
293 6 ▶ my $self = shift;
Subroutine join_path: called CC=2 SCAR=7.0
Pod documented
294 6 ▶ my @args = map { defined($_) ? $_ : '' } @_[0..2];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 18 0 18
295 6 my $spec = $self->spec;
296 6 $spec->canonpath( $spec->catpath(@args) );
297 }
298  
299 sub join_directory {
300 2454 ▶ my $self = shift;
Subroutine join_directory: called CC=5 SCAR=16.7
Pod documented
301 2454 ▶ my $dir = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1981 473 2454
302 2454 my $spec = $self->spec;
303 2454 ▶ $self->debug("join_dir(", ref $dir eq ARRAY ? '[' . join(', ', @$dir) . ']' : $dir, ")\n") if $DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
if $Badger::Filesystem::DEBUG 0 2454 2454
ref $dir eq 'ARRAY' ? : 0 0 0
304 2454 ▶ ref $dir eq ARRAY
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 503 1951 2454
305 ? $spec->catdir(@$dir)
306 : $spec->canonpath($dir);
307 }
308  
309 sub split_path {
310 527 ▶ my $self = shift;
Subroutine split_path: called CC=4 SCAR=14.5
Pod documented
311 527 my $path = $self->join_directory(@_);
312 527 ▶ my @split = map { defined($_) ? $_ : '' } $self->spec->splitpath($path);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1581 0 1581
313 527 ▶ $self->debug("split_path($path) => ", join(', ', @split), "\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::DEBUG 0 527 527
314 527 ▶ return wantarray ? @split : \@split;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 527 0 527
315 }
316  
317 sub split_directory {
318 95 ▶ my $self = shift;
Subroutine split_directory: called CC=2 SCAR=6.9
Pod documented
319 95 my $path = $self->join_directory(@_);
320 95 my @split = $self->spec->splitdir($path);
321 95 ▶ return wantarray ? @split : \@split;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 75 20 95
322 }
323  
324 sub collapse_directory {
325 75 ▶ my $self = shift->prototype;
Subroutine collapse_directory: called CC=5 SCAR=16.2
Pod documented
326 75 my @dirs = $self->split_directory(shift);
327 75 my ($up, $cur) = @$self{qw( updir curdir )};
328 75 my ($node, @path);
329 75 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 471 75 546
330 471 $node = shift @dirs;
331 471 ▶ if ($node eq $cur) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node eq $cur) { } 0 471 471
elsif ($node eq $up) { } 20 451 471
332 # do nothing
333 }
334 elsif ($node eq $up) {
335 20 ▶ pop @path if @path;
Branch: 1 branch
BranchTrueFalseTotal
if @path 20 0 20
336 }
337 else {
338 451 push(@path, $node);
339 }
340 }
341 75 $self->join_directory(@path);
342 }
343  
344 sub slash_directory {
345 4 ▶ my $self = shift->prototype;
Subroutine slash_directory: called CC=3 SCAR=11.1
Pod documented
346 4 my $path = $self->absolute(shift);
347 4 ▶ my $slash = $self->{ slashed } ||= do {
Condition66%2/3
$self->{"slashed"} ||= do { my $sep = quotemeta $self->{"separator"}; qr/$sep$/ }
l!l&&r!l&&!r
220
MC/DC0%0/2
$self->{"slashed"} ||= do { my $sep = quotemeta $self->{"separator"}; qr/$sep$/ }
$self->{"slashed"} do { my $sep = quotemeta $self->{"separator"}; qr/$sep$/ }
Truth table66%2/3
A ||= B
A= $self->{"slashed"}B= do { my $sep = quotemeta $self->{"separator"}; qr/$sep$/ }
ABresult
1X1
011
000
348 2 my $sep = quotemeta $self->{ separator };
349 2 qr/$sep$/;
350 };
351 4 ▶ $path .= $self->{ separator } unless $path =~ $slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$slash/ 4 0 4
352 4 return $path;
353 }
354  
355  
356 #-----------------------------------------------------------------------
357 # absolute and relative path tests and transmogrifiers
358 #-----------------------------------------------------------------------
359  
360 sub is_absolute {
361 104 ▶ my $self = shift;
Subroutine is_absolute: called CC=2 SCAR=6.9
Pod documented
362 # $self->debug("args: ", $self->dump_data(\@_));
363 104 ▶ $self->spec->file_name_is_absolute(
Branch: 1 branch
BranchTrueFalseTotal
$self->spec->file_name_is_absolute($self->join_directory(@_)) ? : 22 82 104
364 $self->join_directory(@_)
365 ) ? 1 : 0;
366 }
367  
368 sub is_relative {
369 1 ▶ shift->is_absolute(@_) ? 0 : 1;
Subroutine is_relative: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->is_absolute(@_) ? : 0 1 1
370 }
371  
372 sub absolute {
373 608 ▶ my $self = shift;
Subroutine absolute: called CC=3 SCAR=11.2
Pod documented
374 608 my $path = $self->join_directory(shift);
375 608 my $spec = $self->spec;
376 608 ▶ return $path if $spec->file_name_is_absolute($path);
Branch: 1 branch
BranchTrueFalseTotal
if $spec->file_name_is_absolute($path) 80 528 608
377 528 ▶ $spec->catdir(shift || $self->cwd, $path);
Condition33%1/3
shift() || $self->cwd
l!l&&r!l&&!r
05280
MC/DC0%0/2
shift() || $self->cwd
shift() $self->cwd
Truth table33%1/3
A || B
A= shift()B= $self->cwd
ABresult
1X1
011
000
378 }
379  
380 sub relative {
381 1 ▶ my $self = shift;
Subroutine relative: called CC=2 SCAR=8.1
Pod documented
382 1 ▶ $self->spec->abs2rel($self->join_directory(shift), shift || $self->cwd);
Condition33%1/3
shift() || $self->cwd
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->cwd
shift() $self->cwd
Truth table33%1/3
A || B
A= shift()B= $self->cwd
ABresult
1X1
011
000
383 }
384  
385  
386 #-----------------------------------------------------------------------
387 # file/directory test methods
388 #-----------------------------------------------------------------------
389  
390 sub path_exists {
391 0 ▶ shift->stat_path(@_);
Subroutine path_exists: not called CC=1 SCAR=6.9
Pod documented
392 }
393  
394 sub file_exists {
395 71 ▶ my $self = shift;
Subroutine file_exists: called CC=3 SCAR=11.0
Pod documented
396 71 ▶ my $stats = $self->stat_path(shift) || return;
Condition100%2/2
$self->stat_path(shift()) || (return)
l!l
692
MC/DC100%1/1
$self->stat_path(shift()) || (return)
$self->stat_path(shift())
Truth table100%2/2
A
A= $self->stat_path(shift())
Aresult
11
00
397 69 ▶ return -f _ ? $stats : 0; # relies on cached stat
Branch: 1 branch
BranchTrueFalseTotal
-f _ ? : 59 10 69
398 }
399  
400 sub directory_exists {
401 38 ▶ my $self = shift;
Subroutine directory_exists: called CC=3 SCAR=11.0
Pod documented
402 38 ▶ my $stats = $self->stat_path(shift) || return;
Condition100%2/2
$self->stat_path(shift()) || (return)
l!l
353
MC/DC100%1/1
$self->stat_path(shift()) || (return)
$self->stat_path(shift())
Truth table100%2/2
A
A= $self->stat_path(shift())
Aresult
11
00
403 35 ▶ return -d _ ? $stats : 0; # relies on cached stat
Branch: 1 branch
BranchTrueFalseTotal
-d _ ? : 32 3 35
404 }
405  
406 sub stat_path {
407 147 ▶ my $self = shift;
Subroutine stat_path: called CC=4 SCAR=14.1
Pod documented
408 147 ▶ my $path = $self->definitive_read(shift) || return;
Condition100%2/2
$self->definitive_read(shift()) || (return)
l!l
1425
MC/DC100%1/1
$self->definitive_read(shift()) || (return)
$self->definitive_read(shift())
Truth table100%2/2
A
A= $self->definitive_read(shift())
Aresult
11
00
409 142 my @stats = (stat($path), -r _, -w _, -x _, -o _, $path);
410  
411 return $self->error_msg( bad_stat => $self->{ path } )
412 142 ▶ unless @stats;
Branch: 1 branch
BranchTrueFalseTotal
unless @stats 0 142 142
413  
414 return wantarray
415 ? @stats
416 142 ▶ : \@stats;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 142 142
417 }
418  
419 sub chmod_path {
420 1 ▶ my $self = shift;
Subroutine chmod_path: called CC=1 SCAR=0.0
Pod documented
421 1 my $path = $self->definitive_write(shift);
422 1 chmod(shift, $path);
423 }
424  
425  
426 #-----------------------------------------------------------------------
427 # file manipulation methods
428 #-----------------------------------------------------------------------
429  
430 sub create_file {
431 2 ▶ my ($self, $path) = @_;
Subroutine create_file: called CC=2 SCAR=7.0
Pod documented
432 2 ▶ unless (-e $self->definitive_write($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->definitive_write($path)) 2 0 2
433 2 $self->write_file($path); # calls definitive_write again
434 }
435 2 return 1;
436 }
437  
438 sub touch_file {
439 2 ▶ my ($self, $path) = @_;
Subroutine touch_file: called CC=2 SCAR=6.9
Pod documented
440 2 my $definitive = $self->definitive_write($path);
441 2 ▶ if (-e $definitive) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $definitive) { } 1 1 2
442 1 my $now = time();
443 1 utime $now, $now, $definitive;
444 }
445 else {
446 1 $self->write_file($path); # calls definitive_write again
447 }
448 }
449  
450 sub delete_file {
451 11 ▶ my $self = shift;
Subroutine delete_file: called CC=2 SCAR=7.1
Pod documented
452 11 my $path = $self->definitive_write(shift);
453 11 ▶ unlink($path)
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or return $self->error_msg("delete_failed", "file", $path, $!) 0 11 11
454 || return $self->error_msg( delete_failed => file => $path => $! );
455 }
456  
457 sub open_file {
458 47 ▶ my $self = shift;
Subroutine open_file: called CC=10 SCAR=25.7
Pod documented
459 47 my $name = shift;
460 47 ▶ my $mode = $_[0] || 'r'; # leave it in @_ for IO::File
Condition50%1/2
$_[0] || 'r'
l!l
470
MC/DC0%0/1
$_[0] || 'r'
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
461 47 ▶ my $opts = @_ && ref $_[-1] eq HASH ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 47 0 47
Condition33%1/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
0047
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
462 47 ▶ my $path = $mode eq 'r'
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'r' ? : 27 20 47
463 ? $self->definitive_read($name)
464 : $self->definitive_write($name);
465 47 ▶ return $self->error_msg( no_path => $name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 47 47
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
0047
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
466 unless defined $path && length $path;
467  
468 47 require IO::File;
469 47 ▶ $self->debug("about to open file $path (", join(', ', @_), ")\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::DEBUG 0 47 47
470  
471 47 ▶ my $fh = IO::File->new($path, @_)
Condition33%1/3
'IO::File'->new($path, @_) || $self->error_msg('open_failed', 'file', $path, $!)
l!l&&r!l&&!r
4700
MC/DC0%0/2
'IO::File'->new($path, @_) || $self->error_msg('open_failed', 'file', $path, $!)
'IO::File'->new($path, @_) $self->error_msg('open_failed', 'file', $path, $!)
Truth table33%1/3
A || B
A= 'IO::File'->new($path, @_)B= $self->error_msg('open_failed', 'file', $path, $!)
ABresult
1X1
011
000
472 || $self->error_msg( open_failed => file => $path => $! );
473  
474 $fh->binmode( $opts->{ encoding } )
475 47 ▶ if $opts->{ encoding };
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"encoding"} 2 45 47
476  
477 47 return $fh;
478 }
479  
480 sub read_file {
481 27 ▶ my $self = shift;
Subroutine read_file: called CC=4 SCAR=14.3
Pod documented
482 27 ▶ my $opts = @_ && ref $_[-1] eq HASH ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 27 0 27
Condition33%1/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
0027
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
483 27 my $fh = $self->open_file(shift, 'r', $opts);
484 return wantarray
485 ? <$fh>
486 27 ▶ : do { local $/ = undef; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 25 27
487 }
488  
489 sub write_file {
490 15 ▶ my $self = shift;
Subroutine write_file: called CC=4 SCAR=13.9
Pod documented
491 15 ▶ my $opts = @_ && ref $_[-1] eq HASH ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 12 3 15
Condition66%2/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
0312
MC/DC50%1/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
492 15 my $fh = $self->open_file(shift, 'w', $opts);
493 15 ▶ return $fh unless @_; # return handle if no args
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 11 15
494 11 print $fh @_; # or print args and close
495 11 $fh->close;
496 11 return 1;
497 }
498  
499 sub append_file {
500 2 ▶ my $self = shift;
Subroutine append_file: called CC=4 SCAR=14.2
Pod documented
501 2 ▶ my $opts = @_ && ref $_[-1] eq HASH ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 2 0 2
Condition33%1/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
502 2 my $fh = $self->open_file(shift, 'a', $opts);
503 2 ▶ return $fh unless @_; # return handle if no args
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 1 2
504 1 print $fh @_; # or print args and close
505 1 $fh->close;
506 1 return 1;
507 }
508  
509  
510 sub copy_file {
511 3 ▶ shift->_file_copy( copy => @_ );
Subroutine copy_file: called CC=1 SCAR=0.0
Pod documented
512 }
513  
514 sub move_file {
515 1 ▶ shift->_file_copy( move => @_ );
Subroutine move_file: called CC=1 SCAR=0.0
Pod documented
516 }
517  
518 sub _file_copy {
519 4 ▶ require File::Copy;
Subroutine _file_copy: called CC=16 SCAR=30.8
520  
521 4 my ($self, $action, $from, $to, $params)
522 = (shift, shift, shift, shift, params(@_));
523
524 4 ▶ my $src
Branch: 2 branches
BranchTrueFalseTotal
&is_object('Badger::Filesystem::Path', $from) ? : 0 4 4
ref $from ? : 1 3 4
525 = is_object(PATH, $from) ? $from->definitive # path object
526 : ref($from) ? $from # file handle
527 : $self->definitive_read($from); # file path
528  
529 4 ▶ my $dest
Branch: 2 branches
BranchTrueFalseTotal
&is_object('Badger::Filesystem::Path', $to) ? : 3 1 4
ref $to ? : 0 1 1
530 = is_object(PATH, $to) ? $to->definitive # as above
531 : ref($to) ? $to
532 : $self->definitive_write($to);
533
534 4 ▶ my $code
Branch: 2 branches
BranchTrueFalseTotal
$action eq 'copy' ? : 3 1 4
$action eq 'move' ? : 1 0 1
535 = $action eq 'copy' ? \&File::Copy::copy
536 : $action eq 'move' ? \&File::Copy::move
537 : return $self->error( invalid => action => $action );
538  
539 4 my $file;
540  
541 4 ▶ unless (ref $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dest) 4 0 4
542 # NOTE: don't use $self->file($dest) because $self could be a
543 # VFS and $dest is already a definitive path
544 4 $file = File($dest);
545 # capture our current working directory
546 4 my $cwd = cwd;
547 eval {
548 # Change to the destination volume if one exists.
549 # Should work for any volume except Windows shares
550 # where resulting behavior is version dependent.
551 4 ▶ chdir $file->volume if ($file->volume);
Branch: 1 branch
BranchTrueFalseTotal
if $file->volume 0 4 4
552 # this code strips volume information
553 $file->directory->must_exist(
554 $params->{ mkdir },
555 $params->{ dir_mode },
556 4 );
557 # change back to the current working directory
558 4 chdir $cwd;
559 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { chdir $file->volume if $file->volume; $file->directory->must_exist($params->{"mkdir"}, $params->{"dir_mode"}); chdir $cwd } } or do { if ($@) { chdir $cwd; die $@; } } 0 4 4
560 # capture any exception from above
561 # change back to the oringial cwd
562 # and rethrow the execption.
563 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
564 0 chdir $cwd;
565 0 die $@;
566 }
567 }
568 }
569  
570 4 ▶ $code->($src, $dest)
Branch: 1 branch
BranchTrueFalseTotal
&$code($src, $dest) or return $self->error_msg("copy_failed", $action, $from, $to, $!) 0 4 4
571 || return $self->error_msg( copy_failed => $action, $from, $to, $! );
572  
573 4 my $mode = $params->{ file_mode };
574 4 ▶ $mode = $params->{ mode } unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 3 1 4
575  
576 4 ▶ $file->chmod($mode)
Branch: 1 branch
BranchTrueFalseTotal
if $file and defined $mode 1 3 4
Condition66%2/3
$file and defined $mode
!ll&&!rl&&r
031
MC/DC50%1/2
$file and defined $mode
$file defined $mode
Truth table66%2/3
A and B
A= $fileB= defined $mode
ABresult
0X0
100
111
577 if $file && defined $mode;
578  
579 4 ▶ return $file || $dest;
Condition33%1/3
$file || $dest
l!l&&r!l&&!r
400
MC/DC0%0/2
$file || $dest
$file $dest
Truth table33%1/3
A || B
A= $fileB= $dest
ABresult
1X1
011
000
580 }
581  
582  
583 #-----------------------------------------------------------------------
584 # directory manipulation methods
585 #-----------------------------------------------------------------------
586  
587 sub create_directory {
588 5 ▶ my $self = shift;
Subroutine create_directory: called CC=2 SCAR=7.0
Pod documented
589 5 my $path = $self->definitive_write(shift);
590  
591 5 require File::Path;
592  
593 5 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $Carp::CarpLevel = 1; File::Path::mkpath($path, 0, @_) } } or return $self->error($@) 0 5 5
594 5 local $Carp::CarpLevel = 1;
595 5 File::Path::mkpath($path, 0, @_)
596 } || return $self->error($@);
597 }
598
599 sub delete_directory {
600 3 ▶ my $self = shift;
Subroutine delete_directory: called CC=1 SCAR=0.0
Pod documented
601 3 my $path = $self->definitive_write(shift);
602  
603 3 require File::Path;
604 3 File::Path::rmtree($path, @_)
605 }
606  
607 sub open_directory {
608 67 ▶ my $self = shift;
Subroutine open_directory: called CC=3 SCAR=11.8
Pod documented
609 67 my $path = $self->definitive_read(shift);
610  
611 67 require IO::Dir;
612 67 ▶ $self->debug("Opening directory: $path\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::DEBUG 0 67 67
613  
614 67 ▶ return IO::Dir->new($path, @_)
Condition33%1/3
'IO::Dir'->new($path, @_) || $self->error_msg('open_failed', 'directory', $path, $!)
l!l&&r!l&&!r
6700
MC/DC0%0/2
'IO::Dir'->new($path, @_) || $self->error_msg('open_failed', 'directory', $path, $!)
'IO::Dir'->new($path, @_) $self->error_msg('open_failed', 'directory', $path, $!)
Truth table33%1/3
A || B
A= 'IO::Dir'->new($path, @_)B= $self->error_msg('open_failed', 'directory', $path, $!)
ABresult
1X1
011
000
615 || $self->error_msg( open_failed => directory => $path => $! );
616 }
617  
618 sub read_directory {
619 67 ▶ my $self = shift;
Subroutine read_directory: called CC=6 SCAR=18.3
Pod documented
620 67 my $dirh = $self->open_directory(shift);
621 67 my $all = shift;
622 67 my ($path, @paths);
623 67 ▶ while (defined ($path = $dirh->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($path = $dirh->read)) 420 67 487
624 420 push(@paths, $path);
625 }
626 @paths = $self->spec->no_upwards(@paths)
627 67 ▶ unless $all || ref $self && $self->{ all_entries };
Branch: 1 branch
BranchTrueFalseTotal
unless $all or ref $self and $self->{"all_entries"} 66 1 67
Condition50%3/6
$all or ref $self and $self->{"all_entries"}
l!l&&r!l&&!r
1066
ref $self and $self->{"all_entries"}
!ll&&!rl&&r
0660
MC/DC33%1/3
$all or ref $self and $self->{"all_entries"}
$all ref $self $self->{"all_entries"}
Truth table50%2/4
A or B and C
A= $allB= ref $selfC= $self->{"all_entries"}
ABCresult
1XX1
0111
00X0
0100
628  
629 67 $dirh->close;
630 67 ▶ return wantarray ? @paths : \@paths;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 67 0 67
631 }
632  
633 sub directory_child {
634 292 ▶ my $self = shift;
Subroutine directory_child: called CC=3 SCAR=11.0
Pod documented
635 292 my $path = $self->join_directory(@_);
636 292 stat $self->definitive_read($path);
637 292 ▶ -d _ ? $self->directory($path) :
Branch: 2 branches
BranchTrueFalseTotal
-d _ ? : 68 224 292
-f _ ? : 224 0 224
638 -f _ ? $self->file($path) :
639 $self->path($path);
640 }
641
642 sub directory_children {
643 68 ▶ my $self = shift;
Subroutine directory_children: called CC=2 SCAR=7.0
Pod documented
644 68 my $dir = shift;
645 my @paths = map {
646 68 $self->directory_child($dir, $_)
647 } $self->read_directory($dir, @_);
648 68 ▶ return wantarray ? @paths : \@paths;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 68 0 68
649 }
650  
651  
652 #-----------------------------------------------------------------------
653 # temporary directory/file methods
654 #-----------------------------------------------------------------------
655  
656 sub temp_directory {
657 3 ▶ my $self = shift;
Subroutine temp_directory: called CC=1 SCAR=0.0
Pod documented
658 3 return $self->directory( FILESPEC->tmpdir, @_ )->must_exist(1);
659 }
660  
661 sub temp_file {
662 1 ▶ my $self = shift;
Subroutine temp_file: called CC=2 SCAR=7.2
Pod documented
663 1 ▶ return $self->temp_directory->file( @_ ? @_ : random_name() )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
664 }
665  
666  
667 #-----------------------------------------------------------------------
668 # visitor methods
669 #-----------------------------------------------------------------------
670  
671 sub visitor {
672 14 ▶ my $self = shift;
Subroutine visitor: called CC=3 SCAR=11.0
Pod documented
673 14 my $vtype = $self->VISITOR;
674 14 class($vtype)->load;
675
676 14 ▶ return @_ && is_object($vtype => $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ && &is_object($vtype, $_[0]) ? : 1 13 14
Condition100%3/3
@_ and &is_object($vtype, $_[0])
!ll&&!rl&&r
1121
MC/DC100%2/2
@_ and &is_object($vtype, $_[0])
@_ &is_object($vtype, $_[0])
Truth table100%3/3
A and B
A= @_B= &is_object($vtype, $_[0])
ABresult
0X0
100
111
677 ? shift
678 : $vtype->new(@_);
679 }
680  
681 sub visit {
682 0 ▶ shift->root->visit(@_);
Subroutine visit: not called CC=1 SCAR=6.9
Pod documented
683 }
684  
685 sub collect {
686 0 ▶ shift->visit(@_)->collect;
Subroutine collect: not called CC=1 SCAR=6.9
Pod documented
687 }
688  
689 sub accept {
690 0 ▶ shift->root->accept(@_);
Subroutine accept: not called CC=1 SCAR=6.9
Pod documented
691 }
692  
693 #-----------------------------------------------------------------------
694 # internal methods
695 #-----------------------------------------------------------------------
696  
697 sub _child_args {
698 541 ▶ my $self = shift->prototype;
Subroutine _child_args: called CC=5 SCAR=16.1
699 541 my $type = shift;
700 541 my $args = { %{ $self->{ options } } };
701  
702 541 ▶ if (@_ && ref $_[-1] eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[-1] eq "HASH") 76 465 541
Condition100%3/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
546076
MC/DC100%2/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
703 76 my $more = pop @_;
704 76 @$args{ keys %$more } = values %$more;
705 }
706  
707 541 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 11 530 541
elsif (@_ == 1) { } 525 5 530
708 11 $args->{ path } = [@_];
709 }
710 elsif (@_ == 1) {
711 525 $args->{ path } = shift;
712 }
713 else {
714 5 $args->{ path } = undef;
715 }
716  
717 541 $args->{ filesystem } = $self;
718 541 return $args;
719 }
720  
721  
722  
723 70 1;
724  
725 __END__
« blib/lib/Badger/Rainbow.pm ↑ Summary lib/Badger/Exception.pm »