Skip to content
Snippets Groups Projects
Commit d3ed0af3 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- improved fixity calculation, now all checksums build at once to avoid multiple reads

parent b0b9c1cf
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,9 @@ use Time::Progress; ...@@ -28,6 +28,9 @@ use Time::Progress;
use XML::LibXML::XPathContext; use XML::LibXML::XPathContext;
use Getopt::Long; use Getopt::Long;
use constant DEBUG => 0; # no debug use constant DEBUG => 0; # no debug
use Digest::CRC;
use Digest::MD5;
use Digest::SHA;
use Pod::Usage; use Pod::Usage;
use IO::Handle; use IO::Handle;
use Fcntl qw(SEEK_END SEEK_SET); use Fcntl qw(SEEK_END SEEK_SET);
...@@ -249,21 +252,33 @@ sub check_file_seekable($$) { ...@@ -249,21 +252,33 @@ sub check_file_seekable($$) {
} }
# check if referenced file has correct checksum for given algorithm # check if referenced file has correct checksum for given algorithm
sub check_file_fixity($$$) { sub check_file_fixities($$) {
my $fileobj = shift; my $fileobj = shift;
my $result = shift; my $result = shift;
my $fixity_algorithm = shift; my %digest_mapping = ( # maps Rosetta fixity algorithm names to Perl fixity algorithm names
my $digest_mapping = { # maps Rosetta fixity algorithm names to Perl fixity algorithm names 'CRC32' => Digest::CRC->new(type=>"crc32"),
'CRC32' => 'CRC-32', 'MD5' => Digest::MD5->new(),
'MD5' => 'MD5', 'SHA1' => Digest::SHA->new(1),
'SHA1' => 'SHA-1', 'SHA256' => Digest::SHA->new(256),
'SHA256' => 'SHA-256', 'SHA512' => Digest::SHA->new(512)
'SHA512' => 'SHA-512' );
}; my $fh = path($fileobj->{file_mounted})->openr();
my $digest_name = $digest_mapping->{$fixity_algorithm}; binmode($fh);
$result->{fixity}->{$fixity_algorithm} = path($fileobj->{file_mounted})->digest($digest_name); my $buffer;
if ($result->{fixity}->{$fixity_algorithm} ne $fileobj->{fixity}->{$fixity_algorithm}) { while (read($fh, $buffer, 128*1024)) { # 128kB blocks
add_error($fileobj, $result, "has fixity $result->{fixity}->{$fixity_algorithm} for algorithm $fixity_algorithm, but $fileobj->{fixity}->{$fixity_algorithm} was expected"); foreach my $fixity_algorithm (keys %digest_mapping) {
$digest_mapping{$fixity_algorithm}->add($buffer);
}
}
close ($fh);
foreach my $fixity_algorithm (keys %digest_mapping) {
if (defined $fileobj->{fixity}->{$fixity_algorithm} && length( $fileobj->{fixity}->{$fixity_algorithm} ) > 0) {
$result->{fixity}->{$fixity_algorithm} = $digest_mapping{$fixity_algorithm}->hexdigest();
if ($result->{fixity}->{$fixity_algorithm} ne $fileobj->{fixity}->{$fixity_algorithm}) {
add_error($fileobj, $result, "has fixity $result->{fixity}->{$fixity_algorithm} for algorithm $fixity_algorithm, but $fileobj->{fixity}->{$fixity_algorithm} was expected");
die;
}
}
} }
return $result; return $result;
} }
...@@ -342,11 +357,7 @@ if (defined $search_dir && -d "$search_dir") { ...@@ -342,11 +357,7 @@ if (defined $search_dir && -d "$search_dir") {
$stat->{scansize} += $result->{size}; $stat->{scansize} += $result->{size};
$result = check_file_seekable($fileobj, $result); $result = check_file_seekable($fileobj, $result);
if ($result->{seekable}) { if ($result->{seekable}) {
foreach my $fixity_algorithm (@algorithms) { $result = check_file_fixities($fileobj, $result);
if ($fileobj->{fixity}->{$fixity_algorithm}) {
$result = check_file_fixity($fileobj, $result, $fixity_algorithm);
}
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment