File Coverage

blib/lib/Archive/BagIt/Role/Portability.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition 3 3 100.0
subroutine 10 10 100.0
pod 3 3 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Archive::BagIt::Role::Portability;
2 16     16   785491 use strict;
  16         51  
  16         726  
3 16     16   109 use warnings;
  16         45  
  16         917  
4 16     16   7258 use namespace::autoclean;
  16         213018  
  16         130  
5 16     16   1427 use Carp ();
  16         35  
  16         395  
6 16     16   101 use File::Spec ();
  16         34  
  16         365  
7 16     16   1274 use Moo::Role;
  16         36664  
  16         160  
8             # ABSTRACT: A role that handles filepaths for improved portability
9             our $VERSION = '0.101'; # VERSION
10              
11              
12             sub chomp_portable {
13 350     350 1 691 my ($line) = @_;
14 350         2234 $line =~ s#\x{0d}?\x{0a}?\Z##s; # replace CR|CRNL with empty
15 350         868 return $line;
16             }
17              
18              
19             sub normalize_payload_filepath {
20 876     876 1 2283 my ($filename) = @_;
21 876         1814 $filename =~ s#[\\](?![/])#/#g; # normalize Windows Backslashes, but only if they are no escape sequences
22 876         1429 $filename =~ s#%#%25#g; # normalize percent
23 876         1459 $filename =~ s#\x{0a}#%0A#g; #normalize NEWLINE
24 876         1317 $filename =~ s#\x{0d}#%0D#g; #normalize CARRIAGE RETURN
25 876         1344 $filename =~ s# #%20#g; # space
26 876         1351 $filename =~ s#"##g; # quotes
27 876         1844 return $filename;
28             }
29              
30              
31             sub check_if_payload_filepath_violates{
32 365     365 1 659 my ($local_name) = @_;
33             # HINT: there is no guarantuee *not* to escape!
34             return
35 365   100     6373 ($local_name =~ m/^~/) # Unix Home
36             || ($local_name =~ m#\./#) # Unix, parent dir escape
37             || ($local_name =~ m#^[A-Z]:[\\/]#) # Windows Drive
38             || ($local_name =~ m#^/#) # Unix absolute path
39             || ($local_name =~ m#^$#) # Unix Env
40             || ($local_name =~ m#^\\#) # Windows absolute path
41             || ($local_name =~ m#^%[^%]*%#) # Windows ENV
42             || ($local_name =~ m#^\*#) # Artifact of md5sum-Tool, where ' *' is allowed to separate checksum and file in fixity line
43             || ($local_name =~ m#[<>:"?|]#) # Windows reserved chars
44             || ($local_name =~ m#(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])#) # Windows reserved filenames
45             ;
46             }
47              
48 16     16   20133 no Moo::Role;
  16         36  
  16         125  
49             1;
50              
51             __END__