| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer2::FileUtils; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: File utility helpers |
|
3
|
|
|
|
|
|
|
$Dancer2::FileUtils::VERSION = '2.0.1'; |
|
4
|
186
|
|
|
186
|
|
594179
|
use strict; |
|
|
186
|
|
|
|
|
514
|
|
|
|
186
|
|
|
|
|
7518
|
|
|
5
|
186
|
|
|
186
|
|
1065
|
use warnings; |
|
|
186
|
|
|
|
|
422
|
|
|
|
186
|
|
|
|
|
11146
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
186
|
|
|
186
|
|
2125
|
use File::Basename (); |
|
|
186
|
|
|
|
|
791
|
|
|
|
186
|
|
|
|
|
4157
|
|
|
8
|
186
|
|
|
186
|
|
1162
|
use File::Spec; |
|
|
186
|
|
|
|
|
424
|
|
|
|
186
|
|
|
|
|
5265
|
|
|
9
|
186
|
|
|
186
|
|
1081
|
use Carp; |
|
|
186
|
|
|
|
|
644
|
|
|
|
186
|
|
|
|
|
15066
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
186
|
|
|
186
|
|
1187
|
use Exporter 'import'; |
|
|
186
|
|
|
|
|
379
|
|
|
|
186
|
|
|
|
|
142394
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
|
13
|
|
|
|
|
|
|
dirname open_file path read_file_content read_glob_content |
|
14
|
|
|
|
|
|
|
path_or_empty set_file_mode normalize_path escape_filename |
|
15
|
|
|
|
|
|
|
); |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub path { |
|
19
|
16517
|
|
|
16517
|
1
|
67409
|
my @parts = @_; |
|
20
|
16517
|
|
|
|
|
119770
|
my $path = File::Spec->catfile(@parts); |
|
21
|
|
|
|
|
|
|
|
|
22
|
16517
|
|
|
|
|
48470
|
return normalize_path($path); |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub path_or_empty { |
|
26
|
2
|
|
|
2
|
1
|
5146
|
my @parts = @_; |
|
27
|
2
|
|
|
|
|
9
|
my $path = path(@parts); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# return empty if it doesn't exist |
|
30
|
2
|
100
|
|
|
|
44
|
return -e $path ? $path : ''; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
261
|
|
|
261
|
1
|
447760
|
sub dirname { File::Basename::dirname(@_) } |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub set_file_mode { |
|
36
|
75
|
|
|
75
|
1
|
175
|
my $fh = shift; |
|
37
|
75
|
|
|
|
|
175
|
my $charset = 'utf-8'; |
|
38
|
75
|
|
|
22
|
|
1833
|
binmode $fh, ":encoding($charset)"; |
|
|
22
|
|
|
|
|
18724
|
|
|
|
22
|
|
|
|
|
396
|
|
|
|
22
|
|
|
|
|
146
|
|
|
39
|
75
|
|
|
|
|
37399
|
return $fh; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub open_file { |
|
43
|
55
|
|
|
55
|
1
|
340628
|
my ( $mode, $filename ) = @_; |
|
44
|
|
|
|
|
|
|
|
|
45
|
55
|
100
|
|
|
|
3662
|
open my $fh, $mode, $filename |
|
46
|
|
|
|
|
|
|
or croak "Can't open '$filename' using mode '$mode': $!"; |
|
47
|
|
|
|
|
|
|
|
|
48
|
54
|
|
|
|
|
390
|
return set_file_mode($fh); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub read_file_content { |
|
52
|
24
|
100
|
|
24
|
1
|
8625
|
my $file = shift or return; |
|
53
|
23
|
|
|
|
|
115
|
my $fh = open_file( '<', $file ); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
return wantarray |
|
56
|
23
|
100
|
|
|
|
127
|
? read_glob_content($fh) |
|
57
|
|
|
|
|
|
|
: scalar read_glob_content($fh); |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub read_glob_content { |
|
61
|
24
|
|
|
24
|
1
|
70
|
my $fh = shift; |
|
62
|
|
|
|
|
|
|
|
|
63
|
24
|
|
|
|
|
1558
|
my @content = <$fh>; |
|
64
|
24
|
|
|
|
|
825
|
close $fh; |
|
65
|
|
|
|
|
|
|
|
|
66
|
24
|
100
|
|
|
|
296
|
return wantarray ? @content : join '', @content; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub normalize_path { |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# this is a revised version of what is described in |
|
72
|
|
|
|
|
|
|
# http://www.linuxjournal.com/content/normalizing-path-names-bash |
|
73
|
|
|
|
|
|
|
# by Mitch Frazier |
|
74
|
16526
|
50
|
|
16526
|
0
|
52117
|
my $path = shift or return; |
|
75
|
16526
|
|
|
|
|
39654
|
my $seqregex = qr{ |
|
76
|
|
|
|
|
|
|
[^/]* # anything without a slash |
|
77
|
|
|
|
|
|
|
/\.\.(/|\z) # that is accompanied by two dots as such |
|
78
|
|
|
|
|
|
|
}x; |
|
79
|
|
|
|
|
|
|
|
|
80
|
16526
|
|
|
|
|
34326
|
$path =~ s{/\./}{/}g; |
|
81
|
16526
|
|
|
|
|
77866
|
while ( $path =~ s{$seqregex}{} ) {} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#see https://rt.cpan.org/Public/Bug/Display.html?id=80077 |
|
84
|
16526
|
|
|
|
|
28026
|
$path =~ s{^//}{/}; |
|
85
|
16526
|
|
|
|
|
102977
|
return $path; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub escape_filename { |
|
89
|
51
|
50
|
|
51
|
1
|
4366
|
my $filename = shift or return; |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# based on escaping used in CHI::Driver. Our use-case is one-way, |
|
92
|
|
|
|
|
|
|
# so we allow utf8 chars to be escaped, but NEVER do the inverse |
|
93
|
|
|
|
|
|
|
# operation. |
|
94
|
51
|
|
|
|
|
201
|
$filename =~ s/([^A-Za-z0-9_\=\-\~])/sprintf("+%02x", ord($1))/ge; |
|
|
12
|
|
|
|
|
50
|
|
|
95
|
51
|
|
|
|
|
322
|
return $filename; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |