Add garbage collection at the end of each run.
[dupemerge] / dm6
diff --git a/dm6 b/dm6
index b0ff96b84b633c717bf0516038f74e6ae2be494d..874534fac260f177fd174250296d0a872fb16aa3 100755 (executable)
--- a/dm6
+++ b/dm6
@@ -156,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) {
@@ -165,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__