Clean up script

This commit is contained in:
Dan Church 2018-01-21 12:30:35 -06:00
parent 801bed9fa9
commit a10996e70f
Signed by: h3xx
GPG key ID: EA2BF379CD2CDBD0

View file

@ -3,7 +3,7 @@
use strict; use strict;
use warnings; use warnings;
our $VERSION = '1.2.2'; our $VERSION = '1.2.3';
=pod =pod
@ -115,7 +115,7 @@ Fixed bug when processing files with \r characters in the name.
=head1 COPYRIGHT =head1 COPYRIGHT
Copyright (C) 2010-2013 Dan Church. Copyright (C) 2010-2018 Dan Church.
License GPLv3+: GNU GPL version 3 or later (L<http://gnu.org/licenses/gpl.html>). License GPLv3+: GNU GPL version 3 or later (L<http://gnu.org/licenses/gpl.html>).
@ -132,7 +132,6 @@ Written by Dan Church S<E<lt>amphetamachine@gmail.comE<gt>>
use File::Find qw/ find /; use File::Find qw/ find /;
require Digest::SHA; require Digest::SHA;
use Getopt::Std qw/ getopts /; use Getopt::Std qw/ getopts /;
require Pod::Text;
sub HELP_MESSAGE { sub HELP_MESSAGE {
# my $fh = shift; # my $fh = shift;
@ -152,6 +151,7 @@ sub HELP_MESSAGE {
#symlinked by default. #symlinked by default.
#EOF #EOF
#; #;
require Pod::Text;
my ($fh, $pod) = (shift, Pod::Text->new); my ($fh, $pod) = (shift, Pod::Text->new);
$pod->parse_from_file($0, $fh); $pod->parse_from_file($0, $fh);
@ -202,8 +202,11 @@ sub findexec {
return; return;
} }
# make sure the file exists and it's not a link # skip non-existent files and links
if (-f $File::Find::name && ! -l $File::Find::name) { unless (-f $File::Find::name && ! -l $File::Find::name) {
return;
}
#my $ctx = Digest::MD5->new; #my $ctx = Digest::MD5->new;
my $ctx = Digest::SHA->new; my $ctx = Digest::SHA->new;
$ctx->addfile($File::Find::name); $ctx->addfile($File::Find::name);
@ -219,16 +222,16 @@ sub findexec {
= ($File::Find::name, lstat $File::Find::name); = ($File::Find::name, lstat $File::Find::name);
# skip zero-length files if wanted (`-z') # skip zero-length files if wanted (`-z')
# truth table:
# -z | non-zero length | return?
# 0 | 0 | 1
# 0 | 1 | 0
# 1 | 0 | 0
# 1 | 1 | 0
return unless $opts{z} or $entry->{size}; return unless $opts{z} or $entry->{size};
# check to see if we've come across a file with the same crc # check to see if we've come across a file with the same checksum
if (exists $files{$digest}) { unless (exists $files{$digest}) {
# the file is unique (as far as we know)
# create a new entry in the hash table
$files{$digest} = $entry;
return;
}
my $curr_entry = $files{$digest}; my $curr_entry = $files{$digest};
# don't waste my time # don't waste my time
@ -242,7 +245,7 @@ sub findexec {
# (addendum: perhaps I should congratulate the user on # (addendum: perhaps I should congratulate the user on
# finding a collision in SHA-1) # finding a collision in SHA-1)
if ($curr_entry->{size} != $entry->{size}) { if ($curr_entry->{size} != $entry->{size}) {
die "ERROR: checksums identical for two non-identical files!!!:\n". die "ERROR: checksums identical for two non-identical files!!!:\n".
"files:\t`$curr_entry->{name}'\n". "files:\t`$curr_entry->{name}'\n".
"\t`$entry->{name}'\n". "\t`$entry->{name}'\n".
"SHA1: ($digest)\n". "SHA1: ($digest)\n".
@ -264,7 +267,6 @@ die "ERROR: checksums identical for two non-identical files!!!:\n".
# must not already be hard-linked # must not already be hard-linked
if ($curr_entry->{dev} == $entry->{dev} && if ($curr_entry->{dev} == $entry->{dev} &&
! $opts{s}) { ! $opts{s}) {
# print "hard-linking $new_file\t=>$old_file\n";
# attempt to unlink the file # attempt to unlink the file
printf STDERR "removing file `%s'\n", printf STDERR "removing file `%s'\n",
$less_linked->{name} if $opts{v}; $less_linked->{name} if $opts{v};
@ -272,11 +274,7 @@ die "ERROR: checksums identical for two non-identical files!!!:\n".
# couldn't do it; try more-linked file # couldn't do it; try more-linked file
printf STDERR <<EOF printf STDERR "Failed to remove file `%s': %s\n(using `%s')\n",
Failed to remove file `%s': %s
(using `%s')
EOF
,
$less_linked->{name}, $less_linked->{name},
$!, $!,
$more_linked->{name} $more_linked->{name}
@ -285,11 +283,7 @@ EOF
# if we can't do this, there's no point # if we can't do this, there's no point
# in continuing # in continuing
unless (unlink $more_linked->{name}) { unless (unlink $more_linked->{name}) {
printf STDERR <<EOF printf STDERR "Failed to remove file `%s' (second failure on match): %s\nSkipping...\n",
Failed to remove file `%s' (second failure on match): %s
Skipping...
EOF
,
$more_linked->{name}, $more_linked->{name},
$! $!
if $opts{v}; if $opts{v};
@ -358,11 +352,6 @@ EOF
# preserve older time stamp # preserve older time stamp
utime $atime, $mtime, $less_linked->{name}; utime $atime, $mtime, $less_linked->{name};
} }
} else {
# the file is unique (as far as we know)
# create a new entry in the hash table
$files{$digest} = $entry;
}
#} elsif (-l $File::Find::name) { #} elsif (-l $File::Find::name) {
# # do something to simplify symlinks # # do something to simplify symlinks
# printf STDERR "FIXME: simplifying symlink `%s'\n", # printf STDERR "FIXME: simplifying symlink `%s'\n",
@ -371,7 +360,6 @@ EOF
# printf STDERR "symlink `%s' points to `%s'\n", # printf STDERR "symlink `%s' points to `%s'\n",
# $File::Find::name, readlink $File::Find::name; # $File::Find::name, readlink $File::Find::name;
}
} }
printf STDERR "freed %d bytes (%0.4G %s)\n", printf STDERR "freed %d bytes (%0.4G %s)\n",