lib/Archive/BagIt.pm

filter: Statementstmt 91.5% 529 / 578 Branchbran 76.4% 159 / 208 Conditioncond 58.8% 30 / 51 MC/DCmcdc 25.8% 8 / 31 Subroutinesub 86.6% 65 / 75 Podpod 90.0% 18 / 20 totaltotal 84.0% 809 / 963 CC 137 SCAR 16.6
CC
137
Cov
86%
_calc_bagsize_by_octets
14.9
verify_bag
13.9
_check_baginfo_keys_generically
11.1
CRAP 5.3
« lib/Archive/BagIt/Role/Manifest.pm ↑ Summary lib/Archive/BagIt/Role/Algorithm.pm »
1 package Archive::BagIt;
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 open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Carp qw( carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 with "Archive::BagIt::Role::Portability";
12  
13 12 our $VERSION = '0.103'; # VERSION
14  
15 # ABSTRACT: The main module to handle bags.
16  
17  
18  
19 84 ▶around 'BUILDARGS' , sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
20 84 my $orig = shift;
21 84 my $class = shift;
22 84 ▶ if (@_ == 1 && !ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 7 77 84
Condition100%3/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
7167
MC/DC100%2/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
23 7 return $class->$orig(bag_path=>$_[0]);
24 } else {
25 77 return $class->$orig(@_);
26 }
27 12 };
28  
29  
30 84 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
31 84 my ($self, $args) = @_;
32 84 ▶ if (!defined $self->use_plugins()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->use_plugins) 83 1 84
33 83 return $self->load_plugins(("Archive::BagIt::Plugin::MD5", "Archive::BagIt::Plugin::SHA512"));
34 }
35 1 return $self->load_plugins($self->use_plugins());
36 }
37  
38 ###############################################
39  
40  
41 12 has 'force_utf8' => (
42 is => 'rw',
43 lazy => 1,
44 predicate => 'has_force_utf8',
45 );
46  
47 ###############################################
48  
49  
50 12 has 'bag_path' => (
51 is => 'rw',
52 );
53  
54 # if the user wants specific plugins!
55 12 has 'use_plugins' => (
56 is => 'rw',
57 lazy => 1,
58 );
59  
60 ###############################################
61  
62 12 has 'bag_path_arr' => (
63 is => 'ro',
64 lazy => 1,
65 builder => '_build_bag_path_arr',
66 );
67  
68 ###############################################
69  
70  
71 12 has 'metadata_path' => (
72 is=> 'ro',
73 lazy => 1,
74 builder => '_build_metadata_path',
75 );
76  
77 67 ▶sub _build_metadata_path {
Subroutine _build_metadata_path: called CC=1 SCAR=0.0
78 67 my ($self) = @_;
79 67 return $self->bag_path;
80 }
81  
82 ###############################################
83  
84 12 has 'metadata_path_arr' => (
85 is =>'ro',
86 lazy => 1,
87 builder => '_build_metadata_path_arr',
88 );
89  
90  
91 ###############################################
92  
93 12 has 'rel_metadata_path' => (
94 is => 'ro',
95 lazy => 1,
96 builder => '_build_rel_metadata_path',
97 );
98  
99 ###############################################
100  
101  
102 12 has 'payload_path' => (
103 is => 'ro',
104 lazy => 1,
105 builder => '_build_payload_path',
106 );
107  
108 57 ▶sub _build_payload_path {
Subroutine _build_payload_path: called CC=1 SCAR=0.0
109 57 my ($self) = @_;
110 57 return File::Spec->catdir($self->bag_path, "data");
111 }
112  
113 ###############################################
114  
115 12 has 'payload_path_arr' => (
116 is => 'ro',
117 lazy => 1,
118 builder => '_build_payload_path_arr',
119 );
120  
121 ###############################################
122  
123 12 has 'rel_payload_path' => (
124 is => 'ro',
125 lazy => 1,
126 builder => '_build_rel_payload_path',
127 );
128  
129 ###############################################
130  
131  
132 12 has 'checksum_algos' => (
133 is => 'ro',
134 lazy => 1,
135 builder => '_build_checksum_algos',
136 );
137  
138 ###############################################
139  
140  
141 12 has 'bag_version' => (
142 is => 'ro',
143 lazy => 1,
144 builder => '_build_bag_version',
145 );
146  
147 ###############################################
148  
149  
150 12 has 'bag_encoding' => (
151 is => 'ro',
152 lazy => 1,
153 builder => '_build_bag_encoding',
154 );
155  
156 ###############################################
157  
158  
159 12 has 'bag_info' => (
160 is => 'rw',
161 lazy => 1,
162 builder => '_build_bag_info',
163 predicate => 'has_bag_info'
164 );
165  
166 ###############################################
167  
168  
169 has 'errors' => (
170 is => 'ro',
171 lazy => 1,
172 0 ▶ builder => sub { my $self = shift; return [];},
Subroutine _build_errors: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 12 );
174  
175 ###############################################
176  
177  
178  
179 has 'warnings' => (
180 is => 'ro',
181 lazy => 1,
182 0 ▶ builder => sub { my $self = shift; return [];},
Subroutine _build_warnings: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 12 );
184  
185 ###############################################
186  
187  
188 has 'digest_callback' => (
189 is => 'ro',
190 lazy => 1,
191 28 ▶ builder => sub {
Subroutine _build_digest_callback: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
192 546 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
193 546 my ($digestobj, $filename) = @_;
194 546 ▶ if (-f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 546 0 546
195 546 ▶ open(my $fh, '<:raw', $filename) or confess("Cannot open '$filename', $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $filename or confess("Cannot open '${filename}', $!") 546 0 546
196 546 binmode($fh);
197 546 my $digest = $digestobj->get_hash_string($fh);
198 546 ▶ close $fh or confess("could not close file '$filename', $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or confess("could not close file '${filename}', $!") 546 0 546
199 546 return $digest;
200 } else {
201 0 croak "file $filename is not a real file!";
202 }
203 28 };
204 28 return $sub;
205 }
206 12 );
207  
208 ###############################################
209  
210  
211 105 ▶sub get_baginfo_values_by_key {
Subroutine get_baginfo_values_by_key: called CC=6 SCAR=18.0
Pod documented
212 105 my ($self, $searchkey) = @_;
213 105 my $info = $self->bag_info();
214 105 my @values;
215 105 ▶ if (defined $searchkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $searchkey) 105 0 105
216 105 my $lc_flag = $self->is_baginfo_key_reserved( $searchkey );
217 105 foreach my $entry (@{ $info }) {
218 344 ▶ return unless defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 344 344
219 344 my ($key, $value) = %{ $entry };
220 344 ▶ if ( __case_aware_compare_for_baginfo( $key, $searchkey, $lc_flag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (__case_aware_compare_for_baginfo($key, $searchkey, $lc_flag)) 61 283 344
221 61 push @values, $value;
222 }
223 }
224 }
225 105 ▶ return @values if (scalar(@values) > 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @values > 0 60 45 105
226 45 return;
227 }
228  
229 ###############################################
230  
231  
232 257 ▶sub is_baginfo_key_reserved_as_uniq {
Subroutine is_baginfo_key_reserved_as_uniq: called CC=1 SCAR=0.0
Pod documented
233 257 my ($self, $searchkey) = @_;
234 # my $rx = qr{Bag-Count|Bag-Group-Identifier|Bag-Size|Bagging-Date|Payload-Oxum};
235 257 return $searchkey =~ m/^(?:Bag(?:-(?:Group-Identifier|Count|Size)|ging-Date)|Payload-Oxum)$/i;
236 }
237  
238 ###############################################
239  
240  
241 363 ▶sub is_baginfo_key_reserved {
Subroutine is_baginfo_key_reserved: called CC=1 SCAR=0.0
Pod documented
242 363 my ($self, $searchkey) = @_;
243 # my $rx = qr/
244 # Bag-Count|
245 # Bag-Group-Identifier|
246 # Bag-Size|
247 # Bagging-Date|
248 # Contact-Email|
249 # Contact-Name|
250 # Contact-Phone|
251 # External-Description|
252 # External-Identifier|
253 # Internal-Sender-Description|
254 # Internal-Sender-Identifier|
255 # Organisation-Adress|
256 # Payload-Oxum|
257 # Source-Organization
258 # /;
259 363 return $searchkey =~ m/^(?:Bag(?:-(?:Group-Identifier|Count|Size)|ging-Date)|Internal-Sender-(?:Description|Identifier)|External-(?:Description|Identifier)|Contact-(?:(?:Phon|Nam)e|Email)|Source-Organization|Organisation-Adress|Payload-Oxum)$/ix;
260 }
261  
262 ###############################################
263  
264 469 ▶sub __case_aware_compare_for_baginfo {
Subroutine __case_aware_compare_for_baginfo: called CC=5 SCAR=16.1
265 469 my ($internal_key, $search_key, $lc_flag) = @_;
266 469 ▶ return (defined $internal_key) && (
Condition91%11/12
defined $internal_key && ($lc_flag && lc $internal_key eq lc $search_key || !$lc_flag && $internal_key eq $search_key)
!ll&&!rl&&r
039277
$lc_flag and lc $internal_key eq lc $search_key or not $lc_flag and $internal_key eq $search_key
l!l&&r!l&&!r
6314392
$lc_flag and lc $internal_key eq lc $search_key
!ll&&!rl&&r
8831863
not $lc_flag and $internal_key eq $search_key
!ll&&!rl&&r
3187414
MC/DC60%3/5
defined $internal_key && ($lc_flag && lc $internal_key eq lc $search_key || !$lc_flag && $internal_key eq $search_key)
defined $internal_key $lc_flag lc $internal_key eq lc $search_key not $lc_flag $internal_key eq $search_key
Truth table50%4/8
A && B and C or not(D) and E
A= defined $internal_keyB= $lc_flagC= lc $internal_key eq lc $search_keyD= not $lc_flagE= $internal_key eq $search_key
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
267 ( $lc_flag && ((lc $internal_key) eq (lc $search_key)) ) # for reserved keys use caseinsensitive search
268 ||
269 ( (!$lc_flag) && ($internal_key eq $search_key) ) # for other keys sensitive search
270 )
271 }
272  
273 ###############################################
274  
275 61 ▶sub _find_baginfo_idx {
Subroutine _find_baginfo_idx: called CC=5 SCAR=16.1
276 61 my ($self, $searchkey) = @_;
277 61 ▶ if (defined $searchkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $searchkey) 61 0 61
278 61 ▶ if (-1 < index($searchkey, ":")) {croak "key should not contain a colon! (searchkey='$searchkey')";}
Branch: 1 branch
BranchTrueFalseTotal
if (index($searchkey, ":") != -1) 1 60 61
279 60 my $info = $self->bag_info();
280 60 my $size = scalar(@{$info});
281 60 my $lc_flag = $self->is_baginfo_key_reserved($searchkey);
282 60 foreach my $idx (reverse 0.. $size-1) { # for multiple entries return the latest addition
283 125 my %entry = %{$info->[$idx]};
284 125 my ($key, $value) = %entry;
285 125 ▶ if (__case_aware_compare_for_baginfo($key, $searchkey, $lc_flag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (__case_aware_compare_for_baginfo($key, $searchkey, $lc_flag)) 16 109 125
286 16 return $idx;
287 }
288 }
289 }
290 44 return;
291 }
292  
293 ###############################################
294  
295 5 ▶sub _collect_errors {
Subroutine _collect_errors: called CC=1 SCAR=0.0
296 5 my ($self, $res) = @_;
297 5 push @{$self->{errors}}, $res;
298 5 return;
299 }
300  
301 ###############################################
302  
303 396 ▶sub _if_error_push {
Subroutine _if_error_push: called CC=2 SCAR=7.6
304 396 my ($self, $res) = @_;
305 396 ▶ if ($res ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res ne "") 0 396 396
306 0 return $self->_collect_errors($res);
307 }
308 396 return 1;
309 }
310  
311 ###############################################
312  
313 47 ▶sub _check_baginfo_keys_generically {
Subroutine _check_baginfo_keys_generically: called CC=9 SCAR=24.0
314 47 my ($self, $info) = @_;
315 47 my %keys;
316 47 my $ret=1;
317 47 foreach my $entry (@{$info}) {
318 198 my ($key, $value) = %{$entry};
319 198 my $res = _check_key($key); # check key
320 198 ▶ $ret &&= $self->_if_error_push($res);
Condition33%1/3
$ret &&= $self->_if_error_push($res)
!ll&&!rl&&r
00198
MC/DC0%0/2
$ret &&= $self->_if_error_push($res)
$ret $self->_if_error_push($res)
Truth table33%1/3
A &&= B
A= $retB= $self->_if_error_push($res)
ABresult
0X0
100
111
321 198 $res = _check_value($value); # check value
322 198 ▶ $ret &&= $self->_if_error_push($res);
Condition33%1/3
$ret &&= $self->_if_error_push($res)
!ll&&!rl&&r
00198
MC/DC0%0/2
$ret &&= $self->_if_error_push($res)
$ret $self->_if_error_push($res)
Truth table33%1/3
A &&= B
A= $retB= $self->_if_error_push($res)
ABresult
0X0
100
111
323 # code to prepare check of uniqueness
324 198 ▶ if ($self->is_baginfo_key_reserved($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_baginfo_key_reserved($key)) { } 140 58 198
325 140 $keys{ lc $key }++;
326 } else {
327 58 $keys{ $key }++
328 }
329 }
330 # check for uniqueness
331 47 foreach my $key (keys %keys) {
332 197 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_baginfo_key_reserved_as_uniq($key) and $keys{$key} > 1) 0 197 197
Condition66%2/3
$self->is_baginfo_key_reserved_as_uniq($key) and $keys{$key} > 1
!ll&&!rl&&r
851120
MC/DC0%0/2
$self->is_baginfo_key_reserved_as_uniq($key) and $keys{$key} > 1
$self->is_baginfo_key_reserved_as_uniq($key) $keys{$key} > 1
Truth table66%2/3
A and B
A= $self->is_baginfo_key_reserved_as_uniq($key)B= $keys{$key} > 1
ABresult
0X0
100
111
333 ($self->is_baginfo_key_reserved_as_uniq($key))
334 and ($keys{$key} > 1)
335 ) {
336 0 ▶ $ret &&=$self->_collect_errors("Baginfo key '$key' exists $keys{$key}, but should be uniq!");
Condition0%0/3
$ret &&= $self->_collect_errors("Baginfo key '${key}' exists $keys{$key}, but should be uniq!")
!ll&&!rl&&r
000
MC/DC0%0/2
$ret &&= $self->_collect_errors("Baginfo key '${key}' exists $keys{$key}, but should be uniq!")
$ret $self->_collect_errors("Baginfo key '${key}' exists $keys{$key}, but should be uniq!")
Truth table0%0/3
A &&= B
A= $retB= $self->_collect_errors("Baginfo key '${key}' exists $keys{$key}, but should be uniq!")
ABresult
0X0
100
111
337 }
338 }
339 47 return $ret;
340 }
341  
342 ###############################################
343  
344 50 ▶sub _verify_baginfo {
Subroutine _verify_baginfo: called CC=8 SCAR=22.6
345 50 my ($self, $info) = @_;
346 50 my $ret = 1;
347  
348 50 ▶ if (!defined $info) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $info) { } 3 47 50
349 3 ▶ if (exists $self->{bag_info_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"bag_info_file"}) 0 3 3
350 0 ▶ $ret &&= $self->_collect_errors("'bag-info.txt' exists, but is not (partially) parseable!");
Condition0%0/3
$ret &&= $self->_collect_errors("'bag-info.txt' exists, but is not (partially) parseable!")
!ll&&!rl&&r
000
MC/DC0%0/2
$ret &&= $self->_collect_errors("'bag-info.txt' exists, but is not (partially) parseable!")
$ret $self->_collect_errors("'bag-info.txt' exists, but is not (partially) parseable!")
Truth table0%0/3
A &&= B
A= $retB= $self->_collect_errors("'bag-info.txt' exists, but is not (partially) parseable!")
ABresult
0X0
100
111
351 }
352 } else {
353 47 ▶ $ret &&= $self->_check_baginfo_keys_generically($info);
Condition33%1/3
$ret &&= $self->_check_baginfo_keys_generically($info)
!ll&&!rl&&r
0047
MC/DC0%0/2
$ret &&= $self->_check_baginfo_keys_generically($info)
$ret $self->_check_baginfo_keys_generically($info)
Truth table33%1/3
A &&= B
A= $retB= $self->_check_baginfo_keys_generically($info)
ABresult
0X0
100
111
354 # check for payload oxum
355 47 my ($loaded_payloadoxum) = $self->get_baginfo_values_by_key('Payload-Oxum');
356 47 ▶ if (!defined $loaded_payloadoxum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $loaded_payloadoxum) { } 3 44 47
357 3 push @{$self->{warnings}}, "Payload-Oxum was expected in bag-info.txt, but not found!"; # payload-oxum is recommended, but optional
358 } else {
359 44 my ($octets, $streamcount) = $self->calc_payload_oxum();
360 44 ▶ if ("$octets.$streamcount" ne $loaded_payloadoxum) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$octets.$streamcount" ne $loaded_payloadoxum) 5 39 44
361 5 ▶ $ret &&= $self->_collect_errors( "Payload-Oxum differs, calculated $octets.$streamcount but $loaded_payloadoxum was expected by bag-info.txt");
Condition33%1/3
$ret &&= $self->_collect_errors("Payload-Oxum differs, calculated $octets.$streamcount but $loaded_payloadoxum was expected by bag-info.txt")
!ll&&!rl&&r
050
MC/DC0%0/2
$ret &&= $self->_collect_errors("Payload-Oxum differs, calculated $octets.$streamcount but $loaded_payloadoxum was expected by bag-info.txt")
$ret $self->_collect_errors("Payload-Oxum differs, calculated $octets.$streamcount but $loaded_payloadoxum was expected by bag-info.txt")
Truth table33%1/3
A &&= B
A= $retB= $self->_collect_errors("Payload-Oxum differs, calculated $octets.$streamcount but $loaded_payloadoxum was expected by bag-info.txt")
ABresult
0X0
100
111
362 }
363 }
364 }
365 50 return $ret;
366 }
367  
368 ###############################################
369  
370 58 ▶sub verify_baginfo {
Subroutine verify_baginfo: called CC=2 SCAR=6.9
Pod documented
371 58 my ($self) = @_;
372 58 my $info = $self->bag_info();
373 58 ▶ if (List::Util::any { /the baginfo file .* could not be parsed correctly/ } @{$self->{'errors'}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::any(sub { /the baginfo file .* could not be parsed correctly/; } , @{$$self{"errors"};})) 8 50 58
374 8 return;
375 }
376 50 return $self->_verify_baginfo($info);
377 }
378  
379 ###############################################
380  
381  
382 3 ▶sub delete_baginfo_by_key {
Subroutine delete_baginfo_by_key: called CC=2 SCAR=6.9
Pod documented
383 3 my ($self, $searchkey) = @_;
384 3 my $idx = $self->_find_baginfo_idx($searchkey);
385 3 ▶ if (defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 2 1 3
386 2 splice @{$self->{bag_info}}, $idx, 1; # delete nth indexed entry
387 }
388 3 return 1;
389 }
390  
391 ###############################################
392  
393  
394 2 ▶sub exists_baginfo_key {
Subroutine exists_baginfo_key: called CC=1 SCAR=0.0
Pod documented
395 2 my ($self, $searchkey) =@_;
396 2 return (defined $self->_find_baginfo_idx($searchkey));
397 }
398  
399 ###############################################
400  
401 56 ▶sub _replace_baginfo_by_first_match {
Subroutine _replace_baginfo_by_first_match: called CC=2 SCAR=6.9
402 56 my ($self, $searchkey, $newvalue) = @_;
403 56 my $idx = $self->_find_baginfo_idx( $searchkey);
404 55 ▶ if (defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 13 42 55
405 13 $self->{bag_info}[$idx] = {$searchkey => $newvalue};
406 13 return $idx;
407 }
408 42 return;
409 }
410  
411 ###############################################
412  
413 397 ▶sub _check_key {
Subroutine _check_key: called CC=5 SCAR=16.1
414 397 my ($key) = @_;
415 397 ▶ if (!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 1 396 397
416 1 return "key should match '[^\\r\\n:]+', but is not defined";
417 }
418 396 ▶ if ($key =~ m/[\r\n]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /[\r\n]/s) 1 395 396
419 1 return "key should match '[^\\r\\n:]+', but contains newlines (key='$key')";
420 }
421 395 ▶ if ($key =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /:/) 2 393 395
422 2 return "key should not contain a colon! (key='$key')";
423 }
424 393 ▶ if ($key =~ m/^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$/) 1 392 393
425 1 return "key should have a length > null (key='')";
426 }
427 392 return "";
428 }
429  
430 ###############################################
431  
432 195 ▶sub _check_key_or_croak {
Subroutine _check_key_or_croak: called CC=2 SCAR=6.9
433 195 my ($key) = @_;
434 195 my $res = _check_key($key);
435 195 ▶ if ($res eq "") { return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "") 194 1 195
436 1 croak $res;
437 }
438  
439 ###############################################
440  
441 264 ▶sub _check_value {
Subroutine _check_value: called CC=3 SCAR=11.0
442 264 my ($value) = @_;
443 264 ▶ if (!defined $value) { return "value should match '[^\\r\\n:]+', but is not defined"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 263 264
444 263 ▶ if ($value =~ m/^$/s) { return "value should have a length > null (value='')"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^$/s) 1 262 263
445 262 return "";
446 }
447  
448 ###############################################
449  
450 64 ▶sub _check_value_or_croak {
Subroutine _check_value_or_croak: called CC=2 SCAR=7.4
451 64 my ($value) = @_;
452 64 my $res = _check_value($value);
453 64 ▶ if ($res eq "") { return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq "") 64 0 64
454 0 croak $res;
455 }
456  
457 ###############################################
458  
459 60 ▶sub append_baginfo_by_key {
Subroutine append_baginfo_by_key: called CC=6 SCAR=18.7
Pod documented
460 60 my ($self, $searchkey, $newvalue) = @_;
461 60 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (_check_key_or_croak($searchkey) and defined $newvalue) 60 0 60
Condition33%1/3
_check_key_or_croak($searchkey) and defined $newvalue
!ll&&!rl&&r
0060
MC/DC0%0/2
_check_key_or_croak($searchkey) and defined $newvalue
_check_key_or_croak($searchkey) defined $newvalue
Truth table33%1/3
A and B
A= _check_key_or_croak($searchkey)B= defined $newvalue
ABresult
0X0
100
111
462 _check_key_or_croak($searchkey)
463 and (defined $newvalue)
464 ) {
465 0 ▶ if (-1 < index($searchkey, ":")) {croak "key should not contain a colon! (searchkey='$searchkey')";}
Branch: 1 branch
BranchTrueFalseTotal
if (index($searchkey, ":") != -1) 0 60 60
466 60 ▶ if ($self->is_baginfo_key_reserved_as_uniq($searchkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_baginfo_key_reserved_as_uniq($searchkey)) 44 16 60
467 44 ▶ if (defined $self->get_baginfo_values_by_key($searchkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_baginfo_values_by_key($searchkey)) 5 39 44
468 # hmm, search key is marked as uniq and still exists
469 5 return;
470 }
471 }
472 55 push @{$self->{bag_info}}, { $searchkey => $newvalue };
473 }
474 55 return 1;
475 }
476  
477 ###############################################
478  
479  
480 71 ▶sub add_or_replace_baginfo_by_key {
Subroutine add_or_replace_baginfo_by_key: called CC=5 SCAR=16.3
Pod documented
481 71 my ($self, $searchkey, $newvalue) = @_;
482 71 ▶ if ( _check_key_or_croak($searchkey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_check_key_or_croak($searchkey)) 70 0 70
483 0 ▶ if (-1 < index($searchkey, ":")) { croak "key should not contain a colon! (searchkey='$searchkey')"; }
Branch: 1 branch
BranchTrueFalseTotal
if (index($searchkey, ":") != -1) 0 70 70
484 70 ▶ if (defined $self->{bag_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"bag_info"}) 53 17 70
485 53 my $idx = $self->_replace_baginfo_by_first_match( $searchkey, $newvalue);
486 53 ▶ if (defined $idx) { return $idx;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 12 41 53
487 }
488 58 $self->append_baginfo_by_key( $searchkey, $newvalue );
489 58 return -1;
490 }
491 }
492  
493 ###############################################
494  
495  
496 12 has 'forced_fixity_algorithm' => (
497 is => 'ro',
498 lazy => 1,
499 builder => '_build_forced_fixity_algorithm',
500 );
501  
502 44 ▶sub has_forced_fixity_algorithm {
Subroutine has_forced_fixity_algorithm: called CC=1 SCAR=0.0
Pod undocumented
503 44 my $self = shift;
504 44 return (defined $self->forced_fixity_algorithm() );
505 } # false if use_plugins used
506  
507 ###############################################
508  
509  
510 12 has 'manifest_files' => (
511 is => 'ro',
512 lazy => 1,
513 builder => '_build_manifest_files',
514 );
515  
516 ###############################################
517  
518  
519 12 has 'tagmanifest_files' => (
520 is => 'ro',
521 lazy => 1,
522 builder => '_build_tagmanifest_files',
523 );
524  
525 ###############################################
526  
527  
528 12 has 'payload_files' => ( # relatively to bagit base
529 is => 'ro',
530 lazy => 1,
531 builder => '_build_payload_files',
532 );
533  
534 ###############################################
535  
536  
537 12 has 'non_payload_files' => (
538 is=>'ro',
539 lazy => 1,
540 builder => '_build_non_payload_files',
541 );
542  
543 ###############################################
544  
545  
546 12 has 'plugins' => (
547 is=>'rw',
548 #isa=>'HashRef',
549 );
550  
551 ###############################################
552  
553  
554  
555 12 has 'manifests' => (
556 is => 'rw',
557 lazy => 1,
558 builder => '_build_manifests'
559 #isa=>'HashRef',
560 );
561  
562 ###############################################
563  
564  
565  
566 12 has 'algos' => (
567 is=>'rw',
568 #isa=>'HashRef',
569 );
570  
571 ###############################################
572  
573 0 ▶sub _build_bag_path_arr {
Subroutine _build_bag_path_arr: not called CC=1 SCAR=6.9
574 0 my ($self) = @_;
575 0 my @split_path = File::Spec->splitdir($self->bag_path);
576 0 return @split_path;
577 }
578  
579 0 ▶sub _build_payload_path_arr {
Subroutine _build_payload_path_arr: not called CC=1 SCAR=6.9
580 0 my ($self) = @_;
581 0 my @split_path = File::Spec->splitdir($self->payload_path);
582 0 return @split_path;
583 }
584  
585 0 ▶sub _build_rel_payload_path {
Subroutine _build_rel_payload_path: not called CC=1 SCAR=6.9
586 0 my ($self) = @_;
587 0 my $rel_path = File::Spec->abs2rel( $self->payload_path, $self->bag_path ) ;
588 0 return $rel_path;
589 }
590  
591 0 ▶sub _build_metadata_path_arr {
Subroutine _build_metadata_path_arr: not called CC=1 SCAR=6.9
592 0 my ($self) = @_;
593 0 my @split_path = File::Spec->splitdir($self->metadata_path);
594 0 return @split_path;
595 }
596  
597 0 ▶sub _build_rel_metadata_path {
Subroutine _build_rel_metadata_path: not called CC=1 SCAR=6.9
598 0 my ($self) = @_;
599 0 my $rel_path = File::Spec->abs2rel( $self->metadata_path, $self->bag_path ) ;
600 0 return $rel_path;
601 }
602  
603 19 ▶sub _build_checksum_algos {
Subroutine _build_checksum_algos: called CC=1 SCAR=0.0
604 19 my($self) = @_;
605 19 my @checksums = keys %{ $self->manifests() };
606 19 return \@checksums;
607 }
608  
609 19 ▶sub _build_manifest_files {
Subroutine _build_manifest_files: called CC=3 SCAR=11.0
610 19 my($self) = @_;
611 19 my @manifest_files;
612 19 foreach my $algo (@{$self->checksum_algos}) {
613 38 my $manifest_file = File::Spec->catfile($self->metadata_path, "manifest-$algo.txt");
614 38 ▶ if (-f $manifest_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $manifest_file) 35 3 38
615 35 push @manifest_files, $manifest_file;
616 }
617 }
618 19 return \@manifest_files;
619 }
620  
621 19 ▶sub _build_tagmanifest_files {
Subroutine _build_tagmanifest_files: called CC=3 SCAR=11.0
622 19 my ($self) = @_;
623 19 my @tagmanifest_files;
624 19 foreach my $algo (@{$self->checksum_algos}) {
625 38 my $tagmanifest_file = File::Spec->catfile($self->metadata_path,"tagmanifest-$algo.txt");
626 38 ▶ if (-f $tagmanifest_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $tagmanifest_file) 35 3 38
627 35 push @tagmanifest_files, $tagmanifest_file;
628 }
629 }
630 19 return \@tagmanifest_files;
631 }
632  
633 360 ▶sub __handle_nonportable_local_entry {
Subroutine __handle_nonportable_local_entry: called CC=3 SCAR=11.0
634 360 my $self = shift;
635 360 my $local_entry = shift;
636 360 my $dir = shift;
637 360 ▶ if ($local_entry !~ m/^[a-zA-Z0-9._-]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local_entry =~ /^[a-zA-Z0-9._-]+$/) 4 356 360
638 4 my $local_entry_utf8 = decode("UTF-8", $local_entry);
639 4 ▶ if ((!$self->has_force_utf8)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_force_utf8) 2 2 4
640 2 my $hexdump = "0x" . unpack('H*', $local_entry);
641 2 $local_entry =~m/[^a-zA-Z0-9._-]/; # to find PREMATCH, needed nextline
642 ## no critic (Variables::ProhibitMatchVars)
643 # the slowdown using prematch is accepatable, because only triggered in failure path
644 2 my $prematch_position = $`;
645 2 carp "possible non portable pathname detected in $dir,\n",
646 "got path (hexdump)='$hexdump'(hex),\n",
647 "decoded path='$local_entry_utf8'\n",
648 " "." "x length($prematch_position)."^"."------- first non portable char\n";
649 }
650 4 $local_entry = $local_entry_utf8;
651 }
652 360 return $local_entry;
653 }
654  
655  
656 # own implementation, because File::Find has problems with UTF8 encoded Paths under MSWin32
657 # finds recursively all files in given directory.
658 # if $excludedir is defined, the content will be excluded
659 90 ▶sub __file_find { ## no critic (CognitiveComplexity::ProhibitExcessCognitiveComplexity)
Subroutine __file_find: called CC=2 SCAR=6.9
660 90 my ($self,$dir, $excludedir) = @_;
661 90 ▶ if (defined $excludedir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $excludedir) 88 2 90
662 88 $excludedir = File::Spec->rel2abs( $excludedir);
663 }
664 90 my @file_paths;
665 my $finder;
666 108 ▶ $finder = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
667 108 my ($current_dir) = @_; #absolute path
668 108 my @todo;
669 my @tmp_file_paths;
670 108 opendir( my $dh, $current_dir);
671 108 my @paths = sort File::Spec->no_upwards ( readdir $dh );
672 108 closedir $dh;
673 108 foreach my $local_entry (@paths) {
674 360 my $path_entry = File::Spec->catdir($current_dir, $self->__handle_nonportable_local_entry($local_entry, $dir));
675 360 ▶ if ((defined $excludedir) && ($path_entry eq $excludedir)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $excludedir and $path_entry eq $excludedir) { } 34 326 360
elsif (-f $path_entry) { } 308 18 326
elsif (-d $path_entry) { } 18 0 18
Condition100%3/3
defined $excludedir and $path_entry eq $excludedir
!ll&&!rl&&r
3329334
MC/DC100%2/2
defined $excludedir and $path_entry eq $excludedir
defined $excludedir $path_entry eq $excludedir
Truth table100%3/3
A and B
A= defined $excludedirB= $path_entry eq $excludedir
ABresult
0X0
100
111
676 # ignore it, because excluded
677 } elsif (-f $path_entry) {
678 308 push @tmp_file_paths, $path_entry;
679 } elsif (-d $path_entry) {
680 18 push @todo, $path_entry;
681 } else {
682 0 croak "not a file nor a dir found '$path_entry'";
683 }
684 }
685 108 push @file_paths, @tmp_file_paths;
686 108 foreach my $subdir (@todo) {
687 18 &$finder($subdir);
688 }
689 90 };
690 90 my $absolute = File::Spec->rel2abs( $dir );
691 90 &$finder($absolute);
692 90 @file_paths = map { File::Spec->abs2rel( $_, $dir)} @file_paths;
693 90 return @file_paths;
694 }
695  
696 54 ▶sub _build_payload_files{
Subroutine _build_payload_files: called CC=3 SCAR=11.2
697 54 my ($self) = @_;
698 54 my $payload_dir = $self->payload_path;
699 54 my $reldir = File::Spec->abs2rel($payload_dir, $self->bag_path());
700 54 $reldir =~ s/^\.$//;
701 my @payload = map {
702 54 ▶ $reldir eq "" ? $_ : File::Spec->catfile($reldir, $_)
Branch: 1 branch
BranchTrueFalseTotal
$reldir eq '' ? : 0 121 121
703 } $self->__file_find($payload_dir, File::Spec->rel2abs($self->metadata_path));
704 54 ▶ return wantarray ? @payload : \@payload;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 54 54
705 }
706  
707  
708 109 ▶sub __build_read_bagit_txt {
Subroutine __build_read_bagit_txt: called CC=4 SCAR=13.9
709 109 my($self) = @_;
710 109 my $bagit = $self->metadata_path;
711 109 my $file = File::Spec->catfile($bagit, "bagit.txt");
712 109 ▶ open(my $BAGIT, "<:encoding(UTF-8)", $file) or croak("Cannot read '$file': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $BAGIT, "<:encoding(UTF-8)", $file or croak("Cannot read '${file}': $!") 108 1 109
713 108 my $version_string = <$BAGIT>;
714 108 my $encoding_string = <$BAGIT>;
715 108 close($BAGIT);
716 108 ▶ if (defined $version_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version_string) 108 0 108
717 108 $version_string =~ s/[\r\n]//;
718 }
719 108 ▶ if (defined $encoding_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding_string) 106 2 108
720 106 $encoding_string =~s/[\r\n]//;
721 }
722 108 return ($version_string, $encoding_string, $file);
723 }
724  
725 57 ▶sub _build_bag_version {
Subroutine _build_bag_version: called CC=3 SCAR=11.0
726 57 my($self) = @_;
727 57 my ($version_string, $encoding_string, $file) = $self->__build_read_bagit_txt();
728 56 ▶ croak "Version line missed in '$file" unless defined $version_string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version_string 0 56 56
729 56 ▶ if ($version_string =~ /^BagIt-Version: ([01]\.[0-9]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version_string =~ /^BagIt-Version: ([01]\.[0-9]+)$/) { } 53 3 56
730 53 return $1;
731 } else {
732 3 $version_string =~ s/\r/<CR>/;
733 3 $version_string =~ s/^\N{U+FEFF}/<BOM>/;
734 3 croak "Version string '$version_string' of '$file' is incorrect";
735 };
736 }
737  
738 52 ▶sub _build_bag_encoding {
Subroutine _build_bag_encoding: called CC=3 SCAR=11.0
739 52 my($self) = @_;
740 52 my ($version_string, $encoding_string, $file) = $self->__build_read_bagit_txt();
741 52 ▶ croak "Encoding line missed in '$file" unless defined $encoding_string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoding_string 1 51 52
742 51 ▶ croak "Encoding '$encoding_string' of '$file' not supported by current Archive::BagIt module!" unless ($encoding_string ne "UTF-8");
Branch: 1 branch
BranchTrueFalseTotal
unless $encoding_string ne "UTF-8" 0 51 51
743 51 return $encoding_string;
744 }
745  
746 1 ▶sub __sort_bag_info {
Subroutine __sort_bag_info: called CC=2 SCAR=6.9
747 my @sorted = sort {
748 1 my %tmpa = %{$a};
749 11 my %tmpb = %{$b};
750 11 my ($ka, $va) = each %tmpa;
751 11 my ($kb, $vb) = each %tmpb;
752 11 my $kres = $ka cmp $kb;
753 11 ▶ if ($kres != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kres != 0) { } 10 1 11
754 10 return $kres;
755 } else {
756 1 return $va cmp $vb;
757 }
758 } @_;
759 1 return @sorted;
760 }
761  
762  
763 932 ▶sub _extract_key_from_textblob {
Subroutine _extract_key_from_textblob: called CC=4 SCAR=13.9
764 932 my ($self, $textblob) = @_;
765 932 ▶ if (!defined $textblob) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $textblob) 1 931 932
766 1 push @{$self->{errors}}, "the baginfo file '" . $self->{bag_info_file} . "' could not be parsed correctly, because textblob for key extraction is undefined";
767 1 return (undef, undef);
768 }
769 931 my $key;
770 931 my $rx_word = qr{[^: \t\r\n]+};# Hint: this word definition for bag-info.txt-keys differs from word definition of bag-info.txt-values!
771 931 my $rx_spc = qr{\s}; #qr{[\t ]};
772 931 ▶ if ($textblob =~ s/\A($rx_word)$rx_spc*:[\t ]*//m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textblob =~ s/\A($rx_word)$rx_spc*:[\t ]*//m) { } 927 4 931
773 # label if starts with chars not colon or whitespace followed by zero or more spaces, a colon, zero or more spaces
774 927 ▶ if ($textblob eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($textblob eq "") 1 926 927
775 1 push @{$self->{errors}}, "the baginfo file '" . $self->{bag_info_file} . "' could not be parsed correctly, because following text blob not fullfill the match requirements for values: '$textblob', empty value detected";
776 1 return ($1, undef);
777 }
778 926 $key = $1;
779 } else {
780 4 push @{$self->{errors}}, "the baginfo file '".$self->{bag_info_file}."' could not be parsed correctly, because following text blob not fullfill the match requirements for keys: '$textblob'";
781 }
782 930 return ($key, $textblob);
783 }
784  
785 926 ▶sub _extract_value_from_textblob {
Subroutine _extract_value_from_textblob: called CC=5 SCAR=16.1
786 926 my ($self, $textblob) = @_;
787 926 ▶ if (!defined $textblob) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $textblob) 1 925 926
788 1 push @{$self->{errors}}, "the baginfo file '" . $self->{bag_info_file} . "' could not be parsed correctly, because textblob for value extraction is undefined";
789 1 return (undef, undef);
790 }
791 925 ▶ if ($textblob eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($textblob eq "") 1 924 925
792 1 push @{$self->{errors}}, "the baginfo file '" . $self->{bag_info_file} . "' could not be parsed correctly, because textblob for value extraction is empty";
793 1 return (undef, "");
794 }
795 924 my $value;
796 # Ex1:
797 # |bar
798 # | baz --> one value: bar\n baz\n tss
799 # | tss
800 # Ex2:
801 # |bar --> one value: bar
802 # |baz
803 # Ex3:
804 # |bar:baz -> one value: bar:baz
805 924 my $rx_word = qr{[^ \t\r\n]+}; # Hint: this word definition for bag-info.txt-values differs from word definition of bag-info.txt-keys!
806 924 my $rx_spc = qr{[\t ]+};
807 924 my $rx_word_spc_word = qr{($rx_word($rx_spc$rx_word)*)};
808 924 ▶ if ($textblob =~ s/\A($rx_word_spc_word([\r\n]$rx_spc$rx_word_spc_word)+)[\r\n]*//ms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($textblob =~ s/\A($rx_word_spc_word([\r\n]$rx_spc$rx_word_spc_word)+)[\r\n]*//ms) { } 6 918 924
elsif ($textblob =~ s/\A($rx_word_spc_word)[\r\n]*//s) { } 913 5 918
809 6 $value = $1;
810 } elsif ($textblob =~ s/\A($rx_word_spc_word)[\r\n]*//s) {
811 913 $value = $1;
812 } else {
813 5 push @{$self->{errors}}, "the baginfo file '".$self->{bag_info_file}."' could not be parsed correctly, because following text blob not fullfill the match requirements for values: '$textblob'";
814 }
815 924 return ($value, $textblob);
816 }
817  
818 # parses a bag-info textblob
819 764 ▶sub _parse_bag_info { ## no critic (CognitiveComplexity::ProhibitExcessCognitiveComplexity)
Subroutine _parse_bag_info: called CC=7 SCAR=19.5
820 764 my ($self, $textblob) = @_;
821 # metadata elements are OPTIONAL and MAY be repeated. Because "bag-
822 # info.txt" is intended for human reading and editing, ordering MAY be
823 # significant and the ordering of metadata elements MUST be preserved.
824 #
825 # A metadata element MUST consist of a label, a colon ":", a single
826 # linear whitespace character (space or tab), and a value that is
827 # terminated with an LF, a CR, or a CRLF.
828 #
829 # The label MUST NOT contain a colon (:), LF, or CR. The label MAY
830 # contain linear whitespace characters but MUST NOT start or end with
831 # whitespace.
832 #
833 # It is RECOMMENDED that lines not exceed 79 characters in length.
834 # Long values MAY be continued onto the next line by inserting a LF,
835 # CR, or CRLF, and then indenting the next line with one or more linear
836 # white space characters (spaces or tabs). Except for linebreaks, such
837 # padding does not form part of the value.
838 #
839 # Implementations wishing to support previous BagIt versions MUST
840 # accept multiple linear whitespace characters before and after the
841 # colon when the bag version is earlier than 1.0; such whitespace does
842 # not form part of the label or value.
843 # find all labels
844 764 my @labels;
845 764 ▶ croak "_parse_baginfo() called with undef value!" unless (defined $textblob);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $textblob 1 763 764
846 763 while (1) {
847 1675 ▶ last if ($textblob eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $textblob eq "" 755 920 1675
848 920 my ($key, $value);
849 920 ($key, $textblob) = $self->_extract_key_from_textblob($textblob);
850 920 ▶ last unless (defined $key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 3 917 920
851 917 ▶ last unless (defined $textblob );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $textblob 0 917 917
852 917 ($value, $textblob) = $self->_extract_value_from_textblob($textblob);
853 917 ▶ last unless (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 912 917
854 912 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 912 0 912
855 912 my $entry = { $key => $value };
856 912 push @labels, $entry;
857 }
858 }
859 # The RFC does not allow reordering:
860 #my @sorted = __sort_bag_info(@labels);
861 #return \@sorted;
862 763 return \@labels;
863 }
864  
865 59 ▶sub _build_bag_info {
Subroutine _build_bag_info: called CC=4 SCAR=13.9
866 59 my ($self) = @_;
867 59 my $bagit = $self->metadata_path;
868 59 my $file = File::Spec->catfile($bagit, "bag-info.txt");
869 59 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 44 15 59
870 44 ▶ open(my $BAGINFO, "<:encoding(UTF-8)", $file) or croak("Cannot read $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $BAGINFO, "<:encoding(UTF-8)", $file or croak("Cannot read ${file}: $!") 44 0 44
871 44 my @lines;
872 44 ▶ while ( my $line = <$BAGINFO>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $BAGINFO)) 202 44 246
873 202 push @lines, $line;
874 }
875 44 close($BAGINFO);
876 44 my $lines = join("", @lines);
877 44 $self->{bag_info_file}=$file;
878 44 return $self->_parse_bag_info($lines);
879 }
880 # bag-info.txt is optional
881 15 return;
882 }
883  
884 33 ▶sub _build_non_payload_files {
Subroutine _build_non_payload_files: called CC=3 SCAR=11.2
885 33 my ($self) = @_;
886 33 my $non_payload_dir = $self->metadata_path();
887 33 my $reldir = File::Spec->abs2rel($non_payload_dir, $self->bag_path());
888 33 $reldir =~ s/^\.$//;
889 my @non_payload = map {
890 33 ▶ $reldir eq "" ? $_ : File::Spec->catfile($reldir, $_)
Branch: 1 branch
BranchTrueFalseTotal
$reldir eq '' ? : 161 0 161
891 } $self->__file_find($non_payload_dir, File::Spec->rel2abs($self->payload_path));
892 33 ▶ return wantarray ? @non_payload : \@non_payload;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 33 33
893 }
894  
895 44 ▶sub _build_forced_fixity_algorithm {
Subroutine _build_forced_fixity_algorithm: called CC=3 SCAR=11.2
896 44 my ($self) = @_;
897 44 ▶ if ($self->use_plugins()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_plugins) { } 0 44 44
898 0 return;
899 } else {
900 44 ▶ if ($self->bag_version() >= 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bag_version >= 1.0) { } 28 16 44
901 28 return Archive::BagIt::Plugin::SHA512->new(bagit => $self);
902 }
903 else {
904 16 return Archive::BagIt::Plugin::MD5->new(bagit => $self);
905 }
906 }
907 }
908  
909 ###############################################
910  
911  
912 84 ▶sub load_plugins {
Subroutine load_plugins: called CC=7 SCAR=20.1
Pod documented
913 84 my ($self, @plugins) = @_;
914 84 my $loaded_plugins = $self->plugins;
915 84 ▶ if (defined $loaded_plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loaded_plugins) 0 84 84
916 0 @plugins = grep {not exists $loaded_plugins->{$_}} @plugins;
917 }
918 84 ▶ return if @plugins == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @plugins == 0 0 84 84
919 84 ▶ if (exists $ENV{TEST_ACTIVE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"TEST_ACTIVE"}) 84 0 84
920 12 ▶ use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
921 84 my $dir = getcwd();
922 84 my @dirs = File::Spec->splitdir($dir);
923 84 ▶ if ($dirs[-1] eq 't') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirs[-1] eq "t") 82 2 84
924 82 push @INC, "../lib";
925 }
926 }
927 84 foreach my $plugin (@plugins) {
928 167 ▶ load_class ($plugin) or croak ("Can't load $plugin");
Branch: 1 branch
BranchTrueFalseTotal
load_class($plugin) or croak("Can't load $plugin") 167 0 167
929 167 $plugin->new({bagit => $self});
930 }
931  
932 84 return 1;
933 }
934  
935 ###############################################
936  
937  
938 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
939 1 my ($self) = @_;
940 # call trigger
941 1 $self->bag_path;
942 1 $self->bag_version;
943 1 $self->bag_encoding;
944 1 $self->bag_info;
945 1 $self->payload_path;
946 1 $self->manifest_files;
947 1 $self->checksum_algos;
948 1 $self->tagmanifest_files;
949 1 return 1;
950 }
951  
952 ###############################################
953  
954  
955 56 ▶sub verify_bag {
Subroutine verify_bag: called CC=13 SCAR=26.3
Pod documented
956 56 my ($self,$opts) = @_;
957 #removed the ability to pass in a bag in the parameters, but might want options
958 #like $return all errors rather than dying on first one
959 56 my $bagit = $self->bag_path;
960 56 my $version = $self->bag_version(); # to call trigger
961 52 my $encoding = $self->bag_encoding(); # to call trigger
962 51 my $baginfo = $self->verify_baginfo(); #to call trigger
963  
964 51 my $fetch_file = File::Spec->catfile($self->metadata_path, "fetch.txt");
965 51 my $payload_dir = $self->payload_path;
966 51 my $return_all_errors = $opts->{return_all_errors};
967  
968 51 ▶ if (-f $fetch_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fetch_file) 7 44 51
969 7 croak("Fetching via file '$fetch_file' is not supported by current Archive::BagIt implementation")
970 }
971 # check forced fixity
972 44 ▶ if ($self->has_forced_fixity_algorithm()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_forced_fixity_algorithm) 44 0 44
973 44 my $forced_fixity_alg = $self->forced_fixity_algorithm()->name();
974 44 my $manifest_file = File::Spec->catfile($self->metadata_path, "manifest-$forced_fixity_alg.txt");
975 44 ▶ croak("Manifest '$manifest_file' is not a regular file or does not exist for given bagit version '$version'") unless -f ($manifest_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $manifest_file 1 43 44
976 }
977 43 ▶ croak("Payload-directory '$payload_dir' is not a directory or does not exist") unless -d ($payload_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $payload_dir 0 43 43
978  
979 43 ▶ unless ($version > .95) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version > "0.95") 0 43 43
980 0 croak ("Bag Version $version is unsupported");
981 }
982  
983 43 my @errors;
984  
985  
986 # check for manifests
987 43 foreach my $algorithm ( keys %{ $self->manifests }) {
988 81 my $res = $self->manifests->{$algorithm}->verify_manifest($self->payload_files, $return_all_errors);
989 0 ▶ if ((defined $res) && ($res ne "1")) { push @errors, $res; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res ne 1) 0 66 66
Condition66%2/3
defined $res and $res ne 1
!ll&&!rl&&r
18480
MC/DC0%0/2
defined $res and $res ne 1
defined $res $res ne 1
Truth table66%2/3
A and B
A= defined $resB= $res ne 1
ABresult
0X0
100
111
990 }
991 #check for tagmanifests
992 28 foreach my $algorithm ( keys %{ $self->manifests }) {
993 54 my $res = $self->manifests->{$algorithm}->verify_tagmanifest($self->non_payload_files, $return_all_errors);
994 0 ▶ if ((defined $res) && ($res ne "1")) { push @errors, $res; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res and $res ne 1) 0 50 50
Condition66%2/3
defined $res and $res ne 1
!ll&&!rl&&r
16340
MC/DC0%0/2
defined $res and $res ne 1
defined $res $res ne 1
Truth table66%2/3
A and B
A= defined $resB= $res ne 1
ABresult
0X0
100
111
995 }
996 24 push @{$self->{errors}}, @errors;
997 24 my $err = $self->errors();
998 24 my @err = @{ $err };
999 24 ▶ if (scalar( @err ) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @err > 0) 8 16 24
1000 8 croak join("\n","bag verify for bagit version '$version' failed with invalid files.", @err);
1001 }
1002 16 return 1;
1003 }
1004  
1005  
1006 61 ▶sub calc_payload_oxum {
Subroutine calc_payload_oxum: called CC=4 SCAR=13.9
Pod documented
1007 61 my($self) = @_;
1008 61 my @payload = @{$self->payload_files};
1009 61 my $octets=0;
1010 61 my $streamcount = scalar @payload;
1011 61 foreach my $local_name (@payload) {# local_name is relative to bagit base
1012 148 my $file = File::Spec->catfile($self->bag_path(), $local_name);
1013 148 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 148 0 148
1014 148 my $filesize = 0;
1015 148 ▶ $filesize = -s $file or carp "empty file $file detected";
Branch: 1 branch
BranchTrueFalseTotal
$filesize = -s $file or carp("empty file $file detected") 147 1 148
1016 148 $octets += $filesize;
1017 0 } else { croak "file $file does not exist, $!"; }
1018 }
1019 61 return ($octets, $streamcount);
1020 }
1021  
1022 17 ▶sub _calc_bagsize_by_octets {
Subroutine _calc_bagsize_by_octets: called CC=5 SCAR=27.0
1023 17 my $octets = $_[0];
1024 17 ▶ if ($octets < 1024) { return "$octets B"; }
Branch: 4 branches
BranchTrueFalseTotal
if ($octets < 1024) { } 17 0 17
elsif ($octets < 1048576) { } 0 0 0
elsif ($octets < 1073741824) { } 0 0 0
elsif ($octets < 1099511627776) { } 0 0 0
1025 0 elsif ($octets < 1024*1024) {return sprintf("%0.1f kB", $octets/1024); }
1026 0 elsif ($octets < 1024*1024*1024) {return sprintf "%0.1f MB", $octets/(1024*1024); }
1027 0 elsif ($octets < 1024*1024*1024*1024) {return sprintf "%0.1f GB", $octets/(1024*1024*1024); }
1028 0 else { return sprintf "%0.2f TB", $octets/(1024*1024*1024*1024); }
1029 }
1030  
1031  
1032 0 ▶sub calc_bagsize {
Subroutine calc_bagsize: not called CC=1 SCAR=6.9
Pod documented
1033 0 my($self) = @_;
1034 0 my ($octets,$streamcount) = $self->calc_payload_oxum();
1035 0 return _calc_bagsize_by_octets( $octets );
1036 }
1037  
1038  
1039 17 ▶sub create_bagit {
Subroutine create_bagit: called CC=2 SCAR=7.0
Pod documented
1040 17 my($self) = @_;
1041 17 my $metadata_path = $self->metadata_path();
1042 17 my $bagit_path = File::Spec->catfile( $metadata_path, "bagit.txt");
1043 17 ▶ open(my $BAGIT, ">:encoding(UTF-8)", $bagit_path) or croak("Can't open $bagit_path for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $BAGIT, ">:encoding(UTF-8)", $bagit_path or croak("Can't open $bagit_path for writing: $!") 17 0 17
1044 17 print($BAGIT "BagIt-Version: 1.0\nTag-File-Character-Encoding: UTF-8");
1045 17 close($BAGIT);
1046 17 return 1;
1047 }
1048  
1049  
1050 17 ▶sub create_baginfo {
Subroutine create_baginfo: called CC=6 SCAR=18.0
Pod documented
1051 17 my($self) = @_; # because bag-info.txt allows multiple key-value-entries, hash is replaced
1052 17 $self->add_or_replace_baginfo_by_key('Bagging-Date', POSIX::strftime("%Y-%m-%d", gmtime(time)));
1053 17 $self->add_or_replace_baginfo_by_key('Bag-Software-Agent', 'Archive::BagIt <https://metacpan.org/pod/Archive::BagIt>');
1054 17 my ($octets, $streams) = $self->calc_payload_oxum();
1055 17 $self->add_or_replace_baginfo_by_key('Payload-Oxum', "$octets.$streams");
1056 17 my $bagsize = _calc_bagsize_by_octets($octets);
1057  
1058 17 $self->add_or_replace_baginfo_by_key('Bag-Size', $bagsize);
1059 # The RFC does not allow reordering:
1060 17 my $metadata_path = $self->metadata_path();
1061 17 my $bag_info_path = File::Spec->catfile( $metadata_path, "bag-info.txt");
1062 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"errors"} and scalar @{$self->{"errors"};} > 0) 1 16 17
Condition66%2/3
exists $self->{"errors"} and scalar @{$self->{"errors"};} > 0
!ll&&!rl&&r
1601
MC/DC50%1/2
exists $self->{"errors"} and scalar @{$self->{"errors"};} > 0
exists $self->{"errors"} scalar @{$self->{"errors"};} > 0
Truth table66%2/3
A and B
A= exists $self->{"errors"}B= scalar @{$self->{"errors"};} > 0
ABresult
0X0
100
111
1063 (exists $self->{errors})
1064 1 and ((scalar @{$self->{errors}}) > 0)
1065 ) {
1066 1 croak "Could not create baginfo, because current file $bag_info_path has parsing errors!";
1067 }
1068 16 ▶ open(my $BAGINFO, ">:encoding(UTF-8)", $bag_info_path) or croak("Can't open $bag_info_path for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $BAGINFO, ">:encoding(UTF-8)", $bag_info_path or croak("Can't open $bag_info_path for writing: $!") 16 0 16
1069 16 foreach my $entry (@{ $self->bag_info() }) {
1070 64 my %tmp = %{ $entry };
1071 64 my ($key, $value) = %tmp;
1072 64 _check_key_or_croak($key);
1073 64 _check_value_or_croak($value);
1074 0 ▶ if (-1 < index($key,":")) { carp "key should not contain a colon! (searchkey='$key')"; }
Branch: 1 branch
BranchTrueFalseTotal
if (index($key, ":") != -1) 0 64 64
1075 64 print($BAGINFO "$key: $value\n");
1076 }
1077 16 close($BAGINFO);
1078 16 return 1;
1079 }
1080  
1081  
1082 17 ▶sub store {
Subroutine store: called CC=3 SCAR=11.0
Pod documented
1083 17 my($self) = @_;
1084 # it is important to create all manifest files first, because tagmanifest should include all manifest-xxx.txt
1085 17 foreach my $algorithm ( keys %{ $self->manifests }) {
1086 34 $self->manifests->{$algorithm}->create_manifest();
1087 }
1088 17 $self->create_bagit();
1089 17 $self->create_baginfo();
1090 16 foreach my $algorithm ( keys %{ $self->manifests }) {
1091 32 $self->manifests->{$algorithm}->create_tagmanifest();
1092 }
1093 # retrigger builds
1094 16 $self->{checksum_algos} = $self->_build_checksum_algos();
1095 16 $self->{tagmanifest_files} = $self->_build_tagmanifest_files();
1096 16 $self->{manifest_files} = $self->_build_manifest_files();
1097 16 return 1;
1098 }
1099  
1100  
1101 17 ▶sub init_metadata {
Subroutine init_metadata: called CC=5 SCAR=16.4
Pod documented
1102 17 my ($class, $bag_path, $options) = @_;
1103 17 $bag_path =~ s#/$##; # replace trailing slash
1104 0 ▶ unless ( -d $bag_path) { croak ( "source bag directory '$bag_path' doesn't exist"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $bag_path) 0 17 17
1105 17 my $self = $class->new(bag_path=>$bag_path, %$options);
1106 17 ▶ carp "no payload path" if ! -d $self->payload_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->payload_path 2 15 17
1107 17 ▶ unless ( -d $self->payload_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->payload_path) 2 15 17
1108 2 rename ($bag_path, $bag_path.".tmp");
1109 2 mkdir ($bag_path);
1110 2 rename ($bag_path.".tmp", $self->payload_path);
1111 }
1112 17 ▶ unless ( -d $self->metadata_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->metadata_path) 0 17 17
1113 #metadata path is not the root path for some reason
1114 0 mkdir ($self->metadata_path);
1115 }
1116 17 $self->store();
1117 16 return $self;
1118 }
1119  
1120  
1121 17 ▶sub make_bag {
Subroutine make_bag: called CC=2 SCAR=7.2
Pod documented
1122 17 my ($class, $bag_path, $options) = @_;
1123 17 my $isa = ref $class;
1124 17 ▶ if ($isa eq "Archive::BagIt") { # not a class, but an object!
Branch: 1 branch
BranchTrueFalseTotal
if ($isa eq "Archive::BagIt") 0 17 17
1125 0 croak "make_bag() only a class subroutine, not useable with objects. Try store() instead!\n";
1126 }
1127 17 my $self = $class->init_metadata($bag_path, $options);
1128 16 return $self;
1129 }
1130  
1131  
1132  
1133  
1134  
1135 12 __PACKAGE__->meta->make_immutable;
1136  
1137 12 1;
1138  
1139 __END__
« lib/Archive/BagIt/Role/Manifest.pm ↑ Summary lib/Archive/BagIt/Role/Algorithm.pm »