line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Brackup::Util; |
2
|
13
|
|
|
13
|
|
72
|
use strict; |
|
13
|
|
|
|
|
26
|
|
|
13
|
|
|
|
|
837
|
|
3
|
13
|
|
|
13
|
|
68
|
use warnings; |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
562
|
|
4
|
|
|
|
|
|
|
require Exporter; |
5
|
|
|
|
|
|
|
|
6
|
13
|
|
|
13
|
|
135
|
use vars qw(@ISA @EXPORT_OK); |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
1051
|
|
7
|
|
|
|
|
|
|
@ISA = ('Exporter'); |
8
|
|
|
|
|
|
|
@EXPORT_OK = qw(tempfile tempfile_obj tempdir slurp valid_params noclobber_filename io_print_to_fh io_sha1); |
9
|
|
|
|
|
|
|
|
10
|
13
|
|
|
13
|
|
70
|
use File::Path qw(); |
|
13
|
|
|
|
|
32
|
|
|
13
|
|
|
|
|
434
|
|
11
|
13
|
|
|
13
|
|
71
|
use Carp; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
1109
|
|
12
|
13
|
|
|
13
|
|
76
|
use Fcntl qw(O_RDONLY); |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
899
|
|
13
|
13
|
|
|
13
|
|
22247
|
use Digest::SHA1; |
|
13
|
|
|
|
|
13923
|
|
|
13
|
|
|
|
|
1624
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $mainpid = $$; |
16
|
|
|
|
|
|
|
my $_temp_directory; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
END { |
19
|
|
|
|
|
|
|
# will happen after File::Temp's cleanup |
20
|
13
|
100
|
66
|
13
|
|
64992
|
if ($$ == $mainpid and $_temp_directory) { |
21
|
12
|
50
|
|
|
|
1635
|
File::Path::rmtree($_temp_directory, 0, 1) unless $ENV{BRACKUP_TEST_NOCLEANUP}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
13
|
|
|
13
|
|
187643
|
use File::Temp (); |
|
13
|
|
|
|
|
483411
|
|
|
13
|
|
|
|
|
14368
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _get_temp_directory { |
27
|
|
|
|
|
|
|
# Create temporary directory if we need one. By default, all temporary |
28
|
|
|
|
|
|
|
# files will be placed in it. |
29
|
269
|
100
|
|
269
|
|
3338
|
unless (defined($_temp_directory)) { |
30
|
12
|
50
|
|
|
|
140
|
$_temp_directory = File::Temp::tempdir(CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
269
|
|
|
|
|
25122
|
return $_temp_directory; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub tempfile { |
37
|
198
|
|
|
198
|
0
|
68104
|
my (@ret) = File::Temp::tempfile(DIR => _get_temp_directory()); |
38
|
198
|
50
|
|
|
|
132471
|
return wantarray ? @ret : $ret[0]; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub tempfile_obj { |
42
|
41
|
50
|
|
41
|
0
|
465
|
return File::Temp->new(DIR => _get_temp_directory(), CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Utils::tempdir() accepts the same options as File::Temp::tempdir. |
46
|
|
|
|
|
|
|
sub tempdir { |
47
|
30
|
|
|
30
|
0
|
187
|
my %options = @_; |
48
|
30
|
|
33
|
|
|
479
|
$options{DIR} ||= _get_temp_directory(); |
49
|
30
|
|
|
|
|
304
|
return File::Temp::tempdir(%options); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub slurp { |
53
|
101
|
|
|
101
|
0
|
821
|
my $file = shift; |
54
|
101
|
|
|
|
|
463
|
my %opts = @_; |
55
|
101
|
|
|
|
|
214
|
my $fh; |
56
|
101
|
100
|
66
|
|
|
1081
|
if ($opts{decompress} and eval { require IO::Uncompress::AnyUncompress }) { |
|
13
|
|
|
|
|
10729
|
|
57
|
13
|
50
|
|
|
|
434105
|
$fh = IO::Uncompress::AnyUncompress->new($file) |
58
|
|
|
|
|
|
|
or die "Failed to open file $file: $IO::Uncompress::AnyUncompress::AnyUncompressError"; |
59
|
|
|
|
|
|
|
} else { |
60
|
88
|
50
|
|
|
|
10122
|
sysopen($fh, $file, O_RDONLY) or die "Failed to open $file: $!"; |
61
|
|
|
|
|
|
|
} |
62
|
101
|
|
|
|
|
36508
|
return do { local $/; <$fh>; }; |
|
101
|
|
|
|
|
2085
|
|
|
101
|
|
|
|
|
8318
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub valid_params { |
66
|
0
|
|
|
0
|
0
|
0
|
my ($vlist, %uarg) = @_; |
67
|
0
|
|
|
|
|
0
|
my %ret; |
68
|
0
|
|
|
|
|
0
|
$ret{$_} = delete $uarg{$_} foreach @$vlist; |
69
|
0
|
0
|
|
|
|
0
|
croak("Bogus options: " . join(', ', sort keys %uarg)) if %uarg; |
70
|
0
|
|
|
|
|
0
|
return %ret; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Uniquify the given filename to avoid clobbering existing files |
74
|
|
|
|
|
|
|
sub noclobber_filename { |
75
|
0
|
|
|
0
|
0
|
0
|
my ($filename) = @_; |
76
|
0
|
0
|
|
|
|
0
|
return $filename if ! -e $filename; |
77
|
0
|
|
|
|
|
0
|
for (my $i = 1; ; $i++) { |
78
|
0
|
0
|
|
|
|
0
|
return "$filename.$i" if ! -e "$filename.$i"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Prints all data from an IO::Handle to a filehandle |
83
|
|
|
|
|
|
|
sub io_print_to_fh { |
84
|
98
|
|
|
98
|
0
|
635
|
my ($io_handle, $fh, $sha1) = @_; |
85
|
98
|
|
|
|
|
162
|
my $buf; |
86
|
98
|
|
|
|
|
201
|
my $bytes = 0; |
87
|
|
|
|
|
|
|
|
88
|
98
|
|
|
|
|
1018
|
while($io_handle->read($buf, 4096)) { |
89
|
99
|
|
|
|
|
35282
|
print $fh $buf; |
90
|
99
|
|
|
|
|
293
|
$bytes += length $buf; |
91
|
99
|
100
|
|
|
|
2031
|
$sha1->add($buf) if $sha1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
98
|
|
|
|
|
1738
|
return $bytes; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# computes sha1 of data in an IO::Handle |
98
|
|
|
|
|
|
|
sub io_sha1 { |
99
|
116
|
|
|
116
|
0
|
324
|
my ($io_handle) = @_; |
100
|
|
|
|
|
|
|
|
101
|
116
|
|
|
|
|
3233
|
my $sha1 = Digest::SHA1->new; |
102
|
116
|
|
|
|
|
229
|
my $buf; |
103
|
|
|
|
|
|
|
|
104
|
116
|
|
|
|
|
1721
|
while($io_handle->read($buf, 4096)) { |
105
|
117
|
|
|
|
|
19065
|
$sha1->add($buf); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
116
|
|
|
|
|
5781
|
return $sha1->hexdigest; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# vim:sw=4 |