blib/lib/App/FileSync/Admin.pm

filter: Statementstmt 98.9% 283 / 286 Branchbran 85.3% 70 / 82 Conditioncond 70.5% 67 / 95 MC/DCmcdc 43.1% 22 / 51 Subroutinesub 100% 51 / 51 Podpod 100% 13 / 13 totaltotal 87.5% 506 / 578 CC 79 SCAR 19.2
CC
79
Cov
91%
__ANON__
17.1
restore
13.4
repo_create
11.5
CRAP 6.8
« blib/lib/App/FileSync/Ignore.pm ↑ Summary blib/lib/App/FileSync/Server.pm »
1 package App::FileSync::Admin;
2  
3 # Server-side administration: the operations behind filesync-admin. Every
4 # method works directly on the server root; none of them talks to a client.
5  
6 429 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 429 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 429 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 429 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 429 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 429 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 429 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 429 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 429 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 429 ▶use parent 'App::FileSync::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 429 ▶use App::FileSync qw(json_decode validate_relpath validate_repo_name validate_client_id parse_since parse_until);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 429 ▶use App::FileSync::Args qw(named positional);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 429 ▶use App::FileSync::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 429 ▶use App::FileSync::Constants qw($DEFAULT_SERVER_ROOT $ROLE_CLIENT @ROLES $MAX_PERCENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 429 ▶use App::FileSync::I18N qw(os_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 429 ▶use App::FileSync::Ignore;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 429 ▶use App::FileSync::IO qw(ensure_dir list_dir open_file sha256_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 429 ▶use App::FileSync::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 429 ▶use App::FileSync::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 429 ▶use App::FileSync::Trash;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 429 ▶use App::FileSync::Verify;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 429 ▶use version; our $VERSION = version->declare('0.002.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # The per-repo metadata directories a new repo starts with.
35 429 Readonly my @META_DIRS => qw(clients trash journal locks);
36  
37 # Journal files are one per month, named YYYY-MM.jsonl.
38 429 Readonly my $JOURNAL_FILE => qr/\.jsonl\z/;
39  
40 # A trash date argument.
41 429 Readonly my $DATE => qr/\A\d{4}-\d{2}-\d{2}\z/;
42  
43 # Arguments most methods take.
44 429 my %REPO = (repo => { type => 'string', min => 1 });
45  
46 =encoding utf8
47  
48 =head1 NAME
49  
50 App::FileSync::Admin - server-side repo, client and recovery administration
51  
52 =head1 SYNOPSIS
53  
54 my $admin = App::FileSync::Admin->new(root => '/srv/filesync');
55 $admin->repo_create(name => 'docs');
56 $admin->client_allow(repo => 'docs', client => 'laptop');
57 my $report = $admin->verify(repo => 'docs', deep => 1);
58  
59 =head1 DESCRIPTION
60  
61 The library behind C<filesync-admin>. Methods return hashrefs or arrayrefs
62 for the command line to print (or encode as JSON) and raise catalogued
63 errors (see L<App::FileSync::I18N>) on failure.
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 Purpose: an administrator for one server root.
70  
71 Args: optional C<root> (default C<FILESYNC_ROOT>, then F</srv/filesync>),
72 C<logger> (default from L<App::FileSync::Settings>), C<language>.
73  
74 Returns: the object.
75  
76 Side Effects: builds a logger from the settings when none is given.
77  
78 Usage:
79  
80 my $admin = App::FileSync::Admin->new(root => $root);
81  
82 =head3 EXAMPLE
83  
84 my $admin = App::FileSync::Admin->new;
85 print $admin->root, "\n"; # /srv/filesync unless FILESYNC_ROOT says otherwise
86  
87 =head3 API SPECIFICATION
88  
89 =head4 INPUT
90  
91 {
92 root => { type => 'string', optional => 1 },
93 logger => { type => 'object', optional => 1 },
94 language => { type => 'string', optional => 1 },
95 }
96  
97 =head4 OUTPUT
98  
99 { type => 'object', isa => 'App::FileSync::Admin' }
100  
101 =head3 MESSAGES
102  
103 Key Meaning Resolution
104 args.invalid an argument is malformed fix the call
105  
106 =head3 FORMAL SPECIFICATION
107  
108 root' = first non-empty ⟨root, FILESYNC_ROOT, "/srv/filesync"⟩
109  
110 =cut
111  
112 186 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod documented
113 186 my $class = shift;
114 186 my $args = named(\@_, {
115 root => { type => 'string', optional => 1 },
116 logger => { type => 'object', optional => 1 },
117 language => { type => 'string', optional => 1 },
118 }, 'Admin->new');
119  
120 186 ▶ my ($root) = grep { defined && length } $args->{root}, $ENV{FILESYNC_ROOT}, $DEFAULT_SERVER_ROOT;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
1860372
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
121  
122 return bless {
123 root => $root,
124 language => $args->{language},
125 186 ▶ logger => $args->{logger} || App::FileSync::Settings::logger(),
Condition33%1/3
$args->{'logger'} || App::FileSync::Settings::logger()
l!l&&r!l&&!r
01860
MC/DC0%0/2
$args->{'logger'} || App::FileSync::Settings::logger()
$args->{'logger'} App::FileSync::Settings::logger()
Truth table33%1/3
A || B
A= $args->{'logger'}B= App::FileSync::Settings::logger()
ABresult
1X1
011
000
126 }, $class;
127 }
128  
129 =head2 root
130  
131 Purpose: the server root this object administers.
132  
133 Args: none.
134  
135 Returns: the path.
136  
137 Side Effects: none.
138  
139 Usage:
140  
141 my $root = $admin->root;
142  
143 =head3 EXAMPLE
144  
145 print $admin->root, "\n";
146  
147 =head3 API SPECIFICATION
148  
149 =head4 INPUT
150  
151 [ { name => 'self', type => 'object' } ]
152  
153 =head4 OUTPUT
154  
155 { type => 'string', min => 1 }
156  
157 =head3 MESSAGES
158  
159 None.
160  
161 =head3 FORMAL SPECIFICATION
162  
163 root = self.root
164  
165 =cut
166  
167 30 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
168 30 my ($self) = @_;
169  
170 30 return $self->{root};
171 }
172  
173 =head2 repo_create
174  
175 Purpose: create a repo: its master tree, metadata directories, policy and an
176 empty index.
177  
178 Args: C<name> (a repo id), optional C<trash_days>, C<max_deletes>,
179 C<max_delete_percent> (non-negative integers; C<max_delete_percent> is at
180 most 100) and C<ignore> (patterns; the
181 default ignore list when absent or empty). A C<trash_days> of 0 keeps the
182 server's trash forever. A C<max_deletes> or C<max_delete_percent> of 0
183 switches that deletion limit off; the other still applies, and setting both
184 to 0 lets a sync delete any number of files without C<--force>. Both limits
185 are inclusive (a sync may delete exactly C<max_deletes> files), and the
186 percentage is of the files the client last synced, rounded down but never
187 below one file.
188  
189 Returns: C<{ name, master, config }> (the master and config paths).
190  
191 Side Effects: creates F<ROOT/master/NAME> and F<ROOT/.filesync/repos/NAME>.
192  
193 Usage:
194  
195 $admin->repo_create(name => 'docs', trash_days => 60);
196  
197 =head3 EXAMPLE
198  
199 my $made = $admin->repo_create(name => 'photos', ignore => [ '*.tmp', '/cache/' ]);
200 print "master at $made->{master}\n";
201  
202 =head3 API SPECIFICATION
203  
204 =head4 INPUT
205  
206 {
207 name => { type => 'string', min => 1, max => 64 },
208 trash_days => { type => 'integer', min => 0, optional => 1 },
209 max_deletes => { type => 'integer', min => 0, optional => 1 },
210 max_delete_percent => { type => 'integer', min => 0, max => 100, optional => 1 },
211 ignore => { type => 'arrayref', optional => 1 },
212 }
213  
214 =head4 OUTPUT
215  
216 {
217 type => 'hashref',
218 schema => {
219 name => { type => 'string' },
220 master => { type => 'string' },
221 config => { type => 'string' },
222 },
223 }
224  
225 =head3 MESSAGES
226  
227 Key Meaning Resolution
228 admin.bad_repo the name is not a valid id letters, digits, . _ - ; at most 64
229 admin.repo_exists the repo already exists choose another name
230 admin.root_unwritable the root cannot be written chown it, or use --root elsewhere
231 io.mkdir / io.write the files cannot be written check permissions
232  
233 =head3 FORMAL SPECIFICATION
234  
235 ΔFS
236 pre name ∈ Id ∧ repo_config_path(root, name) ∉ dom FS ∧ writable(root)
237 pre max_delete_percent ∈ 0 .. 100
238 post repo_config_path(root, name) ∈ dom FS' ∧ index(name) = ∅
239  
240 =head3 PSEUDOCODE
241  
242 validate the name; refuse if the repo exists or the root cannot be written
243 start from the default policy; apply the given limits and ignore list
244 create the master and metadata directories
245 save the policy and an empty index
246  
247 =cut
248  
249 118 ▶sub repo_create {
Subroutine repo_create: called CC=10 SCAR=24.4
Pod documented
250 118 my $self = shift;
251 118 my $args = named(\@_, {
252 name => { type => 'string', optional => 1 },
253 trash_days => { type => 'integer', min => 0, optional => 1 },
254 max_deletes => { type => 'integer', min => 0, optional => 1 },
255 max_delete_percent => { type => 'integer', min => 0, max => $MAX_PERCENT, optional => 1 },
256 ignore => { type => 'arrayref', optional => 1 },
257 }, 'Admin::repo_create');
258  
259 114 my $name = $self->_valid_repo($args->{name});
260  
261 114 my $config_path = App::FileSync::Config::repo_config_path($self->{root}, $name);
262 114 ▶ $self->_throw('admin.repo_exists', { repo => $name, path => $config_path }) if -e $config_path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $config_path 0 114 114
263  
264 114 my $master = File::Spec->catdir($self->{root}, 'master', $name);
265 114 my $meta = App::FileSync::Config::repo_meta_dir($self->{root}, $name);
266  
267 114 my $config = App::FileSync::Config::default_repo_config($name);
268 114 for my $limit (qw(trash_days max_deletes max_delete_percent)) {
269 342 ▶ $config->{$limit} = $args->{$limit} + 0 if defined $args->{$limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$limit} 13 329 342
270 }
271 # A copy: the caller's list must not become the stored policy's storage.
272 0 ▶ $config->{ignore} = [ @{ $args->{ignore} } ] if $args->{ignore} && @{ $args->{ignore} };
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"ignore"} and @{$args->{"ignore"};} 0 114 114
Condition33%1/3
$args->{"ignore"} and @{$args->{"ignore"};}
!ll&&!rl&&r
11400
MC/DC0%0/2
$args->{"ignore"} and @{$args->{"ignore"};}
$args->{"ignore"} @{$args->{"ignore"};}
Truth table33%1/3
A and B
A= $args->{"ignore"}B= @{$args->{"ignore"};}
ABresult
0X0
100
111
273  
274 114 my $made = eval {
275 114 ensure_dir($_) for $master, map { File::Spec->catdir($meta, $_) } @META_DIRS;
276 112 1;
277 };
278 114 ▶ unless ($made) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($made) 2 112 114
279 2 my $error = $@;
280 # A root another account owns is a setup mistake. Name the directory
281 # to fix rather than the deepest one make_path could not create, which
282 # is several levels below the one the administrator passed. Only when
283 # that directory really is unwritable: a full disk or a bad path keeps
284 # its own error.
285 2 ▶ my $blocked = _unwritable_base($master) // _unwritable_base($meta);
Condition33%1/3
_unwritable_base($master) // _unwritable_base($meta)
l!l&&r!l&&!r
200
MC/DC0%0/2
_unwritable_base($master) // _unwritable_base($meta)
_unwritable_base($master) _unwritable_base($meta)
Truth table33%1/3
A // B
A= _unwritable_base($master)B= _unwritable_base($meta)
ABresult
1X1
011
000
286 2 ▶ $self->_throw('admin.root_unwritable', { root => $self->{root}, blocked => $blocked }) if defined $blocked;
Branch: 1 branch
BranchTrueFalseTotal
if defined $blocked 2 0 2
287 0 die $error; ## no critic (ErrorHandling::RequireCarping) -- rethrow unchanged
288 }
289  
290 112 App::FileSync::Config::save_repo($self->{root}, $name, $config);
291 112 $self->_store($name, $config)->save_index({});
292  
293 112 return { name => $name, master => $master, config => $config_path };
294 }
295  
296 =head2 repo_list
297  
298 Purpose: the repos under this root.
299  
300 Args: none.
301  
302 Returns: an arrayref of names, sorted.
303  
304 Side Effects: reads the metadata directory.
305  
306 Usage:
307  
308 print "$_\n" for @{ $admin->repo_list };
309  
310 =head3 EXAMPLE
311  
312 my $repos = $admin->repo_list;
313 print @{$repos} ? "@{$repos}\n" : "none\n";
314  
315 =head3 API SPECIFICATION
316  
317 =head4 INPUT
318  
319 [ { name => 'self', type => 'object' } ]
320  
321 =head4 OUTPUT
322  
323 { type => 'arrayref' }
324  
325 =head3 MESSAGES
326  
327 None; a root with no repos gives an empty list.
328  
329 =head3 FORMAL SPECIFICATION
330  
331 repo_list = sort { n | n ∈ children(root/.filesync/repos) ∧ isdir(n) }
332  
333 =cut
334  
335 1 ▶sub repo_list {
Subroutine repo_list: called CC=2 SCAR=7.0
Pod documented
336 1 my ($self) = @_;
337  
338 1 my $root = File::Spec->catdir($self->{root}, '.filesync', 'repos');
339 1 ▶ return [ grep { -d File::Spec->catdir($root, $_) } @{ list_dir($root) || [] } ];
Condition50%1/2
list_dir($root) || []
l!l
10
MC/DC0%0/1
list_dir($root) || []
list_dir($root)
Truth table50%1/2
A
A= list_dir($root)
Aresult
11
00
340 }
341  
342 =head2 repo_show
343  
344 Purpose: a repo's policy and where it lives.
345  
346 Args: C<$name>.
347  
348 Returns: C<{ config, master, meta, clients }>; C<clients> is a sorted arrayref.
349  
350 Side Effects: reads the repo config.
351  
352 Usage:
353  
354 my $info = $admin->repo_show('docs');
355  
356 =head3 EXAMPLE
357  
358 my $info = $admin->repo_show('docs');
359 print "trash kept $info->{config}{trash_days} days\n";
360  
361 =head3 API SPECIFICATION
362  
363 =head4 INPUT
364  
365 [ { name => 'name', type => 'string', min => 1 } ]
366  
367 =head4 OUTPUT
368  
369 { type => 'hashref' }
370  
371 =head3 MESSAGES
372  
373 Key Meaning Resolution
374 admin.bad_repo the name is not an id check the spelling
375 config.no_repo the repo does not exist filesync-admin repo list
376  
377 =head3 FORMAL SPECIFICATION
378  
379 repo_show(n) = ⟨config ↦ load_repo(n), master ↦ master_dir(n), meta ↦ meta_dir(n), clients ↦ sort dom config.clients⟩
380  
381 =cut
382  
383 9 ▶sub repo_show {
Subroutine repo_show: called CC=1 SCAR=0.0
Pod documented
384 9 my ($self, $name) = @_;
385  
386 9 my $store = $self->_store($name);
387 return {
388 config => $store->config,
389 master => $store->master_dir(),
390 meta => $store->meta_dir(),
391 9 clients => [ sort keys %{ $store->config->{clients} } ],
392 };
393 }
394  
395 =head2 client_allow
396  
397 Purpose: let a client (or replica) sync a repo.
398  
399 Args: C<repo>, C<client> (a client id), optional C<role> (C<client>, the
400 default, or C<replica>).
401  
402 Returns: C<{ client, role }>.
403  
404 Side Effects: rewrites the repo config. Allowing an existing client again
405 changes its role.
406  
407 Usage:
408  
409 $admin->client_allow(repo => 'docs', client => 'backup', role => 'replica');
410  
411 =head3 EXAMPLE
412  
413 $admin->client_allow(repo => 'docs', client => 'laptop');
414  
415 =head3 API SPECIFICATION
416  
417 =head4 INPUT
418  
419 {
420 repo => { type => 'string', min => 1 },
421 client => { type => 'string', optional => 1 },
422 role => { type => 'string', optional => 1, memberof => [ 'client', 'replica' ] },
423 }
424  
425 =head4 OUTPUT
426  
427 { type => 'hashref', schema => { client => { type => 'string' }, role => { type => 'string' } } }
428  
429 =head3 MESSAGES
430  
431 Key Meaning Resolution
432 admin.bad_client the client id is not valid letters, digits, . _ - ; at most 64
433 admin.bad_role the role is not client/replica choose one of those
434 admin.bad_repo the repo name is not an id check the spelling
435 config.no_repo the repo does not exist create it first
436  
437 =head3 FORMAL SPECIFICATION
438  
439 ΔRepoConfig
440 pre client ∈ Id ∧ role ∈ {client, replica}
441 clients' = clients ⊕ {client ↦ role}
442  
443 =cut
444  
445 106 ▶sub client_allow {
Subroutine client_allow: called CC=3 SCAR=11.0
Pod documented
446 106 my $self = shift;
447 106 my $args = named(\@_, {
448 %REPO,
449 client => { type => 'string', optional => 1 },
450 role => { type => 'string', optional => 1 },
451 }, 'Admin::client_allow');
452  
453 106 my $client = $self->_valid_client($args->{client});
454 105 ▶ my $role = $args->{role} || $ROLE_CLIENT;
Condition66%2/3
$args->{'role'} || $App::FileSync::Admin::ROLE_CLIENT
l!l&&r!l&&!r
10230
MC/DC0%0/2
$args->{'role'} || $App::FileSync::Admin::ROLE_CLIENT
$args->{'role'} $App::FileSync::Admin::ROLE_CLIENT
Truth table66%2/3
A || B
A= $args->{'role'}B= $App::FileSync::Admin::ROLE_CLIENT
ABresult
1X1
011
000
455 $self->_throw('admin.bad_role', { role => $role, roles => join(', ', @ROLES) })
456 105 ▶ unless any { $_ eq $role } @ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $role; } , @App::FileSync::Admin::ROLES) 0 105 105
457  
458 105 my $repo = $self->_valid_repo($args->{repo});
459 105 my $config = App::FileSync::Config::load_repo($self->{root}, $repo);
460 104 $config->{clients}{$client} = { role => $role };
461 104 App::FileSync::Config::save_repo($self->{root}, $repo, $config);
462  
463 104 return { client => $client, role => $role };
464 }
465  
466 =head2 client_list
467  
468 Purpose: the clients allowed on a repo.
469  
470 Args: C<$repo>.
471  
472 Returns: an arrayref, sorted by id, of C<{ client, role, base }>; C<base> is 1
473 when the server holds that client's base (it has synced at least once).
474  
475 Side Effects: reads the repo config.
476  
477 Usage:
478  
479 for my $c (@{ $admin->client_list('docs') }) { ... }
480  
481 =head3 EXAMPLE
482  
483 printf "%-12s %s\n", $_->{client}, $_->{role} for @{ $admin->client_list('docs') };
484  
485 =head3 API SPECIFICATION
486  
487 =head4 INPUT
488  
489 [ { name => 'repo', type => 'string', min => 1 } ]
490  
491 =head4 OUTPUT
492  
493 { type => 'arrayref' }
494  
495 =head3 MESSAGES
496  
497 Key Meaning Resolution
498 config.no_repo the repo does not exist check the name
499  
500 =head3 FORMAL SPECIFICATION
501  
502 client_list(r) = sort ⟨ (c, role(c), base(c) ∈ dom FS) | c ∈ dom clients(r) ⟩
503  
504 =cut
505  
506 9 ▶sub client_list {
Subroutine client_list: called CC=5 SCAR=16.1
Pod documented
507 9 my ($self, $repo) = @_;
508  
509 9 my $store = $self->_store($repo);
510 8 my $clients = $store->config->{clients};
511  
512 # A hand-edited config may hold an id that is not one; list it without a base.
513 return [
514 map {
515 18 my $entry = $clients->{$_};
516 18 my $base = eval { $store->base_path($_) };
517 {
518 client => $_,
519 18 ▶ role => (ref($entry) eq 'HASH' && $entry->{role}) || $ROLE_CLIENT,
Branch: 1 branch
BranchTrueFalseTotal
defined $base && -e $base ? : 3 15 18
Condition77%7/9
ref $entry eq 'HASH' && $entry->{'role'} || $App::FileSync::Admin::ROLE_CLIENT
l!l&&r!l&&!r
1620
ref $entry eq "HASH" and $entry->{"role"}
!ll&&!rl&&r
2016
defined $base and -e $base
!ll&&!rl&&r
2133
MC/DC0%0/3
ref $entry eq 'HASH' && $entry->{'role'} || $App::FileSync::Admin::ROLE_CLIENT
ref $entry eq "HASH" $entry->{"role"} $App::FileSync::Admin::ROLE_CLIENT
MC/DC100%2/2
defined $base and -e $base
defined $base -e $base
Truth table40%2/5
A and B || C
A= ref $entry eq "HASH"B= $entry->{"role"}C= $App::FileSync::Admin::ROLE_CLIENT
ABCresult
11X1
0X11
1011
0X00
1000
Truth table100%3/3
A and B
A= defined $baseB= -e $base
ABresult
0X0
100
111
520 base => (defined $base && -e $base) ? 1 : 0,
521 }
522 8 } sort keys %{$clients}
523 ];
524 }
525  
526 =head2 client_forget
527  
528 Purpose: drop the server's base for a client, so its next sync starts afresh.
529  
530 Args: C<repo>, C<client>.
531  
532 Returns: C<{ removed =E<gt> 0|1, path }>.
533  
534 Side Effects: removes the client's base file. The client stays allowed.
535  
536 Usage:
537  
538 $admin->client_forget(repo => 'docs', client => 'laptop');
539  
540 =head3 EXAMPLE
541  
542 my $done = $admin->client_forget(repo => 'docs', client => 'old-laptop');
543 print $done->{removed} ? "forgot\n" : "nothing to forget\n";
544  
545 =head3 API SPECIFICATION
546  
547 =head4 INPUT
548  
549 {
550 repo => { type => 'string', min => 1 },
551 client => { type => 'string', min => 1 },
552 }
553  
554 =head4 OUTPUT
555  
556 { type => 'hashref', schema => { removed => { type => 'integer' }, path => { type => 'string' } } }
557  
558 =head3 MESSAGES
559  
560 Key Meaning Resolution
561 store.bad_client the client id is not valid check the spelling
562 io.remove the base cannot be removed check permissions
563  
564 =head3 FORMAL SPECIFICATION
565  
566 ΔFS
567 FS' = {base_path(r, c)} ⩤ FS
568 removed = 1 ⇔ base_path(r, c) ∈ dom FS
569  
570 =cut
571  
572 5 ▶sub client_forget {
Subroutine client_forget: called CC=3 SCAR=11.1
Pod documented
573 5 my $self = shift;
574 5 my $args = named(\@_, { %REPO, client => { type => 'string', min => 1 } }, 'Admin::client_forget');
575  
576 # base_path validates the id, so "../x" cannot reach outside the repo.
577 5 my $path = $self->_store($args->{repo})->base_path($args->{client});
578 4 ▶ return { removed => 0, path => $path } unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 4 4
579  
580 4 ▶ eval { unlink $path; 1 } or $self->_throw('io.remove', { path => $path, error => os_error($@) });
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &App::FileSync::Admin::unlink($path); 1 } } or $self->_throw("io.remove", {"path", $path, "error", os_error($@)}) 4 0 4
581 4 return { removed => 1, path => $path };
582 }
583  
584 =head2 journal_dir
585  
586 Purpose: where a repo's journal lives.
587  
588 Args: C<$repo>.
589  
590 Returns: F<ROOT/.filesync/repos/REPO/journal>.
591  
592 Side Effects: reads the repo config.
593  
594 Usage:
595  
596 my $dir = $admin->journal_dir('docs');
597  
598 =head3 EXAMPLE
599  
600 print $admin->journal_dir('docs'), "\n";
601  
602 =head3 API SPECIFICATION
603  
604 =head4 INPUT
605  
606 [ { name => 'repo', type => 'string', min => 1 } ]
607  
608 =head4 OUTPUT
609  
610 { type => 'string', min => 1 }
611  
612 =head3 MESSAGES
613  
614 Key Meaning Resolution
615 config.no_repo the repo does not exist check the name
616  
617 =head3 FORMAL SPECIFICATION
618  
619 journal_dir(r) = meta_dir(r) / "journal"
620  
621 =cut
622  
623 92 ▶sub journal_dir {
Subroutine journal_dir: called CC=1 SCAR=0.0
Pod documented
624 92 my ($self, $repo) = @_;
625  
626 92 return File::Spec->catdir($self->_store($repo)->meta_dir(), 'journal');
627 }
628  
629 =head2 journal_read
630  
631 Purpose: the journal's events, filtered.
632  
633 Args: C<repo>; optional filters C<client> (id), C<since> and C<until>
634 (C<YYYY-MM-DD> or epoch, inclusive), C<exits> (arrayref of exit codes; selects
635 session outcomes), C<warnings> (sessions that kept conflict copies) and
636 C<limit> (the newest N).
637  
638 Returns: an arrayref of event hashrefs, oldest first.
639  
640 Side Effects: reads every monthly journal file. A torn final line from a
641 killed run is skipped.
642  
643 Usage:
644  
645 my $events = $admin->journal_read(repo => 'docs', since => '2026-10-01', limit => 50);
646  
647 =head3 EXAMPLE
648  
649 for my $event (@{ $admin->journal_read(repo => 'docs', exits => [ 1, 2 ]) }) {
650 print "$event->{ts} $event->{client} exit $event->{exit}\n";
651 }
652  
653 =head3 API SPECIFICATION
654  
655 =head4 INPUT
656  
657 {
658 repo => { type => 'string', min => 1 },
659 client => { type => 'string', optional => 1 },
660 since => { type => 'string', optional => 1 },
661 until => { type => 'string', optional => 1 },
662 exits => { type => 'arrayref', optional => 1 },
663 warnings => { type => 'boolean', optional => 1 },
664 limit => { type => 'integer', optional => 1 },
665 }
666  
667 =head4 OUTPUT
668  
669 { type => 'arrayref' }
670  
671 =head3 MESSAGES
672  
673 Key Meaning Resolution
674 when.bad since/until is not a date use YYYY-MM-DD or an epoch
675 io.read a journal file cannot be read check permissions
676  
677 =head3 FORMAL SPECIFICATION
678  
679 let E = ⟨ e | f ∈ sort(journal files), line ∈ f, e = json(line) ∈ HASH ⟩
680 journal_read = last limit (sort_by ts (filter(E)))
681  
682 =head3 PSEUDOCODE
683  
684 read every journal file in name (month) order, skipping torn lines
685 keep events for the client, inside since..until
686 keep session outcomes with a wanted exit code, if asked
687 keep sessions that kept conflict copies, if asked
688 sort by time; keep the newest limit
689  
690 =cut
691  
692 64 ▶sub journal_read {
Subroutine journal_read: called CC=7 SCAR=19.6
Pod documented
693 64 my $self = shift;
694 64 my $args = named(\@_, {
695 %REPO,
696 client => { type => 'string', optional => 1 },
697 since => { type => 'string', optional => 1 },
698 until => { type => 'string', optional => 1 },
699 exits => { type => 'arrayref', optional => 1 },
700 warnings => { type => 'scalar', optional => 1 },
701 limit => { type => 'integer', optional => 1 },
702 }, 'Admin::journal_read');
703  
704 # list_dir, not glob: glob splits a root containing spaces into words.
705 64 my $dir = $self->journal_dir($args->{repo});
706 64 ▶ my @files = map { File::Spec->catfile($dir, $_) } grep { $_ =~ $JOURNAL_FILE } @{ list_dir($dir) || [] };
Condition50%1/2
list_dir($dir) || []
l!l
640
MC/DC0%0/1
list_dir($dir) || []
list_dir($dir)
Truth table50%1/2
A
A= list_dir($dir)
Aresult
11
00
707  
708 64 my $keep = $self->_event_filter($args);
709 63 ▶ my @events = sort { ($a->{ts} || 0) <=> ($b->{ts} || 0) } grep { $keep->($_) } map { @{ _read_events($_) } } @files;
Condition50%2/4
$App::FileSync::Admin::a->{'ts'} || 0
l!l
7290
$App::FileSync::Admin::b->{'ts'} || 0
l!l
7290
MC/DC0%0/1
$App::FileSync::Admin::a->{'ts'} || 0
$App::FileSync::Admin::a->{'ts'}
MC/DC0%0/1
$App::FileSync::Admin::b->{'ts'} || 0
$App::FileSync::Admin::b->{'ts'}
Truth table50%1/2
A
A= $App::FileSync::Admin::a->{'ts'}
Aresult
11
00
Truth table50%1/2
A
A= $App::FileSync::Admin::b->{'ts'}
Aresult
11
00
710  
711 63 my $limit = $args->{limit};
712 63 ▶ splice(@events, 0, @events - $limit) if defined $limit && $limit > 0 && @events > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $limit > 0 and @events > $limit 5 58 63
Condition83%5/6
defined $limit and $limit > 0 and @events > $limit
!ll&&!rl&&r
33255
defined $limit and $limit > 0
!ll&&!rl&&r
33030
MC/DC66%2/3
defined $limit and $limit > 0 and @events > $limit
defined $limit $limit > 0 @events > $limit
Truth table75%3/4
A and B and C
A= defined $limitB= $limit > 0C= @events > $limit
ABCresult
0XX0
10X0
1100
1111
713 63 return \@events;
714 }
715  
716 =head2 verify
717  
718 Purpose: rehash the master against its index and report what no longer
719 agrees.
720  
721 Args: C<repo>, optional C<deep> (hash every file) and C<sample> (hash N
722 random files).
723  
724 Returns: the L<App::FileSync::Verify> report, plus C<repo> and C<master>.
725  
726 Side Effects: reads the master tree; logs a debug summary.
727  
728 Usage:
729  
730 my $report = $admin->verify(repo => 'docs', deep => 1);
731  
732 =head3 EXAMPLE
733  
734 my $report = $admin->verify(repo => 'docs', sample => 20);
735 print $report->{ok} ? "clean\n" : "drift\n";
736  
737 =head3 API SPECIFICATION
738  
739 =head4 INPUT
740  
741 {
742 repo => { type => 'string', min => 1 },
743 deep => { type => 'boolean', optional => 1 },
744 sample => { type => 'integer', min => 0, optional => 1 },
745 }
746  
747 =head4 OUTPUT
748  
749 { type => 'hashref' }
750  
751 =head3 MESSAGES
752  
753 Key Meaning Resolution
754 config.no_repo the repo does not exist check the name
755 verify.* see App::FileSync::Verify
756  
757 =head3 FORMAL SPECIFICATION
758  
759 verify(r) = Verify(master_dir(r), index(r), ignore(r)) ⊕ ⟨repo ↦ r, master ↦ master_dir(r)⟩
760  
761 =cut
762  
763 15 ▶sub verify {
Subroutine verify: called CC=2 SCAR=6.9
Pod documented
764 15 my $self = shift;
765 15 my $args = named(\@_, {
766 %REPO,
767 deep => { type => 'scalar', optional => 1 },
768 sample => { type => 'integer', min => 0, optional => 1 },
769 }, 'Admin::verify');
770  
771 15 my $repo = $args->{repo};
772 15 my $store = $self->_store($repo);
773  
774 my $report = App::FileSync::Verify::verify(
775 root => $store->master_dir(),
776 entries => $store->load_index(),
777 ignore => App::FileSync::Ignore->new(patterns => $store->config->{ignore}),
778 deep => $args->{deep},
779 sample => $args->{sample},
780 15 );
781 15 @{$report}{qw(repo master)} = ($repo, $store->master_dir());
782  
783 $self->{logger}->debug('verify finished', {
784 repo => $repo,
785 files => $report->{files},
786 45 ▶ (map { $_ => scalar @{ $report->{$_} } } qw(modified missing extra)),
Branch: 1 branch
BranchTrueFalseTotal
$report->{'ok'} ? : 8 7 15
787 15 ok => $report->{ok} ? 1 : 0,
788 });
789  
790 15 return $report;
791 }
792  
793 =head2 reindex
794  
795 Purpose: rebuild F<master.json> from the tree, hashing every file.
796  
797 Used after a manual repair (a rollback from a backup, say) so the index
798 describes the restored master without waiting for the next client session.
799 The old index is deliberately not reused: a restored file can keep its size
800 and mtime while its content differs, which is exactly the case reindex exists
801 for.
802  
803 Args: C<repo>, optional C<dry_run>.
804  
805 Returns: C<{ repo, master, index, files, dirs, bytes, skipped, dry_run, written }>.
806  
807 Side Effects: holds the repo lock (the same one the server takes), so it never
808 races a session; writes the index and a journal event unless C<dry_run>.
809  
810 Usage:
811  
812 my $result = $admin->reindex(repo => 'docs');
813  
814 =head3 EXAMPLE
815  
816 my $plan = $admin->reindex(repo => 'docs', dry_run => 1);
817 print "would index $plan->{files} files\n";
818  
819 =head3 API SPECIFICATION
820  
821 =head4 INPUT
822  
823 {
824 repo => { type => 'string', min => 1 },
825 dry_run => { type => 'boolean', optional => 1 },
826 }
827  
828 =head4 OUTPUT
829  
830 { type => 'hashref' }
831  
832 =head3 MESSAGES
833  
834 Key Meaning Resolution
835 admin.locked a sync session holds the repo lock retry when it has finished
836 config.no_repo the repo does not exist check the name
837  
838 =head3 FORMAL SPECIFICATION
839  
840 ΔFS
841 pre lock(r) is free
842 ¬dry_run ⇒ index'(r) = scan(master_dir(r))
843  
844 =head3 PSEUDOCODE
845  
846 open the repo and take its lock
847 walk the master, hashing everything
848 unless dry run: save the index and journal the reindex
849 log and report the counts
850  
851 =cut
852  
853 9 ▶sub reindex {
Subroutine reindex: called CC=5 SCAR=16.1
Pod documented
854 9 my $self = shift;
855 9 my $args = named(\@_, { %REPO, dry_run => { type => 'scalar', optional => 1 } }, 'Admin::reindex');
856  
857 9 my $repo = $args->{repo};
858 9 my $store = $self->_store($repo);
859 9 ▶ my $dry_run = $args->{dry_run} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'dry_run'} ? : 2 7 9
860  
861 # Held until $lock goes out of scope at the end of this method.
862 9 ▶ my $lock = $store->acquire_lock() or $self->_throw('admin.locked', { repo => $repo });
Branch: 1 branch
BranchTrueFalseTotal
my $lock = $store->acquire_lock or $self->_throw("admin.locked", {"repo", $repo}) 8 1 9
863  
864 8 my ($manifest, $stats, $skipped) = @{ $store->walk_master({}) }{qw(manifest stats skipped)};
865 8 my %counts = (map { $_ => $stats->{$_} } qw(files dirs bytes));
866  
867 8 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 6 2 8
868 6 $store->save_index($manifest);
869 6 $store->journal({ action => 'reindex', %counts, skipped => scalar(keys %{$skipped}), result => 'ok' });
870 }
871  
872 $self->{logger}->info('reindexed master', {
873 8 repo => $repo, %counts, skipped => scalar(keys %{$skipped}), dry_run => $dry_run,
874 });
875  
876 return {
877 8 ▶ repo => $repo,
Branch: 1 branch
BranchTrueFalseTotal
$dry_run ? : 2 6 8
878 master => $store->master_dir(),
879 index => $store->index_path(),
880 %counts,
881 skipped => $skipped,
882 dry_run => $dry_run,
883 written => $dry_run ? 0 : 1,
884 };
885 }
886  
887 =head2 restore
888  
889 Purpose: put a path back from the server trash: into the master by default,
890 or into C<to> as an export.
891  
892 Args: C<repo>, C<path> (the relative path as synced), optional C<to> (a
893 directory), C<date> (C<YYYY-MM-DD>; the newest version from that day), C<list>
894 (only list the versions).
895  
896 Returns: with C<list>, C<{ repo, path, versions }> (rows from
897 L<App::FileSync::Trash/version_info>); otherwise
898 C<{ repo, path, from, to, date, sha256, into_master }>.
899  
900 Side Effects: copies the version out (the trash entry is kept, so this is
901 repeatable); when restoring into the master, rebuilds the index so verify and
902 the next sync agree; journals the restore.
903  
904 Usage:
905  
906 $admin->restore(repo => 'docs', path => 'notes/a.txt');
907  
908 =head3 EXAMPLE
909  
910 my $done = $admin->restore(repo => 'docs', path => 'a.txt', to => '/tmp/out', date => '2026-10-20');
911 print "restored to $done->{to}\n";
912  
913 =head3 API SPECIFICATION
914  
915 =head4 INPUT
916  
917 {
918 repo => { type => 'string', min => 1 },
919 path => { type => 'string', min => 1 },
920 to => { type => 'string', optional => 1 },
921 date => { type => 'string', optional => 1 },
922 list => { type => 'boolean', optional => 1 },
923 }
924  
925 =head4 OUTPUT
926  
927 { type => 'hashref' }
928  
929 =head3 MESSAGES
930  
931 Key Meaning Resolution
932 admin.unsafe_path the path fails validate_relpath fix the path
933 admin.no_versions nothing of that path is in the trash check "restore --list"
934 admin.bad_date the date is not YYYY-MM-DD fix it
935 admin.no_version_on no version from that day pick a listed date
936 admin.not_dir the export target is not a directory choose another
937 admin.exists the destination exists move it aside first
938  
939 =head3 FORMAL SPECIFICATION
940  
941 ΔFS
942 pre path ∈ SafePath ∧ versions(path) ≠ ⟨⟩ ∧ dest ∉ dom FS
943 post tree'(dest) = tree(chosen) ∧ (into_master ⇒ index' = scan(master))
944  
945 =head3 PSEUDOCODE
946  
947 validate the path and list its trashed versions
948 if listing, return them
949 choose the newest version, or the newest from the given day
950 destination: the master, or the export directory (created if needed)
951 refuse to overwrite; copy the version out
952 if into the master, rebuild the index
953 journal, log and report
954  
955 =cut
956  
957 12 ▶sub restore {
Subroutine restore: called CC=13 SCAR=25.9
Pod documented
958 12 my $self = shift;
959 12 my $args = named(\@_, {
960 %REPO,
961 path => { type => 'string', optional => 1 },
962 to => { type => 'string', optional => 1 },
963 date => { type => 'string', optional => 1 },
964 list => { type => 'scalar', optional => 1 },
965 }, 'Admin::restore');
966  
967 12 my ($repo, $path) = @{$args}{qw(repo path)};
968 12 my ($safe, $reason) = validate_relpath($path);
969 12 ▶ $self->_throw('admin.unsafe_path', { path => $path // '', reason => $reason }) unless $safe;
Branch: 1 branch
BranchTrueFalseTotal
unless $safe 0 12 12
Condition0%0/2
$path // ""
l!l
00
MC/DC0%0/1
$path // ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
970  
971 12 my $store = $self->_store($repo);
972 12 my $versions = App::FileSync::Trash::versions(root => $store->trash_dir(), rel => $path);
973  
974 12 ▶ if ($args->{list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"list"}) 3 9 12
975 3 return { repo => $repo, path => $path, versions => [ map { App::FileSync::Trash::version_info($_) } @{$versions} ] };
976 }
977  
978 9 my $chosen = $self->_choose_version($versions, $args);
979  
980 5 ▶ my $into_master = !(defined $args->{to} && length $args->{to});
Condition66%2/3
defined $args->{'to'} && length $args->{'to'}
!ll&&!rl&&r
203
MC/DC50%1/2
defined $args->{'to'} && length $args->{'to'}
defined $args->{'to'} length $args->{'to'}
Truth table66%2/3
A && B
A= defined $args->{'to'}B= length $args->{'to'}
ABresult
0X0
100
111
981 5 ▶ my $base = $into_master ? $store->master_dir() : $args->{to};
Branch: 1 branch
BranchTrueFalseTotal
$into_master ? : 2 3 5
982 5 ▶ unless ($into_master) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($into_master) 3 2 5
983 3 ensure_dir($base);
984 3 ▶ $self->_throw('admin.not_dir', { path => $base }) unless -d $base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $base 0 3 3
985 }
986  
987 5 my $destination = File::Spec->catfile($base, split m{/}, $path);
988 5 ▶ $self->_throw('admin.exists', { path => $destination }) if -e $destination || -l $destination;
Branch: 1 branch
BranchTrueFalseTotal
if -e $destination or -l $destination 1 4 5
Condition66%2/3
-e $destination or -l $destination
l!l&&r!l&&!r
104
MC/DC50%1/2
-e $destination or -l $destination
-e $destination -l $destination
Truth table66%2/3
A or B
A= -e $destinationB= -l $destination
ABresult
1X1
011
000
989  
990 4 App::FileSync::Trash::restore(src => $chosen, to => $destination);
991 4 my $sha = sha256_file($destination);
992  
993 # The index is a cache; rebuild it so verify and the next sync agree that
994 # the restored tree is what the master now holds.
995 4 ▶ $store->save_index($store->walk_master($store->load_index())->{manifest}) if $into_master;
Branch: 1 branch
BranchTrueFalseTotal
if $into_master 1 3 4
996  
997 4 ▶ my %moved = (path => $path, to => $destination, (defined $sha ? (sha256 => $sha) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $sha ? : 4 0 4
998 4 $store->journal({ action => 'restore', %moved, trash => $chosen, result => 'ok' });
999 4 $self->{logger}->info('restored path', { repo => $repo, path => $path, from => $chosen, to => $destination });
1000  
1001 return {
1002 4 ▶ repo => $repo,
Branch: 1 branch
BranchTrueFalseTotal
$into_master ? : 1 3 4
1003 %moved,
1004 from => $chosen,
1005 date => App::FileSync::Trash::date_of($chosen),
1006 into_master => $into_master ? 1 : 0,
1007 };
1008 }
1009  
1010 # _unwritable_base
1011 # Purpose: find the nearest existing directory above a path that this
1012 # account cannot write.
1013 # Entry: $path is the file or directory about to be created.
1014 # Exit: returns that directory, or undef when every existing one is
1015 # writable (so creation should succeed).
1016 # Side effects: none.
1017 2 ▶sub _unwritable_base :Private {
Subroutine _unwritable_base: called CC=5 SCAR=17.2
1018 2 my ($path) = @_;
1019  
1020 2 my $base = $path;
1021 2 ▶ $base = dirname($base) while !-d $base && dirname($base) ne $base;
Branch: 1 branch
BranchTrueFalseTotal
not -d $base and dirname($base) ne $base and do { $base = dirname($base) } 3 2 5
Condition66%2/3
not -d $base and dirname($base) ne $base
!ll&&!rl&&r
203
MC/DC50%1/2
not -d $base and dirname($base) ne $base
not -d $base dirname($base) ne $base
Truth table66%2/3
not(A) and B
A= not -d $baseB= dirname($base) ne $base
ABresult
0X0
100
111
1022  
1023 2 ▶ return (-d $base && !-w $base) ? $base : undef;
Branch: 1 branch
BranchTrueFalseTotal
-d $base && !(-w $base) ? : 2 0 2
Condition33%1/3
-d $base and not -w $base
!ll&&!rl&&r
002
MC/DC0%0/2
-d $base and not -w $base
-d $base not -w $base
Truth table33%1/3
A and not(B)
A= -d $baseB= not -w $base
ABresult
0X0
100
111
1024 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1025  
1026 # _store
1027 # Purpose: open a repo after checking its name.
1028 # Entry: $repo is the caller's name; $config optional (saves a read).
1029 # Exit: returns an App::FileSync::Store.
1030 # Side effects: reads the repo config unless given; raises admin.bad_repo or
1031 # config.no_repo.
1032 263 ▶sub _store :Private {
Subroutine _store: called CC=2 SCAR=6.9
1033 263 my ($self, $repo, $config) = @_;
1034  
1035 return App::FileSync::Store->new(
1036 root => $self->{root},
1037 repo => $self->_valid_repo($repo),
1038 language => $self->{language},
1039 263 ▶ ($config ? (config => $config) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$config ? : 112 151 263
1040 );
1041 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1042  
1043 # _valid_repo / _valid_client
1044 # Purpose: check an id with the shared validators (which also cap the
1045 # length), so admin, server and transport agree on what an id is.
1046 # Entry: the candidate, possibly undef.
1047 # Exit: returns the id.
1048 # Side effects: raises admin.bad_repo / admin.bad_client.
1049 482 ▶sub _valid_repo :Private {
Subroutine _valid_repo: called CC=2 SCAR=7.0
1050 482 my ($self, $name) = @_;
1051  
1052 482 my ($ok, $reason) = validate_repo_name($name);
1053 482 ▶ $self->_throw('admin.bad_repo', { reason => $reason }) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 482 482
1054 482 return $name;
1055 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1056  
1057 106 ▶sub _valid_client :Private {
Subroutine _valid_client: called CC=2 SCAR=6.9
1058 106 my ($self, $id) = @_;
1059  
1060 106 my ($ok, $reason) = validate_client_id($id);
1061 106 ▶ $self->_throw('admin.bad_client', { reason => $reason }) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 105 106
1062 105 return $id;
1063 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1064  
1065 # _event_filter
1066 # Purpose: turn journal_read's options into one predicate.
1067 # Entry: $args validated by journal_read.
1068 # Exit: returns a coderef taking an event, true to keep it.
1069 # Side effects: raises when.bad for a malformed since/until (before any
1070 # file is read, since the bounds are parsed here).
1071 64 ▶sub _event_filter :Private {
Subroutine _event_filter: called CC=6 SCAR=17.9
1072 64 my ($self, $args) = @_;
1073  
1074 64 ▶ my $client = (defined $args->{client} && length $args->{client}) ? $args->{client} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'client'} && length $args->{'client'} ? : 3 61 64
Condition66%2/3
defined $args->{"client"} and length $args->{"client"}
!ll&&!rl&&r
6103
MC/DC50%1/2
defined $args->{"client"} and length $args->{"client"}
defined $args->{"client"} length $args->{"client"}
Truth table66%2/3
A and B
A= defined $args->{"client"}B= length $args->{"client"}
ABresult
0X0
100
111
1075 64 ▶ my $since = defined $args->{since} ? parse_since($args->{since}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'since'} ? : 4 60 64
1076 63 ▶ my $until = defined $args->{until} ? parse_until($args->{until}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'until'} ? : 3 60 63
1077  
1078 # Only session outcomes record an exit code, so this selects those.
1079 63 ▶ my %exits = map { ($_ + 0) => 1 } @{ $args->{exits} || [] };
Condition100%2/2
$args->{'exits'} || []
l!l
3528
MC/DC100%1/1
$args->{'exits'} || []
$args->{'exits'}
Truth table100%2/2
A
A= $args->{'exits'}
Aresult
11
00
1080  
1081 # The server journals planned conflicts on the plan event too, but only a
1082 # session outcome shows a run that kept the conflict copies, which is the
1083 # client's --warnings case.
1084 63 my $warnings = $args->{warnings};
1085  
1086 752 ▶ return sub {
Subroutine __ANON__: called CC=17 SCAR=28.4
1087 752 my ($event) = @_;
1088 752 ▶ my $ts = $event->{ts} || 0;
Condition50%1/2
$event->{'ts'} || 0
l!l
7520
MC/DC0%0/1
$event->{'ts'} || 0
$event->{'ts'}
Truth table50%1/2
A
A= $event->{'ts'}
Aresult
11
00
1089 752 ▶ return 0 if defined $client && !(defined $event->{client} && $event->{client} eq $client);
Branch: 1 branch
BranchTrueFalseTotal
if defined $client and not defined $event->{"client"} && $event->{"client"} eq $client 42 710 752
Condition83%5/6
defined $client and not defined $event->{"client"} && $event->{"client"} eq $client
!ll&&!rl&&r
709142
defined $event->{'client'} && $event->{'client'} eq $client
!ll&&!rl&&r
0421
MC/DC66%2/3
defined $client and not defined $event->{"client"} && $event->{"client"} eq $client
defined $client defined $event->{'client'} $event->{'client'} eq $client
Truth table75%3/4
A and not(B && C)
A= defined $clientB= defined $event->{'client'}C= $event->{'client'} eq $client
ABCresult
0XX0
1110
10X1
1101
1090 710 ▶ return 0 if (defined $since && $ts < $since) || (defined $until && $ts > $until);
Branch: 1 branch
BranchTrueFalseTotal
if defined $since and $ts < $since or defined $until and $ts > $until 56 654 710
Condition100%9/9
defined $since and $ts < $since or defined $until and $ts > $until
l!l&&r!l&&!r
2630654
defined $since and $ts < $since
!ll&&!rl&&r
6711326
defined $until and $ts > $until
!ll&&!rl&&r
6391530
MC/DC100%4/4
defined $since and $ts < $since or defined $until and $ts > $until
defined $since $ts < $since defined $until $ts > $until
Truth table71%5/7
A and B or C and D
A= defined $sinceB= $ts < $sinceC= defined $untilD= $ts > $until
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1091 654 ▶ return 0 if %exits && !(defined $event->{exit} && $exits{ $event->{exit} + 0 });
Branch: 1 branch
BranchTrueFalseTotal
if %exits and not defined $event->{"exit"} && $exits{$event->{"exit"} + 0} 193 461 654
Condition100%6/6
%exits and not defined $event->{"exit"} && $exits{$event->{"exit"} + 0}
!ll&&!rl&&r
44417193
defined $event->{'exit'} && $exits{$event->{'exit'} + 0}
!ll&&!rl&&r
1405317
MC/DC100%3/3
%exits and not defined $event->{"exit"} && $exits{$event->{"exit"} + 0}
%exits defined $event->{'exit'} $exits{$event->{'exit'} + 0}
Truth table100%4/4
A and not(B && C)
A= %exitsB= defined $event->{'exit'}C= $exits{$event->{'exit'} + 0}
ABCresult
0XX0
1110
10X1
1101
1092 461 ▶ return 0 if $warnings && !(($event->{action} || '') eq 'session' && ($event->{conflicts} || 0) > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $warnings and not +($event->{"action"} || "") eq "session" && ($event->{"conflicts"} || 0) > 0 76 385 461
Condition90%9/10
$warnings and not +($event->{"action"} || "") eq "session" && ($event->{"conflicts"} || 0) > 0
!ll&&!rl&&r
380576
($event->{'action'} || '') eq 'session' && ($event->{'conflicts'} || 0) > 0
!ll&&!rl&&r
50265
$event->{'action'} || ''
l!l
810
$event->{'conflicts'} || 0
l!l
526
MC/DC66%2/3
$warnings and not +($event->{"action"} || "") eq "session" && ($event->{"conflicts"} || 0) > 0
$warnings $event->{'action'} $event->{'conflicts'}
Truth table75%3/4
A and not(B && C)
A= $warningsB= $event->{'action'}C= $event->{'conflicts'}
ABCresult
0XX0
1110
10X1
1101
1093 385 return 1;
1094 63 };
1095 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1096  
1097 # _read_events
1098 # Purpose: parse one journal file.
1099 # Entry: $file exists.
1100 # Exit: returns an arrayref of event hashrefs.
1101 # Side effects: reads the file; raises io.read if it cannot be opened.
1102 # Lines that are not JSON objects (a torn final line from a
1103 # killed run) are skipped, not fatal.
1104 62 ▶sub _read_events :Private {
Subroutine _read_events: called CC=2 SCAR=6.9
1105 62 my ($file) = @_;
1106  
1107 62 my $fh = open_file($file, '<');
1108 62 ▶ my @events = grep { ref($_) eq 'HASH' } map { chomp; length ? eval { json_decode($_) } : () } <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 752 0 752
1109 62 close $fh;
1110  
1111 62 return \@events;
1112 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1113  
1114 # _choose_version
1115 # Purpose: pick the trashed version restore should use.
1116 # Entry: $versions newest first; $args has path, repo and maybe date.
1117 # Exit: returns a version path.
1118 # Side effects: raises admin.no_versions, admin.bad_date or
1119 # admin.no_version_on.
1120 9 ▶sub _choose_version :Private {
Subroutine _choose_version: called CC=7 SCAR=19.5
1121 9 my ($self, $versions, $args) = @_;
1122  
1123 9 my ($repo, $path, $date) = @{$args}{qw(repo path date)};
1124 9 ▶ $self->_throw('admin.no_versions', { path => $path, repo => $repo }) unless @{$versions};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$versions;} 1 8 9
1125 8 ▶ return $versions->[0] unless defined $date && length $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date and length $date 4 4 8
Condition66%2/3
defined $date and length $date
!ll&&!rl&&r
404
MC/DC50%1/2
defined $date and length $date
defined $date length $date
Truth table66%2/3
A and B
A= defined $dateB= length $date
ABresult
0X0
100
111
1126  
1127 4 ▶ $self->_throw('admin.bad_date', { date => $date }) unless $date =~ $DATE;
Branch: 1 branch
BranchTrueFalseTotal
unless $date =~ /$DATE/ 1 3 4
1128  
1129 # date_of is undef for a path outside the dated layout; such a version
1130 # never matches a date.
1131 3 ▶ my ($match) = grep { (App::FileSync::Trash::date_of($_) // '') eq $date } @{$versions};
Condition50%1/2
App::FileSync::Trash::date_of($_) // ""
l!l
50
MC/DC0%0/1
App::FileSync::Trash::date_of($_) // ""
App::FileSync::Trash::date_of($_)
Truth table50%1/2
A
A= App::FileSync::Trash::date_of($_)
Aresult
11
00
1132 3 ▶ $self->_throw('admin.no_version_on', { path => $path, date => $date }) unless defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 2 1 3
1133 1 return $match;
1134 429 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1135  
1136 429 1;
1137  
1138 __END__
« blib/lib/App/FileSync/Ignore.pm ↑ Summary blib/lib/App/FileSync/Server.pm »