summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2012-07-08 12:38:56 +0200
committerDaniel Friesel <derf@finalrewind.org>2012-07-08 12:38:56 +0200
commit7cc60cd4f505634fd1d4cdb052ceb1769be4e3a0 (patch)
tree1c30ae5ba543f009b4593404f97440cb2d28f90a
parenta5a6a29ebbb4a96cd957004cfb1c5c7685eced7d (diff)
Set read_size to zero take read whole files
-rwxr-xr-xbin/hashl3
-rw-r--r--lib/App/Hashl.pm14
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<hashl>
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<App::Hashl> object. Accepted parameters are:
=item B<read_size> => I<bytes>
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