line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::FileUtils; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: helper providing file utilities |
4
|
|
|
|
|
|
|
$Dancer::FileUtils::VERSION = '1.3514_04'; # TRIAL |
5
|
|
|
|
|
|
|
$Dancer::FileUtils::VERSION = '1.351404'; |
6
|
196
|
|
|
196
|
|
112252
|
use strict; |
|
196
|
|
|
|
|
348
|
|
|
196
|
|
|
|
|
4716
|
|
7
|
196
|
|
|
196
|
|
907
|
use warnings; |
|
196
|
|
|
|
|
319
|
|
|
196
|
|
|
|
|
4118
|
|
8
|
|
|
|
|
|
|
|
9
|
196
|
|
|
196
|
|
76593
|
use IO::File; |
|
196
|
|
|
|
|
1291096
|
|
|
196
|
|
|
|
|
18016
|
|
10
|
|
|
|
|
|
|
|
11
|
196
|
|
|
196
|
|
1355
|
use File::Basename (); |
|
196
|
|
|
|
|
403
|
|
|
196
|
|
|
|
|
3013
|
|
12
|
196
|
|
|
196
|
|
839
|
use File::Spec; |
|
196
|
|
|
|
|
362
|
|
|
196
|
|
|
|
|
4470
|
|
13
|
196
|
|
|
196
|
|
118885
|
use File::Temp qw(tempfile); |
|
196
|
|
|
|
|
1796162
|
|
|
196
|
|
|
|
|
10537
|
|
14
|
|
|
|
|
|
|
|
15
|
196
|
|
|
196
|
|
1314
|
use Carp; |
|
196
|
|
|
|
|
368
|
|
|
196
|
|
|
|
|
8184
|
|
16
|
196
|
|
|
196
|
|
1037
|
use Cwd 'realpath'; |
|
196
|
|
|
|
|
368
|
|
|
196
|
|
|
|
|
6699
|
|
17
|
|
|
|
|
|
|
|
18
|
196
|
|
|
196
|
|
2441
|
use Dancer::Exception qw(:all); |
|
196
|
|
|
|
|
363
|
|
|
196
|
|
|
|
|
20224
|
|
19
|
|
|
|
|
|
|
|
20
|
196
|
|
|
196
|
|
1152
|
use base 'Exporter'; |
|
196
|
|
|
|
|
417
|
|
|
196
|
|
|
|
|
17230
|
|
21
|
196
|
|
|
196
|
|
1109
|
use vars '@EXPORT_OK'; |
|
196
|
|
|
|
|
404
|
|
|
196
|
|
|
|
|
135963
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
24
|
|
|
|
|
|
|
dirname open_file path read_file_content read_glob_content |
25
|
|
|
|
|
|
|
path_or_empty set_file_mode normalize_path |
26
|
|
|
|
|
|
|
atomic_write |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# path should not verify paths |
30
|
|
|
|
|
|
|
# just normalize |
31
|
|
|
|
|
|
|
sub path { |
32
|
2537
|
|
|
2537
|
1
|
8044
|
my @parts = @_; |
33
|
2537
|
|
|
|
|
17582
|
my $path = File::Spec->catfile(@parts); |
34
|
|
|
|
|
|
|
|
35
|
2537
|
|
|
|
|
6275
|
return normalize_path($path); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub path_or_empty { |
39
|
1
|
|
|
1
|
0
|
369
|
my @parts = @_; |
40
|
1
|
|
|
|
|
3
|
my $path = path(@parts); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# return empty if it doesn't exist |
43
|
1
|
50
|
|
|
|
13
|
return -e $path ? $path : ''; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
12
|
|
|
12
|
1
|
514
|
sub dirname { File::Basename::dirname(@_) } |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub set_file_mode { |
49
|
143
|
|
|
143
|
1
|
423
|
my $fh = shift; |
50
|
|
|
|
|
|
|
|
51
|
143
|
|
|
|
|
2280
|
require Dancer::Config; |
52
|
143
|
|
100
|
|
|
572
|
my $charset = Dancer::Config::setting('charset') || 'utf-8'; |
53
|
143
|
|
|
2
|
|
2271
|
binmode $fh, ":encoding($charset)"; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
18
|
|
54
|
|
|
|
|
|
|
|
55
|
143
|
|
|
|
|
16122
|
return $fh; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub open_file { |
59
|
140
|
|
|
140
|
1
|
381
|
my ( $mode, $filename ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
140
|
50
|
|
|
|
7802
|
open my $fh, $mode, $filename |
62
|
|
|
|
|
|
|
or raise core_fileutils => "$! while opening '$filename' using mode '$mode'"; |
63
|
|
|
|
|
|
|
|
64
|
140
|
|
|
|
|
734
|
return set_file_mode($fh); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub read_file_content { |
68
|
60
|
50
|
|
60
|
1
|
2304665
|
my $file = shift or return; |
69
|
60
|
|
|
|
|
254
|
my $fh = open_file( '<', $file ); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
return wantarray ? |
72
|
60
|
100
|
|
|
|
225
|
read_glob_content($fh) : |
73
|
|
|
|
|
|
|
scalar read_glob_content($fh); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub read_glob_content { |
77
|
64
|
|
|
64
|
1
|
3309
|
my $fh = shift; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# we don't want to do that as we'll encode the stuff later |
80
|
|
|
|
|
|
|
# binmode $fh; |
81
|
|
|
|
|
|
|
|
82
|
64
|
|
|
|
|
2127
|
my @content = <$fh>; |
83
|
64
|
|
|
|
|
1900
|
close $fh; |
84
|
|
|
|
|
|
|
|
85
|
64
|
100
|
|
|
|
651
|
return wantarray ? @content : join '', @content; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub normalize_path { |
89
|
|
|
|
|
|
|
# this is a revised version of what is described in |
90
|
|
|
|
|
|
|
# http://www.linuxjournal.com/content/normalizing-path-names-bash |
91
|
|
|
|
|
|
|
# by Mitch Frazier |
92
|
2547
|
50
|
|
2547
|
0
|
9796
|
my $path = shift or return; |
93
|
2547
|
|
|
|
|
6770
|
my $seqregex = qr{ |
94
|
|
|
|
|
|
|
[^/]* # anything without a slash |
95
|
|
|
|
|
|
|
/\.\./ # that is accompanied by two dots as such |
96
|
|
|
|
|
|
|
}x; |
97
|
|
|
|
|
|
|
|
98
|
2547
|
|
|
|
|
5317
|
$path =~ s{/\./}{/}g; |
99
|
2547
|
|
|
|
|
9296
|
while ( $path =~ s{$seqregex}{} ) {} |
100
|
|
|
|
|
|
|
|
101
|
2547
|
|
|
|
|
2511240
|
return $path; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# !! currently unused |
105
|
|
|
|
|
|
|
# Undo UNC special-casing catfile-voodoo on cygwin |
106
|
|
|
|
|
|
|
sub _trim_UNC { |
107
|
0
|
|
|
0
|
|
0
|
my @args = @_; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# if we're using cygwin |
110
|
0
|
0
|
|
|
|
0
|
if ( $^O eq 'cygwin' ) { |
111
|
|
|
|
|
|
|
# no @args, no problem |
112
|
0
|
0
|
|
|
|
0
|
@args or return; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
my ( $slashes, $part, @parts) = ( 0, undef, @args ); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# start pulling part from @parts |
117
|
0
|
|
|
|
|
0
|
while ( defined ( $part = shift @parts ) ) { |
118
|
0
|
0
|
|
|
|
0
|
last if $part; |
119
|
0
|
|
|
|
|
0
|
$slashes++; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# count slashes in $part |
123
|
0
|
|
|
|
|
0
|
$slashes += ( $part =~ s/^[\/\\]+// ); |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
0
|
if ( $slashes == 2 ) { |
126
|
0
|
|
|
|
|
0
|
return ( '/' . $part, @parts ); |
127
|
|
|
|
|
|
|
} else { |
128
|
0
|
|
|
|
|
0
|
my $slashstr = ''; |
129
|
0
|
|
|
|
|
0
|
$slashstr .= '/' for ( 1 .. $slashes ); |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
return ( $slashstr . $part, @parts ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
0
|
return @args; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub atomic_write { |
139
|
4
|
|
|
4
|
0
|
13702
|
my ($path, $file, $data) = @_; |
140
|
4
|
|
|
|
|
13
|
my ($fh, $filename) = tempfile("tmpXXXXXXXXX", DIR => $path); |
141
|
3
|
|
|
|
|
843
|
set_file_mode($fh); |
142
|
3
|
|
|
|
|
15
|
print $fh $data; |
143
|
3
|
50
|
|
|
|
163
|
close $fh or die "Can't close '$file': $!\n"; |
144
|
3
|
50
|
|
|
|
255
|
rename($filename, $file) or die "Can't move '$filename' to '$file'"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |