blib/lib/App/FileSync/CLI.pm

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