System/Table.pm

filter: Statementstmt 93.9% 124 / 132 Branchbran 66.0% 33 / 50 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.4% 21 / 22 Podpod 93.3% 14 / 15 totaltotal 86.6% 194 / 224 CC 26 SCAR 11.7
CC
26
Cov
86%
mount
7.3
unmount
4.2
get_property
4.0
CRAP 3.2
« System.pm ↑ Summary System/Passthrough.pm »
1 package File::System::Table;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base 'File::System::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '1.15';
12  
13 =head1 NAME
14  
15 File::System::Table - A file system implementation for mounting other modules
16  
17 =head1 SYNOPSIS
18  
19 use File::System;
20  
21 my $root = File::System->new('Table',
22 '/' => [ 'Real', root => '/home/foo' ],
23 '/tmp' => [ 'Real', root => '/tmp' ],
24 '/bin' => [ 'Real', root => '/bin' ],
25 );
26  
27 my $file = $root->create('/tmp/dude', 'f');
28 my $fh = $file->open('w');
29 print $fh "Party on! Excellent!\n";
30 close $fh;
31  
32 =head1 DESCRIPTION
33  
34 This file system module allows for the creation of a tabular virtual file system. Each L<File::System::Table> is created with a root file system (at least) and then can have zero or more mounts to allow for more complicated file system handling. All mount points can be changed after the initial file system creation (except for the root, which is static).
35  
36 =head2 MOUNT POINTS
37  
38 There are a few rules regarding mount points that this system requires. This should be familiar to anyone familiar with Unix file system mounting:
39  
40 =over
41  
42 =item 1.
43  
44 The root mount point (F</>) is special and static. It cannot be unmounted except by deleting the file system object altogether.
45  
46 =item 2.
47  
48 A specific mount point cannot be mounted more than once. I.e., the following code would fail:
49  
50 $root = File::System->new('Table', '/' => [ 'Real' ]);
51 $root->mount('/tmp' => [ 'Real', root => '/tmp' ]);
52 $root->mount('/tmp' => [ 'Real', root => '/var/tmp' ]);
53 # ^^^ ERROR! Mount point already in use!
54  
55 =item 3.
56  
57 A file system may only be mounted onto existing containers. When mounting a path, the path must exist as per the already present mount table and that path must represent a container. Otherwise, an error will occur. I.e., the following code would fail:
58  
59 $root = File::System->new('Table', '/' => [ 'Real' ]);
60 $obj = $root->lookup('/foo');
61 $obj->remove('force') if defined $obj;
62 $root->mount('/foo' => [ 'Real', root => '/tmp' ]);
63 # ^^^ ERROR! Mount point does not exist!
64  
65 $root->mkfile('/foo');
66 $root->mount('/foo' => [ 'Real', root => '/tmp' ]);
67 # ^^^ ERROR! Mount point is not a container!
68  
69 =item 4.
70  
71 Any content or containers within a container that is mounted to within the parent is immediately invisible. These objects are hidden by the child mount until the file system is unmounted.
72  
73 =item 5.
74  
75 A mount point cannot be set above an existing mount point so that it would hide an existing mount. I.e., the following code would fail:
76  
77 $root = File::System->new('Table', '/' => [ 'Real' ]);
78 $obj = $root->mkdir('/foo/bar');
79 $obj->mount('/foo/bar' => [ 'Real', root => '/tmp' ]);
80 $obj->mount('/foo' => [ 'Real', root => '/var/tmp' ]);
81 # ^^^ ERROR! Mount point hides an already mounted file system!
82  
83 =item 6.
84  
85 As a corollary to the fifth principle, a mount point cannot be removed above another mount point below. If you mount one file system within another, the inner file system must be unmounted prior to unmounting the outer.
86  
87 =back
88  
89 Because of these rules it is obvious that the order in which mounting takes place is significant and will affect the outcome. As such, the root mount must always be specified first in the constructor.
90  
91 =head2 MOUNT TABLE API
92  
93 This file system module provides a constructor (duh) and a few extra methods. All other methods are given in the documentation of L<File::System::Object>.
94  
95 =over
96  
97 =item $root = File::System-E<gt>new('Table', '/' =E<gt> $fs, ...)
98  
99 The constructor establishes the initial mount table for the file system. The mount table must always contain at least one entry for the root directory (F</>). The root directory entry must always be the first entry given as well.
100  
101 Each entry is made of two elements, the path to mount to and then a reference to either a reference to the file system object responsible for files under that mount point, or an array reference that can be passed to L<File::System> to create a file system object.
102  
103 =cut
104  
105 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
106 3 my $class = shift;
107  
108 3 ▶ $_[0] eq '/'
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "/" or croak("The first mount point given must always be the root (/), but found '$_[0]' instead.") 3 0 3
109 or croak "The first mount point given must always be the root (/), but found '$_[0]' instead.";
110  
111 3 my $self = bless { cwd => '/' }, $class;
112  
113 3 ▶ while (my ($mp, $fs) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 4 3 7
114 4 $self->mount($mp, $fs);
115 }
116  
117 3 return $self;
118 }
119  
120 =item $obj-E<gt>mount($path, $fs)
121  
122 Each entry is made of two elements, the path to mount to and then a reference to either a reference to the file system object responsible for files under that mount point, or an array reference that can be passed to L<File::System> to create a file system object.
123  
124 =cut
125  
126 8 ▶sub mount {
Subroutine mount: called CC=7 SCAR=19.9
Pod documented
127 8 my $self = shift;
128 8 my $path = $self->normalize_path(shift);
129 8 my $fs = $self->_init_fs(shift);
130  
131 8 ▶ if ($path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 3 5 8
132 3 ▶ if (defined $self->{mounts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'mounts'}) { } 0 3 3
133 0 croak "The root mount point cannot be overridden.";
134 } else {
135 3 $self->{cwd_fs} = $self->{mounts}{$path} = $fs;
136 }
137 } else {
138 5 my $dir = $self->lookup($path);
139  
140 5 ▶ defined $dir
Branch: 1 branch
BranchTrueFalseTotal
defined $dir or croak("The mount point '${path}' does not exist.") 5 0 5
141 or croak "The mount point '$path' does not exist.";
142  
143 5 ▶ $dir->is_container
Branch: 1 branch
BranchTrueFalseTotal
$dir->is_container or croak("The mount point '${path}' is not a container.") 5 0 5
144 or croak "The mount point '$path' is not a container.";
145  
146 5 my @inner = grep /^$path/, keys %{ $self->{mounts} };
147 5 ▶ croak "The mount point '$inner[0]' must be unmounted before mount point '$path' may be used."
Branch: 1 branch
BranchTrueFalseTotal
if @inner 0 5 5
148 if @inner;
149  
150 5 ▶ $dir->has_children
Branch: 1 branch
BranchTrueFalseTotal
$dir->has_children and carp("Mounting on mount point '${path}' will hide some files.") 3 2 5
151 and carp "Mounting on mount point '$path' will hide some files.";
152  
153 5 $self->{mounts}{$path} = $fs;
154 }
155 }
156  
157 =item $unmounted_fs = $fs-E<gt>unmount($path)
158  
159 Unmounts the file system mounted to the given path. This method will raise an exception if the user attempts to unmount a path that has no file system mounted.
160  
161 This method returns the file system that was mounted at the given path.
162  
163 =cut
164  
165 4 ▶sub unmount {
Subroutine unmount: called CC=4 SCAR=14.2
Pod documented
166 4 my $self = shift;
167 4 my $path = $self->normalize_path(shift);
168  
169 4 ▶ $path eq '/'
Branch: 1 branch
BranchTrueFalseTotal
$path eq "/" and croak("The root mount point cannot be unmounted.") 0 4 4
170 and croak "The root mount point cannot be unmounted.";
171  
172 4 ▶ defined $self->{mounts}{$path}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"mounts"}{$path} or croak("No file system is mounted at '${path}'. Therefore it cannot be unmounted.") 4 0 4
173 or croak "No file system is mounted at '$path'. Therefore it cannot be unmounted.";
174  
175 4 my @inner = grep /^$path./, keys %{ $self->{mounts} };
176 4 ▶ croak "Mount point '$inner[0]' must be unmounted before '$path'"
Branch: 1 branch
BranchTrueFalseTotal
if @inner 0 4 4
177 if @inner;
178  
179 4 delete $self->{mounts}{$path};
180 }
181  
182 =item @paths = $fs-E<gt>mounts
183  
184 Returns the list of all paths that have been mounted to.
185  
186 =cut
187  
188 225 ▶sub mounts {
Subroutine mounts: called CC=1 SCAR=0.0
Pod documented
189 225 my $self = shift;
190 225 return keys %{ $self->{mounts} };
191 }
192  
193 =back
194  
195 =cut
196  
197 8 ▶sub _init_fs {
Subroutine _init_fs: called CC=3 SCAR=12.7
198 8 my $self = shift;
199 8 my $fs = shift;
200  
201 8 ▶ if (UNIVERSAL::isa($fs, 'File::System::Object')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($fs, 'File::System::Object')) { } 0 8 8
elsif (ref $fs eq 'ARRAY') { } 8 0 8
202 0 return $fs;
203 } elsif (ref $fs eq 'ARRAY') {
204 8 return File::System->new(@$fs);
205 } else {
206 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::Table for details.";
207 }
208 }
209  
210 27334 ▶sub _resolve_fs {
Subroutine _resolve_fs: called CC=2 SCAR=6.9
211 27334 my $self = shift;
212 27334 my $path = $self->normalize_path(shift);
213  
214 # The mount point we want should be the longest one which matches our
215 # given path name.
216 my ($mp) =
217 5217 sort { -(length($a) <=> length($b)) }
218 69253 grep { $path =~ /^$_/ }
219 27334 keys %{ $self->{mounts} };
220
221 27334 my $rel_path = substr $path, length($mp);
222 27334 ▶ $rel_path = '/'.$rel_path unless $rel_path =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_path =~ m[^/] 26303 1031 27334
223  
224 27334 return ($self->{mounts}{$mp}, $rel_path);
225 }
226  
227 1686 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
228 1686 my $self = shift;
229  
230 return bless {
231 cwd => '/',
232 cwd_fs => $self->{mounts}{'/'},
233 mounts => $self->{mounts},
234 1686 }, ref $self;
235 }
236  
237 492 ▶sub exists {
Subroutine exists: called CC=2 SCAR=7.0
Pod documented
238 492 my $self = shift;
239 492 ▶ my ($fs, $path) = $self->_resolve_fs(shift || $self->path);
Condition66%2/3
shift() || $self->path
l!l&&r!l&&!r
417750
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
240 492 return $fs->exists($path);
241 }
242  
243 26387 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=6.9
Pod documented
244 26387 my $self = shift;
245 26387 my $cwd = $self->normalize_path($_[0]);
246 26387 my ($fs, $path) = $self->_resolve_fs(shift);
247  
248 26387 my $cwd_fs = $fs->lookup($path);
249  
250 26387 ▶ return undef unless defined $cwd_fs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cwd_fs 11 26376 26387
251  
252 return bless {
253 cwd => $cwd,
254 cwd_fs => $cwd_fs,
255 mounts => $self->{mounts},
256 26376 }, ref $self;
257 }
258  
259 3 my @delegates = qw/
260 is_valid
261 properties
262 settable_properties
263 set_property
264 remove
265 has_content
266 is_container
267 is_readable
268 is_seekable
269 is_writable
270 is_appendable
271 open
272 content
273 has_children
274 children_paths
275 /;
276  
277 3 for my $name (@delegates) {
278 45 eval qq(
279 #line 287 "File::Sytem::Table ($name)"
280 sub $name {
281 my \$self = shift;
282 return \$self->{cwd_fs}->$name(\@_);
283 }
284 );
285 45 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 45 45
286 }
287  
288 104525 ▶sub get_property {
Subroutine get_property: called CC=4 SCAR=13.9
Pod documented
289 104525 my $self = shift;
290 104525 local $_ = shift;
291  
292 SWITCH: {
293 104525 ▶ /^path$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^path$/ and do { return $self->{"cwd"} } 96412 8113 104525
294 96412 return $self->{cwd};
295 };
296 8113 ▶ /^dirname$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^dirname$/ and do { return $self->dirname_of_path($self->{"cwd"}) } 5495 2618 8113
297 5495 return $self->dirname_of_path($self->{cwd});
298 };
299 2618 ▶ /^basename$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^basename$/ and do { return $self->basename_of_path($self->{"cwd"}) } 1826 792 2618
300 1826 return $self->basename_of_path($self->{cwd});
301 };
302 DEFAULT: {
303 792 return $self->{cwd_fs}->get_property($_);
304 }
305 }
306 }
307  
308 382 ▶sub rename {
Subroutine rename: called CC=2 SCAR=7.0
Pod documented
309 382 my $self = shift;
310 382 my $name = shift;
311  
312 382 ▶ grep { $self->{cwd} eq $_ } keys %{ $self->{mounts} }
Branch: 1 branch
BranchTrueFalseTotal
grep {$self->{"cwd"} eq $_;} keys %{$$self{"mounts"};} and croak("Cannot rename the mount point '${self}'") 0 382 382
313 and croak "Cannot rename the mount point '$self'";
314  
315 382 $self->{cwd_fs}->rename($name);
316
317 382 $self->{cwd} =~ s#[^/]+$ #$name#x;
318  
319 382 return $self;
320 }
321  
322 382 ▶sub move {
Subroutine move: called CC=2 SCAR=7.0
Pod documented
323 382 my $self = shift;
324 382 my $path = shift;
325 382 my $force = shift;
326  
327 382 ▶ UNIVERSAL::isa($path, 'File::System::Table')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($path, "File::System::Table") or croak("Move failed; the '${path}' object is not a 'File::System::Table'") 382 0 382
328 or croak "Move failed; the '$path' object is not a 'File::System::Table'";
329  
330 382 $self->{cwd_fs}->move($path->{cwd_fs}, $force);
331 382 $self->{cwd} = $self->normalize_path($path->path.'/'.$self->basename);
332  
333 382 return $self;
334 }
335  
336 191 ▶sub copy {
Subroutine copy: called CC=2 SCAR=7.0
Pod documented
337 191 my $self = shift;
338 191 my $path = shift;
339 191 my $force = shift;
340
341 191 ▶ UNIVERSAL::isa($path, 'File::System::Table')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($path, "File::System::Table") or croak("Copy failed; the '${path}' object is not a 'File::System::Table'") 191 0 191
342 or croak "Copy failed; the '$path' object is not a 'File::System::Table'";
343  
344 191 my $copy = $self->{cwd_fs}->copy($path->{cwd_fs}, $force);
345 191 my $copy_cwd = $self->normalize_path($path->path.'/'.$self->basename);
346  
347 return bless {
348 cwd_fs => $copy,
349 cwd => $copy_cwd,
350 mounts => $self->{mounts},
351 191 }, ref $self;
352 }
353
354 2137 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod documented
355 2137 my $self = shift;
356 return
357 3098 map { $self->lookup($_) }
358 2137 grep !/^\.\.?$/, $self->{cwd_fs}->children_paths;
359 }
360  
361 242 ▶sub child {
Subroutine child: called CC=3 SCAR=11.3
Pod documented
362 242 my $self = shift;
363 242 my $name = shift;
364  
365 242 ▶ $self->is_container
Branch: 1 branch
BranchTrueFalseTotal
$self->is_container or croak("The child method called on non-container.") 242 0 242
366 or croak "The child method called on non-container.";
367  
368 242 ▶ $name !~ /\//
Branch: 1 branch
BranchTrueFalseTotal
$name =~ m[/] and croak("Argument to child must not be a path.") 0 242 242
369 or croak "Argument to child must not be a path.";
370  
371 242 return $self->lookup($name);
372 }
373  
374 0 ▶sub is_createable {
Subroutine is_createable: not called CC=1 SCAR=6.9
Pod undocumented
375 0 my $self = shift;
376 0 my $path = $self->normalize_path($_[0]);
377 0 my ($fs, $rel_path) = $self->_resolve_fs(shift);
378 0 my $type = shift;
379  
380 0 return $fs->is_creatable($rel_path, $type);
381 }
382  
383 455 ▶sub create {
Subroutine create: called CC=2 SCAR=7.0
Pod documented
384 455 my $self = shift;
385 455 my $path = $self->normalize_path($_[0]);
386 455 my ($fs, $rel_path) = $self->_resolve_fs(shift);
387 455 my $type = shift;
388  
389 455 my $obj = $fs->create($rel_path, $type);
390  
391 455 ▶ return undef unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 455 455
392  
393 return bless {
394 cwd => $path,
395 cwd_fs => $obj,
396 mounts => $self->{mounts},
397 455 }, ref $self;
398 }
399  
400 =head1 BUGS
401  
402 The C<copy> and C<move> methods will fail if used between file systems. This can be remedied, but it will require some delicate planning that hasn't yet been done.
403  
404 =head1 SEE ALSO
405  
406 L<File::System>, L<File::System::Object>, L<File::System::Real>, L<File::System::Layered>
407  
408 =head1 AUTHOR
409  
410 Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt>
411  
412 =head1 COPYRIGHT AND LICENSE
413  
414 Copyright 2005 Andrew Sterling Hanenkamp. All Rights Reserved.
415  
416 This library is distributed and licensed under the same terms as Perl itself.
417  
418 =cut
419  
420 3 1
« System.pm ↑ Summary System/Passthrough.pm »