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);
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) {
# 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__