-
Notifications
You must be signed in to change notification settings - Fork 34
/
fsf.pl
executable file
·137 lines (105 loc) · 2.75 KB
/
fsf.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 23 July 2015
# https://github.com/trizen
# Find files which have almost the same content (at least, mathematically).
#
## WARNING! For strict duplicates, use the 'fdf' script:
# https://github.com/trizen/perl-scripts/blob/master/Finders/fdf
#
use 5.014;
use strict;
use warnings;
use Math::BigInt (try => 'GMP');
use File::Find qw(find);
use Getopt::Long qw(GetOptions);
sub help {
my ($code) = @_;
print <<"HELP";
usage: $0 [options] /my/path [...]
Options:
-w --whitespaces! : remove whitespaces (default: false)
-u --unique! : don't include a file in more groups (default: false)
-h --help : print this message and exit
Example:
$0 -w ~/Documents
HELP
exit($code // 0);
}
my $strip_spaces = 0; # bool
my $unique = 0; # bool
GetOptions(
'w|whitespaces!' => \$strip_spaces,
'u|unique!' => \$unique,
'h|help' => \&help,
)
or die("Error in command line arguments");
sub hash ($) {
my ($str) = @_;
$strip_spaces
and $str =~ s/\s+//g;
state $ten = Math::BigInt->new(10);
my $hash1 = Math::BigInt->new(0);
my $pow = Math::BigInt->new(1);
state $chars = {};
my @chars = map { $chars->{$_} //= Math::BigInt->new($_) } unpack("C*", $str);
foreach my $char (@chars) {
$hash1->badd($pow->copy->bmul($char));
$pow->bmul($ten);
}
return $hash1;
}
sub hash_file ($) {
my ($file) = @_;
open my $fh, '<:raw', $file;
hash(
do { local $/; <$fh> }
);
}
sub alike_hashes ($$) {
my ($h1, $h2) = @_;
my $pow = abs($h1->copy->blog(10) - $h2->copy->blog(10));
my $ratio = ($h2 > $h1 ? ($h2 / $h1) : ($h1 / $h2));
my $limit = 10**$pow;
$ratio == $limit;
}
sub find_similar_files (&@) {
my $code = shift;
my @files;
find {
wanted => sub {
(-f)
&& push @files,
{
hash => hash_file($File::Find::name),
name => $File::Find::name,
};
}
} => @_;
my %dups;
foreach my $i (0 .. $#files - 1) {
for (my $j = $i + 1 ; $j <= $#files ; $j++) {
if (alike_hashes($files[$i]{hash}, $files[$j]{hash})) {
push @{$dups{$files[$i]{name}}},
(
$unique
? ${splice @files, $j--, 1}{name}
: $files[$j]{name}
);
}
}
}
while (my ($fparent, $fdups) = each %dups) {
$code->(sort $fparent, @{$fdups});
}
return 1;
}
{
@ARGV || help(1);
local $, = "\n";
find_similar_files {
say @_, "-" x 80 if @_;
}
@ARGV;
}