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