blib/lib/App/FileSync/Server.pm

filter: Statementstmt 99.0% 637 / 643 Branchbran 76.9% 160 / 208 Conditioncond 54.2% 83 / 153 MC/DCmcdc 20.6% 18 / 87 Subroutinesub 100% 102 / 102 Podpod 100% 3 / 3 totaltotal 83.8% 1003 / 1196 CC 170 SCAR 19.1
CC
170
Cov
88%
_commit_upload
20.4
validate_forced_command
15.0
_manifest
13.7
CRAP 6.8
« blib/lib/App/FileSync/Admin.pm ↑ Summary blib/lib/App/FileSync/Trash.pm »
1 package App::FileSync::Server;
2  
3 # The server end of one sync session. It is started by sshd (or the local
4 # transport) with the repo, client and role pinned on its command line, speaks
5 # the lock-step protocol on its connection, and only ever does what its own
6 # plan allows: a client cannot write, read, delete or acknowledge a path the
7 # server did not plan for it.
8  
9 414 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 414 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 414 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 414 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 414 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 414 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 414 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 414 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 414 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 414 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
21 414 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 414 ▶use parent 'App::FileSync::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 414 ▶use App::FileSync qw(validate_relpath validate_client_id validate_repo_name name_with_suffix);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 414 ▶use App::FileSync::Args qw(named);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 414 ▶use App::FileSync::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 $PROTOCOL_VERSION $ROLE_CLIENT @ROLES $TYPE_FILE $TYPE_DIR
29 $MODE_MASK $DEFAULT_FILE_MODE $MAX_CHUNK_BYTES $SHA256_HEX
30 $EXIT_OK $EXIT_FATAL $EXIT_LOCKED $EXIT_WARNINGS $SERVER_PROGRAM
31 $STAT_MODE $STAT_SIZE $STAT_MTIME $SHA_BITS
32 414 );
33 414 ▶use App::FileSync::Delta;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 414 ▶use App::FileSync::I18N qw(message os_error strip_location);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 414 ▶use App::FileSync::IO qw(list_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 414 ▶use App::FileSync::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 414 ▶use App::FileSync::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 414 ▶use App::FileSync::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 414 ▶use App::FileSync::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 414 ▶use version; our $VERSION = version->declare('0.002.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Package variables for compatibility: the CLI checks $PROTO, and tests may
44 # localise $CHUNK.
45 414 our $CHUNK = 262_144;
46 414 our $PROTO = $PROTOCOL_VERSION;
47  
48 # Bounds on a pull signature the client uploads, so a hostile client cannot
49 # make the server allocate or hash without limit.
50 414 Readonly::Scalar my $MAX_SIGNATURE_BLOCKS => 16_384;
51 414 Readonly::Scalar my $MIN_SIGNATURE_BLOCK => 1024;
52 414 Readonly::Scalar my $MAX_SIGNATURE_BLOCK => 1024 * 1024;
53  
54 # Wire cost of one delta record, for deciding whether a delta is worth it;
55 # the constant covers the reply headers.
56 414 Readonly::Scalar my $DELTA_OP_BYTES => 12;
57 414 Readonly::Scalar my $DELTA_OVERHEAD => 64;
58  
59 # Unsigned decimal integers, and mtimes (which may be fractional or, for a
60 # file dated before 1970, negative).
61 414 Readonly my $UINT => qr/\A\d+\z/;
62 414 Readonly my $MTIME => qr/\A-?\d+(?:\.\d+)?\z/;
63  
64 # The protocol's commands and the method that serves each.
65 414 Readonly my %COMMANDS => (
66 manifest => '_manifest',
67 plan => '_plan',
68 get => '_get',
69 put => '_put',
70 chunk => '_chunk',
71 chunk_end => '_chunk_end',
72 sig => '_signature',
73 delta => '_delta_start',
74 dcopy => '_delta_copy',
75 dlit => '_delta_literal',
76 delta_end => '_delta_end',
77 dsig => '_pull_signature',
78 dop => '_pull_op',
79 mkdir => '_mkdir',
80 del => '_delete',
81 ack => '_ack',
82 done => '_finish',
83 );
84  
85 # Which planned actions each kind of request may serve. "conflict:client"
86 # is a conflict the client's version won; "conflict:remote" one the master's won.
87 # The client wins a conflict only with a file (Merge::_conflict), so mkdir
88 # never serves one.
89 414 Readonly my %MAY => (
90 read => [ 'pull', 'conflict:remote' ],
91 write => [ 'push', 'conflict:client' ],
92 delta => [ 'push' ],
93 pull => [ 'pull' ],
94 mkdir => [ 'push' ],
95 delete => [ 'delete_remote' ],
96 ack => [ 'pull', 'delete_local', 'conflict:remote' ],
97 );
98  
99 # The flags a forced command may carry, and the pinned field each must match.
100 414 Readonly my %FORCED_FLAGS => (repo => 'repo', client => 'client', role => 'role', proto => 'proto');
101  
102 =encoding utf8
103  
104 =head1 NAME
105  
106 App::FileSync::Server - serve one sync session against a repo's master
107  
108 =head1 SYNOPSIS
109  
110 my $server = App::FileSync::Server->new(
111 root => '/srv/filesync', repo => 'docs', client => 'laptop', role => 'client',
112 connection => App::FileSync::Protocol->new(in => \*STDIN, out => \*STDOUT),
113 );
114 exit($server->run ? 0 : 1);
115  
116 =head1 DESCRIPTION
117  
118 A session is: hello, manifest, plan, then the transfers the plan lists, then
119 C<done>. The server holds the repo lock throughout, so sessions on one repo
120 never interleave. It updates the master index and the client's base only in
121 C<done>, after every planned action has been applied; a session that dies part
122 way leaves both as they were, and the next session replans from the tree.
123  
124 =head1 METHODS
125  
126 =head2 new
127  
128 Purpose: prepare a session.
129  
130 Args: C<root>, C<repo>, C<client>, C<role> (C<client> or C<replica>),
131 C<connection> (an L<App::FileSync::Protocol>), optional C<logger> and
132 C<language>.
133  
134 Returns: the server object.
135  
136 Side Effects: builds a logger from the settings when none is given.
137  
138 Usage:
139  
140 my $server = App::FileSync::Server->new(%pinned, connection => $connection);
141  
142 =head3 EXAMPLE
143  
144 my $server = App::FileSync::Server->new(
145 root => $root, repo => 'docs', client => 'laptop', role => 'client', connection => $conn,
146 );
147  
148 =head3 API SPECIFICATION
149  
150 =head4 INPUT
151  
152 {
153 root => { type => 'string', min => 1 },
154 repo => { type => 'string', min => 1, max => 64 },
155 client => { type => 'string', min => 1, max => 64 },
156 role => { type => 'string', memberof => [ 'client', 'replica' ] },
157 connection => { type => 'object', can => [ 'send', 'recv' ] },
158 logger => { type => 'object', optional => 1 },
159 language => { type => 'string', optional => 1 },
160 }
161  
162 =head4 OUTPUT
163  
164 { type => 'object', isa => 'App::FileSync::Server' }
165  
166 =head3 MESSAGES
167  
168 Key Meaning Resolution
169 server.missing a required argument is absent pass it
170 server.bad_role role is not client or replica fix the forced command
171 server.bad_id repo or client is not an id fix the forced command
172 server.no_connection no connection was given pass connection
173  
174 =head3 FORMAL SPECIFICATION
175  
176 pre root ≠ "" ∧ repo ∈ Id ∧ client ∈ Id ∧ role ∈ {client, replica} ∧ connection ≠ ⊥
177 post finished = 0 ∧ plan = ∅ ∧ uploads = ∅
178  
179 =cut
180  
181 271 ▶sub new {
Subroutine new: called CC=9 SCAR=23.3
Pod documented
182 271 my $class = shift;
183 271 my $args = named(\@_, {
184 root => { type => 'string', optional => 1 },
185 repo => { type => 'string', optional => 1 },
186 client => { type => 'string', optional => 1 },
187 role => { type => 'string', optional => 1 },
188 connection => { type => 'object', optional => 1 },
189 logger => { type => 'object', optional => 1 },
190 language => { type => 'string', optional => 1 },
191 }, 'Server->new');
192  
193 # Checked here rather than by the schema so the messages read as a
194 # server refusal rather than an argument error.
195 271 for my $required (qw(root repo client role)) {
196 App::FileSync::I18N::throw('server.missing', { what => $required })
197 1084 ▶ unless defined $args->{$required} && length $args->{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$required} and length $args->{$required} 0 1084 1084
Condition33%1/3
defined $args->{$required} and length $args->{$required}
!ll&&!rl&&r
001084
MC/DC0%0/2
defined $args->{$required} and length $args->{$required}
defined $args->{$required} length $args->{$required}
Truth table33%1/3
A and B
A= defined $args->{$required}B= length $args->{$required}
ABresult
0X0
100
111
198 }
199 App::FileSync::I18N::throw('server.bad_role', { role => $args->{role} })
200 271 ▶ unless any { $_ eq $args->{role} } @ROLES;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $args->{"role"}; } , @App::FileSync::Server::ROLES) 0 271 271
201  
202 271 for my $check ([ repo => \&validate_repo_name ], [ client => \&validate_client_id ]) {
203 542 my ($field, $validate) = @{$check};
204 542 my ($ok, $reason) = $validate->($args->{$field});
205 542 ▶ App::FileSync::I18N::throw('server.bad_id', { reason => $reason }) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 542 542
206 }
207 271 ▶ App::FileSync::I18N::throw('server.no_connection') unless $args->{connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"connection"} 0 271 271
208  
209 return bless {
210 1355 ▶ (map { $_ => $args->{$_} } qw(root repo client role language)),
Condition66%2/3
$args->{'logger'} || App::FileSync::Settings::logger()
l!l&&r!l&&!r
26560
MC/DC0%0/2
$args->{'logger'} || App::FileSync::Settings::logger()
$args->{'logger'} App::FileSync::Settings::logger()
Truth table66%2/3
A || B
A= $args->{'logger'}B= App::FileSync::Settings::logger()
ABresult
1X1
011
000
211 conn => $args->{connection},
212 271 logger => $args->{logger} || App::FileSync::Settings::logger(),
213 dry_run => 0,
214 force => 0,
215 finished => 0,
216 uploads => {},
217 pull_deltas => {},
218 client_manifest => {},
219 plan => {},
220 index => {},
221 base => {},
222 base_after => {},
223 applied => {},
224 }, $class;
225 }
226  
227 =head2 run
228  
229 Purpose: serve the whole session.
230  
231 Args: none.
232  
233 Returns: 1 when the session finished, 0 when it failed (the client has been
234 sent the error).
235  
236 Side Effects: takes the repo lock; reads and writes the master, its index, the
237 client's base, the trash and the journal; prunes the trash and removes stale
238 temporaries. A failed session is journalled (exit 2 for a lock refusal,
239 otherwise 1) unless it never got as far as opening the repo.
240  
241 Usage:
242  
243 exit($server->run ? 0 : 1);
244  
245 =head3 EXAMPLE
246  
247 my $ok = $server->run;
248 warn "session failed\n" unless $ok;
249  
250 =head3 API SPECIFICATION
251  
252 =head4 INPUT
253  
254 [ { name => 'self', type => 'object' } ]
255  
256 =head4 OUTPUT
257  
258 { type => 'integer', memberof => [ 0, 1 ] }
259  
260 =head3 MESSAGES
261  
262 Key Meaning Resolution
263 server.locked another session holds the lock the client retries later
264 server.not_allowed the client is not registered filesync-admin client allow
265 server.wrong_role the client is registered otherwise fix the key's --role
266 server.proto the client speaks another protocol upgrade one side
267 server.pinned the hello disagrees with the pin fix the client config
268 server.unknown an unknown command arrived upgrade one side
269  
270 Raised once the session is open. Each ends the session: the open uploads
271 are discarded, the failure is journalled, and the text (without Perl's
272 file and line) reaches the client as C<< { ok => 0, fatal => 1, error => ... } >>.
273 In that text a path in the master is relative to the repo, and any other
274 server path starts with C<[server root]>: the client is not told where the
275 server keeps its files. The server's log is written the same way, because
276 over ssh its standard error reaches the client; the journal, which stays on
277 the server, keeps the full paths.
278 Most mean a client bug or a hostile client; the resolution is to fix the
279 client, or simply to rerun the sync.
280  
281 Key Meaning
282 server.manifest_list the manifest is not a list
283 server.manifest_entry a manifest entry is not an object
284 server.manifest_path a manifest path is unsafe
285 server.manifest_field a manifest t, h, s, m or mode field is malformed
286 server.refused the deletion guard stopped the plan (rerun with --force if intended)
287 server.unplanned an operation names a path that is not in the plan
288 server.not_planned the path's planned action does not allow this operation
289 server.bad_field an off, max, len, s, mode or h field is malformed
290 server.conflict_name the conflict copy is named other than the plan says
291 server.conflict_unsafe the planned conflict name is not a safe path
292 server.conflict_same the planned conflict name equals the path
293 server.conflict_exists something already exists at the conflict name
294 server.conflict_move the master's copy could not be moved aside
295 server.bad_ack an ack disagrees with the planned action
296 server.busy a second put for a path already being received
297 server.no_put chunk or chunk_end without a put
298 server.too_large one chunk is larger than the chunk limit
299 server.overflow the upload is larger than its announced size
300 server.hash_mismatch the received content does not have the announced hash
301 server.size_mismatch the received content does not have the announced size
302 server.no_delta delta data without a delta start
303 server.bad_copy a delta copy reaches outside the base file
304 server.bad_signature a pull signature is malformed
305 server.no_signature a pull delta operation without a signature
306 server.cannot_stat a planned file vanished from the master
307 server.not_file a get for a path that is a directory
308 server.unfinished done arrived before every planned action completed
309  
310 =head3 FORMAL SPECIFICATION
311  
312 ΔFS
313 run = 1 ⇔ hello ⨾ open ⨾ lock ⨾ loop ⨾ done succeed
314 run = 0 ⇒ index' = index ∧ base' = base
315  
316 =head3 PSEUDOCODE
317  
318 read and check the hello against the pinned arguments
319 open the repo; check the client is allowed in its role
320 take the lock (refuse with code "locked" otherwise)
321 load the index and the client's base; tidy temporaries and the trash
322 send the policy, then serve commands until done
323 on any error: log it, journal the failed session, discard partial
324 uploads, and send the error to the client
325  
326 =cut
327  
328 265 ▶sub run {
Subroutine run: called CC=6 SCAR=17.9
Pod documented
329 265 my ($self) = @_;
330  
331 265 my $connection = $self->{conn};
332  
333 265 my $ok = eval {
334 265 $self->_validate_hello($connection->recv());
335  
336 my $store = $self->{store} = App::FileSync::Store->new(
337 root => $self->{root}, repo => $self->{repo}, language => $self->{language},
338 264 );
339 263 $self->_check_client_allowed();
340  
341 262 $self->{lock} = $store->acquire_lock();
342 262 ▶ unless ($self->{lock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lock"}) 3 259 262
343 3 $self->{refusal} = 'locked';
344 3 $self->_throw('server.locked', { repo => $self->{repo} });
345 }
346  
347 259 $self->{index} = $store->load_index();
348 259 $self->{base} = $store->load_base($self->{client});
349 259 $store->clean_stale_temps();
350 259 ▶ $store->prune_trash() unless $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dry_run"} 250 9 259
351  
352 259 $self->{logger}->info('session opened', { map { $_ => $self->{$_} } qw(repo client role) });
353  
354 259 my $policy = $store->config();
355 $connection->send({
356 ok => 1,
357 proto => $PROTO,
358 server => { os => $^O },
359 repo => { name => $self->{repo} },
360 259 policy => { map { $_ => $policy->{$_} } qw(ignore trash_days max_deletes max_delete_percent) },
361 });
362  
363 259 $self->_loop();
364 194 1;
365 };
366  
367 265 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 194 71 265
368  
369 # The client shows this text to a person, so no Perl file and line, and
370 # only where in the repo, not where the server keeps it. The log is
371 # rewritten too: over ssh the server's stderr reaches the client. Only
372 # the journal, which stays on the server, keeps the absolute paths.
373 71 ▶ my $error = strip_location($@) || $self->i18n('server.unknown_error');
Condition33%1/3
strip_location($@) || $self->i18n('server.unknown_error')
l!l&&r!l&&!r
7100
MC/DC0%0/2
strip_location($@) || $self->i18n('server.unknown_error')
strip_location($@) $self->i18n('server.unknown_error')
Truth table33%1/3
A || B
A= strip_location($@)B= $self->i18n('server.unknown_error')
ABresult
1X1
011
000
374 71 my $shown = $self->_for_client($error);
375 71 $self->{logger}->error($shown);
376 71 $self->_discard_uploads();
377 71 $self->_journal_failed_session($error);
378  
379 71 eval {
380 71 ▶ $connection->send({ ok => 0, error => $shown, fatal => 1, ($self->{refusal} ? (code => $self->{refusal}) : ()) });
Branch: 1 branch
BranchTrueFalseTotal
$self->{'refusal'} ? : 3 68 71
381 };
382 71 return 0;
383 }
384  
385 # _for_client
386 # Purpose: take the server's file layout out of an error for the client
387 # (and for the log, whose stderr reaches the client over ssh).
388 # Entry: $text is an error as the server logs it.
389 # Exit: returns the text with each path under the master made relative
390 # to the repo ("x", not "/srv/filesync/master/docs/x") and any
391 # other mention of the server root replaced by a placeholder.
392 # Side effects: none.
393 77 ▶sub _for_client :Private {
Subroutine _for_client: called CC=1 SCAR=0.0
394 77 my ($self, $text) = @_;
395  
396 # new() refuses a server without a root or a repo, so both are set. The
397 # store builds its paths with catdir, so match the canonical form: a root
398 # given as "/srv/filesync/" appears as "/srv/filesync/master/...".
399 77 my $root = File::Spec->canonpath($self->{root});
400 77 my $master = File::Spec->catdir($root, 'master', $self->{repo});
401 77 $text =~ s{\Q$master\E[\\/]}{}g;
402 # Only a whole path component: a root of /srv must not touch /srvdata.
403 77 my $placeholder = $self->i18n('server.root_placeholder');
404 77 $text =~ s{\Q$root\E(?=[\\/'"\s:,)]|\z)}{$placeholder}g;
405  
406 77 return $text;
407 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
408  
409 =head2 validate_forced_command
410  
411 Purpose: check the command a client asked sshd to run against the arguments
412 pinned in F<authorized_keys>.
413  
414 The pinned arguments are the authority for a session. sshd passes the command
415 the client asked for in C<SSH_ORIGINAL_COMMAND>; it must agree with the pinned
416 repo, client, role and protocol and ask for nothing else: in particular it may
417 not choose its own C<--root>.
418  
419 Args: C<$original> (the requested command; undef or blank is accepted),
420 C<\%pinned> (C<repo>, C<client>, C<role>).
421  
422 Returns: C<(1, undef)> or C<(0, $reason)>. A list, deliberately: callers
423 write C<my ($ok, $why) = ...>, matching L<App::FileSync/validate_relpath>.
424  
425 Side Effects: none.
426  
427 Usage:
428  
429 my ($ok, $reason) = App::FileSync::Server::validate_forced_command($ENV{SSH_ORIGINAL_COMMAND}, \%pinned);
430  
431 =head3 EXAMPLE
432  
433 my ($ok, $why) = App::FileSync::Server::validate_forced_command(
434 'filesync-server --repo docs --client laptop --role client --proto 2',
435 { repo => 'docs', client => 'laptop', role => 'client' },
436 ); # (1, undef)
437  
438 =head3 API SPECIFICATION
439  
440 =head4 INPUT
441  
442 [
443 { name => 'original', type => 'string', optional => 1 },
444 { name => 'pinned', type => 'hashref' },
445 ]
446  
447 =head4 OUTPUT
448  
449 { type => 'arrayref', min => 2, max => 2 } # returned as a two-element list
450  
451 =head3 MESSAGES
452  
453 Key Meaning
454 forced.program the command runs some other program
455 forced.argument a token is not a --flag
456 forced.no_value a flag has no value
457 forced.flag the flag is not repo, client, role or proto
458 forced.proto the protocol differs from this server's
459 forced.pinned the value differs from the pinned one
460  
461 =head3 FORMAL SPECIFICATION
462  
463 let ts = tokens(original)
464 ok ⇔ (ts = ⟨⟩) ∨ ((head ts ∈ FLAG ∨ basename(head ts) = filesync-server) ∧
465 ∀ (f, v) : flags(ts) • f ∈ {repo, client, role, proto} ∧
466 (f = proto ⇒ v = PROTO) ∧ (f ≠ proto ⇒ v = pinned(f)))
467  
468 =head3 PSEUDOCODE
469  
470 accept an empty command
471 split on white space; allow a leading filesync-server path
472 for each --flag[=value] (value from the next token if not attached):
473 reject an unknown flag, a missing value, a wrong protocol
474 reject a value that differs from the pinned one
475  
476 =cut
477  
478 29 ▶sub validate_forced_command {
Subroutine validate_forced_command: called CC=15 SCAR=27.1
Pod documented
479 29 my ($original, $pinned) = @_;
480  
481 29 ▶ my @tokens = defined $original ? split(' ', $original) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $original ? : 28 1 29
482 29 ▶ return (1, undef) unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 3 26 29
483  
484 26 ▶ if ($tokens[0] !~ /\A--/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tokens[0] =~ /\A--/) 25 1 26
485 25 my $program = shift @tokens;
486 25 ▶ return (0, message('forced.program', { program => $program }))
Branch: 1 branch
BranchTrueFalseTotal
unless $program =~ m[(?:\A|/)\Q$App::FileSync::Server::SERVER_PROGRAM\E\z] 1 24 25
487 unless $program =~ m{(?:\A|/)\Q$SERVER_PROGRAM\E\z};
488 }
489  
490 25 ▶ while (@tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 79 14 93
491 79 my $token = shift @tokens;
492 79 ▶ return (0, message('forced.argument', { token => $token }))
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\A--([a-z][a-z-]*)(?:=(.*))?\z/s 1 78 79
493 unless $token =~ /\A--([a-z][a-z-]*)(?:=(.*))?\z/s;
494  
495 78 my ($flag, $value) = ($1, $2);
496 78 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 73 5 78
497 73 ▶ return (0, message('forced.no_value', { flag => $flag })) unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 1 72 73
498 72 $value = shift @tokens;
499 }
500  
501 77 ▶ my $field = $FORCED_FLAGS{$flag} or return (0, message('forced.flag', { flag => $flag }));
Branch: 1 branch
BranchTrueFalseTotal
my $field = $FORCED_FLAGS{$flag} or return 0, message("forced.flag", {"flag", $flag}) 74 3 77
502  
503 74 ▶ if ($field eq 'proto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "proto") 14 60 74
504 14 ▶ return (0, message('forced.proto', { proto => $value })) unless $value eq $PROTO;
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $App::FileSync::Server::PROTO 1 13 14
505 13 next;
506 }
507  
508 60 my $expected = $pinned->{$field};
509 60 ▶ return (0, message('forced.pinned', { field => $field, value => $value, expected => $expected // '' }))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expected and $value eq $expected 5 55 60
Condition60%3/5
defined $expected and $value eq $expected
!ll&&!rl&&r
0555
$expected // ""
l!l
50
MC/DC50%1/2
defined $expected and $value eq $expected
defined $expected $value eq $expected
MC/DC0%0/1
$expected // ""
$expected
Truth table66%2/3
A and B
A= defined $expectedB= $value eq $expected
ABresult
0X0
100
111
Truth table50%1/2
A
A= $expected
Aresult
11
00
510 unless defined $expected && $value eq $expected;
511 }
512  
513 14 return (1, undef);
514 }
515  
516 # _journal_failed_session
517 # Purpose: record a session that never reached done, so the admin sees
518 # the attempt with the same exit code the client records.
519 # Entry: $error is the message sent to the client.
520 # Exit: returns 1 if journalled, else 0.
521 # Side effects: appends to the journal. Nothing is journalled without a store
522 # (a bad hello, say) or on a dry run, and a journal write that
523 # fails is logged, never allowed to mask the original error.
524 71 ▶sub _journal_failed_session :Private {
Subroutine _journal_failed_session: called CC=10 SCAR=23.5
525 71 my ($self, $error) = @_;
526  
527 71 ▶ return 0 if $self->{finished} || $self->{session_journalled} || $self->{dry_run} || !$self->{store};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} or $self->{"session_journalled"} or $self->{"dry_run"} or not $self->{"store"} 3 68 71
Condition66%6/9
$self->{"finished"} or $self->{"session_journalled"} or $self->{"dry_run"} or not $self->{"store"}
l!l&&r!l&&!r
1268
$self->{"finished"} or $self->{"session_journalled"} or $self->{"dry_run"}
l!l&&r!l&&!r
0170
$self->{"finished"} or $self->{"session_journalled"}
l!l&&r!l&&!r
0071
MC/DC50%2/4
$self->{"finished"} or $self->{"session_journalled"} or $self->{"dry_run"} or not $self->{"store"}
$self->{"finished"} $self->{"session_journalled"} $self->{"dry_run"} not $self->{"store"}
Truth table60%3/5
A or B or C or not(D)
A= $self->{"finished"}B= $self->{"session_journalled"}C= $self->{"dry_run"}D= not $self->{"store"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
528  
529 68 ▶ my $locked = ($self->{refusal} || '') eq 'locked';
Condition100%2/2
$self->{'refusal'} || ''
l!l
365
MC/DC100%1/1
$self->{'refusal'} || ''
$self->{'refusal'}
Truth table100%2/2
A
A= $self->{'refusal'}
Aresult
11
00
530 68 my $ok = eval {
531 68 ▶ $self->_journal({
Branch: 2 branches
BranchTrueFalseTotal
$locked ? : 3 65 68
$locked ? : 3 65 68
532 action => 'session',
533 result => 'error',
534 exit => $locked ? $EXIT_LOCKED : $EXIT_FATAL,
535 error => $error,
536 ($locked ? (locked => 1) : ()),
537 });
538 68 1;
539 };
540 68 ▶ $self->{logger}->warn($self->_for_client($self->i18n('server.journal_failed', { error => strip_location($@) }))) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 68 68
541  
542 68 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 68 0 68
543 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
544  
545 # _validate_hello
546 # Purpose: check the client's hello against the pinned session.
547 # Entry: $hello is the first message.
548 # Exit: returns 1.
549 # Side effects: records dry_run and force; raises server.proto or
550 # server.pinned.
551 264 ▶sub _validate_hello :Private {
Subroutine _validate_hello: called CC=8 SCAR=21.8
552 264 my ($self, $hello) = @_;
553  
554 264 ▶ $self->_throw('server.proto') unless defined $hello->{proto} && $hello->{proto} eq $PROTO;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hello->{"proto"} and $hello->{"proto"} eq $App::FileSync::Server::PROTO 0 264 264
Condition33%1/3
defined $hello->{"proto"} and $hello->{"proto"} eq $App::FileSync::Server::PROTO
!ll&&!rl&&r
00264
MC/DC0%0/2
defined $hello->{"proto"} and $hello->{"proto"} eq $App::FileSync::Server::PROTO
defined $hello->{"proto"} $hello->{"proto"} eq $App::FileSync::Server::PROTO
Truth table33%1/3
A and B
A= defined $hello->{"proto"}B= $hello->{"proto"} eq $App::FileSync::Server::PROTO
ABresult
0X0
100
111
555  
556 264 for my $field (qw(client repo role)) {
557 792 ▶ my $given = $hello->{$field} // '';
Condition50%1/2
$hello->{$field} // ""
l!l
7920
MC/DC0%0/1
$hello->{$field} // ""
$hello->{$field}
Truth table50%1/2
A
A= $hello->{$field}
Aresult
11
00
558 $self->_throw('server.pinned', { field => $field, expected => $self->{$field}, given => $given })
559 792 ▶ unless $given eq $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $given eq $self->{$field} 0 792 792
560 }
561  
562 # case_insensitive is part of the hello but only the client acts on it.
563 264 ▶ $self->{dry_run} = $hello->{dry_run} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hello->{'dry_run'} ? : 9 255 264
564 264 ▶ $self->{force} = $hello->{force} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hello->{'force'} ? : 8 256 264
565 264 return 1;
566 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
567  
568 # _check_client_allowed
569 # Purpose: enforce the repo's client list.
570 # Entry: the store is open.
571 # Exit: returns 1.
572 # Side effects: raises server.not_allowed or server.wrong_role. An empty
573 # list allows everyone (a repo nobody has restricted yet).
574 263 ▶sub _check_client_allowed :Private {
Subroutine _check_client_allowed: called CC=7 SCAR=21.5
575 263 my ($self) = @_;
576  
577 263 ▶ my $clients = $self->{store}->config()->{clients} || {};
Condition50%1/2
$self->{'store'}->config->{'clients'} || {}
l!l
2630
MC/DC0%0/1
$self->{'store'}->config->{'clients'} || {}
$self->{'store'}->config->{'clients'}
Truth table50%1/2
A
A= $self->{'store'}->config->{'clients'}
Aresult
11
00
578 263 ▶ return 1 unless %{$clients};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$clients;} 0 263 263
579  
580 my $entry = $clients->{ $self->{client} }
581 263 ▶ or $self->_throw('server.not_allowed', { client => $self->{client}, repo => $self->{repo} });
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $$clients{$self->{"client"}} or $self->_throw("server.not_allowed", {"client", $self->{"client"}, "repo", $self->{"repo"}}) 262 1 263
582  
583 262 ▶ my $role = (ref($entry) eq 'HASH' && $entry->{role}) || $ROLE_CLIENT;
Condition33%2/6
ref $entry eq 'HASH' && $entry->{'role'} || $App::FileSync::Server::ROLE_CLIENT
l!l&&r!l&&!r
26200
ref $entry eq "HASH" and $entry->{"role"}
!ll&&!rl&&r
00262
MC/DC0%0/3
ref $entry eq 'HASH' && $entry->{'role'} || $App::FileSync::Server::ROLE_CLIENT
ref $entry eq "HASH" $entry->{"role"} $App::FileSync::Server::ROLE_CLIENT
Truth table20%1/5
A and B || C
A= ref $entry eq "HASH"B= $entry->{"role"}C= $App::FileSync::Server::ROLE_CLIENT
ABCresult
11X1
0X11
1011
0X00
1000
584 $self->_throw('server.wrong_role', { client => $self->{client}, registered => $role, asked => $self->{role} })
585 262 ▶ unless $role eq $self->{role};
Branch: 1 branch
BranchTrueFalseTotal
unless $role eq $self->{"role"} 0 262 262
586  
587 262 return 1;
588 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
589  
590 # _loop
591 # Purpose: serve commands until done.
592 # Entry: the hello has been answered.
593 # Exit: returns 1 once _finish has run.
594 # Side effects: whatever the commands do; raises server.unknown.
595 259 ▶sub _loop :Private {
Subroutine _loop: called CC=6 SCAR=18.0
596 259 my ($self) = @_;
597  
598 259 ▶ until ($self->{finished}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"finished"}) 1915 194 2109
599 1915 my $message = $self->{conn}->recv();
600 1905 my $received = $self->{conn}->bytes_received();
601 1905 my $ok = eval {
602 1905 ▶ my $command = $message->{cmd} // '';
Condition50%1/2
$message->{"cmd"} // ""
l!l
19050
MC/DC0%0/1
$message->{"cmd"} // ""
$message->{"cmd"}
Truth table50%1/2
A
A= $message->{"cmd"}
Aresult
11
00
603 1905 ▶ my $method = $COMMANDS{$command} or $self->_throw('server.unknown', { command => $command });
Branch: 1 branch
BranchTrueFalseTotal
my $method = $COMMANDS{$command} or $self->_throw("server.unknown", {"command", $command}) 1905 0 1905
604 1905 $self->$method($message);
605 1850 1;
606 };
607 1905 ▶ next if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1850 55 1905
608  
609 55 my $error = $@;
610 55 ▶ $self->_discard_payload($message) if $self->{conn}->bytes_received() == $received;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"conn"}->bytes_received == $received 54 1 55
611 55 die $error;
612 }
613  
614 194 return 1;
615 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
616  
617 # _discard_payload
618 # Purpose: read and drop the payload of a request that was refused before
619 # its handler read it.
620 # Entry: $message announced len bytes that are still on the wire.
621 # Exit: returns 1.
622 # Side effects: consumes the bytes. The client writes a payload straight
623 # after its header, so a server that exits without reading it
624 # can close the pipe under that write: the client then sees a
625 # broken pipe (on Windows, where the server often wins the race)
626 # instead of the refusal. A bad length, a failed transport or a
627 # read error leaves the bytes where they are.
628 54 ▶sub _discard_payload :Private {
Subroutine _discard_payload: called CC=6 SCAR=18.4
629 54 my ($self, $message) = @_;
630  
631 54 my $length = $message->{len};
632 return 1 unless defined $length && $length =~ $UINT && $length > 0
633 54 ▶ && $length <= $App::FileSync::Protocol::MAX_WIRE && !$self->{conn}->transport_failed();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length and $length =~ /$UINT/ and $length > 0 and $length <= $App::FileSync::Protocol::MAX_WIRE and not $self->{"conn"}->transport_failed 50 4 54
Condition66%8/12
defined $length and $length =~ /$UINT/ and $length > 0 and $length <= $App::FileSync::Protocol::MAX_WIRE and not $self->{"conn"}->transport_failed
!ll&&!rl&&r
5004
defined $length and $length =~ /$UINT/ and $length > 0 and $length <= $App::FileSync::Protocol::MAX_WIRE
!ll&&!rl&&r
5004
defined $length and $length =~ /$UINT/ and $length > 0
!ll&&!rl&&r
5004
defined $length and $length =~ /$UINT/
!ll&&!rl&&r
5004
MC/DC20%1/5
defined $length and $length =~ /$UINT/ and $length > 0 and $length <= $App::FileSync::Protocol::MAX_WIRE and not $self->{"conn"}->transport_failed
defined $length $length =~ /$UINT/ $length > 0 $length <= $App::FileSync::Protocol::MAX_WIRE not $self->{"conn"}->transport_failed
Truth table33%2/6
A and B and C and D and not(E)
A= defined $lengthB= $length =~ /$UINT/C= $length > 0D= $length <= $App::FileSync::Protocol::MAX_WIREE= not $self->{"conn"}->transport_failed
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
634  
635 4 eval { $self->{conn}->read_bytes($length + 0) };
636 4 return 1;
637 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
638  
639 # _manifest
640 # Purpose: accept (part of) the client's manifest.
641 # Entry: $message->{entries} is an arrayref of { p, t, s, m, h, mode }.
642 # Exit: returns 1 after replying ok.
643 # Side effects: adds normalised entries to client_manifest; raises
644 # server.manifest_* for anything malformed, so the plan never
645 # sees an unchecked path or field.
646 258 ▶sub _manifest :Private {
Subroutine _manifest: called CC=12 SCAR=26.2
647 258 my ($self, $message) = @_;
648  
649 258 my $entries = $message->{entries};
650 258 ▶ $self->_throw('server.manifest_list') unless ref($entries) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entries eq "ARRAY" 0 258 258
651  
652 258 for my $entry (@{$entries}) {
653 929 ▶ $self->_throw('server.manifest_entry') unless ref($entry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 929 929
654  
655 929 my $path = $entry->{p};
656 929 my ($safe, $reason) = validate_relpath($path);
657 929 ▶ $self->_throw('server.manifest_path', { reason => $reason }) unless $safe;
Branch: 1 branch
BranchTrueFalseTotal
unless $safe 0 929 929
658  
659 929 ▶ my $type = $entry->{t} // $TYPE_FILE;
Condition33%1/3
$entry->{"t"} // $App::FileSync::Server::TYPE_FILE
l!l&&r!l&&!r
92900
MC/DC0%0/2
$entry->{"t"} // $App::FileSync::Server::TYPE_FILE
$entry->{"t"} $App::FileSync::Server::TYPE_FILE
Truth table33%1/3
A // B
A= $entry->{"t"}B= $App::FileSync::Server::TYPE_FILE
ABresult
1X1
011
000
660 929 ▶ $self->_throw('server.manifest_field', { field => 't', path => $path })
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq $App::FileSync::Server::TYPE_FILE or $type eq $App::FileSync::Server::TYPE_DIR 0 929 929
Condition66%2/3
$type eq $App::FileSync::Server::TYPE_FILE or $type eq $App::FileSync::Server::TYPE_DIR
l!l&&r!l&&!r
902270
MC/DC0%0/2
$type eq $App::FileSync::Server::TYPE_FILE or $type eq $App::FileSync::Server::TYPE_DIR
$type eq $App::FileSync::Server::TYPE_FILE $type eq $App::FileSync::Server::TYPE_DIR
Truth table66%2/3
A or B
A= $type eq $App::FileSync::Server::TYPE_FILEB= $type eq $App::FileSync::Server::TYPE_DIR
ABresult
1X1
011
000
661 unless $type eq $TYPE_FILE || $type eq $TYPE_DIR;
662  
663 929 ▶ if ($type eq $TYPE_DIR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq $App::FileSync::Server::TYPE_DIR) 27 902 929
664 27 $self->{client_manifest}{$path} = { t => $TYPE_DIR };
665 27 next;
666 }
667  
668 902 my %field = (h => $SHA256_HEX, s => $UINT, m => $MTIME, mode => $UINT);
669 902 my %entry = (m => 0, mode => $DEFAULT_FILE_MODE, %{$entry});
670 902 for my $name (sort keys %field) {
671 $self->_throw('server.manifest_field', { field => $name, path => $path })
672 3608 ▶ unless defined $entry{$name} && $entry{$name} =~ $field{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry{$name} and $entry{$name} =~ /$field{$name}/ 0 3608 3608
Condition33%1/3
defined $entry{$name} and $entry{$name} =~ /$field{$name}/
!ll&&!rl&&r
003608
MC/DC0%0/2
defined $entry{$name} and $entry{$name} =~ /$field{$name}/
defined $entry{$name} $entry{$name} =~ /$field{$name}/
Truth table33%1/3
A and B
A= defined $entry{$name}B= $entry{$name} =~ /$field{$name}/
ABresult
0X0
100
111
673 }
674  
675 $self->{client_manifest}{$path} = {
676 t => $TYPE_FILE,
677 h => $entry{h},
678 902 (map { $_ => $entry{$_} + 0 } qw(s m mode)),
679 };
680 }
681  
682 258 $self->{conn}->send({ ok => 1 });
683 258 return 1;
684 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
685  
686 # _plan
687 # Purpose: plan the session and send it to the client.
688 # Entry: the manifest has arrived.
689 # Exit: returns 1 after sending the actions.
690 # Side effects: rescans the master (reusing the index's hashes); stores the
691 # plan; journals it unless dry run; raises the delete guard's
692 # refusal when the plan deletes too much.
693 258 ▶sub _plan :Private {
Subroutine _plan: called CC=3 SCAR=11.0
694 258 my ($self) = @_;
695  
696 258 my $store = $self->{store};
697 258 my $master = $self->{index} = $store->walk_master($self->{index})->{manifest};
698 258 my $config = $store->config();
699  
700 my $plan = App::FileSync::Merge::plan(
701 base => $self->{base},
702 client => $self->{client_manifest},
703 master => $master,
704 client_id => $self->{client},
705 role => $self->{role},
706 258 now => time,
707 );
708  
709 my $refusal = App::FileSync::Merge::check_delete_limit(
710 deletions => $plan->{deletions},
711 258 base_size => scalar(keys %{ $self->{base} }),
712 max_deletes => $config->{max_deletes},
713 max_delete_percent => $config->{max_delete_percent},
714 force => $self->{force},
715 258 );
716 258 ▶ $self->_throw('server.refused', { reason => $refusal }) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 9 249 258
717  
718 249 $self->_unique_loser_names($plan->{actions});
719  
720 249 $self->{plan} = { map { $_->{p} => $_ } @{ $plan->{actions} } };
721 249 $self->{base_after} = { %{ $self->{client_manifest} } };
722  
723 my %counts = (
724 249 actions => scalar @{ $plan->{actions} },
725 deletions => $plan->{deletions},
726 conflicts => $plan->{conflicts},
727 dry_run => $self->{dry_run},
728 249 );
729 249 $self->{logger}->debug('plan', { repo => $self->{repo}, client => $self->{client}, %counts });
730 249 ▶ $self->_journal({ action => 'plan', %counts }) unless $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dry_run"} 241 8 249
731  
732 249 $self->{conn}->send({ ok => 1, actions => $plan->{actions}, warnings => $plan->{warnings} });
733 249 return 1;
734 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
735  
736 # _unique_loser_names
737 # Purpose: make every conflict-copy name unique against the master and
738 # the rest of the plan.
739 # Entry: $actions from Merge::plan.
740 # Exit: returns 1.
741 # Side effects: rewrites loser_name in place. Two conflicts resolved in the
742 # same second would otherwise pick the same name.
743 249 ▶sub _unique_loser_names :Private {
Subroutine _unique_loser_names: called CC=5 SCAR=16.6
744 249 my ($self, $actions) = @_;
745  
746 249 my %used;
747 249 ▶ for my $action (grep { $_->{a} eq 'conflict' && defined $_->{loser_name} } @{$actions}) {
Condition66%2/3
$_->{'a'} eq 'conflict' && defined $_->{'loser_name'}
!ll&&!rl&&r
598080
MC/DC50%1/2
$_->{'a'} eq 'conflict' && defined $_->{'loser_name'}
$_->{'a'} eq 'conflict' defined $_->{'loser_name'}
Truth table66%2/3
A && B
A= $_->{'a'} eq 'conflict'B= defined $_->{'loser_name'}
ABresult
0X0
100
111
748 80 my ($name, $suffix) = ($action->{loser_name}, 1);
749 $name = name_with_suffix($action->{loser_name}, ++$suffix)
750 80 ▶ while $used{$name} || exists $self->{index}{$name};
Branch: 1 branch
BranchTrueFalseTotal
$used{$name} || exists $self->{"index"}{$name} and do { $name = name_with_suffix($action->{"loser_name"}, ++$suffix) } 0 80 80
Condition33%1/3
$used{$name} or exists $self->{"index"}{$name}
l!l&&r!l&&!r
0080
MC/DC0%0/2
$used{$name} or exists $self->{"index"}{$name}
$used{$name} exists $self->{"index"}{$name}
Truth table33%1/3
A or B
A= $used{$name}B= exists $self->{"index"}{$name}
ABresult
1X1
011
000
751 80 $used{$name} = 1;
752 80 $action->{loser_name} = $name;
753 }
754  
755 249 return 1;
756 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
757  
758 # _expect
759 # Purpose: the one gate every path-bearing request passes.
760 # Entry: $kind is a key of %MAY; $path is from the client.
761 # Exit: returns the planned action for $path.
762 # Side effects: raises server.unplanned when the plan has no such path and
763 # server.not_planned when it does not allow this request.
764 599 ▶sub _expect :Private {
Subroutine _expect: called CC=7 SCAR=20.4
765 599 my ($self, $kind, $path) = @_;
766  
767 599 my $what = $self->i18n("server.op.$kind");
768 599 ▶ my $action = defined $path ? $self->{plan}{$path} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 599 0 599
769 599 ▶ $self->_throw('server.unplanned', { what => $what, path => $path // '' }) unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 599 599
Condition0%0/2
$path // ""
l!l
00
MC/DC0%0/1
$path // ""
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
770  
771 599 ▶ my $label = $action->{a} eq 'conflict' ? "conflict:" . ($action->{winner} // '') : $action->{a};
Branch: 1 branch
BranchTrueFalseTotal
$action->{'a'} eq 'conflict' ? : 32 567 599
Condition50%1/2
$action->{"winner"} // ""
l!l
320
MC/DC0%0/1
$action->{"winner"} // ""
$action->{"winner"}
Truth table50%1/2
A
A= $action->{"winner"}
Aresult
11
00
772 $self->_throw('server.not_planned', { what => $what, path => $path })
773 599 ▶ unless any { $_ eq $label } @{ $MAY{$kind} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $label; } , @{$MAY{$kind};}) 33 566 599
774  
775 566 return $action;
776 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
777  
778 # _get
779 # Purpose: send one chunk of a master file the client is pulling.
780 # Entry: $message has p, optional off and max.
781 # Exit: returns 1 after sending the chunk.
782 # Side effects: reads the master; raises server.bad_field for a malformed
783 # offset or size, server.not_file and io.* errors. max is
784 # clamped to MAX_CHUNK_BYTES so a client cannot ask the server to
785 # buffer an entire large file.
786 103 ▶sub _get :Private {
Subroutine _get: called CC=6 SCAR=17.9
787 103 my ($self, $message) = @_;
788  
789 103 my $path = $message->{p};
790 103 $self->_expect('read', $path);
791  
792 99 my $offset = $self->_uint($message, 'off', 0);
793 97 my $max = $self->_uint($message, 'max', $CHUNK);
794 96 ▶ $max = $MAX_CHUNK_BYTES if $max > $MAX_CHUNK_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if $max > $App::FileSync::Server::MAX_CHUNK_BYTES 1 95 96
795  
796 96 my $abs = $self->{store}->abs_path($path);
797 96 my $file = $self->_file_stat($abs, $path);
798 96 ▶ $self->_throw('server.bad_field', { field => 'off', path => $path }) if $offset > $file->{s};
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $file->{"s"} 1 95 96
799  
800 95 my $length = $file->{s} - $offset;
801 95 ▶ $length = $max if $length > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $max 2 93 95
802  
803 95 my $bytes = '';
804 95 ▶ if ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 95 0 95
805 95 my $fh = App::FileSync::IO::open_file($abs, '<:raw');
806 95 seek($fh, $offset, 0);
807 95 $bytes = App::FileSync::Delta::read_exact($fh, $length, $abs);
808 95 close $fh;
809 }
810  
811 95 ▶ $self->{conn}->send_data({ ok => 1, eof => ($offset + $length >= $file->{s}) ? 1 : 0, %{$file} }, $bytes);
Branch: 1 branch
BranchTrueFalseTotal
$offset + $length >= $file->{'s'} ? : 93 2 95
812 95 return 1;
813 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
814  
815 # _put
816 # Purpose: start receiving a whole file the client is pushing.
817 # Entry: $message has p, s, h, m, mode; conflict_name is optional and,
818 # when sent, must equal the plan's loser_name.
819 # Exit: returns 1 after replying ok with off 0.
820 # Side effects: for a conflict the client won, moves the master's version
821 # aside under the plan's name first - whatever the client
822 # sends, so a client that omits the name cannot overwrite the
823 # master's losing version. Opens a temporary file.
824 276 ▶sub _put :Private {
Subroutine _put: called CC=5 SCAR=16.1
825 276 my ($self, $message) = @_;
826  
827 276 my $path = $message->{p};
828 276 my $action = $self->_expect('write', $path);
829  
830 271 ▶ if ($action->{a} eq 'conflict' && defined $action->{loser_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"a"} eq "conflict" and defined $action->{"loser_name"}) 7 264 271
Condition66%2/3
$action->{"a"} eq "conflict" and defined $action->{"loser_name"}
!ll&&!rl&&r
26407
MC/DC50%1/2
$action->{"a"} eq "conflict" and defined $action->{"loser_name"}
$action->{"a"} eq "conflict" defined $action->{"loser_name"}
Truth table66%2/3
A and B
A= $action->{"a"} eq "conflict"B= defined $action->{"loser_name"}
ABresult
0X0
100
111
831 $self->_throw('server.conflict_name', { path => $path, expected => $action->{loser_name} })
832 7 ▶ if defined $message->{conflict_name} && $message->{conflict_name} ne $action->{loser_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $message->{"conflict_name"} and $message->{"conflict_name"} ne $action->{"loser_name"} 1 6 7
Condition100%3/3
defined $message->{"conflict_name"} and $message->{"conflict_name"} ne $action->{"loser_name"}
!ll&&!rl&&r
241
MC/DC100%2/2
defined $message->{"conflict_name"} and $message->{"conflict_name"} ne $action->{"loser_name"}
defined $message->{"conflict_name"} $message->{"conflict_name"} ne $action->{"loser_name"}
Truth table100%3/3
A and B
A= defined $message->{"conflict_name"}B= $message->{"conflict_name"} ne $action->{"loser_name"}
ABresult
0X0
100
111
833 6 $self->_preserve_loser($path, $action->{loser_name}, { sha256 => $message->{h}, mtime => $message->{m} });
834 }
835  
836 270 $self->_open_upload($path, $message);
837 269 $self->{conn}->send({ ok => 1, off => 0 });
838 269 return 1;
839 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
840  
841 # _chunk
842 # Purpose: receive one chunk of a whole-file upload.
843 # Entry: a put for $message->{p} is open; len gives the payload size.
844 # Exit: returns 1 after replying with the new offset.
845 # Side effects: appends to the temporary file and the running hash.
846 272 ▶sub _chunk :Private {
Subroutine _chunk: called CC=5 SCAR=16.8
847 272 my ($self, $message) = @_;
848  
849 272 ▶ my $path = $message->{p} // '';
Condition50%1/2
$message->{"p"} // ""
l!l
2720
MC/DC0%0/1
$message->{"p"} // ""
$message->{"p"}
Truth table50%1/2
A
A= $message->{"p"}
Aresult
11
00
850 272 ▶ my $upload = $self->{uploads}{$path} or $self->_throw('server.no_put', { path => $path });
Branch: 1 branch
BranchTrueFalseTotal
my $upload = $self->{"uploads"}{$path} or $self->_throw("server.no_put", {"path", $path}) 272 0 272
851  
852 272 my $length = $self->_uint($message, 'len', 0);
853 272 ▶ $self->_throw('server.too_large', { path => $path }) if $length > $MAX_CHUNK_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $App::FileSync::Server::MAX_CHUNK_BYTES 0 272 272
854  
855 272 ▶ $self->_append_upload($path, $upload, $length ? $self->{conn}->read_data($message) : '');
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 272 0 272
856 271 $self->{conn}->send({ ok => 1, off => $upload->{got} });
857 271 return 1;
858 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
859  
860 # _chunk_end
861 # Purpose: finish a whole-file upload.
862 # Entry: a put for $message->{p} is open.
863 # Exit: returns 1 after replying ok.
864 # Side effects: verifies and installs the file (see _commit_upload).
865 265 ▶sub _chunk_end :Private {
Subroutine _chunk_end: called CC=3 SCAR=11.2
866 265 my ($self, $message) = @_;
867  
868 265 ▶ my $path = $message->{p} // '';
Condition50%1/2
$message->{"p"} // ""
l!l
2650
MC/DC0%0/1
$message->{"p"} // ""
$message->{"p"}
Truth table50%1/2
A
A= $message->{"p"}
Aresult
11
00
869 265 ▶ my $upload = delete $self->{uploads}{$path} or $self->_throw('server.no_put', { path => $path });
Branch: 1 branch
BranchTrueFalseTotal
my $upload = delete $self->{"uploads"}{$path} or $self->_throw("server.no_put", {"path", $path}) 265 0 265
870  
871 265 $self->_commit_upload($path, $upload);
872 265 $self->{conn}->send({ ok => 1 });
873 265 return 1;
874 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
875  
876 # _signature
877 # Purpose: describe the master's current version so the client can push
878 # only the blocks that differ.
879 # Entry: $message->{p} is a planned push.
880 # Exit: returns 1 after replying; delta 0 when the file is too small
881 # or absent.
882 # Side effects: reads the master file.
883 11 ▶sub _signature :Private {
Subroutine _signature: called CC=4 SCAR=13.9
884 11 my ($self, $message) = @_;
885  
886 11 my $path = $message->{p};
887 11 $self->_expect('delta', $path);
888  
889 6 my $abs = $self->{store}->abs_path($path);
890 6 ▶ my $signature = (-f $abs && (-s _) >= $App::FileSync::Delta::MIN_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
-f $abs && -s _ >= $App::FileSync::Delta::MIN_SIZE ? : 2 4 6
Condition66%2/3
-f $abs and -s _ >= $App::FileSync::Delta::MIN_SIZE
!ll&&!rl&&r
402
MC/DC50%1/2
-f $abs and -s _ >= $App::FileSync::Delta::MIN_SIZE
-f $abs -s _ >= $App::FileSync::Delta::MIN_SIZE
Truth table66%2/3
A and B
A= -f $absB= -s _ >= $App::FileSync::Delta::MIN_SIZE
ABresult
0X0
100
111
891 ? App::FileSync::Delta::signature($abs)
892 : undef;
893  
894 6 ▶ return $self->_reply({ ok => 1, delta => 0 }) unless $signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $signature 4 2 6
895  
896 $self->{conn}->send_data({
897 ok => 1,
898 delta => 1,
899 s => $signature->{size},
900 b => $signature->{blocklen},
901 2 n => scalar @{ $signature->{blocks} },
902 }, App::FileSync::Delta::encode_signature($signature));
903 2 return 1;
904 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
905  
906 # _delta_start
907 # Purpose: start receiving a pushed file as a delta against the master.
908 # Entry: $message has p, h, s, m, mode.
909 # Exit: returns 1 after replying ok.
910 # Side effects: opens a temporary file and, when the master has the file,
911 # a read handle on it for copy records.
912 2 ▶sub _delta_start :Private {
Subroutine _delta_start: called CC=4 SCAR=14.2
913 2 my ($self, $message) = @_;
914  
915 2 my $path = $message->{p};
916 2 $self->_expect('delta', $path);
917 $self->_throw('server.bad_field', { field => 'h', path => $path })
918 2 ▶ unless defined $message->{h} && $message->{h} =~ $SHA256_HEX;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/ 0 2 2
Condition33%1/3
defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
defined $message->{"h"} $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
Truth table33%1/3
A and B
A= defined $message->{"h"}B= $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
ABresult
0X0
100
111
919 2 $self->_uint($message, 's');
920  
921 2 my $upload = $self->_open_upload($path, $message);
922 2 $upload->{delta} = 1;
923  
924 2 my $old = $self->{store}->abs_path($path);
925 2 ▶ if (-f $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $old) 2 0 2
926 2 @{$upload}{qw(old old_fh old_size)} = ($old, App::FileSync::IO::open_file($old, '<:raw'), -s _);
927 }
928  
929 2 $self->{conn}->send({ ok => 1 });
930 2 return 1;
931 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
932  
933 # _delta_copy
934 # Purpose: apply a copy record: bytes from the master's old version.
935 # Entry: a delta for p is open; off and len are integers, len > 0.
936 # Exit: returns 1 after replying with the new offset.
937 # Side effects: appends to the upload; raises server.bad_copy for a range
938 # outside the old file.
939 27 ▶sub _delta_copy :Private {
Subroutine _delta_copy: called CC=4 SCAR=14.8
940 27 my ($self, $message) = @_;
941  
942 27 my ($path, $upload) = $self->_open_delta($message);
943 27 my $offset = $self->_uint($message, 'off');
944 27 my $length = $self->_uint($message, 'len');
945  
946 $self->_throw('server.bad_copy', { path => $path })
947 27 ▶ unless $length > 0 && $upload->{old_fh} && $offset + $length <= $upload->{old_size};
Branch: 1 branch
BranchTrueFalseTotal
unless $length > 0 and $upload->{"old_fh"} and $offset + $length <= $upload->{"old_size"} 0 27 27
Condition33%2/6
$length > 0 and $upload->{"old_fh"} and $offset + $length <= $upload->{"old_size"}
!ll&&!rl&&r
0027
$length > 0 and $upload->{"old_fh"}
!ll&&!rl&&r
0027
MC/DC0%0/3
$length > 0 and $upload->{"old_fh"} and $offset + $length <= $upload->{"old_size"}
$length > 0 $upload->{"old_fh"} $offset + $length <= $upload->{"old_size"}
Truth table25%1/4
A and B and C
A= $length > 0B= $upload->{"old_fh"}C= $offset + $length <= $upload->{"old_size"}
ABCresult
0XX0
10X0
1100
1111
948  
949 27 seek($upload->{old_fh}, $offset, 0);
950 27 $self->_append_upload($path, $upload, App::FileSync::Delta::read_exact($upload->{old_fh}, $length, $upload->{old}));
951 27 $self->{conn}->send({ ok => 1, off => $upload->{got} });
952 27 return 1;
953 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
954  
955 # _delta_literal
956 # Purpose: apply a literal record: bytes sent by the client.
957 # Entry: a delta for p is open.
958 # Exit: returns 1 after replying with the new offset.
959 # Side effects: appends to the upload.
960 3 ▶sub _delta_literal :Private {
Subroutine _delta_literal: called CC=1 SCAR=0.0
961 3 my ($self, $message) = @_;
962  
963 3 my ($path, $upload) = $self->_open_delta($message);
964 3 $self->_append_upload($path, $upload, $self->{conn}->read_data($message));
965 3 $self->{conn}->send({ ok => 1, off => $upload->{got} });
966 3 return 1;
967 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
968  
969 # _delta_end
970 # Purpose: finish a delta upload.
971 # Entry: a delta for p is open.
972 # Exit: returns 1 after replying ok.
973 # Side effects: verifies and installs the file; journals it as a delta push.
974 2 ▶sub _delta_end :Private {
Subroutine _delta_end: called CC=1 SCAR=0.0
975 2 my ($self, $message) = @_;
976  
977 2 my ($path, $upload) = $self->_open_delta($message);
978 2 delete $self->{uploads}{$path};
979  
980 2 $self->_commit_upload($path, $upload, delta => 1);
981 2 $self->{conn}->send({ ok => 1, delta => 1 });
982 2 return 1;
983 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
984  
985 # _pull_signature
986 # Purpose: take the client's description of its old version and spool a
987 # delta from the master's current one.
988 # Entry: $message->{p} is a planned pull; n and b describe the blocks.
989 # Exit: returns 1 after replying; delta 0 when a delta is not worth it.
990 # Side effects: reads the master file; keeps the spool for _pull_op.
991 15 ▶sub _pull_signature :Private {
Subroutine _pull_signature: called CC=6 SCAR=17.9
992 15 my ($self, $message) = @_;
993  
994 15 my $path = $message->{p};
995 15 $self->_expect('pull', $path);
996  
997 10 my $count = $self->_uint($message, 'n');
998 10 my $block = $self->_uint($message, 'b');
999 10 ▶ $self->_throw('server.bad_signature', { path => $path })
Branch: 1 branch
BranchTrueFalseTotal
unless $count <= $MAX_SIGNATURE_BLOCKS and $block >= $MIN_SIGNATURE_BLOCK and $block <= $MAX_SIGNATURE_BLOCK 4 6 10
Condition100%6/6
$count <= $MAX_SIGNATURE_BLOCKS and $block >= $MIN_SIGNATURE_BLOCK and $block <= $MAX_SIGNATURE_BLOCK
!ll&&!rl&&r
316
$count <= $MAX_SIGNATURE_BLOCKS and $block >= $MIN_SIGNATURE_BLOCK
!ll&&!rl&&r
127
MC/DC100%3/3
$count <= $MAX_SIGNATURE_BLOCKS and $block >= $MIN_SIGNATURE_BLOCK and $block <= $MAX_SIGNATURE_BLOCK
$count <= $MAX_SIGNATURE_BLOCKS $block >= $MIN_SIGNATURE_BLOCK $block <= $MAX_SIGNATURE_BLOCK
Truth table100%4/4
A and B and C
A= $count <= $MAX_SIGNATURE_BLOCKSB= $block >= $MIN_SIGNATURE_BLOCKC= $block <= $MAX_SIGNATURE_BLOCK
ABCresult
0XX0
10X0
1100
1111
1000 unless $count <= $MAX_SIGNATURE_BLOCKS && $block >= $MIN_SIGNATURE_BLOCK && $block <= $MAX_SIGNATURE_BLOCK;
1001  
1002 6 my $blocks = App::FileSync::Delta::decode_signature($self->{conn}->read_data($message), $count);
1003  
1004 6 my $abs = $self->{store}->abs_path($path);
1005 6 my $file = $self->_file_stat($abs, $path);
1006 6 ▶ return $self->_reply({ ok => 1, delta => 0 }) if $file->{s} < $App::FileSync::Delta::MIN_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $file->{"s"} < $App::FileSync::Delta::MIN_SIZE 3 3 6
1007  
1008 3 my $spool = File::Temp->new(UNLINK => 1);
1009 3 binmode $spool;
1010 3 my $stats = App::FileSync::Delta::diff($abs, { blocklen => $block, blocks => $blocks }, $spool);
1011 3 $spool->flush();
1012  
1013 # Only the literal bytes and record headers go on the wire; compression can
1014 # shrink them further, so this is a conservative estimate.
1015 3 my $estimated = $stats->{literal} + $DELTA_OP_BYTES * $stats->{ops} + $DELTA_OVERHEAD;
1016 3 ▶ return $self->_reply({ ok => 1, delta => 0 }) if $estimated >= $file->{s};
Branch: 1 branch
BranchTrueFalseTotal
if $estimated >= $file->{"s"} 1 2 3
1017  
1018 2 seek($spool, 0, 0);
1019 2 $self->{pull_deltas}{$path} = { spool => $spool, %{$file} };
1020 2 $self->{conn}->send({ ok => 1, delta => 1, %{$file}, b => $block });
1021 2 return 1;
1022 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1023  
1024 # _pull_op
1025 # Purpose: serve the next record of a spooled pull delta.
1026 # Entry: _pull_signature spooled a delta for p.
1027 # Exit: returns 1 after sending a copy, a literal, or done.
1028 # Side effects: advances the spool; forgets it at the end.
1029 26 ▶sub _pull_op :Private {
Subroutine _pull_op: called CC=5 SCAR=16.2
1030 26 my ($self, $message) = @_;
1031  
1032 26 ▶ my $path = $message->{p} // '';
Condition50%1/2
$message->{"p"} // ""
l!l
260
MC/DC0%0/1
$message->{"p"} // ""
$message->{"p"}
Truth table50%1/2
A
A= $message->{"p"}
Aresult
11
00
1033 26 ▶ my $state = $self->{pull_deltas}{$path} or $self->_throw('server.no_signature', { path => $path });
Branch: 1 branch
BranchTrueFalseTotal
my $state = $self->{"pull_deltas"}{$path} or $self->_throw("server.no_signature", {"path", $path}) 26 0 26
1034  
1035 26 my ($type, @rest) = App::FileSync::Delta::read_op($state->{spool});
1036 26 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 25 26
1037 1 delete $self->{pull_deltas}{$path};
1038 1 return $self->_reply({ ok => 1, done => 1 });
1039 }
1040  
1041 25 ▶ return $self->_reply({ ok => 1, op => 'copy', off => $rest[0], len => $rest[1] }) if $type eq 'copy';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "copy" 23 2 25
1042  
1043 2 $self->{conn}->send_data({ ok => 1, op => 'lit' }, $rest[0]);
1044 2 return 1;
1045 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1046  
1047 # _mkdir
1048 # Purpose: create a directory the client pushed.
1049 # Entry: $message->{p} is a planned push of a directory.
1050 # Exit: returns 1 after replying ok.
1051 # Side effects: creates the directory in the master; records it.
1052 12 ▶sub _mkdir :Private {
Subroutine _mkdir: called CC=1 SCAR=0.0
1053 12 my ($self, $message) = @_;
1054  
1055 12 my $path = $message->{p};
1056 12 $self->_expect('mkdir', $path);
1057  
1058 7 App::FileSync::IO::ensure_dir($self->{store}->abs_path($path));
1059 7 $self->{index}{$path} = { t => $TYPE_DIR };
1060 7 $self->{base_after}{$path} = { t => $TYPE_DIR };
1061 7 $self->_journal({ action => 'mkdir', path => $path, result => 'ok' });
1062  
1063 7 $self->{applied}{$path} = 1;
1064 7 return $self->_reply({ ok => 1 });
1065 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1066  
1067 # _delete
1068 # Purpose: move a master path the client deleted into the trash.
1069 # Entry: $message->{p} is a planned delete_remote.
1070 # Exit: returns 1 after replying with the trash path.
1071 # Side effects: moves the path to the trash; records it.
1072 41 ▶sub _delete :Private {
Subroutine _delete: called CC=1 SCAR=0.0
1073 41 my ($self, $message) = @_;
1074  
1075 41 my $path = $message->{p};
1076 41 $self->_expect('delete', $path);
1077  
1078 36 my $trashed = $self->{store}->move_to_trash($path, time);
1079 36 delete $self->{index}{$path};
1080 36 delete $self->{base_after}{$path};
1081 36 $self->_journal({ action => 'delete', path => $path, trash => $trashed, result => 'ok' });
1082  
1083 36 $self->{applied}{$path} = 1;
1084 36 return $self->_reply({ ok => 1, trash => $trashed });
1085 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1086  
1087 # _ack
1088 # Purpose: record what the client now holds after a change on its side.
1089 # Entry: $message->{p} is a planned pull, delete_local or conflict the
1090 # master won. deleted is only valid for delete_local, and only
1091 # a delete_local may omit the file description.
1092 # Exit: returns 1 after replying ok.
1093 # Side effects: updates base_after; journals a conflict the master won.
1094 # Restricting acks to these actions stops a client from writing
1095 # arbitrary entries into its own base, which would make the
1096 # next plan delete or skip master files.
1097 139 ▶sub _ack :Private {
Subroutine _ack: called CC=9 SCAR=22.1
1098 139 my ($self, $message) = @_;
1099  
1100 139 my $path = $message->{p};
1101 139 my $action = $self->_expect('ack', $path);
1102 135 my $deleting = $action->{a} eq 'delete_local';
1103  
1104 135 ▶ $self->_throw('server.bad_ack', { path => $path }) unless !$message->{deleted} == !$deleting;
Branch: 1 branch
BranchTrueFalseTotal
unless !$message->{"deleted"} == !$deleting 1 134 135
1105  
1106 134 ▶ if ($deleting) {
Branch: 2 branches
BranchTrueFalseTotal
if ($deleting) { } 35 99 134
elsif (($message->{'t'} // '') eq $App::FileSync::Server::TYPE_DIR) { } 6 93 99
1107 35 delete $self->{base_after}{$path};
1108 ▶ } elsif (($message->{t} // '') eq $TYPE_DIR) {
Condition100%2/2
$message->{"t"} // ""
l!l
693
MC/DC100%1/1
$message->{"t"} // ""
$message->{"t"}
Truth table100%2/2
A
A= $message->{"t"}
Aresult
11
00
1109 6 $self->{base_after}{$path} = { t => $TYPE_DIR };
1110 } else {
1111 $self->_throw('server.bad_field', { field => 'h', path => $path })
1112 93 ▶ unless defined $message->{h} && $message->{h} =~ $SHA256_HEX;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/ 0 93 93
Condition33%1/3
defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
!ll&&!rl&&r
0093
MC/DC0%0/2
defined $message->{"h"} and $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
defined $message->{"h"} $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
Truth table33%1/3
A and B
A= defined $message->{"h"}B= $message->{"h"} =~ /$App::FileSync::Server::SHA256_HEX/
ABresult
0X0
100
111
1113 $self->{base_after}{$path} = {
1114 t => $TYPE_FILE,
1115 h => $message->{h},
1116 s => $self->_uint($message, 's', 0),
1117 93 ▶ m => ($message->{m} // 0) + 0,
Condition50%1/2
$message->{"m"} // 0
l!l
930
MC/DC0%0/1
$message->{"m"} // 0
$message->{"m"}
Truth table50%1/2
A
A= $message->{"m"}
Aresult
11
00
1118 mode => $self->_uint($message, 'mode', $DEFAULT_FILE_MODE),
1119 };
1120 }
1121  
1122 134 ▶ if ($action->{a} eq 'conflict') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"a"} eq "conflict") 7 127 134
1123 $self->_journal_conflict($path, 'remote', $action->{loser_name},
1124 7 $self->{index}{$path}, $self->{client_manifest}{$path});
1125 }
1126  
1127 134 $self->{applied}{$path} = 1;
1128 134 delete $self->{pull_deltas}{$path};
1129 134 return $self->_reply({ ok => 1 });
1130 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1131  
1132 # _preserve_loser
1133 # Purpose: move the master's losing version aside before the client's
1134 # version is written.
1135 # Entry: $conflict_rel is the plan's loser_name for $path.
1136 # Exit: returns 1.
1137 # Side effects: renames the master file; moves its index entry; journals the
1138 # copy and the conflict. Does nothing if the master no longer
1139 # has the file.
1140 6 ▶sub _preserve_loser :Private {
Subroutine _preserve_loser: called CC=10 SCAR=25.2
1141 6 my ($self, $path, $conflict_rel, $winner) = @_;
1142  
1143 6 my ($safe, $reason) = validate_relpath($conflict_rel);
1144 6 ▶ $self->_throw('server.conflict_unsafe', { reason => $reason }) unless $safe;
Branch: 1 branch
BranchTrueFalseTotal
unless $safe 0 6 6
1145 6 ▶ $self->_throw('server.conflict_same', { path => $path }) if $conflict_rel eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $conflict_rel eq $path 0 6 6
1146  
1147 6 my $store = $self->{store};
1148 6 my $abs = $store->abs_path($path);
1149 6 ▶ return 1 unless -e $abs || -l $abs;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $abs or -l $abs 0 6 6
Condition33%1/3
-e $abs or -l $abs
l!l&&r!l&&!r
600
MC/DC0%0/2
-e $abs or -l $abs
-e $abs -l $abs
Truth table33%1/3
A or B
A= -e $absB= -l $abs
ABresult
1X1
011
000
1150  
1151 6 my $destination = $store->abs_path($conflict_rel);
1152 6 ▶ $self->_throw('server.conflict_exists', { path => $conflict_rel }) if -e $destination || -l $destination;
Branch: 1 branch
BranchTrueFalseTotal
if -e $destination or -l $destination 0 6 6
Condition33%1/3
-e $destination or -l $destination
l!l&&r!l&&!r
006
MC/DC0%0/2
-e $destination or -l $destination
-e $destination -l $destination
Truth table33%1/3
A or B
A= -e $destinationB= -l $destination
ABresult
1X1
011
000
1153  
1154 6 $store->ensure_parent($conflict_rel);
1155 6 ▶ eval { rename $abs, $destination; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &App::FileSync::Server::rename($abs, $destination); 1 } } or $self->_throw("server.conflict_move", {"path", $path, "to", $conflict_rel, "error", os_error($@)}) 6 0 6
1156 or $self->_throw('server.conflict_move', { path => $path, to => $conflict_rel, error => os_error($@) });
1157  
1158 6 my $old = delete $self->{index}{$path};
1159 6 ▶ $self->{index}{$conflict_rel} = $old if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 6 0 6
1160  
1161 6 ▶ $self->_journal({ action => 'conflict_copy', path => $conflict_rel, from => $path, sha256 => $old ? $old->{h} : undef, result => 'ok' });
Branch: 1 branch
BranchTrueFalseTotal
$old ? : 6 0 6
1162 6 $self->_journal_conflict($path, 'client', $conflict_rel, { h => $winner->{sha256}, m => $winner->{mtime} }, $old);
1163  
1164 6 return 1;
1165 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1166  
1167 # _journal_conflict
1168 # Purpose: record how a conflict was resolved.
1169 # Entry: $side is client or remote; entries may be undef.
1170 # Exit: returns 1.
1171 # Side effects: appends a conflict event.
1172 13 ▶sub _journal_conflict :Private {
Subroutine _journal_conflict: called CC=3 SCAR=11.7
1173 13 my ($self, $path, $side, $loser_name, $winner, $loser) = @_;
1174  
1175 13 ▶ ($winner, $loser) = ($winner || {}, $loser || {});
Condition50%2/4
$winner || {}
l!l
130
$loser || {}
l!l
130
MC/DC0%0/1
$winner || {}
$winner
MC/DC0%0/1
$loser || {}
$loser
Truth table50%1/2
A
A= $winner
Aresult
11
00
Truth table50%1/2
A
A= $loser
Aresult
11
00
1176 return $self->_journal({
1177 action => 'conflict',
1178 path => $path,
1179 winner => $side,
1180 loser_name => $loser_name,
1181 winner_sha256 => $winner->{h},
1182 winner_mtime => $winner->{m},
1183 loser_sha256 => $loser->{h},
1184 loser_mtime => $loser->{m},
1185 13 result => 'ok',
1186 });
1187 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1188  
1189 # _finish
1190 # Purpose: end the session: save the index and the client's base.
1191 # Entry: every planned action has been applied (unless dry run).
1192 # Exit: returns 1 after sending the summary.
1193 # Side effects: writes the index, the base and a session event; sets
1194 # finished. Raises server.unfinished, naming the missing paths
1195 # in order, when the client stopped early.
1196 195 ▶sub _finish :Private {
Subroutine _finish: called CC=5 SCAR=16.1
1197 195 my ($self) = @_;
1198  
1199 195 my @actions = values %{ $self->{plan} };
1200 195 ▶ unless ($self->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dry_run"}) 187 8 195
1201 187 my @unfinished = sort map { $_->{p} } grep { !$self->{applied}{ $_->{p} } } @actions;
1202 187 ▶ $self->_throw('server.unfinished', { paths => join(', ', @unfinished) }) if @unfinished;
Branch: 1 branch
BranchTrueFalseTotal
if @unfinished 1 186 187
1203  
1204 186 $self->{store}->save_index($self->{index});
1205 186 $self->{store}->save_base($self->{client}, $self->{base_after});
1206 }
1207  
1208 194 my %summary = map { my $kind = $_; $kind => scalar(grep { $_->{a} =~ /\A$kind/ } @actions) }
1209 qw(pull push delete conflict);
1210 %summary = (
1211 pulled => $summary{pull},
1212 pushed => $summary{push},
1213 deleted => $summary{delete},
1214 conflicts => $summary{conflict},
1215 194 );
1216  
1217 194 $self->{logger}->info('session finished', { repo => $self->{repo}, client => $self->{client}, %summary, dry_run => $self->{dry_run} });
1218  
1219 # The client exits 3 when it had to keep conflict copies; record that here
1220 # too, so --exit means the same thing on both sides.
1221 $self->_journal({
1222 action => 'session',
1223 result => 'ok',
1224 exit => $summary{conflicts} ? $EXIT_WARNINGS : $EXIT_OK,
1225 dry_run => $self->{dry_run},
1226 %summary,
1227 194 ▶ }) unless $self->{dry_run};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"dry_run"} 186 8 194
$summary{'conflicts'} ? : 9 177 186
1228 194 $self->{session_journalled} = 1;
1229  
1230 194 $self->{conn}->send({ ok => 1, summary => \%summary });
1231 194 $self->{finished} = 1;
1232 194 return 1;
1233 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1234  
1235 # _open_upload
1236 # Purpose: start an upload for a planned path.
1237 # Entry: $message has s, h, m, mode (s and h may be absent for a put).
1238 # Exit: returns the upload record.
1239 # Side effects: creates the parent directory and a temporary file; raises
1240 # server.busy if the path is already being received.
1241 272 ▶sub _open_upload :Private {
Subroutine _open_upload: called CC=3 SCAR=11.2
1242 272 my ($self, $path, $message) = @_;
1243  
1244 272 ▶ $self->_throw('server.busy', { path => $path }) if $self->{uploads}{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"uploads"}{$path} 0 272 272
1245  
1246 272 ▶ my $size = defined $message->{s} ? $self->_uint($message, 's') : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $message->{'s'} ? : 272 0 272
1247 272 $self->{store}->ensure_parent($path);
1248 272 my ($fh, $temp) = @{ $self->{store}->temp_file($path) };
1249  
1250 return $self->{uploads}{$path} = {
1251 fh => $fh,
1252 temp => $temp,
1253 sha => Digest::SHA->new($SHA_BITS),
1254 got => 0,
1255 size => $size,
1256 hash => $message->{h},
1257 mtime => $message->{m},
1258 mode => $message->{mode},
1259 271 };
1260 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1261  
1262 # _open_delta
1263 # Purpose: find the delta upload a record belongs to.
1264 # Entry: $message->{p} names it.
1265 # Exit: returns ($path, $upload).
1266 # Side effects: raises server.no_delta when there is none, or the open
1267 # upload for that path is a whole-file put.
1268 32 ▶sub _open_delta :Private {
Subroutine _open_delta: called CC=4 SCAR=15.2
1269 32 my ($self, $message) = @_;
1270  
1271 32 ▶ my $path = $message->{p} // '';
Condition50%1/2
$message->{"p"} // ""
l!l
320
MC/DC0%0/1
$message->{"p"} // ""
$message->{"p"}
Truth table50%1/2
A
A= $message->{"p"}
Aresult
11
00
1272 32 my $upload = $self->{uploads}{$path};
1273 32 ▶ $self->_throw('server.no_delta', { path => $path }) unless $upload && $upload->{delta};
Branch: 1 branch
BranchTrueFalseTotal
unless $upload and $upload->{"delta"} 0 32 32
Condition33%1/3
$upload and $upload->{"delta"}
!ll&&!rl&&r
0032
MC/DC0%0/2
$upload and $upload->{"delta"}
$upload $upload->{"delta"}
Truth table33%1/3
A and B
A= $uploadB= $upload->{"delta"}
ABresult
0X0
100
111
1274 32 return ($path, $upload);
1275 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1276  
1277 # _append_upload
1278 # Purpose: add received bytes to an upload.
1279 # Entry: $upload is open.
1280 # Exit: returns 1.
1281 # Side effects: writes to the temporary file and the hash. Refuses more
1282 # bytes than were announced, so a client cannot fill the disk
1283 # behind a small size; on any failure the upload is discarded.
1284 302 ▶sub _append_upload :Private {
Subroutine _append_upload: called CC=4 SCAR=14.5
1285 302 my ($self, $path, $upload, $bytes) = @_;
1286  
1287 302 my $length = length $bytes;
1288 302 ▶ if (defined $upload->{size} && $upload->{got} + $length > $upload->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upload->{"size"} and $upload->{"got"} + $length > $upload->{"size"}) 1 301 302
Condition66%2/3
defined $upload->{"size"} and $upload->{"got"} + $length > $upload->{"size"}
!ll&&!rl&&r
03011
MC/DC50%1/2
defined $upload->{"size"} and $upload->{"got"} + $length > $upload->{"size"}
defined $upload->{"size"} $upload->{"got"} + $length > $upload->{"size"}
Truth table66%2/3
A and B
A= defined $upload->{"size"}B= $upload->{"got"} + $length > $upload->{"size"}
ABresult
0X0
100
111
1289 1 $self->_discard_upload($path);
1290 1 $self->_throw('server.overflow', { path => $path });
1291 }
1292  
1293 301 ▶ unless (print { $upload->{fh} } $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print {$upload->{"fh"};} $bytes) 0 301 301
1294 0 my $error = os_error();
1295 0 $self->_discard_upload($path);
1296 0 $self->_throw('io.write', { path => $upload->{temp}, error => $error });
1297 }
1298  
1299 301 $upload->{sha}->add($bytes);
1300 301 $upload->{got} += $length;
1301 301 return 1;
1302 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1303  
1304 # _commit_upload
1305 # Purpose: close, verify and install an upload, whole or delta.
1306 # Entry: $upload is no longer in uploads.
1307 # Exit: returns the new index entry.
1308 # Side effects: installs the file in the master; updates the index and
1309 # base_after; journals the push. A hash or size mismatch
1310 # discards the temporary file and raises.
1311 267 ▶sub _commit_upload :Private {
Subroutine _commit_upload: called CC=15 SCAR=30.1
1312 267 my ($self, $path, $upload, %extra) = @_;
1313  
1314 267 my $closed = eval { close $upload->{fh}; 1 };
1315 267 ▶ my $error = $closed ? undef : os_error($@);
Branch: 1 branch
BranchTrueFalseTotal
$closed ? : 267 0 267
1316 267 ▶ eval { close $upload->{old_fh} } if $upload->{old_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $upload->{"old_fh"} 2 265 267
1317  
1318 267 my $hash = $upload->{sha}->hexdigest();
1319 my ($key, %args) =
1320 !$closed ? ('io.write', path => $upload->{temp}, error => $error)
1321 : (defined $upload->{hash} && $hash ne $upload->{hash}) ? ('server.hash_mismatch', path => $path, expected => $upload->{hash}, got => $hash)
1322 : (defined $upload->{size} && $upload->{got} != $upload->{size}) ? ('server.size_mismatch', path => $path, expected => $upload->{size}, got => $upload->{got})
1323 267 ▶ : ();
Branch: 3 branches
BranchTrueFalseTotal
!$closed ? : 0 267 267
defined $upload->{'hash'} && $hash ne $upload->{'hash'} ? : 0 267 267
defined $upload->{'size'} && $upload->{'got'} != $upload->{'size'} ? : 0 267 267
Condition33%2/6
defined $upload->{"hash"} and $hash ne $upload->{"hash"}
!ll&&!rl&&r
02670
defined $upload->{"size"} and $upload->{"got"} != $upload->{"size"}
!ll&&!rl&&r
02670
MC/DC0%0/2
defined $upload->{"hash"} and $hash ne $upload->{"hash"}
defined $upload->{"hash"} $hash ne $upload->{"hash"}
MC/DC0%0/2
defined $upload->{"size"} and $upload->{"got"} != $upload->{"size"}
defined $upload->{"size"} $upload->{"got"} != $upload->{"size"}
Truth table33%1/3
A and B
A= defined $upload->{"hash"}B= $hash ne $upload->{"hash"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $upload->{"size"}B= $upload->{"got"} != $upload->{"size"}
ABresult
0X0
100
111
1324 267 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 267 267
1325 0 eval { unlink $upload->{temp} };
1326 0 $self->_throw($key, \%args);
1327 }
1328  
1329 # A directory where the file goes still holds what the sync does not list
1330 # (ignored files such as .DS_Store): its listed contents were deleted
1331 # earlier in this plan. Keep it in the trash rather than refuse the file,
1332 # which would fail this and every later sync.
1333 267 my $abs = $self->{store}->abs_path($path);
1334 267 ▶ if (-d $abs && !-l $abs && @{ list_dir($abs) || [$abs] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $abs and not -l $abs and @{list_dir($abs) or [$abs];}) 1 266 267
Condition75%6/8
-d $abs and not -l $abs and @{list_dir($abs) or [$abs];}
!ll&&!rl&&r
26421
-d $abs and not -l $abs
!ll&&!rl&&r
26403
list_dir($abs) || [$abs]
l!l
30
MC/DC66%2/3
-d $abs and not -l $abs and @{list_dir($abs) or [$abs];}
-d $abs not -l $abs @{list_dir($abs) or [$abs];}
MC/DC0%0/1
list_dir($abs) || [$abs]
list_dir($abs)
Truth table75%3/4
A and not(B) and C
A= -d $absB= not -l $absC= @{list_dir($abs) or [$abs];}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= list_dir($abs)
Aresult
11
00
1335 1 my $trashed = $self->{store}->move_to_trash($path, time);
1336 1 $self->_journal({ action => 'delete', path => $path, trash => $trashed, reason => 'replaced by a file', result => 'ok' });
1337 }
1338  
1339 267 $self->{store}->commit_temp($path, $upload->{temp}, $upload->{mtime}, $upload->{mode});
1340  
1341 my $entry = {
1342 t => $TYPE_FILE,
1343 s => $upload->{got},
1344 m => $upload->{mtime} // time,
1345 h => $hash,
1346 267 ▶ mode => $upload->{mode} // $DEFAULT_FILE_MODE,
Condition40%2/5
$upload->{"mtime"} // time
l!l
2670
$upload->{"mode"} // $App::FileSync::Server::DEFAULT_FILE_MODE
l!l&&r!l&&!r
26700
MC/DC0%0/1
$upload->{"mtime"} // time
$upload->{"mtime"}
MC/DC0%0/2
$upload->{"mode"} // $App::FileSync::Server::DEFAULT_FILE_MODE
$upload->{"mode"} $App::FileSync::Server::DEFAULT_FILE_MODE
Truth table50%1/2
A
A= $upload->{"mtime"}
Aresult
11
00
Truth table33%1/3
A // B
A= $upload->{"mode"}B= $App::FileSync::Server::DEFAULT_FILE_MODE
ABresult
1X1
011
000
1347 };
1348 267 $self->{index}{$path} = $entry;
1349 267 $self->{base_after}{$path} = { %{$entry} };
1350  
1351 267 $self->_journal({ action => 'push', path => $path, size => $upload->{got}, sha256 => $hash, result => 'ok', %extra });
1352 267 $self->{applied}{$path} = 1;
1353 267 return $entry;
1354 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1355  
1356 # _discard_upload / _discard_uploads
1357 # Purpose: abandon one, or every, partial upload.
1358 # Entry: none.
1359 # Exit: returns 1.
1360 # Side effects: closes handles and removes temporary files, so a failed
1361 # session leaves nothing for clean_stale_temps to find an hour
1362 # later.
1363 4 ▶sub _discard_upload :Private {
Subroutine _discard_upload: called CC=3 SCAR=11.0
1364 4 my ($self, $path) = @_;
1365  
1366 # Best effort: the session is ending or the upload was refused, so a
1367 # handle that will not close or a temp that will not go is not an error.
1368 4 ▶ if (my $upload = delete $self->{uploads}{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $upload = delete $self->{"uploads"}{$path}) 4 0 4
1369 4 eval { close $_ } for grep { defined } @{$upload}{qw(fh old_fh)};
1370 4 eval { unlink $upload->{temp} };
1371 }
1372  
1373 4 return 1;
1374 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1375  
1376 71 ▶sub _discard_uploads :Private {
Subroutine _discard_uploads: called CC=2 SCAR=6.9
1377 71 my ($self) = @_;
1378  
1379 71 $self->_discard_upload($_) for keys %{ $self->{uploads} };
1380 71 return 1;
1381 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1382  
1383 # _file_stat
1384 # Purpose: size, mtime and mode of a master file the client is reading.
1385 # Entry: $abs is inside the master.
1386 # Exit: returns { s, m, mode }.
1387 # Side effects: raises server.cannot_stat or server.not_file.
1388 102 ▶sub _file_stat :Private {
Subroutine _file_stat: called CC=3 SCAR=11.3
1389 102 my ($self, $abs, $path) = @_;
1390  
1391 102 my @stat = Time::HiRes::stat($abs);
1392 102 ▶ $self->_throw('server.cannot_stat', { path => $path, error => os_error() }) unless @stat;
Branch: 1 branch
BranchTrueFalseTotal
unless @stat 0 102 102
1393 102 ▶ $self->_throw('server.not_file', { path => $path }) unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 102 102
1394 102 return { s => $stat[$STAT_SIZE], m => $stat[$STAT_MTIME], mode => $stat[$STAT_MODE] & $MODE_MASK };
1395 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1396  
1397 # _uint
1398 # Purpose: read an unsigned integer field from a client message.
1399 # Entry: $default is used when the field is absent; without one the
1400 # field is required.
1401 # Exit: returns the number.
1402 # Side effects: raises server.bad_field for anything else (a negative or
1403 # fractional offset, a string, a missing required field).
1404 1002 ▶sub _uint :Private {
Subroutine _uint: called CC=5 SCAR=16.6
1405 1002 my ($self, $message, $field, $default) = @_;
1406  
1407 1002 ▶ my $value = $message->{$field} // $default;
Condition66%2/3
$message->{$field} // $default
l!l&&r!l&&!r
100110
MC/DC0%0/2
$message->{$field} // $default
$message->{$field} $default
Truth table66%2/3
A // B
A= $message->{$field}B= $default
ABresult
1X1
011
000
1408 1002 ▶ $self->_throw('server.bad_field', { field => $field, path => $message->{p} // '' })
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /$UINT/ 3 999 1002
Condition60%3/5
defined $value and $value =~ /$UINT/
!ll&&!rl&&r
03999
$message->{"p"} // ""
l!l
30
MC/DC50%1/2
defined $value and $value =~ /$UINT/
defined $value $value =~ /$UINT/
MC/DC0%0/1
$message->{"p"} // ""
$message->{"p"}
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /$UINT/
ABresult
0X0
100
111
Truth table50%1/2
A
A= $message->{"p"}
Aresult
11
00
1409 unless defined $value && $value =~ $UINT;
1410 999 return $value + 0;
1411 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1412  
1413 # _journal
1414 # Purpose: append an event for this client.
1415 # Entry: $event is a hashref without client.
1416 # Exit: returns 1.
1417 # Side effects: appends to the journal.
1418 825 ▶sub _journal :Private {
Subroutine _journal: called CC=1 SCAR=0.0
1419 825 my ($self, $event) = @_;
1420  
1421 825 $self->{store}->journal({ client => $self->{client}, %{$event} });
1422 825 return 1;
1423 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1424  
1425 # _reply
1426 # Purpose: send a reply and end the handler.
1427 # Entry: $reply is a hashref.
1428 # Exit: returns 1.
1429 # Side effects: writes to the connection.
1430 209 ▶sub _reply :Private {
Subroutine _reply: called CC=1 SCAR=0.0
1431 209 my ($self, $reply) = @_;
1432  
1433 209 $self->{conn}->send($reply);
1434 209 return 1;
1435 414 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1436  
1437 414 1;
1438  
1439 __END__
« blib/lib/App/FileSync/Admin.pm ↑ Summary blib/lib/App/FileSync/Trash.pm »