lib/DBIx/QuickDB/Util.pm

filter: Statementstmt 85.4% 164 / 192 Branchbran 55.7% 68 / 122 Conditioncond 40.0% 24 / 60 MC/DCmcdc 17.1% 6 / 35 Subroutinesub 92.8% 26 / 28 Podpod 0.0% 0 / 7 totaltotal 64.8% 288 / 444 CC 90 SCAR 25.8
CC
90
Cov
68%
strip_hash_defaults
34.2
__ANON__
30.0
_dbi_name_matches_dir
23.3
CRAP 13.2
« blib/lib/DBIx/QuickDB/Driver/SQLite.pm ↑ Summary lib/DBIx/QuickDB/Util/HashBase.pm »
1 package DBIx::QuickDB::Util;
2 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '0.000066';
6  
7 70 ▶use Errno qw/EEXIST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 70 ▶use File::Path qw/remove_tree/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 70 ▶use IPC::Cmd qw/can_run/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 70 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 70 ▶use Time::HiRes qw/sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 70 ▶use Importer Importer => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our @EXPORT_OK = qw/clone_dir strip_hash_defaults env_timeout find_bin remove_tree_robust remove_tree_or_quarantine disconnect_dbi_handles/;
16  
17 70 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 52 ▶sub _retry_deferred_remove {
Subroutine _retry_deferred_remove: called CC=2 SCAR=7.0
25 @DEFERRED_REMOVE = grep {
26 52 ▶ -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 56 return;
29 }
30  
31 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
32 66 local $?;
33 66 _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 55 ▶sub _dbi_name_matches_dir {
Subroutine _dbi_name_matches_dir: called CC=16 SCAR=31.5
45 4 my ($name, $dir, $win32) = @_;
46 55 ▶ 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 2 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 3 ▶sub disconnect_dbi_handles {
Subroutine disconnect_dbi_handles: called CC=5 SCAR=29.4
Pod undocumented
78 3 my ($dir) = @_;
79  
80 2 ▶ return 0 unless $INC{'DBI.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DBI.pm"} 71 0 71
81 70 ▶ 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 70 my @handles;
84 DBI->visit_handles(
85 70 ▶ sub {
Subroutine __ANON__: 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 0 ▶sub remove_tree_robust {
Subroutine remove_tree_robust: not called CC=7 SCAR=23.0
Pod undocumented
117 0 my ($dir, $opts) = @_;
118 0 ▶ $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 8 ▶ last if $try == $tries;
Branch: 1 branch
BranchTrueFalseTotal
if $try == $tries 0 0 0
129 8 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 8 ▶sub _rename_tree {
Subroutine _rename_tree: called CC=1 SCAR=6.9
138 0 return rename($_[0], $_[1]);
139 }
140  
141 3 ▶sub _quarantine_retry_pause {
Subroutine _quarantine_retry_pause: called CC=2 SCAR=13.0
142 0 ▶ sleep 0.2 if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
143 3 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 0 ▶sub remove_tree_or_quarantine {
Subroutine remove_tree_or_quarantine: not called CC=11 SCAR=24.7
Pod undocumented
154 0 my ($dir, $opts) = @_;
155  
156 0 ▶ _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 8 my $error;
162 8 my $max_attempts = 10;
163 5 for my $attempt (1 .. $max_attempts) {
164 5 ▶ return '' unless -d $dir; # A concurrent watcher won the cleanup race.
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 16 16
165  
166 5 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 16 push @DEFERRED_REMOVE => $stale;
171 16 return $stale;
172 }
173  
174 4 $error = $!;
175 4 ▶ _quarantine_retry_pause() if $attempt < $max_attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt < $max_attempts 11 1 12
176 }
177  
178 12 ▶ return '' unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
179  
180 12 ▶ $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 9 ▶sub env_timeout {
Subroutine env_timeout: called CC=4 SCAR=14.0
Pod undocumented
190 2 my ($name, $default) = @_;
191 2 my $val = $ENV{$name};
192 54 ▶ return $val if defined($val) && $val =~ /^\d+$/ && $val > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and $val =~ /^\d+$/ and $val > 0 52 2 54
Condition66%4/6
defined $val and $val =~ /^\d+$/ and $val > 0
!ll&&!rl&&r
2052
defined $val and $val =~ /^\d+$/
!ll&&!rl&&r
2052
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 53 return $default;
194 }
195  
196 # can_run() for database binaries. Cygwin inherits the Windows PATH, and a
197 # native Windows server or helper found there cannot work under Cygwin (POSIX
198 # paths, Unix sockets), so skip anything under /cygdrive on that platform.
199 53 ▶sub find_bin {
Subroutine find_bin: called CC=8 SCAR=20.8
Pod undocumented
200 53 my ($cmd) = @_;
201 2 ▶ return can_run($cmd) unless $^O eq 'cygwin';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "cygwin" 124 3 127
202  
203 127 ▶ for my $dir (split /:/, $ENV{PATH} // '') {
Condition50%1/2
$ENV{"PATH"} // ""
l!l
30
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
204 127 ▶ next unless length($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir 3 8 11
205 3 ▶ next if $dir =~ m{^/cygdrive/};
Branch: 1 branch
BranchTrueFalseTotal
if $dir =~ m[^/cygdrive/] 3 5 8
206 11 my $path = "$dir/$cmd";
207 8 ▶ return $path if -f $path && -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path and -x _ 1 4 5
Condition100%3/3
-f $path and -x _
!ll&&!rl&&r
311
MC/DC100%2/2
-f $path and -x _
-f $path -x _
Truth table100%3/3
A and B
A= -f $pathB= -x _
ABresult
0X0
100
111
208 }
209  
210 5 return undef;
211 }
212  
213 70 my ($RSYNC, $CP);
214  
215 70 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
216 70 local $@;
217 70 $RSYNC = can_run('rsync');
218 70 $CP = can_run('cp');
219 }
220  
221 # clone_dir($src, $dest, %params) -- copy a data dir, dying on failure with the
222 # decoded exit status, both paths and the tool's stderr. %params: checksum,
223 # verbose. The rsync backend retries a partial transfer and warns when it does.
224 127 ▶sub clone_dir {
Subroutine clone_dir: called CC=3 SCAR=11.1
Pod undocumented
225 5 ▶ return _clone_dir_rsync(@_) if $RSYNC;
Branch: 1 branch
BranchTrueFalseTotal
if $RSYNC 5 1 6
226 2 ▶ return _clone_dir_cp(@_) if $CP;
Branch: 1 branch
BranchTrueFalseTotal
if $CP 1 0 1
227 6 return _clone_dir_fcr(@_);
228 }
229  
230 # Decode a wait status into an actionable message. $bang is passed in because
231 # the filehandle ops between system() and this call would have clobbered $!.
232 6 ▶sub _copy_error {
Subroutine _copy_error: called CC=7 SCAR=27.8
233 1 my ($cmd, $status, $src, $dest, $stderr, $bang) = @_;
234  
235 0 my $msg = "$cmd failed copying '$src' -> '$dest': ";
236  
237 3 ▶ if ($status == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == -1) { } 0 3 3
elsif (my $sig = $status & 127) { } 0 3 3
238 3 ▶ $msg .= "could not execute (" . (defined($bang) ? $bang : $!) . ")";
Branch: 1 branch
BranchTrueFalseTotal
defined $bang ? : 0 0 0
239 }
240 elsif (my $sig = $status & 127) {
241 3 ▶ $msg .= "terminated by signal $sig" . (($status & 128) ? " (core dumped)" : "");
Branch: 1 branch
BranchTrueFalseTotal
$status & 128 ? : 0 0 0
242 }
243 else {
244 0 $msg .= "exited " . ($status >> 8);
245 }
246  
247 0 ▶ $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
248  
249 3 return $msg;
250 }
251  
252 # Run $cmd with its stderr captured to a scratch file, returning ($status,
253 # $stderr, $errno). Only stderr is redirected: rsync writes -vP progress to
254 # STDOUT, so verbose runs still stream live exactly as before.
255 3 ▶sub _run_captured {
Subroutine _run_captured: called CC=8 SCAR=21.0
256 3 my (@cmd) = @_;
257  
258 3 require File::Temp;
259  
260 # QDB_TMPDIR because File::Spec->tmpdir falls back to the CURRENT DIRECTORY
261 # when /tmp is unwritable. UNLINK is only a backstop -- File::Temp keeps the
262 # handle registered, so a leak here would hold the fd until process exit;
263 # every path below unlinks explicitly.
264 my ($fh, $file) = File::Temp::tempfile(
265 "qdb-copy-err-$$-XXXXXXXX",
266 11 ▶ $ENV{QDB_TMPDIR} ? (DIR => $ENV{QDB_TMPDIR}) : (TMPDIR => 1),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'QDB_TMPDIR'} ? : 0 11 11
267 UNLINK => 1,
268 );
269  
270 # Best effort: a caller that closed STDERR (a daemonizer, say) makes this
271 # dup fail, and it could copy fine before capturing existed. Run uncaptured
272 # rather than failing the copy over diagnostics.
273 11 my $save;
274 11 ▶ unless (open($save, '>&', \*STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $save, ">&", \*STDERR) 1 10 11
275 11 close($fh);
276 11 unlink($file);
277  
278 1 system(@cmd);
279 1 my ($st, $bg) = ($?, $!);
280  
281 1 return ($st, '', $bg);
282 }
283  
284 # Restore STDERR on every path: reopening it closes it first, so bailing out
285 # mid-way would leave the process with no STDERR and free fd 2 for the next
286 # open(). Redirect through File::Temp's handle rather than reopening the
287 # path by name, which is the symlink race File::Temp exists to avoid.
288 1 my ($status, $bang);
289 1 my $ok = eval {
290 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
291  
292 10 system(@cmd);
293 10 ($status, $bang) = ($?, $!); # before anything can clobber them
294  
295 10 1;
296 };
297 10 my $err = $@;
298  
299 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
300 10 close($save);
301  
302 # Drain and clean up before any rethrow: an escaping exception would leave
303 # $fh registered with File::Temp, holding the fd until process exit. Rewind
304 # first, since the dup shared its offset with the child's writes.
305 10 my $stderr = '';
306 10 ▶ if (seek($fh, 0, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (seek $fh, 0, 0) 10 0 10
307 10 local $/;
308 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
309 }
310 10 close($fh);
311 10 unlink($file);
312  
313 10 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 10 10
314  
315 10 return ($status, $stderr, $bang);
316 }
317  
318 # Attempts for rsync exit 23, "partial transfer due to error". Exit 24
319 # ("vanished source files") is not retried -- the source is gone.
320 70 my $RSYNC_RETRIES = 3;
321  
322 11 ▶sub _clone_dir_rsync {
Subroutine _clone_dir_rsync: called CC=12 SCAR=26.0
323 10 my ($src, $dest, %params) = @_;
324  
325 my @cmd = (
326 $RSYNC, '-a', '--delete', '--exclude' => '.nfs*',
327 $params{checksum} ? ('-c') : (),
328 10 ▶ $params{verbose} ? ('-vP') : (),
Branch: 2 branches
BranchTrueFalseTotal
$params{'checksum'} ? : 0 5 5
$params{'verbose'} ? : 0 5 5
329 "$src/", $dest,
330 );
331  
332 5 for my $try (1 .. $RSYNC_RETRIES) {
333 5 my ($status, $stderr, $bang) = _run_captured(@cmd);
334  
335 5 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 2 6 8
336 8 ▶ print STDERR $stderr if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 2 0 2
337 8 return;
338 }
339  
340 2 ▶ 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
341  
342 2 ▶ 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
343 if !defined($code) || $code != 23 || $try == $RSYNC_RETRIES;
344  
345 # A retry that succeeds leaves this warning as the only record that a
346 # partial transfer happened at all.
347 6 my $msg = "$RSYNC exited 23 (partial transfer) copying '$src' -> '$dest'"
348 . " on attempt $try of $RSYNC_RETRIES, retrying";
349 6 ▶ $msg .= "\n=== $RSYNC stderr ===\n$stderr" if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 3 0 3
350 3 warn "$msg\n";
351  
352 3 sleep(0.25 * $try);
353 }
354  
355 3 return;
356 }
357  
358 5 ▶sub _clone_dir_cp {
Subroutine _clone_dir_cp: called CC=5 SCAR=17.0
359 3 my ($src, $dest, %params) = @_;
360 0 my $err;
361 3 ▶ remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest 2 1 3
362  
363 3 ▶ my ($status, $stderr, $bang) = _run_captured($CP, '-a', $params{verbose} ? ('-v') : (), "$src/.", $dest);
Branch: 1 branch
BranchTrueFalseTotal
$params{'verbose'} ? : 0 3 3
364 3 ▶ die _copy_error($CP, $status, $src, $dest, $stderr, $bang) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 3 3
365 3 ▶ print STDERR $stderr if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 0 3 3
366  
367 3 return;
368 }
369  
370 3 ▶sub _clone_dir_fcr {
Subroutine _clone_dir_fcr: called CC=3 SCAR=11.3
371 3 my ($src, $dest, %params) = @_;
372 3 require File::Copy::Recursive;
373  
374 2 my $err;
375 2 ▶ remove_tree($dest, {safe => 1, keep_root => 1, error => \$err}) if -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest 2 0 2
376 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
377 or die "File::Copy::Recursive::dircopy failed copying '$src' -> '$dest': $!";
378 }
379  
380 2 ▶sub strip_hash_defaults {
Subroutine strip_hash_defaults: called CC=7 SCAR=35.3
Pod undocumented
381 2 my ($hash, $defaults) = @_;
382  
383 2 my $out = {%$hash};
384  
385 0 for my $key (keys %$defaults) {
386 0 my $refout = ref($out->{$key});
387 0 my $refdef = ref($defaults->{$key});
388  
389 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
390 0 $out->{$key} = strip_hash_defaults($out->{$key}, $defaults->{$key});
391 0 next;
392 }
393  
394 0 ▶ if ($refout ne $refdef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refout ne $refdef) 0 0 0
395 0 delete $out->{$key};
396 0 next;
397 }
398  
399 70 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
400 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
401 }
402  
403 0 return $out;
404 }
405  
406 70 1;
« blib/lib/DBIx/QuickDB/Driver/SQLite.pm ↑ Summary lib/DBIx/QuickDB/Util/HashBase.pm »