don't delete if source and target are the same file (inode/filesystem)

This commit is contained in:
Ward Wouts 2002-06-09 20:17:07 +00:00
parent 692ec451fe
commit 23e4c0e01b

View file

@ -44,20 +44,28 @@ sub help {
sub compare($source, $target) {
my ($source_size, $target_size);
my (@source_stat, @target_stat);
print "Comparing... ";
# First compare size. If this differs, we're done quickly.
if ( (-s $source) == (-s $target) ) {
if ( $DEBUG ) { print "$source and $target are the same size\n"; }
$source_digest = &calc_md5($source);
if ( $DEBUG ) { print "source digest $source_digest\n"; }
$target_digest = &calc_md5($target);
if ( $DEBUG ) { print "source digest $target_digest\n"; }
if ( $source_digest eq $target_digest ) {
print "Files are the same. Deleting $source.\n";
unlink($source)
or die "Can't delete $source: $!\n";
@source_stat = stat $source;
@target_stat = stat $target;
if (( $source_stat[1] == $target_stat[1] ) &&
( $source_stat[0] == $target_stat[0] )) {
print "Skipped $source & $target are one and the same!\n"
} else {
print "Skipped mv $source -> $target files differ\n"
if ( $DEBUG ) { print "$source and $target are the same size\n"; }
$source_digest = &calc_md5($source);
if ( $DEBUG ) { print "source digest $source_digest\n"; }
$target_digest = &calc_md5($target);
if ( $DEBUG ) { print "source digest $target_digest\n"; }
if ( $source_digest eq $target_digest ) {
print "Files are the same. Deleting $source.\n";
unlink($source)
or die "Can't delete $source: $!\n";
} else {
print "Skipped mv $source -> $target files differ\n"
}
}
} else {
print "Skipped mv $source -> $target files differ\n";