| 1 | package Csistck::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 17 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 13 | backup_file | ||||||||||||||||||||||||||||
| 14 | hash_file | ||||||||||||||||||||||||||||
| 15 | hash_string | ||||||||||||||||||||||||||||
| 16 | package_manager | ||||||||||||||||||||||||||||
| 17 | /; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 17 | ▶ | use Csistck::Oper qw/debug info/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 17 | ▶ | use Csistck::Config qw/option/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Backup single file | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub backup_file { | ||||||||||||||||||||||||||
|
Subroutine
backup_file: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | debug("Backing up file: <file=$file>"); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Get absolute backup path | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my $dest_base = option('backup_path') // | ||||||||||||||||||||||||||
|
Condition0%0/2
option("backup_path") // join("/", $FindBin::Bin, "backup")
MC/DC0%0/1
option("backup_path") // join("/", $FindBin::Bin, "backup")
option("backup_path")
Truth table0%0/2
A
A= option("backup_path")
| |||||||||||||||||||||||||||||
| 30 | join '/', $FindBin::Bin, 'backup'; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | die("Backup path does not exist: path=<$dest_base>") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | if (! -e $dest_base); | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | die("Backup path is not writable: path=<$dest_base>") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $dest_base and not -w $dest_base
MC/DC0%0/2
-e $dest_base and not -w $dest_base
-e $dest_base
not -w $dest_base
Truth table0%0/3
A and not(B)
A= -e $dest_baseB= not -w $dest_base
| |||||||||||||||||||||||||||||
| 35 | if (-e $dest_base and ! -w $dest_base); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Generate temporary file to backup to using mangled path | ||||||||||||||||||||||||||||
| 38 | 0 | my $tmp_template = $file; | |||||||||||||||||||||||||||
| 39 | 0 | $tmp_template =~ s/\W/_/g; | |||||||||||||||||||||||||||
| 40 | 0 | $tmp_template .= "-XXXXXX"; | |||||||||||||||||||||||||||
| 41 | 0 | my $tmp = File::Temp->new( | |||||||||||||||||||||||||||
| 42 | TEMPLATE => $tmp_template, | ||||||||||||||||||||||||||||
| 43 | DIR => $dest_base, | ||||||||||||||||||||||||||||
| 44 | UNLINK => 0 | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | 0 | my $dest = $tmp->filename; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | copy($file, $dest) or die("Backup failed: $!: file=<$file> dest=<$dest>"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | info("Backup succeeded: file=<$file> dest=<$dest>"); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Hash file, return hash or die if error | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub hash_file { | ||||||||||||||||||||||||||
|
Subroutine
hash_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | debug("Hashing file: file=<$file>"); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Errors to die on | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | die("File does not exist: file=<$file>") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | if (! -e $file); | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | die("File not readable: file=<$file>") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | if (! -r $file); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | open(my $h, $file) or die("Error opening file: $!: file=<$file>"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $hash = Digest::MD5->new(); | |||||||||||||||||||||||||||
| 67 | 0 | $hash->addfile($h); | |||||||||||||||||||||||||||
| 68 | 0 | close($h); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $digest = $hash->hexdigest(); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | debug(sprintf "File hash successful: file=<%s> hash=<%s>", $file, $digest); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | return $digest; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Returns md5 hash of input string | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub hash_string { | ||||||||||||||||||||||||||
|
Subroutine
hash_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $hash = Digest::MD5->new(); | |||||||||||||||||||||||||||
| 82 | 0 | $hash->add($string); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | return $hash->hexdigest(); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 17 | 1; | |||||||||||||||||||||||||||