lib/Archive/BagIt/Role/Manifest.pm

filter: Statementstmt 98.0% 154 / 157 Branchbran 76.1% 32 / 42 Conditioncond 47.0% 8 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 95.8% 23 / 24 Podpod 83.3% 5 / 6 totaltotal 87.1% 224 / 257 CC 32 SCAR 17.2
CC
32
Cov
90%
_verify_XXX_manifests
14.1
__build_xxxmanifest_entries
5.2
__create_xxmanifest
5.2
CRAP 5.6
« lib/Archive/BagIt/Role/Portability.pm ↑ Summary lib/Archive/BagIt.pm »
1 package Archive::BagIt::Role::Manifest;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 with 'Archive::BagIt::Role::Portability';
9 # ABSTRACT: A role that handles all manifest files for a specific Algorithm
10 12 our $VERSION = '0.103'; # VERSION
11  
12 12 has 'algorithm' => (
13 is => 'rw',
14 builder => '_build_algorithm',
15 #isa=>'HashRef',
16 );
17  
18 211 ▶sub _build_algorithm {
Subroutine _build_algorithm: called CC=1 SCAR=0.0
19 211 my $self = shift;
20 211 $self;
21 }
22  
23 12 has 'manifest_file' => (
24 is => 'rw',
25 lazy => 1,
26 builder => '_build_manifest_file',
27 );
28  
29 81 ▶sub _build_manifest_file {
Subroutine _build_manifest_file: called CC=2 SCAR=6.9
30 81 my $self = shift;
31 81 my $algorithm = $self->algorithm()->name;
32 81 my $file = File::Spec->catfile($self->bagit->metadata_path, "manifest-$algorithm.txt");
33 81 ▶ return $file if (-f $file);
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 67 14 81
34 14 return;
35 }
36  
37  
38 12 has 'tagmanifest_file' => (
39 is => 'rw',
40 lazy => 1,
41 builder => '_build_tagmanifest_file'
42 );
43  
44 54 ▶sub _build_tagmanifest_file {
Subroutine _build_tagmanifest_file: called CC=2 SCAR=6.9
45 54 my $self = shift;
46 54 my $algorithm = $self->algorithm()->name;
47 54 my $file = File::Spec->catfile( $self->bagit->metadata_path, "tagmanifest-$algorithm.txt");
48 54 ▶ return $file if (-f $file);
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 46 8 54
49 8 return;
50 }
51  
52 0 ▶sub BUILD {}
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod undocumented
53  
54 211 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 211 my $self = shift;
56 211 my $algorithm = $self->algorithm->name;
57 211 $self->{bagit}->{manifests}->{$algorithm} = $self;
58 12 };
59  
60  
61 12 has 'manifest_entries' => (
62 is => 'ro',
63 lazy => 1,
64 builder => '_build_manifest_entries',
65 );
66  
67  
68 12 has 'tagmanifest_entries' => (
69 is => 'ro',
70 lazy => 1,
71 builder => '_build_tagmanifest_entries',
72 );
73  
74 113 ▶sub __build_xxxmanifest_entries {
Subroutine __build_xxxmanifest_entries: called CC=5 SCAR=16.5
75 113 my ($self, $xxmanifest_file) = @_;
76 113 my $xxmanifest_entries = {};
77 113 my $algorithm = $self->algorithm()->name;
78 113 ▶ open(my $XXMANIFEST, "<:encoding(UTF-8)", $xxmanifest_file) or croak("Cannot open $xxmanifest_file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $XXMANIFEST, "<:encoding(UTF-8)", $xxmanifest_file or croak("Cannot open ${xxmanifest_file}: $!") 113 0 113
79 113 ▶ while (my $line = <$XXMANIFEST>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $XXMANIFEST)) 353 113 466
80 353 $line = chomp_portable($line);
81 353 my ($digest, $file) = split(/\s+/, $line, 2);
82 353 ▶ next unless ((defined $digest) && (defined $file)); # empty lines!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest and defined $file 0 353 353
Condition33%1/3
defined $digest and defined $file
!ll&&!rl&&r
00353
MC/DC0%0/2
defined $digest and defined $file
defined $digest defined $file
Truth table33%1/3
A and B
A= defined $digestB= defined $file
ABresult
0X0
100
111
83 353 $xxmanifest_entries->{$file} = $digest;
84 }
85 113 close($XXMANIFEST);
86 113 return $xxmanifest_entries;
87 }
88  
89 46 ▶sub _build_tagmanifest_entries {
Subroutine _build_tagmanifest_entries: called CC=2 SCAR=7.6
90 46 my ($self) = @_;
91 46 my $tm_file = $self->tagmanifest_file();
92 46 ▶ return $self->__build_xxxmanifest_entries($tm_file) if (defined $tm_file);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tm_file 46 0 46
93 0 return;
94 }
95  
96 67 ▶sub _build_manifest_entries {
Subroutine _build_manifest_entries: called CC=2 SCAR=7.6
97 67 my ($self) = @_;
98 67 my $m_file = $self->manifest_file();
99 67 ▶ return $self->__build_xxxmanifest_entries($m_file) if (defined $m_file);
Branch: 1 branch
BranchTrueFalseTotal
if defined $m_file 67 0 67
100 0 return;
101 }
102  
103 546 ▶sub _fill_digest_hashref { # should be handle if empty values and ignore it (because parallel map)
Subroutine _fill_digest_hashref: called CC=4 SCAR=14.4
104 546 my ($self, $bagit, $localname) = @_;
105 546 ▶ return if ((!defined $localname) or (0 == length($localname)));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $localname or 0 == length $localname 0 546 546
Condition33%1/3
not defined $localname or 0 == length $localname
l!l&&r!l&&!r
00546
MC/DC0%0/2
not defined $localname or 0 == length $localname
not defined $localname 0 == length $localname
Truth table33%1/3
not(A) or B
A= not defined $localnameB= 0 == length $localname
ABresult
1X1
011
000
106 546 my $digest_hashref;
107 546 my $fullname = File::Spec->catfile($bagit, $localname);
108 546 my $calc_digest = $self->bagit->digest_callback();
109 546 my $eval = &$calc_digest($self->algorithm(), $fullname);
110 546 ▶ $digest_hashref->{calculated_digest} = $eval // '';
Condition50%1/2
$eval // ""
l!l
5460
MC/DC0%0/1
$eval // ""
$eval
Truth table50%1/2
A
A= $eval
Aresult
11
00
111 546 $digest_hashref->{local_name} = $localname;
112 546 $digest_hashref->{full_name} = $fullname;
113 546 return $digest_hashref;
114 }
115  
116  
117  
118  
119 # calc digest
120 # expects expected_ref, array_ref of filenames
121 # returns arrayref of hashes where each entry has
122 # $tmp->{calculated_digest} = $digest;
123 # $tmp->{expected_digest} = $expected_digest;
124 # $tmp->{filename} = $filename;
125 169 ▶sub calc_digests {
Subroutine calc_digests: called CC=1 SCAR=0.0
Pod documented
126 169 my ($self, $bagit, $filenames_ref) = @_;
127 169 my @digest_hashes;
128 my %digest_results;
129 # serial variant
130 169 @digest_hashes = map {$self->_fill_digest_hashref($bagit, $_)} @{$filenames_ref};
131 169 return \@digest_hashes;
132 }
133  
134 113 ▶sub _verify_XXX_manifests {
Subroutine _verify_XXX_manifests: called CC=14 SCAR=26.5
135 113 my ($self, $xxprefix, $xxmanifest_entries, $files_ref, $return_all_errors) = @_;
136 # Read the manifest file
137 113 my @files = @{ $files_ref };
138 113 my @invalid_messages;
139 113 my $bagit = $self->bagit->bag_path;
140 113 my $algorithm = $self->algorithm()->name;
141 33 ▶ my $subref_invalid_report_or_die = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
142 33 my $message = shift;
143 33 ▶ if (defined $return_all_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $return_all_errors) { } 14 19 33
144 14 push @invalid_messages, $message;
145 } else {
146 19 croak($message);
147 }
148 14 return;
149 113 };
150 113 my $local_xxfilename = "${xxprefix}-${algorithm}.txt";
151 113 my %normalised_files;
152 113 foreach my $local_name (@files) {
153 #### Test readability
154 # local_name is relative to bagit base
155 346 my $filepath = File::Spec->catfile($bagit, $local_name);
156 346 ▶ unless (-r $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $filepath) 0 346 346
157 0 &$subref_invalid_report_or_die(
158 "cannot read $local_name (bag-path:$bagit)",
159 );
160 }
161 ### Evaluate each file against the manifest
162 # first check if each file from payload exists in manifest_entries for given alg
163 346 my $normalized_local_name = normalize_payload_filepath($local_name);
164 # local_name is relative to bagit base
165 346 ▶ unless (exists $xxmanifest_entries->{$normalized_local_name}) { # localname as value should exist!
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $xxmanifest_entries->{$normalized_local_name}) 4 342 346
166 4 &$subref_invalid_report_or_die(
167 "file '$local_name' (normalized='$normalized_local_name') found, which is not in '$local_xxfilename' (bag-path:'$bagit')!"
168 #."DEBUG: \n".join("\n", keys %{$xxmanifest_entries->{$algorithm}})
169 );
170 }
171 ### second check if each file from manifest_entries for given alg exists in payload
172 342 $normalised_files{ $normalized_local_name } = 1;
173 }
174  
175 109 foreach my $local_mf_entry_path (keys %{$xxmanifest_entries}) {
176 340 ▶ if ( # to avoid escapes via manifest-files
Branch: 1 branch
BranchTrueFalseTotal
if (check_if_payload_filepath_violates($local_mf_entry_path)) { } 7 333 340
177 check_if_payload_filepath_violates($local_mf_entry_path)
178 ) {
179 7 &$subref_invalid_report_or_die("file '$local_mf_entry_path' not allowed in '$local_xxfilename' (bag-path:'$bagit'")
180 } else {
181 333 ▶ unless (exists $normalised_files{$local_mf_entry_path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $normalised_files{$local_mf_entry_path}) 1 332 333
182 1 &$subref_invalid_report_or_die(
183 "file '$local_mf_entry_path' NOT found, but expected via '$local_xxfilename' (bag-path:'$bagit')!"
184 );
185 }
186 }
187 }
188 # all preconditions full filled, now calc all digests
189 101 my $digest_hashes_ref = $self->calc_digests($bagit, \@files);
190 # compare digests
191 101 ▶ if (defined $digest_hashes_ref && (ref $digest_hashes_ref eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY") 101 0 101
Condition33%1/3
defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY"
!ll&&!rl&&r
00101
MC/DC0%0/2
defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY"
defined $digest_hashes_ref ref $digest_hashes_ref eq "ARRAY"
Truth table33%1/3
A and B
A= defined $digest_hashes_refB= ref $digest_hashes_ref eq "ARRAY"
ABresult
0X0
100
111
192 101 foreach my $digest_entry (@{$digest_hashes_ref}) {
193 309 my $normalized = normalize_payload_filepath($digest_entry->{local_name});
194 309 $digest_entry->{expected_digest} = $xxmanifest_entries->{$normalized};
195 309 ▶ next unless (defined $digest_entry->{expected_digest}); # undef expected digests only occur if all preconditions fullfilled but return_all_errors was set, we should ignore it!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest_entry->{"expected_digest"} 0 309 309
196 309 ▶ if ($digest_entry->{calculated_digest} ne $digest_entry->{expected_digest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digest_entry->{"calculated_digest"} ne $digest_entry->{"expected_digest"}) 21 288 309
197 21 my $xxfilename = File::Spec->catfile($bagit, $local_xxfilename);
198 &$subref_invalid_report_or_die(
199 sprintf("file '%s' (normalized='%s') invalid, digest (%s) calculated=%s, but expected=%s in file '%s'",
200 $digest_entry->{local_name},
201 $normalized,
202 $algorithm,
203 $digest_entry->{calculated_digest},
204 $digest_entry->{expected_digest},
205 21 $xxfilename
206 )
207 );
208 }
209 }
210 }
211  
212 94 ▶ if ($return_all_errors && (scalar @invalid_messages > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_all_errors and scalar @invalid_messages > 0) 12 82 94
Condition100%3/3
$return_all_errors and scalar @invalid_messages > 0
!ll&&!rl&&r
701212
MC/DC100%2/2
$return_all_errors and scalar @invalid_messages > 0
$return_all_errors scalar @invalid_messages > 0
Truth table100%3/3
A and B
A= $return_all_errorsB= scalar @invalid_messages > 0
ABresult
0X0
100
111
213 12 push @{$self->bagit->{errors}},
214 join("\n\t",
215 sort @invalid_messages
216 );
217 12 return;
218 }
219 82 return 1;
220 }
221  
222  
223 81 ▶sub verify_manifest {
Subroutine verify_manifest: called CC=2 SCAR=6.9
Pod documented
224 81 my ($self, $payload_files_ref, $return_all_errors) = @_;
225 81 ▶ if ($self->manifest_file()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->manifest_file) 67 14 81
226 67 return $self->_verify_XXX_manifests(
227 "manifest",
228 $self->manifest_entries(),
229 $payload_files_ref,
230 $return_all_errors
231 );
232 }
233 14 return;
234 }
235  
236  
237 54 ▶sub verify_tagmanifest {
Subroutine verify_tagmanifest: called CC=2 SCAR=6.9
Pod documented
238 54 my ($self, $non_payload_files_ref, $return_all_errors) = @_;
239 54 my @non_payload_files = grep {$_ !~ m#tagmanifest-[0-9a-zA-Z]+\.txt$#} @{ $non_payload_files_ref };
240 54 ▶ if ($self->tagmanifest_file()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tagmanifest_file) 46 8 54
241 46 return $self->_verify_XXX_manifests(
242 "tagmanifest",
243 $self->tagmanifest_entries(),
244 \@non_payload_files,
245 $return_all_errors
246 );
247 }
248 8 return;
249 }
250  
251 68 ▶sub __create_xxmanifest {
Subroutine __create_xxmanifest: called CC=5 SCAR=16.4
252 68 my ($self, $prefix, $files_ref) = @_;
253 68 my $algo = $self->algorithm->name;
254 68 my $bagit = $self->bagit->bag_path;
255 68 my $manifest_file = File::Spec->catfile($self->bagit->metadata_path, "$prefix-${algo}.txt");
256 # Generate digests for all of the files under ./data
257 68 my $digest_hashes_ref = $self->calc_digests($bagit, $files_ref);
258 68 ▶ if (defined $digest_hashes_ref && (ref $digest_hashes_ref eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY") 68 0 68
Condition33%1/3
defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY"
!ll&&!rl&&r
0068
MC/DC0%0/2
defined $digest_hashes_ref and ref $digest_hashes_ref eq "ARRAY"
defined $digest_hashes_ref ref $digest_hashes_ref eq "ARRAY"
Truth table33%1/3
A and B
A= defined $digest_hashes_refB= ref $digest_hashes_ref eq "ARRAY"
ABresult
0X0
100
111
259 68 ▶ open(my $fh, ">:encoding(UTF-8)",$manifest_file) or croak("Cannot create $prefix-${algo}.txt: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $manifest_file or croak("Cannot create $prefix-$algo.txt: $!\n") 68 0 68
260 68 foreach my $digest_entry (@{$digest_hashes_ref}) {
261 222 my $normalized_file = normalize_payload_filepath($digest_entry->{local_name});
262 222 my $digest = $digest_entry->{calculated_digest};
263 222 print($fh "$digest $normalized_file\n");
264 }
265 68 close($fh);
266 }
267 68 return 1;
268 }
269  
270  
271 34 ▶sub create_manifest {
Subroutine create_manifest: called CC=1 SCAR=0.0
Pod documented
272 34 my ($self) = @_;
273 34 $self->__create_xxmanifest('manifest', $self->bagit->payload_files);
274 34 return 1;
275 }
276  
277  
278 34 ▶sub create_tagmanifest {
Subroutine create_tagmanifest: called CC=1 SCAR=0.0
Pod documented
279 34 my ($self) = @_;
280 34 my @non_payload_files = grep {$_ !~ m#^tagmanifest-.*\.txt$#} @{ $self->bagit->non_payload_files };
281 34 $self->__create_xxmanifest('tagmanifest', \@non_payload_files);
282 34 return 1;
283 }
284  
285 12 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 12 1;
287  
288 __END__
« lib/Archive/BagIt/Role/Portability.pm ↑ Summary lib/Archive/BagIt.pm »