blib/lib/App/FileSync/Trash.pm

filter: Statementstmt 95.0% 155 / 163 Branchbran 65.9% 29 / 44 Conditioncond 50.0% 20 / 40 MC/DCmcdc 13.0% 3 / 23 Subroutinesub 100% 32 / 32 Podpod 100% 6 / 6 totaltotal 79.5% 245 / 308 CC 45 SCAR 18.9
CC
45
Cov
83%
_restore_entry
15.8
prune
12.0
versions
9.1
CRAP 6.6
« blib/lib/App/FileSync/Ignore.pm ↑ Summary blib/lib/App/FileSync/Server.pm »
1 package App::FileSync::Trash;
2  
3 # Deleted files are never unlinked: they move into a dated trash tree
4 # (ROOT/YYYY/MM/DD/<relative path>) and are pruned a whole day at a time once
5 # they are older than the retention period.
6  
7 329 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 329 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 329 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 329 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 329 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 329 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 329 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 329 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 329 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 329 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 329 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
20 329 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 329 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 329 ▶use App::FileSync::Args qw(named);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 329 ▶use App::FileSync::Constants qw($MODE_MASK $TRASH_DAYS $SECONDS_PER_DAY $STAT_MODE $STAT_SIZE $STAT_MTIME);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 329 ▶use App::FileSync::I18N qw(throw os_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 329 ▶use App::FileSync::IO qw(copy_preserving ensure_dir list_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 329 ▶use version; our $VERSION = version->declare('0.002.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # The three levels of a day bucket, as strftime writes them and as the
31 # patterns recognise them.
32 329 Readonly::Scalar my $BUCKET_FORMAT => '%Y/%m/%d';
33 329 Readonly my $YEAR => qr/\A\d{4}\z/;
34 329 Readonly my $MONTH => qr/\A\d{2}\z/;
35 329 Readonly my $DAY => qr/\A\d{2}\z/;
36  
37 # Windows has no POSIX permission bits worth restoring on a directory.
38 329 Readonly::Scalar my $HAS_MODES => $^O ne 'MSWin32';
39  
40 =encoding utf8
41  
42 =head1 NAME
43  
44 App::FileSync::Trash - dated trash with retention
45  
46 =head1 SYNOPSIS
47  
48 my $where = App::FileSync::Trash::move_to_trash(
49 src => $abs, rel => 'notes/a.txt', root => $trash_dir,
50 );
51 my $versions = App::FileSync::Trash::versions(root => $trash_dir, rel => 'notes/a.txt');
52 App::FileSync::Trash::restore(src => $versions->[0], to => $abs);
53 App::FileSync::Trash::prune(root => $trash_dir, days => 30);
54  
55 =head1 FUNCTIONS
56  
57 =head2 move_to_trash
58  
59 Purpose: move a file or directory into today's trash bucket.
60  
61 Args: C<src> (absolute path), C<rel> (its relative path, kept inside the
62 bucket), C<root> (the trash directory), optional C<epoch> (default now).
63  
64 Returns: the absolute path it was moved to. A second deletion of the same
65 path on the same day is kept as C<PATH.1>, then C<PATH.2>, and so on.
66  
67 Side Effects: renames C<src>; across filesystems a file is copied (keeping its
68 mtime) and the original removed.
69  
70 Usage:
71  
72 my $where = App::FileSync::Trash::move_to_trash(src => $abs, rel => $rel, root => $trash);
73  
74 =head3 EXAMPLE
75  
76 App::FileSync::Trash::move_to_trash(
77 src => '/home/foo/Documents/old.txt', rel => 'old.txt',
78 root => '/home/foo/.filesync/trash/documents', epoch => time,
79 );
80  
81 =head3 API SPECIFICATION
82  
83 =head4 INPUT
84  
85 {
86 src => { type => 'string', min => 1 },
87 rel => { type => 'string', min => 1 },
88 root => { type => 'string', min => 1 },
89 epoch => { type => 'number', optional => 1 },
90 }
91  
92 =head4 OUTPUT
93  
94 { type => 'string', min => 1 }
95  
96 =head3 MESSAGES
97  
98 Key Meaning Resolution
99 trash.move the entry could not be moved check permissions
100 io.mkdir the bucket could not be created check permissions
101  
102 =head3 FORMAL SPECIFICATION
103  
104 ΔFS
105 src?, rel?, root? : PATH ; t? : ℕ ; dst! : PATH
106 let b = root? / day(t?) / rel?
107 dst! = b if b ∉ dom FS, else b ⁀ "." ⁀ min { n : ℕ₁ | b ⁀ "." ⁀ n ∉ dom FS }
108 FS'(dst!) = FS(src?) ∧ src? ∉ dom FS'
109  
110 =cut
111  
112 160 ▶sub move_to_trash {
Subroutine move_to_trash: called CC=7 SCAR=20.5
Pod documented
113 160 my $args = named(\@_, {
114 src => { type => 'string', min => 1 },
115 rel => { type => 'string', min => 1 },
116 root => { type => 'string', min => 1 },
117 epoch => { type => 'number', optional => 1 },
118 }, 'Trash::move_to_trash');
119  
120 160 my $src = $args->{src};
121 160 ▶ my $epoch = defined $args->{epoch} ? $args->{epoch} : time;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'epoch'} ? : 159 1 160
122  
123 160 my $destination = File::Spec->catfile(_bucket($args->{root}, $epoch), split m{/}, $args->{rel});
124 160 ensure_dir(dirname($destination));
125  
126 # Two deletions of the same path on the same day keep both.
127 160 my $final = $destination;
128 160 my $suffix = 0;
129 160 ▶ $final = $destination . '.' . ++$suffix while -e $final || -l $final;
Branch: 1 branch
BranchTrueFalseTotal
-e $final || -l $final and do { $final = $destination . '.' . ++$suffix } 5152 160 5312
Condition66%2/3
-e $final or -l $final
l!l&&r!l&&!r
51520160
MC/DC50%1/2
-e $final or -l $final
-e $final -l $final
Truth table66%2/3
A or B
A= -e $finalB= -l $final
ABresult
1X1
011
000
130  
131 # File::Copy::move renames when it can and otherwise copies (keeping the
132 # mtime) and unlinks; a directory has to rename in one step.
133 160 local $@;
134 my $moved = -d $src && !-l $src
135 0 ▶ ? eval { rename $src, $final; 1 }
Branch: 1 branch
BranchTrueFalseTotal
-d $src && !(-l $src) ? : 0 160 160
Condition33%1/3
-d $src and not -l $src
!ll&&!rl&&r
16000
MC/DC0%0/2
-d $src and not -l $src
-d $src not -l $src
Truth table33%1/3
A and not(B)
A= -d $srcB= not -l $src
ABresult
0X0
100
111
136 : File::Copy::move($src, $final);
137 160 ▶ throw('trash.move', { from => $src, to => $final, error => os_error($@) }) unless $moved;
Branch: 1 branch
BranchTrueFalseTotal
unless $moved 0 160 160
138  
139 160 return $final;
140 }
141  
142 =head2 prune
143  
144 Purpose: remove day buckets older than the retention period.
145  
146 Args: C<root>, optional C<days> (default 30; 0 or less disables pruning),
147 C<now> (default the current time), C<dry_run>.
148  
149 Returns: an arrayref of the bucket directories removed (or that would be).
150  
151 Side Effects: removes whole day directories. A bucket that cannot be removed
152 is left and not reported.
153  
154 Usage:
155  
156 my $removed = App::FileSync::Trash::prune(root => $trash, days => 30);
157  
158 =head3 EXAMPLE
159  
160 my $would = App::FileSync::Trash::prune(root => $trash, days => 7, dry_run => 1);
161 print "$_\n" for @{$would};
162  
163 =head3 API SPECIFICATION
164  
165 =head4 INPUT
166  
167 {
168 root => { type => 'string', optional => 1 },
169 days => { type => 'number', optional => 1 },
170 now => { type => 'number', optional => 1 },
171 dry_run => { type => 'boolean', optional => 1 },
172 }
173  
174 =head4 OUTPUT
175  
176 { type => 'arrayref' }
177  
178 =head3 MESSAGES
179  
180 None.
181  
182 =head3 FORMAL SPECIFICATION
183  
184 ΔFS
185 Old == { b : buckets(root) | midnight(b) ≤ now − days × 86400 }
186 dry_run ⇒ FS' = FS ∧ result = Old
187 ¬dry_run ⇒ result ⊆ Old ∧ ∀ b : result • b ∉ dom FS'
188  
189 =cut
190  
191 466 ▶sub prune {
Subroutine prune: called CC=11 SCAR=24.8
Pod documented
192 466 my $args = named(\@_, {
193 root => { type => 'string', optional => 1 },
194 days => { type => 'number', optional => 1 },
195 now => { type => 'number', optional => 1 },
196 dry_run => { type => 'boolean', optional => 1 },
197 }, 'Trash::prune');
198  
199 466 my $root = $args->{root};
200 466 ▶ my $days = defined $args->{days} ? $args->{days} : $TRASH_DAYS;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'days'} ? : 466 0 466
201 466 ▶ return [] unless defined $root && length $root && -d $root && $days > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and length $root and -d $root and $days > 0 252 214 466
Condition55%5/9
defined $root and length $root and -d $root and $days > 0
!ll&&!rl&&r
2520214
defined $root and length $root and -d $root
!ll&&!rl&&r
0252214
defined $root and length $root
!ll&&!rl&&r
00466
MC/DC25%1/4
defined $root and length $root and -d $root and $days > 0
defined $root length $root -d $root $days > 0
Truth table40%2/5
A and B and C and D
A= defined $rootB= length $rootC= -d $rootD= $days > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
202  
203 214 ▶ my $cutoff = (defined $args->{now} ? $args->{now} : time) - $days * $SECONDS_PER_DAY;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'now'} ? : 48 166 214
204  
205 214 my @removed;
206 214 for my $bucket (@{ _buckets($root) }) {
207 # Compare whole days so a mid-day run does not prune a bucket that
208 # only just crossed the boundary.
209 36 ▶ next if _midnight_epoch(@{$bucket}{qw(year month day)}) > $cutoff;
Branch: 1 branch
BranchTrueFalseTotal
if _midnight_epoch(@{$bucket;}{"year", "month", "day"}) > $cutoff 30 6 36
210  
211 6 ▶ my $gone = $args->{dry_run} || remove_tree($bucket->{path}, { error => \my $errors });
Condition66%2/3
$args->{'dry_run'} || remove_tree($bucket->{'path'}, {'error', \my $errors})
l!l&&r!l&&!r
240
MC/DC0%0/2
$args->{'dry_run'} || remove_tree($bucket->{'path'}, {'error', \my $errors})
$args->{'dry_run'} remove_tree($bucket->{'path'}, {'error', \my $errors})
Truth table66%2/3
A || B
A= $args->{'dry_run'}B= remove_tree($bucket->{'path'}, {'error', \my $errors})
ABresult
1X1
011
000
212 6 ▶ push @removed, $bucket->{path} if $gone;
Branch: 1 branch
BranchTrueFalseTotal
if $gone 6 0 6
213 }
214  
215 214 return \@removed;
216 }
217  
218 =head2 versions
219  
220 Purpose: every stored version of a path.
221  
222 Args: C<root>, C<rel>.
223  
224 Returns: an arrayref of absolute paths, newest first: by day, then by the
225 same-day suffix (C<.2> before C<.1> before the unsuffixed first deletion).
226  
227 Side Effects: none.
228  
229 Usage:
230  
231 my $versions = App::FileSync::Trash::versions(root => $trash, rel => 'notes/a.txt');
232  
233 =head3 EXAMPLE
234  
235 my ($newest) = @{ App::FileSync::Trash::versions(root => $trash, rel => 'a.txt') };
236  
237 =head3 API SPECIFICATION
238  
239 =head4 INPUT
240  
241 {
242 root => { type => 'string', optional => 1 },
243 rel => { type => 'string', min => 1 },
244 }
245  
246 =head4 OUTPUT
247  
248 { type => 'arrayref' }
249  
250 =head3 MESSAGES
251  
252 Key Meaning Resolution
253 args.invalid no rel was given pass the path
254  
255 =head3 FORMAL SPECIFICATION
256  
257 versions(root, rel) = sort_desc(day, suffix)
258 { v | ∃ b : buckets(root) • v = b/rel ∨ ∃ n : ℕ₁ • v = b/rel ⁀ "." ⁀ n }
259  
260 =cut
261  
262 38 ▶sub versions {
Subroutine versions: called CC=9 SCAR=22.0
Pod documented
263 38 my $args = named(\@_, {
264 root => { type => 'string', optional => 1 },
265 rel => { type => 'string', min => 1 },
266 }, 'Trash::versions');
267  
268 38 my $root = $args->{root};
269 38 ▶ return [] unless defined $root && -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and -d $root 2 36 38
Condition66%2/3
defined $root and -d $root
!ll&&!rl&&r
0236
MC/DC50%1/2
defined $root and -d $root
defined $root -d $root
Truth table66%2/3
A and B
A= defined $rootB= -d $root
ABresult
0X0
100
111
270  
271 36 my @parts = split m{/}, $args->{rel};
272 36 my $name = pop @parts;
273 36 my $stored = qr/\A\Q$name\E(?:\.(\d+))?\z/;
274  
275 # Collect [ path, date, suffix ] so the order is numeric: a plain string
276 # sort would put "a.txt.10" before "a.txt.9".
277 36 my @found;
278 36 for my $bucket (@{ _buckets($root) }) {
279 29 my $dir = File::Spec->catdir($bucket->{path}, @parts);
280 29 ▶ for my $entry (@{ list_dir($dir) || [] }) {
Condition50%1/2
list_dir($dir) || []
l!l
290
MC/DC0%0/1
list_dir($dir) || []
list_dir($dir)
Truth table50%1/2
A
A= list_dir($dir)
Aresult
11
00
281 138 ▶ next unless $entry =~ $stored;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /$stored/ 10 128 138
282 128 ▶ push @found, [ File::Spec->catfile($dir, $entry), $bucket->{date}, defined $1 ? $1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 102 26 128
283 }
284 }
285  
286 36 ▶ my @newest_first = sort { $b->[1] cmp $a->[1] || $b->[2] <=> $a->[2] } @found;
Condition66%2/3
$App::FileSync::Trash::b->[1] cmp $App::FileSync::Trash::a->[1] || $App::FileSync::Trash::b->[2] <=> $App::FileSync::Trash::a->[2]
l!l&&r!l&&!r
43760
MC/DC0%0/2
$App::FileSync::Trash::b->[1] cmp $App::FileSync::Trash::a->[1] || $App::FileSync::Trash::b->[2] <=> $App::FileSync::Trash::a->[2]
$App::FileSync::Trash::b->[1] cmp $App::FileSync::Trash::a->[1] $App::FileSync::Trash::b->[2] <=> $App::FileSync::Trash::a->[2]
Truth table66%2/3
A || B
A= $App::FileSync::Trash::b->[1] cmp $App::FileSync::Trash::a->[1]B= $App::FileSync::Trash::b->[2] <=> $App::FileSync::Trash::a->[2]
ABresult
1X1
011
000
287 36 return [ map { $_->[0] } @newest_first ];
288 }
289  
290 =head2 restore
291  
292 Purpose: copy one trashed version out, keeping modes and mtimes.
293  
294 Args: C<src> (a path from L</versions>), C<to> (the destination; must not exist).
295  
296 Returns: C<to>.
297  
298 Side Effects: copies a file, or a directory tree, to C<to>. The trash entry is
299 left in place, so the same version can be restored twice.
300  
301 Usage:
302  
303 App::FileSync::Trash::restore(src => $versions->[0], to => $abs);
304  
305 =head3 EXAMPLE
306  
307 App::FileSync::Trash::restore(src => $version, to => '/tmp/recovered.txt');
308  
309 =head3 API SPECIFICATION
310  
311 =head4 INPUT
312  
313 {
314 src => { type => 'string', min => 1 },
315 to => { type => 'string', min => 1 },
316 }
317  
318 =head4 OUTPUT
319  
320 { type => 'string', min => 1 }
321  
322 =head3 MESSAGES
323  
324 Key Meaning Resolution
325 trash.missing the version is not there list versions again
326 trash.exists the destination already exists move it aside
327 trash.cannot_stat an entry cannot be examined check permissions
328 trash.not_regular a special file is in the version restore by hand
329 io.copy a file could not be copied check space
330  
331 =head3 FORMAL SPECIFICATION
332  
333 ΔFS
334 pre src ∈ dom FS ∧ to ∉ dom FS
335 post tree'(to) = tree(src) ∧ src ∈ dom FS'
336  
337 =cut
338  
339 9 ▶sub restore {
Subroutine restore: called CC=5 SCAR=18.1
Pod documented
340 9 my $args = named(\@_, {
341 src => { type => 'string', min => 1 },
342 to => { type => 'string', min => 1 },
343 }, 'Trash::restore');
344 9 my ($src, $to) = @{$args}{qw(src to)};
345  
346 9 ▶ throw('trash.missing', { path => $src }) unless -e $src || -l $src;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $src or -l $src 0 9 9
Condition33%1/3
-e $src or -l $src
l!l&&r!l&&!r
900
MC/DC0%0/2
-e $src or -l $src
-e $src -l $src
Truth table33%1/3
A or B
A= -e $srcB= -l $src
ABresult
1X1
011
000
347 9 ▶ throw('trash.exists', { path => $to }) if -e $to || -l $to;
Branch: 1 branch
BranchTrueFalseTotal
if -e $to or -l $to 0 9 9
Condition33%1/3
-e $to or -l $to
l!l&&r!l&&!r
009
MC/DC0%0/2
-e $to or -l $to
-e $to -l $to
Truth table33%1/3
A or B
A= -e $toB= -l $to
ABresult
1X1
011
000
348  
349 9 _restore_entry($src, $to);
350 9 return $to;
351 }
352  
353 =head2 date_of
354  
355 Purpose: the trash day a stored path belongs to.
356  
357 Args: C<$path>, as returned by L</versions> or L</move_to_trash>.
358  
359 Returns: C<YYYY-MM-DD>, or C<undef> for a path outside a day bucket. Both
360 C</> and C<\> separators are understood.
361  
362 Side Effects: none.
363  
364 Usage:
365  
366 my $date = App::FileSync::Trash::date_of($version);
367  
368 =head3 EXAMPLE
369  
370 print App::FileSync::Trash::date_of('/t/trash/2026/10/22/a/b.txt'), "\n"; # 2026-10-22
371  
372 =head3 API SPECIFICATION
373  
374 =head4 INPUT
375  
376 [ { name => 'path', type => 'string', optional => 1 } ]
377  
378 =head4 OUTPUT
379  
380 { type => 'string', optional => 1, matches => qr/\A\d{4}-\d{2}-\d{2}\z/ }
381  
382 =head3 MESSAGES
383  
384 None.
385  
386 =head3 FORMAL SPECIFICATION
387  
388 date_of(p ⁀ sep ⁀ yyyy ⁀ sep ⁀ mm ⁀ sep ⁀ dd ⁀ sep ⁀ q) = yyyy ⁀ "-" ⁀ mm ⁀ "-" ⁀ dd
389  
390 =cut
391  
392 21 ▶sub date_of {
Subroutine date_of: called CC=3 SCAR=11.7
Pod documented
393 21 my ($path) = @_;
394  
395 21 ▶ my @date = defined $path ? $path =~ m{[/\\](\d{4})[/\\](\d{2})[/\\](\d{2})[/\\]} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 21 0 21
396 21 ▶ return @date ? join('-', @date) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@date ? : 21 0 21
397 }
398  
399 =head2 version_info
400  
401 Purpose: describe one trashed version for a listing.
402  
403 Args: C<$path>, a path from L</versions>.
404  
405 Returns: C<{ path, date, size, mtime }>; C<size> and C<mtime> are C<undef> if
406 the version has gone since it was listed.
407  
408 Side Effects: stats the path.
409  
410 Usage:
411  
412 my @rows = map { App::FileSync::Trash::version_info($_) } @{$versions};
413  
414 =head3 EXAMPLE
415  
416 my $info = App::FileSync::Trash::version_info($versions->[0]);
417 print "$info->{date} $info->{size} bytes\n";
418  
419 =head3 API SPECIFICATION
420  
421 =head4 INPUT
422  
423 [ { name => 'path', type => 'string', min => 1 } ]
424  
425 =head4 OUTPUT
426  
427 {
428 type => 'hashref',
429 schema => {
430 path => { type => 'string' },
431 date => { type => 'string', optional => 1 },
432 size => { type => 'integer', optional => 1 },
433 mtime => { type => 'number', optional => 1 },
434 },
435 }
436  
437 =head3 MESSAGES
438  
439 None.
440  
441 =head3 FORMAL SPECIFICATION
442  
443 version_info(p) = ⟨path ↦ p, date ↦ date_of(p), size ↦ size(FS(p)), mtime ↦ mtime(FS(p))⟩
444  
445 =cut
446  
447 6 ▶sub version_info {
Subroutine version_info: called CC=3 SCAR=11.7
Pod documented
448 6 my ($path) = @_;
449  
450 # A listing races with prune, so a vanished version is reported, not fatal.
451 6 my @stat = Time::HiRes::stat($path);
452 return {
453 6 ▶ path => $path,
Branch: 2 branches
BranchTrueFalseTotal
@stat ? : 6 0 6
@stat ? : 6 0 6
454 date => date_of($path),
455 size => @stat ? $stat[$STAT_SIZE] : undef,
456 mtime => @stat ? $stat[$STAT_MTIME] : undef,
457 };
458 }
459  
460 # _bucket
461 # Purpose: the day directory for an epoch.
462 # Entry: $root is the trash root; $epoch a time.
463 # Exit: returns ROOT/YYYY/MM/DD (UTC).
464 # Side effects: none.
465 166 ▶sub _bucket :Private {
Subroutine _bucket: called CC=1 SCAR=0.0
466 166 my ($root, $epoch) = @_;
467  
468 # One directory per year, month and day: strftime gives "2026/11/04", and
469 # catdir rejoins the parts with this platform's separator.
470 166 return File::Spec->catdir($root, split m{/}, strftime($BUCKET_FORMAT, gmtime($epoch)));
471 329 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
472  
473 # _buckets
474 # Purpose: every day bucket under a trash root.
475 # Entry: $root exists.
476 # Exit: returns an arrayref of { path, year, month, day, date },
477 # oldest first.
478 # Side effects: none. Stray names at any level are ignored.
479 250 ▶sub _buckets :Private {
Subroutine _buckets: called CC=7 SCAR=19.7
480 250 my ($root) = @_;
481  
482 250 my @buckets;
483 250 ▶ for my $year (grep { $_ =~ $YEAR } @{ list_dir($root) || [] }) {
Condition50%1/2
list_dir($root) || []
l!l
2500
MC/DC0%0/1
list_dir($root) || []
list_dir($root)
Truth table50%1/2
A
A= list_dir($root)
Aresult
11
00
484 57 my $year_dir = File::Spec->catdir($root, $year);
485 57 ▶ for my $month (grep { $_ =~ $MONTH } @{ list_dir($year_dir) || [] }) {
Condition50%1/2
list_dir($year_dir) || []
l!l
570
MC/DC0%0/1
list_dir($year_dir) || []
list_dir($year_dir)
Truth table50%1/2
A
A= list_dir($year_dir)
Aresult
11
00
486 59 my $month_dir = File::Spec->catdir($year_dir, $month);
487 59 ▶ for my $day (grep { $_ =~ $DAY } @{ list_dir($month_dir) || [] }) {
Condition50%1/2
list_dir($month_dir) || []
l!l
590
MC/DC0%0/1
list_dir($month_dir) || []
list_dir($month_dir)
Truth table50%1/2
A
A= list_dir($month_dir)
Aresult
11
00
488 65 push @buckets, {
489 path => File::Spec->catdir($month_dir, $day),
490 year => $year,
491 month => $month,
492 day => $day,
493 date => "$year-$month-$day",
494 };
495 }
496 }
497 }
498  
499 250 return \@buckets;
500 329 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
501  
502 # _restore_entry
503 # Purpose: copy a trashed file or directory tree out of the trash.
504 # Entry: $src exists; $to does not (or is a directory being filled).
505 # Exit: returns 1.
506 # Side effects: creates $to; raises trash.cannot_stat or trash.not_regular.
507 9 ▶sub _restore_entry :Private {
Subroutine _restore_entry: called CC=7 SCAR=27.6
508 9 my ($src, $to) = @_;
509  
510 9 my @stat = Time::HiRes::lstat($src);
511 9 ▶ throw('trash.cannot_stat', { path => $src, error => os_error() }) unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 9 9
512  
513 9 ▶ if (-d _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) 0 9 9
514 0 ensure_dir($to);
515 _restore_entry(File::Spec->catfile($src, $_), File::Spec->catfile($to, $_))
516 0 ▶ for @{ list_dir($src) || [] };
Condition0%0/2
list_dir($src) || []
l!l
00
MC/DC0%0/1
list_dir($src) || []
list_dir($src)
Truth table0%0/2
A
A= list_dir($src)
Aresult
11
00
517  
518 # Directory metadata last: filling it changed its mtime.
519 0 ▶ chmod($stat[$STAT_MODE] & $MODE_MASK, $to) if $HAS_MODES;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_MODES 0 0 0
520 0 utime($stat[$STAT_MTIME], $stat[$STAT_MTIME], $to);
521 0 return 1;
522 }
523  
524 9 ▶ throw('trash.not_regular', { path => $src }) unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 9 9
525 9 copy_preserving($src, $to, \@stat);
526  
527 9 return 1;
528 329 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
529  
530 # _midnight_epoch
531 # Purpose: the UTC epoch at the start of a bucket's day.
532 # Entry: numeric year, month (1-12) and day strings from a bucket path.
533 # Exit: returns the epoch.
534 # Side effects: none.
535 36 ▶sub _midnight_epoch :Private {
Subroutine _midnight_epoch: called CC=1 SCAR=0.0
536 36 my ($year, $month, $day) = @_;
537  
538 36 return Time::Local::timegm(0, 0, 0, $day, $month - 1, $year);
539 329 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
540  
541 329 1;
542  
543 __END__
« blib/lib/App/FileSync/Ignore.pm ↑ Summary blib/lib/App/FileSync/Server.pm »