SimpleBackuper/StorageCheck.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 144 CC 18 SCAR 55.9
CC
18
Cov
0%
StorageCheck
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 268.1
1 package App::SimpleBackuper;
2  
3 use strict;
4 use warnings;
5 use App::SimpleBackuper::BackupDB;
6 use App::SimpleBackuper::_format;
7 use App::SimpleBackuper::_BlocksInfo;
8  
9 sub StorageCheck {
10 my($options, $state, $fix) = @_;
11
12 my %fails;
13
14 print "Fetching files listing from storage...\t" if $options->{verbose};
15 my $listing = $state->{storage}->listing();
16 print keys(%$listing)." files done\n" if $options->{verbose};
17
18 if(@{ $state->{db}->{backups} } and (! exists $listing->{db} or ! exists $listing->{'db.key'})) {
19 if($fix) {
20 App::SimpleBackuper::BackupDB($options, $state);
21 } else {
22 push @{ $fails{'Storage lost file'} }, grep {! exists $listing->{ $_ }} qw(db db.key);
23 }
24 }
25 delete @$listing{qw(db db.key)};
26
27
28 my %blocks2delete;
29 for(my $q = 0; $q <= $#{ $state->{db}->{parts} }; $q++) {
30 my $part = $state->{db}->{parts}->unpack($state->{db}->{parts}->[ $q ]);
31 my $name = fmt_hex2base64($part->{hash});
32 if(! exists $listing->{ $name }) {
33 if($fix) {
34 push @{ $blocks2delete{ $part->{block_id} } }, $part->{hash};
35 } else {
36 push @{ $fails{'Storage lost file'} }, $name;
37 }
38 }
39 elsif($part->{size} != $listing->{ $name }) {
40 if($fix) {
41 push @{ $blocks2delete{ $part->{block_id} } }, $part->{hash};
42 } else {
43 push @{ $fails{'Storage corrupted'} }, "$name weights $listing->{ $name } instead of $part->{size}";
44 }
45 }
46 delete $listing->{ $name };
47 }
48 if(%blocks2delete) {
49 print "Preparing to delete ".keys(%blocks2delete)." blocks...\t";
50 my $blocks_info = App::SimpleBackuper::_BlocksInfo($options, $state);
51 print "done\n";
52
53 foreach my $block_id (keys %blocks2delete) {
54 my $block = $state->{db}->{blocks}->find_row({ id => $block_id });
55 my $justRemoveParts;
56 if(! $block) {
57 print "Block # $block_id wasn't found. Maybe it removed earler.";
58 $justRemoveParts = 1;
59 }
60 if(! $blocks_info->{ $block_id }){
61 print "Info of block # $block_id wasn't collected. ";
62 $justRemoveParts = 1;
63 }
64 if($justRemoveParts) {
65 print "Removing parts too:\n";
66 foreach my $part (@{ $blocks2delete{ $block_id } }) {
67 $state->{db}->{parts}->delete({hash => $part});
68 print "\t".fmt_hex2base64($part)."\n";
69 }
70 next;
71 }
72
73 print "Removing block # $block_id\n";
74 try {
75 App::SimpleBackuper::_BlockDelete($options, $state, $block, $blocks_info->{$block_id}->[2]);
76 } catch {
77 die $_ if $_ !~ /No such file/;
78 };
79 }
80 App::SimpleBackuper::BackupDB($options, $state);
81 }
82
83 if(%$listing) {
84 if($fix) {
85 foreach my $name (keys %$listing) {
86 print "Removing unknown extra file $name...\t";
87 $state->{storage}->remove($name);
88 print "done.\n";
89 }
90 } else {
91 $fails{'Storage has unknown extra file'} = [ keys %$listing ];
92 }
93 }
94  
95 if(%fails) {
96 print "Storage data was corrupted:\n";
97 while(my($error, $list) = each %fails) {
98 print "\t$error (".@$list."):\n";
99 print "\t\t$_\n" foreach @$list;
100 }
101 print "Please run `simple-backuper storage-fix` to sync database about storage state.\n";
102 print "But the lost data will remain lost.\n";
103 exit -2;
104 } else {
105 print "Storage checking done.\n" if $options->{verbose};
106 }
107 }
108  
109 1;