Export/Unix/WriteFS.pm

filter: Statementstmt 64.8% 96 / 148 Branchbran 37.8% 50 / 132 Conditioncond 26.0% 31 / 119 MC/DCmcdc 9.2% 6 / 65 Subroutinesub 68.9% 20 / 29 Podpod 100% 7 / 7 totaltotal 42.0% 210 / 500 CC 108 SCAR 44.8
CC
108
Cov
44%
_compare_dirent
272.0
add
179.7
_apply_stat
101.7
CRAP 87.9
« Export/CPIO.pm ↑ Summary Export/VFAT.pm »
1 package Sys::Export::Unix::WriteFS;
2  
3 # ABSTRACT: An export target that writes files to a directory in the host filesystem
4 5 our $VERSION = '0.006'; # VERSION
5  
6  
7 5 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 our @CARP_NOT= qw( Sys::Export::Unix );
13 5 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Sys::Export qw( :stat_modes :stat_tests isa_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 require Sys::Export::Unix;
16  
17 7 ▶sub new {
Subroutine new: called CC=9 SCAR=24.8
Pod documented
18 7 my $class= shift;
19 0 ▶ my %attrs= @_ == 1 && isa_hash $_[0]? %{$_[0]}
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 && &isa_hash($_[0]) ? : 0 7 7
!(@_ & 1) ? : 7 0 7
Condition33%1/3
@_ == 1 and &isa_hash($_[0])
!ll&&!rl&&r
700
MC/DC0%0/2
@_ == 1 and &isa_hash($_[0])
@_ == 1 &isa_hash($_[0])
Truth table33%1/3
A and B
A= @_ == 1B= &isa_hash($_[0])
ABresult
0X0
100
111
20 : !(@_ & 1)? @_
21 : croak "Expected hashref or even-length list";
22  
23 7 ▶ defined $attrs{dst} or croak "Require 'dst' attribute";
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs{"dst"} or croak("Require 'dst' attribute") 7 0 7
24 7 ▶ my $dst_abs= abs_path($attrs{dst} =~ s,(?<=[^/])$,/,r)
Branch: 1 branch
BranchTrueFalseTotal
my $dst_abs = abs_path($attrs{"dst"} =~ s[(?<=[^/])$][/]ru) or croak(qq[dst directory '$attrs{"dst"}' does not exist]) 7 0 7
25 or croak "dst directory '$attrs{dst}' does not exist";
26 7 ▶ length $dst_abs > 1
Branch: 1 branch
BranchTrueFalseTotal
length $dst_abs > 1 or croak("cowardly refusing to export to '${dst_abs}'") 7 0 7
27 or croak "cowardly refusing to export to '$dst_abs'";
28 7 $attrs{dst_abs}= "$dst_abs/";
29  
30 7 ▶ $attrs{tmp} //= do {
Condition33%1/3
$attrs{"tmp"} //= do { my $tmp = "File::Temp"->newdir; my($tmp_dev) = stat "$tmp/"; my($dst_dev) = stat $attrs{"dst"}; $tmp = "File::Temp"->newdir("DIR", $attrs{"dst_abs"}) if $tmp_dev != $dst_dev; $tmp }
l!l&&r!l&&!r
070
MC/DC0%0/2
$attrs{"tmp"} //= do { my $tmp = "File::Temp"->newdir; my($tmp_dev) = stat "$tmp/"; my($dst_dev) = stat $attrs{"dst"}; $tmp = "File::Temp"->newdir("DIR", $attrs{"dst_abs"}) if $tmp_dev != $dst_dev; $tmp }
$attrs{"tmp"} do { my $tmp = "File::Temp"->newdir; my($tmp_dev) = stat "$tmp/"; my($dst_dev) = stat $attrs{"dst"}; $tmp = "File::Temp"->newdir("DIR", $attrs{"dst_abs"}) if $tmp_dev != $dst_dev; $tmp }
Truth table33%1/3
A //= B
A= $attrs{"tmp"}B= do { my $tmp = "File::Temp"->newdir; my($tmp_dev) = stat "$tmp/"; my($dst_dev) = stat $attrs{"dst"}; $tmp = "File::Temp"->newdir("DIR", $attrs{"dst_abs"}) if $tmp_dev != $dst_dev; $tmp }
ABresult
1X1
011
000
31 7 my $tmp= File::Temp->newdir;
32 # Make sure can rename() from this $tmp to $dst
33 7 my ($tmp_dev)= stat "$tmp/";
34 7 my ($dst_dev)= stat $attrs{dst};
35 $tmp= File::Temp->newdir(DIR => $attrs{dst_abs})
36 7 ▶ if $tmp_dev != $dst_dev;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp_dev != $dst_dev 0 7 7
37 7 $tmp;
38 };
39  
40 7 my $self= bless \%attrs, $class;
41  
42 7 return $self;
43 }
44  
45  
46 0 ▶sub dst($self) { $self->{dst} }
Subroutine dst: not called CC=1 SCAR=6.9
Pod documented
47 69 ▶sub dst_abs($self) { $self->{dst_abs} }
Subroutine dst_abs: called CC=1 SCAR=0.0
Pod documented
48 16 ▶sub tmp($self) { $self->{tmp} }
Subroutine tmp: called CC=1 SCAR=0.0
Pod documented
49  
50 0 ▶sub on_collision {
Subroutine on_collision: not called CC=2 SCAR=17.9
Pod documented
51 0 ▶ return shift->_set_on_collision(@_) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
52 shift->{on_collision}
53 0 }
54 5 my %_valid_on_collision= (
55 croak => 'croak',
56 ignore_if_same => 'croak',
57 ignore => 'ignore',
58 overwrite => 'overwrite',
59 );
60 0 ▶sub _set_on_collision($self, $val) {
Subroutine _set_on_collision: not called CC=3 SCAR=24.8
61 $self->{on_collision}= ref $val eq 'CODE'? $val
62 0 ▶ : $_valid_on_collision{$val}
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 0 0 0
Condition0%0/2
$_valid_on_collision{$val} // croak("Invalid 'on_collision' value '${val}'")
l!l
00
MC/DC0%0/1
$_valid_on_collision{$val} // croak("Invalid 'on_collision' value '${val}'")
$_valid_on_collision{$val}
Truth table0%0/2
A
A= $_valid_on_collision{$val}
Aresult
11
00
63 // croak("Invalid 'on_collision' value '$val'");
64 0 $self;
65 }
66  
67 # a hashref tracking files with link-count higher than 1, so that hardlinks can be preserved.
68 # the keys are "$dev:$ino"
69 4 ▶sub _link_map($self) { $self->{link_map} //= {} }
Subroutine _link_map: called CC=2 SCAR=6.9
Condition100%2/2
$self->{"link_map"} //= {}
l!l
22
MC/DC100%1/1
$self->{"link_map"} //= {}
$self->{"link_map"}
Truth table100%2/2
A
A= $self->{"link_map"}
Aresult
11
00
70  
71 7 ▶sub DESTROY($self, @) {
Subroutine DESTROY: called CC=2 SCAR=6.9
72 7 ▶ $self->finish if $self->{_delayed_apply_stat};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_delayed_apply_stat"} 2 5 7
73 }
74  
75  
76 20 ▶sub add($self, $file) {
Subroutine add: called CC=23 SCAR=51.9
Pod documented
77 croak "Path must start with a name, not slash or dot: '$file->{name}'"
78 20 ▶ if $file->{name} =~ m{(^|^\.|^\.\.)(/|\z)};
Branch: 1 branch
BranchTrueFalseTotal
if $file->{"name"} =~ m[(^|^\.|^\.\.)(/|\z)]u 0 20 20
79 20 ▶ my $mode= $file->{mode} // croak "attribute 'mode' is required, for '$file->{name}'";
Condition50%1/2
$file->{"mode"} // croak(qq[attribute 'mode' is required, for '$file->{"name"}'])
l!l
200
MC/DC0%0/1
$file->{"mode"} // croak(qq[attribute 'mode' is required, for '$file->{"name"}'])
$file->{"mode"}
Truth table50%1/2
A
A= $file->{"mode"}
Aresult
11
00
80 # Does it already exist?
81 20 my $dst_abs= $self->dst_abs . $file->{name};
82 20 my %old= (name => $file->{name});
83 20 ▶ if (@old{qw( dev ino mode nlink uid gid rdev size atime mtime)}= lstat($dst_abs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@old{"dev", "ino", "mode", "nlink", "uid", "gid", "rdev", "size", "atime", "mtime"} = lstat $dst_abs) 0 20 20
84 0 ▶ my $action= $self->on_collision // 'croak';
Condition0%0/2
$self->on_collision // "croak"
l!l
00
MC/DC0%0/1
$self->on_collision // "croak"
$self->on_collision
Truth table0%0/2
A
A= $self->on_collision
Aresult
11
00
85 # Nothing to do if user wants collisions ignored
86 0 ▶ return !!0 if $action eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "ignore" 0 0 0
87 0 ▶ if (defined(my $difference= $self->_compare_dirent($file, \%old))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $difference = $self->_compare_dirent($file, \%old))) 0 0 0
88 # Call user callback, if any
89 0 ▶ $action= $action->($dst_abs, $file)
Branch: 1 branch
BranchTrueFalseTotal
if ref $action eq "CODE" 0 0 0
90 if ref $action eq 'CODE';
91 # validate its return value
92 0 ▶ $action= $_valid_on_collision{$action // ''}
Condition0%0/4
$_valid_on_collision{$action // ""} // croak("Unknown on_collision action '${action}'")
l!l
00
$action // ""
l!l
00
MC/DC0%0/1
$_valid_on_collision{$action // ""} // croak("Unknown on_collision action '${action}'")
$_valid_on_collision{$action // ""}
MC/DC0%0/1
$action // ""
$action
Truth table0%0/2
A
A= $_valid_on_collision{$action // ""}
Aresult
11
00
Truth table0%0/2
A
A= $action
Aresult
11
00
93 // croak "Unknown on_collision action '$action'";
94 0 ▶ return !!0 if $action eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "ignore" 0 0 0
95 0 ▶ croak $difference unless $action eq 'overwrite';
Branch: 1 branch
BranchTrueFalseTotal
unless $action eq "overwrite" 0 0 0
96 # overwrite, but directory might not be empty, so handle that later
97 0 ▶ unlink $dst_abs unless S_ISDIR($old{mode});
Branch: 1 branch
BranchTrueFalseTotal
unless S_ISDIR($old{"mode"}) 0 0 0
98 }
99 }
100 return S_ISREG($mode)? $self->_add_file($file)
101 : S_ISDIR($mode)? $self->_add_dir($file, (defined $old{mode}? \%old : undef))
102 : S_ISLNK($mode)? $self->_add_symlink($file)
103 : (S_ISBLK($mode) || S_ISCHR($mode))? $self->_add_devnode($file)
104 : S_ISFIFO($mode)? $self->_add_fifo($file)
105 : S_ISSOCK($mode)? $self->_add_socket($file)
106 : croak "Can't export ".(S_ISWHT($mode)? 'whiteout entries' : '(unknown)')
107 20 ▶ .': "'.($file->{src_path} // $file->{name}).'"'
Branch: 8 branches
BranchTrueFalseTotal
S_ISREG($mode) ? : 6 14 20
S_ISDIR($mode) ? : 8 6 14
defined $old{'mode'} ? : 0 8 8
S_ISLNK($mode) ? : 5 1 6
S_ISBLK($mode) || S_ISCHR($mode) ? : 0 1 1
S_ISFIFO($mode) ? : 0 1 1
S_ISSOCK($mode) ? : 1 0 1
S_ISWHT($mode) ? : 0 0 0
Condition16%1/6
S_ISBLK($mode) or S_ISCHR($mode)
l!l&&r!l&&!r
001
$file->{"src_path"} // $file->{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
S_ISBLK($mode) or S_ISCHR($mode)
S_ISBLK($mode) S_ISCHR($mode)
MC/DC0%0/2
$file->{"src_path"} // $file->{"name"}
$file->{"src_path"} $file->{"name"}
Truth table33%1/3
A or B
A= S_ISBLK($mode)B= S_ISCHR($mode)
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $file->{"src_path"}B= $file->{"name"}
ABresult
1X1
011
000
108 }
109  
110 5 ▶our %_mode_name= (
Branch: 1 branch
BranchTrueFalseTotal
S_IFWHT() ? : 0 5 5
111 S_IFREG , 'file',
112 S_IFDIR , 'dir',
113 S_IFLNK , 'symlink',
114 S_IFBLK , 'block device',
115 S_IFCHR , 'char device',
116 S_IFIFO , 'fifo',
117 S_IFSOCK , 'socket',
118 (S_IFWHT? (
119 S_IFWHT , 'whiteout',
120 ):()),
121 );
122 0 ▶sub _mode_name($mode) { $_mode_name{($mode & S_IFMT)} // '(unknown)' }
Subroutine _mode_name: not called CC=2 SCAR=17.9
Condition0%0/2
$Sys::Export::Unix::WriteFS::_mode_name{$mode & S_IFMT()} // "(unknown)"
l!l
00
MC/DC0%0/1
$Sys::Export::Unix::WriteFS::_mode_name{$mode & S_IFMT()} // "(unknown)"
$Sys::Export::Unix::WriteFS::_mode_name{$mode & S_IFMT()}
Truth table0%0/2
A
A= $Sys::Export::Unix::WriteFS::_mode_name{$mode & S_IFMT()}
Aresult
11
00
123  
124 # Compare two dirents and return error msg if new is not equivalent to old
125 0 ▶sub _compare_dirent($self, $file, $old) {
Subroutine _compare_dirent: not called CC=16 SCAR=56.1
126 0 my $dst_abs= $self->dst_abs . $file->{name};
127 return "Attempt to write "._mode_name($file->{mode})." overtop existing "._mode_name($old->{mode})." at $dst_abs"
128 0 ▶ if ($file->{mode} & S_IFMT) != ($old->{mode} & S_IFMT);
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"mode"} & S_IFMT()) != ($old->{"mode"} & S_IFMT()) 0 0 0
129 return "Attempt to write ownership $file->{uid}:$file->{gid} to $dst_abs which was previously $old->{uid}:$old->{gid}"
130 0 ▶ if (defined $file->{uid} && $file->{uid} != $old->{uid}) || (defined $file->{gid} && $file->{gid} != $old->{gid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $file->{"uid"} and $file->{"uid"} != $old->{"uid"} or defined $file->{"gid"} and $file->{"gid"} != $old->{"gid"} 0 0 0
Condition0%0/9
defined $file->{"uid"} and $file->{"uid"} != $old->{"uid"} or defined $file->{"gid"} and $file->{"gid"} != $old->{"gid"}
l!l&&r!l&&!r
000
defined $file->{"uid"} and $file->{"uid"} != $old->{"uid"}
!ll&&!rl&&r
000
defined $file->{"gid"} and $file->{"gid"} != $old->{"gid"}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $file->{"uid"} and $file->{"uid"} != $old->{"uid"} or defined $file->{"gid"} and $file->{"gid"} != $old->{"gid"}
defined $file->{"uid"} $file->{"uid"} != $old->{"uid"} defined $file->{"gid"} $file->{"gid"} != $old->{"gid"}
Truth table0%0/7
A and B or C and D
A= defined $file->{"uid"}B= $file->{"uid"} != $old->{"uid"}C= defined $file->{"gid"}D= $file->{"gid"} != $old->{"gid"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
131 # For symlinks, compare only the content of the link. Permissions are ignored.
132 0 ▶ if (S_ISLNK($file->{mode})) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISLNK($file->{"mode"})) 0 0 0
133 0 my $targ= readlink $dst_abs;
134 return "Attempt to rewrite symlink $dst_abs from $targ to $file->{data}"
135 0 ▶ if $targ ne $file->{data};
Branch: 1 branch
BranchTrueFalseTotal
if $targ ne $file->{"data"} 0 0 0
136 0 return !!0;
137 }
138 # For everything else, compare permissions
139 return "Attempt to write permissions ".($file->{mode} & ~S_IFMT)." overtop existing ".($old->{mode} & ~S_IFMT)." at $dst_abs"
140 0 ▶ unless $file->{mode} == $old->{mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $file->{"mode"} == $old->{"mode"} 0 0 0
141  
142 0 ▶ if (S_ISREG($file->{mode})) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISREG($file->{'mode'})) { } 0 0 0
elsif (S_ISBLK($file->{'mode'}) or S_ISCHR($file->{'mode'})) { } 0 0 0
143 # compare file contents
144 return "Attempt to overwrite $dst_abs with different content"
145 if (defined $file->{size} && $file->{size} != $old->{size})
146 0 ▶ || !_contents_same($file, $dst_abs);
Branch: 1 branch
BranchTrueFalseTotal
if defined $file->{"size"} and $file->{"size"} != $old->{"size"} or not _contents_same($file, $dst_abs) 0 0 0
Condition0%0/6
defined $file->{"size"} and $file->{"size"} != $old->{"size"} or not _contents_same($file, $dst_abs)
l!l&&r!l&&!r
000
defined $file->{"size"} and $file->{"size"} != $old->{"size"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $file->{"size"} and $file->{"size"} != $old->{"size"} or not _contents_same($file, $dst_abs)
defined $file->{"size"} $file->{"size"} != $old->{"size"} not _contents_same($file, $dst_abs)
Truth table0%0/5
A and B or not(C)
A= defined $file->{"size"}B= $file->{"size"} != $old->{"size"}C= not _contents_same($file, $dst_abs)
ABCresult
11X1
0X11
1011
0X00
1000
147 }
148 ▶ elsif (S_ISBLK($file->{mode}) || S_ISCHR($file->{mode})) {
Condition0%0/3
S_ISBLK($file->{"mode"}) or S_ISCHR($file->{"mode"})
l!l&&r!l&&!r
000
MC/DC0%0/2
S_ISBLK($file->{"mode"}) or S_ISCHR($file->{"mode"})
S_ISBLK($file->{"mode"}) S_ISCHR($file->{"mode"})
Truth table0%0/3
A or B
A= S_ISBLK($file->{"mode"})B= S_ISCHR($file->{"mode"})
ABresult
1X1
011
000
149 # compare major/minor numbers
150 return "Attempt to overwrite $dst_abs with different major/minor value"
151 0 ▶ if $file->{rdev} != $old->{rdev};
Branch: 1 branch
BranchTrueFalseTotal
if $file->{"rdev"} != $old->{"rdev"} 0 0 0
152 }
153 0 undef;
154 }
155  
156 # Compare file contents for equality
157 0 ▶sub _contents_same($file, $dst_abs) {
Subroutine _contents_same: not called CC=1 SCAR=6.9
158 0 my $dst_data= Sys::Export::map_or_load_file($dst_abs);
159 0 return ${$file->{data}} eq $$dst_data;
160 }
161  
162 # compare device nodes for equality
163 0 ▶sub _rdev_same($file, $old) {
Subroutine _rdev_same: not called CC=5 SCAR=34.0
164 # old will always have 'rdev' defined because we ran lstat.
165 # file might not if the user specified rdev_major and rdev_minor
166 0 ▶ if (defined $file->{rdev}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $file->{'rdev'}) { } 0 0 0
elsif (defined $file->{'rdev_major'} and defined $file->{'rdev_minor'}) { } 0 0 0
167 0 return $file->{rdev} == $old->{rdev};
168 ▶ } elsif (defined $file->{rdev_major} && defined $file->{rdev_minor}) {
Condition0%0/3
defined $file->{"rdev_major"} and defined $file->{"rdev_minor"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file->{"rdev_major"} and defined $file->{"rdev_minor"}
defined $file->{"rdev_major"} defined $file->{"rdev_minor"}
Truth table0%0/3
A and B
A= defined $file->{"rdev_major"}B= defined $file->{"rdev_minor"}
ABresult
0X0
100
111
169 0 my ($maj, $min)= Sys::Export::Unix::_dev_major_minor($old->{rdev});
170 0 ▶ return $file->{rdev_major} == $maj && $file->{rdev_minor} == $min;
Condition0%0/3
$file->{'rdev_major'} == $maj && $file->{'rdev_minor'} == $min
!ll&&!rl&&r
000
MC/DC0%0/2
$file->{'rdev_major'} == $maj && $file->{'rdev_minor'} == $min
$file->{'rdev_major'} == $maj $file->{'rdev_minor'} == $min
Truth table0%0/3
A && B
A= $file->{'rdev_major'} == $majB= $file->{'rdev_minor'} == $min
ABresult
0X0
100
111
171 } else {
172 0 return !!0; # not defined, so can't be same
173 }
174 }
175  
176 # Install a file into ->dst
177 6 ▶sub _add_file($self, $file) {
Subroutine _add_file: called CC=11 SCAR=25.6
178 6 my $dst= $self->dst_abs . $file->{name};
179 # See if this is supposed to be a hardlink
180 6 ▶ if ($file->{nlink} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"nlink"} > 1) 1 5 6
181 1 ▶ if (defined(my $already= $self->_link_map->{"$file->{dev}:$file->{ino}"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $already = $self->_link_map->{qq[$file->{"dev"}:$file->{"ino"}]})) 1 0 1
182 # Yep, make a link of that file instead of copying again
183 1 ▶ link($already, $dst)
Branch: 1 branch
BranchTrueFalseTotal
link $already, $dst or croak("link($already, $dst): $!") 1 0 1
184 or croak "link($already, $dst): $!";
185 1 return !!1;
186 }
187 }
188 # Record all file inodes in case a delayed hardlink is created by the caller
189 $self->_link_map->{"$file->{dev}:$file->{ino}"}= $dst
190 5 ▶ if defined $file->{dev} && defined $file->{ino};
Branch: 1 branch
BranchTrueFalseTotal
if defined $file->{"dev"} and defined $file->{"ino"} 3 2 5
Condition66%2/3
defined $file->{"dev"} and defined $file->{"ino"}
!ll&&!rl&&r
203
MC/DC50%1/2
defined $file->{"dev"} and defined $file->{"ino"}
defined $file->{"dev"} defined $file->{"ino"}
Truth table66%2/3
A and B
A= defined $file->{"dev"}B= defined $file->{"ino"}
ABresult
0X0
100
111
191 # Write data into a temp file on same filesystem, then rename it into place
192 # to ensure we never write a partial file into the destination.
193 # But, check if the caller gave us a LazyFileData within our ->tmp directory.
194 5 my $tmp;
195 5 ▶ if (blessed $file->{data} && $file->{data}->can('abs_path')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($file->{'data'}) and $file->{'data'}->can('abs_path') and substr($file->{'data'}->abs_path // '', 0, length $self->tmp) eq $self->tmp) { } 0 5 5
Condition62%5/8
builtin::blessed($file->{"data"}) and $file->{"data"}->can("abs_path") and substr($file->{"data"}->abs_path // "", 0, length $self->tmp) eq $self->tmp
!ll&&!rl&&r
320
builtin::blessed($file->{"data"}) and $file->{"data"}->can("abs_path")
!ll&&!rl&&r
302
$file->{"data"}->abs_path // ""
l!l
20
MC/DC0%0/3
builtin::blessed($file->{"data"}) and $file->{"data"}->can("abs_path") and substr($file->{"data"}->abs_path // "", 0, length $self->tmp) eq $self->tmp
builtin::blessed($file->{"data"}) $file->{"data"}->can("abs_path") substr($file->{"data"}->abs_path // "", 0, length $self->tmp) eq $self->tmp
MC/DC0%0/1
$file->{"data"}->abs_path // ""
$file->{"data"}->abs_path
Truth table50%2/4
A and B and C
A= builtin::blessed($file->{"data"})B= $file->{"data"}->can("abs_path")C= substr($file->{"data"}->abs_path // "", 0, length $self->tmp) eq $self->tmp
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $file->{"data"}->abs_path
Aresult
11
00
196 && substr($file->{data}->abs_path//'', 0, length $self->tmp) eq $self->tmp
197 ) {
198 0 $tmp= $file->{data}->abs_path;
199 } else {
200 5 $tmp= File::Temp->new(DIR => $self->tmp, UNLINK => 0);
201 5 Sys::Export::Unix::_syswrite_all($tmp, $file->{data});
202 }
203 # Apply matching permissions and ownership
204 5 $self->_apply_stat("$tmp", $file);
205 # Rename the temp file into place
206 5 ▶ rename($tmp, $dst) or croak "rename($tmp, $dst): $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $dst or croak("rename($tmp, $dst): $!") 5 0 5
207 }
208  
209 # Install a dir into ->dst, unless it already exists
210 8 ▶sub _add_dir($self, $dir, $old) {
Subroutine _add_dir: called CC=3 SCAR=12.7
211 8 my $dst_abs= $self->dst_abs . $dir->{name};
212 # If the directory already exists, just apply the permissions
213 8 ▶ mkdir($dst_abs) || croak "mkdir($dst_abs): $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $old 8 0 8
Condition40%2/5
$old || (mkdir($dst_abs) || croak("mkdir($dst_abs): $!"))
l!l&&r!l&&!r
080
mkdir $dst_abs or croak("mkdir($dst_abs): $!")
l!l
80
MC/DC0%0/2
$old || (mkdir($dst_abs) || croak("mkdir($dst_abs): $!"))
$old mkdir $dst_abs
Truth table33%1/3
A || B
A= $oldB= mkdir $dst_abs
ABresult
1X1
011
000
214 unless $old;
215 8 $self->_apply_stat($dst_abs, $dir);
216 }
217  
218 # Install a symlink into ->dst
219 5 ▶sub _add_symlink($self, $file) {
Subroutine _add_symlink: called CC=3 SCAR=11.4
220 5 my $dst_abs= $self->dst_abs . $file->{name};
221 length $file->{data}
222 5 ▶ or croak "Missing symlink contents for $file->{name}";
Branch: 1 branch
BranchTrueFalseTotal
length $file->{"data"} or croak(qq[Missing symlink contents for $file->{"name"}]) 5 0 5
223 5 ▶ symlink($file->{data}, $dst_abs)
Branch: 1 branch
BranchTrueFalseTotal
symlink $file->{"data"}, $dst_abs or croak(qq[symlink($file->{"data"}, $dst_abs): $!]) 5 0 5
224 or croak "symlink($file->{data}, $dst_abs): $!";
225 5 $self->_apply_stat($dst_abs, $file);
226 }
227  
228 # Install a device node into ->dst
229 0 ▶sub _add_devnode($self, $file) {
Subroutine _add_devnode: not called CC=6 SCAR=37.4
230 0 ▶ if (defined $file->{rdev} && (!defined $file->{rdev_major} || !defined $file->{rdev_minor})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file->{"rdev"} and not defined $file->{"rdev_major"} && defined $file->{"rdev_minor"}) 0 0 0
Condition0%0/6
defined $file->{"rdev"} and not defined $file->{"rdev_major"} && defined $file->{"rdev_minor"}
!ll&&!rl&&r
000
defined $file->{'rdev_major'} && defined $file->{'rdev_minor'}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $file->{"rdev"} and not defined $file->{"rdev_major"} && defined $file->{"rdev_minor"}
defined $file->{"rdev"} defined $file->{'rdev_major'} defined $file->{'rdev_minor'}
Truth table0%0/4
A and not(B && C)
A= defined $file->{"rdev"}B= defined $file->{'rdev_major'}C= defined $file->{'rdev_minor'}
ABCresult
0XX0
1110
10X1
1101
231 0 my ($major,$minor)= Sys::Export::Unix::_dev_major_minor($file->{rdev});
232 0 ▶ $file->{rdev_major} //= $major;
Condition0%0/3
$file->{"rdev_major"} //= $major
l!l&&r!l&&!r
000
MC/DC0%0/2
$file->{"rdev_major"} //= $major
$file->{"rdev_major"} $major
Truth table0%0/3
A //= B
A= $file->{"rdev_major"}B= $major
ABresult
1X1
011
000
233 0 ▶ $file->{rdev_minor} //= $minor;
Condition0%0/3
$file->{"rdev_minor"} //= $minor
l!l&&r!l&&!r
000
MC/DC0%0/2
$file->{"rdev_minor"} //= $minor
$file->{"rdev_minor"} $minor
Truth table0%0/3
A //= B
A= $file->{"rdev_minor"}B= $minor
ABresult
1X1
011
000
234 }
235 0 my $dst_abs= $self->dst_abs . $file->{name};
236 0 Sys::Export::Unix::_mknod_or_die($dst_abs, $file->{mode}, $file->{rdev_major}, $file->{rdev_minor});
237 0 $self->_apply_stat($dst_abs, $file);
238 }
239  
240 # Install a fifo into ->dst
241 0 ▶sub _add_fifo($self, $file) {
Subroutine _add_fifo: not called CC=2 SCAR=17.9
242 0 require POSIX;
243 0 my $dst_abs= $self->dst_abs . $file->{name};
244 POSIX::mkfifo($dst_abs, $file->{mode})
245 0 ▶ or croak "mkfifo($dst_abs): $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::mkfifo($dst_abs, $file->{"mode"}) or croak("mkfifo($dst_abs): $!") 0 0 0
246 0 $self->_apply_stat($dst_abs, $file);
247 }
248  
249 # Bind a socket (thus creating it) in ->dst
250 1 ▶sub _add_socket($self, $file) {
Subroutine _add_socket: called CC=3 SCAR=11.3
251 1 require Socket;
252 1 my $dst_abs= $self->dst_abs . $file->{name};
253 1 ▶ socket(my $s, Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0) or die "socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket my $s, Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0 or die "socket: $!" 1 0 1
254 1 ▶ bind($s, Socket::pack_sockaddr_un($dst_abs)) or die "Failed to bind socket at $dst_abs: $!";
Branch: 1 branch
BranchTrueFalseTotal
bind $s, Socket::pack_sockaddr_un($dst_abs) or die "Failed to bind socket at ${dst_abs}: $!" 1 0 1
255 1 $self->_apply_stat($dst_abs, $file);
256 }
257  
258  
259 2 ▶sub finish($self) {
Subroutine finish: called CC=2 SCAR=6.9
Pod documented
260 2 my $todo= delete $self->{_delayed_apply_stat};
261 # Reverse sort causes child directories to be updated before parents,
262 # which is required for updating mtimes.
263 $self->_delayed_apply_stat(@$_)
264 2 for sort { $b->[0] cmp $a->[0] } @$todo;
265 # free the temp directory if it was located within /dst_abs
266 2 undef $self->{tmp};
267 }
268  
269 # Apply permissions and mtime to a path
270 19 ▶sub _apply_stat($self, $abs_path, $stat) {
Subroutine _apply_stat: called CC=26 SCAR=46.2
271 19 ▶ my ($mode, $uid, $gid, $atime, $mtime)= (lstat $abs_path)[2,4,5,8,9]
Branch: 1 branch
BranchTrueFalseTotal
() = (lstat $abs_path)[2, 4, 5, 8, 9] or croak("Failed to stat file just created at '${abs_path}': $!") 19 0 19
272 or croak "Failed to stat file just created at '$abs_path': $!";
273 19 ▶ my $change_uid= defined $stat->{uid} && $stat->{uid} != $uid;
Condition66%2/3
defined $stat->{'uid'} && $stat->{'uid'} != $uid
!ll&&!rl&&r
5140
MC/DC0%0/2
defined $stat->{'uid'} && $stat->{'uid'} != $uid
defined $stat->{'uid'} $stat->{'uid'} != $uid
Truth table66%2/3
A && B
A= defined $stat->{'uid'}B= $stat->{'uid'} != $uid
ABresult
0X0
100
111
274 19 ▶ my $change_gid= defined $stat->{gid} && $stat->{gid} != $gid;
Condition66%2/3
defined $stat->{'gid'} && $stat->{'gid'} != $gid
!ll&&!rl&&r
5140
MC/DC0%0/2
defined $stat->{'gid'} && $stat->{'gid'} != $gid
defined $stat->{'gid'} $stat->{'gid'} != $gid
Truth table66%2/3
A && B
A= defined $stat->{'gid'}B= $stat->{'gid'} != $gid
ABresult
0X0
100
111
275 19 ▶ if ($change_uid || $change_gid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($change_uid or $change_gid) 0 19 19
Condition33%1/3
$change_uid or $change_gid
l!l&&r!l&&!r
0019
MC/DC0%0/2
$change_uid or $change_gid
$change_uid $change_gid
Truth table33%1/3
A or B
A= $change_uidB= $change_gid
ABresult
1X1
011
000
276 # only UID 0 can change UID, and only GID 0 or GID in supplemental groups can change GID.
277 0 ▶ $uid= -1 unless $change_uid && $> == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $change_uid and $> == 0 0 0 0
Condition0%0/3
$change_uid and $> == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$change_uid and $> == 0
$change_uid $> == 0
Truth table0%0/3
A and B
A= $change_uidB= $> == 0
ABresult
0X0
100
111
278 0 ▶ $gid= -1 unless $change_gid && ($) == 0 || grep $stat->{gid}, split / /, $) );
Branch: 1 branch
BranchTrueFalseTotal
unless $change_gid and $) == 0 || grep($stat->{"gid"}, split(/ /u, $), 0)) 0 0 0
Condition0%0/6
$change_gid and $) == 0 || grep($stat->{"gid"}, split(/ /u, $), 0))
!ll&&!rl&&r
000
$) == 0 or grep $stat->{"gid"}, split(/ /u, $), 0)
l!l&&r!l&&!r
000
MC/DC0%0/3
$change_gid and $) == 0 || grep($stat->{"gid"}, split(/ /u, $), 0))
$change_gid $) == 0 grep $stat->{"gid"}, split(/ /u, $), 0)
Truth table0%0/4
A and B or C
A= $change_gidB= $) == 0C= grep $stat->{"gid"}, split(/ /u, $), 0)
ABCresult
0XX0
1000
11X1
1011
279 # Only attempt change if able
280 0 ▶ POSIX::lchown($uid, $gid, $abs_path) or croak "lchown($uid, $gid, $abs_path): $!"
Branch: 1 branch
BranchTrueFalseTotal
if $uid >= 0 or $gid >= 0 0 0 0
Condition0%0/8
($uid >= 0 || $gid >= 0) && (POSIX::lchown($uid, $gid, $abs_path) || croak("lchown($uid, $gid, $abs_path): $!"))
!ll&&!rl&&r
000
$uid >= 0 or $gid >= 0
l!l&&r!l&&!r
000
POSIX::lchown($uid, $gid, $abs_path) or croak("lchown($uid, $gid, $abs_path): $!")
l!l
00
MC/DC0%0/3
($uid >= 0 || $gid >= 0) && (POSIX::lchown($uid, $gid, $abs_path) || croak("lchown($uid, $gid, $abs_path): $!"))
$uid >= 0 $gid >= 0 POSIX::lchown($uid, $gid, $abs_path)
Truth table0%0/5
A or B && C
A= $uid >= 0B= $gid >= 0C= POSIX::lchown($uid, $gid, $abs_path)
ABCresult
00X0
1X00
0100
1X11
0111
281 if $uid >= 0 || $gid >= 0;
282 }
283  
284 19 my @delayed;
285  
286 # Don't change permission bits on symlinks
287 19 ▶ if (!S_ISLNK($mode) && ($mode & 0xFFF) != ($stat->{mode} & 0xFFF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not S_ISLNK($mode) and ($mode & 4095) != ($stat->{"mode"} & 4095)) 6 13 19
Condition100%3/3
not S_ISLNK($mode) and ($mode & 4095) != ($stat->{"mode"} & 4095)
!ll&&!rl&&r
586
MC/DC100%2/2
not S_ISLNK($mode) and ($mode & 4095) != ($stat->{"mode"} & 4095)
not S_ISLNK($mode) ($mode & 4095) != ($stat->{"mode"} & 4095)
Truth table100%3/3
not(A) and B
A= not S_ISLNK($mode)B= ($mode & 4095) != ($stat->{"mode"} & 4095)
ABresult
0X0
100
111
288 # If changing permissions on a directory to something that removes our ability
289 # to write to it, delay this change until the end.
290 6 ▶ if (S_ISDIR($mode) && !(($stat->{mode} & 0222) && ($stat->{mode} & 0111))) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR($mode) and not $stat->{'mode'} & 146 && $stat->{'mode'} & 73) { } 0 6 6
Condition50%3/6
S_ISDIR($mode) and not $stat->{"mode"} & 146 && $stat->{"mode"} & 73
!ll&&!rl&&r
510
$stat->{'mode'} & 146 && $stat->{'mode'} & 73
!ll&&!rl&&r
001
MC/DC0%0/3
S_ISDIR($mode) and not $stat->{"mode"} & 146 && $stat->{"mode"} & 73
S_ISDIR($mode) $stat->{'mode'} & 146 $stat->{'mode'} & 73
Truth table50%2/4
A and not(B && C)
A= S_ISDIR($mode)B= $stat->{'mode'} & 146C= $stat->{'mode'} & 73
ABCresult
0XX0
1110
10X1
1101
291 0 push @delayed, 'chmod';
292 }
293 else {
294 chmod $stat->{mode}&0xFFF, $abs_path
295 6 ▶ or croak sprintf "chmod(0%o, %s): $!", $stat->{mode}&0xFFF, $abs_path;
Branch: 1 branch
BranchTrueFalseTotal
chmod $stat->{"mode"} & 4095, $abs_path or croak(sprintf("chmod(0%o, %s): $!", $stat->{"mode"} & 4095, $abs_path)) 6 0 6
296 }
297 }
298  
299 19 ▶ if (!S_ISLNK($mode) && (defined $stat->{mtime} || defined $stat->{atime})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not S_ISLNK($mode) and defined $stat->{"mtime"} || defined $stat->{"atime"}) 9 10 19
Condition83%5/6
not S_ISLNK($mode) and defined $stat->{"mtime"} || defined $stat->{"atime"}
!ll&&!rl&&r
559
defined $stat->{"mtime"} or defined $stat->{"atime"}
l!l&&r!l&&!r
905
MC/DC66%2/3
not S_ISLNK($mode) and defined $stat->{"mtime"} || defined $stat->{"atime"}
not S_ISLNK($mode) defined $stat->{"mtime"} defined $stat->{"atime"}
Truth table75%3/4
not(A) and B or C
A= not S_ISLNK($mode)B= defined $stat->{"mtime"}C= defined $stat->{"atime"}
ABCresult
0XX0
1000
11X1
1011
300 9 ▶ if (S_ISDIR($mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR($mode)) { } 5 4 9
301 # No point in applying mtime to a directory now, because it will get
302 # changed when sub-entries get written.
303 5 push @delayed, 'utime';
304 }
305 else {
306 4 ▶ utime $stat->{atime}, $stat->{mtime}, $abs_path
Branch: 1 branch
BranchTrueFalseTotal
utime $stat->{"atime"}, $stat->{"mtime"}, $abs_path or warn "utime($abs_path): $!" 4 0 4
307 or warn "utime($abs_path): $!";
308 }
309 }
310  
311 19 ▶ push @{$self->{_delayed_apply_stat}}, [ $abs_path, $stat, @delayed ]
Branch: 1 branch
BranchTrueFalseTotal
if @delayed 5 14 19
312 if @delayed;
313 }
314 5 ▶sub _delayed_apply_stat($self, $abs_path, $stat, @delayed) {
Subroutine _delayed_apply_stat: called CC=5 SCAR=20.9
315 5 ▶ if (grep $_ eq 'chmod', @delayed) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq "chmod"), @delayed)) 0 5 5
316 chmod $stat->{mode}&0xFFF, $abs_path
317 0 ▶ or croak sprintf "chmod(0%o, %s): $!", $stat->{mode}&0xFFF, $abs_path;
Branch: 1 branch
BranchTrueFalseTotal
chmod $stat->{"mode"} & 4095, $abs_path or croak(sprintf("chmod(0%o, %s): $!", $stat->{"mode"} & 4095, $abs_path)) 0 0 0
318 }
319 5 ▶ if (grep $_ eq 'utime', @delayed) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq "utime"), @delayed)) 5 0 5
320 5 ▶ utime $stat->{atime}, $stat->{mtime}, $abs_path
Branch: 1 branch
BranchTrueFalseTotal
utime $stat->{"atime"}, $stat->{"mtime"}, $abs_path or warn "utime($abs_path): $!" 5 0 5
321 or warn "utime($abs_path): $!";
322 }
323 }
324  
325 # Avoiding dependency on namespace::clean
326 5 delete @{Sys::Export::Unix::WriteFS::}{qw(
327 carp croak abs_path blessed isa_hash
328 S_IFBLK S_IFCHR S_IFDIR S_IFIFO S_IFLNK S_IFMT S_IFREG S_IFSOCK S_IFWHT
329 S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISLNK S_ISREG S_ISSOCK S_ISWHT
330 )};
331 5 1;
332  
333 __END__
« Export/CPIO.pm ↑ Summary Export/VFAT.pm »