line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Csistck::Util; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
273
|
use 5.010; |
|
17
|
|
|
|
|
44
|
|
4
|
17
|
|
|
17
|
|
77
|
use strict; |
|
17
|
|
|
|
|
21
|
|
|
17
|
|
|
|
|
357
|
|
5
|
17
|
|
|
17
|
|
74
|
use warnings; |
|
17
|
|
|
|
|
20
|
|
|
17
|
|
|
|
|
459
|
|
6
|
|
|
|
|
|
|
|
7
|
17
|
|
|
17
|
|
71
|
use base 'Exporter'; |
|
17
|
|
|
|
|
22
|
|
|
17
|
|
|
|
|
1169
|
|
8
|
17
|
|
|
17
|
|
9254
|
use FindBin; |
|
17
|
|
|
|
|
16323
|
|
|
17
|
|
|
|
|
810
|
|
9
|
17
|
|
|
17
|
|
8768
|
use File::Copy; |
|
17
|
|
|
|
|
37720
|
|
|
17
|
|
|
|
|
1019
|
|
10
|
17
|
|
|
17
|
|
12736
|
use File::Temp; |
|
17
|
|
|
|
|
326931
|
|
|
17
|
|
|
|
|
1825
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
13
|
|
|
|
|
|
|
backup_file |
14
|
|
|
|
|
|
|
hash_file |
15
|
|
|
|
|
|
|
hash_string |
16
|
|
|
|
|
|
|
package_manager |
17
|
|
|
|
|
|
|
/; |
18
|
|
|
|
|
|
|
|
19
|
17
|
|
|
17
|
|
144
|
use Csistck::Oper qw/debug info/; |
|
17
|
|
|
|
|
29
|
|
|
17
|
|
|
|
|
933
|
|
20
|
17
|
|
|
17
|
|
92
|
use Csistck::Config qw/option/; |
|
17
|
|
|
|
|
25
|
|
|
17
|
|
|
|
|
7200
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Backup single file |
23
|
|
|
|
|
|
|
sub backup_file { |
24
|
0
|
|
|
0
|
0
|
|
my $file = shift; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
debug("Backing up file: "); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Get absolute backup path |
29
|
0
|
|
0
|
|
|
|
my $dest_base = option('backup_path') // join '/', $FindBin::Bin, 'backup'; |
30
|
0
|
0
|
|
|
|
|
die("Backup path does not exist: path=<$dest_base>") |
31
|
|
|
|
|
|
|
if (! -e $dest_base); |
32
|
0
|
0
|
0
|
|
|
|
die("Backup path is not writable: path=<$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
|
0
|
|
|
|
|
copy($file, $dest) or die("Backup failed: $!: file=<$file> dest=<$dest>"); |
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
|
|
|
0
|
0
|
|
my $file = shift; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
debug("Hashing file: file=<$file>"); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Errors to die on |
56
|
0
|
0
|
|
|
|
|
die("File does not exist: file=<$file>") |
57
|
|
|
|
|
|
|
if (! -e $file); |
58
|
0
|
0
|
|
|
|
|
die("File not readable: file=<$file>") |
59
|
|
|
|
|
|
|
if (! -r $file); |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
open(my $h, $file) or die("Error opening file: $!: file=<$file>"); |
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
|
|
|
0
|
0
|
|
my $string = shift; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $hash = Digest::MD5->new(); |
79
|
0
|
|
|
|
|
|
$hash->add($string); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $hash->hexdigest(); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |