blib/lib/App/FileSync/Merge.pm

filter: Statementstmt 100% 135 / 135 Branchbran 91.1% 62 / 68 Conditioncond 73.2% 52 / 71 MC/DCmcdc 56.1% 23 / 41 Subroutinesub 100% 24 / 24 Podpod 100% 2 / 2 totaltotal 87.3% 298 / 341 CC 64 SCAR 24.3
CC
64
Cov
91%
plan
22.1
check_delete_limit
13.0
__ANON__
13.0
CRAP 11.4
« blib/lib/App/FileSync/Process.pm ↑ Summary blib/lib/App/FileSync/Snapshot.pm »
1 package App::FileSync::Merge;
2  
3 # The three-way merge: given what the client had at the last sync (the base),
4 # what it has now, and what the master has now, decide what moves where.
5 # Pure functions of their arguments; nothing here touches the disk.
6  
7 315 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 315 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 315 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 315 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 315 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 315 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 315 ▶use App::FileSync qw(conflict_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 315 ▶use App::FileSync::Args qw(named);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 315 ▶use App::FileSync::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 $ROLE_CLIENT $ROLE_REPLICA @ROLES $TYPE_FILE $TYPE_DIR
20 $MAX_DELETES $MAX_DELETE_PERCENT $MTIME_TOLERANCE
21 315 );
22 315 ▶use App::FileSync::I18N qw(message);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 315 ▶use version; our $VERSION = version->declare('0.002.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # max_delete_percent is a percentage of the repo's size.
27 315 Readonly::Scalar my $PERCENT => 100;
28  
29 # The comparisons below run several times per path in the tree, so they are
30 # lexical code references rather than wrapped private subs; see
31 # App::FileSync/LIMITATIONS.
32  
33 # Entry type, treating a missing "t" as a file.
34 1699 ▶my $type = sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
35 1699 my ($entry) = @_;
36 1699 ▶ return ($entry && defined $entry->{t} && length $entry->{t}) ? $entry->{t} : $TYPE_FILE;
Branch: 1 branch
BranchTrueFalseTotal
$entry && defined $entry->{'t'} && length $entry->{'t'} ? : 1699 0 1699
Condition33%2/6
$entry and defined $entry->{"t"} and length $entry->{"t"}
!ll&&!rl&&r
001699
$entry and defined $entry->{"t"}
!ll&&!rl&&r
001699
MC/DC0%0/3
$entry and defined $entry->{"t"} and length $entry->{"t"}
$entry defined $entry->{"t"} length $entry->{"t"}
Truth table25%1/4
A and B and C
A= $entryB= defined $entry->{"t"}C= length $entry->{"t"}
ABCresult
0XX0
10X0
1100
1111
37 315 };
38  
39 # Two entries describe the same content: both absent, both directories, or
40 # both files with the same hash.
41 534 ▶my $same = sub {
Subroutine __ANON__: called CC=10 SCAR=25.6
42 534 my ($left, $right) = @_;
43  
44 534 ▶ return 1 unless $left || $right;
Branch: 1 branch
BranchTrueFalseTotal
unless $left or $right 0 534 534
Condition33%1/3
$left or $right
l!l&&r!l&&!r
53400
MC/DC0%0/2
$left or $right
$left $right
Truth table33%1/3
A or B
A= $leftB= $right
ABresult
1X1
011
000
45 534 ▶ return 0 unless $left && $right;
Branch: 1 branch
BranchTrueFalseTotal
unless $left and $right 0 534 534
Condition33%1/3
$left and $right
!ll&&!rl&&r
00534
MC/DC0%0/2
$left and $right
$left $right
Truth table33%1/3
A and B
A= $leftB= $right
ABresult
0X0
100
111
46  
47 534 my $left_type = $type->($left);
48 534 ▶ return 0 unless $left_type eq $type->($right);
Branch: 1 branch
BranchTrueFalseTotal
unless $left_type eq &$type($right) 3 531 534
49 531 ▶ return 1 if $left_type eq $TYPE_DIR;
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq $App::FileSync::Merge::TYPE_DIR 20 511 531
50  
51 511 ▶ return (defined $left->{h} && defined $right->{h} && $left->{h} eq $right->{h}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $left->{'h'} && defined $right->{'h'} && $left->{'h'} eq $right->{'h'} ? : 428 83 511
Condition50%3/6
defined $left->{"h"} and defined $right->{"h"} and $left->{"h"} eq $right->{"h"}
!ll&&!rl&&r
083428
defined $left->{"h"} and defined $right->{"h"}
!ll&&!rl&&r
00511
MC/DC33%1/3
defined $left->{"h"} and defined $right->{"h"} and $left->{"h"} eq $right->{"h"}
defined $left->{"h"} defined $right->{"h"} $left->{"h"} eq $right->{"h"}
Truth table50%2/4
A and B and C
A= defined $left->{"h"}B= defined $right->{"h"}C= $left->{"h"} eq $right->{"h"}
ABCresult
0XX0
10X0
1100
1111
52 315 };
53  
54 # A pull or push action for one path, carrying the hash the receiver must
55 # verify against.
56 300 ▶my $transfer = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
57 300 my ($verb, $path, $entry) = @_;
58 return {
59 a => $verb,
60 p => $path,
61 t => $type->($entry),
62 300 ▶ (defined $entry->{h} ? (h => $entry->{h}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->{'h'} ? : 288 12 300
63 };
64 315 };
65  
66 =encoding utf8
67  
68 =head1 NAME
69  
70 App::FileSync::Merge - three-way merge of base, client and master manifests
71  
72 =head1 SYNOPSIS
73  
74 my $plan = App::FileSync::Merge::plan(
75 base => $base, client => $client, master => $master,
76 client_id => 'laptop-1', role => 'client', now => time,
77 );
78 my $refusal = App::FileSync::Merge::check_delete_limit(
79 deletions => $plan->{deletions}, base_size => scalar(keys %$base),
80 );
81  
82 =head1 FUNCTIONS
83  
84 =head2 plan
85  
86 Purpose: decide, path by path, what a session must do.
87  
88 Args: C<base>, C<client>, C<master> (manifests: path =E<gt> entry, see
89 L<App::FileSync::Scan>), C<client_id>, C<role> (C<client> or C<replica>),
90 C<now> (the epoch used in conflict-copy names).
91  
92 Returns: a hashref C<{ actions, warnings, deletions, conflicts }>. Actions are
93 hashes with C<a> (pull, push, delete_local, delete_remote or conflict) and
94 C<p> (the path); deletions come first, so a type change never writes over a
95 directory. Warnings are translated sentences.
96  
97 Side Effects: none.
98  
99 Usage:
100  
101 my $plan = App::FileSync::Merge::plan(%manifests, client_id => $id);
102  
103 =head3 EXAMPLE
104  
105 my $plan = App::FileSync::Merge::plan(
106 base => {},
107 client => { 'a.txt' => { t => 'f', h => 'aa' } },
108 master => {},
109 );
110 print $plan->{actions}[0]{a}, "\n"; # push
111  
112 =head3 API SPECIFICATION
113  
114 =head4 INPUT
115  
116 {
117 base => { type => 'hashref', optional => 1 },
118 client => { type => 'hashref', optional => 1 },
119 master => { type => 'hashref', optional => 1 },
120 client_id => { type => 'string', optional => 1 },
121 role => { type => 'string', memberof => [ 'client', 'replica' ], optional => 1 },
122 now => { type => 'number', optional => 1 },
123 }
124  
125 =head4 OUTPUT
126  
127 {
128 type => 'hashref',
129 schema => {
130 actions => { type => 'arrayref' },
131 warnings => { type => 'arrayref' },
132 deletions => { type => 'integer', min => 0 },
133 conflicts => { type => 'integer', min => 0 },
134 },
135 }
136  
137 =head3 MESSAGES
138  
139 Warnings (returned, not raised):
140  
141 Key Meaning
142 merge.deleted_changed deleted on the client, changed on the master; master copy restored
143 merge.changed_deleted changed on the client, deleted on the master; client copy restored
144 merge.conflict_copy changed on both; loser kept as a conflict copy
145 merge.conflict_dir changed on both; an empty directory lost
146 merge.replica_overwritten a replica's local edit was overwritten
147 merge.replica_removed a replica's extra file was removed
148  
149 =head3 FORMAL SPECIFICATION
150  
151 B, A, M : PATH ⇸ ENTRY (base, client, master)
152 ∀ p ∈ dom B ∪ dom A ∪ dom M •
153 p ∉ dom A ∧ p ∉ dom M ⇒ no action
154 p ∉ dom A ∧ same(M p, B p) ⇒ delete_remote p
155 p ∉ dom A ∧ ¬same(M p, B p) ⇒ pull p
156 p ∉ dom M ∧ same(A p, B p) ⇒ delete_local p
157 p ∉ dom M ∧ ¬same(A p, B p) ⇒ push p
158 same(A p, M p) ⇒ no action
159 same(A p, B p) ⇒ pull p
160 same(M p, B p) ⇒ push p
161 otherwise ⇒ conflict p
162 role = replica ⇒ actions ⊆ {pull, delete_local}
163  
164 =head3 PSEUDOCODE
165  
166 if replica: mirror the master and return
167 for each path in base, client or master, sorted:
168 apply the table above, adding a warning where one side's change
169 is undone or a conflict copy is made
170 put deletions before transfers
171 count deletions and conflicts
172  
173 =cut
174  
175 199 ▶sub plan {
Subroutine plan: called CC=22 SCAR=31.0
Pod documented
176 199 my $args = named(\@_, {
177 base => { type => 'hashref', optional => 1 },
178 client => { type => 'hashref', optional => 1 },
179 master => { type => 'hashref', optional => 1 },
180 client_id => { type => 'string', optional => 1 },
181 role => { type => 'string', memberof => [@ROLES], optional => 1 },
182 now => { type => 'number', optional => 1 },
183 }, 'Merge::plan');
184  
185 199 ▶ my $base = $args->{base} || {};
Condition50%1/2
$args->{'base'} || {}
l!l
1990
MC/DC0%0/1
$args->{'base'} || {}
$args->{'base'}
Truth table50%1/2
A
A= $args->{'base'}
Aresult
11
00
186 199 ▶ my $client = $args->{client} || {};
Condition50%1/2
$args->{'client'} || {}
l!l
1990
MC/DC0%0/1
$args->{'client'} || {}
$args->{'client'}
Truth table50%1/2
A
A= $args->{'client'}
Aresult
11
00
187 199 ▶ my $master = $args->{master} || {};
Condition50%1/2
$args->{'master'} || {}
l!l
1990
MC/DC0%0/1
$args->{'master'} || {}
$args->{'master'}
Truth table50%1/2
A
A= $args->{'master'}
Aresult
11
00
188 199 ▶ my $client_id = defined $args->{client_id} ? $args->{client_id} : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'client_id'} ? : 199 0 199
189 199 ▶ my $now = defined $args->{now} ? $args->{now} : time;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'now'} ? : 198 1 199
190  
191 return _replica_plan($base, $client, $master)
192 199 ▶ if ($args->{role} || $ROLE_CLIENT) eq $ROLE_REPLICA;
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"role"} || $App::FileSync::Merge::ROLE_CLIENT) eq $App::FileSync::Merge::ROLE_REPLICA 29 170 199
Condition66%2/3
$args->{'role'} || $App::FileSync::Merge::ROLE_CLIENT
l!l&&r!l&&!r
175240
MC/DC0%0/2
$args->{'role'} || $App::FileSync::Merge::ROLE_CLIENT
$args->{'role'} $App::FileSync::Merge::ROLE_CLIENT
Truth table66%2/3
A || B
A= $args->{'role'}B= $App::FileSync::Merge::ROLE_CLIENT
ABresult
1X1
011
000
193  
194 170 my (@actions, @warnings);
195 170 my %paths = (%{$base}, %{$client}, %{$master});
196  
197 170 for my $path (sort keys %paths) {
198 567 my ($b, $a, $m) = ($base->{$path}, $client->{$path}, $master->{$path});
199 567 my %who = (path => $path, client => $client_id);
200  
201 # Gone on both sides: nothing to do, the base entry is dropped at commit.
202 567 ▶ next unless $a || $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $a or $m 1 566 567
Condition100%3/3
$a or $m
l!l&&r!l&&!r
506601
MC/DC100%2/2
$a or $m
$a $m
Truth table100%3/3
A or B
A= $aB= $m
ABresult
1X1
011
000
203  
204 566 ▶ if (!$a || !$m) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $a && $m) { } 254 312 566
elsif (&$same($a, $m)) { } 273 39 312
elsif ($b and &$same($a, $b)) { } 6 33 39
elsif ($b and &$same($m, $b)) { } 9 24 33
Condition100%3/3
$a && $m
!ll&&!rl&&r
60194312
MC/DC100%2/2
$a && $m
$a $m
Truth table100%3/3
A && B
A= $aB= $m
ABresult
0X0
100
111
205 # One side lacks it: an unchanged survivor follows the deletion;
206 # a changed one wins, and the deletion is undone with a warning.
207 254 ▶ my ($have, $verb, $delete, $undo) = $a
Branch: 1 branch
BranchTrueFalseTotal
$a ? : 194 60 254
208 ? ($a, 'push', { a => 'delete_local', p => $path, t => $type->($a) }, 'merge.changed_deleted')
209 : ($m, 'pull', { a => 'delete_remote', p => $path }, 'merge.deleted_changed');
210  
211 254 ▶ if ($b && $same->($have, $b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b and &$same($have, $b)) { } 44 210 254
Condition100%3/3
$b and &$same($have, $b)
!ll&&!rl&&r
205544
MC/DC100%2/2
$b and &$same($have, $b)
$b &$same($have, $b)
Truth table100%3/3
A and B
A= $bB= &$same($have, $b)
ABresult
0X0
100
111
212 44 push @actions, $delete;
213 } else {
214 210 push @actions, $transfer->($verb, $path, $have);
215 210 ▶ push @warnings, message($undo, \%who) if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 5 205 210
216 }
217 } elsif ($same->($a, $m)) {
218 273 next; # already converged
219 ▶ } elsif ($b && $same->($a, $b)) {
Condition100%3/3
$b and &$same($a, $b)
!ll&&!rl&&r
11226
MC/DC100%2/2
$b and &$same($a, $b)
$b &$same($a, $b)
Truth table100%3/3
A and B
A= $bB= &$same($a, $b)
ABresult
0X0
100
111
220 6 push @actions, $transfer->('pull', $path, $m);
221 ▶ } elsif ($b && $same->($m, $b)) {
Condition100%3/3
$b and &$same($m, $b)
!ll&&!rl&&r
11139
MC/DC100%2/2
$b and &$same($m, $b)
$b &$same($m, $b)
Truth table100%3/3
A and B
A= $bB= &$same($m, $b)
ABresult
0X0
100
111
222 9 push @actions, $transfer->('push', $path, $a);
223 } else {
224 24 my $resolution = _conflict($path, $a, $m, $client_id, $now);
225 24 push @actions, $resolution;
226 24 push @warnings, _conflict_warning($resolution, \%who);
227 }
228 }
229  
230 170 return _ordered(\@actions, \@warnings);
231 }
232  
233 =head2 check_delete_limit
234  
235 Purpose: guard against a misconfigured client (or an empty mount) wiping the
236 master.
237  
238 Args: C<deletions> (planned), C<base_size> (files known at the last sync),
239 C<max_deletes> (default 200; 0 disables), C<max_delete_percent> (default 10;
240 0 disables), C<force>.
241  
242 Returns: C<undef> when the plan is acceptable, or a translated sentence
243 explaining the refusal. At least one deletion is always allowed, so a small
244 repo stays usable.
245  
246 Side Effects: none.
247  
248 Usage:
249  
250 my $refusal = App::FileSync::Merge::check_delete_limit(%counts, force => $force);
251  
252 =head3 EXAMPLE
253  
254 my $why = App::FileSync::Merge::check_delete_limit(deletions => 300, base_size => 1000);
255 print "$why\n" if defined $why; # refusing to delete 300 files (limit is 200); ...
256  
257 =head3 API SPECIFICATION
258  
259 =head4 INPUT
260  
261 {
262 deletions => { type => 'integer', min => 0, optional => 1 },
263 base_size => { type => 'integer', min => 0, optional => 1 },
264 max_deletes => { type => 'integer', min => 0, optional => 1 },
265 max_delete_percent => { type => 'number', min => 0, optional => 1 },
266 force => { type => 'boolean', optional => 1 },
267 }
268  
269 =head4 OUTPUT
270  
271 { type => 'string', optional => 1 }
272  
273 =head3 MESSAGES
274  
275 Key Meaning Resolution
276 merge.too_many over the absolute limit --force if intended
277 merge.too_many_percent over the percentage of known files --force if intended
278  
279 =head3 FORMAL SPECIFICATION
280  
281 allowed(n, k) ⇔ n = 0 ∨ force ∨ ((max = 0 ∨ n ≤ max) ∧ (pct = 0 ∨ k = 0 ∨ n ≤ max(1, ⌊k × pct / 100⌋)))
282 check(n, k) = ⊥ if allowed(n, k), reason otherwise
283  
284 =cut
285  
286 183 ▶sub check_delete_limit {
Subroutine check_delete_limit: called CC=13 SCAR=25.7
Pod documented
287 183 my $args = named(\@_, {
288 deletions => { type => 'integer', min => 0, optional => 1 },
289 base_size => { type => 'integer', min => 0, optional => 1 },
290 max_deletes => { type => 'integer', min => 0, optional => 1 },
291 max_delete_percent => { type => 'number', min => 0, optional => 1 },
292 force => { type => 'boolean', optional => 1 },
293 }, 'Merge::check_delete_limit');
294  
295 183 ▶ my $deletions = $args->{deletions} || 0;
Condition100%2/2
$args->{'deletions'} || 0
l!l
39144
MC/DC100%1/1
$args->{'deletions'} || 0
$args->{'deletions'}
Truth table100%2/2
A
A= $args->{'deletions'}
Aresult
11
00
296 183 ▶ my $known = $args->{base_size} || 0;
Condition100%2/2
$args->{'base_size'} || 0
l!l
10380
MC/DC100%1/1
$args->{'base_size'} || 0
$args->{'base_size'}
Truth table100%2/2
A
A= $args->{'base_size'}
Aresult
11
00
297 183 ▶ my $max = defined $args->{max_deletes} ? $args->{max_deletes} : $MAX_DELETES;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'max_deletes'} ? : 181 2 183
298 183 ▶ my $percent = defined $args->{max_delete_percent} ? $args->{max_delete_percent} : $MAX_DELETE_PERCENT;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'max_delete_percent'} ? : 181 2 183
299  
300 # Never block a single deletion just because the repo is small: 10% of
301 # five files is zero, which would make the tool unusable.
302 183 my $allowed = int($known * $percent / $PERCENT);
303 183 ▶ $allowed = 1 if $allowed < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $allowed < 1 160 23 183
304  
305 183 my %args = (count => $deletions, known => $known, max => $max, percent => $percent);
306 my $refusal =
307 183 ▶ ($deletions == 0 || $args->{force}) ? undef
Branch: 3 branches
BranchTrueFalseTotal
$deletions == 0 || $args->{'force'} ? : 153 30 183
$max && $deletions > $max ? : 3 27 30
$percent && $known > 0 && $deletions > $allowed ? : 7 20 27
Condition83%10/12
$deletions == 0 or $args->{"force"}
l!l&&r!l&&!r
144930
$max and $deletions > $max
!ll&&!rl&&r
0273
$percent and $known > 0 and $deletions > $allowed
!ll&&!rl&&r
2187
$percent and $known > 0
!ll&&!rl&&r
0225
MC/DC100%2/2
$deletions == 0 or $args->{"force"}
$deletions == 0 $args->{"force"}
MC/DC50%1/2
$max and $deletions > $max
$max $deletions > $max
MC/DC66%2/3
$percent and $known > 0 and $deletions > $allowed
$percent $known > 0 $deletions > $allowed
Truth table100%3/3
A or B
A= $deletions == 0B= $args->{"force"}
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= $maxB= $deletions > $max
ABresult
0X0
100
111
Truth table75%3/4
A and B and C
A= $percentB= $known > 0C= $deletions > $allowed
ABCresult
0XX0
10X0
1100
1111
308 : ($max && $deletions > $max) ? message('merge.too_many', \%args)
309 : ($percent && $known > 0 && $deletions > $allowed) ? message('merge.too_many_percent', \%args)
310 : undef;
311  
312 183 return $refusal;
313 }
314  
315 # _replica_plan
316 # Purpose: plan for a replica: mirror the master, never push.
317 # Entry: manifests for base, replica and master.
318 # Exit: returns the same shape as plan(); local edits are reported and
319 # then overwritten, local extras removed.
320 # Side effects: none.
321 29 ▶sub _replica_plan :Private {
Subroutine _replica_plan: called CC=8 SCAR=20.8
322 29 my ($base, $client, $master) = @_;
323  
324 29 my (@actions, @warnings);
325  
326 29 for my $path (sort keys %{$master}) {
327 189 my $have = $client->{$path};
328 189 ▶ next if $have && $same->($have, $master->{$path});
Branch: 1 branch
BranchTrueFalseTotal
if $have and &$same($have, $master->{$path}) 114 75 189
Condition100%3/3
$have and &$same($have, $master->{$path})
!ll&&!rl&&r
705114
MC/DC100%2/2
$have and &$same($have, $master->{$path})
$have &$same($have, $master->{$path})
Truth table100%3/3
A and B
A= $haveB= &$same($have, $master->{$path})
ABresult
0X0
100
111
329  
330 75 push @actions, $transfer->('pull', $path, $master->{$path});
331 push @warnings, message('merge.replica_overwritten', { path => $path })
332 75 ▶ if $have && $base->{$path} && !$same->($have, $base->{$path});
Branch: 1 branch
BranchTrueFalseTotal
if $have and $base->{$path} and not &$same($have, $base->{$path}) 2 73 75
Condition100%6/6
$have and $base->{$path} and not &$same($have, $base->{$path})
!ll&&!rl&&r
7122
$have and $base->{$path}
!ll&&!rl&&r
7014
MC/DC100%3/3
$have and $base->{$path} and not &$same($have, $base->{$path})
$have $base->{$path} not &$same($have, $base->{$path})
Truth table100%4/4
A and B and not(C)
A= $haveB= $base->{$path}C= not &$same($have, $base->{$path})
ABCresult
0XX0
10X0
1100
1111
333 }
334  
335 29 for my $path (sort grep { !$master->{$_} } keys %{$client}) {
336 41 push @actions, { a => 'delete_local', p => $path, t => $type->($client->{$path}) };
337 41 push @warnings, message('merge.replica_removed', { path => $path });
338 }
339  
340 29 return _ordered(\@actions, \@warnings);
341 315 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
342  
343 # _ordered
344 # Purpose: the plan result: deletions first, with counts.
345 # Entry: $actions and $warnings are arrayrefs.
346 # Exit: returns { actions, warnings, deletions, conflicts }.
347 # Side effects: none.
348 199 ▶sub _ordered :Private {
Subroutine _ordered: called CC=1 SCAR=0.0
349 199 my ($actions, $warnings) = @_;
350  
351 # Deletions run first so a type change never tries to write over a directory.
352 199 my @deletions = grep { $_->{a} =~ /\Adelete_/ } @{$actions};
353 199 my @transfers = grep { $_->{a} !~ /\Adelete_/ } @{$actions};
354  
355 return {
356 actions => [ @deletions, @transfers ],
357 warnings => $warnings,
358 deletions => scalar(@deletions),
359 199 conflicts => scalar(grep { $_->{a} eq 'conflict' } @transfers),
360 };
361 315 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
362  
363 # _conflict
364 # Purpose: resolve a path both sides changed.
365 # Entry: both entries exist and differ from each other and the base.
366 # Exit: returns a conflict action naming the winner and, for a losing
367 # file, the name to keep it under.
368 # Side effects: none.
369 24 ▶sub _conflict :Private {
Subroutine _conflict: called CC=9 SCAR=22.5
370 24 my ($path, $client_entry, $master_entry, $client_id, $now) = @_;
371  
372 24 my ($client_type, $master_type) = ($type->($client_entry), $type->($master_entry));
373  
374 # A file beats an empty directory; between two files the newer wins, and
375 # within the clock tolerance the master wins deterministically.
376 my $winner = $client_type ne $TYPE_FILE ? 'remote'
377 : $master_type ne $TYPE_FILE ? 'client'
378 24 ▶ : ($client_entry->{m} || 0) > ($master_entry->{m} || 0) + $MTIME_TOLERANCE ? 'client'
Branch: 3 branches
BranchTrueFalseTotal
$client_type ne $App::FileSync::Merge::TYPE_FILE ? : 0 24 24
$master_type ne $App::FileSync::Merge::TYPE_FILE ? : 1 23 24
($client_entry->{'m'} || 0) > ($master_entry->{'m'} || 0) + $App::FileSync::Merge::MTIME_TOLERANCE ? : 14 9 23
Condition50%2/4
$client_entry->{'m'} || 0
l!l
230
$master_entry->{'m'} || 0
l!l
230
MC/DC0%0/1
$client_entry->{'m'} || 0
$client_entry->{'m'}
MC/DC0%0/1
$master_entry->{'m'} || 0
$master_entry->{'m'}
Truth table50%1/2
A
A= $client_entry->{'m'}
Aresult
11
00
Truth table50%1/2
A
A= $master_entry->{'m'}
Aresult
11
00
379 : 'remote';
380  
381 24 ▶ my ($winner_entry, $loser) = $winner eq 'client'
Branch: 1 branch
BranchTrueFalseTotal
$winner eq 'client' ? : 15 9 24
382 ? ($client_entry, $master_entry)
383 : ($master_entry, $client_entry);
384 24 my $loser_type = $type->($loser);
385  
386 return {
387 a => 'conflict',
388 p => $path,
389 winner => $winner,
390 winner_type => $type->($winner_entry),
391 24 ▶ (defined $winner_entry->{h} ? (h => $winner_entry->{h}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $winner_entry->{'h'} ? : 24 0 24
$loser_type eq $App::FileSync::Merge::TYPE_FILE ? : 23 1 24
392 ($loser_type eq $TYPE_FILE ? (loser_name => conflict_name($path, $client_id, $now)) : ()),
393 loser_type => $loser_type,
394 };
395 315 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
396  
397 # _conflict_warning
398 # Purpose: the sentence that reports a conflict.
399 # Entry: $resolution is a conflict action; $who holds path and client.
400 # Exit: returns the translated warning.
401 # Side effects: none.
402 24 ▶sub _conflict_warning :Private {
Subroutine _conflict_warning: called CC=3 SCAR=11.0
403 24 my ($resolution, $who) = @_;
404  
405 24 my $master = message('merge.master');
406 my ($kept, $other) = $resolution->{winner} eq 'client'
407 ? ($who->{client}, $master)
408 24 ▶ : ($master, $who->{client});
Branch: 1 branch
BranchTrueFalseTotal
$resolution->{'winner'} eq 'client' ? : 15 9 24
409  
410 return message($resolution->{loser_name} ? 'merge.conflict_copy' : 'merge.conflict_dir', {
411 24 %{$who},
412 kept => $kept,
413 other => $other,
414 copy => $resolution->{loser_name},
415 24 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$resolution->{'loser_name'} ? : 23 1 24
416 315 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
417  
418 315 1;
419  
420 __END__
« blib/lib/App/FileSync/Process.pm ↑ Summary blib/lib/App/FileSync/Snapshot.pm »