Debian Codegraph @

src/verify-file

raw
1 #!/usr/bin/perl -w
2 #
3 # This is a file verification tool for lftp.
4 #
5 # Copyright (c) 2005-2014 by Alexander V. Lukyanov ([email protected])
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 use strict;
22 use Digest::MD5;
23 use String::CRC32;
24
25 my $err=0;
26 file: foreach my $file (@ARGV) {
27 my $verified=0;
28 if(-d $file) {
29 print STDERR "$file: Is a directory\n";
30 $err++;
31 next;
32 }
33 unless(-r $file) {
34 print STDERR "$file: $!\n";
35 $err++;
36 next;
37 }
38 my ($dir,$name);
39 if($file=~m'/') {
40 ($dir,$name)=($file=~m{(.*)/(.+)});
41 $dir='/' if $dir eq '';
42 } else {
43 $name=$file;
44 $dir='.';
45 }
46 my @md5files=(glob("$dir/*md5{,sum}"),glob("$dir/*MD5{,SUM}"));
47 if(@md5files) {
48 open MD5,'-|',qw{fgrep -w --},$name,'/dev/null',@md5files;
49 while(<MD5>) {
50 chomp;
51 my ($md5,$name1)=split;
52 (my $md5_file,$md5)=($md5=~m{(.*):(.*)});
53 $md5_file=~s{^\./}{};
54 next if $name1 ne $name;
55 my $ctx=Digest::MD5->new;
56 open FILE,'<',$file;
57 $ctx->addfile(*FILE);
58 close FILE;
59 if(lc($md5) ne $ctx->hexdigest) {
60 print STDERR "$file: MD5 digest mismatch (md5 file: $md5_file)\n";
61 $err++;
62 $verified++;
63 } else {
64 print "$file: MD5 OK (md5 file: $md5_file)\n";
65 $verified++;
66 }
67 }
68 close MD5;
69 }
70
71 next file if $verified;
72
73 my @sfvfiles=(glob("$dir/*.sfv"),glob("$dir/*.SFV"));
74 if(@sfvfiles) {
75 open SFV,'-|',qw{fgrep -w --},$name,'/dev/null',@sfvfiles;
76 while(<SFV>) {
77 chomp;
78 my ($name1,$crc32)=split;
79 (my $sfv_file,$name1)=($name1=~m{(.*):(.*)});
80 $sfv_file=~s{^\./}{};
81 next if $name1 ne $name;
82 open FILE,'<',$file;
83 my $crc321=crc32(*FILE);
84 close FILE;
85 if($crc321!=hex($crc32)) {
86 print STDERR "$file: CRC32 mismatch (sfv file: $sfv_file)\n";
87 $err++;
88 $verified++;
89 } else {
90 print "$file: CRC32 OK (sfv file: $sfv_file)\n";
91 $verified++;
92 }
93 }
94 close SFV;
95 }
96
97 next file if $verified;
98
99 if($name=~/\.gz$/) {
100 system qw{gzip -tv},$file;
101 my $e=($?>>8);
102 $err++ if $e;
103 next file;
104 }
105 if($name=~/\.bz2$/) {
106 system qw{bzip2 -tv},$file;
107 my $e=($?>>8);
108 $err++ if $e;
109 next file;
110 }
111 if($name=~/\.zip$/) {
112 system qw{unzip -tqq},$file;
113 my $e=($?>>8);
114 $err++ if $e;
115 next file;
116 }
117 if($name=~/\.rpm$/) {
118 system qw{rpm --checksig},$file;
119 my $e=($?>>8);
120 $err++ if $e;
121 next file;
122 }
123 }
124 exit $err?1:0;