blib/lib/Badger/Filesystem/Virtual.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 195 CC 32 SCAR 49.9
CC
32
Cov
0%
roots
306.0
read_directory
56.0
definitive_read
30.0
CRAP 146.3
1 #========================================================================
2 #
3 # Badger::Filesystem::Virtual
4 #
5 # DESCRIPTION
6 # Subclass of Badger::Filesystem which implements a virtual filesystem
7 # composed from several source directories, conceptually layered on
8 # top of each other.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@wardley.org>
12 #
13 #========================================================================
14  
15 package Badger::Filesystem::Virtual;
16  
17 use Badger::Debug ':dump';
18 use Badger::Class
19 version => 0.01,
20 debug => 0,
21 base => 'Badger::Filesystem',
22 # accessors => 'root', # interferes
23 utils => 'blessed',
24 constants => 'ARRAY CODE',
25 constant => {
26 VFS => __PACKAGE__,
27 virtual => __PACKAGE__,
28 PATH_METHOD => 'path',
29 PATHS_METHOD => 'paths',
30 ROOTS_METHOD => 'roots',
31 },
32 exports => {
33 any => 'VFS',
34 },
35 messages => {
36 bad_root => 'Invalid root directory: %s',
37 max_roots => 'The number of virtual filesystem roots exceeds the max_roots limit of %s',
38 };
39  
40 *definitive = \&definitive_write;
41  
42 our $MAX_ROOTS = 32 unless defined $MAX_ROOTS;
43  
44 sub init {
45 my ($self, $config) = @_;
46  
47 # let the base class have a go first, so it can set rootdir et al
48 $self->SUPER::init($config);
49  
50 # root can be a single item or list ref
51 my $root = $config->{ root } || $self->{ rootdir };
52 $root = [$root] unless ref $root eq ARRAY;
53 $self->{ root } = $root;
54  
55 # the dynamic flag indicates that the list of roots can change so we must
56 # recompute them each time we use them. max_roots sets a limit on the
57 # expansion to prevent runaways
58 $self->{ dynamic } = $config->{ dynamic };
59 $self->{ max_roots } =
60 defined $config->{ max_roots }
61 ? $config->{ max_roots }
62 : $MAX_ROOTS;
63  
64 # we must set cwd to / so that the relative -> absolute path translation
65 # works as expected. The concept of having a current working directory
66 # in a VFS is just a bit too weird to contemplate anyway.
67 $self->{ cwd } = $self->{ rootdir };
68  
69 $self->debug("Virtual root: ", join(', ', @$root), "\n" ) if $DEBUG;
70  
71 return $self;
72 }
73  
74 sub roots {
75 my $self = shift;
76  
77 if (my $roots = $self->{ roots }) {
78 return wantarray
79 ? @$roots
80 : $roots;
81 }
82  
83 my $max = $self->{ max_roots };
84 my @paths = @{ $self->{ root } };
85 my (@roots, $type, $paths, $dir, $code);
86  
87 # If a positive max_roots is defined then we'll limit the number of
88 # roots we resolve. If it's zero or negative then it will pre-decrement
89 # before being tested so will always be true
90 while (@paths && --$max) {
91 $dir = shift @paths || next;
92  
93 $type = ref $dir || do {
94 # non-reference paths get added as they are
95 $self->debug("discovered root directory: $dir\n") if DEBUG;
96 push(@roots, $dir);
97 next;
98 };
99  
100 # anything else can expand out to one or more paths, each of which
101 # can expand recursively, so we push all new paths back onto the
102 # candidate list and test each in turn.
103  
104 if ($type eq CODE) {
105 # call code ref
106 $paths = $dir->();
107 $self->debug(
108 "discovered root directories from code ref: ",
109 $self->dump_data_inline($paths), "\n"
110 ) if DEBUG;
111 unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
112 next;
113 }
114 elsif ($type eq ARRAY) {
115 # expand list ref
116 $self->debug(
117 "discovered root directories from code ref: ",
118 join(', ', @$dir), "\n"
119 ) if DEBUG;
120 unshift(@paths, @$dir);
121 next;
122 }
123 elsif (blessed $dir) {
124 # see if object has a path(), paths() or roots() method
125 # TODO: this is broken - we don't want to recompute paths
126 # each time just because we're using an object that has a
127 # path
128 if ($code = $dir->can(PATH_METHOD)
129 || $dir->can(PATHS_METHOD)
130 || $dir->can(ROOTS_METHOD) ) {
131 $paths = $code->($dir);
132 $self->debug(
133 "discovered root directories from $type object: $paths / ",
134 $self->dump_data_inline($paths), "\n"
135 ) if DEBUG;
136 unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
137 next;
138 }
139 }
140  
141 $self->error( bad_root => $dir );
142 }
143  
144 # anything left in @paths means we must have blown the max_roots limit
145 return $self->error_msg( max_roots => $self->{ max_roots } )
146 if @paths;
147  
148 # we can cache roots if all are static and the dynamic flag isn't set
149 $self->{ roots } = \@roots
150 unless $self->{ dynamic };
151  
152 $self->debug("resolved roots: [\n ", join("\n ", @roots), "\n]\n") if DEBUG;
153  
154 return wantarray
155 ? @roots
156 : \@roots;
157 }
158  
159 sub definitive_paths {
160 my $self = shift;
161 my $path = $self->absolute(@_);
162 my @paths = map { $self->merge_paths($_, $path) } $self->roots;
163 return wantarray
164 ? @paths
165 : \@paths;
166 }
167  
168 sub definitive_write {
169 my $self = shift;
170 $self->debug("definitive_write(", join(', ', @_), ")\n") if DEBUG;
171 my $path = $self->absolute(@_);
172 return $self->join_directory($self->roots->[0], $path);
173 }
174  
175 sub definitive_read {
176 my $self = shift;
177 my $path = $self->absolute(@_);
178 my ($base, $full);
179  
180 $self->debug("definitive_read($path)") if DEBUG;
181  
182 foreach $base ($self->roots) {
183 $full = $self->merge_paths($base, $path);
184 $self->debug("looking for [$base] + [$path] => $full\n") if DEBUG;
185 return $full if -e $full;
186 $self->debug("not found\n") if DEBUG;
187 }
188 return undef;
189 }
190  
191 sub read_directory {
192 my $self = shift;
193 my $path = $self->absolute(shift);
194 my $all = shift;
195 my ($base, $full, $dirh, $item, @items, %seen);
196  
197 require IO::Dir;
198  
199 foreach $base ($self->roots) {
200 $full = $self->join_directory($base, $path);
201 $self->debug("Opening directory: $full\n") if DEBUG;
202 $dirh = IO::Dir->new($full)
203 || next;
204 # Some directory may not exist, so ignore them
205 # || $self->error_msg( open_failed => directory => $full => $! );
206 while (defined ($item = $dirh->read)) {
207 if (DEBUG) {
208 if ($seen{ $item }) {
209 $self->debug("skipping $item (already seen)");
210 }
211 else {
212 $self->debug("adding $item");
213 }
214 }
215 push(@items, $item) unless $seen{ $item }++;
216 }
217 $dirh->close;
218 }
219 @items = $self->FILESPEC->no_upwards(@items)
220 unless $all || ref $self && $self->{ all_entries };
221  
222 return wantarray ? @items : \@items;
223 }
224  
225  
226 1;
227  
228 __END__