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 8     8   58996 use strict;
  8         14  
  8         240  
3 8     8   39 use warnings;
  8         77  
  8         257  
4 8     8   3266 use namespace::autoclean;
  8         64744  
  8         30  
5 8     8   477 use Carp ();
  8         22  
  8         99  
6 8     8   31 use File::Spec ();
  8         13  
  8         105  
7 8     8   514 use Moo::Role;
  8         11536  
  8         44  
8             # ABSTRACT: A role that handles filepaths for improved portability
9             our $VERSION = '0.092'; # VERSION
10              
11              
12             sub chomp_portable {
13 598     598 1 1065 my ($line) = @_;
14 598         2665 $line =~ s#\x{0d}?\x{0a}?\Z##s; # replace CR|CRNL with empty
15 598         1221 return $line;
16             }
17              
18              
19             sub normalize_payload_filepath {
20 1295     1295 1 2062 my ($filename) = @_;
21 1295         1557 $filename =~ s#[\\](?![/])#/#g; # normalize Windows Backslashes, but only if they are no escape sequences
22 1295         1212 $filename =~ s#%#%25#g; # normalize percent
23 1295         1239 $filename =~ s#\x{0a}#%0A#g; #normalize NEWLINE
24 1295         1185 $filename =~ s#\x{0d}#%0D#g; #normalize CARRIAGE RETURN
25 1295         1180 $filename =~ s# #%20#g; # space
26 1295         1162 $filename =~ s#"##g; # quotes
27 1295         2112 return $filename;
28             }
29              
30              
31             sub check_if_payload_filepath_violates{
32 392     392 1 493 my ($local_name) = @_;
33             # HINT: there is no guarantuee *not* to escape!
34             return
35 392   100     4929 ($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 8     8   5579 no Moo::Role;
  8         18  
  8         43  
49             1;
50              
51             __END__