Simple/Engine/FileSystem.pm untested

filter: Statementstmt 0.0% 0 / 210 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 73 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 437 CC 34 SCAR 33.8
CC
34
Cov
0%
has_permission
90.0
set_scalar
72.0
path_exists
42.0
CRAP 29.4
1 package Repository::Simple::Engine::FileSystem;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.06';
7  
8 use Carp;
9 use Repository::Simple;
10 use Repository::Simple::Engine qw( $NODE_EXISTS $PROPERTY_EXISTS $NOT_EXISTS );
11 use Repository::Simple::Permission;
12 use Repository::Simple::Type::Node;
13 use Repository::Simple::Type::Property;
14 use Repository::Simple::Util qw( dirname basename );
15 use File::Spec;
16 use IO::Scalar;
17 use Scalar::Util qw( weaken );
18 use Symbol;
19  
20  
21 use base 'Repository::Simple::Engine';
22  
23 =head1 NAME
24  
25 Repository::Simple::Engine::FileSystem - Native file system repository storage
26  
27 =head1 SYNOPSIS
28  
29 use Repository::Simple;
30 my $fs = Repository::Simple->attach('FileSystem', root => '/usr/local');
31  
32 =head1 DESCRIPTION
33  
34 This repository maps directly into the native file system. The goal is to make this mapping as direct as possible with very few deviations from native features and functionality.
35  
36 As of this documentation, the storage engine is capable of handling only files and directories. Symlinks, devices, FIFOs, or any other kind of file type is partially handled, but the specifics functionality provided by these certainly isn't address completely.
37  
38 =head1 OPTIONS
39  
40 This file system module accepts only a single option, C<root>. If not given, the current working directory is assumed for the value C<root>. All files returned by the file system will be rooted at the given (or assumed) point. No file outside of that point is accessible.
41  
42 =head1 NODE TYPES
43  
44 There are three node types used by this engine:
45  
46 =over
47  
48 =item fs:object
49  
50 This represents any non-file/non-directory file system object. The fs:file and fs:directory objects inherit from this. The stat properties are associated with this object.
51  
52 =item fs:file
53  
54 This represents a file object, i.e., anything that would pass the C<-f> test. This object has the stat properties plus the fs:content property associated with it.
55  
56 =item fs:directory
57  
58 This represents a directory object, i.e., anything that would pass the C<-d> test. This object has the stat properties associated with it. It may also have child nodes associated with it. The names and types of child nodes is not restricted.
59  
60 =back
61  
62 =head1 NODE PROPERTIES
63  
64 All file system nodes have stat properties associated with them. These properties are populated by the return of the C<stat()> built-in subroutine. The stat properties are:
65  
66 =over
67  
68 =item fs:dev
69  
70 device number of file system
71  
72 =item fs:ino
73  
74 inode number
75  
76 =item fs:mode
77  
78 file mode (type and permissions)
79  
80 =item fs:nlink
81  
82 number of (hard) links to the file
83  
84 =item fs:uid
85  
86 numeric user ID of file's owner
87  
88 =item fs:gid
89  
90 numeric group ID of file's owner
91  
92 =item fs:rdev
93  
94 the device identifier (special files only)
95  
96 =item fs:size
97  
98 total size of file, in bytes
99  
100 =item fs:atime
101  
102 last access time in seconds since the epoch
103  
104 =item fs:mtime
105  
106 last modify time in seconds since the epoch
107  
108 =item fs:ctime
109  
110 last change time in seconds since the epoch
111  
112 =item fs:blksize
113  
114 preferred block size for file system I/O
115  
116 =item fs:blocks
117  
118 actual number of blocks allocated
119  
120 =back
121  
122 The definitions were taken from the documentation in L<perlfunc>. Each of these will be an integer number. Once modification is implemented, the fs:mode, fs:uid, fs:gid, fs:atime, fs:mtime, and fs:ctime fields will be updatable. All other fields are not updatable. All of these fields are auto_created and all or not removable.
123  
124 In addition to these properties, fs:file nodes also have an fs:content property, which will contain the file contents. You may wish to grab this data via the C<get_handle()> method rather than C<get_scalar()>.
125  
126 =cut
127  
128 my %namespaces = (
129 fs => 'http://contentment.org/Repository/Simple/Engine/FileSystem',
130 );
131  
132 my %node_type_defs = (
133 'fs:object' => {
134 name => 'fs:object',
135 property_types => {
136 'fs:dev' => 'fs:scalar-static',
137 'fs:ino' => 'fs:scalar-static',
138 'fs:mode' => 'fs:scalar',
139 'fs:nlink' => 'fs:scalar-static',
140 'fs:uid' => 'fs:scalar',
141 'fs:gid' => 'fs:scalar',
142 'fs:rdev' => 'fs:scalar-static',
143 'fs:size' => 'fs:scalar-static',
144 'fs:atime' => 'fs:scalar',
145 'fs:mtime' => 'fs:scalar',
146 'fs:ctime' => 'fs:scalar-static',
147 'fs:blksize' => 'fs:scalar-static',
148 'fs:blocks' => 'fs:scalar-static',
149 },
150 updatable => 1,
151 removable => 1,
152 },
153  
154 'fs:file' => {
155 name => 'fs:file',
156 super_types => [ qw( fs:object ) ],
157 property_types => {
158 'fs:content' => 'fs:handle',
159 },
160 updatable => 1,
161 removable => 1,
162 },
163  
164 'fs:directory' => {
165 name => 'fs:directory',
166 super_types => [ qw( fs:object ) ],
167 node_types => {
168 '*' => [ 'fs:object' ],
169 },
170 updatable => 1,
171 removable => 1,
172 },
173 );
174  
175 my %property_type_defs = (
176 'fs:scalar' => {
177 name => 'fs:scalar',
178 auto_created => 1,
179 updatable => 1,
180 removable => 0,
181 },
182  
183 'fs:scalar-static' => {
184 name => 'fs:scalar-static',
185 auto_created => 1,
186 updatable => 0,
187 removable => 0,
188 },
189
190 'fs:handle' => {
191 name => 'fs:handle',
192 auto_created => 1,
193 updatable => 1,
194 removable => 0,
195 },
196 );
197  
198 my %stat_names = (
199 'fs:dev' => 0,
200 'fs:ino' => 1,
201 'fs:mode' => 2,
202 'fs:nlink' => 3,
203 'fs:uid' => 4,
204 'fs:gid' => 5,
205 'fs:rdev' => 6,
206 'fs:size' => 7,
207 'fs:atime' => 8,
208 'fs:mtime' => 9,
209 'fs:ctime' => 10,
210 'fs:blksize' => 11,
211 'fs:blocks' => 12,
212 );
213  
214 sub new {
215 my $class = shift;
216 my %args = @_;
217  
218 $args{root} ||= '.';
219 $args{root} = File::Spec->rel2abs($args{root});
220 my $root = File::Spec->canonpath($args{root});
221  
222 -e $root or croak "Sorry, root $root does not exist!";
223 -d $root or croak "Sorry, root $root is not a directory!";
224  
225 my $self = bless {
226 fs_root => $root,
227 }, $class;
228  
229 while (my ($name, $node_def) = each %node_type_defs) {
230 $self->{node_types}{$name}
231 = Repository::Simple::Type::Node->new(
232 engine => $self,
233 %$node_def,
234 );
235 }
236  
237 while (my ($name, $prop_def) = each %property_type_defs) {
238 $self->{property_types}{$name}
239 = Repository::Simple::Type::Property->new(
240 engine => $self,
241 %$prop_def,
242 );
243 }
244  
245 return $self;
246 }
247  
248 sub node_type_named {
249 my ($self, $type_name) = @_;
250 return $self->{node_types}{ $type_name };
251 }
252  
253 sub property_type_named {
254 my ($self, $type_name) = @_;
255 return $self->{property_types}{ $type_name };
256 }
257  
258 sub nodes_in {
259 my ($self, $path) = @_;
260  
261 my $real_path = $self->real_path($path);
262
263 $self->check_real_path($real_path, $path);
264  
265 if (!-d $real_path) {
266 return ();
267 }
268  
269 my $handle = gensym;
270 opendir $handle, $real_path
271 or croak qq(failed to readdir for path "$path");
272 my @dirs = grep { $_ !~ /^\.\.?$/ } readdir $handle;
273 closedir $handle;
274  
275 return @dirs;
276 }
277  
278 sub properties_in {
279 my ($self, $path) = @_;
280  
281 my $real_path = $self->real_path($path);
282  
283 $self->check_real_path($real_path, $path);
284  
285 my @properties = keys %stat_names;
286  
287 if (-f $real_path) {
288 push @properties, 'fs:content';
289 }
290  
291 return @properties;
292 }
293  
294 sub node_type_of {
295 my ($self, $path) = @_;
296  
297 my $real_path = $self->real_path($path);
298  
299 $self->check_real_path($real_path, $path);
300  
301 if (-d $real_path) {
302 return $self->{node_types}{'fs:directory'};
303 }
304  
305 elsif (-f $real_path) {
306 return $self->{node_types}{'fs:file'};
307 }
308  
309 else {
310 return $self->{node_types}{'fs:object'};
311 }
312 }
313  
314 sub property_type_of {
315 my ($self, $path) = @_;
316  
317 my $basename = basename($path);
318 my $dirname = dirname($path);
319  
320 my $node_type = $self->node_type_of($dirname);
321 my %property_types = $node_type->property_types;
322  
323 if (!defined $property_types{$basename}) {
324 croak qq(no property named "$basename" for node "$dirname");
325 }
326  
327 return $self->property_type_named($property_types{$basename});
328 }
329  
330 sub path_exists {
331 my ($self, $path) = @_;
332  
333 my $dirname = dirname($path);
334 my $basename = basename($path);
335  
336 my $real_path = $self->real_path($path);
337  
338 # If it is a node path, just find if it exists
339 return $NODE_EXISTS if -e $real_path;
340  
341 # Next, check to see if it's a property
342 my $property = $basename =~ m[
343 fs:
344 (?: dev | ino | mode | nlink
345 | uid | gid | rdev | size
346 | atime | mtime | ctime | blksize
347 | blocks | content )
348 ]x;
349  
350 if ($property) {
351 $real_path = $self->real_path($dirname);
352  
353 # fs:content exists only if the path is a file, the other properties
354 # exist for both files or directories
355 if ($basename eq 'fs:content') {
356 return -f $real_path ? $PROPERTY_EXISTS : $NOT_EXISTS;
357 }
358  
359 else {
360 return -e $real_path ? $PROPERTY_EXISTS : $NOT_EXISTS;
361 }
362 }
363  
364 # Doesn't exist
365 return $NOT_EXISTS;
366 }
367  
368 sub _get_scalar {
369 my ($self, $file, $property) = @_;
370  
371 return (stat $file)[ $stat_names{ $property } ];
372 }
373  
374 sub _get_handle {
375 my ($self, $dirname, $file, $mode) = @_;
376  
377 my $handle = gensym;
378 open $handle, $mode, $file
379 or croak qq(failed to open "fs:content" property of node ),
380 qq("$dirname" with mode "$mode");
381  
382 $self->{handles}{$dirname} = $handle;
383 #weaken $self->{handles}{$dirname};
384  
385 return $handle;
386 }
387  
388 sub get_scalar {
389 my ($self, $path) = @_;
390  
391 my $basename = basename($path);
392 my $dirname = dirname($path);
393  
394 my $real_path = $self->real_path($dirname);
395  
396 $self->check_real_path($real_path, $dirname);
397  
398 if ($basename eq 'fs:content') {
399 unless (-f $real_path) {
400 croak qq(no "fs:content" property associated with node at ),
401 qq("$dirname");
402 }
403  
404 my $handle = $self->_get_handle($dirname, $real_path, '<');
405 my $scalar = join '', <$handle>;
406 close $handle;
407  
408 return $scalar;
409 }
410  
411 elsif (defined $stat_names{ $basename }) {
412 return $self->_get_scalar($real_path, $basename);
413 }
414  
415 else {
416 croak qq(no "$basename" property associated with node at "$dirname");
417 }
418 }
419  
420 sub get_handle {
421 my ($self, $path, $mode) = @_;
422  
423 $mode ||= '<';
424  
425 # if ($mode ne '<') {
426 # croak qq(invalid mode "$mode" given);
427 # }
428  
429 my $basename = basename($path);
430 my $dirname = dirname($path);
431  
432 my $real_path = $self->real_path($dirname);
433  
434 $self->check_real_path($real_path, $dirname);
435  
436 if ($basename eq 'fs:content') {
437 if (!-f $real_path) {
438 croak qq(no "fs:content" property associated with node at ),
439 qq("$dirname");
440 }
441  
442 return $self->_get_handle($dirname, $real_path, $mode);
443 }
444  
445 elsif (defined $stat_names{ $basename }) {
446 my $scalar = $self->_get_scalar($real_path, $basename);
447 return IO::Scalar->new(\$scalar);
448 }
449  
450 else {
451 croak qq(no "$basename" property associated with node at "$dirname");
452 }
453 }
454  
455 sub real_path {
456 my ($self, $fs_path) = @_;
457  
458 return File::Spec->catfile($self->{fs_root}, $fs_path);
459 }
460  
461 sub check_real_path {
462 my ($self, $real_path, $path) = @_;
463  
464 if (!-e $real_path) {
465 croak qq(no file found at path "$path");
466 }
467 }
468  
469 sub namespaces { return \%namespaces; }
470  
471 my %ustat_props = (
472 'fs:mode' => 1,
473 'fs:uid' => 1,
474 'fs:gid' => 1,
475 'fs:atime' => 1,
476 'fs:mtime' => 1,
477 'fs:ctime' => 1,
478 );
479  
480 # TODO I think I've got this matching POSIX, but I'm surely wrong since I did
481 # this when I was half asleep and when I can't really remember the official
482 # POSIX standard on this anymore. I need to verify this is correct and then
483 # correct the heinous mistakes I've made.
484 sub has_permission {
485 my ($self, $path, $action) = @_;
486  
487 my $pname = basename($path);
488 my $real_path = $self->real_path($path);
489 my $par_path = $self->real_path(dirname($path));
490 my $dir_path = $self->real_path(dirname(dirname($path)));
491  
492 if ($action eq $ADD_NODE && -d $par_path && -w $par_path) {
493 return 1;
494 }
495  
496 if ($action eq $SET_PROPERTY && $ustat_props{$pname} && -w $dir_path) {
497 return 1;
498 }
499  
500 if ($action eq $SET_PROPERTY && $pname eq 'fs:content' && -w $par_path) {
501 return 1;
502 }
503  
504 if ($action eq $REMOVE && -e $real_path && -w $par_path) {
505 return 1;
506 }
507  
508 if ($action eq $READ && $pname eq 'fs:content' && -r $par_path) {
509 return 1;
510 }
511  
512 if ($action eq $READ && -d $real_path && -r $real_path && -x $real_path) {
513 return 1;
514 }
515  
516 if ($action eq $READ && -e $real_path && -r $real_path) {
517 return 1;
518 }
519  
520 if ($action eq $READ && defined $stat_names{$pname} && -r $dir_path) {
521 return 1;
522 }
523  
524 return 0;
525 }
526  
527 sub set_scalar {
528 my ($self, $path, $value) = @_;
529  
530 my $basename = basename($path);
531 my $dirname = dirname($path);
532  
533 my $real_path = $self->real_path($dirname);
534  
535 $self->check_real_path($real_path, $dirname);
536  
537 if ($basename eq 'fs:content') {
538 unless (-f $real_path) {
539 croak qq(no "fs:content" property associated with node at ),
540 qq("$dirname");
541 }
542  
543 my $handle = $self->_get_handle($dirname, $real_path, '>');
544 print $handle $value;
545 close $handle;
546 }
547  
548 elsif ($basename eq 'fs:mode') {
549 chmod $value, $real_path
550 or croak qq(Failed to change "$path" to "$value": $!);
551 }
552  
553 elsif ($basename eq 'fs:uid') {
554 my $gid = $self->_get_scalar($real_path, 'fs:gid');
555 chown $value, $gid, $real_path
556 or croak qq(Failed to change "$path" to "$value": $!);
557 }
558  
559 elsif ($basename eq 'fs:gid') {
560 my $uid = $self->_get_scalar($real_path, 'fs:uid');
561 chown $uid, $value, $real_path
562 or croak qq(Failed to change "$path" to "$value": $!);
563 }
564  
565 elsif ($basename eq 'fs:atime') {
566 my $mtime = $self->_get_scalar($real_path, 'fs:mtime');
567 utime $value, $mtime, $real_path
568 or croak qq(Failed to change "$path" to "$value": $!);
569 }
570  
571 elsif ($basename eq 'fs:mtime') {
572 my $atime = $self->_get_scalar($real_path, 'fs:atime');
573 utime $atime, $value, $real_path
574 or croak qq(Failed to change "$path" to "$value": $!);
575 }
576  
577 else {
578 croak qq(property "$basename" is static or does not exist for ),
579 qq("$dirname" );
580 }
581 }
582  
583 sub set_handle {
584 my ($self, $path, $handle) = @_;
585  
586 # TODO This is cheating and should be done better
587 my $value = join '', readline($handle);
588 $self->set_scalar($path, $value);
589 }
590  
591 sub save_property {
592 my ($self, $path) = @_;
593  
594 my $dirname = dirname($path);
595  
596 # Check for a file handle at the given path; close it if found
597 if (defined $self->{handles}{$dirname}) {
598 my $handle = delete $self->{handles}{$dirname};
599 close $handle;
600 }
601 }
602  
603 =head1 SEE ALSO
604  
605 L<Repository::Simple>
606  
607 =head1 AUTHOR
608  
609 Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt>
610  
611 =head1 LICENSE AND COPYRIGHT
612  
613 Copyright 2006 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All
614 Rights Reserved.
615  
616 This module is free software; you can redistribute it and/or modify it under
617 the same terms as Perl itself. See L<perlartistic>.
618  
619 This program is distributed in the hope that it will be useful, but WITHOUT
620 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
621 FOR A PARTICULAR PURPOSE.
622  
623 =cut
624  
625 1