All pastes #1873136 Raw Edit

Something

public text v1 · immutable
#1873136 ·published 2010-05-27 22:12 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use warnings;
use Digest::MD5;

my $ctx = Digest::MD5->new;
my $cmp;

sub md5sum()
{
	my $file = shift;
	local *FILE;
	open(FILE,"$file");
		$ctx->addfile(*FILE);
	close(FILE);
	return $ctx->hexdigest;
}

sub dupeAction()
{
	my $file = shift;
	print "Dupe: $file\n";
}

sub dupeCheck()
{
	my $file = shift;
	my $size = (stat($file))[7];
	if (defined($cmp->{'size'}->{"$size"})) {
		if (!defined($cmp->{'size'}->{"$size"}->{"$file"}->{'hex'})) {
			foreach my $i (keys %{ $cmp->{'size'}->{"$size"} })
			{
				if (!defined($cmp->{'size'}->{"$size"}->{"$i"}->{'hex'}) || $cmp->{'size'}->{"$size"}->{"$i"}->{'hex'} != 2) {
					my $hex = &md5sum($i);
					$cmp->{'size'}->{"$size"}->{"$i"}->{'hex'} = 2;
					if (defined($cmp->{'hex'}->{"$hex"})) {
						&dupeAction($i);
					} else {
						$cmp->{'hex'}->{"$hex"} = 1;
					}
				}
			}
		}
	} else {
		$cmp->{'size'}->{"$size"}->{"$file"}->{'hex'} = 1;
	}
}

sub scandir()
{
	my $dir = shift;
	local *DIR;
	opendir(DIR, $dir);
		while (my $file = readdir(DIR))
		{
			next if ($file eq '.' || $file eq '..');
			my $flink = "$dir/$file";
			if (-d $flink) {
				&scandir($flink);
			} elsif (-f $flink) {
				&dupeCheck($flink);
			}
		}
	closedir(DIR);
}

sub main()
{
	my $dir = $ARGV[0] || ".";
	&scandir($dir);
}

&main();