line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Csistck::Util; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
322
|
use 5.010; |
|
17
|
|
|
|
|
56
|
|
|
17
|
|
|
|
|
747
|
|
4
|
17
|
|
|
17
|
|
88
|
use strict; |
|
17
|
|
|
|
|
27
|
|
|
17
|
|
|
|
|
516
|
|
5
|
17
|
|
|
17
|
|
95
|
use warnings; |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
626
|
|
6
|
|
|
|
|
|
|
|
7
|
17
|
|
|
17
|
|
89
|
use base 'Exporter'; |
|
17
|
|
|
|
|
36
|
|
|
17
|
|
|
|
|
1513
|
|
8
|
17
|
|
|
17
|
|
17268
|
use FindBin; |
|
17
|
|
|
|
|
32269
|
|
|
17
|
|
|
|
|
747
|
|
9
|
17
|
|
|
17
|
|
18002
|
use File::Copy; |
|
17
|
|
|
|
|
46604
|
|
|
17
|
|
|
|
|
1240
|
|
10
|
17
|
|
|
17
|
|
34967
|
use File::Temp; |
|
17
|
|
|
|
|
3510294
|
|
|
17
|
|
|
|
|
2082
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
13
|
|
|
|
|
|
|
backup_file |
14
|
|
|
|
|
|
|
hash_file |
15
|
|
|
|
|
|
|
hash_string |
16
|
|
|
|
|
|
|
package_manager |
17
|
|
|
|
|
|
|
/; |
18
|
|
|
|
|
|
|
|
19
|
17
|
|
|
17
|
|
158
|
use Csistck::Oper qw/debug info/; |
|
17
|
|
|
|
|
39
|
|
|
17
|
|
|
|
|
1250
|
|
20
|
17
|
|
|
17
|
|
103
|
use Csistck::Config qw/option/; |
|
17
|
|
|
|
|
37
|
|
|
17
|
|
|
|
|
11758
|
|
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') // |
30
|
|
|
|
|
|
|
join '/', $FindBin::Bin, 'backup'; |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
|
|
|
|
die("Backup path does not exist: path=<$dest_base>") |
33
|
|
|
|
|
|
|
if (! -e $dest_base); |
34
|
0
|
0
|
0
|
|
|
|
die("Backup path is not writable: path=<$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
|
0
|
|
|
|
|
copy($file, $dest) or die("Backup failed: $!: file=<$file> dest=<$dest>"); |
49
|
0
|
|
|
|
|
|
info("Backup succeeded: file=<$file> dest=<$dest>"); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Hash file, return hash or die if error |
53
|
|
|
|
|
|
|
sub hash_file { |
54
|
0
|
|
|
0
|
0
|
|
my $file = shift; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
debug("Hashing file: file=<$file>"); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Errors to die on |
59
|
0
|
0
|
|
|
|
|
die("File does not exist: file=<$file>") |
60
|
|
|
|
|
|
|
if (! -e $file); |
61
|
0
|
0
|
|
|
|
|
die("File not readable: file=<$file>") |
62
|
|
|
|
|
|
|
if (! -r $file); |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
open(my $h, $file) or die("Error opening file: $!: file=<$file>"); |
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
|
|
|
|
|
|
|
sub hash_string { |
79
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
1; |