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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
#!/usr/bin/env perl
## Copyright © 2010 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>
## 0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;
no if $] >= 5.018, warnings => 'experimental::smartmatch';
use App::Hashl;
use Cwd;
use DateTime;
use File::Copy;
use File::Find;
use Getopt::Long;
use IO::Handle;
use List::MoreUtils qw(any);
use Time::Progress;
my $add_unignore = 0;
my $base = getcwd();
my $rel_paths = 1;
my $db_file = '.hashl.db';
my $total = 0;
my $cur = 0;
my $show_progress = 1;
my $xdev_fsno;
my @edb_files;
my $timer;
my $incoming_dir;
my $read_size;
my ( $find_ref, $find_db_write );
my $hashl;
my @ehashl;
our $VERSION = '1.00';
STDERR->autoflush(1);
GetOptions(
'd|database=s' => \$db_file,
'e|extra-db=s' => \@edb_files,
'f|force' => \$add_unignore,
'n|no-progress' => sub { $show_progress = 0 },
's|read-size=i' => sub { $read_size = $_[1] * 1024 },
'V|version' => sub { say "hashl version ${VERSION}"; exit 0 },
'x|one-file-system' => sub { $xdev_fsno = ( stat($base) )[0] },
) or usage();
if ( substr( $db_file, 0, 1 ) ne q{/} ) {
$db_file = "${base}/${db_file}";
}
my $action = shift;
sub usage {
die(<<"EOF");
Usage: $0 [options] <update|list|info|...> [args]
See 'perldoc -F $0' (or 'man hashl' if it is properly installed)
EOF
}
if ( not defined $action ) {
usage();
}
if ( -r $db_file ) {
$hashl = App::Hashl->new_from_file($db_file);
}
else {
$hashl = App::Hashl->new( read_size => $read_size );
}
@ehashl = ($hashl);
for my $file (@edb_files) {
if ( -r $file ) {
push( @ehashl, App::Hashl->new_from_file($file) );
}
else {
die("-e ${file}: database does not exist\n");
}
}
local $SIG{INT} = \&quit_save_db;
local $SIG{TERM} = \&quit_save_db;
sub quit_save_db {
$hashl->save($db_file);
exit 0;
}
sub get_total {
my $file = $File::Find::name;
if ( -f $file
and not -l $file
and $file ne $db_file
and ( not $xdev_fsno or ( stat($file) )[0] == $xdev_fsno ) )
{
$total++;
}
return;
}
sub drop_deleted {
for my $file ( $hashl->files ) {
if ( not -e $file ) {
$hashl->delete_file($file);
}
}
return;
}
sub ensure_equal_hash_sizes {
for my $i ( 1 .. $#ehashl ) {
if ( $ehashl[$i]->read_size != $hashl->read_size ) {
printf STDERR (
'Cannot list: main database has read size %d, but database'
. " %s has read size %d\n",
$hashl->read_size,
$edb_files[ $i - 1 ],
$ehashl[$i]->read_size
);
exit 1;
}
}
return;
}
sub copy_file {
my ( $file, $to ) = @_;
my $cp_base = substr( $file, length($base) + 1 );
if ( $base =~ s{ / [^/]+ $}{}x ) {
mkdirs( $incoming_dir, $cp_base );
}
copy( $file, "${to}/${cp_base}" )
or die("Cannot copy ${file} to ${to}/${cp_base}: $!\n");
return;
}
sub db_find_new {
my ( $file, $path ) = @_;
if ( not any { $_->file_in_db($path) } @ehashl ) {
print STDERR "\r\e[2K";
say $file;
}
return;
}
sub db_find_known {
my ( $file, $path ) = @_;
if ( any { $_->file_in_db($path) } @ehashl ) {
print STDERR "\r\e[2K";
say $file;
}
return;
}
sub file_info {
my ($file) = @_;
my $entry = $hashl->file($file);
if ( not $entry ) {
die("No such file in database\n");
}
printf(
"File: %s\nSize: %d bytes (%s)\nHash: %s\n",
$file, $entry->{size}, $hashl->si_size( $entry->{size} ),
$entry->{hash},
);
return;
}
sub process_file {
my ( $code, $write ) = ( $find_ref, $find_db_write );
my $file = $File::Find::name;
my $path = $file;
if ( not -f $file
or -l $file
or $file eq $db_file
or ( $xdev_fsno and ( stat($file) )[0] != $xdev_fsno ) )
{
return;
}
if ($rel_paths) {
$file = substr( $file, length($base) + 1 );
}
$cur++;
if ($show_progress) {
print STDERR $timer->report(
"\r\e[2KScanning directory: %p done, %L elapsed, %E remaining",
$cur, );
}
&{$code}( $file, $path );
if ( $write and ( ( $cur % 5000 ) == 0 ) ) {
$hashl->save($db_file);
}
return;
}
sub db_update {
my ( $file, $path ) = @_;
$hashl->add_file(
file => $file,
path => $path,
unignore => $add_unignore,
);
return;
}
sub db_ignore {
my ( $file, $path ) = @_;
$hashl->ignore( $file, $path );
return;
}
sub db_copy {
my ( $file, $path ) = @_;
if ( not any { $_->file_in_db($path) } @ehashl ) {
copy_file( $path, $incoming_dir );
}
return;
}
sub mkdirs {
my ( $dir_base, $new ) = @_;
for my $dir ( split( qr{/}, $new ) ) {
$dir_base .= "/$dir";
if ( !-d $dir_base ) {
mkdir($dir_base)
or die("Cannot create ${dir_base}: $!\n");
}
}
return;
}
sub prepare_db_run {
my ($dir) = @_;
$dir //= $base;
if ( not $show_progress ) {
return;
}
find( \&get_total, $dir );
$timer = Time::Progress->new();
$timer->attr(
min => 1,
max => $total,
);
return;
}
sub cmd_copy {
($incoming_dir) = @_;
prepare_db_run();
if ( not $incoming_dir ) {
usage();
}
if ( substr( $incoming_dir, 0, 1 ) ne q{/} ) {
$incoming_dir = $base . q{/} . $incoming_dir;
}
$find_ref = \&db_copy;
$find_db_write = 0;
find( \&process_file, $base );
print "\n";
return;
}
sub cmd_find_known {
my ($dir) = @_;
$dir //= $base;
if ( substr( $dir, 0, 1 ) ne q{/} ) {
$dir = $base . q{/} . $dir;
}
prepare_db_run($dir);
$find_ref = \&db_find_known;
$find_db_write = 0;
find( \&process_file, $dir );
print "\n";
return;
}
sub cmd_find_new {
my ($new_dir) = @_;
$new_dir //= $base;
if ( substr( $new_dir, 0, 1 ) ne q{/} ) {
$new_dir = $base . q{/} . $new_dir;
}
prepare_db_run($new_dir);
$find_ref = \&db_find_new;
$find_db_write = 0;
find( \&process_file, $new_dir );
print "\n";
return;
}
sub cmd_ignore {
my ($ign_dir) = @_;
$ign_dir //= $base;
prepare_db_run();
if ( substr( $ign_dir, 0, 1 ) ne q{/} ) {
$ign_dir = $base . q{/} . $ign_dir;
}
$find_ref = \&db_ignore;
$find_db_write = 1;
find( \&process_file, $ign_dir );
$hashl->save($db_file);
print "\n";
return;
}
sub cmd_info {
my ($file) = @_;
if ($file) {
file_info($file);
}
else {
print $hashl->db_info();
}
return;
}
sub map_with_prefix {
my ( $prefix, @items ) = @_;
return map { [ $prefix, $_ ] } @items;
}
sub cmd_list {
my ($re) = @_;
ensure_equal_hash_sizes();
printf(
"# hashl v%s Read Size %d bytes (%s)\n",
$VERSION, $hashl->read_size, $hashl->si_size( $hashl->read_size ),
);
for my $pair (
sort { $a->[1] cmp $b->[1] }
map { map_with_prefix( $_, $_->files ) } @ehashl
)
{
my ( $db, $name ) = @{$pair};
my $file = $db->file($name);
if ( $re and $name !~ m{$re} ) {
next;
}
printf( "%s %-7s %s\n",
$file->{hash}, $db->si_size( $file->{size} ), $name );
}
return;
}
sub cmd_ls {
my ($re) = @_;
my $now = DateTime->now;
ensure_equal_hash_sizes();
for my $pair (
sort { $a->[1] cmp $b->[1] }
map { map_with_prefix( $_, $_->files ) } @ehashl
)
{
my ( $db, $name ) = @{$pair};
my $file = $db->file($name);
my $dt = DateTime->from_epoch(
epoch => $file->{mtime},
);
my $time_format = '%b %d %H:%M';
# Date math is hard. So we don't account for leap years (or leap seconds) here.
if ( $now->epoch - $dt->epoch >= 31536000 ) {
$time_format = '%b %d %Y';
}
if ( $re and $name !~ m{$re} ) {
next;
}
printf( "%-7s %s %s\n",
$db->si_size( $file->{size} ),
$dt->strftime($time_format), $name );
}
return;
}
sub cmd_list_files {
say join( "\n", sort map { $_->files } @ehashl );
return;
}
sub cmd_list_ignored {
ensure_equal_hash_sizes();
say join( "\n", map { $_->ignored } @ehashl );
return;
}
sub cmd_update {
drop_deleted();
prepare_db_run();
$find_ref = \&db_update;
$find_db_write = 1;
find( \&process_file, $base );
print "\n";
$hashl->save($db_file);
return;
}
given ($action) {
when ('copy') { cmd_copy(@ARGV) }
when ('find-known') { cmd_find_known(@ARGV) }
when ('find-new') { cmd_find_new(@ARGV) }
when ('ignore') { cmd_ignore(@ARGV) }
when ('info') { cmd_info(@ARGV) }
when ('ls') { cmd_ls(@ARGV) }
when ('list') { cmd_list(@ARGV) }
when ('list-files') { cmd_list_files(@ARGV) }
when ('list-ignored') { cmd_list_ignored(@ARGV) }
when ('update') { cmd_update(@ARGV) }
default { usage() }
}
__END__
=head1 NAME
B<hashl> - Create database with partial file hashes, check if other files are in it
=head1 SYNOPSIS
B<hashl> [B<-fnx>] [B<-d> I<dbfile>] [B<-s> I<read-size>] I<action> [I<args>]
=head1 VERSION
This manual documents hashl version 1.00
=head1 DESCRIPTION
Actions:
=over
=item B<copy> I<newdir>
Copy all files in the current directory which are not in any database to
I<newdir>.
=item B<find-known> [I<directory>]
List all files which are already in any database. Scans either the current
directory or I<directory>.
=item B<find-new> [I<directory>]
List all files which are not in any database. Scans either the current
directory or I<directory>.
=item B<ignore> [I<directory>]
Add all files in I<directory> (or the current directory) as "ignored" to the
database. This means that hashl will save the file's hash and skip matching
files for B<copy> or B<find-new>.
=item B<info> [I<file>]
Show information on I<file> (or the database, if I<file> is not specified).
=item B<list> [I<regex>]
List all files and their hashes. The list format is C<< hash size file >>.
If I<regex> (a perl regular expression) is specifed, only matching files will
be listed.
=item B<list-files>
List all filenames, one file per line.
=item B<list-ignored>
List ignored hashes.
=item B<update>
Update or create hash database. Iterates over all files below the current
directory.
=back
=head1 OPTIONS
=over
=item B<-d>|B<--database> I<dbfile>
Use I<dbfile> instead of F<.hashl.db>
=item B<-e>|B<--extra-db> I<dbfile>
Use I<dbfile> in addition to F<.hashl.db> / B<-d>. May be specified
several times.
Database files specified with this option will be opened read-only and ignored
by writing actions (such as B<update> or B<ignore>).
=item B<-f>|B<--force>
For use with C<< hashl add >>: If there are ignored files in the directory,
unignore and add them.
=item B<-n>|B<--no-progress>
Do not show progress information. Most useful with C<< hashl find-new >>.
=item B<-s>|B<--read-size> I<kibibytes>
Change size of the part of each file which is hashed. By default, B<hashl>
hashes the first 4 MiB. Note that this option only makes sense when using C<<
hashl update >> to create a new database.
A size of 0 (zero) makes hashl read whole files, i.e. turning it into sha1sum
with a database.
=item B<-V>|B<--version>
Print version information.
=item B<-x>|B<--one-file-system>
Do not cross filesystem boundaries when processing files. At the time of this
writing, this may not prevent hashl from recursing into other filesystems,
but they will never be hashed, copied or otherwise processed.
=back
=head1 EXIT STATUS
Unless an error occured, B<hashl> will always return zero.
=head1 CONFIGURATION
None, so far
=head1 DEPENDENCIES
=over
=item * Digest::SHA
=item * List::MoreUtils
=item * Time::Progress
=back
=head1 BUGS AND LIMITATIONS
Unknown.
=head1 EXAMPLES
=head2 LEECHING
First, create a database of your local files:
C<< cd /media/videos; hashl update >>
Now, assume you have a (possibly slow) external share mounted at
F</tmp/mnt/ext>. You do not want to copy all files to your disk and then use
B<fdupes> or similar to weed out the duplicates. Since you just used B<hashl>
to create a database with the hashes of the first 4MB of all your files, you
can now use it to check if you (very probably) already have any remote file.
For that, you only need to leech the first 4MB of every file on the share, and
not the whole file. For example:
C<< cd /tmp/mnt/ext; hashl copy /media/videos/incoming >>
=head2 EXTERNAL HARD DISK
Personally, I have all my videos on an external hard disk, which I usually do
not carry with me. So, when I get new videos, I put them into F<~/lib/videos>
on my netbook, and then later copy them to the external disk. Of course, it
can always happen that I get a movie I already have, or forget to move
something from F<~/lib/videos> to the external disk, especially since I also
always have some stuff from the disk in F<~/lib/videos>.
However, I can use B<hashl> to conveniently solve this issue. Run
periodically:
C<< cd /media/argon; hashl -d ~/lib/video/.argon update >>
Now, I always have a list of files on the external disk with me. When I get a
new file:
C<< hashl -d ~/lib/video/.argon new-file $file >>
And to find out which files are not on the external disk:
C<< cd ~/lib/video; print -l **/*(.) | hashl -d .argon new-file >>
=head1 AUTHOR
Copyright (C) 2010 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>
=head1 LICENSE
0. You just DO WHAT THE FUCK YOU WANT TO.
|