lib/Doit.pm

filter: Statementstmt 70.9% 1010 / 1424 Branchbran 62.0% 489 / 788 Conditioncond 47.3% 108 / 228 MC/DCmcdc 34.7% 49 / 141 Subroutinesub 80.5% 149 / 185 Podpod 16.6% 8 / 48 totaltotal 64.4% 1813 / 2814 SCAR 92.6
CC
501
Cov
66%
do_connect
1217.0
is_in_path
413.6
gentle_retry
210.0
CRAP 10488.1
« Build.PL ↑ Summary blib/lib/Doit/User.pm »
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3  
4 #
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2017,2018,2019,2020,2022 Slaven Rezic. All rights reserved.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 {
19 package Doit;
20 47 our $VERSION = '0.026';
21 47 $VERSION =~ s{_}{};
22  
23 47 ▶ use constant IS_WIN => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 }
25  
26 {
27 47 package Doit::Log;
28  
29 sub _use_coloring {
30 47 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 108 ▶ *colored_error = sub ($) { Term::ANSIColor::colored($_[0], 'red on_black')};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _use_coloring: called CC=1 SCAR=0.0
32 47 ▶ *colored_info = sub ($) { Term::ANSIColor::colored($_[0], 'green on_black')};
Subroutine __ANON__: called CC=1 SCAR=0.0
33 }
34 sub _no_coloring {
35 47 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 0 ▶ *colored_error = *colored_info = sub ($) { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _no_coloring: not called CC=1 SCAR=6.9
37 }
38 {
39 47 my $can_coloring;
40 sub _can_coloring {
41 47 ▶ return $can_coloring if defined $can_coloring;
Subroutine _can_coloring: called CC=5 SCAR=18.6
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_coloring 0 47 47
42 # XXX What needs to be done to get coloring on Windows?
43 # XXX Probably should also check if the terminal is ANSI-capable at all
44 # XXX Probably should not use coloring on non-terminals (but
45 # there could be a --color option like in git to force it)
46 47 ▶ $can_coloring = !Doit::IS_WIN && ($ENV{TERM}||'') !~ m{^(|dumb)$} && eval { require Term::ANSIColor; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!(($ENV{'TERM'} || '') =~ /^(|dumb)$/) && eval { do { require Term::ANSIColor; 1 } } ? : 47 0 47
Condition40%2/5
not +($ENV{"TERM"} || "") =~ /^(|dumb)$/ and eval { do { require Term::ANSIColor; 1 } }
!ll&&!rl&&r
0047
$ENV{'TERM'} || ''
l!l
470
MC/DC0%0/2
not +($ENV{"TERM"} || "") =~ /^(|dumb)$/ and eval { do { require Term::ANSIColor; 1 } }
$ENV{'TERM'} eval { do { require Term::ANSIColor; 1 } }
Truth table0%0/3
not(A) and B
A= $ENV{'TERM'}B= eval { do { require Term::ANSIColor; 1 } }
ABresult
1X0
000
011
47 }
48 }
49  
50 47 BEGIN {
51 47 ▶ if (_can_coloring()) {
Subroutine BEGIN: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
if (_can_coloring()) { } 47 0 47
52 47 _use_coloring();
53 } else {
54 0 _no_coloring();
55 }
56 }
57  
58 47 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 47 ▶ our @EXPORT; BEGIN { @EXPORT = qw(info warning error) }
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 47 ▶ BEGIN { $INC{'Doit/Log.pm'} = __FILE__ } # XXX hack
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 47 my $current_label = '';
64  
65 464 ▶ sub info ($) { print STDERR colored_info("INFO$current_label:"), " ", $_[0], "\n" }
Subroutine info: called CC=1 SCAR=0.0
Pod documented
66 2 ▶ sub warning ($) { print STDERR colored_error("WARN$current_label:"), " ", $_[0], "\n" }
Subroutine warning: called CC=1 SCAR=0.0
Pod documented
67 106 ▶ sub error ($) { require Carp; Carp::croak(colored_error("ERROR$current_label:"), " ", $_[0]) }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
68  
69 sub set_label ($) {
70 0 ▶ my $label = shift;
Subroutine set_label: not called CC=2 SCAR=17.9
Pod documented
71 0 ▶ if (defined $label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label) { } 0 0 0
72 0 $current_label = " $label";
73 } else {
74 0 $current_label = '';
75 }
76 }
77 }
78  
79 {
80 47 package Doit::Exception;
81 47 ▶ use overload '""' => 'stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 47 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 47 our @EXPORT_OK = qw(throw);
84 47 $INC{'Doit/Exception.pm'} = __FILE__; # XXX hack
85  
86 sub new {
87 56 ▶ my($class, $msg, %opts) = @_;
Subroutine new: called CC=5 SCAR=16.6
Pod undocumented
88 56 ▶ my $level = delete $opts{__level} || 'auto';
Condition50%1/2
delete $opts{'__level'} || 'auto'
l!l
056
MC/DC0%0/1
delete $opts{'__level'} || 'auto'
delete $opts{'__level'}
Truth table50%1/2
A
A= delete $opts{'__level'}
Aresult
11
00
89 56 ▶ if ($level eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($level eq "auto") 56 0 56
90 56 my $_level = 0;
91 56 while() {
92 322 my @stackinfo = caller($_level);
93 322 ▶ if (!@stackinfo) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stackinfo) 0 322 322
94 0 $level = $_level - 1;
95 0 last;
96 }
97 322 ▶ if ($stackinfo[1] !~ m{([/\\]|^)Doit\.pm$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stackinfo[1] =~ m(([/\\]|^)Doit\.pm$)) 56 266 322
98 56 $level = $_level;
99 56 last;
100 }
101 266 $_level++;
102 }
103 }
104 56 ($opts{__package}, $opts{__filename}, $opts{__line}) = caller($level);
105 56 bless {
106 __msg => $msg,
107 %opts,
108 }, $class;
109 }
110 sub stringify {
111 33 ▶ my $self = shift;
Subroutine stringify: called CC=3 SCAR=11.0
Pod undocumented
112 33 my $msg = $self->{__msg};
113 33 ▶ $msg = 'Died' if !defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 2 31 33
114 33 ▶ if ($msg !~ /\n\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg =~ /\n\z/) 31 2 33
115 31 $msg .= ' at ' . $self->{__filename} . ' line ' . $self->{__line} . ".\n";
116 }
117 33 $msg;
118 }
119  
120 52 ▶ sub throw { die Doit::Exception->new(@_) }
Subroutine throw: called CC=1 SCAR=0.0
Pod undocumented
121 }
122  
123 {
124 47 package Doit::ScopeCleanups;
125 47 $INC{'Doit/ScopeCleanups.pm'} = __FILE__; # XXX hack
126 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 sub new {
129 133 ▶ my($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
130 133 bless [], $class;
131 }
132  
133 sub add_scope_cleanup {
134 137 ▶ my($self, $code) = @_;
Subroutine add_scope_cleanup: called CC=1 SCAR=0.0
Pod undocumented
135 137 push @$self, { code => $code };
136 }
137  
138 sub DESTROY {
139 133 ▶ my $self = shift;
Subroutine DESTROY: called CC=4 SCAR=14.0
140 133 for my $scope_cleanup (@$self) {
141 135 my($code) = $scope_cleanup->{code};
142 135 ▶ if ($] >= 5.014) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.014) { } 135 0 135
143 135 eval {
144 135 $code->();
145 };
146 135 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 131 135
147 # error() will give visual feedback about the problem,
148 # die() would be left unnoticed. Note that
149 # an exception in a DESTROY block is not fatal,
150 # and can be only detected by inspecting $@.
151 4 error "Scope cleanup failed: $@";
152 }
153 } else {
154 # And eval {} in older perl versions would
155 # clobber an outside $@. See
156 # perldoc perl5140delta, "Exception Handling"
157 0 $code->();
158 }
159 }
160 }
161 }
162  
163 {
164 47 package Doit::Util;
165 47 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
166 47 ▶ our @EXPORT; BEGIN { @EXPORT = qw(in_directory new_scope_cleanup copy_stat get_sudo_cmd is_in_path) }
Subroutine BEGIN: called CC=1 SCAR=0.0
167 47 $INC{'Doit/Util.pm'} = __FILE__; # XXX hack
168 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 sub new_scope_cleanup (&) {
171 133 ▶ my($code) = @_;
Subroutine new_scope_cleanup: called CC=1 SCAR=0.0
Pod documented
172 133 my $sc = Doit::ScopeCleanups->new;
173 133 $sc->add_scope_cleanup($code);
174 133 $sc;
175 }
176  
177 sub in_directory (&$) {
178 303 ▶ my($code, $dir) = @_;
Subroutine in_directory: called CC=5 SCAR=16.1
Pod documented
179 303 my $scope_cleanup;
180 303 ▶ if (defined $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir) 103 200 303
181 103 require Cwd;
182 103 my $pwd = Cwd::getcwd();
183 103 ▶ if (!defined $pwd || $pwd eq '') { # XS variant returns undef, PP variant returns '' --- see https://rt.perl.org/Ticket/Display.html?id=132648
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pwd or $pwd eq '') { } 2 101 103
Condition66%2/3
not defined $pwd or $pwd eq ""
l!l&&r!l&&!r
20101
MC/DC50%1/2
not defined $pwd or $pwd eq ""
not defined $pwd $pwd eq ""
Truth table66%2/3
not(A) or B
A= not defined $pwdB= $pwd eq ""
ABresult
1X1
011
000
184 2 warning "No known current working directory";
185 } else {
186 $scope_cleanup = new_scope_cleanup
187 (sub {
188 101 ▶ chdir $pwd or error "Can't chdir to $pwd: $!";
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
chdir $pwd or &error("Can't chdir to ${pwd}: $!") 2 99 101
189 101 });
190 }
191 103 ▶ chdir $dir
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or &error("Can't chdir to ${dir}: $!") 3 100 103
192 or error "Can't chdir to $dir: $!";
193 }
194 300 $code->();
195 }
196  
197 # $src may be a source file or an arrayref with stat information
198 sub copy_stat ($$;@) {
199 66 ▶ my($src, $dest, %preserve) = @_;
Subroutine copy_stat: called CC=13 SCAR=26.3
Pod documented
200 66 ▶ my @stat = ref $src eq 'ARRAY' ? @$src : stat($src);
Branch: 1 branch
BranchTrueFalseTotal
ref $src eq 'ARRAY' ? : 6 60 66
201 66 ▶ error "Can't stat $src: $!" if !@stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 66 66
202  
203 66 my $preserve_default = !%preserve;
204 66 ▶ my $preserve_ownership = exists $preserve{ownership} ? delete $preserve{ownership} : $preserve_default;
Branch: 1 branch
BranchTrueFalseTotal
exists $preserve{'ownership'} ? : 23 43 66
205 66 ▶ my $preserve_mode = exists $preserve{mode} ? delete $preserve{mode} : $preserve_default;
Branch: 1 branch
BranchTrueFalseTotal
exists $preserve{'mode'} ? : 24 42 66
206 66 ▶ my $preserve_time = exists $preserve{time} ? delete $preserve{time} : $preserve_default;
Branch: 1 branch
BranchTrueFalseTotal
exists $preserve{'time'} ? : 1 65 66
207  
208 66 ▶ error "Unhandled preserve values: " . join(" ", %preserve) if %preserve;
Branch: 1 branch
BranchTrueFalseTotal
if %preserve 0 66 66
209  
210 66 ▶ if ($preserve_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_mode) 63 3 66
211 63 ▶ chmod $stat[2], $dest
Branch: 1 branch
BranchTrueFalseTotal
chmod $stat[2], $dest or &warning("Can't chmod $dest to " . sprintf("0%o", $stat[2]) . ": $!") 0 63 63
212 or warning "Can't chmod $dest to " . sprintf("0%o", $stat[2]) . ": $!";
213 }
214 66 ▶ if ($preserve_ownership) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_ownership) 63 3 66
215 chown $stat[4], $stat[5], $dest
216 63 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
chown $stat[4], $stat[5], $dest or do { my $save_err = $!; &warning("Can't chown $dest to " . (getpwuid $stat[4])[0] . '/' . (getgrgid $stat[5])[0] . ": $save_err") } 0 63 63
217 0 my $save_err = $!; # otherwise it's lost in the get... calls
218 0 warning "Can't chown $dest to " .
219 (getpwuid($stat[4]))[0] . "/" .
220 (getgrgid($stat[5]))[0] . ": $save_err";
221 };
222 }
223 66 ▶ if ($preserve_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_time) 41 25 66
224 41 ▶ utime $stat[8], $stat[9], $dest
Branch: 1 branch
BranchTrueFalseTotal
utime $stat[8], $stat[9], $dest or &warning("Can't utime $dest to " . scalar(localtime $stat[8]) . '/' . scalar(localtime $stat[9]) . ": $!") 0 41 41
225 or warning "Can't utime $dest to " .
226 scalar(localtime $stat[8]) . "/" .
227 scalar(localtime $stat[9]) .
228 ": $!";
229 }
230 }
231  
232 sub get_sudo_cmd () {
233 0 ▶ return () if $> == 0;
Subroutine get_sudo_cmd: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $> == 0 0 0 0
234 0 return ('sudo');
235 }
236  
237 sub is_in_path {
238 20 ▶ my($prog) = @_;
Subroutine is_in_path: called CC=32 SCAR=60.2
Pod undocumented
239  
240 20 ▶ if (!defined &_file_name_is_absolute) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Doit::Util::_file_name_is_absolute) 15 5 20
241 15 ▶ if (eval { require File::Spec; defined &File::Spec::file_name_is_absolute }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require File::Spec; defined &File::Spec::file_name_is_absolute } }) { } 0 15 15
242 0 *_file_name_is_absolute = \&File::Spec::file_name_is_absolute;
243 } else {
244 *_file_name_is_absolute = sub {
245 20 ▶ my $file = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
246 20 my $r;
247 20 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 20 20
248 0 $r = ($file =~ m;^([a-z]:(/|\\)|\\\\|//);i);
249 } else {
250 20 $r = ($file =~ m|^/|);
251 }
252 20 $r;
253 15 };
254 }
255 }
256  
257 20 ▶ if (_file_name_is_absolute($prog)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_file_name_is_absolute($prog)) 0 20 20
258 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
259 0 ▶ return $prog if (-f $prog && -x $prog);
Branch: 1 branch
BranchTrueFalseTotal
if -f $prog and -x $prog 0 0 0
Condition0%0/3
-f $prog and -x $prog
!ll&&!rl&&r
000
MC/DC0%0/2
-f $prog and -x $prog
-f $prog -x $prog
Truth table0%0/3
A and B
A= -f $progB= -x $prog
ABresult
0X0
100
111
260 0 ▶ return "$prog.bat" if (-f "$prog.bat" && -x "$prog.bat");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$prog.bat" and -x "$prog.bat" 0 0 0
Condition0%0/3
-f "$prog.bat" and -x "$prog.bat"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$prog.bat" and -x "$prog.bat"
-f "$prog.bat" -x "$prog.bat"
Truth table0%0/3
A and B
A= -f "$prog.bat"B= -x "$prog.bat"
ABresult
0X0
100
111
261 0 ▶ return "$prog.com" if (-f "$prog.com" && -x "$prog.com");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$prog.com" and -x "$prog.com" 0 0 0
Condition0%0/3
-f "$prog.com" and -x "$prog.com"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$prog.com" and -x "$prog.com"
-f "$prog.com" -x "$prog.com"
Truth table0%0/3
A and B
A= -f "$prog.com"B= -x "$prog.com"
ABresult
0X0
100
111
262 0 ▶ return "$prog.exe" if (-f "$prog.exe" && -x "$prog.exe");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$prog.exe" and -x "$prog.exe" 0 0 0
Condition0%0/3
-f "$prog.exe" and -x "$prog.exe"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$prog.exe" and -x "$prog.exe"
-f "$prog.exe" -x "$prog.exe"
Truth table0%0/3
A and B
A= -f "$prog.exe"B= -x "$prog.exe"
ABresult
0X0
100
111
263 0 ▶ return "$prog.cmd" if (-f "$prog.cmd" && -x "$prog.cmd");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$prog.cmd" and -x "$prog.cmd" 0 0 0
Condition0%0/3
-f "$prog.cmd" and -x "$prog.cmd"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$prog.cmd" and -x "$prog.cmd"
-f "$prog.cmd" -x "$prog.cmd"
Truth table0%0/3
A and B
A= -f "$prog.cmd"B= -x "$prog.cmd"
ABresult
0X0
100
111
264 } else {
265 0 ▶ return $prog if -f $prog and -x $prog;
Branch: 1 branch
BranchTrueFalseTotal
if -f $prog and -x $prog 0 0 0
Condition0%0/3
-f $prog and -x $prog
!ll&&!rl&&r
000
MC/DC0%0/2
-f $prog and -x $prog
-f $prog -x $prog
Truth table0%0/3
A and B
A= -f $progB= -x $prog
ABresult
0X0
100
111
266 }
267 }
268 20 require Config;
269 20 %Config::Config = %Config::Config if 0; # cease -w
270 20 ▶ my $sep = $Config::Config{'path_sep'} || ':';
Condition50%1/2
$Config::Config{'path_sep'} || ':'
l!l
200
MC/DC0%0/1
$Config::Config{'path_sep'} || ':'
$Config::Config{'path_sep'}
Truth table50%1/2
A
A= $Config::Config{'path_sep'}
Aresult
11
00
271 20 foreach (split(/$sep/o, $ENV{PATH})) {
272 158 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 158 158
273 # maybe use $ENV{PATHEXT} like maybe_command in ExtUtils/MM_Win32.pm?
274 0 ▶ return "$_\\$prog" if (-f "$_\\$prog" && -x "$_\\$prog");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_\\$prog" and -x "$_\\$prog" 0 0 0
Condition0%0/3
-f "$_\\$prog" and -x "$_\\$prog"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$_\\$prog" and -x "$_\\$prog"
-f "$_\\$prog" -x "$_\\$prog"
Truth table0%0/3
A and B
A= -f "$_\\$prog"B= -x "$_\\$prog"
ABresult
0X0
100
111
275 0 ▶ return "$_\\$prog.bat" if (-f "$_\\$prog.bat" && -x "$_\\$prog.bat");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_\\$prog.bat" and -x "$_\\$prog.bat" 0 0 0
Condition0%0/3
-f "$_\\$prog.bat" and -x "$_\\$prog.bat"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$_\\$prog.bat" and -x "$_\\$prog.bat"
-f "$_\\$prog.bat" -x "$_\\$prog.bat"
Truth table0%0/3
A and B
A= -f "$_\\$prog.bat"B= -x "$_\\$prog.bat"
ABresult
0X0
100
111
276 0 ▶ return "$_\\$prog.com" if (-f "$_\\$prog.com" && -x "$_\\$prog.com");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_\\$prog.com" and -x "$_\\$prog.com" 0 0 0
Condition0%0/3
-f "$_\\$prog.com" and -x "$_\\$prog.com"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$_\\$prog.com" and -x "$_\\$prog.com"
-f "$_\\$prog.com" -x "$_\\$prog.com"
Truth table0%0/3
A and B
A= -f "$_\\$prog.com"B= -x "$_\\$prog.com"
ABresult
0X0
100
111
277 0 ▶ return "$_\\$prog.exe" if (-f "$_\\$prog.exe" && -x "$_\\$prog.exe");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_\\$prog.exe" and -x "$_\\$prog.exe" 0 0 0
Condition0%0/3
-f "$_\\$prog.exe" and -x "$_\\$prog.exe"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$_\\$prog.exe" and -x "$_\\$prog.exe"
-f "$_\\$prog.exe" -x "$_\\$prog.exe"
Truth table0%0/3
A and B
A= -f "$_\\$prog.exe"B= -x "$_\\$prog.exe"
ABresult
0X0
100
111
278 0 ▶ return "$_\\$prog.cmd" if (-f "$_\\$prog.cmd" && -x "$_\\$prog.cmd");
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_\\$prog.cmd" and -x "$_\\$prog.cmd" 0 0 0
Condition0%0/3
-f "$_\\$prog.cmd" and -x "$_\\$prog.cmd"
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$_\\$prog.cmd" and -x "$_\\$prog.cmd"
-f "$_\\$prog.cmd" -x "$_\\$prog.cmd"
Truth table0%0/3
A and B
A= -f "$_\\$prog.cmd"B= -x "$_\\$prog.cmd"
ABresult
0X0
100
111
279 } else {
280 158 ▶ return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog");
Branch: 1 branch
BranchTrueFalseTotal
if -x "$_/$prog" and not -d "$_/$prog" 14 144 158
Condition66%2/3
-x "$_/$prog" and not -d "$_/$prog"
!ll&&!rl&&r
144014
MC/DC50%1/2
-x "$_/$prog" and not -d "$_/$prog"
-x "$_/$prog" not -d "$_/$prog"
Truth table66%2/3
A and not(B)
A= -x "$_/$prog"B= not -d "$_/$prog"
ABresult
0X0
100
111
281 }
282 }
283 6 undef;
284 }
285 }
286  
287 {
288 47 package Doit::Win32Util;
289  
290 # Taken from http://blogs.perl.org/users/graham_knop/2011/12/using-system-or-exec-safely-on-windows.html
291 sub win32_quote_list {
292 0 ▶ my (@args) = @_;
Subroutine win32_quote_list: not called CC=2 SCAR=17.9
293  
294 0 my $args = join ' ', map { _quote_literal($_) } @args;
295  
296 0 ▶ if (_has_shell_metachars($args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_shell_metachars($args)) 0 0 0
297 # cmd.exe treats quotes differently from standard
298 # argument parsing. just escape everything using ^.
299 0 $args =~ s/([()%!^"<>&|])/^$1/g;
300 }
301 0 return $args;
302 }
303  
304 sub _quote_literal {
305 0 ▶ my ($text) = @_;
Subroutine _quote_literal: not called CC=7 SCAR=40.3
306  
307 # basic argument quoting. uses backslashes and quotes to escape
308 # everything.
309 #
310 # The original code had a \v here, but this is not supported
311 # in perl5.8. Also, \v probably matches too many characters here
312 # --- restrict to the ones < 0x100
313 0 ▶ if ($text ne '' && $text !~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne '' and not $text =~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/) { } 0 0 0
Condition0%0/3
$text ne "" and not $text =~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/
!ll&&!rl&&r
000
MC/DC0%0/2
$text ne "" and not $text =~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/
$text ne "" not $text =~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/
Truth table0%0/3
A and not(B)
A= $text ne ""B= not $text =~ /[ \t\n\x0a\x0b\x0c\x0d\x85"]/
ABresult
0X0
100
111
314 # no quoting needed
315 } else {
316 0 my @text = split '', $text;
317 0 $text = q{"};
318 0 for (my $i = 0; ; $i++) {
319 0 my $bs_count = 0;
320 0 ▶ while ( $i < @text && $text[$i] eq "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @text and $text[$i] eq "\\") 0 0 0
Condition0%0/3
$i < @text and $text[$i] eq "\\"
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @text and $text[$i] eq "\\"
$i < @text $text[$i] eq "\\"
Truth table0%0/3
A and B
A= $i < @textB= $text[$i] eq "\\"
ABresult
0X0
100
111
321 0 $i++;
322 0 $bs_count++;
323 }
324 0 ▶ if ($i > $#text) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i > $#text) { } 0 0 0
elsif ($text[$i] eq '"') { } 0 0 0
325 0 $text .= "\\" x ($bs_count * 2);
326 0 last;
327 } elsif ($text[$i] eq q{"}) {
328 0 $text .= "\\" x ($bs_count * 2 + 1);
329 } else {
330 0 $text .= "\\" x $bs_count;
331 }
332 0 $text .= $text[$i];
333 }
334 0 $text .= q{"};
335 }
336  
337 0 return $text;
338 }
339  
340 # direct port of code from win32.c
341 sub _has_shell_metachars {
342 0 ▶ my $string = shift;
Subroutine _has_shell_metachars: not called CC=11 SCAR=48.8
343 0 my $inquote = 0;
344 0 my $quote = '';
345  
346 0 my @string = split '', $string;
347 0 for my $char (@string) {
348 0 ▶ if ($char eq q{%}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '%') { } 0 0 0
elsif ($char eq q['] or $char eq '"') { } 0 0 0
elsif ($char eq '<' or $char eq '>' or $char eq '|') { } 0 0 0
349 0 return 1;
350 ▶ } elsif ($char eq q{'} || $char eq q{"}) {
Condition0%0/3
$char eq "'" or $char eq "\""
l!l&&r!l&&!r
000
MC/DC0%0/2
$char eq "'" or $char eq "\""
$char eq "'" $char eq "\""
Truth table0%0/3
A or B
A= $char eq "'"B= $char eq "\""
ABresult
1X1
011
000
351 0 ▶ if ($inquote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inquote) { } 0 0 0
352 0 ▶ if ($char eq $quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $quote) 0 0 0
353 0 $inquote = 0;
354 0 $quote = '';
355 }
356 } else {
357 0 $quote = $char;
358 0 $inquote++;
359 }
360 ▶ } elsif ($char eq q{<} || $char eq q{>} || $char eq q{|}) {
Condition0%0/6
$char eq "<" or $char eq ">" or $char eq "|"
l!l&&r!l&&!r
000
$char eq "<" or $char eq ">"
l!l&&r!l&&!r
000
MC/DC0%0/3
$char eq "<" or $char eq ">" or $char eq "|"
$char eq "<" $char eq ">" $char eq "|"
Truth table0%0/4
A or B or C
A= $char eq "<"B= $char eq ">"C= $char eq "|"
ABCresult
1XX1
01X1
0011
0000
361 0 ▶ if ( ! $inquote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inquote) 0 0 0
362 0 return 1;
363 }
364 }
365 }
366 0 return;
367 }
368 }
369  
370 {
371 47 package Doit;
372  
373 sub import {
374 52 ▶ warnings->import;
Subroutine import: called CC=1 SCAR=0.0
375 52 strict->import;
376 }
377  
378 sub unimport {
379 2 ▶ warnings->unimport;
Subroutine unimport: called CC=1 SCAR=0.0
380 2 strict->unimport;
381 }
382  
383 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
384  
385 47 my $diff_error_shown;
386 47 our @diff_cmd;
387  
388 sub _new {
389 46 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
390 46 my $self = bless { }, $class;
391 46 $self;
392 }
393 sub runner {
394 39 ▶ my($self) = @_;
Subroutine runner: called CC=2 SCAR=8.1
Pod undocumented
395 # XXX hmmm, creating now self-refential data structures ...
396 39 ▶ $self->{runner} ||= Doit::Runner->new($self);
Condition33%1/3
$self->{"runner"} ||= "Doit::Runner"->new($self)
l!l&&r!l&&!r
0390
MC/DC0%0/2
$self->{"runner"} ||= "Doit::Runner"->new($self)
$self->{"runner"} "Doit::Runner"->new($self)
Truth table33%1/3
A ||= B
A= $self->{"runner"}B= "Doit::Runner"->new($self)
ABresult
1X1
011
000
397 }
398
399 sub dryrunner {
400 7 ▶ my($self) = @_;
Subroutine dryrunner: called CC=2 SCAR=8.1
Pod undocumented
401 # XXX hmmm, creating now self-refential data structures ...
402 7 ▶ $self->{dryrunner} ||= Doit::Runner->new($self, dryrun => 1);
Condition33%1/3
$self->{"dryrunner"} ||= "Doit::Runner"->new($self, "dryrun", 1)
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{"dryrunner"} ||= "Doit::Runner"->new($self, "dryrun", 1)
$self->{"dryrunner"} "Doit::Runner"->new($self, "dryrun", 1)
Truth table33%1/3
A ||= B
A= $self->{"dryrunner"}B= "Doit::Runner"->new($self, "dryrun", 1)
ABresult
1X1
011
000
403 }
404  
405 sub init {
406 46 ▶ my($class) = @_;
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
407 46 require Getopt::Long;
408 46 my $getopt = Getopt::Long::Parser->new;
409 46 $getopt->configure(qw(pass_through noauto_abbrev));
410 46 $getopt->getoptions(
411 'dry-run|n' => \my $dry_run,
412 );
413 46 my $doit = $class->_new;
414 46 ▶ if ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 7 39 46
415 7 $doit->dryrunner;
416 } else {
417 39 $doit->runner;
418 }
419 }
420  
421 sub install_generic_cmd {
422 2 ▶ my($self, $name, $check, $code, $msg) = @_;
Subroutine install_generic_cmd: called CC=2 SCAR=6.9
Pod undocumented
423 2 ▶ if (!$msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 2 0 2
424 2 ▶ $msg = sub { my($self, $args) = @_; $name . ($args ? " @$args" : '') };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 2 0 2
425 }
426 my $cmd = sub {
427 3 ▶ my($self, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
428 3 my @commands;
429 3 my $addinfo = {};
430 3 ▶ if ($check->($self, \@args, $addinfo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$check($self, \@args, $addinfo)) 2 1 3
431 push @commands, {
432 2 ▶ code => sub { $code->($self, \@args, $addinfo) },
Subroutine __ANON__: called CC=1 SCAR=0.0
433 2 msg => $msg->($self, \@args, $addinfo),
434 };
435 }
436 3 Doit::Commands->new(@commands);
437 2 };
438 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
439 2 *{"cmd_$name"} = $cmd;
440 }
441  
442 sub cmd_chmod {
443 48 ▶ my($self, @args) = @_;
Subroutine cmd_chmod: called CC=9 SCAR=22.0
Pod undocumented
444 48 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 17 31 48
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
03117
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
445 48 my $quiet = delete $options{quiet};
446 48 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 48 48
447 48 my($mode, @files) = @args;
448 48 my @files_to_change;
449 48 for my $file (@files) {
450 51 my @s = stat($file);
451 51 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) { } 47 4 51
452 47 ▶ if (($s[2] & 07777) != $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (($s[2] & 4095) != $mode) 44 3 47
453 44 push @files_to_change, $file;
454 }
455 } else {
456 4 push @files_to_change, $file;
457 }
458 }
459 48 ▶ if (@files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files_to_change) { } 45 3 48
460 my @commands = {
461 code => sub {
462 42 ▶ my $changed_files = chmod $mode, @files_to_change;
Subroutine __ANON__: called CC=4 SCAR=13.9
463 42 ▶ if ($changed_files != @files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed_files != @files_to_change) 3 39 42
464 3 ▶ if (@files_to_change == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@files_to_change == 1) { } 1 2 3
elsif ($changed_files == 0) { } 1 1 2
465 1 error "chmod failed: $!";
466 } elsif ($changed_files == 0) {
467 1 error "chmod failed on all files: $!";
468 } else {
469 1 error "chmod failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!";
470 }
471 }
472 },
473 45 ▶ ($quiet ? () : (msg => sprintf("chmod 0%o %s", $mode, join(" ", @files_to_change)))), # shellquote?
Branch: 1 branch
BranchTrueFalseTotal
$quiet ? : 16 29 45
474 rv => scalar @files_to_change,
475 };
476 45 Doit::Commands->new(@commands);
477 } else {
478 3 Doit::Commands->return_zero;
479 }
480 }
481  
482 sub cmd_chown {
483 12 ▶ my($self, @args) = @_;
Subroutine cmd_chown: called CC=20 SCAR=30.8
Pod undocumented
484 12 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 2 10 12
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
0102
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
485 12 my $quiet = delete $options{quiet};
486 12 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 12 12
487 12 my($uid, $gid, @files) = @args;
488  
489 12 ▶ if (!defined $uid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $uid) { } 3 9 12
elsif (not $uid =~ /^-?\d+$/) { } 2 7 9
490 3 $uid = -1;
491 } elsif ($uid !~ /^-?\d+$/) {
492 2 my $_uid = (getpwnam $uid)[2];
493 2 ▶ if (!defined $_uid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_uid) 1 1 2
494 # XXX problem: in dry-run mode the user/group could be
495 # created in _this_ pass, so this error would happen
496 # while in wet-run everything would be fine. Good solution?
497 # * do uid/gid resolution _again_ in the command if it failed here?
498 # * maintain a virtual list of created users/groups while this run, and
499 # use this list as a fallback?
500 1 error "User '$uid' does not exist";
501 }
502 1 $uid = $_uid;
503 }
504 11 ▶ if (!defined $gid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $gid) { } 6 5 11
elsif (not $gid =~ /^-?\d+$/) { } 1 4 5
505 6 $gid = -1;
506 } elsif ($gid !~ /^-?\d+$/) {
507 1 my $_gid = (getgrnam $gid)[2];
508 1 ▶ if (!defined $_gid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_gid) 1 0 1
509 1 error "Group '$gid' does not exist";
510 }
511 0 $gid = $_gid;
512 }
513  
514 10 my @files_to_change;
515 10 ▶ if ($uid != -1 || $gid != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid != -1 or $gid != -1) 9 1 10
Condition100%3/3
$uid != -1 or $gid != -1
l!l&&r!l&&!r
721
MC/DC100%2/2
$uid != -1 or $gid != -1
$uid != -1 $gid != -1
Truth table100%3/3
A or B
A= $uid != -1B= $gid != -1
ABresult
1X1
011
000
516 9 for my $file (@files) {
517 10 my @s = stat($file);
518 10 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) { } 7 3 10
519 7 ▶ if ($uid != -1 && $s[4] != $uid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uid != -1 and $s[4] != $uid) { } 0 7 7
elsif ($gid != -1 and $s[5] != $gid) { } 0 12 12
Condition66%2/3
$uid != -1 and $s[4] != $uid
!ll&&!rl&&r
250
MC/DC0%0/2
$uid != -1 and $s[4] != $uid
$uid != -1 $s[4] != $uid
Truth table66%2/3
A and B
A= $uid != -1B= $s[4] != $uid
ABresult
0X0
100
111
520 0 push @files_to_change, $file;
521 ▶ } elsif ($gid != -1 && $s[5] != $gid) {
Condition66%2/3
$gid != -1 and $s[5] != $gid
!ll&&!rl&&r
520
MC/DC0%0/2
$gid != -1 and $s[5] != $gid
$gid != -1 $s[5] != $gid
Truth table66%2/3
A and B
A= $gid != -1B= $s[5] != $gid
ABresult
0X0
100
111
522 0 push @files_to_change, $file;
523 }
524 } else {
525 3 push @files_to_change, $file;
526 }
527 }
528 }
529  
530 10 ▶ if (@files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files_to_change) { } 2 8 10
531 my @commands = {
532 code => sub {
533 2 ▶ my $changed_files = chown $uid, $gid, @files_to_change;
Subroutine __ANON__: called CC=4 SCAR=14.5
534 2 ▶ if ($changed_files != @files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed_files != @files_to_change) 2 0 2
535 2 ▶ if (@files_to_change == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@files_to_change == 1) { } 1 1 2
elsif ($changed_files == 0) { } 1 0 1
536 1 error "chown failed: $!";
537 } elsif ($changed_files == 0) {
538 1 error "chown failed on all files: $!";
539 } else {
540 0 error "chown failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!";
541 }
542 }
543 },
544 2 ▶ ($quiet ? () : (msg => "chown $uid, $gid, @files_to_change")), # shellquote?
Branch: 1 branch
BranchTrueFalseTotal
$quiet ? : 0 2 2
545 rv => scalar @files_to_change,
546 };
547 2 Doit::Commands->new(@commands);
548 } else {
549 8 Doit::Commands->return_zero;
550 }
551 }
552  
553 sub cmd_cond_run {
554 12 ▶ my($self, %opts) = @_;
Subroutine cmd_cond_run: called CC=14 SCAR=26.4
Pod undocumented
555 12 my $if = delete $opts{if};
556 12 my $unless = delete $opts{unless};
557 12 my $creates = delete $opts{creates};
558 12 my $cmd = delete $opts{cmd};
559 12 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 11 12
560  
561 11 ▶ if (!$cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 1 10 11
562 1 error "cmd is a mandatory option for cond_run";
563 }
564 10 ▶ if (ref $cmd ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd ne "ARRAY") 1 9 10
565 1 error "cmd must be an array reference";
566 }
567  
568 9 my $doit = 1;
569 9 ▶ if ($if && !$if->()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($if and not &$if()) 2 7 9
Condition100%3/3
$if and not &$if()
!ll&&!rl&&r
522
MC/DC100%2/2
$if and not &$if()
$if not &$if()
Truth table100%3/3
A and not(B)
A= $ifB= not &$if()
ABresult
0X0
100
111
570 2 $doit = 0;
571 }
572 9 ▶ if ($doit && $unless && $unless->()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit and $unless and &$unless()) 1 8 9
Condition100%6/6
$doit and $unless and &$unless()
!ll&&!rl&&r
621
$doit and $unless
!ll&&!rl&&r
243
MC/DC100%3/3
$doit and $unless and &$unless()
$doit $unless &$unless()
Truth table100%4/4
A and B and C
A= $doitB= $unlessC= &$unless()
ABCresult
0XX0
10X0
1100
1111
573 1 $doit = 0;
574 }
575 9 ▶ if ($doit && $creates && -e $creates) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit and $creates and -e $creates) 1 8 9
Condition100%6/6
$doit and $creates and -e $creates
!ll&&!rl&&r
621
$doit and $creates
!ll&&!rl&&r
333
MC/DC100%3/3
$doit and $creates and -e $creates
$doit $creates -e $creates
Truth table100%4/4
A and B and C
A= $doitB= $createsC= -e $creates
ABCresult
0XX0
10X0
1100
1111
576 1 $doit = 0;
577 }
578  
579 9 ▶ if ($doit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit) { } 5 4 9
580 5 my $doit_commands;
581 5 ▶ if (ref $cmd->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd->[0] eq 'ARRAY') { } 0 5 5
582 0 $doit_commands = $self->cmd_run(@$cmd);
583 } else {
584 5 $doit_commands = $self->cmd_system(@$cmd);
585 }
586 5 $doit_commands->set_last_rv(1);
587 5 $doit_commands;
588 } else {
589 4 Doit::Commands->return_zero;
590 }
591 }
592  
593 sub cmd_ln_nsf {
594 7 ▶ my($self, $oldfile, $newfile) = @_;
Subroutine cmd_ln_nsf: called CC=8 SCAR=20.8
Pod undocumented
595  
596 7 my $doit = 1;
597 7 ▶ if (!defined $oldfile) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $oldfile) { } 1 6 7
elsif (not defined $newfile) { } 1 5 6
elsif (-l $newfile) { } 2 3 5
elsif (-d $newfile) { } 1 2 3
598 1 error "oldfile was not specified for ln_nsf";
599 } elsif (!defined $newfile) {
600 1 error "newfile was not specified for ln_nsf";
601 } elsif (-l $newfile) {
602 2 ▶ my $points_to = readlink $newfile
Branch: 1 branch
BranchTrueFalseTotal
my $points_to = readlink $newfile or &error("Unexpected: readlink $newfile failed (race condition?)") 0 2 2
603 or error "Unexpected: readlink $newfile failed (race condition?)";
604 2 ▶ if ($points_to eq $oldfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points_to eq $oldfile) 1 1 2
605 1 $doit = 0;
606 }
607 } elsif (-d $newfile) {
608 # Theoretically "ln -nsf destination directory" works (not always,
609 # e.g. fails with destination=/), but results are not very useful,
610 # so fail here.
611 1 error qq{"$newfile" already exists as a directory};
612 } else {
613 # probably a file, keep $doit=1
614 }
615  
616 4 ▶ if ($doit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit) { } 3 1 4
617 my @commands = {
618 code => sub {
619 3 ▶ system 'ln', '-nsf', $oldfile, $newfile;
Subroutine __ANON__: called CC=2 SCAR=6.9
620 3 ▶ error "ln -nsf $oldfile $newfile failed" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 2 3
621 },
622 3 msg => "ln -nsf $oldfile $newfile",
623 rv => 1,
624 };
625 3 Doit::Commands->new(@commands);
626 } else {
627 1 Doit::Commands->return_zero;
628 }
629 }
630  
631 sub cmd_make_path {
632 4 ▶ my($self, @directories) = @_;
Subroutine cmd_make_path: called CC=3 SCAR=11.0
Pod undocumented
633 4 ▶ my $options = {}; if (ref $directories[-1] eq 'HASH') { $options = pop @directories }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $directories[-1] eq "HASH") 1 3 4
634 4 my @directories_to_create = grep { !-d $_ } @directories;
635 4 ▶ if (@directories_to_create) {
Branch: 1 branch
BranchTrueFalseTotal
if (@directories_to_create) { } 3 1 4
636 my @commands = {
637 code => sub {
638 3 ▶ require File::Path;
Subroutine __ANON__: called CC=2 SCAR=7.2
639 3 ▶ File::Path::make_path(@directories_to_create, $options)
Branch: 1 branch
BranchTrueFalseTotal
File::Path::make_path(@directories_to_create, $options) or &error($!) 0 2 2
640 or error $!;
641 },
642 3 msg => "make_path @directories",
643 rv => scalar @directories_to_create,
644 };
645 3 Doit::Commands->new(@commands);
646 } else {
647 1 Doit::Commands->return_zero;
648 }
649 }
650  
651 sub cmd_mkdir {
652 26 ▶ my($self, $directory, $mode) = @_;
Subroutine cmd_mkdir: called CC=3 SCAR=11.0
Pod undocumented
653 26 ▶ if (!-d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $directory) { } 24 2 26
654 24 my @commands;
655 24 ▶ if (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) { } 2 22 24
656 push @commands, {
657 2 ▶ code => sub { mkdir $directory, $mode or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
mkdir $directory, $mode or &error("$!") 1 1 2
658 2 msg => "mkdir $directory with mask $mode",
659 rv => 1,
660 };
661 } else {
662 push @commands, {
663 22 ▶ code => sub { mkdir $directory or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
mkdir $directory or &error("$!") 1 21 22
664 22 msg => "mkdir $directory",
665 rv => 1,
666 };
667 }
668 24 Doit::Commands->new(@commands);
669 } else {
670 2 Doit::Commands->return_zero;
671 }
672 }
673  
674 sub cmd_remove_tree {
675 6 ▶ my($self, @directories) = @_;
Subroutine cmd_remove_tree: called CC=3 SCAR=11.0
Pod undocumented
676 6 ▶ my $options = {}; if (ref $directories[-1] eq 'HASH') { $options = pop @directories }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $directories[-1] eq "HASH") 1 5 6
677 6 my @directories_to_remove = grep { -d $_ } @directories;
678 6 ▶ if (@directories_to_remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (@directories_to_remove) { } 5 1 6
679 my @commands = {
680 code => sub {
681 5 ▶ require File::Path;
Subroutine __ANON__: called CC=2 SCAR=6.9
682 5 ▶ File::Path::remove_tree(@directories_to_remove, $options)
Branch: 1 branch
BranchTrueFalseTotal
File::Path::remove_tree(@directories_to_remove, $options) or &error("$!") 1 4 5
683 or error "$!";
684 },
685 5 msg => "remove_tree @directories_to_remove",
686 rv => scalar @directories_to_remove,
687 };
688 5 Doit::Commands->new(@commands);
689 } else {
690 1 Doit::Commands->return_zero;
691 }
692 }
693  
694 sub cmd_rename {
695 15 ▶ my($self, $from, $to) = @_;
Subroutine cmd_rename: called CC=1 SCAR=0.0
Pod undocumented
696 15 my @commands;
697 push @commands, {
698 13 ▶ code => sub { rename $from, $to or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
rename $from, $to or &error("$!") 1 12 13
699 15 msg => "rename $from, $to",
700 rv => 1,
701 };
702 15 Doit::Commands->new(@commands);
703 }
704  
705 sub cmd_copy {
706 17 ▶ my($self, @args) = @_;
Subroutine cmd_copy: called CC=10 SCAR=23.0
Pod undocumented
707 17 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 3 14 17
Condition100%3/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
1133
MC/DC100%2/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
708 17 my $quiet = delete $options{quiet};
709 17 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 1 16 17
710 16 ▶ if (@args != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args != 2) 1 15 16
711 1 error "Expecting two arguments: from and to filenames";
712 }
713 15 my($from, $to) = @args;
714  
715 15 my $real_to;
716 15 ▶ if (-d $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $to) { } 4 11 15
717 4 require File::Basename;
718 4 $real_to = "$to/" . File::Basename::basename($from);
719 } else {
720 11 $real_to = $to;
721 }
722 15 ▶ if (!-e $real_to || do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $real_to or do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }) { } 11 4 15
Condition100%3/3
not -e $real_to or do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }
l!l&&r!l&&!r
744
MC/DC100%2/2
not -e $real_to or do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }
not -e $real_to do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }
Truth table100%3/3
not(A) or B
A= not -e $real_toB= do { require File::Compare; File::Compare::compare($from, $real_to) != 0 }
ABresult
1X1
011
000
723 my @commands = {
724 code => sub {
725 11 ▶ require File::Copy;
Subroutine __ANON__: called CC=2 SCAR=6.9
726 11 ▶ File::Copy::copy($from, $to)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($from, $to) or &error("Copy failed: $!") 2 9 11
727 or error "Copy failed: $!";
728 },
729 11 msg => do {
730 11 ▶ if (!-e $real_to) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $real_to) { } 7 4 11
731 7 "copy $from $real_to (destination does not exist)";
732 } else {
733 4 ▶ if ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quiet) { } 1 3 4
734 1 "copy $from $real_to";
735 } else {
736 3 "copy $from $real_to\ndiff:\n" . _diff_files($real_to, $from);
737 }
738 }
739 },
740 rv => 1,
741 };
742 11 Doit::Commands->new(@commands);
743 } else {
744 4 Doit::Commands->return_zero;
745 }
746 }
747  
748 sub cmd_move {
749 3 ▶ my($self, $from, $to) = @_;
Subroutine cmd_move: called CC=1 SCAR=0.0
Pod undocumented
750 my @commands = {
751 code => sub {
752 2 ▶ require File::Copy;
Subroutine __ANON__: called CC=2 SCAR=6.9
753 2 ▶ File::Copy::move($from, $to)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::move($from, $to) or &error("Move failed: $!") 1 1 2
754 or error "Move failed: $!";
755 },
756 3 msg => "move $from $to",
757 rv => 1,
758 };
759 3 Doit::Commands->new(@commands);
760 }
761  
762 sub _analyze_dollar_questionmark () {
763 52 ▶ if ($? == -1) {
Subroutine _analyze_dollar_questionmark: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 1 51 52
elsif ($? & 127) { } 8 43 51
764 (
765 1 msg => sprintf("Could not execute command: %s", $!),
766 errno => $!,
767 exitcode => $?,
768 );
769 } elsif ($? & 127) {
770 8 my $signalnum = $? & 127;
771 8 ▶ my $coredump = ($? & 128) ? 'with' : 'without';
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 8 8
772 (
773 8 msg => sprintf("Command died with signal %d, %s coredump", $signalnum, $coredump),
774 signalnum => $signalnum,
775 coredump => $coredump,
776 );
777 } else {
778 43 my $exitcode = $?>>8;
779 (
780 43 msg => "Command exited with exit code " . $exitcode,
781 exitcode => $exitcode,
782 );
783 }
784 }
785  
786 sub _handle_dollar_questionmark (@) {
787 48 ▶ my(%opts) = @_;
Subroutine _handle_dollar_questionmark: called CC=3 SCAR=11.0
788 48 my $prefix_msg = delete $opts{prefix_msg};
789 48 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 48 48
790  
791 48 my %res = _analyze_dollar_questionmark;
792 48 my $msg = delete $res{msg};
793 48 ▶ if (defined $prefix_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix_msg) 22 26 48
794 22 $msg = $prefix_msg.$msg;
795 }
796 48 Doit::Exception::throw($msg, %res);
797 }
798  
799 sub _show_cwd ($) {
800 139 ▶ my $flag = shift;
Subroutine _show_cwd: called CC=2 SCAR=6.9
801 139 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 42 97 139
802 42 require Cwd;
803 42 " (in " . Cwd::getcwd() . ")";
804 } else {
805 97 "";
806 }
807 }
808  
809 sub _open2 {
810 10 ▶ my($instr, @args) = @_;
Subroutine _open2: called CC=1 SCAR=0.0
811 10 @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN;
812  
813 10 require IPC::Open2;
814  
815 10 my($chld_out, $chld_in);
816 10 my $pid = IPC::Open2::open2($chld_out, $chld_in, @args);
817 10 print $chld_in $instr;
818 10 close $chld_in;
819 10 local $/;
820 10 my $buf = <$chld_out>;
821 10 close $chld_out;
822 10 waitpid $pid, 0;
823  
824 10 $buf;
825 }
826  
827 sub cmd_open2 {
828 11 ▶ my($self, @args) = @_;
Subroutine cmd_open2: called CC=7 SCAR=19.5
Pod undocumented
829 11 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 8 3 11
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
038
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
830 11 my $quiet = delete $options{quiet};
831 11 my $info = delete $options{info};
832 11 ▶ my $instr = delete $options{instr}; $instr = '' if !defined $instr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $instr 6 5 11
833 11 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 11 11
834  
835 my $code = sub {
836 10 ▶ my $buf = _open2($instr, @args);
Subroutine __ANON__: called CC=4 SCAR=13.9
837 10 ▶ $? == 0
Branch: 2 branches
BranchTrueFalseTotal
$? == 0 or &_handle_dollar_questionmark($quiet || $info ? ("prefix_msg", "open2 command '@{args}' failed: ") : ()) 3 7 10
$quiet || $info ? : 1 2 3
Condition66%2/3
$quiet or $info
l!l&&r!l&&!r
012
MC/DC50%1/2
$quiet or $info
$quiet $info
Truth table66%2/3
A or B
A= $quietB= $info
ABresult
1X1
011
000
838 or _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "open2 command '@args' failed: ") : ());
839 7 $buf;
840 11 };
841  
842 11 my @commands;
843 push @commands, {
844 11 ▶ ($info ? (rv => $code->(), code => sub {}) : (code => $code)),
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
$info ? : 4 7 11
$quiet ? : 1 9 10
845 ($quiet ? () : (msg => "@args")),
846 };
847 10 Doit::Commands->new(@commands);
848 }
849  
850 sub cmd_info_open2 {
851 3 ▶ my($self, @args) = @_;
Subroutine cmd_info_open2: called CC=3 SCAR=11.0
Pod undocumented
852 3 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 1 2 3
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
021
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
853 3 $options{info} = 1;
854 3 $self->cmd_open2(\%options, @args);
855 }
856  
857 sub _open3 {
858 74 ▶ my($instr, @args) = @_;
Subroutine _open3: called CC=8 SCAR=20.8
859 74 @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN;
860  
861 74 require IO::Select;
862 74 require IPC::Open3;
863 74 require Symbol;
864  
865 74 my($chld_out, $chld_in, $chld_err);
866 74 $chld_err = Symbol::gensym();
867 74 ▶ my $pid = IPC::Open3::open3((defined $instr ? $chld_in : undef), $chld_out, $chld_err, @args);
Branch: 1 branch
BranchTrueFalseTotal
defined $instr ? : 35 39 74
868 73 ▶ if (defined $instr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $instr) 35 38 73
869 35 print $chld_in $instr;
870 35 close $chld_in;
871 }
872  
873 73 my $sel = IO::Select->new;
874 73 $sel->add($chld_out);
875 73 $sel->add($chld_err);
876  
877 73 my %buf = ($chld_out => '', $chld_err => '');
878 73 ▶ while(my @ready_fhs = $sel->can_read()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready_fhs) = $sel->can_read) 164 73 237
879 164 for my $ready_fh (@ready_fhs) {
880 216 my $buf = '';
881 216 ▶ while (sysread $ready_fh, $buf, 1024, length $buf) { }
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $ready_fh, $buf, 1024, length $buf) 70 216 286
882 216 ▶ if ($buf eq '') { # eof
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq '') { } 146 70 216
883 146 $sel->remove($ready_fh);
884 146 $ready_fh->close;
885 146 ▶ last if $sel->count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sel->count == 0 73 73 146
886 } else {
887 70 $buf{$ready_fh} .= $buf;
888 }
889 }
890 }
891  
892 73 waitpid $pid, 0;
893  
894 73 ($buf{$chld_out}, $buf{$chld_err});
895 }
896  
897 sub cmd_open3 {
898 38 ▶ my($self, @args) = @_;
Subroutine cmd_open3: called CC=6 SCAR=17.9
Pod undocumented
899 38 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 36 2 38
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
0236
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
900 38 my $quiet = delete $options{quiet};
901 38 my $info = delete $options{info};
902 38 my $instr = delete $options{instr};
903 38 my $errref = delete $options{errref};
904 38 my $statusref = delete $options{statusref};
905 38 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 38 38
906  
907 my $code = sub {
908 37 ▶ my($stdout, $stderr) = _open3($instr, @args);
Subroutine __ANON__: called CC=6 SCAR=17.9
909  
910 36 ▶ if ($errref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errref) 29 7 36
911 29 $$errref = $stderr;
912 }
913  
914 36 ▶ if ($statusref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statusref) { } 2 34 36
915 2 %$statusref = ( _analyze_dollar_questionmark );
916 } else {
917 34 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 4 30 34
918 4 ▶ _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "open3 command '@args' failed: ") : ());
Branch: 1 branch
BranchTrueFalseTotal
$quiet || $info ? : 1 3 4
Condition66%2/3
$quiet or $info
l!l&&r!l&&!r
013
MC/DC50%1/2
$quiet or $info
$quiet $info
Truth table66%2/3
A or B
A= $quietB= $info
ABresult
1X1
011
000
919 }
920 }
921  
922 32 $stdout;
923 38 };
924  
925 38 my @commands;
926 push @commands, {
927 38 ▶ ($info ? (rv => $code->(), code => sub {}) : (code => $code)),
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
$info ? : 4 34 38
$quiet ? : 23 14 37
928 ($quiet ? () : (msg => "@args")),
929 };
930 37 Doit::Commands->new(@commands);
931 }
932  
933 sub cmd_info_open3 {
934 3 ▶ my($self, @args) = @_;
Subroutine cmd_info_open3: called CC=3 SCAR=11.0
Pod undocumented
935 3 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 1 2 3
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
021
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
936 3 $options{info} = 1;
937 3 $self->cmd_open3(\%options, @args);
938 }
939  
940 sub _qx {
941 143 ▶ my(@args) = @_;
Subroutine _qx: called CC=2 SCAR=7.0
942 143 @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN;
943  
944 143 ▶ open my $fh, '-|', @args
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @args or &error("Error running '@{args}': $!") 0 143 143
945 or error "Error running '@args': $!";
946 143 local $/;
947 143 my $buf = <$fh>;
948 143 close $fh;
949 143 \$buf;
950 }
951  
952 sub cmd_qx {
953 144 ▶ my($self, @args) = @_;
Subroutine cmd_qx: called CC=6 SCAR=17.9
Pod undocumented
954 144 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 138 6 144
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
06138
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
955 144 my $quiet = delete $options{quiet};
956 144 my $info = delete $options{info};
957 144 my $statusref = delete $options{statusref};
958 144 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 144 144
959  
960 my $code = sub {
961 143 ▶ my $bufref = _qx(@args);
Subroutine __ANON__: called CC=5 SCAR=16.1
962 143 ▶ if ($statusref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statusref) { } 2 141 143
963 2 %$statusref = ( _analyze_dollar_questionmark );
964 } else {
965 141 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 22 119 141
966 22 ▶ _handle_dollar_questionmark($quiet||$info ? (prefix_msg => "qx command '@args' failed: ") : ());
Branch: 1 branch
BranchTrueFalseTotal
$quiet || $info ? : 20 2 22
Condition100%3/3
$quiet or $info
l!l&&r!l&&!r
1912
MC/DC100%2/2
$quiet or $info
$quiet $info
Truth table100%3/3
A or B
A= $quietB= $info
ABresult
1X1
011
000
967 }
968 }
969 121 $$bufref;
970 144 };
971  
972 144 my @commands;
973 push @commands, {
974 144 ▶ ($info ? (rv => $code->(), code => sub {}) : (code => $code)),
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
$info ? : 135 9 144
$quiet ? : 113 11 124
975 ($quiet ? () : (msg => "@args")),
976 };
977 124 Doit::Commands->new(@commands);
978 }
979  
980 sub cmd_info_qx {
981 134 ▶ my($self, @args) = @_;
Subroutine cmd_info_qx: called CC=3 SCAR=11.0
Pod undocumented
982 134 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 131 3 134
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
03131
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
983 134 $options{info} = 1;
984 134 $self->cmd_qx(\%options, @args);
985 }
986  
987 sub cmd_rmdir {
988 11 ▶ my($self, $directory) = @_;
Subroutine cmd_rmdir: called CC=2 SCAR=6.9
Pod undocumented
989 11 ▶ if (-d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $directory) { } 10 1 11
990 my @commands = {
991 10 ▶ code => sub { rmdir $directory or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
rmdir $directory or &error("$!") 1 9 10
992 10 msg => "rmdir $directory",
993 };
994 10 Doit::Commands->new(@commands);
995 } else {
996 1 Doit::Commands->return_zero;
997 }
998 }
999  
1000 sub cmd_run {
1001 0 ▶ my($self, @args) = @_;
Subroutine cmd_run: not called CC=3 SCAR=24.8
Pod undocumented
1002 0 my @commands;
1003 push @commands, {
1004 code => sub {
1005 0 ▶ require IPC::Run;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1006 0 my $success = IPC::Run::run(@args);
1007 0 ▶ if (!$success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $success) 0 0 0
1008 0 _handle_dollar_questionmark;
1009 }
1010 },
1011 0 msg => do {
1012 0 my @print_cmd;
1013 0 for my $arg (@args) {
1014 0 ▶ if (ref $arg eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 0 0
1015 0 push @print_cmd, @$arg;
1016 } else {
1017 0 push @print_cmd, $arg;
1018 }
1019 }
1020 0 join " ", @print_cmd;
1021 },
1022 rv => 1,
1023 };
1024 0 Doit::Commands->new(@commands);
1025 }
1026  
1027 sub cmd_setenv {
1028 3 ▶ my($self, $key, $val) = @_;
Subroutine cmd_setenv: called CC=4 SCAR=13.9
Pod undocumented
1029 3 ▶ if (!defined $ENV{$key} || $ENV{$key} ne $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ENV{$key} or $ENV{$key} ne $val) { } 2 1 3
Condition100%3/3
not defined $ENV{$key} or $ENV{$key} ne $val
l!l&&r!l&&!r
111
MC/DC100%2/2
not defined $ENV{$key} or $ENV{$key} ne $val
not defined $ENV{$key} $ENV{$key} ne $val
Truth table100%3/3
not(A) or B
A= not defined $ENV{$key}B= $ENV{$key} ne $val
ABresult
1X1
011
000
1030 my @commands = {
1031 2 ▶ code => sub { $ENV{$key} = $val },
Subroutine __ANON__: called CC=1 SCAR=0.0
1032 2 ▶ msg => qq{set \$ENV{$key} to "$val", previous value was } . (defined $ENV{$key} ? qq{"$ENV{$key}"} : qq{unset}),
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{$key} ? : 1 1 2
1033 rv => 1,
1034 };
1035 2 Doit::Commands->new(@commands);
1036 } else {
1037 1 Doit::Commands->return_zero;
1038 }
1039 }
1040  
1041 sub cmd_symlink {
1042 3 ▶ my($self, $oldfile, $newfile) = @_;
Subroutine cmd_symlink: called CC=6 SCAR=18.4
Pod undocumented
1043 3 my $doit;
1044 3 ▶ if (-l $newfile) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $newfile) { } 1 2 3
elsif (not -e $newfile) { } 2 0 2
1045 1 ▶ my $points_to = readlink $newfile
Branch: 1 branch
BranchTrueFalseTotal
my $points_to = readlink $newfile or &error("Unexpected: readlink $newfile failed (race condition?)") 0 1 1
1046 or error "Unexpected: readlink $newfile failed (race condition?)";
1047 1 ▶ if ($points_to ne $oldfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points_to ne $oldfile) 0 1 1
1048 0 $doit = 1;
1049 }
1050 } elsif (!-e $newfile) {
1051 2 $doit = 1;
1052 } else {
1053 0 warning "$newfile exists but is not a symlink, will fail later...";
1054 }
1055 3 ▶ if ($doit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit) { } 2 1 3
1056 my @commands = {
1057 2 ▶ code => sub { symlink $oldfile, $newfile or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
symlink $oldfile, $newfile or &error("$!") 1 1 2
1058 2 msg => "symlink $oldfile $newfile",
1059 rv => 1,
1060 };
1061 2 Doit::Commands->new(@commands);
1062 } else {
1063 1 Doit::Commands->return_zero;
1064 }
1065 }
1066  
1067 sub cmd_system {
1068 141 ▶ my($self, @args) = @_;
Subroutine cmd_system: called CC=6 SCAR=17.9
Pod undocumented
1069 141 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 46 95 141
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
09546
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1070 141 my $quiet = delete $options{quiet};
1071 141 my $info = delete $options{info};
1072 141 my $show_cwd = delete $options{show_cwd};
1073 141 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 141 141
1074  
1075 141 @args = Doit::Win32Util::win32_quote_list(@args) if Doit::IS_WIN;
1076  
1077 my $code = sub {
1078 140 ▶ system @args;
Subroutine __ANON__: called CC=2 SCAR=6.9
1079 140 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 19 121 140
1080 19 _handle_dollar_questionmark;
1081 }
1082 141 };
1083  
1084 141 my @commands;
1085 push @commands, {
1086 ($info
1087 ? (
1088 2 ▶ rv => do { $code->(); 1 },
Branch: 1 branch
BranchTrueFalseTotal
$quiet ? : 2 139 141
1089 ▶ code => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
1090 )
1091 141 ▶ : (
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 2 139 141
1092 rv => 1,
1093 code => $code,
1094 )
1095 ),
1096 ($quiet ? () : (msg => "@args" . _show_cwd($show_cwd))),
1097 };
1098 141 Doit::Commands->new(@commands);
1099 }
1100  
1101 sub cmd_info_system {
1102 1 ▶ my($self, @args) = @_;
Subroutine cmd_info_system: called CC=3 SCAR=13.0
Pod undocumented
1103 1 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 0 1 1
Condition33%1/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
010
MC/DC0%0/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1104 1 $options{info} = 1;
1105 1 $self->cmd_system(\%options, @args);
1106 }
1107  
1108 sub cmd_touch {
1109 24 ▶ my($self, @files) = @_;
Subroutine cmd_touch: called CC=3 SCAR=11.0
Pod undocumented
1110 24 my @commands;
1111 24 for my $file (@files) {
1112 30 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $file) { } 29 1 30
1113 push @commands, {
1114 29 ▶ code => sub { open my $fh, '>>', $file or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or &error("$!") 1 28 29
1115 29 msg => "touch non-existent file $file",
1116 }
1117 } else {
1118 push @commands, {
1119 1 ▶ code => sub { utime time, time, $file or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
utime time, time, $file or &error("$!") 0 1 1
1120 1 msg => "touch existent file $file",
1121 };
1122 }
1123 }
1124 24 my $doit_commands = Doit::Commands->new(@commands);
1125 24 $doit_commands->set_last_rv(scalar @files);
1126 24 $doit_commands;
1127 }
1128  
1129 sub cmd_create_file_if_nonexisting {
1130 18 ▶ my($self, @files) = @_;
Subroutine cmd_create_file_if_nonexisting: called CC=4 SCAR=13.9
Pod undocumented
1131 18 my @commands;
1132 18 for my $file (@files) {
1133 20 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 18 2 20
1134 push @commands, {
1135 18 ▶ code => sub { open my $fh, '>>', $file or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or &error("$!") 1 17 18
1136 18 msg => "create empty file $file",
1137 };
1138 }
1139 }
1140 18 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) { } 17 1 18
1141 17 my $doit_commands = Doit::Commands->new(@commands);
1142 17 $doit_commands->set_last_rv(scalar @commands);
1143 17 $doit_commands;
1144 } else {
1145 1 Doit::Commands->return_zero;
1146 }
1147 }
1148  
1149 sub cmd_unlink {
1150 22 ▶ my($self, @files) = @_;
Subroutine cmd_unlink: called CC=5 SCAR=16.1
Pod undocumented
1151 22 my @files_to_remove;
1152 22 for my $file (@files) {
1153 30 ▶ if (-e $file || -l $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file or -l $file) 27 3 30
Condition100%3/3
-e $file or -l $file
l!l&&r!l&&!r
2613
MC/DC100%2/2
-e $file or -l $file
-e $file -l $file
Truth table100%3/3
A or B
A= -e $fileB= -l $file
ABresult
1X1
011
000
1154 27 push @files_to_remove, $file;
1155 }
1156 }
1157 22 ▶ if (@files_to_remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files_to_remove) { } 20 2 22
1158 my @commands = {
1159 20 ▶ code => sub { unlink @files_to_remove or error "$!" },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unlink @files_to_remove or &error("$!") 1 19 20
1160 20 msg => "unlink @files_to_remove", # shellquote?
1161 };
1162 20 Doit::Commands->new(@commands);
1163 } else {
1164 2 Doit::Commands->return_zero;
1165 }
1166 }
1167  
1168 sub cmd_unsetenv {
1169 2 ▶ my($self, $key) = @_;
Subroutine cmd_unsetenv: called CC=2 SCAR=6.9
Pod undocumented
1170 2 ▶ if (defined $ENV{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{$key}) { } 1 1 2
1171 my @commands = {
1172 1 ▶ code => sub { delete $ENV{$key} },
Subroutine __ANON__: called CC=1 SCAR=0.0
1173 1 msg => qq{unset \$ENV{$key}, previous value was "$ENV{$key}"},
1174 rv => 1,
1175 };
1176 1 Doit::Commands->new(@commands);
1177 } else {
1178 1 Doit::Commands->return_zero;
1179 }
1180 }
1181  
1182 sub cmd_utime {
1183 11 ▶ my($self, $atime, $mtime, @files) = @_;
Subroutine cmd_utime: called CC=10 SCAR=23.1
Pod undocumented
1184  
1185 11 my $now;
1186 11 ▶ if (!defined $atime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $atime) 1 10 11
1187 1 ▶ $atime = ($now ||= time);
Condition33%1/3
$now ||= time
l!l&&r!l&&!r
010
MC/DC0%0/2
$now ||= time
$now time
Truth table33%1/3
A ||= B
A= $nowB= time
ABresult
1X1
011
000
1188 }
1189 11 ▶ if (!defined $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mtime) 1 10 11
1190 1 ▶ $mtime = ($now ||= time);
Condition33%1/3
$now ||= time
l!l&&r!l&&!r
100
MC/DC0%0/2
$now ||= time
$now time
Truth table33%1/3
A ||= B
A= $nowB= time
ABresult
1X1
011
000
1191 }
1192  
1193 11 my @files_to_change;
1194 11 for my $file (@files) {
1195 15 my @s = stat $file;
1196 15 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) { } 11 4 15
1197 11 ▶ if ($s[8] != $atime || $s[9] != $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s[8] != $atime or $s[9] != $mtime) 10 1 11
Condition100%3/3
$s[8] != $atime or $s[9] != $mtime
l!l&&r!l&&!r
911
MC/DC100%2/2
$s[8] != $atime or $s[9] != $mtime
$s[8] != $atime $s[9] != $mtime
Truth table100%3/3
A or B
A= $s[8] != $atimeB= $s[9] != $mtime
ABresult
1X1
011
000
1198 10 push @files_to_change, $file;
1199 }
1200 } else {
1201 4 push @files_to_change, $file; # will fail later
1202 }
1203 }
1204  
1205 11 ▶ if (@files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files_to_change) { } 10 1 11
1206 my @commands = {
1207 code => sub {
1208 10 ▶ my $changed_files = utime $atime, $mtime, @files;
Subroutine __ANON__: called CC=4 SCAR=13.9
1209 10 ▶ if ($changed_files != @files_to_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed_files != @files_to_change) 3 7 10
1210 3 ▶ if (@files_to_change == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@files_to_change == 1) { } 1 2 3
elsif ($changed_files == 0) { } 1 1 2
1211 1 error "utime failed: $!";
1212 } elsif ($changed_files == 0) {
1213 1 error "utime failed on all files: $!";
1214 } else {
1215 1 error "utime failed on some files (" . (@files_to_change-$changed_files) . "/" . scalar(@files_to_change) . "): $!";
1216 }
1217 }
1218 },
1219 10 msg => "utime $atime, $mtime, @files",
1220 rv => scalar @files_to_change,
1221 };
1222 10 Doit::Commands->new(@commands);
1223 } else {
1224 1 Doit::Commands->return_zero;
1225 }
1226 }
1227  
1228 sub cmd_which {
1229 14 ▶ my($self, @args) = @_;
Subroutine cmd_which: called CC=2 SCAR=6.9
Pod undocumented
1230 14 ▶ if (@args != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args != 1) 2 12 14
1231 2 error "Expecting exactly one argument: command";
1232 }
1233 12 my $path = Doit::Util::is_in_path($args[0]);
1234 12 ▶ Doit::Commands->new({ rv => $path, code => sub {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
1235 }
1236  
1237 sub cmd_write_binary {
1238 37 ▶ my($self, @args) = @_;
Subroutine cmd_write_binary: called CC=16 SCAR=27.7
Pod undocumented
1239 37 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 15 22 37
Condition100%3/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
12115
MC/DC100%2/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1240 37 ▶ my $quiet = delete $options{quiet} || 0;
Condition100%2/2
delete $options{'quiet'} || 0
l!l
1324
MC/DC100%1/1
delete $options{'quiet'} || 0
delete $options{'quiet'}
Truth table100%2/2
A
A= delete $options{'quiet'}
Aresult
11
00
1241 37 ▶ my $atomic = exists $options{atomic} ? delete $options{atomic} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'atomic'} ? : 2 35 37
1242 37 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 1 36 37
1243 36 ▶ if (@args != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args != 2) 1 35 36
1244 1 error "Expecting two arguments: filename and contents";
1245 }
1246 35 my($filename, $content) = @args;
1247  
1248 35 my $doit;
1249 my $need_diff;
1250 35 ▶ if (!-e $filename) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -e $filename) { } 15 20 35
elsif (-s $filename != length $content) { } 14 6 20
1251 15 $doit = 1;
1252 } elsif (-s $filename != length($content)) {
1253 14 $doit = 1;
1254 14 $need_diff = 1;
1255 } else {
1256 6 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or &error("Can't open ${filename}: $!") 1 5 6
1257 or error "Can't open $filename: $!";
1258 5 binmode $fh;
1259 5 local $/;
1260 5 my $file_content = <$fh>;
1261 5 ▶ if ($file_content ne $content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_content ne $content) 1 4 5
1262 1 $doit = 1;
1263 1 $need_diff = 1;
1264 }
1265 }
1266  
1267 34 ▶ if ($doit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doit) { } 30 4 34
1268 my @commands = {
1269 code => sub {
1270 # XXX consider to reuse code for atomic writes:
1271 # either from Doit::File::file_atomic_write (problematic, different component)
1272 # or share code with change_file
1273 30 ▶ my $outfile = $atomic ? "$filename.$$.".time.".tmp" : $filename;
Subroutine __ANON__: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
$atomic ? : 28 2 30
1274 30 ▶ open my $ofh, '>', $outfile
Branch: 1 branch
BranchTrueFalseTotal
open my $ofh, ">", $outfile or &error("Can't write to ${outfile}: $!") 2 28 30
1275 or error "Can't write to $outfile: $!";
1276 28 ▶ if (-e $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) 14 14 28
1277 14 Doit::Util::copy_stat($filename, $outfile, ownership => 1, mode => 1);
1278 }
1279 28 binmode $ofh;
1280 28 print $ofh $content;
1281 28 ▶ close $ofh
Branch: 1 branch
BranchTrueFalseTotal
close $ofh or &error("While closing ${outfile}: $!") 0 28 28
1282 or error "While closing $outfile: $!";
1283 28 ▶ if ($atomic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atomic) 27 1 28
1284 27 ▶ rename $outfile, $filename
Branch: 1 branch
BranchTrueFalseTotal
rename $outfile, $filename or &error("Error while renaming $outfile to ${filename}: $!") 0 27 27
1285 or error "Error while renaming $outfile to $filename: $!";
1286 }
1287 },
1288 rv => 1,
1289 ($quiet >= 2
1290 ? ()
1291 30 ▶ : (msg => do {
Branch: 1 branch
BranchTrueFalseTotal
$quiet >= 2 ? : 4 26 30
1292 26 ▶ if ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quiet) { } 7 19 26
1293 7 ▶ if ($need_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_diff) { } 3 4 7
1294 3 "Replace existing file $filename";
1295 } else {
1296 4 "Create new file $filename";
1297 }
1298 } else {
1299 19 ▶ if ($need_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_diff) { } 9 10 19
1300 9 "Replace existing file $filename with diff:\n" . _diff_files($filename, \$content);
1301 } else {
1302 10 "Create new file $filename with content:\n$content";
1303 }
1304 }
1305 }
1306 )),
1307 };
1308 30 Doit::Commands->new(@commands);
1309 } else {
1310 4 Doit::Commands->return_zero;
1311 }
1312 }
1313  
1314 sub cmd_change_file {
1315 47 ▶ my($self, @args) = @_;
Subroutine cmd_change_file: called CC=54 SCAR=40.0
Pod undocumented
1316 47 ▶ my %options; if (@args && ref $args[0] eq 'HASH') { %options = %{ shift @args } }
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] eq "HASH") 3 44 47
Condition100%3/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
1433
MC/DC100%2/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
1317 47 my $check = delete $options{check};
1318 47 my $debug = delete $options{debug};
1319 47 ▶ if ($check && ref $check ne 'CODE') { error "check parameter should be a CODE reference" }
Branch: 1 branch
BranchTrueFalseTotal
if ($check and ref $check ne "CODE") 0 47 47
Condition66%2/3
$check and ref $check ne "CODE"
!ll&&!rl&&r
4520
MC/DC0%0/2
$check and ref $check ne "CODE"
$check ref $check ne "CODE"
Truth table66%2/3
A and B
A= $checkB= ref $check ne "CODE"
ABresult
0X0
100
111
1320 47 ▶ error "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 1 46 47
1321  
1322 46 ▶ if (@args < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 1) 1 45 46
1323 1 error "Expecting at least a filename and one or more changes";
1324 }
1325  
1326 45 my($file, @changes) = @args;
1327 45 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 2 43 45
1328 2 error "$file does not exist";
1329 }
1330 43 ▶ if (!-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 2 41 43
1331 2 error "$file is not a file";
1332 }
1333  
1334 41 my @commands;
1335  
1336 41 for (@changes) {
1337 46 ▶ if ($_->{add_if_missing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"add_if_missing"}) 26 20 46
1338 26 my $line = delete $_->{add_if_missing};
1339 26 $_->{unless_match} = $line;
1340 26 ▶ if (defined $_->{add_after} ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{'add_after'} or defined $_->{'add_after_first'} or defined $_->{'add_before'} or defined $_->{'add_before_last'}) { } 10 16 26
Condition100%9/9
defined $_->{"add_after"} or defined $_->{"add_after_first"} or defined $_->{"add_before"} or defined $_->{"add_before_last"}
l!l&&r!l&&!r
8216
defined $_->{"add_after"} or defined $_->{"add_after_first"} or defined $_->{"add_before"}
l!l&&r!l&&!r
6218
defined $_->{"add_after"} or defined $_->{"add_after_first"}
l!l&&r!l&&!r
4220
MC/DC100%4/4
defined $_->{"add_after"} or defined $_->{"add_after_first"} or defined $_->{"add_before"} or defined $_->{"add_before_last"}
defined $_->{"add_after"} defined $_->{"add_after_first"} defined $_->{"add_before"} defined $_->{"add_before_last"}
Truth table100%5/5
A or B or C or D
A= defined $_->{"add_after"}B= defined $_->{"add_after_first"}C= defined $_->{"add_before"}D= defined $_->{"add_before_last"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1341 defined $_->{add_after_first} ||
1342 defined $_->{add_before} ||
1343 defined $_->{add_before_last}
1344 ) {
1345 my $defines =
1346 (defined $_->{add_after} || 0) +
1347 (defined $_->{add_after_first} || 0) +
1348 (defined $_->{add_before} || 0) +
1349 10 ▶ (defined $_->{add_before_last} || 0)
Condition100%8/8
defined $_->{'add_after'} || 0
l!l
46
defined $_->{'add_after_first'} || 0
l!l
28
defined $_->{'add_before'} || 0
l!l
28
defined $_->{'add_before_last'} || 0
l!l
28
MC/DC100%1/1
defined $_->{'add_after'} || 0
defined $_->{'add_after'}
MC/DC100%1/1
defined $_->{'add_after_first'} || 0
defined $_->{'add_after_first'}
MC/DC100%1/1
defined $_->{'add_before'} || 0
defined $_->{'add_before'}
MC/DC100%1/1
defined $_->{'add_before_last'} || 0
defined $_->{'add_before_last'}
Truth table100%2/2
A
A= defined $_->{'add_after'}
Aresult
11
00
Truth table100%2/2
A
A= defined $_->{'add_after_first'}
Aresult
11
00
Truth table100%2/2
A
A= defined $_->{'add_before'}
Aresult
11
00
Truth table100%2/2
A
A= defined $_->{'add_before_last'}
Aresult
11
00
1350 ;
1351 10 ▶ if ($defines != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defines != 1) 0 10 10
1352 0 error "Can specify only one of the following: 'add_after', 'add_after_first', 'add_before', 'add_before_last' (change for $file)\n";
1353 }
1354 10 my $add;
1355 my $do_after;
1356 10 my $reverse;
1357 10 ▶ if (defined $_->{add_after}) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $_->{'add_after'}) { } 4 6 10
elsif (defined $_->{'add_after_first'}) { } 2 4 6
elsif (defined $_->{'add_before'}) { } 2 2 4
elsif (defined $_->{'add_before_last'}) { } 2 0 2
1358 4 $add = delete $_->{add_after};
1359 4 $reverse = 1;
1360 4 $do_after = 1;
1361 } elsif (defined $_->{add_after_first}) {
1362 2 $add = delete $_->{add_after_first};
1363 2 $reverse = 0;
1364 2 $do_after = 1;
1365 } elsif (defined $_->{add_before}) {
1366 2 $add = delete $_->{add_before};
1367 2 $reverse = 0;
1368 2 $do_after = 0;
1369 } elsif (defined $_->{add_before_last}) {
1370 2 $add = delete $_->{add_before_last};
1371 2 $reverse = 1;
1372 2 $do_after = 0;
1373 } else {
1374 0 error "Can never happen";
1375 }
1376 10 qr{$add}; # must be a regexp
1377 $_->{action} = sub {
1378 6 ▶ my $arrayref = $_[0];
Subroutine __ANON__: called CC=9 SCAR=22.0
1379 6 my $found = 0;
1380 6 ▶ my $from = $reverse ? $#$arrayref : 0;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 4 2 6
1381 6 ▶ my $to = $reverse ? 0 : $#$arrayref;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 4 2 6
1382 6 ▶ my $inc = $reverse ? -1 : +1;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 4 2 6
1383 6 ▶ for(my $i=$from; ($reverse ? $i>=$to : $i<=$to); $i+=$inc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reverse ? $i >= $to : $i <= $to) 12 1 13
$reverse ? : 11 2 13
1384 12 ▶ if ($arrayref->[$i] =~ $add) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arrayref->[$i] =~ /$add/) 5 7 12
1385 5 ▶ if ($do_after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_after) { } 3 2 5
1386 3 splice @$arrayref, $i+1, 0, $line;
1387 } else {
1388 2 splice @$arrayref, $i, 0, $line;
1389 }
1390 5 $found = 1;
1391 5 last;
1392 }
1393 }
1394 6 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) 1 5 6
1395 1 error "Cannot find '$add' in file";
1396 }
1397 10 };
1398 } else {
1399 16 ▶ $_->{action} = sub { my $arrayref = $_[0]; push @$arrayref, $line };
Subroutine __ANON__: called CC=1 SCAR=0.0
1400 }
1401 }
1402 }
1403  
1404 41 my @match_actions;
1405 my @unless_match_actions;
1406 41 for (@changes) {
1407 46 ▶ if ($_->{unless_match}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->{'unless_match'}) { } 29 17 46
elsif ($_->{'match'}) { } 16 1 17
1408 29 ▶ if (ref $_->{unless_match} ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{"unless_match"} ne "Regexp") 26 3 29
1409 26 my $rx = '^' . quotemeta($_->{unless_match}) . '$';
1410 26 $_->{unless_match} = qr{$rx};
1411 }
1412 29 ▶ if (!$_->{action}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->{"action"}) 1 28 29
1413 1 error "action is missing";
1414 }
1415 28 ▶ if (ref $_->{action} ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{"action"} ne "CODE") 1 27 28
1416 1 error "action must be a sub reference";
1417 }
1418 27 push @unless_match_actions, $_;
1419 } elsif ($_->{match}) {
1420 16 ▶ if (ref $_->{match} ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{"match"} ne "Regexp") 3 13 16
1421 3 my $rx = '^' . quotemeta($_->{match}) . '$';
1422 3 $_->{match} = qr{$rx};
1423 }
1424 16 ▶ my $consequences = ($_->{action}?1:0) + (defined $_->{replace}?1:0) + (defined $_->{delete}?1:0);
Branch: 3 branches
BranchTrueFalseTotal
$_->{'action'} ? : 3 13 16
defined $_->{'replace'} ? : 7 9 16
defined $_->{'delete'} ? : 5 11 16
1425 16 ▶ if ($consequences != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($consequences != 1) 1 15 16
1426 1 error "Exactly one of the following is missing: action, replace, or delete";
1427 }
1428 15 ▶ if ($_->{action}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_->{'action'}) { } 3 12 15
elsif (defined $_->{'replace'}) { } 7 5 12
elsif (defined $_->{'delete'}) { } 5 0 5
1429 3 ▶ if (ref $_->{action} ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{"action"} ne "CODE") 1 2 3
1430 1 error "action must be a sub reference";
1431 }
1432 } elsif (defined $_->{replace}) {
1433 # accept
1434 } elsif (defined $_->{delete}) {
1435 # accept
1436 } else {
1437 0 error "FATAL: should never happen";
1438 }
1439 14 push @match_actions, $_;
1440 } else {
1441 1 error "match or unless_match is missing";
1442 }
1443 }
1444  
1445 36 require File::Temp;
1446 36 require File::Basename;
1447 36 require File::Copy;
1448 36 my($tmpfh,$tmpfile) = File::Temp::tempfile('doittemp_XXXXXXXX', UNLINK => 1, DIR => File::Basename::dirname($file));
1449 36 ▶ File::Copy::copy($file, $tmpfile)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($file, $tmpfile) or &error("failed to copy $file to temporary file ${tmpfile}: $!") 0 36 36
1450 or error "failed to copy $file to temporary file $tmpfile: $!";
1451 36 Doit::Util::copy_stat($file, $tmpfile);
1452  
1453 36 require Tie::File;
1454 36 ▶ tie my @lines, 'Tie::File', $tmpfile
Branch: 1 branch
BranchTrueFalseTotal
tie my @lines, "Tie::File", $tmpfile or &error("cannot tie file ${file}: $!") 0 36 36
1455 or error "cannot tie file $file: $!";
1456  
1457 36 my $no_of_changes = 0;
1458 36 for my $match_action (@match_actions) {
1459 14 my $match = $match_action->{match};
1460 14 ▶ for(my $line_i=0; $line_i<=$#lines; $line_i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_i <= $#lines) 44 14 58
1461 44 ▶ if ($debug) { info "change_file check '$lines[$line_i]' =~ '$match'" }
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 44 44
1462 44 ▶ if ($lines[$line_i] =~ $match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$line_i] =~ /$match/) 18 26 44
1463 18 ▶ if (exists $match_action->{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $match_action->{'replace'}) { } 9 9 18
elsif (exists $match_action->{'delete'}) { } 6 3 9
1464 9 my $replace = $match_action->{replace};
1465 9 ▶ if ($lines[$line_i] ne $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$line_i] ne $replace) 9 0 9
1466 9 push @commands, { msg => "replace '$lines[$line_i]' with '$replace' in '$file'" };
1467 9 $lines[$line_i] = $replace;
1468 9 $no_of_changes++;
1469 }
1470 } elsif (exists $match_action->{delete}) {
1471 6 ▶ if ($match_action->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_action->{"delete"}) 5 1 6
1472 5 push @commands, { msg => "delete '$lines[$line_i]' in '$file'" };
1473 5 splice @lines, $line_i, 1;
1474 5 $line_i--;
1475 5 $no_of_changes++;
1476 }
1477 } else {
1478 3 push @commands, { msg => "matched '$match' on line '$lines[$line_i]' in '$file' -> execute action" };
1479 3 my $action = $match_action->{action};
1480 3 $action->($lines[$line_i]);
1481 3 $no_of_changes++;
1482 }
1483 }
1484 }
1485 }
1486 36 ITER: for my $unless_match_action (@unless_match_actions) {
1487 27 my $match = $unless_match_action->{unless_match};
1488 27 for my $line (@lines) {
1489 70 ▶ if ($line =~ $match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$match/) 8 62 70
1490 8 next ITER;
1491 }
1492 }
1493 19 push @commands, { msg => "did not find '$match' in '$file' -> execute action" };
1494 19 my $action = $unless_match_action->{action};
1495 19 $action->(\@lines);
1496 18 $no_of_changes++;
1497 }
1498  
1499 35 untie @lines;
1500 35 close $tmpfh;
1501  
1502 35 ▶ if ($no_of_changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_of_changes) 25 10 35
1503 push @commands, {
1504 code => sub {
1505 24 ▶ if ($check) {
Subroutine __ANON__: called CC=4 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 2 22 24
1506 # XXX maybe it would be good to pass the Doit::Runner object,
1507 # but unfortunately it's not available at this point ---
1508 # maybe the code sub should generally get it as first argument?
1509 2 ▶ $check->($tmpfile)
Branch: 1 branch
BranchTrueFalseTotal
&$check($tmpfile) or &error("Check on file $file failed") 0 1 1
1510 or error "Check on file $file failed";
1511 }
1512 23 ▶ rename $tmpfile, $file
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfile, $file or &error("Can't rename $tmpfile to ${file}: $!") 0 23 23
1513 or error "Can't rename $tmpfile to $file: $!";
1514 },
1515 25 msg => do {
1516 25 "Final changes as diff:\n" . _diff_files($file, $tmpfile);
1517 },
1518 rv => $no_of_changes,
1519 };
1520 }
1521  
1522 35 ▶ if ($no_of_changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_of_changes) { } 25 10 35
1523 25 Doit::Commands->new(@commands);
1524 } else {
1525 10 Doit::Commands->return_zero;
1526 }
1527 }
1528  
1529 sub _diff_files {
1530 37 ▶ my($file1, $file2) = @_;
Subroutine _diff_files: called CC=22 SCAR=47.4
1531  
1532 37 my $stdin;
1533 37 ▶ if (ref $file2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file2) 9 28 37
1534 9 $stdin = $$file2;
1535 9 $file2 = '-';
1536 }
1537  
1538 37 ▶ if (!@diff_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Doit::diff_cmd) 8 29 37
1539 8 my @diff_candidates = (['diff', '-u']);
1540 8 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 8 8
1541 0 push @diff_candidates, ['fc'];
1542 }
1543 8 for my $diff_candidate (@diff_candidates) {
1544 8 ▶ if (Doit::Util::is_in_path($diff_candidate->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (Doit::Util::is_in_path($diff_candidate->[0])) 8 0 8
1545 8 @diff_cmd = @$diff_candidate;
1546 8 last;
1547 }
1548 }
1549 8 ▶ return "(diff not available" . (!$diff_error_shown++ ? ", error: none of the candidates (" . join(", ", map { $_->[0] } @diff_candidates) . ") exist" : "") . ")"
Branch: 2 branches
BranchTrueFalseTotal
unless @Doit::diff_cmd 0 8 8
!$diff_error_shown++ ? : 0 0 0
1550 if !@diff_cmd;
1551 }
1552  
1553 37 my $cannot_use_dash;
1554 37 ▶ if ($^O eq 'MSWin32' && $diff_cmd[0] eq 'fc') { # FC cannot handle forward slashes
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and $Doit::diff_cmd[0] eq "fc") 0 37 37
Condition33%1/3
$^O eq "MSWin32" and $Doit::diff_cmd[0] eq "fc"
!ll&&!rl&&r
3700
MC/DC0%0/2
$^O eq "MSWin32" and $Doit::diff_cmd[0] eq "fc"
$^O eq "MSWin32" $Doit::diff_cmd[0] eq "fc"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $Doit::diff_cmd[0] eq "fc"
ABresult
0X0
100
111
1555 0 s{/}{\\}g for ($file1, $file2);
1556 0 ▶ if ($file2 eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file2 eq "-") 0 0 0
1557 0 $cannot_use_dash = 1;
1558 }
1559 }
1560  
1561 37 my($diff, $diff_stderr);
1562 37 ▶ if (!$cannot_use_dash && eval { require IPC::Run; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cannot_use_dash and eval { do { require IPC::Run; 1 } }) { } 0 37 37
Condition33%1/3
not $cannot_use_dash and eval { do { require IPC::Run; 1 } }
!ll&&!rl&&r
0370
MC/DC0%0/2
not $cannot_use_dash and eval { do { require IPC::Run; 1 } }
not $cannot_use_dash eval { do { require IPC::Run; 1 } }
Truth table33%1/3
not(A) and B
A= not $cannot_use_dashB= eval { do { require IPC::Run; 1 } }
ABresult
0X0
100
111
1563 0 ▶ if (!eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { IPC::Run::run([@Doit::diff_cmd, $file1, $file2], defined $stdin ? ("<", \$stdin) : (), ">", \$diff, "2>", \$diff_stderr); 1 } }) 0 0 0
1564 0 ▶ IPC::Run::run([@diff_cmd, $file1, $file2], (defined $stdin ? ('<', \$stdin) : ()), '>', \$diff, '2>', \$diff_stderr); 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $stdin ? : 0 0 0
1565 }) {
1566 0 ▶ $diff = "(diff not available" . (!$diff_error_shown++ ? ", error: $@" : "") . ")";
Branch: 1 branch
BranchTrueFalseTotal
!$diff_error_shown++ ? : 0 0 0
1567 0 $diff_stderr = '';
1568 }
1569 } else {
1570 37 ▶ if ($^O eq 'MSWin32' || $cannot_use_dash) { # list systems with unreliable IPC::Open3 here
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32' or $cannot_use_dash) { } 0 37 37
Condition33%1/3
$^O eq "MSWin32" or $cannot_use_dash
l!l&&r!l&&!r
0037
MC/DC0%0/2
$^O eq "MSWin32" or $cannot_use_dash
$^O eq "MSWin32" $cannot_use_dash
Truth table33%1/3
A or B
A= $^O eq "MSWin32"B= $cannot_use_dash
ABresult
1X1
011
000
1571 0 my $tmp;
1572 0 ▶ if ($file2 eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file2 eq "-") 0 0 0
1573 0 require File::Temp;
1574 0 $tmp = File::Temp->new;
1575 0 binmode($tmp); # XXX yes or no?
1576 0 $tmp->print($stdin);
1577 0 $tmp->close;
1578 0 $file2 = "$tmp";
1579 }
1580 0 my $diffref = _qx(@diff_cmd, $file1, $file2);
1581 0 $diff = $$diffref;
1582 0 $diff_stderr = '';
1583 } else {
1584 37 ($diff, $diff_stderr) = eval { _open3($stdin, @diff_cmd, $file1, $file2) };
1585 37 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 37 37
1586 0 ▶ $diff = "(diff not available" . (!$diff_error_shown++ ? ", error: $@" : "") . ")";
Branch: 1 branch
BranchTrueFalseTotal
!$diff_error_shown++ ? : 0 0 0
1587 }
1588 }
1589 }
1590 37 "$diff$diff_stderr";
1591 }
1592  
1593 }
1594  
1595 {
1596 47 package Doit::Commands;
1597 sub new {
1598 625 ▶ my($class, @commands) = @_;
Subroutine new: called CC=1 SCAR=0.0
1599 625 my $self = bless \@commands, $class;
1600 625 $self;
1601 }
1602 sub return_zero {
1603 46 ▶ my $class = shift;
Subroutine return_zero: called CC=1 SCAR=0.0
1604 46 ▶ $class->new({ code => sub {}, rv => 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
1605 }
1606 671 ▶ sub commands { @{$_[0]} }
Subroutine commands: called CC=1 SCAR=0.0
1607 sub set_last_rv {
1608 46 ▶ my($self, $rv) = @_;
Subroutine set_last_rv: called CC=2 SCAR=7.0
1609 46 my @commands = $self->commands;
1610 46 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) 46 0 46
1611 46 $commands[-1]->{rv} = $rv;
1612 }
1613 }
1614 sub doit {
1615 605 ▶ my($self) = @_;
Subroutine doit: called CC=5 SCAR=16.1
1616 605 my $rv;
1617 605 for my $command ($self->commands) {
1618 645 ▶ if (exists $command->{msg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{"msg"}) 432 213 645
1619 432 Doit::Log::info($command->{msg});
1620 }
1621 645 ▶ if (exists $command->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{"code"}) 611 34 645
1622 611 my $this_rv = $command->{code}->();
1623 559 ▶ if (exists $command->{rv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{'rv'}) { } 483 76 559
1624 483 $rv = $command->{rv};
1625 } else {
1626 76 $rv = $this_rv;
1627 }
1628 }
1629 }
1630 553 $rv;
1631 }
1632 sub show {
1633 20 ▶ my($self) = @_;
Subroutine show: called CC=5 SCAR=16.1
1634 20 my $rv;
1635 20 for my $command ($self->commands) {
1636 21 ▶ if (exists $command->{msg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{"msg"}) 17 4 21
1637 17 Doit::Log::info($command->{msg} . " (dry-run)");
1638 }
1639 21 ▶ if (exists $command->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{"code"}) 20 1 21
1640 20 ▶ if (exists $command->{rv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command->{'rv'}) { } 17 3 20
1641 17 $rv = $command->{rv};
1642 } else {
1643 # Well, in dry-run mode we have no real return value...
1644 }
1645 }
1646 }
1647 20 $rv;
1648 }
1649 }
1650  
1651 {
1652 47 package Doit::Runner;
1653 sub new {
1654 46 ▶ my($class, $Doit, %options) = @_;
Subroutine new: called CC=2 SCAR=7.0
1655 46 my $dryrun = delete $options{dryrun};
1656 46 ▶ die "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 46 46
1657 46 bless { Doit => $Doit, dryrun => $dryrun, components => [] }, $class;
1658 }
1659 18 ▶ sub is_dry_run { shift->{dryrun} }
Subroutine is_dry_run: called CC=1 SCAR=0.0
1660  
1661 1 ▶ sub can_ipc_run { eval { require IPC::Run; 1 } }
Subroutine can_ipc_run: called CC=1 SCAR=0.4
1662  
1663 sub install_generic_cmd {
1664 2 ▶ my($self, $name, @args) = @_;
Subroutine install_generic_cmd: called CC=1 SCAR=0.0
1665 2 $self->{Doit}->install_generic_cmd($name, @args);
1666 2 __PACKAGE__->install_cmd($name);
1667 }
1668  
1669 sub install_cmd {
1670 1412 ▶ shift; # $class unused
Subroutine install_cmd: called CC=1 SCAR=0.0
1671 1412 my $cmd = shift;
1672 1412 my $meth = 'cmd_' . $cmd;
1673 my $code = sub {
1674 674 ▶ my($self, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
1675 674 ▶ if ($self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dryrun'}) { } 22 652 674
1676 22 $self->{Doit}->$meth(@args)->show;
1677 } else {
1678 652 $self->{Doit}->$meth(@args)->doit;
1679 }
1680 1412 };
1681 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1682 1412 *{$cmd} = $code;
1683 }
1684  
1685 sub add_component {
1686 15 ▶ my($self, $component_or_module) = @_;
Subroutine add_component: called CC=9 SCAR=22.1
1687 15 my $module;
1688 15 ▶ if ($component_or_module =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component_or_module =~ /::/) { } 3 12 15
1689 3 $module = $component_or_module;
1690 } else {
1691 12 $module = 'Doit::' . ucfirst($component_or_module);
1692 }
1693  
1694 15 for (@{ $self->{components} }) {
1695 6 ▶ return if $_->{module} eq $module;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"module"} eq $module 2 4 6
1696 }
1697  
1698 13 ▶ if (!eval qq{ require $module; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval " require $module; 1 ") 1 12 13
1699 1 Doit::Log::error("Cannot load $module: $@");
1700 }
1701 12 ▶ my $o = $module->new
Branch: 1 branch
BranchTrueFalseTotal
my $o = $module->new or &Doit::Log::error("Error while calling $module->new") 0 12 12
1702 or Doit::Log::error("Error while calling $module->new");
1703 12 for my $function ($o->functions) {
1704 36 my $fullqual = $module.'::'.$function;
1705 my $code = sub {
1706 329 ▶ my($self, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1707 329 $self->$fullqual(@args);
1708 36 };
1709 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1710 36 *{$function} = $code;
1711 }
1712 12 my $mod_file = do {
1713 12 (my $relpath = $module) =~ s{::}{/};
1714 12 $relpath .= '.pm';
1715 };
1716 12 push @{ $self->{components} }, { module => $module, path => $INC{$mod_file}, relpath => $mod_file };
1717  
1718 12 ▶ if ($o->can('add_components')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->can("add_components")) 0 12 12
1719 0 for my $sub_component ($o->add_components) {
1720 0 $self->add_component($sub_component);
1721 }
1722 }
1723 }
1724  
1725 47 for my $cmd (
1726 qw(chmod chown mkdir rename rmdir symlink unlink utime),
1727 qw(make_path remove_tree), # File::Path
1728 qw(copy move), # File::Copy
1729 qw(run), # IPC::Run
1730 qw(qx info_qx), # qx// and variant which even runs in dry-run mode, both using list syntax
1731 qw(open2 info_open2), # IPC::Open2
1732 qw(open3 info_open3), # IPC::Open3
1733 qw(system info_system), # builtin system with variant
1734 qw(cond_run), # conditional run
1735 qw(touch), # like unix touch
1736 qw(ln_nsf), # like unix ln -nsf
1737 qw(which), # like unix which
1738 qw(create_file_if_nonexisting), # does the half of touch
1739 qw(write_binary), # like File::Slurper
1740 qw(change_file), # own invention
1741 qw(setenv unsetenv), # $ENV manipulation
1742 ) {
1743 1410 __PACKAGE__->install_cmd($cmd);
1744 }
1745  
1746 sub call_wrapped_method {
1747 6 ▶ my($self, $context, $method, @args) = @_;
Subroutine call_wrapped_method: called CC=3 SCAR=11.0
1748 6 my @ret;
1749 6 ▶ if ($context eq 'a') {
Branch: 1 branch
BranchTrueFalseTotal
if ($context eq 'a') { } 2 4 6
1750 2 @ret = eval { $self->$method(@args) };
1751 } else {
1752 4 $ret[0] = eval { $self->$method(@args) };
1753 }
1754 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 4 6
1755 2 ('e', $@);
1756 } else {
1757 4 ('r', @ret);
1758 }
1759 }
1760  
1761 # XXX call vs. call_with_runner ???
1762 sub call {
1763 6 ▶ my($self, $sub, @args) = @_;
Subroutine call: called CC=2 SCAR=7.0
1764 6 ▶ $sub = 'main::' . $sub if $sub !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /::/ 6 0 6
1765 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1766 6 &$sub(@args);
1767 }
1768  
1769 sub call_with_runner {
1770 2 ▶ my($self, $sub, @args) = @_;
Subroutine call_with_runner: called CC=2 SCAR=7.0
1771 2 ▶ $sub = 'main::' . $sub if $sub !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /::/ 2 0 2
1772 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1773 2 &$sub($self, @args);
1774 }
1775  
1776 # XXX does this belong here?
1777 sub do_ssh_connect {
1778 6 ▶ my($self, $host, %opts) = @_;
Subroutine do_ssh_connect: called CC=1 SCAR=0.4
1779 6 my $remote = Doit::SSH->do_connect($host, dry_run => $self->is_dry_run, components => $self->{components}, %opts);
1780 0 $remote;
1781 }
1782  
1783 # XXX does this belong here?
1784 sub do_sudo {
1785 0 ▶ my($self, %opts) = @_;
Subroutine do_sudo: not called CC=1 SCAR=6.9
1786 0 my $sudo = Doit::Sudo->do_connect(dry_run => $self->is_dry_run, components => $self->{components}, %opts);
1787 0 $sudo;
1788 }
1789 }
1790  
1791 {
1792 47 package Doit::RPC;
1793  
1794 47 require Storable;
1795 47 require IO::Handle;
1796  
1797 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
1798  
1799 sub new {
1800 0 ▶ die "Please use either Doit::RPC::Client, Doit::RPC::Server or Doit::RPC::SimpleServer";
Subroutine new: not called CC=1 SCAR=6.9
1801 }
1802  
1803 6 ▶ sub runner { shift->{runner} }
Subroutine runner: called CC=1 SCAR=0.0
1804  
1805 sub receive_data {
1806 14 ▶ my($self) = @_;
Subroutine receive_data: called CC=4 SCAR=14.8
1807 14 my $fh = $self->{infh};
1808 14 my $buf;
1809 14 my $ret = read $fh, $buf, 4;
1810 14 ▶ if (!defined $ret) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ret) { } 0 14 14
elsif (not $ret) { } 0 14 14
1811 0 die "receive_data failed (getting length): $!";
1812 } elsif (!$ret) {
1813 0 return; # eof
1814 }
1815 14 my $length = unpack("N", $buf);
1816 14 ▶ read $fh, $buf, $length or die "receive_data failed (getting data): $!";
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buf, $length or die "receive_data failed (getting data): $!" 0 14 14
1817 14 @{ Storable::thaw($buf) };
1818 }
1819  
1820 sub send_data {
1821 14 ▶ my($self, @cmd) = @_;
Subroutine send_data: called CC=1 SCAR=0.0
1822 14 my $fh = $self->{outfh};
1823 14 my $data = Storable::nfreeze(\@cmd);
1824 14 print $fh pack("N", length($data)) . $data;
1825 }
1826  
1827 {
1828 47 my $done_POSIX_warning;
1829 sub _reap_process {
1830 0 ▶ my($self, $pid) = @_;
Subroutine _reap_process: not called CC=6 SCAR=37.4
1831 0 ▶ return if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
1832 0 ▶ if (eval { require POSIX; defined &POSIX::WNOHANG }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require POSIX; defined &POSIX::WNOHANG } }) { } 0 0 0
1833 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
1834 0 info "Reaping process $pid...";
1835 }
1836 0 my $start_time = time;
1837 my $got_pid = Doit::RPC::gentle_retry(
1838 code => sub {
1839 0 ▶ waitpid $pid, &POSIX::WNOHANG;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1840 },
1841 retry_msg_code => sub {
1842 0 ▶ my($seconds) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1843 0 ▶ if (time - $start_time >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $start_time >= 2) 0 0 0
1844 0 info "can't reap process $pid, sleep for $seconds seconds";
1845 }
1846 },
1847 0 fast_sleep => 0.01,
1848 );
1849 0 ▶ if (!$got_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $got_pid) 0 0 0
1850 0 warning "Could not reap process $pid...";
1851 }
1852 } else {
1853 0 ▶ if (!$done_POSIX_warning++) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done_POSIX_warning++) 0 0 0
1854 0 warning "Can't require POSIX, cannot reap zombies..."
1855 }
1856 }
1857 }
1858 }
1859  
1860 47 sub gentle_retry {
1861 0 ▶ my(%opts) = @_;
Subroutine gentle_retry: not called CC=14 SCAR=53.5
1862 0 ▶ my $code = delete $opts{code} || die "code is mandatory";
Condition0%0/2
delete $opts{'code'} || die('code is mandatory')
l!l
00
MC/DC0%0/1
delete $opts{'code'} || die('code is mandatory')
delete $opts{'code'}
Truth table0%0/2
A
A= delete $opts{'code'}
Aresult
11
00
1863 0 ▶ my $tries = delete $opts{tries} || 20;
Condition0%0/2
delete $opts{'tries'} || 20
l!l
00
MC/DC0%0/1
delete $opts{'tries'} || 20
delete $opts{'tries'}
Truth table0%0/2
A
A= delete $opts{'tries'}
Aresult
11
00
1864 0 ▶ my $fast_tries = delete $opts{fast_tries} || int($tries/2);
Condition0%0/3
delete $opts{'fast_tries'} || int $tries / 2
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'fast_tries'} || int $tries / 2
delete $opts{'fast_tries'} int $tries / 2
Truth table0%0/3
A || B
A= delete $opts{'fast_tries'}B= int $tries / 2
ABresult
1X1
011
000
1865 0 ▶ my $slow_sleep = delete $opts{slow_sleep} || 1;
Condition0%0/2
delete $opts{'slow_sleep'} || 1
l!l
00
MC/DC0%0/1
delete $opts{'slow_sleep'} || 1
delete $opts{'slow_sleep'}
Truth table0%0/2
A
A= delete $opts{'slow_sleep'}
Aresult
11
00
1866 0 ▶ my $fast_sleep = delete $opts{fast_sleep} || 0.1;
Condition0%0/2
delete $opts{'fast_sleep'} || 0.1
l!l
00
MC/DC0%0/1
delete $opts{'fast_sleep'} || 0.1
delete $opts{'fast_sleep'}
Truth table0%0/2
A
A= delete $opts{'fast_sleep'}
Aresult
11
00
1867 0 my $retry_msg_code = delete $opts{retry_msg_code};
1868 0 my $fail_info_ref = delete $opts{fail_info_ref};
1869 0 ▶ die "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
1870  
1871 0 for my $try (1..$tries) {
1872 0 my $ret = $code->(fail_info_ref => $fail_info_ref, try => $try);
1873 0 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 0 0
1874 0 my $sleep_sub;
1875 0 ▶ if ($fast_tries && eval { require Time::HiRes; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fast_tries and eval { do { require Time::HiRes; 1 } }) { } 0 0 0
Condition0%0/3
$fast_tries and eval { do { require Time::HiRes; 1 } }
!ll&&!rl&&r
000
MC/DC0%0/2
$fast_tries and eval { do { require Time::HiRes; 1 } }
$fast_tries eval { do { require Time::HiRes; 1 } }
Truth table0%0/3
A and B
A= $fast_triesB= eval { do { require Time::HiRes; 1 } }
ABresult
0X0
100
111
1876 0 $sleep_sub = \&Time::HiRes::sleep;
1877 } else {
1878 0 ▶ $sleep_sub = sub { sleep $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1879 }
1880 0 ▶ my $seconds = $try <= $fast_tries && defined &Time::HiRes::sleep ? $fast_sleep : $slow_sleep;
Branch: 1 branch
BranchTrueFalseTotal
$try <= $fast_tries && defined &Time::HiRes::sleep ? : 0 0 0
Condition0%0/3
$try <= $fast_tries and defined &Time::HiRes::sleep
!ll&&!rl&&r
000
MC/DC0%0/2
$try <= $fast_tries and defined &Time::HiRes::sleep
$try <= $fast_tries defined &Time::HiRes::sleep
Truth table0%0/3
A and B
A= $try <= $fast_triesB= defined &Time::HiRes::sleep
ABresult
0X0
100
111
1881 0 ▶ $retry_msg_code->($seconds) if $retry_msg_code;
Branch: 1 branch
BranchTrueFalseTotal
if $retry_msg_code 0 0 0
1882 0 $sleep_sub->($seconds);
1883 }
1884  
1885 0 undef;
1886 }
1887  
1888 }
1889  
1890 {
1891 47 package Doit::RPC::Client;
1892 47 our @ISA = ('Doit::RPC');
1893  
1894 sub new {
1895 1 ▶ my($class, $infh, $outfh, %options) = @_;
Subroutine new: called CC=2 SCAR=7.0
1896  
1897 1 my $debug = delete $options{debug};
1898 1 my $label = delete $options{label};
1899 1 ▶ die "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 1 1
1900  
1901 1 $outfh->autoflush(1);
1902 1 bless {
1903 infh => $infh,
1904 outfh => $outfh,
1905 label => $label,
1906 debug => $debug,
1907 }, $class;
1908 }
1909  
1910 # Call for every command on client
1911 sub call_remote {
1912 7 ▶ my($self, @args) = @_;
Subroutine call_remote: called CC=9 SCAR=24.6
1913 7 ▶ my $context = wantarray ? 'a' : 's'; # XXX more possible context (void...)?
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 5 7
1914 7 $self->send_data($context, @args);
1915 7 my($rettype, @ret) = $self->receive_data(@args);
1916 7 ▶ if (defined $rettype && $rettype eq 'e') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rettype and $rettype eq 'e') { } 2 5 7
elsif (defined $rettype and $rettype eq 'r') { } 5 0 5
Condition66%2/3
defined $rettype and $rettype eq "e"
!ll&&!rl&&r
052
MC/DC50%1/2
defined $rettype and $rettype eq "e"
defined $rettype $rettype eq "e"
Truth table66%2/3
A and B
A= defined $rettypeB= $rettype eq "e"
ABresult
0X0
100
111
1917 2 die $ret[0];
1918 ▶ } elsif (defined $rettype && $rettype eq 'r') {
Condition33%1/3
defined $rettype and $rettype eq "r"
!ll&&!rl&&r
005
MC/DC0%0/2
defined $rettype and $rettype eq "r"
defined $rettype $rettype eq "r"
Truth table33%1/3
A and B
A= defined $rettypeB= $rettype eq "r"
ABresult
0X0
100
111
1919 5 ▶ if ($context eq 'a') {
Branch: 1 branch
BranchTrueFalseTotal
if ($context eq 'a') { } 2 3 5
1920 2 return @ret;
1921 } else {
1922 3 return $ret[0];
1923 }
1924 } else {
1925 0 ▶ die "Unexpected return type " . (defined $self->{label} ? "in connection '$self->{label}' " : "") . (defined $rettype ? "'$rettype'" : "<undefined>") . " (should be 'e' or 'r')";
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'label'} ? : 0 0 0
defined $rettype ? : 0 0 0
1926 }
1927 }
1928 }
1929  
1930 {
1931 47 package Doit::RPC::Server;
1932 47 our @ISA = ('Doit::RPC');
1933  
1934 sub new {
1935 0 ▶ my($class, $runner, $sockpath, %options) = @_;
Subroutine new: not called CC=2 SCAR=17.9
1936  
1937 0 my $debug = delete $options{debug};
1938 0 my $excl = delete $options{excl};
1939 0 ▶ die "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 0 0
1940  
1941 0 bless {
1942 runner => $runner,
1943 sockpath => $sockpath,
1944 debug => $debug,
1945 excl => $excl,
1946 }, $class;
1947 }
1948  
1949 sub run {
1950 0 ▶ my($self) = @_;
Subroutine run: not called CC=8 SCAR=41.3
1951  
1952 0 require IO::Socket::UNIX;
1953 0 IO::Socket::UNIX->VERSION('1.18'); # autoflush
1954 0 IO::Socket::UNIX->import(qw(SOCK_STREAM));
1955 47 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
1956  
1957 0 my $d;
1958 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'debug'}) { } 0 0 0
1959 $d = sub ($) {
1960 0 ▶ Doit::Log::info("WORKER: $_[0]");
Subroutine __ANON__: not called CC=1 SCAR=6.9
1961 0 };
1962 } else {
1963 0 ▶ $d = sub ($) { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1964 }
1965  
1966 0 $d->("Start worker ($$)...");
1967 0 my $sockpath = $self->{sockpath};
1968 0 ▶ if (!$self->{excl} && -e $sockpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"excl"} and -e $sockpath) 0 0 0
Condition0%0/3
not $self->{"excl"} and -e $sockpath
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"excl"} and -e $sockpath
not $self->{"excl"} -e $sockpath
Truth table0%0/3
not(A) and B
A= not $self->{"excl"}B= -e $sockpath
ABresult
0X0
100
111
1969 0 $d->("unlink socket $sockpath");
1970 0 unlink $sockpath;
1971 }
1972 0 ▶ my $sock = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::UNIX"->new("Type", SOCK_STREAM(), "Local", $sockpath, "Listen", 1) or die "WORKER: Can't create socket: $!" 0 0 0
1973 Type => SOCK_STREAM(),
1974 Local => $sockpath,
1975 Listen => 1,
1976 ) or die "WORKER: Can't create socket: $!";
1977 0 $d->("socket was created");
1978  
1979 0 my $sel = IO::Select->new($sock);
1980 0 $d->("waiting for client");
1981 0 my @ready = $sel->can_read();
1982 0 ▶ die "WORKER: unexpected filehandle @ready" if $ready[0] != $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $ready[0] != $sock 0 0 0
1983 0 $d->("accept socket");
1984 0 my $fh = $sock->accept;
1985 0 $self->{infh} = $self->{outfh} = $fh;
1986 0 while () {
1987 0 $d->(" waiting for line from comm");
1988 0 my($context, @data) = $self->receive_data;
1989 0 ▶ if (!defined $context) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $context) { } 0 0 0
elsif ($data[0] =~ /^exit$/) { } 0 0 0
1990 0 $d->(" got eof");
1991 0 $fh->close;
1992 0 return;
1993 } elsif ($data[0] =~ m{^exit$}) {
1994 0 $d->(" got exit command");
1995 0 $self->send_data('r', 'bye-bye');
1996 0 $fh->close;
1997 0 return;
1998 }
1999 0 $d->(" calling method $data[0]");
2000 0 my($rettype, @ret) = $self->runner->call_wrapped_method($context, @data);
2001 0 $d->(" sending result back");
2002 0 $self->send_data($rettype, @ret);
2003 }
2004 }
2005  
2006 }
2007  
2008 {
2009 47 package Doit::RPC::SimpleServer;
2010 47 our @ISA = ('Doit::RPC');
2011
2012 sub new {
2013 1 ▶ my($class, $runner, $infh, $outfh, %options) = @_;
Subroutine new: called CC=4 SCAR=14.3
2014 1 my $debug = delete $options{debug};
2015 1 ▶ die "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 1 1
2016  
2017 1 ▶ $infh = \*STDIN if !$infh;
Branch: 1 branch
BranchTrueFalseTotal
unless $infh 0 1 1
2018 1 ▶ $outfh = \*STDOUT if !$outfh;
Branch: 1 branch
BranchTrueFalseTotal
unless $outfh 0 1 1
2019 1 $outfh->autoflush(1);
2020 1 bless {
2021 runner => $runner,
2022 infh => $infh,
2023 outfh => $outfh,
2024 debug => $debug,
2025 }, $class;
2026 }
2027  
2028 sub run {
2029 1 ▶ my $self = shift;
Subroutine run: called CC=6 SCAR=18.5
2030 1 while() {
2031 7 my($context, @data) = $self->receive_data;
2032 7 ▶ if (!defined $context) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $context) { } 0 7 7
elsif ($data[0] =~ /^exit$/) { } 1 6 7
2033 0 return;
2034 } elsif ($data[0] =~ m{^exit$}) {
2035 1 $self->send_data('r', 'bye-bye');
2036 1 return;
2037 }
2038 6 ▶ open my $oldout, ">&STDOUT" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $oldout, ">&STDOUT" or die $! 0 6 6
2039 6 if (Doit::IS_WIN) {
2040 open STDOUT, '>', 'CON:' or die $!; # XXX????
2041 } else {
2042 6 ▶ open STDOUT, '>', "/dev/stderr" or die $!; # XXX????
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", "/dev/stderr" or die $! 0 6 6
2043 }
2044 6 my($rettype, @ret) = $self->runner->call_wrapped_method($context, @data);
2045 6 ▶ open STDOUT, ">&", $oldout or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or die $! 0 6 6
2046 6 $self->send_data($rettype, @ret);
2047 }
2048 }
2049 }
2050  
2051 {
2052 47 package Doit::_AnyRPCImpl;
2053 sub call_remote {
2054 0 ▶ my($self, @args) = @_;
Subroutine call_remote: not called CC=1 SCAR=6.9
2055 0 $self->{rpc}->call_remote(@args);
2056 }
2057  
2058 47 our $AUTOLOAD;
2059 sub AUTOLOAD {
2060 0 ▶ (my $method = $AUTOLOAD) =~ s{.*::}{};
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
2061 0 my $self = shift;
2062 0 $self->call_remote($method, @_); # XXX or use goto?
2063 }
2064  
2065 sub _can_LANS {
2066 0 ▶ require POSIX;
Subroutine _can_LANS: not called CC=2 SCAR=17.9
2067 0 ▶ $^O eq 'linux' && (POSIX::uname())[2] !~ m{^([01]\.|2\.[01]\.)} # osvers >= 2.2, earlier versions did not have LANS
Branch: 1 branch
BranchTrueFalseTotal
$^O eq "linux" and not +(POSIX::uname())[2] =~ /^([01]\.|2\.[01]\.)/ 0 0 0
2068 }
2069  
2070 }
2071  
2072 {
2073 47 package Doit::_ScriptTools;
2074  
2075 sub add_components {
2076 0 ▶ my(@components) = @_;
Subroutine add_components: not called CC=1 SCAR=6.9
2077 0 q|for my $component_module (qw(| . join(" ", map { qq{$_->{module}} } @components) . q|)) { $d->add_component($component_module) } |;
2078 }
2079  
2080 sub self_require (;$) {
2081 0 ▶ my $realscript = shift;
Subroutine self_require: not called CC=3 SCAR=24.8
2082 0 ▶ if (!defined $realscript) { $realscript = $0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $realscript) 0 0 0
2083 0 ▶ if ($realscript ne '-e') { # not a oneliner
Branch: 1 branch
BranchTrueFalseTotal
if ($realscript ne '-e') { } 0 0 0
2084 0 q{$ENV{DOIT_IN_REMOTE} = 1; } .
2085 q{require "} . File::Basename::basename($realscript) . q{"; };
2086 } else {
2087 0 q{use Doit; };
2088 }
2089 }
2090 }
2091  
2092 {
2093 47 package Doit::Sudo;
2094  
2095 47 our @ISA = ('Doit::_AnyRPCImpl');
2096  
2097 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
2098  
2099 47 my $socket_count = 0;
2100  
2101 sub do_connect {
2102 0 ▶ my($class, %opts) = @_;
Subroutine do_connect: not called CC=14 SCAR=53.5
2103 0 ▶ my @sudo_opts = @{ delete $opts{sudo_opts} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"sudo_opts"} or [] 0 0 0
2104 0 my $dry_run = delete $opts{dry_run};
2105 0 my $debug = delete $opts{debug};
2106 0 ▶ my @components = @{ delete $opts{components} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"components"} or [] 0 0 0
2107 0 ▶ my $perl = delete $opts{perl} || $^X;
Condition0%0/3
delete $opts{'perl'} || $^X
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'perl'} || $^X
delete $opts{'perl'} $^X
Truth table0%0/3
A || B
A= delete $opts{'perl'}B= $^X
ABresult
1X1
011
000
2108 0 ▶ die "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
2109  
2110 0 my $self = bless { }, $class;
2111  
2112 0 require File::Basename;
2113 0 require IPC::Open2;
2114 0 require POSIX;
2115 0 require Symbol;
2116  
2117 # Socket pathname, make it possible to find out
2118 # old outdated sockets easily by including a
2119 # timestamp. Also need to maintain a $socket_count,
2120 # if the same script opens multiple sockets quickly.
2121 0 my $sock_path = "/tmp/." . join(".", "doit", "sudo", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, (++$socket_count)) . ".sock";
2122  
2123 # Make sure password has to be entered only once (if at all)
2124 # Using 'sudo --validate' would be more correct, however,
2125 # mysterious "sudo: ignoring time stamp from the future"
2126 # errors may happen every now and then. Seen on a
2127 # debian/jessie system, possibly related to
2128 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=762465
2129 {
2130 0 my @cmd = ('sudo', @sudo_opts, 'true');
2131 0 system @cmd;
2132 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
2133 # Possible cases:
2134 # - sudo is not installed
2135 # - sudo authentication is not possible or user entered wrong password
2136 # - true is not installed (hopefully this never happens on Unix systems)
2137 0 error "Command '@cmd' failed";
2138 }
2139 }
2140  
2141 # On linux use Linux Abstract Namespace Sockets ---
2142 # invisible and automatically cleaned up. See man 7 unix.
2143 0 ▶ my $LANS_PREFIX = $class->_can_LANS ? '\0' : '';
Branch: 1 branch
BranchTrueFalseTotal
$class->_can_LANS ? : 0 0 0
2144  
2145 # Run the server
2146 0 ▶ my @cmd_worker =
Branch: 3 branches
BranchTrueFalseTotal
$debug ? : 0 0 0
$LANS_PREFIX ? : 0 0 0
$dry_run ? : 0 0 0
2147 (
2148 'sudo', @sudo_opts, $perl, "-I".File::Basename::dirname(__FILE__), "-I".File::Basename::dirname($0), "-e",
2149 Doit::_ScriptTools::self_require() .
2150 q{my $d = Doit->init; } .
2151 Doit::_ScriptTools::add_components(@components) .
2152 q{Doit::RPC::Server->new($d, "} . $LANS_PREFIX . $sock_path . q{", excl => 1, debug => } . ($debug?1:0) . q{)->run();} .
2153 ($LANS_PREFIX ? '' : q<END { unlink "> . $sock_path . q<" }>), # cleanup socket file, except if Linux Abstract Namespace Sockets are used
2154 "--", ($dry_run? "--dry-run" : ())
2155 );
2156 0 my $worker_pid = fork;
2157 0 ▶ if (!defined $worker_pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $worker_pid) { } 0 0 0
elsif ($worker_pid == 0) { } 0 0 0
2158 0 die "fork failed: $!";
2159 } elsif ($worker_pid == 0) {
2160 0 ▶ warn "worker perl cmd: @cmd_worker\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
2161 0 exec @cmd_worker;
2162 0 die "Failed to run '@cmd_worker': $!";
2163 }
2164  
2165 # Run the client --- must also run under root for socket
2166 # access.
2167 0 my($in, $out);
2168 0 my @cmd_comm = ('sudo', @sudo_opts, $perl, "-I".File::Basename::dirname(__FILE__), "-MDoit", "-e",
2169 q{Doit::Comm->comm_to_sock("} . $LANS_PREFIX . $sock_path . q{", debug => shift)}, !!$debug);
2170 0 ▶ warn "comm perl cmd: @cmd_comm\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
2171 0 my $comm_pid = IPC::Open2::open2($out, $in, @cmd_comm);
2172 0 $self->{rpc} = Doit::RPC::Client->new($out, $in, label => "sudo:", debug => $debug);
2173  
2174 0 $self;
2175 }
2176  
2177 ▶ sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
2178  
2179 }
2180  
2181 {
2182 47 package Doit::SSH;
2183  
2184 47 our @ISA = ('Doit::_AnyRPCImpl');
2185  
2186 47 ▶ use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
2187  
2188 sub do_connect {
2189 6 ▶ require File::Basename;
Subroutine do_connect: called CC=36 SCAR=71.0
2190 6 require Net::OpenSSH;
2191 0 require FindBin;
2192 0 my($class, $ssh_or_host, %opts) = @_;
2193 0 my $dry_run = delete $opts{dry_run};
2194 0 ▶ my @components = @{ delete $opts{components} || [] };
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{"components"} or [] 0 0 0
2195 0 my $debug = delete $opts{debug};
2196 0 my $as = delete $opts{as};
2197 0 my $forward_agent = delete $opts{forward_agent};
2198 0 my $tty = delete $opts{tty};
2199 0 my $port = delete $opts{port};
2200 0 my $master_opts = delete $opts{master_opts};
2201 0 my $dest_os = delete $opts{dest_os};
2202 0 ▶ $dest_os = 'unix' if !defined $dest_os;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dest_os 0 0 0
2203 0 ▶ my $put_to_remote = delete $opts{put_to_remote} || 'rsync_put'; # XXX ideally this should be determined automatically
Condition0%0/2
delete $opts{'put_to_remote'} || 'rsync_put'
l!l
00
MC/DC0%0/1
delete $opts{'put_to_remote'} || 'rsync_put'
delete $opts{'put_to_remote'}
Truth table0%0/2
A
A= delete $opts{'put_to_remote'}
Aresult
11
00
2204 0 ▶ $put_to_remote =~ m{^(rsync_put|scp_put)$}
Branch: 1 branch
BranchTrueFalseTotal
$put_to_remote =~ /^(rsync_put|scp_put)$/ or &error("Valid values for put_to_remote: rsync_put or scp_put") 0 0 0
2205 or error "Valid values for put_to_remote: rsync_put or scp_put";
2206 0 ▶ my $perl = delete $opts{perl} || 'perl';
Condition0%0/2
delete $opts{'perl'} || 'perl'
l!l
00
MC/DC0%0/1
delete $opts{'perl'} || 'perl'
delete $opts{'perl'}
Truth table0%0/2
A
A= delete $opts{'perl'}
Aresult
11
00
2207 0 my $umask = delete $opts{umask};
2208 0 ▶ if (defined $umask && $umask !~ m{^\d+$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $umask and not $umask =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $umask and not $umask =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $umask and not $umask =~ /^\d+$/
defined $umask not $umask =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $umaskB= not $umask =~ /^\d+$/
ABresult
0X0
100
111
2209 0 error "The umask '$umask' does not look correct, it should be a (possibly octal) number";
2210 }
2211 0 my $bootstrap = delete $opts{bootstrap};
2212 0 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
2213  
2214 0 my $self = bless { debug => $debug }, $class;
2215 0 ▶ my %ssh_run_opts = (
Branch: 2 branches
BranchTrueFalseTotal
$forward_agent ? : 0 0 0
$tty ? : 0 0 0
2216 ($forward_agent ? (forward_agent => $forward_agent) : ()),
2217 ($tty ? (tty => $tty) : ()),
2218 );
2219 0 ▶ my %ssh_new_opts = (
Branch: 2 branches
BranchTrueFalseTotal
$forward_agent ? : 0 0 0
$master_opts ? : 0 0 0
2220 ($forward_agent ? (forward_agent => $forward_agent) : ()),
2221 ($master_opts ? (master_opts => $master_opts) : ()),
2222 );
2223  
2224 0 my($host, $ssh);
2225 0 ▶ if (UNIVERSAL::isa($ssh_or_host, 'Net::OpenSSH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($ssh_or_host, 'Net::OpenSSH')) { } 0 0 0
2226 0 $ssh = $ssh_or_host;
2227 0 $host = $ssh->get_host; # XXX what about username/port/...?
2228 } else {
2229 0 $host = $ssh_or_host;
2230 0 $ssh = Net::OpenSSH->new($host, %ssh_new_opts);
2231 0 ▶ $ssh->error
Branch: 1 branch
BranchTrueFalseTotal
$ssh->error and &error("Connection error to ${host}: " . $ssh->error) 0 0 0
2232 and error "Connection error to $host: " . $ssh->error;
2233 }
2234 0 $self->{ssh} = $ssh;
2235  
2236 0 ▶ if (($bootstrap||'') eq 'perl') {
Branch: 1 branch
BranchTrueFalseTotal
if (($bootstrap || "") eq "perl") 0 0 0
Condition0%0/2
$bootstrap || ''
l!l
00
MC/DC0%0/1
$bootstrap || ''
$bootstrap
Truth table0%0/2
A
A= $bootstrap
Aresult
11
00
2237 0 require Doit::Bootstrap;
2238 0 Doit::Bootstrap::_bootstrap_perl($self, dry_run => $dry_run);
2239 }
2240  
2241 {
2242 0 my $remote_cmd;
2243 0 ▶ if ($dest_os eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_os eq 'MSWin32') { } 0 0 0
2244 0 $remote_cmd = 'if not exist .doit\lib\ mkdir .doit\lib';
2245 } else {
2246 0 $remote_cmd = "[ ! -d .doit/lib ] && mkdir -p .doit/lib";
2247 }
2248 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
2249 0 info "Running '$remote_cmd' on remote";
2250 }
2251 0 $ssh->system(\%ssh_run_opts, $remote_cmd);
2252 }
2253 0 ▶ if ($FindBin::RealScript ne '-e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($FindBin::RealScript ne "-e") 0 0 0
2254 47 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
2255 0 $ssh->$put_to_remote({verbose => $debug}, "$FindBin::RealBin/$FindBin::RealScript", ".doit/"); # XXX verbose?
2256 }
2257 0 $ssh->$put_to_remote({verbose => $debug}, __FILE__, ".doit/lib/");
2258 {
2259 0 my %seen_dir;
2260 0 ▶ for my $component (
Branch: 1 branch
BranchTrueFalseTotal
$dest_os ne 'MSWin32' ? : 0 0 0
2261 @components,
2262 ( # add additional RPC components
2263 $dest_os ne 'MSWin32' ? () :
2264 do {
2265 0 (my $srcpath = __FILE__) =~ s{\.pm}{/WinRPC.pm};
2266 0 {relpath => "Doit/WinRPC.pm", path => $srcpath},
2267 }
2268 )
2269 ) {
2270 0 my $from = $component->{path};
2271 0 my $to = $component->{relpath};
2272 0 my $full_target = ".doit/lib/$to";
2273 0 my $target_dir = File::Basename::dirname($full_target);
2274 0 ▶ if (!$seen_dir{$target_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_dir{$target_dir}) 0 0 0
2275 0 my $remote_cmd;
2276 0 ▶ if ($dest_os eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_os eq 'MSWin32') { } 0 0 0
2277 0 (my $win_target_dir = $target_dir) =~ s{/}{\\}g;
2278 0 $remote_cmd = "if not exist $win_target_dir mkdir $win_target_dir"; # XXX is this equivalent to mkdir -p?
2279 } else {
2280 0 $remote_cmd = "[ ! -d $target_dir ] && mkdir -p $target_dir";
2281 }
2282 0 $ssh->system(\%ssh_run_opts, $remote_cmd);
2283 0 $seen_dir{$target_dir} = 1;
2284 }
2285 0 $ssh->$put_to_remote({verbose => $debug}, $from, $full_target);
2286 }
2287 }
2288  
2289 my $sock_path = (
2290 $dest_os eq 'MSWin32'
2291 ? join("-", "doit", "ssh", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), int(rand(99999999)))
2292 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$dest_os eq 'MSWin32' ? : 0 0 0
2293 0 require POSIX;
2294 0 "/tmp/." . join(".", "doit", "ssh", POSIX::strftime("%Y%m%d_%H%M%S", gmtime), $<, $$, int(rand(99999999))) . ".sock";
2295 }
2296 );
2297  
2298 0 my @cmd;
2299 0 ▶ if (defined $as) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $as) 0 0 0
2300 0 ▶ if ($as eq 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as eq 'root') { } 0 0 0
2301 0 @cmd = ('sudo');
2302 } else {
2303 0 @cmd = ('sudo', '-u', $as);
2304 }
2305 } # XXX add ssh option -t? for password input?
2306  
2307 0 my @cmd_worker;
2308 0 ▶ if ($dest_os eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_os eq 'MSWin32') { } 0 0 0
2309 0 ▶ @cmd_worker =
Branch: 2 branches
BranchTrueFalseTotal
$debug ? : 0 0 0
$dry_run ? : 0 0 0
2310 (
2311 # @cmd not used here (no sudo)
2312 $perl, "-I.doit", "-I.doit\\lib", "-e",
2313 Doit::_ScriptTools::self_require($FindBin::RealScript) .
2314 q{use Doit::WinRPC; } .
2315 q{my $d = Doit->init; } .
2316 Doit::_ScriptTools::add_components(@components) .
2317 # XXX server cleanup? on signals? on END?
2318 q{Doit::WinRPC::Server->new($d, "} . $sock_path . q{", debug => } . ($debug?1:0).q{)->run();},
2319 "--", ($dry_run? "--dry-run" : ())
2320 );
2321 0 @cmd_worker = Doit::Win32Util::win32_quote_list(@cmd_worker);
2322 } else {
2323 0 ▶ @cmd_worker =
Branch: 3 branches
BranchTrueFalseTotal
defined $umask ? : 0 0 0
$debug ? : 0 0 0
$dry_run ? : 0 0 0
2324 (
2325 @cmd, $perl, "-I.doit", "-I.doit/lib", "-e",
2326 (defined $umask ? qq{umask $umask; } : q{}) .
2327 Doit::_ScriptTools::self_require($FindBin::RealScript) .
2328 q{my $d = Doit->init; } .
2329 Doit::_ScriptTools::add_components(@components) .
2330 q<sub _server_cleanup { unlink "> . $sock_path . q<" }> .
2331 q<$SIG{PIPE} = \&_server_cleanup; > .
2332 q<END { _server_cleanup() } > .
2333 q{Doit::RPC::Server->new($d, "} . $sock_path . q{", excl => 1, debug => } . ($debug?1:0).q{)->run();},
2334 "--", ($dry_run? "--dry-run" : ())
2335 );
2336 }
2337 0 ▶ warn "remote perl cmd: @cmd_worker\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
2338 0 my $worker_pid = $ssh->spawn(\%ssh_run_opts, @cmd_worker); # XXX what to do with worker pid?
2339 0 $self->{worker_pid} = $worker_pid;
2340  
2341 0 my @cmd_comm;
2342 0 ▶ if ($dest_os eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_os eq 'MSWin32') { } 0 0 0
2343 0 @cmd_comm =
2344 ($perl, "-I.doit\\lib", "-MDoit", "-MDoit::WinRPC", "-e",
2345 q{Doit::WinRPC::Comm->new("} . $sock_path . q{", debug => shift)->run},
2346 !!$debug,
2347 );
2348 0 @cmd_comm = Doit::Win32Util::win32_quote_list(@cmd_comm);
2349 } else {
2350 0 @cmd_comm =
2351 (
2352 @cmd, $perl, "-I.doit/lib", "-MDoit", "-e",
2353 q{Doit::Comm->comm_to_sock("} . $sock_path . q{", debug => shift);},
2354 !!$debug,
2355 );
2356 }
2357 0 ▶ warn "comm perl cmd: @cmd_comm\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
2358 0 my($out, $in, $comm_pid) = $ssh->open2(@cmd_comm);
2359 0 $self->{comm_pid} = $comm_pid;
2360 0 $self->{rpc} = Doit::RPC::Client->new($in, $out, label => "ssh:$host", debug => $debug);
2361  
2362 0 $self;
2363 }
2364  
2365 0 ▶ sub ssh { $_[0]->{ssh} }
Subroutine ssh: not called CC=1 SCAR=6.9
2366  
2367 sub DESTROY {
2368 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=4 SCAR=30.0
2369 0 local $?; # XXX Net::OpenSSH::_waitpid sets $?=0
2370 0 ▶ if ($self->{ssh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssh"}) 0 0 0
2371 0 ▶ $self->{ssh}->disconnect if $self->{ssh}->can('disconnect');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ssh"}->can("disconnect") 0 0 0
2372 0 delete $self->{ssh};
2373 }
2374 0 ▶ if ($self->{rpc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rpc"}) 0 0 0
2375 0 $self->{rpc}->_reap_process($self->{comm_pid});
2376 0 $self->{rpc}->_reap_process($self->{worker_pid});
2377 }
2378 }
2379  
2380 }
2381  
2382 {
2383 47 package Doit::Comm;
2384  
2385 sub comm_to_sock {
2386 0 ▶ my(undef, $peer, %options) = @_;
Subroutine comm_to_sock: not called CC=6 SCAR=37.4
2387 0 ▶ die "Please specify path to unix domain socket" if !defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
2388 0 my $debug = delete $options{debug};
2389 0 ▶ die "Unhandled options: " . join(" ", %options) if %options;
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 0 0
2390  
2391 0 my $infh = \*STDIN;
2392 0 my $outfh = \*STDOUT;
2393  
2394 0 require IO::Socket::UNIX;
2395 0 IO::Socket::UNIX->VERSION('1.18'); # autoflush
2396 0 IO::Socket::UNIX->import(qw(SOCK_STREAM));
2397  
2398 0 my $d;
2399 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) { } 0 0 0
2400 $d = sub ($) {
2401 0 ▶ Doit::Log::info("COMM: $_[0]");
Subroutine __ANON__: not called CC=1 SCAR=6.9
2402 0 };
2403 } else {
2404 0 ▶ $d = sub ($) { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2405 }
2406  
2407 0 $d->("Start communication process (pid $$)...");
2408  
2409 0 my $tries = 20;
2410 0 my $sock_err;
2411 my $sock = Doit::RPC::gentle_retry(
2412 code => sub {
2413 0 ▶ my(%opts) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
2414 0 my $sock = IO::Socket::UNIX->new(
2415 Type => SOCK_STREAM(),
2416 Peer => $peer,
2417 );
2418 0 ▶ return $sock if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 0 0 0
2419 0 ${$opts{fail_info_ref}} = "(peer=$peer, errno=$!)";
2420 0 undef;
2421 },
2422 retry_msg_code => sub {
2423 0 ▶ my($seconds) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2424 0 $d->("can't connect, sleep for $seconds seconds");
2425 },
2426 0 fail_info_ref => \$sock_err,
2427 );
2428 0 ▶ if (!$sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
2429 0 die "COMM: Can't connect to socket (after $tries retries) $sock_err";
2430 }
2431 0 $d->("socket to worker was created");
2432  
2433 my $get_and_send = sub ($$$$) {
2434 0 ▶ my($infh, $outfh, $inname, $outname) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
2435  
2436 0 my $length_buf;
2437 0 ▶ read $infh, $length_buf, 4 or die "COMM: reading data from $inname failed (getting length): $!";
Branch: 1 branch
BranchTrueFalseTotal
read $infh, $length_buf, 4 or die "COMM: reading data from $inname failed (getting length): $!" 0 0 0
2438 0 my $length = unpack("N", $length_buf);
2439 0 $d->("starting getting data from $inname, length is $length");
2440 0 my $buf = '';
2441 0 while (1) {
2442 0 my $got = read($infh, $buf, $length, length($buf));
2443 0 ▶ last if $got == $length;
Branch: 1 branch
BranchTrueFalseTotal
if $got == $length 0 0 0
2444 0 ▶ die "COMM: Unexpected error $got > $length" if $got > $length;
Branch: 1 branch
BranchTrueFalseTotal
if $got > $length 0 0 0
2445 0 $length -= $got;
2446 }
2447 0 $d->("finished reading data from $inname");
2448  
2449 0 print $outfh $length_buf;
2450 0 print $outfh $buf;
2451 0 $d->("finished sending data to $outname");
2452 0 };
2453  
2454 0 $outfh->autoflush(1);
2455 0 $d->("about to enter loop");
2456 0 while () {
2457 0 ▶ $d->("seen eof from local"), last if eof($infh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $infh 0 0 0
2458 0 $get_and_send->($infh, $sock, "local", "worker");
2459 0 $get_and_send->($sock, $outfh, "worker", "local");
2460 }
2461 0 $d->("exited loop");
2462 }
2463  
2464 }
2465  
2466 47 1;
2467  
2468 __END__
« Build.PL ↑ Summary blib/lib/Doit/User.pm »