System/Layered.pm

filter: Statementstmt 88.2% 188 / 213 Branchbran 52.0% 25 / 48 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 32 / 34 Podpod 100% 29 / 29 totaltotal 83.8% 276 / 329 CC 45 SCAR 13.7
CC
45
Cov
81%
is_creatable
12.0
copy
6.8
create
6.8
CRAP 3.9
« System/Real.pm ↑ Summary System.pm »
1 package File::System::Layered;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base 'File::System::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.16';
12  
13 =head1 NAME
14  
15 File::System::Layered - A file system implementation with "layered" roots
16  
17 =head1 SYNOPSIS
18  
19 use File::System;
20  
21 my $root = File::System->new('Layered',
22 [ 'Real', root => '/usr/local' ],
23 [ 'Real', root => '/usr' ],
24 [ 'Real', root => '/cw/usr/local' ],
25 [ 'Real', root => '/sw/usr/local' ],
26 );
27  
28 my $dir = $root->lookup('/bin');
29 print "All files:\n";
30 print map({ " - $_\n" } $root->children_paths);
31  
32 =head1 DESCRIPTION
33  
34 This file system allows for the layering of other file systems. A layerd file system contains one or more other file systems such that the list of files available at a certain path in the tree is the union of the files available in all the contained file systems. When reading from or writing to file content, the file system with the highest priority is given preference.
35  
36 The priority of the file systems is determined during construction, and may be modified later.
37  
38 =head2 LAYERED API
39  
40 The constructor of this module provides the initial layer prioritization. The C<File::System::Layered> package also provides methods for altering the layers after the file system has been established.
41  
42 =over
43  
44 =item $root = File::System-E<gt>new('Layered', @file_systems)
45  
46 The constructor establishes the initial layout of the file system. Each element of C<@file_systems> is either a file system object or is a reference to an array that may be passed to C<File::System::new> to construct a file system object.
47  
48 The layers are prioritized by the order given in C<@file_systems>. The file systems listed first are given the higher priority.
49  
50 =cut
51  
52 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
53 1 my $class = shift;
54  
55 @_
56 1 ▶ or croak "No file systems given.";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("No file systems given.") 1 0 1
57  
58 1 my $self = bless { }, $class;
59  
60 1 $self->set_layers(@_);
61  
62 1 $self->{here} = $self->{layers}[0];
63  
64 1 return $self;
65 }
66  
67 =item @layers = $obj-E<gt>get_layers
68  
69 Returns the list of the file system layers in descending order of priority. By using this method to get the list of layers, they can be reordered, removed, added to and then passed back to C<set_layers> to alter the file system.
70  
71 =cut
72  
73 0 ▶sub get_layers {
Subroutine get_layers: not called CC=1 SCAR=6.9
Pod documented
74 0 my $self = shift;
75  
76 0 return @{ $self->{layers} };
77 }
78  
79 =item $obj-E<gt>set_layers(@layers)
80  
81 Reset the layers of the file system in descending order of priority. This effectively reinitializes the file system. The semantics are the same as that of the constructor.
82  
83 =cut
84  
85 1 ▶sub set_layers {
Subroutine set_layers: called CC=5 SCAR=17.8
Pod documented
86 1 my $self = shift;
87  
88 @_
89 1 ▶ or croak "No file systems given.";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("No file systems given.") 1 0 1
90  
91 1 my @layers;
92 1 for my $fs (@_) {
93
94 2 my $init_fs;
95 2 ▶ if (UNIVERSAL::isa($fs, 'File::System::Object')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($fs, 'File::System::Object')) { } 2 0 2
elsif (ref $fs eq 'ARRAY') { } 0 0 0
96 2 $init_fs = $fs;
97 } elsif (ref $fs eq 'ARRAY') {
98 0 $init_fs = File::System->new(@$fs);
99 } else {
100 0 croak "File system must be an array reference or an actual File::System::Object. '$fs' is neither of these. See the documentation of File::System::Layer for details.";
101 }
102  
103 2 push @layers, $init_fs;
104 }
105  
106 1 $self->{layers} = \@layers;
107  
108 1 return @layers;
109 }
110  
111 12 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
112 12 my $self = shift;
113  
114 return bless {
115 here => $self->{layers}[0],
116 layers => $self->{layers},
117 12 }, ref $self;
118 }
119  
120 30 ▶sub exists {
Subroutine exists: called CC=4 SCAR=14.0
Pod documented
121 30 my $self = shift;
122 30 ▶ my $path = shift || $self->path;
Condition66%2/3
shift() || $self->path
l!l&&r!l&&!r
2910
MC/DC0%0/2
shift() || $self->path
shift() $self->path
Truth table66%2/3
A || B
A= shift()B= $self->path
ABresult
1X1
011
000
123  
124 30 for my $layer (@{ $self->{layers} }) {
125 40 my $res = $layer->exists($path);
126 40 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 30 10 40
127 }
128  
129 0 return '';
130 }
131  
132 227 ▶sub lookup {
Subroutine lookup: called CC=3 SCAR=11.0
Pod documented
133 227 my $self = shift;
134 227 my $path = $self->normalize_path(shift);
135  
136 227 for my $layer (@{ $self->{layers} }) {
137 291 my $res = $layer->lookup($path);
138 return bless {
139 here => $res,
140 layers => $self->{layers},
141 291 ▶ }, ref $self if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 227 64 291
142 }
143  
144 0 return undef;
145 }
146  
147 88 ▶sub glob {
Subroutine glob: called CC=3 SCAR=11.0
Pod documented
148 88 my $self = shift;
149 88 my $glob = $self->normalize_path(shift);
150  
151 88 my %results;
152 88 for my $layer (reverse @{ $self->{layers} }) {
153 176 my @matches = $layer->glob($glob);
154 176 for my $match (@matches) {
155 96 $results{$match->path} = $match;
156 }
157 }
158  
159 return
160 88 map { bless { here => $_, layers => $self->{layers} }, ref $self }
161 sort values %results;
162 }
163  
164 88 ▶sub find {
Subroutine find: called CC=4 SCAR=13.9
Pod documented
165 88 my $self = shift;
166 88 my $want = shift;
167  
168 88 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 44 44 88
169 44 @_ = map { $self->normalize_path("$_") } @_;
170 } else {
171 44 @_ = ("$self");
172 }
173  
174 88 my %results;
175 88 for my $layer (reverse @{ $self->{layers} }) {
176 176 my @matches = $layer->find($want, @_);
177  
178 176 for my $match (@matches) {
179 96 $results{$match->path} = $match;
180 }
181 }
182  
183 return
184 88 map { bless { here => $_, layers => $self->{layers} }, ref $self }
185 sort values %results;
186 }
187  
188 0 ▶sub is_creatable {
Subroutine is_creatable: not called CC=3 SCAR=24.8
Pod documented
189 0 my $self = shift;
190 0 my $path = shift;
191 0 my $type = shift;
192  
193 0 for my $layer (@{ $self->{layers} }) {
194 0 my $res = $layer->is_creatable($path, $type);
195 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
196 }
197  
198 0 return '';
199 }
200  
201 9 ▶sub create {
Subroutine create: called CC=6 SCAR=19.2
Pod documented
202 9 my $self = shift;
203 9 my $path = shift;
204 9 my $type = shift;
205  
206 9 ▶ defined $path
Branch: 1 branch
BranchTrueFalseTotal
defined $path or croak("No path argument given.") 9 0 9
207 or croak "No path argument given.";
208  
209 9 ▶ defined $type
Branch: 1 branch
BranchTrueFalseTotal
defined $type or croak("No type argument given.") 9 0 9
210 or croak "No type argument given.";
211  
212 9 for my $layer (@{ $self->{layers} }) {
213 9 ▶ if ($layer->is_creatable($path, $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layer->is_creatable($path, $type)) 9 0 9
214 9 my $obj = $layer->create($path, $type);
215 9 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 9 0 9
216 return bless {
217 here => $obj,
218 layers => $self->{layers},
219 9 }, ref $self;
220 } else {
221 0 return undef;
222 }
223 }
224 }
225  
226 0 return undef;
227 }
228  
229 9 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=16.4
Pod documented
230 9 my $self = shift;
231  
232 9 for my $layer (@{ $self->{layers} }) {
233 18 my $obj = $layer->lookup($self->{here}->path);
234 18 ▶ next unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 18 0 18
235 0 my $res = $obj->is_valid;
236 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
237 }
238  
239 9 return '';
240 }
241  
242 185 ▶sub properties {
Subroutine properties: called CC=3 SCAR=11.0
Pod documented
243 185 my $self = shift;
244  
245 185 my %result;
246 185 for my $layer (reverse @{ $self->{layers} }) {
247 370 my @props = $layer->properties;
248 370 for my $prop (@props) {
249 6290 $result{$prop}++;
250 }
251 }
252  
253 185 return sort keys %result;
254 }
255  
256 26 ▶sub settable_properties {
Subroutine settable_properties: called CC=3 SCAR=11.0
Pod documented
257 26 my $self = shift;
258  
259 26 my %result;
260 26 for my $layer (reverse @{ $self->{layers} }) {
261 52 my @props = $layer->settable_properties;
262 52 for my $prop (@props) {
263 260 $result{$prop}++;
264 }
265 }
266  
267 26 return sort keys %result;
268 }
269  
270 4401 ▶sub get_property {
Subroutine get_property: called CC=1 SCAR=0.0
Pod documented
271 4401 my $self = shift;
272 4401 return $self->{here}->get_property(@_);
273 }
274  
275 78 ▶sub set_property {
Subroutine set_property: called CC=1 SCAR=0.0
Pod documented
276 78 my $self = shift;
277 78 $self->{here}->set_property(@_);
278 }
279  
280 18 ▶sub rename {
Subroutine rename: called CC=1 SCAR=0.0
Pod documented
281 18 my $self = shift;
282 18 $self->{here}->rename(@_);
283 }
284  
285 18 ▶sub move {
Subroutine move: called CC=4 SCAR=14.8
Pod documented
286 18 my $self = shift;
287 18 my $to = shift;
288
289 18 my $layer_to;
290 18 ▶ if (!$self->{here}->exists($to->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'here'}->exists($to->path)) { } 1 17 18
291 1 ▶ if ($self->{here}->is_creatable($to->path, 'd')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'here'}->is_creatable($to->path, 'd')) { } 1 0 1
elsif ($self->{'here'}->is_creatable($to->path, 'df')) { } 0 0 0
292 1 $layer_to = $self->{here}->create($to->path, 'd');
293 } elsif ($self->{here}->is_creatable($to->path, 'df')) {
294 0 $layer_to = $self->{here}->create($to->path, 'df');
295 } else {
296 0 croak "Move failed; no path '$to' exists in the same layer as $self.";
297 }
298 } else {
299 17 $layer_to = $self->{here}->lookup($to->path);
300 }
301  
302 18 $self->{here}->move($layer_to, @_);
303  
304 18 return $self;
305 }
306  
307 9 ▶sub copy {
Subroutine copy: called CC=4 SCAR=19.2
Pod documented
308 9 my $self = shift;
309 9 my $to = shift;
310  
311 9 my $layer_to;
312 9 ▶ if (!$self->{here}->exists($to->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'here'}->exists($to->path)) { } 0 9 9
313 0 ▶ if ($self->{here}->is_creatable($to->path, 'd')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'here'}->is_creatable($to->path, 'd')) { } 0 0 0
elsif ($self->{'here'}->is_creatable($to->path, 'df')) { } 0 0 0
314 0 $layer_to = $self->{here}->create($to->path, 'd');
315 } elsif ($self->{here}->is_creatable($to->path, 'df')) {
316 0 $layer_to = $self->{here}->create($to->path, 'df');
317 } else {
318 0 croak "Copy failed; no path '$to' exists in the same layer as $self.";
319 }
320 } else {
321 9 $layer_to = $self->{here}->lookup($to->path);
322 }
323  
324 return bless {
325 here => $self->{here}->copy($layer_to, @_),
326 layers => $self->{layers},
327 9 }, ref $self;
328 }
329  
330 18 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
331 18 my $self = shift;
332 18 $self->{here}->remove(@_);
333 }
334  
335 1 my @delegates = qw/
336 is_readable
337 is_seekable
338 is_writable
339 is_appendable
340 open
341 content
342 /;
343  
344 1 for my $name (@delegates) {
345 64 ▶ eval q(
Subroutine content: called CC=1 SCAR=0.0
Subroutine is_appendable: called CC=1 SCAR=0.0
Subroutine is_readable: called CC=1 SCAR=0.0
Subroutine is_seekable: called CC=1 SCAR=0.0
Subroutine is_writable: called CC=1 SCAR=0.0
Subroutine open: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
346 sub ).$name.q( {
347 my $self = shift;
348 return $self->{here}->).$name.q((@_);
349 }
350 );
351  
352 6 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
353 }
354  
355 10 ▶sub has_children {
Subroutine has_children: called CC=3 SCAR=11.0
Pod documented
356 10 my $self = shift;
357  
358 10 my $path = $self->path;
359 my @layers
360 20 = grep { defined }
361 20 map { $_->lookup($path) }
362 10 reverse @{ $self->{layers} };
363  
364 10 for my $layer (@layers) {
365 10 my $res = $layer->has_children;
366 10 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 6 4 10
367 }
368  
369 4 return '';
370 }
371  
372 10 ▶sub children_paths {
Subroutine children_paths: called CC=3 SCAR=11.0
Pod documented
373 10 my $self = shift;
374  
375 10 my $path = $self->path;
376  
377 10 my %results;
378 my @layers
379 20 = grep { defined }
380 20 map { $_->lookup($path) }
381 10 reverse @{ $self->{layers} };
382  
383 10 for my $layer (@layers) {
384 14 my @paths = $layer->children_paths;
385 14 for my $path (@paths) {
386 38 $results{$path}++;
387 }
388 }
389  
390 10 return sort keys %results;
391 }
392  
393 10 ▶sub children {
Subroutine children: called CC=3 SCAR=11.0
Pod documented
394 10 my $self = shift;
395  
396 10 my $path = $self->path;
397  
398 10 my %results;
399 my @layers
400 20 = grep { defined }
401 20 map { $_->lookup($path) }
402 10 reverse @{ $self->{layers} };
403  
404 10 for my $layer (@layers) {
405 14 my @children = $layer->children;
406 14 for my $child (@children) {
407 10 $results{$child->path} = $child;
408 }
409 }
410  
411 10 return map { bless { here => $_, layers => $self->{layers} }, ref $self }
412 sort values %results;
413 }
414  
415 26 ▶sub child {
Subroutine child: called CC=4 SCAR=13.9
Pod documented
416 26 my $self = shift;
417 26 my $path = $self->normalize_path(shift);
418  
419 26 my $child;
420 26 for my $layer (@{ $self->{layers} }) {
421 35 $child = $layer->lookup($path);
422 35 ▶ last if defined $child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child 25 10 35
423 }
424  
425 26 ▶ if (defined $child) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $child) { } 25 1 26
426 return bless {
427 here => $child,
428 layers => $self->{layers},
429 25 }, ref $self;
430 } else {
431 1 return undef;
432 }
433 }
434  
435 =back
436  
437 =head1 BUGS
438  
439 This list includes things that aren't always bugs, but eccentricities of the implementation forced by the the nature of the service provided. This provides an explanation for anything that might not be obvious. I've tried to make the implementations work in a simple and natural way, but a few decisions were arbitrary.
440  
441 The C<copy>, C<move>, and C<rename> methods are stuck within the file system they are in. That is, if you move, rename, or copy a file, the new file, location, or duplicate will be stored within the same layer as the original. If you attempt to move or copy to a location that exists in one layer, but not another, those methods will attempt to use C<create> to create the needed directory in the other layer. Due to these kinds of complications, these methods haven't yet been fully tested.
442  
443 Removing a file or directory might not have the expected effect. If there are two layers with the same file or directory, removal will just remove the version in the highest layer, so the file or directory will still appear to exist.
444  
445 The C<is_creatable> method returns true if I<any> layer returns true. The C<create> method uses the C<is_creatable> of each layer to find out if the file can be created and will create the file on the first layer it finds where it is true.
446  
447 The C<glob> and C<find> methods rely upon the slowish defaults. This situation could probably be improved with a little bit of effort.
448  
449 =head1 SEE ALSO
450  
451 L<File::System>, L<File::System::Object>, L<File::System::Real>, L<File::System::Table>
452  
453 =head1 AUTHOR
454  
455 Andrew Sterling Hanenkamp, E<lt>hanenkamp@users.sourceforge.netE<gt>
456  
457 =head1 COPYRIGHT AND LICENSE
458  
459 Copyright 2005 Andrew Sterling Hanenkamp. All Rights Reserved.
460  
461 This library is distributed and licensed under the same terms as Perl itself.
462  
463 =cut
464  
465 1 1
« System/Real.pm ↑ Summary System.pm »