POSIX/Real/Directory.pm

filter: Statementstmt 77.4% 96 / 124 Branchbran 53.7% 29 / 54 Conditioncond 55.5% 5 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 85.7% 18 / 21 Podpod 85.7% 12 / 14 totaltotal 70.6% 161 / 228 CC 32 SCAR 23.6
CC
32
Cov
70%
delete
56.0
read
6.4
rewind
6.0
CRAP 10.6
« POSIX/ReducedPrivileges/Directory.pm ↑ Summary POSIX/ReducedPrivileges/Inode.pm »
1 # Copyright (c) 2016, cPanel, Inc.
2 # All rights reserved.
3 # http://cpanel.net/
4 #
5 # This is free software; you can redistribute it and/or modify it under the same
6 # terms as Perl itself. See the LICENSE file for further details.
7  
8 package Filesys::POSIX::Real::Directory;
9  
10 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use Filesys::POSIX::Bits;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Filesys::POSIX::Directory ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 our @ISA = qw(Filesys::POSIX::Directory);
19  
20 21 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
21 21 my ( $class, $path, $inode ) = @_;
22  
23 return bless {
24 'path' => $path,
25 'inode' => $inode,
26 'mtime' => 0,
27 'overlays' => {},
28 'detached' => {},
29 'skipped' => {},
30 'members' => {
31 '.' => $inode,
32 21 ▶ '..' => $inode->{'parent'} ? $inode->{'parent'} : $inode
Branch: 1 branch
BranchTrueFalseTotal
$inode->{'parent'} ? : 11 10 21
33 }
34 }, $class;
35 }
36  
37 2 ▶sub _sync_all {
Subroutine _sync_all: called CC=4 SCAR=14.0
38 2 my ($self) = @_;
39 2 ▶ my $mtime = ( lstat $self->{'path'} )[9] or Carp::confess("$!");
Branch: 1 branch
BranchTrueFalseTotal
my $mtime = (lstat $self->{"path"})[9] or Carp::confess("$!") 2 0 2
40  
41 2 ▶ return unless $mtime > $self->{'mtime'};
Branch: 1 branch
BranchTrueFalseTotal
unless $mtime > $self->{"mtime"} 0 2 2
42  
43 2 $self->open;
44  
45 # This uses readdir to bypass the aliased/detached overlays
46 2 ▶ while ( my $item = readdir $self->{'dh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir $self->{"dh"})) 9 2 11
47 9 $self->_sync_member($item);
48 }
49  
50 2 $self->close;
51  
52 2 $self->{'mtime'} = $mtime;
53 }
54  
55 40 ▶sub _sync_member {
Subroutine _sync_member: called CC=5 SCAR=16.2
56 40 my ( $self, $name ) = @_;
57 40 my $subpath = "$self->{'path'}/$name";
58 40 my @st = lstat $subpath;
59  
60 40 ▶ if ( scalar @st == 0 && $!{'ENOENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @st == 0 and $!{"ENOENT"}) 15 25 40
Condition66%2/3
scalar @st == 0 and $!{"ENOENT"}
!ll&&!rl&&r
25015
MC/DC50%1/2
scalar @st == 0 and $!{"ENOENT"}
scalar @st == 0 $!{"ENOENT"}
Truth table66%2/3
A and B
A= scalar @st == 0B= $!{"ENOENT"}
ABresult
0X0
100
111
61 15 delete $self->{'members'}->{$name};
62 15 return;
63 }
64  
65 25 ▶ Carp::confess("$!") unless @st;
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 25 25
66  
67 25 ▶ if ( exists $self->{'members'}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'members'}{$name}) { } 7 18 25
68 7 $self->{'members'}->{$name}->update(@st);
69 }
70 else {
71 $self->{'members'}->{$name} = Filesys::POSIX::Real::Inode->from_disk(
72 $subpath,
73 'st_info' => \@st,
74 'dev' => $self->{'inode'}->{'dev'},
75 18 'parent' => $self->{'inode'}
76 );
77 }
78 }
79  
80 45 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
81 45 my ( $self, $name ) = @_;
82 45 ▶ return undef if ( exists $self->{'detached'}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"detached"}{$name} 2 43 45
83  
84 43 ▶ return $self->{'overlays'}->{$name} if ( exists $self->{'overlays'}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"overlays"}{$name} 14 29 43
85  
86 29 ▶ $self->_sync_member($name) unless exists $self->{'members'}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"members"}{$name} 17 12 29
87 29 return $self->{'members'}->{$name};
88 }
89  
90 9 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
91 9 my ( $self, $name, $inode ) = @_;
92 9 delete $self->{'detached'}->{$name};
93 9 $self->{'overlays'}->{$name} = $inode;
94 9 return $inode;
95 }
96  
97 2 ▶sub rename_member {
Subroutine rename_member: called CC=2 SCAR=7.2
Pod documented
98 2 my ( $self, undef, $olddir, $oldname, $newname ) = @_;
99  
100 # TODO: This operation has no awareness of aliasing and detaching
101 return rename( $olddir->path . '/' . $oldname, $self->path . '/' . $newname )
102 2 ▶ && do {
Condition33%1/3
rename($olddir->path . '/' . $oldname, $self->path . '/' . $newname) && do { $olddir->_sync_member($oldname); $self->_sync_member($newname); 1 }
!ll&&!rl&&r
020
MC/DC0%0/2
rename($olddir->path . '/' . $oldname, $self->path . '/' . $newname) && do { $olddir->_sync_member($oldname); $self->_sync_member($newname); 1 }
rename($olddir->path . '/' . $oldname, $self->path . '/' . $newname) do { $olddir->_sync_member($oldname); $self->_sync_member($newname); 1 }
Truth table33%1/3
A && B
A= rename($olddir->path . '/' . $oldname, $self->path . '/' . $newname)B= do { $olddir->_sync_member($oldname); $self->_sync_member($newname); 1 }
ABresult
0X0
100
111
103 2 $olddir->_sync_member($oldname);
104 2 $self->_sync_member($newname);
105 2 1;
106 };
107 }
108  
109 10 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.3
Pod documented
110 10 my ( $self, $name ) = @_;
111 10 ▶ return '' if exists $self->{'detached'}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"detached"}{$name} 0 10 10
112 10 ▶ return 1 if exists $self->{'overlays'}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"overlays"}{$name} 0 10 10
113  
114 10 $self->_sync_member($name);
115 10 return exists $self->{'members'}->{$name};
116 }
117  
118 0 ▶sub delete {
Subroutine delete: not called CC=7 SCAR=40.3
Pod documented
119 0 my ( $self, $name ) = @_;
120  
121 0 ▶ if ( exists $self->{'detached'}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"detached"}{$name}) 0 0 0
122 0 return delete $self->{'detached'}->{$name};
123 }
124  
125 0 ▶ if ( exists $self->{'overlays'}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"overlays"}{$name}) 0 0 0
126 0 return delete $self->{'overlays'}->{$name};
127 }
128  
129 0 ▶ my $member = $self->{'members'}->{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $member = $self->{"members"}{$name} or return 0 0 0
130 0 my $subpath = "$self->{'path'}/$name";
131  
132 0 ▶ if ( $member->dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member->dir) { } 0 0 0
133 0 rmdir($subpath);
134 }
135 else {
136 0 unlink($subpath);
137 }
138  
139 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
140 0 ▶ Carp::confess("$!") unless $!{'ENOENT'};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"ENOENT"} 0 0 0
141 }
142  
143 0 my $now = time;
144 0 @{ $self->{'inode'} }{qw(mtime ctime)} = ( $now, $now );
145  
146 0 my $inode = $self->{'members'}->{$name};
147 0 delete $self->{'members'}->{$name};
148  
149 0 return $inode;
150 }
151  
152 1 ▶sub detach {
Subroutine detach: called CC=4 SCAR=14.2
Pod documented
153 1 my ( $self, $name ) = @_;
154  
155 1 ▶ return undef if ( exists $self->{'detached'}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"detached"}{$name} 0 1 1
156  
157 1 $self->{'detached'}->{$name} = undef;
158  
159 1 foreach my $table (qw(members overlays)) {
160 1 ▶ next unless exists $self->{$table}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$table}{$name} 0 1 1
161  
162 1 return $self->{$table}->{$name};
163 }
164 }
165  
166 2 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
167 2 my ( $self, $name ) = @_;
168 2 $self->_sync_all;
169  
170 2 my %union = ( %{ $self->{'members'} }, %{ $self->{'overlays'} } );
171  
172 2 delete @union{ keys %{ $self->{'detached'} } };
173  
174 2 return keys %union;
175 }
176  
177 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
178 0 scalar( shift->list );
179 }
180  
181 7 ▶sub open {
Subroutine open: called CC=2 SCAR=7.0
Pod documented
182 7 my ($self) = @_;
183  
184 7 @{ $self->{'skipped'} }{ keys %{ $self->{'overlays'} } } =
185 7 values %{ $self->{'overlays'} };
186  
187 7 $self->close;
188  
189 7 ▶ opendir( $self->{'dh'}, $self->{'path'} ) or Carp::confess("$!");
Branch: 1 branch
BranchTrueFalseTotal
opendir $self->{"dh"}, $self->{"path"} or Carp::confess("$!") 7 0 7
190  
191 7 return $self;
192 }
193  
194 0 ▶sub rewind {
Subroutine rewind: not called CC=2 SCAR=17.9
Pod documented
195 0 my ($self) = @_;
196  
197 0 @{ $self->{'skipped'} }{ keys %{ $self->{'overlays'} } } =
198 0 values %{ $self->{'overlays'} };
199  
200 0 ▶ if ( $self->{'dh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dh"}) 0 0 0
201 0 rewinddir $self->{'dh'};
202 }
203  
204 0 return;
205 }
206  
207 34 ▶sub read {
Subroutine read: called CC=6 SCAR=18.5
Pod documented
208 34 my ($self) = @_;
209 34 my $item;
210  
211 do {
212 34 ▶ if ( $self->{'dh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dh"}) 34 0 34
213 34 $item = readdir $self->{'dh'};
214 }
215  
216 34 ▶ if ( defined $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item) { } 29 5 34
217 29 delete $self->{'skipped'}->{$item};
218 }
219 else {
220 5 $item = each %{ $self->{'skipped'} };
221 }
222 34 ▶ } while ( $item && exists $self->{'detached'}->{$item} );
Branch: 1 branch
BranchTrueFalseTotal
$item and exists $self->{"detached"}{$item} and do { do { if ($self->{"dh"}) { $item = readdir $self->{"dh"}; } ; if (defined $item) { delete $self->{"skipped"}{$item}; } else { $item = each %{$$self{"skipped"};}; } } } 0 34 34
Condition66%2/3
$item and exists $self->{"detached"}{$item}
!ll&&!rl&&r
6280
MC/DC0%0/2
$item and exists $self->{"detached"}{$item}
$item exists $self->{"detached"}{$item}
Truth table66%2/3
A and B
A= $itemB= exists $self->{"detached"}{$item}
ABresult
0X0
100
111
223  
224 34 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 34 34
225 0 return ( $item, $self->get($item) );
226 }
227  
228 34 return $item;
229 }
230  
231 13 ▶sub close {
Subroutine close: called CC=2 SCAR=6.9
Pod documented
232 13 my ($self) = @_;
233  
234 13 ▶ if ( $self->{'dh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dh"}) 6 7 13
235 6 closedir $self->{'dh'};
236 6 delete $self->{'dh'};
237 }
238  
239 13 return;
240 }
241  
242 4 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod undocumented
243 4 my ($self) = @_;
244 4 return $self->{'path'};
245 }
246  
247 13 1;
« POSIX/ReducedPrivileges/Directory.pm ↑ Summary POSIX/ReducedPrivileges/Inode.pm »