blib/lib/App/FileSync/CLI.pm

filter: Statementstmt 90.1% 739 / 820 Branchbran 64.1% 181 / 282 Conditioncond 42.1% 104 / 247 MC/DCmcdc 17.0% 25 / 147 Subroutinesub 96.5% 139 / 144 Podpod 100% 3 / 3 totaltotal 72.4% 1191 / 1643 CC 258 SCAR 52.1
CC
258
Cov
76%
_client_init
1654.1
_client_sync
22.2
_admin_log
20.8
CRAP 183.8
↑ Summary blib/lib/App/FileSync/Args.pm »
1 package App::FileSync::CLI;
2  
3 # The three command-line programs: filesync (client), filesync-admin and
4 # filesync-server. Each run_* parses arguments, calls the library and prints;
5 # all text comes from the message catalogue, so this module holds no prose.
6  
7 413 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 413 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 413 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 413 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 413 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 413 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 413 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 413 ▶use List::Util qw(any first);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 413 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 413 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
19 413 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 413 ▶use App::FileSync qw(json_encode format_utc validate_repo_name validate_client_id parse_since parse_until write_atomic);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 413 ▶use App::FileSync::Admin;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 413 ▶use App::FileSync::Args qw(with_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 413 ▶use App::FileSync::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 413 ▶use App::FileSync::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 413 ▶use App::FileSync::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
27 $EXIT_OK $EXIT_FATAL $EXIT_LOCKED $EXIT_WARNINGS $EXIT_DRIFT $EXIT_MAX
28 $DEFAULT_SERVER_ROOT $PROTOCOL_VERSION $ROLE_CLIENT @ROLES $ROOT_MARKER $MAX_ID_LENGTH $TYPE_FILE
29 $TRASH_DAYS $MAX_DELETES $MAX_DELETE_PERCENT
30 413 );
31 413 ▶use App::FileSync::I18N qw(message throw strip_location);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 413 ▶use App::FileSync::IO qw(ensure_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 413 ▶use App::FileSync::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 413 ▶use App::FileSync::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 413 ▶use App::FileSync::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 413 ▶use App::FileSync::Transport::SSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 413 ▶use version; our $VERSION = version->declare('0.002.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # What the marker file says to anyone who finds it. File content, not a
41 # message: it must not change with the reader's language.
42 413 Readonly::Scalar my $MARKER_TEXT => "This directory is a filesync root; do not delete this file.\n";
43  
44 # The client's default local-trash retention, written by init.
45 413 Readonly::Scalar my $LOCAL_TRASH_DAYS => 30;
46  
47 # How many journal events "filesync-admin log" shows by default.
48 413 Readonly::Scalar my $ADMIN_LOG_LIMIT => 20;
49  
50 # Size units for human-readable byte counts.
51 413 Readonly::Scalar my $KIB => 1024;
52 413 Readonly my @SIZE_UNITS => ([ $KIB ** 3, 'GB' ], [ $KIB ** 2, 'MB' ], [ $KIB, 'kB' ]);
53  
54 # The client commands and their handlers.
55 413 Readonly my %CLIENT_COMMANDS => (
56 help => '_client_help',
57 '--help' => '_client_help',
58 version => '_client_version',
59 init => '_client_init',
60 sync => '_client_sync',
61 status => '_client_status',
62 log => '_client_log',
63 settings => '_client_settings',
64 verify => '_client_verify',
65 restore => '_client_restore',
66 snapshot => '_client_snapshot',
67 );
68  
69 # The admin commands; a hashref value is a group of sub-commands.
70 413 Readonly my %ADMIN_COMMANDS => (
71 repo => { create => '_admin_repo_create', list => '_admin_repo_list', show => '_admin_repo_show' },
72 client => { allow => '_admin_client_allow', list => '_admin_client_list', forget => '_admin_client_forget' },
73 log => '_admin_log',
74 verify => '_admin_verify',
75 reindex => '_admin_reindex',
76 restore => '_admin_restore',
77 );
78  
79 # Settings whose empty value means "the transport's default", and what to
80 # show instead of a blank column.
81 413 Readonly my %BLANK_SETTING => (
82 log_file => 'cli.settings.stderr',
83 ssh_identity => 'cli.settings.ssh_default',
84 ssh_known_hosts => 'cli.settings.ssh_default',
85 server_command => 'cli.settings.server_default',
86 );
87  
88 # Journal fields "filesync-admin log" prints as key=value, in this order.
89 413 Readonly my @EVENT_FIELDS => qw(from winner loser_name exit trash sha256);
90 413 Readonly my @EVENT_COUNTERS => qw(actions deletions conflicts pulled pushed deleted);
91 413 Readonly my %EVENT_LABEL => (loser_name => 'loser');
92  
93 =encoding utf8
94  
95 =head1 NAME
96  
97 App::FileSync::CLI - the filesync, filesync-admin and filesync-server commands
98  
99 =head1 SYNOPSIS
100  
101 # bin/filesync
102 use App::FileSync::CLI;
103 exit App::FileSync::CLI::run_client(@ARGV);
104  
105 =head1 DESCRIPTION
106  
107 Each C<run_*> function runs one command line and returns the exit code; it
108 never dies. Errors are reported through the logger (or standard error if the
109 settings that configure the logger are themselves broken), without Perl file
110 and line numbers. Text output goes to standard output; C<--json> prints one
111 JSON document instead.
112  
113 Client exit codes: 0 ok, 1 fatal, 2 another run holds the lock, 3 completed
114 with warnings, 4 verification found drift. Admin: 0, 1 and 4. Server: 0
115 after a session that finished, 1 after any other (a bad argument, a refused
116 forced command, a protocol error, or another session holding the repo lock;
117 the journal records a lock refusal as exit 2, but the process exits 1).
118  
119 =head1 FUNCTIONS
120  
121 =head2 run_client
122  
123 Documented with L</run_server> below.
124  
125 =head2 run_admin
126  
127 Documented with L</run_server> below.
128  
129 =head2 run_server
130  
131 Purpose: run C<filesync>, C<filesync-admin> or C<filesync-server>.
132  
133 Args: the command line, without the program name.
134  
135 Returns: the exit code.
136  
137 Side Effects: whatever the command does; prints to standard output and logs
138 to standard error. C<run_server> speaks the protocol on standard input and
139 output and refuses to run on a terminal unless C<FILESYNC_ALLOW_TTY> is set.
140  
141 Usage:
142  
143 exit App::FileSync::CLI::run_client(@ARGV);
144  
145 =head3 EXAMPLE
146  
147 my $code = App::FileSync::CLI::run_client('sync', '--repo', 'docs', '--json');
148 my $code = App::FileSync::CLI::run_admin('--root', '/srv/filesync', 'repo', 'list');
149  
150 =head3 API SPECIFICATION
151  
152 =head4 INPUT
153  
154 [ { name => 'args', type => 'arrayref' } ] # passed as a list
155  
156 =head4 OUTPUT
157  
158 { type => 'integer', min => 0, max => 255 }
159  
160 =head3 MESSAGES
161  
162 Key Meaning Resolution
163 cli.unknown_command the command is not known run "filesync help"
164 cli.usage.* the options could not be parsed see the usage line
165 cli.unexpected extra arguments were given remove them
166 cli.required a required argument is missing supply it
167 cli.no_repos no repos are configured filesync init
168 cli.no_repo the named repo is not configured check --repo
169 cli.need_repo several repos and no --repo pass --repo
170 cli.init.* init was given an unusable value see the message
171 cli.server.* the server was started wrongly fix authorized_keys
172  
173 =head3 FORMAL SPECIFICATION
174  
175 run(args) = dispatch(args) if dispatch succeeds
176 run(args) = 1 ∧ log(strip_location(error)) if it raises
177  
178 =cut
179  
180 84 ▶sub run_client {
Subroutine run_client: called CC=1 SCAR=0.0
Pod documented
181 84 my (@args) = @_;
182  
183 84 ▶ return _run(sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
184 84 my ($command, @rest) = @_;
185 84 ▶ return _dispatch(\%CLIENT_COMMANDS, 'filesync', $command // 'help', @rest);
Condition50%1/2
$command // "help"
l!l
840
MC/DC0%0/1
$command // "help"
$command
Truth table50%1/2
A
A= $command
Aresult
11
00
186 84 }, @args);
187 }
188  
189 59 ▶sub run_admin {
Subroutine run_admin: called CC=1 SCAR=0.0
Pod documented
190 59 my (@args) = @_;
191  
192 59 return _run(\&_admin_dispatch, @args);
193 }
194  
195 268 ▶sub run_server {
Subroutine run_server: called CC=1 SCAR=0.0
Pod documented
196 268 my (@args) = @_;
197  
198 268 return _run(\&_server_main, @args);
199 }
200  
201 # _run
202 # Purpose: run a dispatcher, turning any error into exit code 1.
203 # Entry: $code takes the arguments and returns an exit code.
204 # Exit: returns the exit code.
205 # Side effects: reports the error without its Perl location.
206 411 ▶sub _run :Private {
Subroutine _run: called CC=2 SCAR=6.9
207 411 my ($code, @args) = @_;
208  
209 # A handler returns its exit code; one that died returns nothing.
210 411 my $exit = eval { $code->(@args) };
211 411 ▶ unless (defined $exit) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exit) 21 390 411
212 21 _report_error(strip_location($@));
213 21 $exit = $EXIT_FATAL;
214 }
215  
216 411 return $exit;
217 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 # _report_error
220 # Purpose: show an error to the user.
221 # Entry: $error is the text.
222 # Exit: returns 1 if logged, 0 if printed directly.
223 # Side effects: logs it; if the logger cannot be built (an unusable settings
224 # file), prints to standard error, so the error that says so is
225 # never hidden.
226 21 ▶sub _report_error :Private {
Subroutine _report_error: called CC=2 SCAR=7.5
227 21 my ($error) = @_;
228  
229 21 my $logger = eval { App::FileSync::Settings::logger() };
230 21 ▶ if ($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 21 0 21
231 21 $logger->error($error);
232 21 return 1;
233 }
234  
235 0 print STDERR "$error\n";
236 0 return 0;
237 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 # _dispatch
240 # Purpose: run one command from a table.
241 # Entry: $table maps command names to handler names.
242 # Exit: returns the handler's exit code.
243 # Side effects: raises cli.unknown_command.
244 84 ▶sub _dispatch :Private {
Subroutine _dispatch: called CC=2 SCAR=7.0
245 84 my ($table, $program, $command, @args) = @_;
246  
247 84 ▶ my $handler = $table->{$command} or throw('cli.unknown_command', { command => $command, program => $program });
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $table->{$command} or throw("cli.unknown_command", {"command", $command, "program", $program}) 84 0 84
248 84 my $code = __PACKAGE__->can($handler);
249 84 return $code->(@args);
250 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 # _say
253 # Purpose: print one catalogued line.
254 # Entry: $key and its arguments.
255 # Exit: returns 1.
256 # Side effects: prints to standard output.
257 252 ▶sub _say :Private {
Subroutine _say: called CC=1 SCAR=0.0
258 252 my ($key, $args) = @_;
259  
260 252 print message($key, $args), "\n";
261 252 return 1;
262 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 # _options
265 # Purpose: parse options for a command, with its usage line on failure.
266 # Entry: $args is the argument list (consumed); $spec is the
267 # Getopt::Long specification; $command names the usage key.
268 # Exit: returns 1.
269 # Side effects: removes the options from @$args; raises cli.usage.
270 399 ▶sub _options :Private {
Subroutine _options: called CC=2 SCAR=7.0
271 399 my ($args, $spec, $command) = @_;
272  
273 399 ▶ GetOptionsFromArray($args, %{$spec}) or throw('cli.usage', { usage => message("cli.usage.$command") });
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray($args, %{$spec;}) or throw("cli.usage", {"usage", message("cli.usage.$command")}) 399 0 399
274 399 return 1;
275 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 # _no_more
278 # Purpose: refuse arguments a command does not take.
279 # Entry: $args holds what is left after parsing.
280 # Exit: returns 1.
281 # Side effects: raises cli.unexpected.
282 349 ▶sub _no_more :Private {
Subroutine _no_more: called CC=2 SCAR=6.9
283 349 my ($args, $command) = @_;
284  
285 349 ▶ throw('cli.unexpected', { command => $command, args => "@{$args}" }) if @{$args};
Branch: 1 branch
BranchTrueFalseTotal
if @{$args;} 1 348 349
286 348 return 1;
287 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
288  
289 # _require
290 # Purpose: insist on a non-empty value.
291 # Entry: $what names it in the message.
292 # Exit: returns the value.
293 # Side effects: raises cli.required.
294 840 ▶sub _require :Private {
Subroutine _require: called CC=3 SCAR=11.0
295 840 my ($value, $command, $what) = @_;
296  
297 840 ▶ throw('cli.required', { command => $command, what => $what }) unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 1 839 840
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
10839
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
298 839 return $value;
299 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 # _repos
302 # Purpose: the repos a multi-repo command works on.
303 # Entry: $given are --repo values (maybe none); $require_any says
304 # whether an empty configuration is an error.
305 # Exit: returns an arrayref of names: the given ones, or all, sorted.
306 # Side effects: raises cli.no_repos or cli.no_repo.
307 67 ▶sub _repos :Private {
Subroutine _repos: called CC=6 SCAR=18.0
308 67 my ($config, $given, $require_any) = @_;
309  
310 67 ▶ throw('cli.no_repos') if $require_any && !%{ $config->{repos} };
Branch: 1 branch
BranchTrueFalseTotal
if $require_any and not %{$config->{"repos"};} 0 67 67
Condition66%2/3
$require_any and not %{$config->{"repos"};}
!ll&&!rl&&r
3640
MC/DC0%0/2
$require_any and not %{$config->{"repos"};}
$require_any not %{$config->{"repos"};}
Truth table66%2/3
A and not(B)
A= $require_anyB= not %{$config->{"repos"};}
ABresult
0X0
100
111
311 67 ▶ my @names = @{$given} ? @{$given} : sort keys %{ $config->{repos} };
Branch: 1 branch
BranchTrueFalseTotal
@{$given;} ? : 63 4 67
312 67 ▶ $config->{repos}{$_} or throw('cli.no_repo', { repo => $_ }) for @names;
Branch: 1 branch
BranchTrueFalseTotal
$config->{"repos"}{$_} or throw("cli.no_repo", {"repo", $_}) 66 3 69
313  
314 64 return \@names;
315 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 # _one_repo
318 # Purpose: the repo a single-repo command works on.
319 # Entry: $name is --repo, maybe undef.
320 # Exit: returns the name; the only repo when there is just one.
321 # Side effects: raises cli.no_repos, cli.need_repo or cli.no_repo.
322 9 ▶sub _one_repo :Private {
Subroutine _one_repo: called CC=6 SCAR=19.6
323 9 my ($config, $name, $command) = @_;
324  
325 9 my @names = sort keys %{ $config->{repos} };
326 9 ▶ throw('cli.no_repos') unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 9 9
327 9 ▶ $name //= $names[0] if @names == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names == 1 9 0 9
Condition33%1/3
$name //= $names[0]
l!l&&r!l&&!r
900
MC/DC0%0/2
$name //= $names[0]
$name $names[0]
Truth table33%1/3
A //= B
A= $nameB= $names[0]
ABresult
1X1
011
000
328 9 ▶ throw('cli.need_repo', { command => $command }) unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 9 9
329 9 ▶ throw('cli.no_repo', { repo => $name }) unless $config->{repos}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"repos"}{$name} 0 9 9
330  
331 9 return $name;
332 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
333  
334 # _client
335 # Purpose: a Client for one configured repo.
336 # Entry: %extra is passed to Client->new.
337 # Exit: returns the client.
338 # Side effects: none until the client is used.
339 75 ▶sub _client :Private {
Subroutine _client: called CC=1 SCAR=0.0
340 75 my ($config, $name, %extra) = @_;
341  
342 75 return App::FileSync::Client->new(config => $config, name => $name, %extra);
343 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
344  
345 # ------------------------------------------------------------------ server
346  
347 # _server_main
348 # Purpose: filesync-server: serve one session on stdin/stdout.
349 # Entry: @args is the forced command's arguments.
350 # Exit: returns 0 when the session finished, 1 when it failed.
351 # Side effects: runs the session. Under sshd the root comes from the
352 # forced command (or FILESYNC_ROOT), never from the client,
353 # and SSH_ORIGINAL_COMMAND must agree with the pinned values.
354 268 ▶sub _server_main :Private {
Subroutine _server_main: called CC=11 SCAR=24.7
355 268 my (@args) = @_;
356  
357 # --forced is accepted (older authorized_keys lines carry it) but implies
358 # nothing: SSH_ORIGINAL_COMMAND is checked whenever sshd sets it.
359 268 my %opt;
360 _options(\@args, {
361 'root=s' => \$opt{root},
362 'repo=s' => \$opt{repo},
363 'client=s' => \$opt{client},
364 'role=s' => \$opt{role},
365 'proto=i' => \$opt{proto},
366 'forced' => \$opt{forced},
367 268 }, 'server');
368 268 _no_more(\@args, 'filesync-server');
369  
370 268 ▶ my $root = first { defined && length } $opt{root}, $ENV{FILESYNC_ROOT}, $DEFAULT_SERVER_ROOT;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 268 0 268
371 268 _require($opt{$_}, 'filesync-server', "--$_") for qw(repo client role);
372 268 ▶ throw('cli.server.bad_role', { role => $opt{role} }) unless any { $_ eq $opt{role} } @ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $opt{"role"}; } , @App::FileSync::CLI::ROLES) 0 268 268
373 268 ▶ throw('cli.server.proto', { proto => $opt{proto} }) if defined $opt{proto} && $opt{proto} != $PROTOCOL_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"proto"} and $opt{"proto"} != $App::FileSync::CLI::PROTOCOL_VERSION 0 268 268
Condition66%2/3
defined $opt{"proto"} and $opt{"proto"} != $App::FileSync::CLI::PROTOCOL_VERSION
!ll&&!rl&&r
256120
MC/DC0%0/2
defined $opt{"proto"} and $opt{"proto"} != $App::FileSync::CLI::PROTOCOL_VERSION
defined $opt{"proto"} $opt{"proto"} != $App::FileSync::CLI::PROTOCOL_VERSION
Truth table66%2/3
A and B
A= defined $opt{"proto"}B= $opt{"proto"} != $App::FileSync::CLI::PROTOCOL_VERSION
ABresult
0X0
100
111
374  
375 268 my $original = $ENV{SSH_ORIGINAL_COMMAND};
376 268 ▶ if (defined $original && length $original) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $original and length $original) 12 256 268
Condition66%2/3
defined $original and length $original
!ll&&!rl&&r
256012
MC/DC50%1/2
defined $original and length $original
defined $original length $original
Truth table66%2/3
A and B
A= defined $originalB= length $original
ABresult
0X0
100
111
377 my ($ok, $reason) = App::FileSync::Server::validate_forced_command(
378 12 $original, { map({ $_ => $opt{$_} } qw(repo client role)), root => $root },
379 );
380 12 ▶ throw('cli.server.forced', { reason => $reason }) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 9 12
381 }
382  
383 265 ▶ throw('cli.server.tty') if -t STDIN && !$ENV{FILESYNC_ALLOW_TTY};
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN and not $ENV{"FILESYNC_ALLOW_TTY"} 0 265 265
Condition33%1/3
-t STDIN and not $ENV{"FILESYNC_ALLOW_TTY"}
!ll&&!rl&&r
26500
MC/DC0%0/2
-t STDIN and not $ENV{"FILESYNC_ALLOW_TTY"}
-t STDIN not $ENV{"FILESYNC_ALLOW_TTY"}
Truth table33%1/3
A and not(B)
A= -t STDINB= not $ENV{"FILESYNC_ALLOW_TTY"}
ABresult
0X0
100
111
384  
385 my $server = App::FileSync::Server->new(
386 root => $root,
387 265 (map { $_ => $opt{$_} } qw(repo client role)),
388 connection => App::FileSync::Protocol->new(in => \*STDIN, out => \*STDOUT),
389 logger => App::FileSync::Settings::logger(),
390 );
391  
392 265 ▶ return $server->run() ? $EXIT_OK : $EXIT_FATAL;
Branch: 1 branch
BranchTrueFalseTotal
$server->run ? : 194 71 265
393 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
394  
395 # ------------------------------------------------------------------ client
396  
397 # _client_help / _client_version
398 # Purpose: print the usage summary, or the version.
399 # Entry: none.
400 # Exit: returns 0.
401 # Side effects: prints.
402 0 ▶sub _client_help :Private {
Subroutine _client_help: not called CC=1 SCAR=6.9
403 0 print message('cli.help.client');
404 0 return $EXIT_OK;
405 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
406  
407 1 ▶sub _client_version :Private {
Subroutine _client_version: called CC=1 SCAR=0.0
408 1 _say('cli.version', { version => $App::FileSync::VERSION, proto => $PROTOCOL_VERSION });
409 1 return $EXIT_OK;
410 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
411  
412 # _client_init
413 # Purpose: filesync init: register a repo in the client configuration.
414 # Entry: @args are init's options.
415 # Exit: returns 0.
416 # Side effects: creates the sync root and its marker (and a local server
417 # root); writes the client configuration. Every id that will
418 # reach the server is checked here, so a bad one is reported
419 # at init rather than as a refusal at the first sync.
420 1 ▶sub _client_init :Private {
Subroutine _client_init: called CC=43 SCAR=74.1
421 1 my (@args) = @_;
422  
423 # init has defaults that are not obvious, the client id above all, so
424 # --help prints them instead of failing as an unknown option.
425 1 ▶ if (grep { $_ eq '--help' || $_ eq '-h' } @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "--help" or $_ eq "-h";} @args) 1 0 1
Condition33%1/3
$_ eq '--help' || $_ eq '-h'
l!l&&r!l&&!r
100
MC/DC0%0/2
$_ eq '--help' || $_ eq '-h'
$_ eq '--help' $_ eq '-h'
Truth table33%1/3
A || B
A= $_ eq '--help'B= $_ eq '-h'
ABresult
1X1
011
000
426 1 print message('cli.help.init');
427 1 return $EXIT_OK;
428 }
429  
430 0 my %opt;
431 _options(\@args, {
432 0 (map { ("$_=s" => \$opt{$_}) } qw(repo root server remote-repo id identity role transport server-root config known-hosts server-command snapshot-mode)),
433 'port=i' => \$opt{port},
434 'accept-host-key' => \$opt{accept},
435 0 }, 'init');
436  
437 0 my $repo = _require($opt{repo}, 'init', '--repo');
438 0 my $root = _require($opt{root}, 'init', '--root');
439 0 _check_id(\&validate_repo_name, $repo);
440 0 ▶ _check_id(\&validate_repo_name, $opt{'remote-repo'}) if defined $opt{'remote-repo'} && length $opt{'remote-repo'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"remote-repo"} and length $opt{"remote-repo"} 0 0 0
Condition0%0/3
defined $opt{"remote-repo"} and length $opt{"remote-repo"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"remote-repo"} and length $opt{"remote-repo"}
defined $opt{"remote-repo"} length $opt{"remote-repo"}
Truth table0%0/3
A and B
A= defined $opt{"remote-repo"}B= length $opt{"remote-repo"}
ABresult
0X0
100
111
441 0 ▶ _check_id(\&validate_client_id, $opt{id}) if defined $opt{id} && length $opt{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"id"} and length $opt{"id"} 0 0 0
Condition0%0/3
defined $opt{"id"} and length $opt{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"id"} and length $opt{"id"}
defined $opt{"id"} length $opt{"id"}
Truth table0%0/3
A and B
A= defined $opt{"id"}B= length $opt{"id"}
ABresult
0X0
100
111
442  
443 0 ▶ my $role = $opt{role} // $ROLE_CLIENT;
Condition0%0/3
$opt{"role"} // $App::FileSync::CLI::ROLE_CLIENT
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"role"} // $App::FileSync::CLI::ROLE_CLIENT
$opt{"role"} $App::FileSync::CLI::ROLE_CLIENT
Truth table0%0/3
A // B
A= $opt{"role"}B= $App::FileSync::CLI::ROLE_CLIENT
ABresult
1X1
011
000
444 0 ▶ throw('cli.init.bad_role', { role => $role }) unless any { $_ eq $role } @ROLES;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $role; } , @App::FileSync::CLI::ROLES) 0 0 0
445  
446 0 my $mode = $opt{'snapshot-mode'};
447 0 ▶ throw('cli.init.bad_mode', { mode => $mode })
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode and length $mode and $mode ne "hardlink" and $mode ne "copy" 0 0 0
Condition0%0/9
defined $mode and length $mode and $mode ne "hardlink" and $mode ne "copy"
!ll&&!rl&&r
000
defined $mode and length $mode and $mode ne "hardlink"
!ll&&!rl&&r
000
defined $mode and length $mode
!ll&&!rl&&r
000
MC/DC0%0/4
defined $mode and length $mode and $mode ne "hardlink" and $mode ne "copy"
defined $mode length $mode $mode ne "hardlink" $mode ne "copy"
Truth table0%0/5
A and B and C and D
A= defined $modeB= length $modeC= $mode ne "hardlink"D= $mode ne "copy"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
448 if defined $mode && length $mode && $mode ne 'hardlink' && $mode ne 'copy';
449  
450 0 my ($server, $server_root, $known) = @opt{qw(server server-root known-hosts)};
451 0 ▶ my $use = $opt{transport} || ($server_root ? 'local' : 'ssh');
Branch: 1 branch
BranchTrueFalseTotal
$server_root ? : 0 0 0
Condition0%0/3
$opt{'transport'} || ($server_root ? 'local' : 'ssh')
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'transport'} || ($server_root ? 'local' : 'ssh')
$opt{'transport'} ($server_root ? 'local' : 'ssh')
Truth table0%0/3
A || B
A= $opt{'transport'}B= ($server_root ? 'local' : 'ssh')
ABresult
1X1
011
000
452 0 ▶ throw('cli.init.bad_transport', { transport => $use }) unless $use eq 'local' || $use eq 'ssh';
Branch: 1 branch
BranchTrueFalseTotal
unless $use eq "local" or $use eq "ssh" 0 0 0
Condition0%0/3
$use eq "local" or $use eq "ssh"
l!l&&r!l&&!r
000
MC/DC0%0/2
$use eq "local" or $use eq "ssh"
$use eq "local" $use eq "ssh"
Truth table0%0/3
A or B
A= $use eq "local"B= $use eq "ssh"
ABresult
1X1
011
000
453 0 ▶ throw('cli.init.need_server_root') if $use eq 'local' && !(defined $server_root && length $server_root);
Branch: 1 branch
BranchTrueFalseTotal
if $use eq "local" and not defined $server_root && length $server_root 0 0 0
Condition0%0/6
$use eq "local" and not defined $server_root && length $server_root
!ll&&!rl&&r
000
defined $server_root && length $server_root
!ll&&!rl&&r
000
MC/DC0%0/3
$use eq "local" and not defined $server_root && length $server_root
$use eq "local" defined $server_root length $server_root
Truth table0%0/4
A and not(B && C)
A= $use eq "local"B= defined $server_rootC= length $server_root
ABCresult
0XX0
1110
10X1
1101
454 0 ▶ throw('cli.init.need_server') if $use eq 'ssh' && !(defined $server && length $server);
Branch: 1 branch
BranchTrueFalseTotal
if $use eq "ssh" and not defined $server && length $server 0 0 0
Condition0%0/6
$use eq "ssh" and not defined $server && length $server
!ll&&!rl&&r
000
defined $server && length $server
!ll&&!rl&&r
000
MC/DC0%0/3
$use eq "ssh" and not defined $server && length $server
$use eq "ssh" defined $server length $server
Truth table0%0/4
A and not(B && C)
A= $use eq "ssh"B= defined $serverC= length $server
ABCresult
0XX0
1110
10X1
1101
455 0 ▶ throw('cli.init.bad_server', { server => $server }) if $use eq 'ssh' && !App::FileSync::Transport::SSH::valid_server($server);
Branch: 1 branch
BranchTrueFalseTotal
if $use eq "ssh" and not App::FileSync::Transport::SSH::valid_server($server) 0 0 0
Condition0%0/3
$use eq "ssh" and not App::FileSync::Transport::SSH::valid_server($server)
!ll&&!rl&&r
000
MC/DC0%0/2
$use eq "ssh" and not App::FileSync::Transport::SSH::valid_server($server)
$use eq "ssh" not App::FileSync::Transport::SSH::valid_server($server)
Truth table0%0/3
A and not(B)
A= $use eq "ssh"B= not App::FileSync::Transport::SSH::valid_server($server)
ABresult
0X0
100
111
456 0 ▶ throw('cli.init.no_known_hosts', { path => $known }) if defined $known && length $known && !-f $known;
Branch: 1 branch
BranchTrueFalseTotal
if defined $known and length $known and not -f $known 0 0 0
Condition0%0/6
defined $known and length $known and not -f $known
!ll&&!rl&&r
000
defined $known and length $known
!ll&&!rl&&r
000
MC/DC0%0/3
defined $known and length $known and not -f $known
defined $known length $known not -f $known
Truth table0%0/4
A and B and not(C)
A= defined $knownB= length $knownC= not -f $known
ABCresult
0XX0
10X0
1100
1111
457 0 ▶ throw('cli.init.accept_ssh_only') if $opt{accept} && $use ne 'ssh';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"accept"} and $use ne "ssh" 0 0 0
Condition0%0/3
$opt{"accept"} and $use ne "ssh"
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"accept"} and $use ne "ssh"
$opt{"accept"} $use ne "ssh"
Truth table0%0/3
A and B
A= $opt{"accept"}B= $use ne "ssh"
ABresult
0X0
100
111
458  
459 0 ensure_dir($root);
460 0 ▶ throw('cli.init.bad_root', { root => $root }) unless -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 0 0 0
461 0 $root = abs_path($root);
462  
463 0 my $marker = File::Spec->catfile($root, $ROOT_MARKER);
464 0 ▶ write_atomic($marker, $MARKER_TEXT) unless -e $marker;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $marker 0 0 0
465  
466 0 ▶ if (defined $server_root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server_root) 0 0 0
467 0 ensure_dir($server_root);
468 0 $server_root = abs_path($server_root);
469 }
470  
471 0 my $config = App::FileSync::Config::load_client($opt{config});
472 0 ▶ $config->{client_id} = _default_client_id() unless defined $config->{client_id} && length $config->{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"client_id"} and length $config->{"client_id"} 0 0 0
Condition0%0/3
defined $config->{"client_id"} and length $config->{"client_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"client_id"} and length $config->{"client_id"}
defined $config->{"client_id"} length $config->{"client_id"}
Truth table0%0/3
A and B
A= defined $config->{"client_id"}B= length $config->{"client_id"}
ABresult
0X0
100
111
473 0 ▶ $config->{client_id} = $opt{id} if defined $opt{id} && length $opt{id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"id"} and length $opt{"id"} 0 0 0
Condition0%0/3
defined $opt{"id"} and length $opt{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"id"} and length $opt{"id"}
defined $opt{"id"} length $opt{"id"}
Truth table0%0/3
A and B
A= defined $opt{"id"}B= length $opt{"id"}
ABresult
0X0
100
111
474  
475 my %optional = (
476 server_root => $server_root,
477 server => $server,
478 port => $opt{port},
479 identity => $opt{identity},
480 known_hosts => $known,
481 host_key_checking => $opt{accept} ? 'accept-new' : undef,
482 0 ▶ server_command => $opt{'server-command'},
Branch: 1 branch
BranchTrueFalseTotal
$opt{'accept'} ? : 0 0 0
483 snapshot_mode => $mode,
484 );
485 $config->{repos}{$repo} = {
486 root => $root,
487 remote_repo => (defined $opt{'remote-repo'} && length $opt{'remote-repo'}) ? $opt{'remote-repo'} : $repo,
488 role => $role,
489 transport => $use,
490 local_trash_days => $LOCAL_TRASH_DAYS,
491 0 ▶ map { $_ => $optional{$_} } grep { defined $optional{$_} && length $optional{$_} } keys %optional,
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'remote-repo'} && length $opt{'remote-repo'} ? : 0 0 0
Condition0%0/6
defined $opt{"remote-repo"} and length $opt{"remote-repo"}
!ll&&!rl&&r
000
defined $optional{$_} && length $optional{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt{"remote-repo"} and length $opt{"remote-repo"}
defined $opt{"remote-repo"} length $opt{"remote-repo"}
MC/DC0%0/2
defined $optional{$_} && length $optional{$_}
defined $optional{$_} length $optional{$_}
Truth table0%0/3
A and B
A= defined $opt{"remote-repo"}B= length $opt{"remote-repo"}
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= defined $optional{$_}B= length $optional{$_}
ABresult
0X0
100
111
492 };
493  
494 0 my $path = App::FileSync::Config::save_client($config, $opt{config});
495  
496 0 _say('cli.init.done', { repo => $repo });
497 0 _say('cli.init.client_id', { id => $config->{client_id} });
498 0 _say('cli.init.root', { root => $root });
499 0 _say('cli.init.transport', { transport => $use });
500 0 ▶ if ($use eq 'ssh') {
Branch: 1 branch
BranchTrueFalseTotal
if ($use eq "ssh") 0 0 0
501 0 _say('cli.init.server', { server => $server });
502 0 ▶ _say($opt{accept} ? 'cli.init.host_key_tofu' : 'cli.init.host_key_strict');
Branch: 1 branch
BranchTrueFalseTotal
$opt{'accept'} ? : 0 0 0
503 }
504 0 _say('cli.init.config', { path => $path });
505  
506 0 return $EXIT_OK;
507 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
508  
509 # _check_id
510 # Purpose: validate an id given to init.
511 # Entry: $validate is validate_repo_name or validate_client_id.
512 # Exit: returns 1.
513 # Side effects: raises cli.init.bad_id with the validator's reason.
514 0 ▶sub _check_id :Private {
Subroutine _check_id: not called CC=2 SCAR=17.9
515 0 my ($validate, $value) = @_;
516  
517 0 my ($ok, $reason) = $validate->($value);
518 0 ▶ throw('cli.init.bad_id', { reason => $reason }) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
519 0 return 1;
520 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
521  
522 # _client_sync
523 # Purpose: filesync sync: sync each selected repo.
524 # Entry: @args are sync's options.
525 # Exit: returns 1 if any repo failed, else 3 if any warned, else 2 if
526 # any was locked, else 0.
527 # Side effects: syncs; prints a summary per repo (or JSON); logs errors.
528 # One repo failing does not stop the others.
529 13 ▶sub _client_sync :Private {
Subroutine _client_sync: called CC=13 SCAR=31.0
530 13 my (@args) = @_;
531  
532 13 my (%opt, @given);
533 _options(\@args, {
534 'repo=s@' => \@given,
535 91 (map { ($_ => \$opt{$_}) } qw(dry-run force verbose quiet json no-prune no-delta)),
536 'config=s' => \$opt{config},
537 13 }, 'sync');
538  
539 13 my $config = App::FileSync::Config::load_client($opt{config});
540 13 my $names = _repos($config, \@given, 1);
541 11 my $logger = App::FileSync::Settings::logger(verbose => $opt{verbose}, quiet => $opt{quiet});
542 11 my $client_opts = { map { (my $key = $_) =~ tr/-/_/; $key => $opt{$_} } qw(dry-run force no-prune no-delta verbose) };
543  
544 11 my (@results, @errors, %seen);
545 11 for my $name (@{$names}) {
546 11 my $result = eval { _client($config, $name, opts => $client_opts, logger => $logger)->sync() };
547 11 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 11 11
548 0 my $error = strip_location($@);
549 0 push @errors, { repo => $name, error => $error };
550 0 ▶ $logger->error(message('cli.repo_error', { repo => $name, error => $error })) unless $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"json"} 0 0 0
551 0 $seen{$EXIT_FATAL} = 1;
552 0 next;
553 }
554  
555 11 push @results, $result;
556 11 ▶ my $exit = $result->{exit} || $EXIT_OK;
Condition33%1/3
$result->{'exit'} || $App::FileSync::CLI::EXIT_OK
l!l&&r!l&&!r
0011
MC/DC0%0/2
$result->{'exit'} || $App::FileSync::CLI::EXIT_OK
$result->{'exit'} $App::FileSync::CLI::EXIT_OK
Truth table33%1/3
A || B
A= $result->{'exit'}B= $App::FileSync::CLI::EXIT_OK
ABresult
1X1
011
000
557 11 $seen{$exit} = 1;
558 11 ▶ if ($exit == $EXIT_LOCKED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == $App::FileSync::CLI::EXIT_LOCKED) 0 11 11
559 0 ▶ $logger->warn(message('cli.repo_error', { repo => $name, error => $result->{error} })) unless $opt{json} || $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"json"} or $opt{"quiet"} 0 0 0
Condition0%0/3
$opt{"json"} or $opt{"quiet"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"json"} or $opt{"quiet"}
$opt{"json"} $opt{"quiet"}
Truth table0%0/3
A or B
A= $opt{"json"}B= $opt{"quiet"}
ABresult
1X1
011
000
560 0 next;
561 }
562  
563 11 ▶ _print_sync($name, $result, \%opt) unless $opt{json} || ($opt{quiet} && _idle($result));
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"json"} or $opt{"quiet"} and _idle($result) 8 3 11
Condition83%5/6
$opt{"json"} or $opt{"quiet"} and _idle($result)
l!l&&r!l&&!r
038
$opt{"quiet"} and _idle($result)
!ll&&!rl&&r
623
MC/DC66%2/3
$opt{"json"} or $opt{"quiet"} and _idle($result)
$opt{"json"} $opt{"quiet"} _idle($result)
Truth table75%3/4
A or B and C
A= $opt{"json"}B= $opt{"quiet"}C= _idle($result)
ABCresult
1XX1
0111
00X0
0100
564 }
565  
566 11 ▶ print json_encode({ results => \@results, errors => \@errors }), "\n" if $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"json"} 0 11 11
567  
568 # Severity order, not numeric order: a failure outranks warnings, which
569 # outrank a lock refusal (nothing went wrong; it just did not run).
570 11 ▶ return (first { $seen{$_} } $EXIT_FATAL, $EXIT_WARNINGS, $EXIT_LOCKED) // $EXIT_OK;
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
&first(sub { $seen{$_}; } , $App::FileSync::CLI::EXIT_FATAL, $App::FileSync::CLI::EXIT_WARNINGS, $App::FileSync::CLI::EXIT_LOCKED) // $App::FileSync::CLI::EXIT_OK
l!l&&r!l&&!r
0110
MC/DC0%0/2
&first(sub { $seen{$_}; } , $App::FileSync::CLI::EXIT_FATAL, $App::FileSync::CLI::EXIT_WARNINGS, $App::FileSync::CLI::EXIT_LOCKED) // $App::FileSync::CLI::EXIT_OK
&first(sub { $seen{$_}; } , $App::FileSync::CLI::EXIT_FATAL, $App::FileSync::CLI::EXIT_WARNINGS, $App::FileSync::CLI::EXIT_LOCKED) $App::FileSync::CLI::EXIT_OK
Truth table33%1/3
A // B
A= &first(sub { $seen{$_}; } , $App::FileSync::CLI::EXIT_FATAL, $App::FileSync::CLI::EXIT_WARNINGS, $App::FileSync::CLI::EXIT_LOCKED)B= $App::FileSync::CLI::EXIT_OK
ABresult
1X1
011
000
571 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
572  
573 # _idle
574 # Purpose: did a sync do nothing at all? --quiet prints no report for
575 # such a run, so a cron job that finds nothing to do is silent.
576 # Entry: $result is a sync result.
577 # Exit: returns true when nothing was planned, moved, deleted,
578 # conflicted or skipped and no bytes crossed the wire.
579 # Side effects: none.
580 5 ▶sub _idle :Private {
Subroutine _idle: called CC=4 SCAR=13.9
581 5 my ($result) = @_;
582  
583 # A dry run moves nothing, so its planned actions count as activity too.
584 5 ▶ my $summary = $result->{summary} || {};
Condition50%1/2
$result->{'summary'} || {}
l!l
50
MC/DC0%0/1
$result->{'summary'} || {}
$result->{'summary'}
Truth table50%1/2
A
A= $result->{'summary'}
Aresult
11
00
585 5 ▶ return !@{ $result->{actions} || [] }
Condition100%2/2
$result->{'actions'} || []
l!l
23
MC/DC100%1/1
$result->{'actions'} || []
$result->{'actions'}
Truth table100%2/2
A
A= $result->{'actions'}
Aresult
11
00
586 5 ▶ && !any { $summary->{$_} } qw(pulled pushed deleted conflicts skipped delta_pulled delta_pushed bytes_sent bytes_received);
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
@{$result->{'actions'} or [];} || &any(sub { $summary->{$_}; } , 'pulled', 'pushed', 'deleted', 'conflicts', 'skipped', 'delta_pulled', 'delta_pushed', 'bytes_sent', 'bytes_received')
l!l&&r!l&&!r
113
MC/DC100%2/2
@{$result->{'actions'} or [];} || &any(sub { $summary->{$_}; } , 'pulled', 'pushed', 'deleted', 'conflicts', 'skipped', 'delta_pulled', 'delta_pushed', 'bytes_sent', 'bytes_received')
@{$result->{'actions'} or [];} &any(sub { $summary->{$_}; } , 'pulled', 'pushed', 'deleted', 'conflicts', 'skipped', 'delta_pulled', 'delta_pushed', 'bytes_sent', 'bytes_received')
Truth table100%3/3
A || B
A= @{$result->{'actions'} or [];}B= &any(sub { $summary->{$_}; } , 'pulled', 'pushed', 'deleted', 'conflicts', 'skipped', 'delta_pulled', 'delta_pushed', 'bytes_sent', 'bytes_received')
ABresult
1X1
011
000
587 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
588  
589 # _print_sync
590 # Purpose: the text report of one repo's sync.
591 # Entry: $result is a sync result; $opt the parsed options.
592 # Exit: returns 1.
593 # Side effects: prints the planned actions (dry run), the counts, the
594 # transfer accounting (a dry run sends no payloads, so it has
595 # none) and, unless --quiet, the warnings.
596 8 ▶sub _print_sync :Private {
Subroutine _print_sync: called CC=9 SCAR=22.0
597 8 my ($name, $result, $opt) = @_;
598  
599 8 ▶ if ($opt->{'dry-run'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"dry-run"}) 2 6 8
600 2 ▶ for my $action (@{ $result->{actions} || [] }) {
Condition50%1/2
$result->{'actions'} || []
l!l
20
MC/DC0%0/1
$result->{'actions'} || []
$result->{'actions'}
Truth table50%1/2
A
A= $result->{'actions'}
Aresult
11
00
601 1 (my $verb = $action->{a}) =~ tr/_/ /;
602 1 _say('cli.sync.would', { verb => $verb, path => $action->{p} });
603 }
604 }
605  
606 8 ▶ my $summary = $result->{summary} || {};
Condition50%1/2
$result->{'summary'} || {}
l!l
80
MC/DC0%0/1
$result->{'summary'} || {}
$result->{'summary'}
Truth table50%1/2
A
A= $result->{'summary'}
Aresult
11
00
607 _say('cli.sync.summary', {
608 repo => $name,
609 dry => $opt->{'dry-run'} ? message('cli.sync.dry_tag') : '',
610 8 ▶ map { $_ => $summary->{$_} || 0 } qw(pulled pushed deleted conflicts),
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'dry-run'} ? : 2 6 8
Condition100%2/2
$summary->{$_} || 0
l!l
428
MC/DC100%1/1
$summary->{$_} || 0
$summary->{$_}
Truth table100%2/2
A
A= $summary->{$_}
Aresult
11
00
611 });
612 8 ▶ _say('cli.sync.transfer', { report => _transfer_report($summary) }) unless $opt->{'dry-run'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"dry-run"} 6 2 8
613  
614 8 ▶ _print_warnings($result->{warnings}) unless $opt->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"quiet"} 6 2 8
615 8 return 1;
616 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
617  
618 # _print_warnings
619 # Purpose: print warnings, one per line.
620 # Entry: $warnings is an arrayref or undef.
621 # Exit: returns 1.
622 # Side effects: prints.
623 49 ▶sub _print_warnings :Private {
Subroutine _print_warnings: called CC=3 SCAR=11.1
624 49 my ($warnings) = @_;
625  
626 49 ▶ _say('cli.warning', { warning => $_ }) for @{ $warnings || [] };
Condition50%1/2
$warnings || []
l!l
490
MC/DC0%0/1
$warnings || []
$warnings
Truth table50%1/2
A
A= $warnings
Aresult
11
00
627 49 return 1;
628 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
629  
630 # _client_status
631 # Purpose: filesync status: when each repo last synced, and how big it is.
632 # Entry: @args are status's options.
633 # Exit: returns 0.
634 # Side effects: reads each repo's cache; prints.
635 3 ▶sub _client_status :Private {
Subroutine _client_status: called CC=12 SCAR=25.5
636 3 my (@args) = @_;
637  
638 3 my (@given, %opt);
639 3 _options(\@args, { 'repo=s@' => \@given, json => \$opt{json}, 'config=s' => \$opt{config} }, 'status');
640  
641 3 my $config = App::FileSync::Config::load_client($opt{config});
642 3 my @status;
643 3 for my $name (@{ _repos($config, \@given, 0) }) {
644 3 my $repo = $config->{repos}{$name};
645 3 my $cache = _client($config, $name)->cache();
646 3 ▶ my $last = ref($cache->{last_run}) eq 'HASH' ? $cache->{last_run} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $cache->{'last_run'} eq 'HASH' ? : 3 0 3
647  
648 push @status, {
649 repo => $name,
650 root => $repo->{root},
651 transport => $repo->{transport} || 'ssh',
652 last_run => $last ? scalar(gmtime($last->{ts})) . ' UTC' : 'never',
653 last_summary => $last ? $last->{summary} : undef,
654 3 ▶ files => scalar(grep { ($_->{t} || $TYPE_FILE) eq $TYPE_FILE } values %{ $cache->{entries} }),
Branch: 2 branches
BranchTrueFalseTotal
$last ? : 3 0 3
$last ? : 3 0 3
Condition40%2/5
$repo->{'transport'} || 'ssh'
l!l
30
$_->{'t'} || $App::FileSync::CLI::TYPE_FILE
l!l&&r!l&&!r
300
MC/DC0%0/1
$repo->{'transport'} || 'ssh'
$repo->{'transport'}
MC/DC0%0/2
$_->{'t'} || $App::FileSync::CLI::TYPE_FILE
$_->{'t'} $App::FileSync::CLI::TYPE_FILE
Truth table50%1/2
A
A= $repo->{'transport'}
Aresult
11
00
Truth table33%1/3
A || B
A= $_->{'t'}B= $App::FileSync::CLI::TYPE_FILE
ABresult
1X1
011
000
655 };
656 }
657  
658 3 ▶ if ($opt{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"json"}) 1 2 3
659 1 print json_encode({ repos => \@status }), "\n";
660 1 return $EXIT_OK;
661 }
662  
663 2 for my $entry (@status) {
664 2 _say('cli.status.repo', $entry);
665 2 _say('cli.status.transport', $entry);
666 # JSON keeps the literal 'never' (it is data); the text is translated.
667 2 ▶ _say('cli.status.last_run', { last_run => $entry->{last_run} eq 'never' ? message('cli.never') : $entry->{last_run} });
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'last_run'} eq 'never' ? : 0 2 2
668  
669 # Caches written before the byte counters existed have no transfer
670 # figures; skip the line rather than invent zeros.
671 2 my $summary = $entry->{last_summary};
672 _say('cli.status.transfer', { report => _transfer_report($summary) })
673 2 ▶ if ref($summary) eq 'HASH' && defined $summary->{bytes_sent};
Branch: 1 branch
BranchTrueFalseTotal
if ref $summary eq "HASH" and defined $summary->{"bytes_sent"} 1 1 2
Condition66%2/3
ref $summary eq "HASH" and defined $summary->{"bytes_sent"}
!ll&&!rl&&r
011
MC/DC50%1/2
ref $summary eq "HASH" and defined $summary->{"bytes_sent"}
ref $summary eq "HASH" defined $summary->{"bytes_sent"}
Truth table66%2/3
A and B
A= ref $summary eq "HASH"B= defined $summary->{"bytes_sent"}
ABresult
0X0
100
111
674 2 _say('cli.status.files', $entry);
675 }
676  
677 2 return $EXIT_OK;
678 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
679  
680 # _client_log
681 # Purpose: filesync log: recent run attempts per repo.
682 # Entry: @args are log's options.
683 # Exit: returns 0.
684 # Side effects: reads the run logs; prints (or JSON). Bad dates are
685 # reported before any repo is touched.
686 50 ▶sub _client_log :Private {
Subroutine _client_log: called CC=6 SCAR=17.9
687 50 my (@args) = @_;
688  
689 50 my (@given, @exits, %opt);
690 _options(\@args, {
691 'repo=s@' => \@given,
692 'n=i' => \$opt{limit},
693 'exit=i@' => \@exits,
694 150 (map { ("$_=s" => \$opt{$_}) } qw(since until config)),
695 50 (map { ($_ => \$opt{$_}) } qw(warnings json)),
696 }, 'log');
697 50 _no_more(\@args, 'log');
698 50 ▶ throw('cli.positive', { command => 'log', option => '-n' }) if defined $opt{limit} && $opt{limit} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"limit"} and $opt{"limit"} < 1 1 49 50
Condition100%3/3
defined $opt{"limit"} and $opt{"limit"} < 1
!ll&&!rl&&r
4361
MC/DC100%2/2
defined $opt{"limit"} and $opt{"limit"} < 1
defined $opt{"limit"} $opt{"limit"} < 1
Truth table100%3/3
A and B
A= defined $opt{"limit"}B= $opt{"limit"} < 1
ABresult
0X0
100
111
699 49 _check_exits(\@exits);
700 48 _check_window(@opt{qw(since until)});
701  
702 47 my $config = App::FileSync::Config::load_client($opt{config});
703 47 my @results;
704 47 for my $name (@{ _repos($config, \@given, 1) }) {
705 my $result = _client($config, $name)->log(
706 limit => $opt{limit}, since => $opt{since}, until => $opt{until}, exits => \@exits, warnings => $opt{warnings},
707 48 );
708 48 push @results, $result;
709 48 ▶ _print_runs($name, $result->{runs}) unless $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"json"} 37 11 48
710 }
711  
712 46 ▶ print json_encode({ results => \@results }), "\n" if $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"json"} 10 36 46
713 46 return $EXIT_OK;
714 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
715  
716 # _print_runs
717 # Purpose: the text form of one repo's run log.
718 # Entry: $runs is from Client->log.
719 # Exit: returns 1.
720 # Side effects: prints one line per run (and its warnings). A failed or
721 # lock-refused attempt has no counts worth printing: its
722 # message explains it. Entries older than the byte counters
723 # keep the shorter line.
724 37 ▶sub _print_runs :Private {
Subroutine _print_runs: called CC=12 SCAR=25.0
725 37 my ($name, $runs) = @_;
726  
727 37 ▶ return _say('cli.log.none', { repo => $name }) unless @{$runs};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$runs;} 9 28 37
728  
729 28 for my $run (@{$runs}) {
730 55 ▶ my %line = (repo => $name, ts => format_utc($run->{ts}), exit => $run->{exit} || 0);
Condition100%2/2
$run->{'exit'} || 0
l!l
2728
MC/DC100%1/1
$run->{'exit'} || 0
$run->{'exit'}
Truth table100%2/2
A
A= $run->{'exit'}
Aresult
11
00
731  
732 55 ▶ if (defined $run->{error} && length $run->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $run->{"error"} and length $run->{"error"}) 12 43 55
Condition66%2/3
defined $run->{"error"} and length $run->{"error"}
!ll&&!rl&&r
43012
MC/DC50%1/2
defined $run->{"error"} and length $run->{"error"}
defined $run->{"error"} length $run->{"error"}
Truth table66%2/3
A and B
A= defined $run->{"error"}B= length $run->{"error"}
ABresult
0X0
100
111
733 12 (my $text = $run->{error}) =~ s/\s+/ /g;
734 12 _say('cli.log.error', { %line, message => $text });
735 12 next;
736 }
737  
738 43 ▶ my $summary = $run->{summary} || {};
Condition50%1/2
$run->{'summary'} || {}
l!l
430
MC/DC0%0/1
$run->{'summary'} || {}
$run->{'summary'}
Truth table50%1/2
A
A= $run->{'summary'}
Aresult
11
00
739 _say('cli.log.run', {
740 %line,
741 172 ▶ (map { $_ => $summary->{$_} || 0 } qw(pulled pushed deleted conflicts)),
Branch: 3 branches
BranchTrueFalseTotal
$summary->{'skipped'} ? : 8 35 43
!$run->{'dry_run'} && defined $summary->{'bytes_sent'} ? : 28 15 43
$run->{'dry_run'} ? : 0 43 43
Condition80%4/5
$summary->{$_} || 0
l!l
31141
not $run->{"dry_run"} and defined $summary->{"bytes_sent"}
!ll&&!rl&&r
01528
MC/DC100%1/1
$summary->{$_} || 0
$summary->{$_}
MC/DC50%1/2
not $run->{"dry_run"} and defined $summary->{"bytes_sent"}
not $run->{"dry_run"} defined $summary->{"bytes_sent"}
Truth table100%2/2
A
A= $summary->{$_}
Aresult
11
00
Truth table66%2/3
not(A) and B
A= not $run->{"dry_run"}B= defined $summary->{"bytes_sent"}
ABresult
0X0
100
111
742 skipped => $summary->{skipped} ? message('cli.log.skipped', { count => $summary->{skipped} }) : '',
743 transfer => (!$run->{dry_run} && defined $summary->{bytes_sent}) ? ', ' . _transfer_report($summary) : '',
744 43 dry => $run->{dry_run} ? message('cli.log.dry_tag') : '',
745 });
746 43 _print_warnings($run->{warnings});
747 }
748  
749 28 return 1;
750 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
751  
752 # _client_settings
753 # Purpose: filesync settings: effective settings and where each came from.
754 # Entry: @args are settings's options.
755 # Exit: returns 0.
756 # Side effects: reads the settings and the client configuration; prints.
757 3 ▶sub _client_settings :Private {
Subroutine _client_settings: called CC=13 SCAR=26.1
758 3 my (@args) = @_;
759  
760 3 my %opt;
761 3 _options(\@args, { json => \$opt{json}, 'config=s' => \$opt{config} }, 'settings');
762 3 _no_more(\@args, 'settings');
763  
764 2 my $report = App::FileSync::Settings::explain();
765 2 ▶ my $client_file = (defined $opt{config} && length $opt{config}) ? $opt{config} : App::FileSync::Config::client_config_path();
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'config'} && length $opt{'config'} ? : 0 2 2
Condition33%1/3
defined $opt{"config"} and length $opt{"config"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opt{"config"} and length $opt{"config"}
defined $opt{"config"} length $opt{"config"}
Truth table33%1/3
A and B
A= defined $opt{"config"}B= length $opt{"config"}
ABresult
0X0
100
111
766 2 $report->{config_file} = $client_file;
767 $report->{repos} = App::FileSync::Settings::explain_repos(
768 2 config => App::FileSync::Config::load_client($opt{config}),
769 report => $report,
770 config_file => $client_file,
771 );
772  
773 2 ▶ if ($opt{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"json"}) 1 1 2
774 1 print json_encode($report), "\n";
775 1 return $EXIT_OK;
776 }
777  
778 1 my $file = $report->{settings_file};
779 1 ▶ _say('cli.settings.file', { file => (defined $file && length $file) ? $file : message('cli.settings.no_file') });
Branch: 1 branch
BranchTrueFalseTotal
defined $file && length $file ? : 1 0 1
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
780 1 print "\n";
781 _say('cli.settings.row', { key => $_->{key}, value => _display_setting($_), source => _describe_source($_) })
782 1 for @{ $report->{settings} };
783 1 print "\n";
784 1 _say('cli.settings.note');
785  
786 1 ▶ my @repos = @{ $report->{repos} || [] };
Condition50%1/2
$report->{'repos'} || []
l!l
10
MC/DC0%0/1
$report->{'repos'} || []
$report->{'repos'}
Truth table50%1/2
A
A= $report->{'repos'}
Aresult
11
00
787 1 ▶ return $EXIT_OK unless @repos;
Branch: 1 branch
BranchTrueFalseTotal
unless @repos 0 1 1
788  
789 1 print "\n";
790 1 _say('cli.settings.repos');
791 1 for my $repo (@repos) {
792 3 ▶ _say('cli.settings.repo', { %{$repo}, server => defined $repo->{server} ? ", $repo->{server}" : '' });
Branch: 1 branch
BranchTrueFalseTotal
defined $repo->{'server'} ? : 2 1 3
793 3 ▶ unless (defined $repo->{ssh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $repo->{"ssh"}) 1 2 3
794 1 _say('cli.settings.local');
795 1 next;
796 }
797 _say('cli.settings.repo_row', { key => $_->{key}, value => _display_setting($_), source => _describe_repo_source($_) })
798 2 for @{ $repo->{ssh} };
799 }
800  
801 1 return $EXIT_OK;
802 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
803  
804 # _describe_repo_source / _describe_source / _display_setting
805 # Purpose: the text columns of "filesync settings".
806 # Entry: $row is a row from Settings::explain or explain_repos.
807 # Exit: returns the text.
808 # Side effects: none. Port and host-key checking have no process-wide
809 # fallback, so their rows say [repo only]; a blank value that
810 # means "the transport's default" is named rather than shown
811 # as an empty column.
812 10 ▶sub _describe_repo_source :Private {
Subroutine _describe_repo_source: called CC=2 SCAR=6.9
813 10 my ($row) = @_;
814  
815 10 my $description = message("cli.repo_source.$row->{source}", $row);
816 10 ▶ return $row->{repo_only} ? message('cli.source.repo_only', { description => $description }) : $description;
Branch: 1 branch
BranchTrueFalseTotal
$row->{'repo_only'} ? : 4 6 10
817 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
818  
819 6 ▶sub _describe_source :Private {
Subroutine _describe_source: called CC=2 SCAR=6.9
820 6 my ($row) = @_;
821  
822 # Config::Abstraction reports file-like sources under several names.
823 6 ▶ my $source = $row->{source} =~ /\A(?:default|merged|env)\z/ ? $row->{source} : 'file';
Branch: 1 branch
BranchTrueFalseTotal
$row->{'source'} =~ /\A(?:default|merged|env)\z/ ? : 5 1 6
824 6 return message("cli.source.$source", $row);
825 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
826  
827 16 ▶sub _display_setting :Private {
Subroutine _display_setting: called CC=5 SCAR=16.6
828 16 my ($row) = @_;
829  
830 16 my $value = $row->{value};
831 16 my $blank = $BLANK_SETTING{ $row->{key} };
832 16 ▶ return (!(defined $value && length $value) && $blank) ? message($blank) : $value // '';
Branch: 1 branch
BranchTrueFalseTotal
!(defined $value && length $value) && $blank ? : 4 12 16
Condition62%5/8
not defined $value && length $value and $blank
!ll&&!rl&&r
1204
defined $value && length $value
!ll&&!rl&&r
0412
$value // ""
l!l
120
MC/DC33%1/3
not defined $value && length $value and $blank
defined $value length $value $blank
MC/DC0%0/1
$value // ""
$value
Truth table40%2/5
not(A && B) and C
A= defined $valueB= length $valueC= $blank
ABCresult
11X0
0X00
1000
0X11
1011
Truth table50%1/2
A
A= $value
Aresult
11
00
833 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
834  
835 # _client_verify
836 # Purpose: filesync verify: compare each repo's tree with its base.
837 # Entry: @args are verify's options.
838 # Exit: returns 4 if any repo drifted, else 0.
839 # Side effects: reads the trees; prints (or JSON).
840 4 ▶sub _client_verify :Private {
Subroutine _client_verify: called CC=4 SCAR=13.9
841 4 my (@args) = @_;
842  
843 4 my (@given, %opt);
844 4 _options(\@args, { 'repo=s@' => \@given, (map { ($_ => \$opt{$_}) } qw(deep json)), 'config=s' => \$opt{config} }, 'verify');
845 4 _no_more(\@args, 'verify');
846  
847 4 my $config = App::FileSync::Config::load_client($opt{config});
848 4 my @reports = map { _client($config, $_)->verify(deep => $opt{deep}) } @{ _repos($config, \@given, 1) };
849  
850 4 ▶ if ($opt{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'json'}) { } 1 3 4
851 1 print json_encode({ results => \@reports }), "\n";
852 } else {
853 3 _print_verify($_, 1) for @reports;
854 }
855  
856 4 ▶ return (any { !$_->{ok} } @reports) ? $EXIT_DRIFT : $EXIT_OK;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { not $_->{'ok'}; } , @reports) ? : 3 1 4
857 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
858  
859 # _print_verify
860 # Purpose: the text form of a verify report (client or admin).
861 # Entry: $report is from Verify; $base_note says whether to mention a
862 # missing base (client only: the admin always has an index).
863 # Exit: returns 1.
864 # Side effects: prints.
865 6 ▶sub _print_verify :Private {
Subroutine _print_verify: called CC=9 SCAR=22.0
866 6 my ($report, $base_note) = @_;
867  
868 _say('cli.verify.summary', {
869 repo => $report->{repo},
870 state => message($report->{ok} ? 'cli.verify.clean' : 'cli.verify.drift'),
871 files => message('word.file', { count => $report->{files} || 0 }),
872 rehashed => $report->{rehashed},
873 trusted => $report->{trusted},
874 6 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$report->{'ok'} ? : 3 3 6
Condition50%1/2
$report->{'files'} || 0
l!l
60
MC/DC0%0/1
$report->{'files'} || 0
$report->{'files'}
Truth table50%1/2
A
A= $report->{'files'}
Aresult
11
00
875 6 ▶ _say('cli.verify.no_base') if $base_note && !$report->{known};
Branch: 1 branch
BranchTrueFalseTotal
if $base_note and not $report->{"known"} 1 5 6
Condition100%3/3
$base_note and not $report->{"known"}
!ll&&!rl&&r
321
MC/DC100%2/2
$base_note and not $report->{"known"}
$base_note not $report->{"known"}
Truth table100%3/3
A and not(B)
A= $base_noteB= not $report->{"known"}
ABresult
0X0
100
111
876  
877 6 for my $entry (@{ $report->{modified} }) {
878 2 ▶ _say('cli.verify.modified', { path => $entry->{p}, reason => defined $entry->{reason} ? " ($entry->{reason})" : '' });
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->{'reason'} ? : 0 2 2
879 }
880 6 _say('cli.verify.missing', { path => $_->{p} }) for @{ $report->{missing} };
881 6 _say('cli.verify.extra', { path => $_->{p} }) for @{ $report->{extra} };
882  
883 6 return 1;
884 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
885  
886 # _client_restore
887 # Purpose: filesync restore: put a path back from the local trash.
888 # Entry: @args are PATH and options.
889 # Exit: returns 0.
890 # Side effects: copies the version out, or lists versions.
891 4 ▶sub _client_restore :Private {
Subroutine _client_restore: called CC=2 SCAR=6.9
892 4 my (@args) = @_;
893  
894 4 my %opt;
895 4 _options(\@args, { (map { ("$_=s" => \$opt{$_}) } qw(repo to config)), list => \$opt{list} }, 'restore');
896 4 my $path = _require(shift @args, 'restore', 'PATH');
897 4 _no_more(\@args, 'restore');
898  
899 4 my $config = App::FileSync::Config::load_client($opt{config});
900 4 my $name = _one_repo($config, $opt{repo}, 'restore');
901 4 my $result = _client($config, $name)->restore(path => $path, to => $opt{to}, list => $opt{list});
902  
903 2 ▶ return _print_versions($result->{versions}, 'cli.restore.no_versions', { path => $path }) if $opt{list};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"list"} 1 1 2
904  
905 1 _say('cli.restore.done', { path => $path, to => $result->{to} });
906 1 return $EXIT_OK;
907 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
908  
909 # _print_versions
910 # Purpose: list trashed versions (client or admin).
911 # Entry: $versions are Trash::version_info rows; $empty_key and
912 # $empty_args say what to print when there are none.
913 # Exit: returns 0.
914 # Side effects: prints.
915 2 ▶sub _print_versions :Private {
Subroutine _print_versions: called CC=7 SCAR=22.9
916 2 my ($versions, $empty_key, $empty_args) = @_;
917  
918 2 ▶ return _say($empty_key, $empty_args) && $EXIT_OK unless @{$versions};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$versions;} 0 2 2
Condition0%0/3
_say($empty_key, $empty_args) && $App::FileSync::CLI::EXIT_OK
!ll&&!rl&&r
000
MC/DC0%0/2
_say($empty_key, $empty_args) && $App::FileSync::CLI::EXIT_OK
_say($empty_key, $empty_args) $App::FileSync::CLI::EXIT_OK
Truth table0%0/3
A && B
A= _say($empty_key, $empty_args)B= $App::FileSync::CLI::EXIT_OK
ABresult
0X0
100
111
919  
920 2 for my $version (@{$versions}) {
921 _say('cli.restore.version', {
922 date => $version->{date} // '-',
923 size => defined $version->{size} ? _format_bytes($version->{size}) : '-',
924 mtime => defined $version->{mtime} ? scalar(gmtime($version->{mtime})) . ' UTC' : '-',
925 path => $version->{path},
926 2 ▶ });
Branch: 2 branches
BranchTrueFalseTotal
defined $version->{'size'} ? : 2 0 2
defined $version->{'mtime'} ? : 2 0 2
Condition50%1/2
$version->{"date"} // "-"
l!l
20
MC/DC0%0/1
$version->{"date"} // "-"
$version->{"date"}
Truth table50%1/2
A
A= $version->{"date"}
Aresult
11
00
927 }
928  
929 2 return $EXIT_OK;
930 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
931  
932 # _client_snapshot
933 # Purpose: filesync snapshot [create|take|list|restore].
934 # Entry: @args start with the verb, or an option (meaning create).
935 # Exit: returns the sub-command's exit code.
936 # Side effects: raises cli.unknown_snapshot.
937 5 ▶sub _client_snapshot :Private {
Subroutine _client_snapshot: called CC=4 SCAR=14.0
938 5 my (@args) = @_;
939  
940 5 ▶ my $verb = (@args && $args[0] !~ /\A-/) ? shift @args : 'create';
Branch: 1 branch
BranchTrueFalseTotal
@args && !($args[0] =~ /\A-/) ? : 2 3 5
Condition66%2/3
@args and not $args[0] =~ /\A-/
!ll&&!rl&&r
032
MC/DC50%1/2
@args and not $args[0] =~ /\A-/
@args not $args[0] =~ /\A-/
Truth table66%2/3
A and not(B)
A= @argsB= not $args[0] =~ /\A-/
ABresult
0X0
100
111
941 5 my %verbs = (create => '_client_snapshot_create', take => '_client_snapshot_create',
942 list => '_client_snapshot_list', restore => '_client_snapshot_restore');
943 5 ▶ my $handler = $verbs{$verb} or throw('cli.unknown_snapshot', { verb => $verb });
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $verbs{$verb} or throw("cli.unknown_snapshot", {"verb", $verb}) 5 0 5
944  
945 5 return __PACKAGE__->can($handler)->(@args);
946 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
947  
948 # _client_snapshot_create
949 # Purpose: take today's snapshot of a replica, pruning old ones.
950 # Entry: @args are the options.
951 # Exit: returns 0, or 2 when another run holds the lock.
952 # Side effects: creates and prunes snapshots; prints.
953 3 ▶sub _client_snapshot_create :Private {
Subroutine _client_snapshot_create: called CC=10 SCAR=24.5
954 3 my (@args) = @_;
955  
956 3 my %opt;
957 3 _options(\@args, { 'repo=s' => \$opt{repo}, 'keep=i' => \$opt{keep}, 'config=s' => \$opt{config} }, 'snapshot');
958 3 _no_more(\@args, 'snapshot');
959 3 ▶ throw('cli.positive', { command => 'snapshot', option => '--keep' }) if defined $opt{keep} && $opt{keep} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"keep"} and $opt{"keep"} < 1 0 3 3
Condition66%2/3
defined $opt{"keep"} and $opt{"keep"} < 1
!ll&&!rl&&r
120
MC/DC0%0/2
defined $opt{"keep"} and $opt{"keep"} < 1
defined $opt{"keep"} $opt{"keep"} < 1
Truth table66%2/3
A and B
A= defined $opt{"keep"}B= $opt{"keep"} < 1
ABresult
0X0
100
111
960  
961 3 my $config = App::FileSync::Config::load_client($opt{config});
962 3 my $name = _one_repo($config, $opt{repo}, 'snapshot');
963 3 my $result = _client($config, $name)->snapshot_create(keep => $opt{keep});
964 2 ▶ return _warn_locked($name, $result) if ($result->{exit} || 0) == $EXIT_LOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"exit"} || 0) == $App::FileSync::CLI::EXIT_LOCKED 0 2 2
Condition50%1/2
$result->{'exit'} || 0
l!l
02
MC/DC0%0/1
$result->{'exit'} || 0
$result->{'exit'}
Truth table50%1/2
A
A= $result->{'exit'}
Aresult
11
00
965  
966 2 ▶ return _say('cli.snapshot.exists', { repo => $name, date => $result->{date} }) && $EXIT_OK unless $result->{created};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"created"} 1 1 2
Condition33%1/3
_say('cli.snapshot.exists', {'repo', $name, 'date', $result->{'date'}}) && $App::FileSync::CLI::EXIT_OK
!ll&&!rl&&r
010
MC/DC0%0/2
_say('cli.snapshot.exists', {'repo', $name, 'date', $result->{'date'}}) && $App::FileSync::CLI::EXIT_OK
_say('cli.snapshot.exists', {'repo', $name, 'date', $result->{'date'}}) $App::FileSync::CLI::EXIT_OK
Truth table33%1/3
A && B
A= _say('cli.snapshot.exists', {'repo', $name, 'date', $result->{'date'}})B= $App::FileSync::CLI::EXIT_OK
ABresult
0X0
100
111
967  
968 _say('cli.snapshot.taken', {
969 repo => $name,
970 date => $result->{date},
971 files => message('word.file', { count => $result->{files} }),
972 size => _format_bytes($result->{bytes}),
973 path => $result->{path},
974 1 });
975  
976 1 ▶ my @pruned = @{ $result->{pruned} || [] };
Condition50%1/2
$result->{'pruned'} || []
l!l
10
MC/DC0%0/1
$result->{'pruned'} || []
$result->{'pruned'}
Truth table50%1/2
A
A= $result->{'pruned'}
Aresult
11
00
977 1 ▶ _say('cli.snapshot.pruned', { repo => $name, count => scalar @pruned, kept => scalar @{ $result->{older} || [] }, dates => "@pruned" })
Branch: 1 branch
BranchTrueFalseTotal
if @pruned 1 0 1
Condition50%1/2
$result->{'older'} || []
l!l
10
MC/DC0%0/1
$result->{'older'} || []
$result->{'older'}
Truth table50%1/2
A
A= $result->{'older'}
Aresult
11
00
978 if @pruned;
979  
980 1 return $EXIT_OK;
981 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
982  
983 # _client_snapshot_list
984 # Purpose: list a replica's snapshots.
985 # Entry: @args are the options.
986 # Exit: returns 0.
987 # Side effects: prints.
988 1 ▶sub _client_snapshot_list :Private {
Subroutine _client_snapshot_list: called CC=4 SCAR=14.2
989 1 my (@args) = @_;
990  
991 1 my %opt;
992 1 _options(\@args, { 'repo=s' => \$opt{repo}, 'config=s' => \$opt{config} }, 'snapshot_list');
993 1 _no_more(\@args, 'snapshot list');
994  
995 1 my $config = App::FileSync::Config::load_client($opt{config});
996 1 my $name = _one_repo($config, $opt{repo}, 'snapshot list');
997 1 my $snapshots = _client($config, $name)->snapshot_list()->{snapshots};
998  
999 1 ▶ return _say('cli.snapshot.none', { repo => $name }) && $EXIT_OK unless @{$snapshots};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$snapshots;} 0 1 1
Condition0%0/3
_say('cli.snapshot.none', {'repo', $name}) && $App::FileSync::CLI::EXIT_OK
!ll&&!rl&&r
000
MC/DC0%0/2
_say('cli.snapshot.none', {'repo', $name}) && $App::FileSync::CLI::EXIT_OK
_say('cli.snapshot.none', {'repo', $name}) $App::FileSync::CLI::EXIT_OK
Truth table0%0/3
A && B
A= _say('cli.snapshot.none', {'repo', $name})B= $App::FileSync::CLI::EXIT_OK
ABresult
0X0
100
111
1000  
1001 1 _say('cli.snapshot.header', { map { $_ => message("cli.snapshot.column.$_") } qw(date files size path) });
1002 1 _say('cli.snapshot.row', { %{$_}, size => _format_bytes($_->{bytes}) }) for @{$snapshots};
1003 1 return $EXIT_OK;
1004 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1005  
1006 # _client_snapshot_restore
1007 # Purpose: copy a snapshot (or one path) back.
1008 # Entry: @args are DATE [PATH] and options.
1009 # Exit: returns 0, or 2 when another run holds the lock.
1010 # Side effects: replaces files; prints, with a reminder that a replica is
1011 # mirrored again on its next sync.
1012 1 ▶sub _client_snapshot_restore :Private {
Subroutine _client_snapshot_restore: called CC=5 SCAR=16.3
1013 1 my (@args) = @_;
1014  
1015 1 my %opt;
1016 1 _options(\@args, { map { ("$_=s" => \$opt{$_}) } qw(repo to config) }, 'snapshot_restore');
1017 1 my $date = _require(shift @args, 'snapshot restore', 'DATE');
1018 1 my $path = shift @args;
1019 1 _no_more(\@args, 'snapshot restore');
1020  
1021 1 my $config = App::FileSync::Config::load_client($opt{config});
1022 1 my $name = _one_repo($config, $opt{repo}, 'snapshot restore');
1023 1 my $result = _client($config, $name)->snapshot_restore(date => $date, path => $path, to => $opt{to});
1024 1 ▶ return _warn_locked($name, $result) if ($result->{exit} || 0) == $EXIT_LOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"exit"} || 0) == $App::FileSync::CLI::EXIT_LOCKED 0 1 1
Condition50%1/2
$result->{'exit'} || 0
l!l
01
MC/DC0%0/1
$result->{'exit'} || 0
$result->{'exit'}
Truth table50%1/2
A
A= $result->{'exit'}
Aresult
11
00
1025  
1026 _say('cli.snapshot.restored', {
1027 repo => $name,
1028 1 what => @{ $result->{paths} } ? "@{ $result->{paths} }" : message('cli.snapshot.whole_tree'),
1029 date => $result->{date},
1030 files => message('word.file', { count => $result->{files} }),
1031 size => _format_bytes($result->{bytes}),
1032 to => $result->{to},
1033 1 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
@{$result->{'paths'};} ? : 1 0 1
1034 1 ▶ _say('cli.snapshot.into_root') if $result->{into_root};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"into_root"} 0 1 1
1035  
1036 1 return $EXIT_OK;
1037 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1038  
1039 # _warn_locked
1040 # Purpose: report a lock refusal from a snapshot command.
1041 # Entry: $result has error.
1042 # Exit: returns 2.
1043 # Side effects: logs a warning.
1044 0 ▶sub _warn_locked :Private {
Subroutine _warn_locked: not called CC=1 SCAR=6.9
1045 0 my ($name, $result) = @_;
1046  
1047 0 App::FileSync::Settings::logger()->warn(message('cli.repo_error', { repo => $name, error => $result->{error} }));
1048 0 return $EXIT_LOCKED;
1049 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1050  
1051 # ------------------------------------------------------------ formatting
1052  
1053 # _format_bytes
1054 # Purpose: a byte count for people: B, kB, MB or GB with one decimal.
1055 # Entry: $bytes may be undef (shown as 0).
1056 # Exit: returns the text.
1057 # Side effects: none.
1058 9 ▶sub _format_bytes :Private {
Subroutine _format_bytes: called CC=3 SCAR=11.3
1059 9 my ($bytes) = @_;
1060  
1061 9 ▶ $bytes //= 0;
Condition50%1/2
$bytes //= 0
l!l
90
MC/DC0%0/1
$bytes //= 0
$bytes
Truth table50%1/2
A
A= $bytes
Aresult
11
00
1062 9 ▶ my $unit = first { $bytes >= $_->[0] } @SIZE_UNITS;
Subroutine __ANON__: called CC=1 SCAR=0.0
1063 9 ▶ return $unit
Branch: 1 branch
BranchTrueFalseTotal
$unit ? : 0 9 9
1064 ? message('cli.size.scaled', { value => $bytes / $unit->[0], unit => $unit->[1] })
1065 : message('cli.size.bytes', { value => $bytes });
1066 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1067  
1068 # _bytes_exact
1069 # Purpose: an exact byte count with thousands separators.
1070 # Entry: $bytes may be undef or junk (shown as 0).
1071 # Exit: returns the text.
1072 # Side effects: none. Transfer accounting is exact: these bytes are what
1073 # the peer actually read, so rounding them would defeat it.
1074 70 ▶sub _bytes_exact :Private {
Subroutine _bytes_exact: called CC=4 SCAR=15.6
1075 70 my ($bytes) = @_;
1076  
1077 70 ▶ $bytes = 0 unless defined $bytes && $bytes =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and $bytes =~ /\A\d+\z/ 0 70 70
Condition33%1/3
defined $bytes and $bytes =~ /\A\d+\z/
!ll&&!rl&&r
0070
MC/DC0%0/2
defined $bytes and $bytes =~ /\A\d+\z/
defined $bytes $bytes =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $bytesB= $bytes =~ /\A\d+\z/
ABresult
0X0
100
111
1078 70 ▶ 1 while $bytes =~ s/(\d)(\d{3})(?!\d)/$1,$2/;
Branch: 1 branch
BranchTrueFalseTotal
$bytes =~ s/(\d)(\d{3})(?!\d)/$1,$2/ and do { '???' } 0 70 70
1079 70 return message('cli.size.exact', { value => $bytes });
1080 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1081  
1082 # _transfer_report
1083 # Purpose: one line of per-run accounting.
1084 # Entry: $summary has bytes_sent, bytes_received, delta_pulled,
1085 # delta_pushed.
1086 # Exit: returns the text: bytes each way, then how many files moved
1087 # as a delta rather than whole.
1088 # Side effects: none.
1089 35 ▶sub _transfer_report :Private {
Subroutine _transfer_report: called CC=3 SCAR=11.6
1090 35 my ($summary) = @_;
1091  
1092 35 ▶ my %delta = map { $_ => $summary->{"delta_$_"} || 0 } qw(pulled pushed);
Condition50%1/2
$$summary{"delta_$_"} || 0
l!l
070
MC/DC0%0/1
$$summary{"delta_$_"} || 0
$$summary{"delta_$_"}
Truth table50%1/2
A
A= $$summary{"delta_$_"}
Aresult
11
00
1093 0 my @parts = map { message("cli.transfer.$_", { count => $delta{$_} }) } grep { $delta{$_} } qw(pulled pushed);
1094  
1095 return message('cli.transfer.report', {
1096 sent => _bytes_exact($summary->{bytes_sent}),
1097 received => _bytes_exact($summary->{bytes_received}),
1098 35 ▶ files => message('word.file', { count => $delta{pulled} + $delta{pushed} }),
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 0 35 35
1099 detail => @parts ? ' (' . join(', ', @parts) . ')' : '',
1100 });
1101 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1102  
1103 # _check_exits / _check_window
1104 # Purpose: validate log filters before anything is read.
1105 # Entry: exit codes; since and until (maybe undef).
1106 # Exit: returns 1.
1107 # Side effects: raises cli.exit_range or when.bad.
1108 76 ▶sub _check_exits :Private {
Subroutine _check_exits: called CC=2 SCAR=7.0
1109 76 my ($exits) = @_;
1110  
1111 76 ▶ throw('cli.exit_range', { max => $EXIT_MAX }) if any { $_ < 0 || $_ > $EXIT_MAX } @{$exits};
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if &any(sub { $_ < 0 or $_ > $App::FileSync::CLI::EXIT_MAX; } , @{$exits;}) 2 74 76
$_ < 0 or $_ > $App::FileSync::CLI::EXIT_MAX 0 21 21
1112 74 return 1;
1113 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1114  
1115 74 ▶sub _check_window :Private {
Subroutine _check_window: called CC=3 SCAR=11.0
1116 74 my ($since, $until) = @_;
1117  
1118 74 ▶ parse_since($since) if defined $since;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since 13 61 74
1119 73 ▶ parse_until($until) if defined $until;
Branch: 1 branch
BranchTrueFalseTotal
if defined $until 14 59 73
1120 72 return 1;
1121 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1122  
1123 # _default_client_id
1124 # Purpose: a client id derived from the host name.
1125 # Entry: none.
1126 # Exit: returns a valid client id: the short host name, lower-cased,
1127 # with anything else replaced by '-', starting with a letter
1128 # or digit and at most 64 characters; "client" if nothing is
1129 # left.
1130 # Side effects: none.
1131 5 ▶sub _default_client_id :Private {
Subroutine _default_client_id: called CC=2 SCAR=6.9
1132 5 my $name = lc Sys::Hostname::hostname();
1133 5 $name =~ s/\..*//s;
1134 5 $name =~ s/[^a-z0-9._-]+/-/g;
1135 5 $name =~ s/\A[^a-z0-9]+//;
1136 5 $name = substr($name, 0, $MAX_ID_LENGTH);
1137 5 $name =~ s/-+\z//;
1138  
1139 5 ▶ return length $name ? $name : $ROLE_CLIENT;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 4 1 5
1140 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1141  
1142 # ------------------------------------------------------------------- admin
1143  
1144 # _admin_dispatch
1145 # Purpose: filesync-admin: parse --root, then run the command.
1146 # Entry: @args is the command line.
1147 # Exit: returns the command's exit code.
1148 # Side effects: raises cli.admin.* for an unknown command or verb.
1149 59 ▶sub _admin_dispatch :Private {
Subroutine _admin_dispatch: called CC=12 SCAR=27.5
1150 59 my (@args) = @_;
1151  
1152 59 my $root = $ENV{FILESYNC_ROOT};
1153 59 ▶ while (@args && $args[0] =~ /\A--root(?:=(.*))?\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] =~ /\A--root(?:=(.*))?\z/s) 59 59 118
Condition66%2/3
@args and $args[0] =~ /\A--root(?:=(.*))?\z/s
!ll&&!rl&&r
05959
MC/DC50%1/2
@args and $args[0] =~ /\A--root(?:=(.*))?\z/s
@args $args[0] =~ /\A--root(?:=(.*))?\z/s
Truth table66%2/3
A and B
A= @argsB= $args[0] =~ /\A--root(?:=(.*))?\z/s
ABresult
0X0
100
111
1154 59 shift @args;
1155 59 ▶ $root = defined $1 ? $1 : shift @args;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 59 59
1156 59 ▶ throw('cli.admin.root_value') unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 59 59
1157 }
1158  
1159 59 ▶ my $group = shift(@args) // 'help';
Condition50%1/2
shift @args // "help"
l!l
590
MC/DC0%0/1
shift @args // "help"
shift @args
Truth table50%1/2
A
A= shift @args
Aresult
11
00
1160 59 ▶ if ($group eq 'help' || $group eq '--help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($group eq "help" or $group eq "--help") 0 59 59
Condition33%1/3
$group eq "help" or $group eq "--help"
l!l&&r!l&&!r
0059
MC/DC0%0/2
$group eq "help" or $group eq "--help"
$group eq "help" $group eq "--help"
Truth table33%1/3
A or B
A= $group eq "help"B= $group eq "--help"
ABresult
1X1
011
000
1161 0 print message('cli.help.admin');
1162 0 return $EXIT_OK;
1163 }
1164  
1165 59 my $admin = App::FileSync::Admin->new(root => $root);
1166 59 ▶ my $entry = $ADMIN_COMMANDS{$group} or throw('cli.unknown_command', { command => $group, program => 'filesync-admin' });
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $ADMIN_COMMANDS{$group} or throw("cli.unknown_command", {"command", $group, "program", "filesync-admin"}) 59 0 59
1167  
1168 59 ▶ unless (ref $entry) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $entry) 40 19 59
1169 40 return __PACKAGE__->can($entry)->($admin, @args);
1170 }
1171  
1172 19 ▶ my $verb = shift(@args) // '';
Condition50%1/2
shift @args // ""
l!l
190
MC/DC0%0/1
shift @args // ""
shift @args
Truth table50%1/2
A
A= shift @args
Aresult
11
00
1173 19 ▶ my $handler = $entry->{$verb} or throw('cli.admin.unknown_verb', { group => $group, verb => $verb });
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $entry->{$verb} or throw("cli.admin.unknown_verb", {"group", $group, "verb", $verb}) 19 0 19
1174 19 return __PACKAGE__->can($handler)->($admin, @args);
1175 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1176  
1177 # _admin_repo_create / _admin_repo_list / _admin_repo_show
1178 # Purpose: filesync-admin repo create|list|show.
1179 # Entry: $admin, then the command's arguments.
1180 # Exit: returns 0.
1181 # Side effects: create makes the repo; all print.
1182 2 ▶sub _admin_repo_create :Private {
Subroutine _admin_repo_create: called CC=2 SCAR=7.0
1183 2 my ($admin, @args) = @_;
1184  
1185 2 my @limits = qw(trash-days max-deletes max-delete-percent);
1186 2 my (%opt, @ignore);
1187 _options(\@args, {
1188 2 (map { ("$_=i" => \$opt{$_}) } @limits),
1189 'ignore=s' => \@ignore,
1190 }, 'repo_create');
1191 2 my $name = _require(shift @args, 'repo create', 'NAME');
1192 2 _no_more(\@args, 'repo create');
1193  
1194 # Each limit option is its parameter with "-" for "_"; a bad value is
1195 # reported by the option the user typed.
1196 2 my %option = map { (my $key = $_) =~ tr/-/_/; $key => $_ } @limits;
1197 6 ▶ my $result = with_options({ map { $_ => "--$option{$_}" } keys %option }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1198 2 $admin->repo_create(name => $name, (map { $_ => $opt{ $option{$_} } } keys %option), ignore => \@ignore);
1199 2 });
1200  
1201 0 _say("cli.repo.created.$_", { %{$result}, repo => $result->{name} }) for qw(name master config);
1202 0 return $EXIT_OK;
1203 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1204  
1205 1 ▶sub _admin_repo_list :Private {
Subroutine _admin_repo_list: called CC=3 SCAR=11.2
1206 1 my ($admin) = @_;
1207  
1208 1 my $repos = $admin->repo_list();
1209 1 ▶ if (@{$repos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$repos;}) { } 1 0 1
1210 1 print "$_\n" for @{$repos};
1211 } else {
1212 0 _say('cli.repo.none', { root => $admin->root });
1213 }
1214  
1215 1 return $EXIT_OK;
1216 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1217  
1218 9 ▶sub _admin_repo_show :Private {
Subroutine _admin_repo_show: called CC=10 SCAR=23.3
1219 9 my ($admin, @args) = @_;
1220  
1221 9 my $info = $admin->repo_show(_require(shift @args, 'repo show', 'NAME'));
1222 9 my $config = $info->{config};
1223 # A missing setting means its default, as in Store::prune_trash and
1224 # Merge::check_delete_limit; 0 keeps the trash forever or switches a
1225 # deletion limit off.
1226 9 ▶ my $days = $config->{trash_days} // $TRASH_DAYS;
Condition66%2/3
$config->{"trash_days"} // $App::FileSync::CLI::TRASH_DAYS
l!l&&r!l&&!r
810
MC/DC0%0/2
$config->{"trash_days"} // $App::FileSync::CLI::TRASH_DAYS
$config->{"trash_days"} $App::FileSync::CLI::TRASH_DAYS
Truth table66%2/3
A // B
A= $config->{"trash_days"}B= $App::FileSync::CLI::TRASH_DAYS
ABresult
1X1
011
000
1227 9 ▶ my $files = $config->{max_deletes} // $MAX_DELETES;
Condition66%2/3
$config->{"max_deletes"} // $App::FileSync::CLI::MAX_DELETES
l!l&&r!l&&!r
810
MC/DC0%0/2
$config->{"max_deletes"} // $App::FileSync::CLI::MAX_DELETES
$config->{"max_deletes"} $App::FileSync::CLI::MAX_DELETES
Truth table66%2/3
A // B
A= $config->{"max_deletes"}B= $App::FileSync::CLI::MAX_DELETES
ABresult
1X1
011
000
1228 9 ▶ my $percent = $config->{max_delete_percent} // $MAX_DELETE_PERCENT;
Condition66%2/3
$config->{"max_delete_percent"} // $App::FileSync::CLI::MAX_DELETE_PERCENT
l!l&&r!l&&!r
810
MC/DC0%0/2
$config->{"max_delete_percent"} // $App::FileSync::CLI::MAX_DELETE_PERCENT
$config->{"max_delete_percent"} $App::FileSync::CLI::MAX_DELETE_PERCENT
Truth table66%2/3
A // B
A= $config->{"max_delete_percent"}B= $App::FileSync::CLI::MAX_DELETE_PERCENT
ABresult
1X1
011
000
1229 9 ▶ my $limits = ($files || $percent)
Branch: 3 branches
BranchTrueFalseTotal
$files || $percent ? : 8 1 9
$files ? : 7 1 8
$percent ? : 7 1 8
Condition100%3/3
$files or $percent
l!l&&r!l&&!r
711
MC/DC100%2/2
$files or $percent
$files $percent
Truth table100%3/3
A or B
A= $filesB= $percent
ABresult
1X1
011
000
1230 ? message('cli.repo.limits', {
1231 files => $files ? message('cli.repo.limit_files', { count => $files }) : message('cli.repo.limit_files_off'),
1232 percent => $percent ? message('cli.repo.limit_percent', { percent => $percent }) : message('cli.repo.limit_percent_off'),
1233 })
1234 : message('cli.repo.limits_off');
1235 _say('cli.repo.show', {
1236 9 ▶ %{$config},
Branch: 2 branches
BranchTrueFalseTotal
$days ? : 8 1 9
@{$info->{'clients'};} ? : 0 9 9
1237 trash => $days ? message('cli.repo.trash_days', { count => $days }) : message('cli.repo.trash_forever'),
1238 limits => $limits,
1239 master => $info->{master},
1240 meta => $info->{meta},
1241 0 clients => @{ $info->{clients} } ? join(', ', @{ $info->{clients} }) : message('cli.repo.no_clients'),
1242 });
1243  
1244 9 return $EXIT_OK;
1245 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1246  
1247 # _admin_log
1248 # Purpose: filesync-admin log: journal events for one or more repos.
1249 # Entry: $admin, then NAME... and options.
1250 # Exit: returns 0.
1251 # Side effects: reads journals; prints (or JSON). Every name is checked
1252 # before anything is printed, so text mode never shows a
1253 # partial answer before failing on a later repo.
1254 27 ▶sub _admin_log :Private {
Subroutine _admin_log: called CC=20 SCAR=30.3
1255 27 my ($admin, @args) = @_;
1256  
1257 27 my (@exits, %opt);
1258 _options(\@args, {
1259 'n=i' => \$opt{limit},
1260 'exit=i@' => \@exits,
1261 81 (map { ("$_=s" => \$opt{$_}) } qw(client since until)),
1262 27 (map { ($_ => \$opt{$_}) } qw(warnings json)),
1263 }, 'admin_log');
1264 27 _check_exits(\@exits);
1265 26 _check_window(@opt{qw(since until)});
1266  
1267 25 my @names = @args;
1268 25 ▶ throw('cli.required', { command => 'log', what => 'NAME' }) unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 25 25
1269 25 ▶ throw('cli.admin.empty_name') if any { !defined $_ || !length $_ } @names;
Subroutine __ANON__: called CC=2 SCAR=8.4
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not defined $_ && length $_; } , @names) 0 25 25
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0030
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1270 25 App::FileSync::Config::load_repo($admin->root, $_) for @names;
1271  
1272 24 my $multiple = @names > 1;
1273 24 my @results;
1274 24 for my $name (@names) {
1275 my %filters = (
1276 (defined $opt{client} && length $opt{client} ? (client => $opt{client}) : ()),
1277 (defined $opt{since} ? (since => parse_since($opt{since})) : ()),
1278 (defined $opt{until} ? (until => parse_until($opt{until})) : ()),
1279 8 ▶ (@exits ? (exits => [ map { $_ + 0 } @exits ]) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$opt{'warnings'} ? : 7 21 28
1280 28 ▶ ($opt{warnings} ? (warnings => 1) : ()),
Branch: 4 branches
BranchTrueFalseTotal
defined $opt{'client'} && length $opt{'client'} ? : 2 26 28
defined $opt{'since'} ? : 0 28 28
defined $opt{'until'} ? : 3 25 28
@exits ? : 7 21 28
Condition66%2/3
defined $opt{"client"} and length $opt{"client"}
!ll&&!rl&&r
2602
MC/DC50%1/2
defined $opt{"client"} and length $opt{"client"}
defined $opt{"client"} length $opt{"client"}
Truth table66%2/3
A and B
A= defined $opt{"client"}B= length $opt{"client"}
ABresult
0X0
100
111
1281 );
1282 my $events = $admin->journal_read(
1283 repo => $name, limit => $opt{limit} // $ADMIN_LOG_LIMIT, client => $opt{client},
1284 since => $opt{since}, until => $opt{until}, exits => \@exits, warnings => $opt{warnings},
1285 28 ▶ );
Condition66%2/3
$opt{"limit"} // $ADMIN_LOG_LIMIT
l!l&&r!l&&!r
3250
MC/DC0%0/2
$opt{"limit"} // $ADMIN_LOG_LIMIT
$opt{"limit"} $ADMIN_LOG_LIMIT
Truth table66%2/3
A // B
A= $opt{"limit"}B= $ADMIN_LOG_LIMIT
ABresult
1X1
011
000
1286 28 push @results, { repo => $name, path => $admin->journal_dir($name), events => $events, %filters };
1287 28 ▶ next if $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"json"} 10 18 28
1288  
1289 # With one repo the lines stand alone; with several, head each group.
1290 18 ▶ _say('cli.log.heading', { repo => $name }) if $multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple 4 14 18
1291 18 ▶ _say('cli.log.no_events', { repo => $name }) unless @{$events};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$events;} 5 13 18
1292 _say('cli.log.event', {
1293 indent => $multiple ? ' ' : '',
1294 ts => format_utc($_->{ts}),
1295 client => $_->{client} // '-',
1296 action => $_->{action} // '?',
1297 detail => _event_detail($_),
1298 18 ▶ }) for @{$events};
Branch: 1 branch
BranchTrueFalseTotal
$multiple ? : 17 57 74
Condition75%3/4
$_->{"client"} // "-"
l!l
731
$_->{"action"} // "?"
l!l
740
MC/DC100%1/1
$_->{"client"} // "-"
$_->{"client"}
MC/DC0%0/1
$_->{"action"} // "?"
$_->{"action"}
Truth table100%2/2
A
A= $_->{"client"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"action"}
Aresult
11
00
1299 }
1300  
1301 24 ▶ print json_encode({ results => \@results }), "\n" if $opt{json};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"json"} 8 16 24
1302 24 return $EXIT_OK;
1303 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1304  
1305 # _event_detail
1306 # Purpose: the free-form tail of a journal line.
1307 # Entry: $event is a journal event.
1308 # Exit: returns the text: path, key=value fields, counters, flags,
1309 # then the error with its white space flattened.
1310 # Side effects: none.
1311 74 ▶sub _event_detail :Private {
Subroutine _event_detail: called CC=6 SCAR=18.0
1312 74 my ($event) = @_;
1313  
1314 74 my @detail = grep { defined } $event->{path};
1315 74 ▶ push @detail, map { ($EVENT_LABEL{$_} // $_) . "=$event->{$_}" } grep { defined $event->{$_} } @EVENT_FIELDS, @EVENT_COUNTERS;
Condition66%2/3
$EVENT_LABEL{$_} // $_
l!l&&r!l&&!r
41730
MC/DC0%0/2
$EVENT_LABEL{$_} // $_
$EVENT_LABEL{$_} $_
Truth table66%2/3
A // B
A= $EVENT_LABEL{$_}B= $_
ABresult
1X1
011
000
1316 74 ▶ push @detail, 'dry-run' if $event->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"dry_run"} 0 74 74
1317 74 ▶ push @detail, 'locked' if $event->{locked};
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"locked"} 6 68 74
1318  
1319 74 ▶ if (defined $event->{error} && length $event->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event->{"error"} and length $event->{"error"}) 13 61 74
Condition66%2/3
defined $event->{"error"} and length $event->{"error"}
!ll&&!rl&&r
61013
MC/DC50%1/2
defined $event->{"error"} and length $event->{"error"}
defined $event->{"error"} length $event->{"error"}
Truth table66%2/3
A and B
A= defined $event->{"error"}B= length $event->{"error"}
ABresult
0X0
100
111
1320 13 (my $text = $event->{error}) =~ s/\s+/ /g;
1321 13 push @detail, $text;
1322 }
1323  
1324 74 return join(' ', @detail);
1325 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1326  
1327 # _admin_reindex
1328 # Purpose: filesync-admin reindex: rebuild the master index.
1329 # Entry: $admin, then NAME and options.
1330 # Exit: returns 0.
1331 # Side effects: rewrites the index (unless --dry-run); prints (or JSON).
1332 6 ▶sub _admin_reindex :Private {
Subroutine _admin_reindex: called CC=5 SCAR=16.1
1333 6 my ($admin, @args) = @_;
1334  
1335 6 my %opt;
1336 6 _options(\@args, { (map { ($_ => \$opt{$_}) } qw(dry-run json)) }, 'reindex');
1337 6 my $name = _require(shift @args, 'reindex', 'NAME');
1338 6 _no_more(\@args, 'reindex');
1339  
1340 6 my $result = $admin->reindex(repo => $name, dry_run => $opt{'dry-run'});
1341 5 ▶ if ($opt{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"json"}) 2 3 5
1342 2 print json_encode($result), "\n";
1343 2 return $EXIT_OK;
1344 }
1345  
1346 _say($opt{'dry-run'} ? 'cli.reindex.would' : 'cli.reindex.done', {
1347 repo => $name,
1348 files => message('word.file', { count => $result->{files} }),
1349 dirs => message('word.empty_dir', { count => $result->{dirs} }),
1350 3 ▶ size => _format_bytes($result->{bytes}),
Branch: 1 branch
BranchTrueFalseTotal
$opt{'dry-run'} ? : 1 2 3
1351 });
1352 3 ▶ _say('cli.reindex.skipped', { path => $_, reason => $result->{skipped}{$_} }) for sort keys %{ $result->{skipped} || {} };
Condition50%1/2
$result->{'skipped'} || {}
l!l
30
MC/DC0%0/1
$result->{'skipped'} || {}
$result->{'skipped'}
Truth table50%1/2
A
A= $result->{'skipped'}
Aresult
11
00
1353  
1354 3 return $EXIT_OK;
1355 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1356  
1357 # _admin_verify
1358 # Purpose: filesync-admin verify: rehash the master against its index.
1359 # Entry: $admin, then NAME and options.
1360 # Exit: returns 4 on drift, else 0.
1361 # Side effects: reads the master; prints (or JSON).
1362 4 ▶sub _admin_verify :Private {
Subroutine _admin_verify: called CC=5 SCAR=16.2
1363 4 my ($admin, @args) = @_;
1364  
1365 4 my %opt;
1366 4 _options(\@args, { deep => \$opt{deep}, 'sample=i' => \$opt{sample}, json => \$opt{json} }, 'admin_verify');
1367 4 my $name = _require(shift @args, 'verify', 'NAME');
1368 4 _no_more(\@args, 'verify');
1369 4 ▶ throw('cli.positive', { command => 'verify', option => '--sample' }) if defined $opt{sample} && $opt{sample} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"sample"} and $opt{"sample"} < 1 0 4 4
Condition66%2/3
defined $opt{"sample"} and $opt{"sample"} < 1
!ll&&!rl&&r
310
MC/DC0%0/2
defined $opt{"sample"} and $opt{"sample"} < 1
defined $opt{"sample"} $opt{"sample"} < 1
Truth table66%2/3
A and B
A= defined $opt{"sample"}B= $opt{"sample"} < 1
ABresult
0X0
100
111
1370  
1371 4 my $report = $admin->verify(repo => $name, deep => $opt{deep}, sample => $opt{sample});
1372 4 ▶ $opt{json} ? print(json_encode($report), "\n") : _print_verify($report, 0);
Branch: 1 branch
BranchTrueFalseTotal
$opt{'json'} ? : 1 3 4
1373  
1374 4 ▶ return $report->{ok} ? $EXIT_OK : $EXIT_DRIFT;
Branch: 1 branch
BranchTrueFalseTotal
$report->{'ok'} ? : 2 2 4
1375 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1376  
1377 # _admin_restore
1378 # Purpose: filesync-admin restore: put a path back from the server trash.
1379 # Entry: $admin, then NAME PATH and options.
1380 # Exit: returns 0.
1381 # Side effects: restores into the master or --to; or lists versions.
1382 3 ▶sub _admin_restore :Private {
Subroutine _admin_restore: called CC=7 SCAR=22.0
1383 3 my ($admin, @args) = @_;
1384  
1385 3 my %opt;
1386 3 _options(\@args, { 'to=s' => \$opt{to}, list => \$opt{list}, 'date=s' => \$opt{date} }, 'admin_restore');
1387 3 my ($name, $path) = splice(@args, 0, 2);
1388 3 ▶ throw('cli.required', { command => 'restore', what => 'NAME and PATH' })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name and defined $path and length $path 0 3 3
Condition33%3/9
defined $name and length $name and defined $path and length $path
!ll&&!rl&&r
003
defined $name and length $name and defined $path
!ll&&!rl&&r
003
defined $name and length $name
!ll&&!rl&&r
003
MC/DC0%0/4
defined $name and length $name and defined $path and length $path
defined $name length $name defined $path length $path
Truth table20%1/5
A and B and C and D
A= defined $nameB= length $nameC= defined $pathD= length $path
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1389 unless defined $name && length $name && defined $path && length $path;
1390 3 _no_more(\@args, 'restore');
1391  
1392 3 my $result = $admin->restore(repo => $name, path => $path, %opt);
1393 2 ▶ return _print_versions($result->{versions}, 'cli.restore.no_versions_in', { path => $path, repo => $name }) if $opt{list};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"list"} 1 1 2
1394  
1395 _say('cli.restore.admin_done', {
1396 repo => $name,
1397 path => $path,
1398 from => $result->{date} // message('cli.restore.the_trash'),
1399 to => $result->{to},
1400 1 ▶ });
Condition33%1/3
$result->{"date"} // message("cli.restore.the_trash")
l!l&&r!l&&!r
100
MC/DC0%0/2
$result->{"date"} // message("cli.restore.the_trash")
$result->{"date"} message("cli.restore.the_trash")
Truth table33%1/3
A // B
A= $result->{"date"}B= message("cli.restore.the_trash")
ABresult
1X1
011
000
1401 1 return $EXIT_OK;
1402 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1403  
1404 # _admin_client_allow / _admin_client_list / _admin_client_forget
1405 # Purpose: filesync-admin client allow|list|forget.
1406 # Entry: $admin, then the command's arguments.
1407 # Exit: returns 0.
1408 # Side effects: allow and forget change the repo; all print.
1409 0 ▶sub _admin_client_allow :Private {
Subroutine _admin_client_allow: not called CC=1 SCAR=6.9
1410 0 my ($admin, @args) = @_;
1411  
1412 0 my %opt;
1413 0 _options(\@args, { 'repo=s' => \$opt{repo}, 'role=s' => \$opt{role} }, 'client_allow');
1414 0 my $client = _require(shift @args, 'client allow', 'ID');
1415 0 my $repo = _require($opt{repo}, 'client allow', '--repo');
1416  
1417 0 my $result = $admin->client_allow(repo => $repo, client => $client, role => $opt{role});
1418 0 _say('cli.client.allowed', { %{$result}, repo => $repo });
1419 0 return $EXIT_OK;
1420 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1421  
1422 4 ▶sub _admin_client_list :Private {
Subroutine _admin_client_list: called CC=4 SCAR=13.9
1423 4 my ($admin, @args) = @_;
1424  
1425 4 my %opt;
1426 4 _options(\@args, { 'repo=s' => \$opt{repo} }, 'client_list');
1427 4 my $repo = _require($opt{repo}, 'client list', '--repo');
1428  
1429 3 my $clients = $admin->client_list($repo);
1430 3 ▶ if (@{$clients}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$clients;}) { } 2 1 3
1431 2 ▶ _say('cli.client.row', { %{$_}, state => message($_->{base} ? 'cli.client.synced' : 'cli.client.never') }) for @{$clients};
Branch: 1 branch
BranchTrueFalseTotal
$_->{'base'} ? : 1 3 4
1432 } else {
1433 1 _say('cli.client.none', { repo => $repo });
1434 }
1435  
1436 3 return $EXIT_OK;
1437 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1438  
1439 3 ▶sub _admin_client_forget :Private {
Subroutine _admin_client_forget: called CC=2 SCAR=7.0
1440 3 my ($admin, @args) = @_;
1441  
1442 3 my %opt;
1443 3 _options(\@args, { 'repo=s' => \$opt{repo} }, 'client_forget');
1444 3 my $repo = _require($opt{repo}, 'client forget', '--repo');
1445 3 my $client = _require(shift @args, 'client forget', 'ID');
1446  
1447 3 my $result = $admin->client_forget(repo => $repo, client => $client);
1448 3 ▶ _say($result->{removed} ? 'cli.client.forgot' : 'cli.client.nothing', { client => $client, path => $result->{path} });
Branch: 1 branch
BranchTrueFalseTotal
$result->{'removed'} ? : 3 0 3
1449 3 return $EXIT_OK;
1450 413 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1451  
1452 413 1;
1453  
1454 __END__
↑ Summary blib/lib/App/FileSync/Args.pm »