blib/lib/App/FileSync/Verify.pm

filter: Statementstmt 95.5% 108 / 113 Branchbran 75.0% 21 / 28 Conditioncond 59.0% 26 / 44 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 80.2% 183 / 228 CC 33 SCAR 22.5
CC
33
Cov
84%
_sample_check
16.0
_compare
15.2
verify
10.0
CRAP 9.5
« blib/lib/App/FileSync/Snapshot.pm ↑ Summary blib/lib/App/FileSync/Scan.pm »
1 package App::FileSync::Verify;
2  
3 # Compare a tree with the record of what it should hold (the server index, or
4 # the client's cached base) and report the differences. Read-only.
5  
6 327 ▶use App::FileSync::Platform;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 327 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 327 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 327 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 327 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 327 ▶use List::Util 1.54 qw(sample);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 327 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 327 ▶use App::FileSync::Args qw(named);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 327 ▶use App::FileSync::Constants qw($TYPE_FILE $TYPE_DIR);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 327 ▶use App::FileSync::I18N qw(message throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 327 ▶use App::FileSync::IO qw(sha256_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 327 ▶use App::FileSync::Scan;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 327 ▶use version; our $VERSION = version->declare('0.002.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # The type of a manifest entry; older entries without "t" are files. Lexical
25 # because it runs several times per path.
26 159 ▶my $type_of = sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
27 159 my ($entry) = @_;
28 159 ▶ return (ref($entry) eq 'HASH' && defined $entry->{t}) ? $entry->{t} : $TYPE_FILE;
Branch: 1 branch
BranchTrueFalseTotal
ref $entry eq 'HASH' && defined $entry->{'t'} ? : 159 0 159
Condition33%1/3
ref $entry eq "HASH" and defined $entry->{"t"}
!ll&&!rl&&r
00159
MC/DC0%0/2
ref $entry eq "HASH" and defined $entry->{"t"}
ref $entry eq "HASH" defined $entry->{"t"}
Truth table33%1/3
A and B
A= ref $entry eq "HASH"B= defined $entry->{"t"}
ABresult
0X0
100
111
29 327 };
30  
31 =encoding utf8
32  
33 =head1 NAME
34  
35 App::FileSync::Verify - check a tree against its recorded manifest
36  
37 =head1 SYNOPSIS
38  
39 my $report = App::FileSync::Verify::verify(
40 root => $master, entries => $index, ignore => $ignore, sample => 50,
41 );
42 print $report->{ok} ? "clean\n" : "drift\n";
43  
44 =head1 FUNCTIONS
45  
46 =head2 verify
47  
48 Purpose: report where a tree no longer matches its record.
49  
50 Args: C<root> (required, a directory), C<entries> (the recorded manifest),
51 C<ignore> (an L<App::FileSync::Ignore>), C<deep> (rehash every file),
52 C<sample> (without C<deep>, rehash this many unchanged-looking files chosen at
53 random).
54  
55 Without C<deep>, entries whose size and mtime still match are trusted, which
56 is what makes a verify of a large tree cheap.
57  
58 Returns: a hashref:
59  
60 root, ok (1 when nothing differs), deep, files (regular files found),
61 known (entries in the record), rehashed, trusted, sampled,
62 modified => [ { p, expected, got } | { p, reason } | { p, expected, got, sampled } ],
63 missing => [ { p, t } ],
64 extra => [ { p, t } ]
65  
66 Each list is sorted by path.
67  
68 Side Effects: reads files; never writes.
69  
70 Usage:
71  
72 my $report = App::FileSync::Verify::verify(root => $root, entries => $base, deep => 1);
73  
74 =head3 EXAMPLE
75  
76 my $report = App::FileSync::Verify::verify(root => '/srv/filesync/master/docs', entries => $index);
77 print "missing: $_->{p}\n" for @{ $report->{missing} };
78  
79 =head3 API SPECIFICATION
80  
81 =head4 INPUT
82  
83 {
84 root => { type => 'string', min => 1 },
85 entries => { type => 'hashref', optional => 1 },
86 ignore => { type => 'object', optional => 1 },
87 deep => { type => 'boolean', optional => 1 },
88 sample => { type => 'integer', min => 0, optional => 1 },
89 }
90  
91 =head4 OUTPUT
92  
93 {
94 type => 'hashref',
95 schema => {
96 ok => { type => 'integer', min => 0, max => 1 },
97 modified => { type => 'arrayref' },
98 missing => { type => 'arrayref' },
99 extra => { type => 'arrayref' },
100 },
101 }
102  
103 =head3 MESSAGES
104  
105 Key Meaning Resolution
106 verify.no_root root is not a directory check the path
107 verify.type_changed (a reason in "modified") a file became a directory or back
108  
109 =head3 FORMAL SPECIFICATION
110  
111 let M = scan(root).manifest ; E = entries
112 missing = { p ∈ dom E | p ∉ dom M ∧ ¬(E(p).t = d ∧ isdir(root/p)) }
113 modified = { p ∈ dom E ∩ dom M | E(p).t ≠ M(p).t ∨ (E(p).t = f ∧ E(p).h ≠ M(p).h) }
114 extra = dom M \ dom E \ (missing ∪ modified)
115 ok ⇔ missing = modified = extra = ∅
116  
117 =head3 PSEUDOCODE
118  
119 scan the tree, reusing recorded hashes unless deep
120 for each recorded path: missing, type changed, or hash differs
121 every scanned path not recorded is extra
122 if sampling: rehash that many trusted files and add any that differ
123 sort the three lists and count
124  
125 =cut
126  
127 26 ▶sub verify {
Subroutine verify: called CC=10 SCAR=23.0
Pod documented
128 26 my $args = named(\@_, {
129 root => { type => 'string', min => 1 },
130 entries => { type => 'hashref', optional => 1 },
131 ignore => { type => 'object', optional => 1 },
132 deep => { type => 'boolean', optional => 1 },
133 sample => { type => 'integer', min => 0, optional => 1 },
134 }, 'Verify::verify');
135  
136 26 my $root = $args->{root};
137 26 ▶ throw('verify.no_root', { root => $root }) unless -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 0 26 26
138  
139 26 ▶ my $entries = $args->{entries} || {};
Condition50%1/2
$args->{'entries'} || {}
l!l
260
MC/DC0%0/1
$args->{'entries'} || {}
$args->{'entries'}
Truth table50%1/2
A
A= $args->{'entries'}
Aresult
11
00
140 26 ▶ my $deep = $args->{deep} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'deep'} ? : 3 23 26
141  
142 my $scan = App::FileSync::Scan::scan(
143 root => $root,
144 ignore => $args->{ignore},
145 26 ▶ cache => $deep ? {} : $entries,
Branch: 1 branch
BranchTrueFalseTotal
$deep ? : 3 23 26
146 );
147 26 my $manifest = $scan->{manifest};
148  
149 26 my $found = _compare($root, $entries, $manifest);
150 my $sampled = ($deep || !$args->{sample})
151 ? 0
152 26 ▶ : _sample_check($root, $entries, $manifest, $args->{sample}, $found);
Branch: 1 branch
BranchTrueFalseTotal
$deep || !$args->{'sample'} ? : 23 3 26
Condition100%3/3
$deep or not $args->{"sample"}
l!l&&r!l&&!r
3203
MC/DC100%2/2
$deep or not $args->{"sample"}
$deep not $args->{"sample"}
Truth table100%3/3
A or not(B)
A= $deepB= not $args->{"sample"}
ABresult
1X1
011
000
153  
154 my %sorted = map {
155 26 my $list = $found->{$_};
156 78 $_ => [ sort { $a->{p} cmp $b->{p} } @{$list} ];
157 } qw(modified missing extra);
158  
159 return {
160 root => $root,
161 26 ok => (@{ $sorted{modified} } || @{ $sorted{missing} } || @{ $sorted{extra} }) ? 0 : 1,
162 deep => $deep,
163 52 files => scalar(grep { $type_of->($manifest->{$_}) eq $TYPE_FILE } keys %{$manifest}),
164 26 known => scalar(keys %{$entries}),
165 rehashed => $scan->{stats}{hashed} + $sampled,
166 trusted => $scan->{stats}{cached},
167 26 ▶ sampled => $sampled,
Branch: 1 branch
BranchTrueFalseTotal
@{$sorted{'modified'};} || @{$sorted{'missing'};} || @{$sorted{'extra'};} ? : 14 12 26
Condition100%6/6
@{$sorted{"modified"};} or @{$sorted{"missing"};} or @{$sorted{"extra"};}
l!l&&r!l&&!r
12212
@{$sorted{"modified"};} or @{$sorted{"missing"};}
l!l&&r!l&&!r
11114
MC/DC100%3/3
@{$sorted{"modified"};} or @{$sorted{"missing"};} or @{$sorted{"extra"};}
@{$sorted{"modified"};} @{$sorted{"missing"};} @{$sorted{"extra"};}
Truth table100%4/4
A or B or C
A= @{$sorted{"modified"};}B= @{$sorted{"missing"};}C= @{$sorted{"extra"};}
ABCresult
1XX1
01X1
0011
0000
168 %sorted,
169 };
170 }
171  
172 # _compare
173 # Purpose: compare the record with what the scan found.
174 # Entry: $entries is the record; $manifest the fresh scan.
175 # Exit: returns { modified, missing, extra, seen } (lists unsorted;
176 # seen is the set of paths already reported).
177 # Side effects: none; stats one directory per recorded empty dir that is gone
178 # from the manifest.
179 26 ▶sub _compare :Private {
Subroutine _compare: called CC=14 SCAR=27.2
180 26 my ($root, $entries, $manifest) = @_;
181  
182 26 my (%found, %seen);
183 26 @found{qw(modified missing extra)} = ([], [], []);
184  
185 26 for my $path (sort keys %{$entries}) {
186 45 my $entry = $entries->{$path};
187 45 ▶ next unless ref($entry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 45 45
188  
189 45 my $type = $type_of->($entry);
190 45 my $have = $manifest->{$path};
191 45 my $problem;
192  
193 45 ▶ if (!$have) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $have) { } 4 41 45
elsif ($type ne &$type_of($have)) { } 0 41 41
elsif ($type eq $App::FileSync::Verify::TYPE_FILE and defined $entry->{'h'} and defined $have->{'h'} and $entry->{'h'} ne $have->{'h'}) { } 11 30 41
194 # The scanner only reports directories that are empty, so an empty
195 # directory that has since gained a file is not missing.
196 4 ▶ next if $type eq $TYPE_DIR && -d File::Spec->catfile($root, split m{/}, $path);
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $App::FileSync::Verify::TYPE_DIR and -d "File::Spec"->catfile($root, split(m[/], $path, 0)) 0 4 4
Condition33%1/3
$type eq $App::FileSync::Verify::TYPE_DIR and -d "File::Spec"->catfile($root, split(m[/], $path, 0))
!ll&&!rl&&r
400
MC/DC0%0/2
$type eq $App::FileSync::Verify::TYPE_DIR and -d "File::Spec"->catfile($root, split(m[/], $path, 0))
$type eq $App::FileSync::Verify::TYPE_DIR -d "File::Spec"->catfile($root, split(m[/], $path, 0))
Truth table33%1/3
A and B
A= $type eq $App::FileSync::Verify::TYPE_DIRB= -d "File::Spec"->catfile($root, split(m[/], $path, 0))
ABresult
0X0
100
111
197 4 push @{ $found{missing} }, { p => $path, t => $type };
198 } elsif ($type ne $type_of->($have)) {
199 0 push @{ $found{modified} }, { p => $path, reason => message('verify.type_changed') };
200 ▶ } elsif ($type eq $TYPE_FILE && defined $entry->{h} && defined $have->{h} && $entry->{h} ne $have->{h}) {
Condition77%7/9
$type eq $App::FileSync::Verify::TYPE_FILE and defined $entry->{"h"} and defined $have->{"h"} and $entry->{"h"} ne $have->{"h"}
!ll&&!rl&&r
92111
$type eq $App::FileSync::Verify::TYPE_FILE and defined $entry->{"h"} and defined $have->{"h"}
!ll&&!rl&&r
9032
$type eq $App::FileSync::Verify::TYPE_FILE and defined $entry->{"h"}
!ll&&!rl&&r
9032
MC/DC50%2/4
$type eq $App::FileSync::Verify::TYPE_FILE and defined $entry->{"h"} and defined $have->{"h"} and $entry->{"h"} ne $have->{"h"}
$type eq $App::FileSync::Verify::TYPE_FILE defined $entry->{"h"} defined $have->{"h"} $entry->{"h"} ne $have->{"h"}
Truth table60%3/5
A and B and C and D
A= $type eq $App::FileSync::Verify::TYPE_FILEB= defined $entry->{"h"}C= defined $have->{"h"}D= $entry->{"h"} ne $have->{"h"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
201 11 push @{ $found{modified} }, { p => $path, expected => $entry->{h}, got => $have->{h} };
202 } else {
203 30 next;
204 }
205 15 $seen{$path} = 1;
206 }
207  
208 26 for my $path (sort keys %{$manifest}) {
209 52 ▶ next if $entries->{$path} || $seen{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $entries->{$path} or $seen{$path} 41 11 52
Condition66%2/3
$entries->{$path} or $seen{$path}
l!l&&r!l&&!r
41011
MC/DC50%1/2
$entries->{$path} or $seen{$path}
$entries->{$path} $seen{$path}
Truth table66%2/3
A or B
A= $entries->{$path}B= $seen{$path}
ABresult
1X1
011
000
210 11 push @{ $found{extra} }, { p => $path, t => $type_of->($manifest->{$path}) };
211 }
212  
213 26 $found{seen} = \%seen;
214 26 return \%found;
215 327 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217 # _sample_check
218 # Purpose: rehash a random sample of files the scan trusted.
219 # Entry: $wanted > 0; $found is _compare's result, updated in place.
220 # Exit: returns how many files were rehashed.
221 # Side effects: reads the sampled files; appends to $found->{modified}.
222 3 ▶sub _sample_check :Private {
Subroutine _sample_check: called CC=9 SCAR=27.7
223 3 my ($root, $entries, $manifest, $wanted, $found) = @_;
224  
225 # Only a file on both sides can be compared by content.
226 my @candidates = grep {
227 ref($entries->{$_}) eq 'HASH'
228 && $type_of->($entries->{$_}) eq $TYPE_FILE
229 && $manifest->{$_}
230 5 ▶ && $type_of->($manifest->{$_}) eq $TYPE_FILE
Condition33%3/9
ref $entries->{$_} eq 'HASH' && &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILE && $manifest->{$_} && &$type_of($manifest->{$_}) eq $App::FileSync::Verify::TYPE_FILE
!ll&&!rl&&r
005
ref $entries->{$_} eq "HASH" and &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILE and $manifest->{$_}
!ll&&!rl&&r
005
ref $entries->{$_} eq "HASH" and &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILE
!ll&&!rl&&r
005
MC/DC0%0/4
ref $entries->{$_} eq 'HASH' && &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILE && $manifest->{$_} && &$type_of($manifest->{$_}) eq $App::FileSync::Verify::TYPE_FILE
ref $entries->{$_} eq "HASH" &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILE $manifest->{$_} &$type_of($manifest->{$_}) eq $App::FileSync::Verify::TYPE_FILE
Truth table20%1/5
A and B and C && D
A= ref $entries->{$_} eq "HASH"B= &$type_of($entries->{$_}) eq $App::FileSync::Verify::TYPE_FILEC= $manifest->{$_}D= &$type_of($manifest->{$_}) eq $App::FileSync::Verify::TYPE_FILE
ABCDresult
0XXX0
10XX0
110X0
11100
11111
231 3 } sort keys %{$entries};
232  
233 3 my $sampled = 0;
234 3 for my $path (sample($wanted, @candidates)) {
235 4 my $got = sha256_file(File::Spec->catfile($root, split m{/}, $path));
236 4 ▶ next unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 4 4
237 4 $sampled++;
238  
239 4 my $expected = $entries->{$path}{h};
240 4 ▶ next if !defined $expected || $got eq $expected || $found->{seen}{$path};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $expected or $got eq $expected or $found->{"seen"}{$path} 4 0 4
Condition33%2/6
not defined $expected or $got eq $expected or $found->{"seen"}{$path}
l!l&&r!l&&!r
400
not defined $expected or $got eq $expected
l!l&&r!l&&!r
040
MC/DC0%0/3
not defined $expected or $got eq $expected or $found->{"seen"}{$path}
not defined $expected $got eq $expected $found->{"seen"}{$path}
Truth table25%1/4
not(A) or B or C
A= not defined $expectedB= $got eq $expectedC= $found->{"seen"}{$path}
ABCresult
1XX1
01X1
0011
0000
241  
242 0 push @{ $found->{modified} }, { p => $path, expected => $expected, got => $got, sampled => 1 };
243 0 $found->{seen}{$path} = 1;
244 }
245  
246 3 return $sampled;
247 327 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249 327 1;
250  
251 __END__
« blib/lib/App/FileSync/Snapshot.pm ↑ Summary blib/lib/App/FileSync/Scan.pm »