Add garbage collection at the end of each run.
[dupemerge] / dm6
diff --git a/dm6 b/dm6
index a5d56a6545a59258d2742e7dcfcedd0a4be5d843..874534fac260f177fd174250296d0a872fb16aa3 100755 (executable)
--- a/dm6
+++ b/dm6
@@ -69,10 +69,13 @@ sub link_files {
 my $link_dir = shift @ARGV;
 (-d $link_dir) or usage;
 
+my $prefix_length = 3;
+
 sub slash_prefix {
        my ($file) = @_;
-       my $prefix = substr($file, 0, 3);
-       my $suffix = substr($file, 3);
+       $file .= '_' x (length($file) + 1 - $prefix_length) if length($file) + 1 < $prefix_length;
+       my $prefix = substr($file, 0, $prefix_length);
+       my $suffix = substr($file, $prefix_length);
        $prefix =~ s:(.):$1/:osg;
        chop($prefix);
        return ($prefix, $suffix);
@@ -153,6 +156,10 @@ while (<STDIN>) {
                                        undef $digest_st;
                                }
                        }
+                       print STDERR "\b";
+
+                       # Which file are we keeping?
+                       my $keep_file;
 
                        # If digest link exists, link it to file
                        if ($digest_st) {
@@ -162,20 +169,32 @@ while (<STDIN>) {
                                # Old, replace input with old file
                                print STDERR '-';
                                link_files($digest_link, $file);
+                               $keep_file = $digest_link;
                        } else {
                                # New, add input to digest
                                print STDERR '+';
                                link_files($file, $digest_link);
+                               $keep_file = $file;
                        }
 
                        # A link to the inode indicates we are done, so do it last
-                       link_files($file, $inode_link);
+                       print STDERR '_';
+                       link_files($keep_file, $inode_link);
 
                }
        };
        warn "$file: $@" if $@;
 }
 
+# Garbage collection
+print STDERR "\nGarbage collection in '$link_dir'...";
+chdir($link_dir) || die "chdir: $link_dir: $!";
+print STDERR "\nRemoving files with link count < 3...";
+system("find digest inode -type f -links -3 -print0 | xargs -0 rm -f") and die "system: exit status $?";
+print STDERR "\nRemoving empty directories...";
+system("find digest inode -type d -empty -print0 | xargs -0r rmdir -p --ignore-fail-on-non-empty") and die "system: exit status $?";
+print STDERR "\nDone.\n";
+
 exit(0);
 
 __END__