From ab97942d06a3946cff48f4027740a31ceae1ad8a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 10 Aug 2017 01:07:17 +1200 Subject: Use generic Digest module frontend Slightly older in core, enables using MD5 if need be --- checkem | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/checkem b/checkem index 0035cee..96d6508 100755 --- a/checkem +++ b/checkem @@ -1,8 +1,10 @@ #!/usr/bin/env perl # -# checkem: Find groups of duplicate files with libraries that have been in Perl -# Core since version 5.9.3 +# checkem: Find groups of duplicate files with core libraries. +# +# Author: Tom Ryder +# Site: # # Package name @@ -13,18 +15,17 @@ use strict; use warnings; use utf8; -# Get the find and current working directory modules +# Tolerate very old Perls +use 5.006; + +# Import required modules use Carp; use Fcntl ':mode'; use File::Find; -use Digest::SHA; - -# Lowest version number that has all of those core modules; Digest::SHA is the -# newest -use 5.009_003; +use Digest; # Version number to make Perl::Critic happy -our $VERSION = 2.7; +our $VERSION = 2.8; # If no arguments, work with the current working directory if ( !@ARGV ) { @@ -42,10 +43,9 @@ my %STATS = ( size => 7, ); -# Figure out the SHA algorithm to use; defaults to sha256, but can be overriden -# by setting CHECKEM_ALG in the environment to e.g. "sha1", which is slightly -# faster but technically broken in practice since early 2017 -my $alg = $ENV{CHECKEM_ALG} // 'sha256'; +# Figure out the Digest algorithm to use; defaults to SHA-256, but can be +# overriden by setting CHECKEM_ALG in the environment +my $alg = $ENV{CHECKEM_ALG} // 'SHA-256'; # Start a hash of filesizes to file names/stats... my %sizes; @@ -96,7 +96,12 @@ for my $fs ( grep { @{$_} > 1 } values %sizes ) { # Files still the same size and not hard linked, group by digest; # create the digest object if it isn't already defined - ( $dig //= Digest::SHA->new($alg) )->addfile( $f->{name} ); + open my $fh, '<', $f->{name} + or croak 'Failed to open file'; + binmode $fh; + ( $dig //= Digest->new($alg) )->addfile($fh); + close $fh + or croak 'Failed to close file'; push @{ $sums{ $dig->digest() } }, $f; } } -- cgit v1.2.3