File Coverage

lib/App/MtAws/Command/CheckLocalHash.pm
Criterion Covered Total %
statement 65 65 100.0
branch 22 22 100.0
condition 15 15 100.0
subroutine 9 9 100.0
pod 0 1 0.0
total 111 112 99.1


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::Command::CheckLocalHash;
22              
23             our $VERSION = '1.114_2';
24              
25 2     2   1030 use strict;
  2         3  
  2         63  
26 2     2   7 use warnings;
  2         4  
  2         58  
27 2     2   8 use utf8;
  2         4  
  2         11  
28 2     2   37 use Carp;
  2         2  
  2         136  
29 2     2   9 use App::MtAws::Utils;
  2         4  
  2         363  
30 2     2   627 use App::MtAws::TreeHash;
  2         5  
  2         66  
31 2     2   17 use App::MtAws::Exceptions;
  2         2  
  2         168  
32 2     2   11 use App::MtAws::Journal;
  2         4  
  2         955  
33              
34             sub run
35             {
36 13     13 0 19728 my ($options, $j) = @_;
37 13         54 $j->read_journal(should_exist => 1);
38 13         39044 my $files = $j->{journal_h};
39              
40 13         59 my ($error_hash, $error_size, $error_zero, $error_missed, $error_mtime, $no_error, $error_io) = (0,0,0,0,0,0,0);
41 13         51 for my $f (keys %$files) {
42 15         89 my $file=$j->latest($f);
43 15         995 my $absfilename = $j->absfilename($f);
44              
45 15 100       43 if ($options->{'dry-run'}) {
46 1         13 print "Will check file $f\n"
47             } else {
48 14 100       62 if (file_exists($absfilename)) {
49 13         13180 my $size = file_size($absfilename);
50 13 100       11558 unless ($size) {
51 1         12 print "ZERO SIZE $f\n";
52 1         3 ++$error_zero;
53             }
54 13 100 100     107 if (defined($file->{mtime}) && (my $actual_mtime = file_mtime($absfilename)) != $file->{mtime}) {
55 1         990 print "MTIME missmatch $f $file->{mtime} != $actual_mtime\n";
56 1         2 ++$error_mtime;
57             }
58 13 100       9040 if ($size) {
59 12 100       41 if ($size == $file->{size}) {
60              
61 11         22 my $F;
62 11 100       51 unless (open_file($F, $absfilename, mode => '<', binary => 1)) {
63 1         2389 print "CANNOT OPEN file $f: ".get_errno($!)."\n";
64 1         2 ++$error_io;
65 1         5 next;
66             }
67 10         15665 my $th = App::MtAws::TreeHash->new();
68 10         4054 $th->eat_file($F);
69 10 100       16207 close $F or confess;
70 9         91 $th->calc_tree();
71              
72 9         3369 my $treehash = $th->get_final_hash();
73 9 100       3103 if ($treehash eq $file->{treehash}) {
74 8         72 print "OK $f $file->{size} $file->{treehash}\n";
75 8         29 ++$no_error;
76             } else {
77 1         8 print "TREEHASH MISSMATCH $f\n";
78 1         5 ++$error_hash;
79             }
80             } else {
81 1         9 print "SIZE MISSMATCH $f\n";
82 1         3 ++$error_size;
83             }
84             }
85             } else {
86 1         1085 print "MISSED $f\n";
87 1         3 ++$error_missed;
88             }
89             }
90             }
91 12 100       45 unless ($options->{'dry-run'}) {
92 11         80 print "TOTALS:\n$no_error OK\n$error_mtime MODIFICATION TIME MISSMATCHES\n$error_hash TREEHASH MISSMATCH\n$error_size SIZE MISSMATCH\n$error_zero ZERO SIZE\n$error_missed MISSED\n$error_io ERRORS\n";
93 11 100 100     175 die exception(check_local_hash_errors => 'check-local-hash reported errors') if $error_hash || $error_size || $error_zero || $error_missed || $error_io;
      100        
      100        
      100        
94             }
95             }
96              
97             1;
98              
99             __END__