DBIx/QuickDB/Util.pm

filter: Statementstmt 83.7% 154 / 184 Branchbran 52.6% 60 / 114 Conditioncond 36.3% 20 / 55 MC/DCmcdc 12.5% 4 / 32 Subroutinesub 88.8% 24 / 27 Podpod 0.0% 0 / 6 totaltotal 62.6% 262 / 418 CC 83 SCAR 26.5
CC
83
Cov
66%
strip_hash_defaults
41.9
__ANON__
30.0
_dbi_name_matches_dir
23.3
CRAP 14.2
« Test2/Tools/QuickDB.pm ↑ Summary DBIx/QuickDB/Driver/SQLite.pm »
1 package DBIx::QuickDB::Util;
2 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 59 our $VERSION = '0.000065';
6  
7 59 ▶use Errno qw/EEXIST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 59 ▶use File::Path qw/remove_tree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 59 ▶use IPC::Cmd qw/can_run/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 59 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 59 ▶use Time::HiRes qw/sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 59 ▶use Importer Importer => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 59 our @EXPORT_OK = qw/clone_dir strip_hash_defaults env_timeout remove_tree_robust remove_tree_or_quarantine disconnect_dbi_handles/;
16  
17 59 my @DEFERRED_REMOVE;
18  
19 # A quarantine can outlive the immediate cleanup attempt while Windows releases
20 # a delete-pending file. Retry earlier quarantines during later cleanups and once
21 # more after normal END blocks have released application/test resources. This is
22 # best-effort and must not alter the exit status or turn successful teardown into
23 # a global-destruction exception.
24 49 ▶sub _retry_deferred_remove {
Subroutine _retry_deferred_remove: called CC=2 SCAR=7.0
25 @DEFERRED_REMOVE = grep {
26 49 ▶ -d $_ && !remove_tree_robust($_)
Condition66%2/3
-d $_ && !remove_tree_robust($_)
!ll&&!rl&&r
310
MC/DC0%0/2
-d $_ && !remove_tree_robust($_)
-d $_ !remove_tree_robust($_)
Truth table66%2/3
A && not(B)
A= -d $_B= !remove_tree_robust($_)
ABresult
0X0
100
111
27 } @DEFERRED_REMOVE;
28 49 return;
29 }
30  
31 45 ▶END {
Subroutine END: called CC=1 SCAR=0.0
32 45 local $?;
33 45 _retry_deferred_remove();
34 }
35  
36 # Return true when a DBI handle Name contains a database directory. DBI drivers
37 # preserve the spelling used in their DSN, while File::Temp/File::Spec can hand
38 # the driver object a differently-spelled version of the same Windows path
39 # (backslashes vs forward slashes, and different case). A raw index() therefore
40 # misses the live SQLite handle precisely on the platform where that handle
41 # prevents unlinking the database file.
42 #
43 # $win32 is private test plumbing. Production callers omit it and use $^O.
44 2 ▶sub _dbi_name_matches_dir {
Subroutine _dbi_name_matches_dir: called CC=16 SCAR=31.5
45 2 my ($name, $dir, $win32) = @_;
46 2 ▶ return 0 unless defined($name) && length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 2 2
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
002
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
47 2 ▶ return 0 unless defined($dir) && length($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 2 2
Condition33%1/3
defined $dir and length $dir
!ll&&!rl&&r
002
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table33%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
48  
49 2 ▶ $win32 = $^O eq 'MSWin32' unless defined $win32;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $win32 0 2 2
50 2 ▶ if ($win32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($win32) 2 0 2
51 2 tr{\\}{/} for $name, $dir;
52 2 $name = lc $name;
53 2 $dir = lc $dir;
54 }
55  
56 # Avoid treating a sibling such as C:\db-old as a child of C:\db. In a
57 # DBI Name the directory is either the whole value, follows a DSN separator,
58 # and is followed by a path/DSN separator or the end of the string.
59 2 ▶ $dir =~ s{/$}{} unless $dir =~ m{^[a-z]:/$}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[^[a-z]:/$]i 2 0 2
60 2 my $at = -1;
61 2 ▶ while (($at = index($name, $dir, $at + 1)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($at = index($name, $dir, $at + 1)) >= 0) 2 1 3
62 2 ▶ my $before = $at ? substr($name, $at - 1, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$at ? : 2 0 2
63 2 my $after_at = $at + length($dir);
64 2 ▶ my $after = $after_at < length($name) ? substr($name, $after_at, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$after_at < length $name ? : 2 0 2
65 2 ▶ next unless !$at || $before =~ /[=;\s]/;
Branch: 1 branch
BranchTrueFalseTotal
unless not $at or $before =~ /[=;\s]/ 0 2 2
Condition33%1/3
not $at or $before =~ /[=;\s]/
l!l&&r!l&&!r
020
MC/DC0%0/2
not $at or $before =~ /[=;\s]/
not $at $before =~ /[=;\s]/
Truth table33%1/3
not(A) or B
A= not $atB= $before =~ /[=;\s]/
ABresult
1X1
011
000
66 2 ▶ return 1 if $after eq '' || $after =~ m{[/;?\s]};
Branch: 1 branch
BranchTrueFalseTotal
if $after eq "" or $after =~ m[[/;?\s]] 1 1 2
Condition66%2/3
$after eq "" or $after =~ m[[/;?\s]]
l!l&&r!l&&!r
011
MC/DC50%1/2
$after eq "" or $after =~ m[[/;?\s]]
$after eq "" $after =~ m[[/;?\s]]
Truth table66%2/3
A or B
A= $after eq ""B= $after =~ m[[/;?\s]]
ABresult
1X1
011
000
67 }
68  
69 1 return 0;
70 }
71  
72 # Disconnect all DBI database handles in this process whose DSN points into the
73 # supplied directory. Collect first and disconnect second: mutating DBI's child
74 # handle tree from inside visit_handles() can otherwise skip a sibling handle.
75 # DBI is optional and may not have been loaded, so this remains a no-op in that
76 # case and during global destruction when DBI's own handle tree is going away.
77 69 ▶sub disconnect_dbi_handles {
Subroutine disconnect_dbi_handles: called CC=5 SCAR=31.3
Pod undocumented
78 69 my ($dir) = @_;
79  
80 69 ▶ return 0 unless $INC{'DBI.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DBI.pm"} 69 0 69
81 0 ▶ return 0 if defined(${^GLOBAL_PHASE}) && ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if defined ${^GLOBAL_PHASE} and ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
Condition0%0/3
defined ${^GLOBAL_PHASE} and ${^GLOBAL_PHASE} eq "DESTRUCT"
!ll&&!rl&&r
000
MC/DC0%0/2
defined ${^GLOBAL_PHASE} and ${^GLOBAL_PHASE} eq "DESTRUCT"
defined ${^GLOBAL_PHASE} ${^GLOBAL_PHASE} eq "DESTRUCT"
Truth table0%0/3
A and B
A= defined ${^GLOBAL_PHASE}B= ${^GLOBAL_PHASE} eq "DESTRUCT"
ABresult
0X0
100
111
82  
83 0 my @handles;
84 DBI->visit_handles(
85 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
86 0 my ($handle) = @_;
87 push @handles => $handle
88 if $handle->{Type} && $handle->{Type} eq 'db' && $handle->{Active}
89 0 ▶ && _dbi_name_matches_dir($handle->{Name}, $dir);
Branch: 1 branch
BranchTrueFalseTotal
if $handle->{"Type"} and $handle->{"Type"} eq "db" and $handle->{"Active"} and _dbi_name_matches_dir($handle->{"Name"}, $dir) 0 0 0
Condition0%0/9
$handle->{"Type"} and $handle->{"Type"} eq "db" and $handle->{"Active"} and _dbi_name_matches_dir($handle->{"Name"}, $dir)
!ll&&!rl&&r
000
$handle->{"Type"} and $handle->{"Type"} eq "db" and $handle->{"Active"}
!ll&&!rl&&r
000
$handle->{"Type"} and $handle->{"Type"} eq "db"
!ll&&!rl&&r
000
MC/DC0%0/4
$handle->{"Type"} and $handle->{"Type"} eq "db" and $handle->{"Active"} and _dbi_name_matches_dir($handle->{"Name"}, $dir)
$handle->{"Type"} $handle->{"Type"} eq "db" $handle->{"Active"} _dbi_name_matches_dir($handle->{"Name"}, $dir)
Truth table0%0/5
A and B and C and D
A= $handle->{"Type"}B= $handle->{"Type"} eq "db"C= $handle->{"Active"}D= _dbi_name_matches_dir($handle->{"Name"}, $dir)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
90 0 return 1;
91 }
92 0 );
93  
94 0 $_->disconnect for @handles;
95 0 return scalar @handles;
96 }
97  
98 # Best-effort recursive removal that also copes with Windows. On MSWin32 a plain
99 # remove_tree can leave the directory non-empty -- the OS releases file handles
100 # asynchronously (delete-pending state; an antivirus/indexer may briefly re-open
101 # a just-closed file such as a SQLite data file), so an immediate rmdir of the
102 # enclosing directory gets ENOTEMPTY. Retry a few times with a short sleep to let
103 # the lock clear. On Unix a single pass is enough, so it behaves exactly like a
104 # plain remove_tree there (no added latency).
105 #
106 # Removal is treated as idempotent best-effort: errors are collected (never
107 # thrown per-file) and File::Path's own hard-die -- "cannot chdir to .. from DIR
108 # ... aborting", which happens when the tree mutates underneath us because the
109 # watcher daemon is deleting the same directory concurrently -- is swallowed by
110 # an eval. Whoever wins the race, the directory ends up gone. Returns true if the
111 # directory is gone afterward, false if it stubbornly survived every attempt.
112 #
113 # $opts is an optional File::Path::remove_tree options hashref (defaults to
114 # {safe => 1}); an 'error' key is always supplied internally and overrides any
115 # caller-provided one.
116 8 ▶sub remove_tree_robust {
Subroutine remove_tree_robust: called CC=7 SCAR=23.0
Pod undocumented
117 8 my ($dir, $opts) = @_;
118 8 ▶ $opts ||= {safe => 1};
Condition50%1/2
$opts ||= {"safe", 1}
l!l
08
MC/DC0%0/1
$opts ||= {"safe", 1}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
119  
120 8 ▶ return 1 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 8 8
121  
122 8 ▶ my $tries = $^O eq 'MSWin32' ? 5 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 8 8
123  
124 8 for my $try (1 .. $tries) {
125 8 my $err = [];
126 8 eval { remove_tree($dir, {%$opts, error => \$err}); 1 };
127 8 ▶ return 1 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 8 0 8
128 0 ▶ last if $try == $tries;
Branch: 1 branch
BranchTrueFalseTotal
if $try == $tries 0 0 0
129 0 sleep 0.2;
130 }
131  
132 0 return 0;
133 }
134  
135 # Keep filesystem operations and retry pauses behind private functions so the
136 # transient-failure sequence can be exercised deterministically in tests.
137 3 ▶sub _rename_tree {
Subroutine _rename_tree: called CC=1 SCAR=0.0
138 3 return rename($_[0], $_[1]);
139 }
140  
141 0 ▶sub _quarantine_retry_pause {
Subroutine _quarantine_retry_pause: not called CC=2 SCAR=17.9
142 0 ▶ sleep 0.2 if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
143 0 return;
144 }
145  
146 # Remove a disposable tree. If deletion still fails after the platform-aware
147 # retries above, move it away from its canonical name so callers never rebuild
148 # or continue using a partially deleted database. The quarantine remains on the
149 # same volume and is retried by later cleanups and the END block above; Pool
150 # sweeps may reclaim it sooner. Returns an empty string for direct removal, the
151 # quarantine path after a successful rename, and undef only when neither
152 # operation cleared the original path.
153 8 ▶sub remove_tree_or_quarantine {
Subroutine remove_tree_or_quarantine: called CC=11 SCAR=24.2
Pod undocumented
154 8 my ($dir, $opts) = @_;
155  
156 8 ▶ _retry_deferred_remove() if @DEFERRED_REMOVE;
Branch: 1 branch
BranchTrueFalseTotal
if @DEFERRED_REMOVE 4 4 8
157  
158 8 ▶ return '' unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 8 8
159 8 ▶ return '' if remove_tree_robust($dir, $opts);
Branch: 1 branch
BranchTrueFalseTotal
if remove_tree_robust($dir, $opts) 3 5 8
160  
161 5 my $error;
162 5 my $max_attempts = 10;
163 5 for my $attempt (1 .. $max_attempts) {
164 16 ▶ return '' unless -d $dir; # A concurrent watcher won the cleanup race.
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 16 16
165  
166 16 my $stale = join '-' => "$dir.STALE", $$, CORE::time(), $attempt;
167 16 ▶ next if -e $stale;
Branch: 1 branch
BranchTrueFalseTotal
if -e $stale 0 16 16
168  
169 16 ▶ if (_rename_tree($dir, $stale)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_rename_tree($dir, $stale)) 4 12 16
170 4 push @DEFERRED_REMOVE => $stale;
171 4 return $stale;
172 }
173  
174 12 $error = $!;
175 12 ▶ _quarantine_retry_pause() if $attempt < $max_attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt < $max_attempts 11 1 12
176 }
177  
178 1 ▶ return '' unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
179  
180 1 ▶ $error = EEXIST unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 1 1
181 1 $! = $error;
182 1 return undef;
183 }
184  
185 # Read a positive-integer timeout (in seconds) from an environment variable,
186 # falling back to $default when it is unset or not a positive integer. Used to
187 # make the server start/stop timeouts generous-but-tunable so slow hosts (e.g.
188 # CPAN smoke boxes) do not spuriously time out.
189 51 ▶sub env_timeout {
Subroutine env_timeout: called CC=4 SCAR=14.0
Pod undocumented
190 51 my ($name, $default) = @_;
191 51 my $val = $ENV{$name};
192 51 ▶ return $val if defined($val) && $val =~ /^\d+$/ && $val > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and $val =~ /^\d+$/ and $val > 0 49 2 51
Condition66%4/6
defined $val and $val =~ /^\d+$/ and $val > 0
!ll&&!rl&&r
2049
defined $val and $val =~ /^\d+$/
!ll&&!rl&&r
2049
MC/DC33%1/3
defined $val and $val =~ /^\d+$/ and $val > 0
defined $val $val =~ /^\d+$/ $val > 0
Truth table50%2/4
A and B and C
A= defined $valB= $val =~ /^\d+$/C= $val > 0
ABCresult
0XX0
10X0
1100
1111
193 2 return $default;
194 }
195  
196 59 my ($RSYNC, $CP);
197  
198 59 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
199 59 local $@;
200 59 $RSYNC = can_run('rsync');
201 59 $CP = can_run('cp');
202 }
203  
204 # clone_dir($src, $dest, %params) -- copy a data dir, dying on failure with the
205 # decoded exit status, both paths and the tool's stderr. %params: checksum,
206 # verbose. The rsync backend retries a partial transfer and warns when it does.
207 6 ▶sub clone_dir {
Subroutine clone_dir: called CC=3 SCAR=11.7
Pod undocumented
208 6 ▶ return _clone_dir_rsync(@_) if $RSYNC;
Branch: 1 branch
BranchTrueFalseTotal
if $RSYNC 5 1 6
209 1 ▶ return _clone_dir_cp(@_) if $CP;
Branch: 1 branch
BranchTrueFalseTotal
if $CP 1 0 1
210 0 return _clone_dir_fcr(@_);
211 }
212  
213 # Decode a wait status into an actionable message. $bang is passed in because
214 # the filehandle ops between system() and this call would have clobbered $!.
215 3 ▶sub _copy_error {
Subroutine _copy_error: called CC=7 SCAR=26.4
216 3 my ($cmd, $status, $src, $dest, $stderr, $bang) = @_;
217  
218 3 my $msg = "$cmd failed copying '$src' -> '$dest': ";
219  
220 3 ▶ if ($status == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == -1) { } 0 3 3
elsif (my $sig = $status & 127) { } 0 3 3
221 0 ▶ $msg .= "could not execute (" . (defined($bang) ? $bang : $!) . ")";
Branch: 1 branch
BranchTrueFalseTotal
defined $bang ? : 0 0 0
222 }
223 elsif (my $sig = $status & 127) {
224 0 ▶ $msg .= "terminated by signal $sig" . (($status & 128) ? " (core dumped)" : "");
Branch: 1 branch
BranchTrueFalseTotal
$status & 128 ? : 0 0 0
225 }
226 else {
227 3 $msg .= "exited " . ($status >> 8);
228 }
229  
230 3 ▶ $msg .= "\n=== $cmd stderr ===\n$stderr" if defined($stderr) && length($stderr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr and length $stderr 3 0 3
Condition33%1/3
defined $stderr and length $stderr
!ll&&!rl&&r
003
MC/DC0%0/2
defined $stderr and length $stderr
defined $stderr length $stderr
Truth table33%1/3
A and B
A= defined $stderrB= length $stderr
ABresult
0X0
100
111
231  
232 3 return $msg;
233 }
234  
235 # Run $cmd with its stderr captured to a scratch file, returning ($status,
236 # $stderr, $errno). Only stderr is redirected: rsync writes -vP progress to
237 # STDOUT, so verbose runs still stream live exactly as before.
238 11 ▶sub _run_captured {
Subroutine _run_captured: called CC=8 SCAR=21.0
239 11 my (@cmd) = @_;
240  
241 11 require File::Temp;
242  
243 # QDB_TMPDIR because File::Spec->tmpdir falls back to the CURRENT DIRECTORY
244 # when /tmp is unwritable. UNLINK is only a backstop -- File::Temp keeps the
245 # handle registered, so a leak here would hold the fd until process exit;
246 # every path below unlinks explicitly.
247 my ($fh, $file) = File::Temp::tempfile(
248 "qdb-copy-err-$$-XXXXXXXX",
249 11 ▶ $ENV{QDB_TMPDIR} ? (DIR => $ENV{QDB_TMPDIR}) : (TMPDIR => 1),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'QDB_TMPDIR'} ? : 0 11 11
250 UNLINK => 1,
251 );
252  
253 # Best effort: a caller that closed STDERR (a daemonizer, say) makes this
254 # dup fail, and it could copy fine before capturing existed. Run uncaptured
255 # rather than failing the copy over diagnostics.
256 11 my $save;
257 11 ▶ unless (open($save, '>&', \*STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $save, ">&", \*STDERR) 1 10 11
258 1 close($fh);
259 1 unlink($file);
260  
261 1 system(@cmd);
262 1 my ($st, $bg) = ($?, $!);
263  
264 1 return ($st, '', $bg);
265 }
266  
267 # Restore STDERR on every path: reopening it closes it first, so bailing out
268 # mid-way would leave the process with no STDERR and free fd 2 for the next
269 # open(). Redirect through File::Temp's handle rather than reopening the
270 # path by name, which is the symlink race File::Temp exists to avoid.
271 10 my ($status, $bang);
272 10 my $ok = eval {
273 10 ▶ open(STDERR, '>&', $fh) or die "Could not redirect STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $fh or die "Could not redirect STDERR: $!" 10 0 10
274  
275 10 system(@cmd);
276 10 ($status, $bang) = ($?, $!); # before anything can clobber them
277  
278 10 1;
279 };
280 10 my $err = $@;
281  
282 10 ▶ open(STDERR, '>&', $save) or warn "Could not restore STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $save or warn "Could not restore STDERR: $!" 10 0 10
283 10 close($save);
284  
285 # Drain and clean up before any rethrow: an escaping exception would leave
286 # $fh registered with File::Temp, holding the fd until process exit. Rewind
287 # first, since the dup shared its offset with the child's writes.
288 10 my $stderr = '';
289 10 ▶ if (seek($fh, 0, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (seek $fh, 0, 0) 10 0 10
290 10 local $/;
291 10 ▶ $stderr = <$fh> // '';
Condition50%1/2
readline $fh // ""
l!l
100
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
292 }
293 10 close($fh);
294 10 unlink($file);
295  
296 10 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 10 10
297  
298 10 return ($status, $stderr, $bang);
299 }
300  
301 # Attempts for rsync exit 23, "partial transfer due to error". Exit 24
302 # ("vanished source files") is not retried -- the source is gone.
303 59 my $RSYNC_RETRIES = 3;
304  
305 5 ▶sub _clone_dir_rsync {
Subroutine _clone_dir_rsync: called CC=12 SCAR=26.4
306 5 my ($src, $dest, %params) = @_;
307  
308 my @cmd = (
309 $RSYNC, '-a', '--delete', '--exclude' => '.nfs*',
310 $params{checksum} ? ('-c') : (),
311 5 ▶ $params{verbose} ? ('-vP') : (),
Branch: 2 branches
BranchTrueFalseTotal
$params{'checksum'} ? : 0 5 5
$params{'verbose'} ? : 0 5 5
312 "$src/", $dest,
313 );
314  
315 5 for my $try (1 .. $RSYNC_RETRIES) {
316 8 my ($status, $stderr, $bang) = _run_captured(@cmd);
317  
318 8 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 2 6 8
319 2 ▶ print STDERR $stderr if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 2 0 2
320 2 return;
321 }
322  
323 6 ▶ my $code = ($status == -1 || ($status & 127)) ? undef : ($status >> 8);
Branch: 1 branch
BranchTrueFalseTotal
$status == -1 || $status & 127 ? : 0 6 6
Condition33%1/3
$status == -1 or $status & 127
l!l&&r!l&&!r
006
MC/DC0%0/2
$status == -1 or $status & 127
$status == -1 $status & 127
Truth table33%1/3
A or B
A= $status == -1B= $status & 127
ABresult
1X1
011
000
324  
325 6 ▶ die _copy_error($RSYNC, $status, $src, $dest, $stderr, $bang)
Branch: 1 branch
BranchTrueFalseTotal
if not defined $code or $code != 23 or $try == $RSYNC_RETRIES 3 3 6
Condition83%5/6
not defined $code or $code != 23 or $try == $RSYNC_RETRIES
l!l&&r!l&&!r
213
not defined $code or $code != 23
l!l&&r!l&&!r
024
MC/DC66%2/3
not defined $code or $code != 23 or $try == $RSYNC_RETRIES
not defined $code $code != 23 $try == $RSYNC_RETRIES
Truth table75%3/4
not(A) or B or C
A= not defined $codeB= $code != 23C= $try == $RSYNC_RETRIES
ABCresult
1XX1
01X1
0011
0000
326 if !defined($code) || $code != 23 || $try == $RSYNC_RETRIES;
327  
328 # A retry that succeeds leaves this warning as the only record that a
329 # partial transfer happened at all.
330 3 my $msg = "$RSYNC exited 23 (partial transfer) copying '$src' -> '$dest'"
331 . " on attempt $try of $RSYNC_RETRIES, retrying";
332 3 ▶ $msg .= "\n=== $RSYNC stderr ===\n$stderr" if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 3 0 3
333 3 warn "$msg\n";
334  
335 3 sleep(0.25 * $try);
336 }
337  
338 0 return;
339 }
340  
341 3 ▶sub _clone_dir_cp {
Subroutine _clone_dir_cp: called CC=5 SCAR=16.5
342 3 my ($src, $dest, %params) = @_;
343 3 my $err;
344 3 ▶ remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest 2 1 3
345  
346 3 ▶ my ($status, $stderr, $bang) = _run_captured($CP, '-a', $params{verbose} ? ('-v') : (), "$src/.", $dest);
Branch: 1 branch
BranchTrueFalseTotal
$params{'verbose'} ? : 0 3 3
347 3 ▶ die _copy_error($CP, $status, $src, $dest, $stderr, $bang) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 3 3
348 3 ▶ print STDERR $stderr if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 0 3 3
349  
350 3 return;
351 }
352  
353 2 ▶sub _clone_dir_fcr {
Subroutine _clone_dir_fcr: called CC=3 SCAR=11.3
354 2 my ($src, $dest, %params) = @_;
355 2 require File::Copy::Recursive;
356  
357 2 my $err;
358 2 ▶ remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest 2 0 2
359 2 ▶ File::Copy::Recursive::dircopy($src, $dest)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::Recursive::dircopy($src, $dest) or die "File::Copy::Recursive::dircopy failed copying '${src}' -> '${dest}': $!" 2 0 2
360 or die "File::Copy::Recursive::dircopy failed copying '$src' -> '$dest': $!";
361 }
362  
363 0 ▶sub strip_hash_defaults {
Subroutine strip_hash_defaults: not called CC=7 SCAR=37.3
Pod undocumented
364 0 my ($hash, $defaults) = @_;
365  
366 0 my $out = {%$hash};
367  
368 0 for my $key (keys %$defaults) {
369 0 my $refout = ref($out->{$key});
370 0 my $refdef = ref($defaults->{$key});
371  
372 0 ▶ if ($refout eq $refdef && $refdef eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($refout eq $refdef and $refdef eq "HASH") 0 0 0
Condition0%0/3
$refout eq $refdef and $refdef eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$refout eq $refdef and $refdef eq "HASH"
$refout eq $refdef $refdef eq "HASH"
Truth table0%0/3
A and B
A= $refout eq $refdefB= $refdef eq "HASH"
ABresult
0X0
100
111
373 0 $out->{$key} = strip_hash_defaults($out->{$key}, $defaults->{$key});
374 0 next;
375 }
376  
377 0 ▶ if ($refout ne $refdef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refout ne $refdef) 0 0 0
378 0 delete $out->{$key};
379 0 next;
380 }
381  
382 59 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
383 0 ▶ delete $out->{$key} if $out->{$key} && $out->{$key} eq $defaults->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{$key} and $out->{$key} eq $defaults->{$key} 0 0 0
Condition0%0/3
$out->{$key} and $out->{$key} eq $defaults->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
$out->{$key} and $out->{$key} eq $defaults->{$key}
$out->{$key} $out->{$key} eq $defaults->{$key}
Truth table0%0/3
A and B
A= $out->{$key}B= $out->{$key} eq $defaults->{$key}
ABresult
0X0
100
111
384 }
385  
386 0 return $out;
387 }
388  
389 59 1;
« Test2/Tools/QuickDB.pm ↑ Summary DBIx/QuickDB/Driver/SQLite.pm »