| 1 | package Brackup::Util; | ||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | require Exporter; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use vars qw(@ISA @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | @ISA = ('Exporter'); | |||||||||||||||||||||||||||
| 8 | 13 | @EXPORT_OK = qw(tempfile tempfile_obj tempdir slurp valid_params noclobber_filename io_print_to_fh io_sha1); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use File::Path qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use Fcntl qw(O_RDONLY); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Digest::SHA1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 13 | my $mainpid = $$; | |||||||||||||||||||||||||||
| 16 | 13 | my $_temp_directory; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 19 | # will happen after File::Temp's cleanup | ||||||||||||||||||||||||||||
| 20 | 13 | ▶ | if ($$ == $mainpid and $_temp_directory) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$$ == $mainpid and $_temp_directory
MC/DC50%1/2
$$ == $mainpid and $_temp_directory
$$ == $mainpid
$_temp_directory
Truth table66%2/3
A and B
A= $$ == $mainpidB= $_temp_directory
| |||||||||||||||||||||||||||||
| 21 | 12 | ▶ | File::Path::rmtree($_temp_directory, 0, 1) unless $ENV{BRACKUP_TEST_NOCLEANUP}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 13 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 39 | ▶ | sub _get_temp_directory { | ||||||||||||||||||||||||||
|
Subroutine
_get_temp_directory: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 27 | # Create temporary directory if we need one. By default, all temporary | ||||||||||||||||||||||||||||
| 28 | # files will be placed in it. | ||||||||||||||||||||||||||||
| 29 | 39 | ▶ | unless (defined($_temp_directory)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | $_temp_directory = File::Temp::tempdir(CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 39 | return $_temp_directory; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 15 | ▶ | sub tempfile { | ||||||||||||||||||||||||||
|
Subroutine
tempfile: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 15 | my (@ret) = File::Temp::tempfile(DIR => _get_temp_directory()); | |||||||||||||||||||||||||||
| 38 | 15 | ▶ | return wantarray ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub tempfile_obj { | ||||||||||||||||||||||||||
|
Subroutine
tempfile_obj: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | return File::Temp->new(DIR => _get_temp_directory(), CLEANUP => $ENV{BRACKUP_TEST_NOCLEANUP} ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Utils::tempdir() accepts the same options as File::Temp::tempdir. | ||||||||||||||||||||||||||||
| 46 | 23 | ▶ | sub tempdir { | ||||||||||||||||||||||||||
|
Subroutine
tempdir: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 23 | my %options = @_; | |||||||||||||||||||||||||||
| 48 | 23 | ▶ | $options{DIR} ||= _get_temp_directory(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$options{"DIR"} ||= _get_temp_directory()
MC/DC0%0/2
$options{"DIR"} ||= _get_temp_directory()
$options{"DIR"}
_get_temp_directory()
Truth table33%1/3
A ||= B
A= $options{"DIR"}B= _get_temp_directory()
| |||||||||||||||||||||||||||||
| 49 | 23 | return File::Temp::tempdir(%options); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=5 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 9 | my $file = shift; | |||||||||||||||||||||||||||
| 54 | 9 | my %opts = @_; | |||||||||||||||||||||||||||
| 55 | 9 | my $fh; | |||||||||||||||||||||||||||
| 56 | 9 | ▶ | if ($opts{decompress} and eval { require IO::Uncompress::AnyUncompress }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"decompress"} and eval {
do {
require IO::Uncompress::AnyUncompress
}
}
MC/DC0%0/2
$opts{"decompress"} and eval {
do {
require IO::Uncompress::AnyUncompress
}
}
$opts{"decompress"}
eval {
do {
require IO::Uncompress::AnyUncompress
}
}
Truth table33%1/3
A and B
A= $opts{"decompress"}B= eval {
do {
require IO::Uncompress::AnyUncompress
}
}
| |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | $fh = IO::Uncompress::AnyUncompress->new($file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | or die "Failed to open file $file: $IO::Uncompress::AnyUncompress::AnyUncompressError"; | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sysopen($fh, $file, O_RDONLY) or die "Failed to open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 9 | return do { local $/; <$fh>; }; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub valid_params { | ||||||||||||||||||||||||||
|
Subroutine
valid_params: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($vlist, %uarg) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | my %ret; | |||||||||||||||||||||||||||
| 68 | 0 | $ret{$_} = delete $uarg{$_} foreach @$vlist; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | croak("Bogus options: " . join(', ', sort keys %uarg)) if %uarg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | return %ret; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Uniquify the given filename to avoid clobbering existing files | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub noclobber_filename { | ||||||||||||||||||||||||||
|
Subroutine
noclobber_filename: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 0 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $filename if ! -e $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | for (my $i = 1; ; $i++) { | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | return "$filename.$i" if ! -e "$filename.$i"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Prints all data from an IO::Handle to a filehandle | ||||||||||||||||||||||||||||
| 83 | 63 | ▶ | sub io_print_to_fh { | ||||||||||||||||||||||||||
|
Subroutine
io_print_to_fh: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 63 | my ($io_handle, $fh, $sha1) = @_; | |||||||||||||||||||||||||||
| 85 | 63 | my $buf; | |||||||||||||||||||||||||||
| 86 | 63 | my $bytes = 0; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 63 | ▶ | while($io_handle->read($buf, 4096)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 64 | print $fh $buf; | |||||||||||||||||||||||||||
| 90 | 64 | $bytes += length $buf; | |||||||||||||||||||||||||||
| 91 | 64 | ▶ | $sha1->add($buf) if $sha1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 63 | return $bytes; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # computes sha1 of data in an IO::Handle | ||||||||||||||||||||||||||||
| 98 | 78 | ▶ | sub io_sha1 { | ||||||||||||||||||||||||||
|
Subroutine
io_sha1: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 78 | my ($io_handle) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 78 | my $sha1 = Digest::SHA1->new; | |||||||||||||||||||||||||||
| 102 | 78 | my $buf; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 78 | ▶ | while($io_handle->read($buf, 4096)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 79 | $sha1->add($buf); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 78 | return $sha1->hexdigest; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 13 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # vim:sw=4 | ||||||||||||||||||||||||||||