lib/Badger/Filesystem/Virtual.pm

filter: Statementstmt 92.8% 78 / 84 Branchbran 72.7% 32 / 44 Conditioncond 52.0% 13 / 25 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 87.5% 7 / 8 Podpod 100% 6 / 6 totaltotal 75.6% 137 / 181 CC 33 SCAR 25.1
CC
33
Cov
80%
roots
19.7
read_directory
9.9
definitive_paths
6.0
CRAP 12.3
« lib/Badger/Period.pm ↑ Summary lib/Badger/Timestamp.pm »
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 6 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Badger::Class
19 6 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 6 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 6 *definitive = \&definitive_write;
41  
42 6 ▶our $MAX_ROOTS = 32 unless defined $MAX_ROOTS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Filesystem::Virtual::MAX_ROOTS 6 0 6
43  
44 sub init {
45 6 ▶ my ($self, $config) = @_;
Subroutine init: called CC=5 SCAR=16.3
Pod documented
46  
47 # let the base class have a go first, so it can set rootdir et al
48 6 $self->SUPER::init($config);
49  
50 # root can be a single item or list ref
51 6 ▶ my $root = $config->{ root } || $self->{ rootdir };
Condition66%2/3
$config->{'root'} || $self->{'rootdir'}
l!l&&r!l&&!r
510
MC/DC0%0/2
$config->{'root'} || $self->{'rootdir'}
$config->{'root'} $self->{'rootdir'}
Truth table66%2/3
A || B
A= $config->{'root'}B= $self->{'rootdir'}
ABresult
1X1
011
000
52 6 ▶ $root = [$root] unless ref $root eq ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $root eq "ARRAY" 3 3 6
53 6 $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 6 $self->{ dynamic } = $config->{ dynamic };
59 $self->{ max_roots } =
60 defined $config->{ max_roots }
61 ? $config->{ max_roots }
62 6 ▶ : $MAX_ROOTS;
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'max_roots'} ? : 0 6 6
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 6 $self->{ cwd } = $self->{ rootdir };
68  
69 6 ▶ $self->debug("Virtual root: ", join(', ', @$root), "\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Virtual::DEBUG 0 6 6
70  
71 6 return $self;
72 }
73  
74 sub roots {
75 76 ▶ my $self = shift;
Subroutine roots: called CC=18 SCAR=29.8
Pod documented
76  
77 76 ▶ if (my $roots = $self->{ roots }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $roots = $self->{"roots"}) 68 8 76
78 return wantarray
79 68 ▶ ? @$roots
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 60 8 68
80 : $roots;
81 }
82  
83 8 my $max = $self->{ max_roots };
84 8 my @paths = @{ $self->{ root } };
85 8 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 8 ▶ while (@paths && --$max) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paths and --$max) 57 8 65
Condition66%2/3
@paths and --$max
!ll&&!rl&&r
8057
MC/DC50%1/2
@paths and --$max
@paths --$max
Truth table66%2/3
A and B
A= @pathsB= --$max
ABresult
0X0
100
111
91 57 ▶ $dir = shift @paths || next;
Condition50%1/2
shift @paths || (next)
l!l
570
MC/DC0%0/1
shift @paths || (next)
shift @paths
Truth table50%1/2
A
A= shift @paths
Aresult
11
00
92  
93 57 ▶ $type = ref $dir || do {
Condition100%3/3
ref $dir || do { '???'; push @roots, $dir; next }
l!l&&r!l&&!r
33213
MC/DC0%0/2
ref $dir || do { '???'; push @roots, $dir; next }
ref $dir do { '???'; push @roots, $dir; next }
Truth table33%1/3
A || B
A= ref $dirB= do { '???'; push @roots, $dir; next }
ABresult
1X1
011
000
94 # non-reference paths get added as they are
95 24 $self->debug("discovered root directory: $dir\n") if DEBUG;
96 24 push(@roots, $dir);
97 24 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 33 ▶ if ($type eq CODE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 8 25 33
elsif ($type eq 'ARRAY') { } 3 22 25
elsif (builtin::blessed($dir)) { } 22 0 22
105 # call code ref
106 8 $paths = $dir->();
107 8 $self->debug(
108 "discovered root directories from code ref: ",
109 $self->dump_data_inline($paths), "\n"
110 ) if DEBUG;
111 8 ▶ unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
Branch: 1 branch
BranchTrueFalseTotal
ref $paths eq 'ARRAY' ? : 8 0 8
112 8 next;
113 }
114 elsif ($type eq ARRAY) {
115 # expand list ref
116 3 $self->debug(
117 "discovered root directories from code ref: ",
118 join(', ', @$dir), "\n"
119 ) if DEBUG;
120 3 unshift(@paths, @$dir);
121 3 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 22 ▶ if ($code = $dir->can(PATH_METHOD)
Branch: 1 branch
BranchTrueFalseTotal
if ($code = $dir->can("path") || $dir->can("paths") || $dir->can("roots")) 22 0 22
Condition33%2/6
$dir->can('path') || $dir->can('paths') || $dir->can('roots')
l!l&&r!l&&!r
2200
$dir->can("path") or $dir->can("paths")
l!l&&r!l&&!r
2200
MC/DC0%0/3
$dir->can('path') || $dir->can('paths') || $dir->can('roots')
$dir->can("path") $dir->can("paths") $dir->can('roots')
Truth table25%1/4
A or B || C
A= $dir->can("path")B= $dir->can("paths")C= $dir->can('roots')
ABCresult
1XX1
01X1
0011
0000
129 || $dir->can(PATHS_METHOD)
130 || $dir->can(ROOTS_METHOD) ) {
131 22 $paths = $code->($dir);
132 22 $self->debug(
133 "discovered root directories from $type object: $paths / ",
134 $self->dump_data_inline($paths), "\n"
135 ) if DEBUG;
136 22 ▶ unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
Branch: 1 branch
BranchTrueFalseTotal
ref $paths eq 'ARRAY' ? : 0 22 22
137 22 next;
138 }
139 }
140  
141 0 $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 8 ▶ if @paths;
Branch: 1 branch
BranchTrueFalseTotal
if @paths 0 8 8
147  
148 # we can cache roots if all are static and the dynamic flag isn't set
149 $self->{ roots } = \@roots
150 8 ▶ unless $self->{ dynamic };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dynamic"} 5 3 8
151  
152 8 $self->debug("resolved roots: [\n ", join("\n ", @roots), "\n]\n") if DEBUG;
153  
154 return wantarray
155 ? @roots
156 8 ▶ : \@roots;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 1 8
157 }
158  
159 sub definitive_paths {
160 0 ▶ my $self = shift;
Subroutine definitive_paths: not called CC=2 SCAR=17.9
Pod documented
161 0 my $path = $self->absolute(@_);
162 0 my @paths = map { $self->merge_paths($_, $path) } $self->roots;
163 return wantarray
164 ? @paths
165 0 ▶ : \@paths;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
166 }
167  
168 sub definitive_write {
169 9 ▶ my $self = shift;
Subroutine definitive_write: called CC=1 SCAR=0.0
Pod documented
170 9 $self->debug("definitive_write(", join(', ', @_), ")\n") if DEBUG;
171 9 my $path = $self->absolute(@_);
172 9 return $self->join_directory($self->roots->[0], $path);
173 }
174  
175 sub definitive_read {
176 63 ▶ my $self = shift;
Subroutine definitive_read: called CC=3 SCAR=11.0
Pod documented
177 63 my $path = $self->absolute(@_);
178 63 my ($base, $full);
179  
180 63 $self->debug("definitive_read($path)") if DEBUG;
181  
182 63 foreach $base ($self->roots) {
183 96 $full = $self->merge_paths($base, $path);
184 96 $self->debug("looking for [$base] + [$path] => $full\n") if DEBUG;
185 96 ▶ return $full if -e $full;
Branch: 1 branch
BranchTrueFalseTotal
if -e $full 58 38 96
186 38 $self->debug("not found\n") if DEBUG;
187 }
188 5 return undef;
189 }
190  
191 sub read_directory {
192 4 ▶ my $self = shift;
Subroutine read_directory: called CC=9 SCAR=23.0
Pod documented
193 4 my $path = $self->absolute(shift);
194 4 my $all = shift;
195 4 my ($base, $full, $dirh, $item, @items, %seen);
196  
197 4 require IO::Dir;
198  
199 4 foreach $base ($self->roots) {
200 10 $full = $self->join_directory($base, $path);
201 10 $self->debug("Opening directory: $full\n") if DEBUG;
202 10 ▶ $dirh = IO::Dir->new($full)
Condition50%1/2
'IO::Dir'->new($full) || (next)
l!l
100
MC/DC0%0/1
'IO::Dir'->new($full) || (next)
'IO::Dir'->new($full)
Truth table50%1/2
A
A= 'IO::Dir'->new($full)
Aresult
11
00
203 || next;
204 # Some directory may not exist, so ignore them
205 # || $self->error_msg( open_failed => directory => $full => $! );
206 10 ▶ while (defined ($item = $dirh->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($item = $dirh->read)) 57 10 67
207 57 if (DEBUG) {
208 if ($seen{ $item }) {
209 $self->debug("skipping $item (already seen)");
210 }
211 else {
212 $self->debug("adding $item");
213 }
214 }
215 57 ▶ push(@items, $item) unless $seen{ $item }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$item}++ 39 18 57
216 }
217 10 $dirh->close;
218 }
219 @items = $self->FILESPEC->no_upwards(@items)
220 4 ▶ unless $all || ref $self && $self->{ all_entries };
Branch: 1 branch
BranchTrueFalseTotal
unless $all or ref $self and $self->{"all_entries"} 4 0 4
Condition33%2/6
$all or ref $self and $self->{"all_entries"}
l!l&&r!l&&!r
004
ref $self and $self->{"all_entries"}
!ll&&!rl&&r
040
MC/DC0%0/3
$all or ref $self and $self->{"all_entries"}
$all ref $self $self->{"all_entries"}
Truth table25%1/4
A or B and C
A= $allB= ref $selfC= $self->{"all_entries"}
ABCresult
1XX1
0111
00X0
0100
221  
222 4 ▶ return wantarray ? @items : \@items;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
223 }
224  
225  
226 6 1;
227  
228 __END__
« lib/Badger/Period.pm ↑ Summary lib/Badger/Timestamp.pm »