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 9     9   78639 use strict;
  9         24  
  9         303  
3 9     9   65 use warnings;
  9         134  
  9         352  
4 9     9   4398 use namespace::autoclean;
  9         83340  
  9         39  
5 9     9   613 use Carp ();
  9         31  
  9         126  
6 9     9   51 use File::Spec ();
  9         16  
  9         146  
7 9     9   488 use Moo::Role;
  9         13086  
  9         61  
8             # ABSTRACT: A role that handles filepaths for improved portability
9             our $VERSION = '0.095'; # VERSION
10              
11              
12             sub chomp_portable {
13 1310     1310 1 2875 my ($line) = @_;
14 1310         6281 $line =~ s#\x{0d}?\x{0a}?\Z##s; # replace CR|CRNL with empty
15 1310         3271 return $line;
16             }
17              
18              
19             sub normalize_payload_filepath {
20 1295     1295 1 2464 my ($filename) = @_;
21 1295         1903 $filename =~ s#[\\](?![/])#/#g; # normalize Windows Backslashes, but only if they are no escape sequences
22 1295         1496 $filename =~ s#%#%25#g; # normalize percent
23 1295         1457 $filename =~ s#\x{0a}#%0A#g; #normalize NEWLINE
24 1295         1511 $filename =~ s#\x{0d}#%0D#g; #normalize CARRIAGE RETURN
25 1295         1483 $filename =~ s# #%20#g; # space
26 1295         1475 $filename =~ s#"##g; # quotes
27 1295         2463 return $filename;
28             }
29              
30              
31             sub check_if_payload_filepath_violates{
32 398     398 1 636 my ($local_name) = @_;
33             # HINT: there is no guarantuee *not* to escape!
34             return
35 398   100     5748 ($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 9     9   7199 no Moo::Role;
  9         23  
  9         44  
49             1;
50              
51             __END__