Skip to content

Instantly share code, notes, and snippets.

@f99aq8ove
Created May 5, 2012 09:44
Show Gist options
  • Save f99aq8ove/2601215 to your computer and use it in GitHub Desktop.
Save f99aq8ove/2601215 to your computer and use it in GitHub Desktop.
dedupe duplicated files using hard-linking.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
my $dryrun = 0;
my $help = 0;
my $min_bytes = 1024;
GetOptions(
'help' => \$help,
'dry-run|n' => \$dryrun,
'min-bytes=i' => \$min_bytes
) or pod2usage 2;
pod2usage 1 if $help;
my @target_dirs = @ARGV;
pod2usage 3 unless scalar @ARGV;
=pod
=head1 NAME
dedupe - dedupe duplicated files
=head1 SYNOPSIS
dedupe [options] dir [dir ...]
Options:
-h, --help help message
-n, --dry-run perfome trial run
--min-bytes specify minimum file size to dedupe
=head1 OPTIONS
=over 8
=item B<-h, --help>
Print a usage and exits.
=item B<-n, --dry-run>
Perfome trial run with no changes.
=item B<--min-bytes>
Specify minimum file size to dedupe.
=back
=head1 DESCRIPTION
B<This program> will read the given input directories recursively and find out
duplicated files using SHA-1 hash.
Then dedupe the files by hard-linking.
=head1 LICENSE
Copyright (c) 2012, Masatoshi Haraoka (f99aq8ove). All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
# inode => {
# numofLinks
# size
# mtime
# name => []
# hash
# }
my %by_inode = ();
# size_mtime => { inode => 1 }
my %by_size_mtime = ();
# size_name => { inode => 1 }
my %by_size_name = ();
# size => { inode => 1 }
my %by_size = ();
main(@target_dirs);
exit;
sub main {
my @dirs = @_;
while (@dirs) {
my $dir = shift @dirs;
my @items = do {
my $res = opendir(my $dh, $dir);
if (!$res) {
warn "cannot open dir $dir";
();
}
else {
my @f = grep !/^\.{1,2}$/, readdir $dh;
closedir $dh;
map { File::Spec->join($dir, $_) } @f;
}
};
push @dirs, grep {-d} @items;
my @files = grep { -f -s > $min_bytes } @items;
next unless @files;
for my $file (@files) {
my (undef, $ino, undef, $nlink, undef, undef, undef,
$size, undef, $mtime, undef, undef, undef
) = stat $file;
if (!exists $by_inode{$ino}) {
$by_inode{$ino} = {
numofLinks => $nlink,
size => $size,
mtime => $mtime,
name => [$file],
};
}
else {
push $by_inode{$ino}{name}, $file;
}
{
my $size_mtime = $size . '_' . $mtime;
$by_size_mtime{$size_mtime} //= {};
$by_size_mtime{$size_mtime}{$ino} = 1;
}
{
my (undef, undef, $name) = File::Spec->splitpath($file);
my $size_name = $size . '_' . $name;
$by_size_name{$size_name} //= {};
$by_size_name{$size_name}{$ino} = 1;
}
$by_size{$size} //= {};
$by_size{$size}{$ino} = 1;
}
}
my $dedupe = sub {
my @inodes = @_;
my ($inode_a, $inode_b) = dup_check(@inodes);
return unless $inode_a;
my ($copy_inode, $remove_inode);
if ($by_inode{$inode_a}{mtime} == $by_inode{$inode_b}{mtime}) {
if ($by_inode{$inode_a}{numofLinks}
< $by_inode{$inode_b}{numofLinks})
{
# remove fewer
($copy_inode, $remove_inode) = ($inode_b, $inode_a);
}
else {
($copy_inode, $remove_inode) = ($inode_a, $inode_b);
}
}
else {
# newer
if ($by_inode{$inode_a}{mtime} > $by_inode{$inode_b}{mtime}) {
($copy_inode, $remove_inode) = ($inode_b, $inode_a);
}
else {
($copy_inode, $remove_inode) = ($inode_a, $inode_b);
}
}
for (@{ $by_inode{$remove_inode}{name} }) {
print "dedupe $_ by $by_inode{$copy_inode}{name}[0]\n";
replace($by_inode{$copy_inode}{name}[0], $_) unless $dryrun;
}
my $remove = $by_inode{$remove_inode};
push $by_inode{$copy_inode}{name}, @{ $remove->{name} };
$by_inode{$copy_inode}{numofLinks}++;
delete $by_inode{$remove_inode} or die $remove_inode;
{
my $key = $remove->{size} . '_' . $remove->{mtime};
delete $by_size_mtime{$key}{$remove_inode} or die $remove_inode;
if (keys %{ $by_size_mtime{$key} } == 0) {
delete $by_size_mtime{$key} or die $key;
}
}
for my $file (@{ $remove->{name} }) {
my (undef, undef, $name) = File::Spec->splitpath($file);
my $key = $remove->{size} . '_' . $name;
last unless exists $by_size_name{$key}{$remove_inode};
delete $by_size_name{$key}{$remove_inode} or die $remove_inode;
if (keys %{ $by_size_name{$key} } == 0) {
delete $by_size_name{$key} or die $key;
}
}
{
my $key = $remove->{size};
delete $by_size{$key}{$remove_inode} or die $remove_inode;
if (keys %{ $by_size{$key} } == 0) {
delete $by_size{$key} or die $key;
}
}
};
warn 'going to dedupe using size and mtime';
&$dedupe(keys %$_) for values %by_size_mtime;
warn 'going to dedupe using size and name';
&$dedupe(keys %$_) for values %by_size_name;
warn 'going to dedupe using size';
&$dedupe(keys %$_) for values %by_size;
# warn Dumper \%by_inode;
# warn Dumper \%by_size_mtime;
# warn Dumper \%by_size_name;
# warn Dumper \%by_size;
}
sub replace {
my ($copy, $remove) = @_;
my $interrupted = 0;
local $SIG{INT} = sub { $interrupted = 1; };
rename $remove, "$remove~" or die $!;
link $copy, $remove or do {
my $err = $!;
rename "$remove~", $remove or warn $!;
die $err;
};
unlink "$remove~" or die $!;
die if $interrupted;
}
sub hash {
use Digest::SHA1;
open my $fh, '<', shift or return;
my $sha1 = Digest::SHA1->new;
$sha1->addfile($fh);
my $hash = $sha1->digest;
close $fh;
$hash;
}
sub dup_check {
my @inodes = @_;
my %hash = ();
return if scalar @inodes < 2;
for my $inode (@inodes) {
my $info = $by_inode{$inode};
my $h = $info->{hash};
if (!$h) {
$h = hash $info->{name}[0];
}
next unless $h;
$info->{hash} = $h;
if (!exists $hash{$h}) {
$hash{$h} = $inode;
}
else {
return ($inode, $hash{$h});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment