From 7cc60cd4f505634fd1d4cdb052ceb1769be4e3a0 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 8 Jul 2012 12:38:56 +0200 Subject: Set read_size to zero take read whole files --- bin/hashl | 3 +++ lib/App/Hashl.pm | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bin/hashl b/bin/hashl index 1c1f0ca..5d1f522 100755 --- a/bin/hashl +++ b/bin/hashl @@ -495,6 +495,9 @@ Change size of the part of each file which is hashed. By default, B hashes the first 4 MiB. Note that this option only makes sense when using C<< hashl update >> to create a new database. +A size of 0 (zero) makes hashl read whole files, i.e. turning it into sha1sum +with a database. + =item B<-V>|B<--version> Print version information. diff --git a/lib/App/Hashl.pm b/lib/App/Hashl.pm index b7d76f6..ec779a9 100644 --- a/lib/App/Hashl.pm +++ b/lib/App/Hashl.pm @@ -4,7 +4,7 @@ use strict; use warnings; use 5.010; -use Digest::SHA qw(sha1_hex); +use Digest::SHA; use Storable qw(nstore retrieve); our $VERSION = '1.00'; @@ -46,10 +46,15 @@ sub si_size { sub hash_file { my ( $self, $file ) = @_; my $data; + my $digest = Digest::SHA->new(1); # read() fails for empty files if ( ( stat($file) )[7] == 0 ) { - return sha1_hex(); + return $digest->hexdigest; + } + if ($self->{config}->{read_size} == 0) { + $digest->addfile($file); + return $digest->hexdigest; } #<<< perltidy has problems indenting 'or die' with tabs @@ -64,7 +69,8 @@ sub hash_file { or die("Can't close ${file}: $!\n"); #>>> - return sha1_hex($data); + $digest->add($data); + return $digest->hexdigest; } sub hash_in_db { @@ -220,7 +226,7 @@ Returns a new B object. Accepted parameters are: =item B => I How many bytes of a file to consider for the hash. Defaults to 4 MiB (4 * -2**20 bytes). +2**20 bytes). 0 means read the whole file. =back -- cgit v1.2.3