dupemerge: add --progress option
[dupemerge] / faster-dupemerge
1 #!/usr/bin/perl -w
2 use strict;
3 use Fcntl qw(:DEFAULT :flock);
4 use File::Compare;
5 use File::Temp;
6
7 # Copyright (C) 2003-2010 Zygo Blaxell <faster-dupemerge@mailtoo.hungrycats.org>
8
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 my $input_links = 0;
24 my $input_files = 0;
25 my $input_bogons = 0;
26 my $hash_bytes = 0;
27 my $hash_files = 0;
28 my $hash_errors = 0;
29 my $compare_bytes = 0;
30 my $compare_count = 0;
31 my $compare_errors = 0;
32 my $compare_differences = 0;
33 my $trivially_unique = 0;
34 my $merges_attempted = 0;
35 my $hard_links = 0;
36 my $link_errors = 0;
37 my $link_retries = 0;
38 my $recovered_bytes = 0;
39 my $recovered_files = 0;
40 my $lost_files = 0;
41 my $lost_bytes = 0;
42 my $surprises = 0;
43
44 eval '
45         use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
46 ';
47
48 if ($@) {
49         warn "Digest::SHA1: $@\nUsing external md5sum program to generate hashes.\nPlease install Digest::SHA1 (libdigest-sha1-perl)";
50
51         eval <<'DIGEST';
52                 sub really_digest {
53                         my ($filename) = (@_);
54                         my $fv = open(MD5SUM, "-|");    
55                         die "fork: $!" unless defined($fv);
56                         if ($fv) {
57                                 my ($sum_line) = <MD5SUM>;
58                                 close(MD5SUM) or die "md5sum: exit status $? (error status $!)";
59                                 die "hash error:  got EOF instead of md5sum output" unless defined($sum_line);
60                                 my ($sum) = $sum_line =~ m/^([a-fA-F0-9]{32})/o;
61                                 die "hash error:  got \Q$sum_line\E instead of md5sum output" unless defined($sum);
62                                 return $sum;
63                         } else {
64                                 sysopen(STDIN, $filename, O_RDONLY|O_NONBLOCK) or die "open: $filename: $!";
65                                 exec('md5sum');
66                                 # Perl guarantees it will die here
67                         }
68                 }
69 DIGEST
70 } else {
71         eval <<'DIGEST';
72                 sub really_digest {
73                         my ($filename) = (@_);
74                         die "'$filename' is not a plain file" if (-l $filename) || ! (-f _);
75                         my $ctx = Digest::SHA1->new;
76                         sysopen(FILE, $filename, O_RDONLY|O_NONBLOCK) or die "open: $filename: $!";
77                         binmode(FILE);          # FIXME:  Necessary?  Probably harmless...
78                         $ctx->addfile(\*FILE);
79                         close(FILE) or die "close: $filename: $!";
80                         return $ctx->b64digest;
81                 }
82 DIGEST
83 }
84         
85 my $collapse_access = 0;
86 my $collapse_timestamp = 0;
87 my $collapse_zero = 0;
88 my $skip_compares = 0;
89 my $skip_hashes = 0;
90 my $progress = 0;
91 my $verbose = 0;
92 my $debug = 0;
93 my $dry_run = 0;
94 my $humane = 0;
95 my @extra_find_opts = ();
96 my @extra_sort_opts = ();
97 my $lock_file;
98 my $lock_rm = 0;
99 my $lock_obtained = 0;
100
101 sub digest {
102         my ($filename) = (@_);
103         if ($skip_hashes) {
104                 return "SKIPPING HASHES";
105         } else {
106                 print STDERR 'H' if $progress;
107                 my $digest = &really_digest($filename);
108                 $hash_bytes += -s $filename;
109                 $hash_files++;
110                 return $digest
111         }
112 }
113
114 my @directories;
115
116 sub usage {
117         my $name = shift(@_);
118         die <<USAGE;
119 Usage: $name [--opts] directory [directory...]
120 Finds duplicate files in the given directories, and replaces all identical
121 copies of a file with hard-links to a single file.
122
123 Several options modify the definition of a "duplicate".  By default, files
124 which have differences in owner uid or gid, permission (mode), or
125 modification time (mtime) are considered different, so that hardlinking
126 files does not also change their attributes.  Additionally, all files of
127 zero size are ignored for performance reasons (there tend to be many
128 of them, and they tend not to release any space when replaced with
129 hard links).
130
131         --access        uid, gid, and mode may be different for identical
132                         files
133
134         --debug         show all steps in duplication discovery process
135                         (implies --verbose)
136
137         --dry-run       do not lock files or make changes to filesystem
138
139         --find          pass next options (up to --) to find command
140
141         --humane        human-readable statistics (e.g. 1 048 576)
142
143         --lock FILE     exit immediately (status 10) if unable to obtain a 
144                         flock(LOCK_EX|LOCK_NB) on FILE
145
146         --lock-rm       remove lock file at exit
147
148         --sort          pass next options (up to --) to sort command
149
150         --timestamps    mtime may be different for identical files
151
152         --skip-compare  skip byte-by-byte file comparisons
153
154         --skip-hash     skip calculation of hash function on files
155
156         --trust         old name for --skip-compare
157                         (trust the hash function)
158
159         --verbose       report files as they are considered
160
161         --zeros         hard-link zero-length files too
162 USAGE
163 }
164
165 while ($#ARGV >= 0) {
166         my $arg = shift(@ARGV);
167         if ($arg eq '--access') {
168                 $collapse_access = 1;
169         } elsif ($arg eq '--timestamps') {
170                 $collapse_timestamp = 1;
171         } elsif ($arg eq '--zeros') {
172                 $collapse_zero = 1;
173         } elsif ($arg eq '--trust' || $arg eq '--skip-compare') {
174                 $skip_compares = 1;
175         } elsif ($arg eq '--skip-hash') {
176                 $skip_hashes = 1;
177         } elsif ($arg eq '--progress') {
178                 $progress = 1;
179         } elsif ($arg eq '--verbose') {
180                 $verbose = 1;
181         } elsif ($arg eq '--lock-rm') {
182                 $lock_rm = 1;
183         } elsif ($arg eq '--lock') {
184                 $lock_file = shift(@ARGV);
185                 unless (defined($lock_file)) {
186                         usage($0);
187                         exit(1);
188                 }
189         } elsif ($arg eq '--debug') {
190                 $debug = $verbose = 1;
191         } elsif ($arg eq '--dry-run') {
192                 $dry_run = 1;
193         } elsif ($arg eq '--humane') {
194                 $humane = 1;
195         } elsif ($arg eq '--find') {
196                 while ($#ARGV >= 0) {
197                         my $extra_arg = shift(@ARGV);
198                         last if $extra_arg eq '--';
199                         push(@extra_find_opts, $extra_arg);
200                 }
201         } elsif ($arg eq '--sort') {
202                 while ($#ARGV >= 0) {
203                         my $extra_arg = shift(@ARGV);
204                         last if $extra_arg eq '--';
205                         push(@extra_sort_opts, $extra_arg);
206                 }
207         } elsif ($arg =~ /^-/o) {
208                 usage($0);
209                 exit(1);
210         } else {
211                 push(@directories, $arg);
212         }
213 }
214
215 if ($skip_hashes && $skip_compares) {
216         die "Cannot skip both hashes and compares.\n";
217 }
218
219 @directories or usage;
220
221 if (defined($lock_file) && !$dry_run) {
222         sysopen(LOCK_FILE, $lock_file, O_CREAT|O_RDONLY, 0666) or die "open: $lock_file: $!";
223         flock(LOCK_FILE, LOCK_EX|LOCK_NB) or die "flock: $lock_file: LOCK_EX|LOCK_NB: $!";
224         print STDERR "Locked '$lock_file' in LOCK_EX mode.\n" if $verbose;
225         $lock_obtained = 1;
226 }
227
228 END {
229         if ($lock_obtained && !$dry_run) {
230                 print STDERR "Removing '$lock_file'.\n" if $verbose;
231                 unlink($lock_file) or warn "unlink: $lock_file: $!";
232         }
233 }
234
235 sub tick_quote {
236         my ($text) = (@_);
237         $text =~ s/'/'\\''/go;
238         return "'$text'";
239 }
240
241 my @find_command = ('find', @directories, @extra_find_opts, '-type', 'f');
242 my $printf_string = '%s ' .
243         ($collapse_access    ? '0 0 0 ' : '%U %G %m ') .
244         ($collapse_timestamp ? '0 '     : '%T@ ') .
245         '%D %i %p\0';
246
247 push(@find_command, '!', '-empty') unless $collapse_zero;
248 push(@find_command, '-printf', $printf_string);
249
250 my @sort_command = ('sort', '-znr', @extra_sort_opts);
251 my @quoted_sort_command = @sort_command;
252 grep(tick_quote($_), @quoted_sort_command);
253 my $quoted_sort_command = "'" . join("' '", @quoted_sort_command) . "'";
254
255 my @quoted_find_command = @find_command;
256 grep(tick_quote($_), @quoted_find_command);
257 my $quoted_find_command = "'" . join("' '", @quoted_find_command) . "'";
258 print STDERR "find command:  $quoted_find_command | $quoted_sort_command\n" if $verbose;
259
260 open(FIND, "$quoted_find_command | $quoted_sort_command |") or die "open: $!";
261 $/ = "\0";
262
263 # Input is sorted so that all weak keys are contiguous.
264 # When the key changes, we have to process all files we previously know about.
265 my $current_key = -1;
266
267 # $inode_to_file_name{$inode} = [@file_names]
268 my %inode_to_file_name = ();
269
270 # Link files
271 sub link_files {
272         my ($from, $to) = (@_);
273
274         my $quoted_from = tick_quote($from);
275         my $quoted_to = tick_quote($to);
276         print STDERR "\n" if $progress;
277         print STDERR "ln -f $quoted_from $quoted_to\n";
278
279         return if $dry_run;
280
281         my $inode_dir = $to;
282         my $inode_base = $to;
283         $inode_dir =~ s:[^/]*$::o;
284         $inode_base =~ s:^.*/::os;
285         my $tmp_to = File::Temp::tempnam($inode_dir, ".$inode_base.");
286         print STDERR "\tlink: $from -> $tmp_to\n" if $debug;
287         print STDERR 'L' if $progress;
288         link($from, $tmp_to) or die "link: $from -> $tmp_to: $!";
289         print STDERR "\trename: $tmp_to -> $to\n" if $debug;
290         print STDERR 'R' if $progress;
291         unless (rename($tmp_to, $to)) {
292                 my $saved_bang = $!;
293                 print STDERR 'U' if $progress;
294                 unlink($tmp_to) or warn "unlink: $tmp_to: $!";  # Try, possibly in vain, to clean up
295                 die "rename: $tmp_to -> $from: $saved_bang";
296         }
297 }
298
299 # Convert $dev,$ino into a single string where lexical and numeric orderings are equivalent
300 sub format_inode ($$) {
301         my ($dev, $ino) = @_;
302         # 64 bits ought to be enough for everybody!
303         return sprintf('%016x:%016x', $dev, $ino);
304 }
305
306 # Process all known files so far.
307 sub merge_files {
308         $merges_attempted++;
309
310         my %hash_to_inode;
311         # Used to stop link retry loops (there is a goto in here!  Actually two...)
312         my %stop_loop;
313
314         my @candidate_list = keys(%inode_to_file_name);
315         $input_files += @candidate_list;
316         if (@candidate_list < 2) {
317                 print STDERR "Merging...only one candidate to merge..." if $debug;
318                 $trivially_unique++;
319                 goto end_merge;
320         }
321
322         print STDERR "Merging...\n" if $debug;
323         foreach my $candidate (sort @candidate_list) {
324                 print STDERR "\tDigesting candidate $candidate\n" if $debug;
325                 my $ok = 0;
326                 my $digest;
327
328 hash_file:
329
330                 foreach my $filename (sort keys(%{$inode_to_file_name{$candidate}})) {
331                         print STDERR "\t\tDigesting file $filename\n" if $debug;
332                         if ((-l $filename) || ! -f _) {
333                                 warn "Bogon file " . tick_quote($filename);
334                                 $surprises++;
335                                 next;
336                         }
337                         eval { 
338                                 $digest = digest($filename); 
339                         };
340                         if ($@) {
341                                 warn "Digest($filename)(#$candidate) failed: $@";
342                                 $hash_errors++;
343                         } else {
344                                 $ok = 1;
345                                 last hash_file;
346                         }
347                 }
348                 if ($ok) {
349                         print STDERR "\t\tDigest is $digest\n" if $debug;
350
351                         my $incumbent_list = ($hash_to_inode{$digest} ||= []);
352                         my $incumbent_matched = 0;
353                         for my $incumbent (sort @$incumbent_list) {
354                                 print STDERR "\t\tInodes $incumbent and $candidate have same hash\n" if $debug;
355
356                                 my $finished = 0;
357
358 link_start:
359
360                                 until ($finished) {
361                                         my @incumbent_names = sort keys(%{$inode_to_file_name{$incumbent}});
362                                         my @candidate_names = sort keys(%{$inode_to_file_name{$candidate}});
363                                         print STDERR "\t\tLinks to $incumbent:", join("\n\t\t\t", '', @incumbent_names), "\n" if $debug;
364                                         print STDERR "\t\tLinks to $candidate:", join("\n\t\t\t", '', @candidate_names), "\n" if $debug;
365
366 incumbent_file:
367
368                                         foreach my $incumbent_file (@incumbent_names) {
369                                                 print STDERR 'S' if $progress;
370                                                 my ($incumbent_dev,$incumbent_ino,$incumbent_mode,$incumbent_nlink,$incumbent_uid,$incumbent_gid,$incumbent_rdev,$incumbent_size,$incumbent_atime,$incumbent_mtime,$incumbent_ctime,$incumbent_blksize,$incumbent_blocks) = lstat($incumbent_file);
371                                                 print STDERR "\t\tINCUMBENT dev=$incumbent_dev ino=$incumbent_ino mode=$incumbent_mode nlink=$incumbent_nlink uid=$incumbent_uid gid=$incumbent_gid rdev=$incumbent_rdev size=$incumbent_size atime=$incumbent_atime mtime=$incumbent_mtime ctime=$incumbent_ctime blksize=$incumbent_blksize blocks=$incumbent_blocks _=$incumbent_file\n" if $debug;
372
373                                                 if (!defined($incumbent_blocks)) {
374                                                         warn "lstat: $incumbent_file: $!";
375                                                         $surprises++;
376                                                         next incumbent_file;
377                                                 }
378
379                                                 if (format_inode($incumbent_dev, $incumbent_ino) ne $incumbent) {
380                                                         warn "$incumbent_file: expected inode $incumbent, found $incumbent_dev:$incumbent_ino";
381                                                         $surprises++;
382                                                         next incumbent_file;
383                                                 }
384
385                                                 my $at_least_one_link_done = 0;
386
387 candidate_file:
388
389                                                 foreach my $candidate_file (@candidate_names) {
390                                                         print STDERR 's' if $progress;
391                                                         my ($candidate_dev,$candidate_ino,$candidate_mode,$candidate_nlink,$candidate_uid,$candidate_gid,$candidate_rdev,$candidate_size,$candidate_atime,$candidate_mtime,$candidate_ctime,$candidate_blksize,$candidate_blocks) = lstat($candidate_file);
392                                                         print STDERR "\t\t\tCANDIDATE dev=$candidate_dev ino=$candidate_ino mode=$candidate_mode nlink=$candidate_nlink uid=$candidate_uid gid=$candidate_gid rdev=$candidate_rdev size=$candidate_size atime=$candidate_atime mtime=$candidate_mtime ctime=$candidate_ctime blksize=$candidate_blksize blocks=$candidate_blocks _=$candidate_file\n" if $debug;
393
394                                                         if (!defined($candidate_blocks)) {
395                                                                 warn "lstat: $candidate_file: $!";
396                                                                 $surprises++;
397                                                                 next candidate_file;
398                                                         }
399
400                                                         if (format_inode($candidate_dev, $candidate_ino) ne $candidate) {
401                                                                 warn "$candidate_file: expected inode $candidate, found $candidate_dev:$candidate_ino";
402                                                                 $surprises++;
403                                                                 next candidate_file;
404                                                         }
405
406                                                         if ($candidate_size != $incumbent_size) {
407                                                                 warn "$candidate_file, $incumbent_file: file sizes are different";
408                                                                 $surprises++;
409                                                                 next candidate_file;
410                                                         }
411
412                                                         my $identical;
413
414                                                         eval {
415                                                                 if ($skip_compares) {
416                                                                         print STDERR "\t\t\t\tSkipping compare!\n" if $debug;
417                                                                         $identical = 1;
418                                                                 } else {
419                                                                         my $quoted_incumbent_file = tick_quote($incumbent_file);
420                                                                         my $quoted_candidate_file = tick_quote($candidate_file);
421                                                                         print STDERR "cmp $quoted_incumbent_file $quoted_candidate_file\n" if $debug;
422                                                                         print STDERR 'C' if $progress;
423                                                                         if (compare($incumbent_file, $candidate_file)) {
424                                                                                 $compare_differences++;
425                                                                                 $identical = 0;
426                                                                                 # It is significant for two non-identical files to have identical SHA1 or MD5 hashes.
427                                                                                 # Some kind of I/O error is more likely, so this message cannot be turned off.
428                                                                                 # On the other hand, if we're skipping hashes, _all_ files will have the same hash,
429                                                                                 # so the warning in that case is quite silly.  Hmmm.
430                                                                                 print STDERR "$quoted_incumbent_file and $quoted_candidate_file have same hash but do not compare equal!\n" unless $skip_hashes;
431                                                                         } else {
432                                                                                 $identical = 1;
433                                                                                 $incumbent_matched = 1;
434                                                                         }
435                                                                         $compare_count++;
436                                                                         $compare_bytes += $incumbent_size;
437                                                                 }
438                                                         };
439                                                         if ($@) {
440                                                                 warn $@;
441                                                                 $compare_errors++;
442                                                                 next candidate_file;
443                                                         }
444
445                                                         if ($identical) {
446                                                                 print STDERR "\t\t\t\tincumbent_nlink=$incumbent_nlink, candidate_nlink=$candidate_nlink\n" if $debug;
447
448                                                                 # We have to do this to break out of a possible infinite loop.
449                                                                 # Given file A, with hardlinks A1 and A2, and file B, with hardlink B1,
450                                                                 # such that A1 and B1 are in non-writable directories, we will loop
451                                                                 # forever hardlinking A2 with A and B.
452                                                                 # To break the loop, we never attempt to hardlink any files X and Y twice.
453
454                                                                 if (defined($stop_loop{$incumbent_file}->{$candidate_file}) ||
455                                                                     defined($stop_loop{$candidate_file}->{$incumbent_file})) {
456                                                                         print STDERR "Already considered linking '$incumbent_file' and '$candidate_file', not trying again now\n";
457                                                                 } else {
458                                                                         $stop_loop{$incumbent_file}->{$candidate_file} = 1;
459                                                                         $stop_loop{$candidate_file}->{$incumbent_file} = 1;
460
461                                                                         my $link_done = 0;
462
463                                                                         my ($from_file, $to_file, $from_inode, $to_inode, $from_nlink, $to_nlink);
464
465                                                                         # If the candidate has more links than incumbent, replace incumbent with candidate.
466                                                                         # If the incumbent has more links than candidate, replace candidate with incumbent.
467                                                                         # If the link counts are equal, we saw incumbent first, so keep the incumbent.
468                                                                         # "We saw incumbent first" is significant because we explicitly sort the inodes.
469                                                                         # Thank Johannes Niess for this idea.
470                                                                         if ($candidate_nlink > $incumbent_nlink) {
471                                                                                 $from_file = $candidate_file;
472                                                                                 $to_file = $incumbent_file;
473                                                                                 $from_inode = $candidate;
474                                                                                 $to_inode = $incumbent;
475                                                                                 $from_nlink = $candidate_nlink;
476                                                                                 $to_nlink = $incumbent_nlink;
477                                                                         } else {
478                                                                                 $to_file = $candidate_file;
479                                                                                 $from_file = $incumbent_file;
480                                                                                 $to_inode = $candidate;
481                                                                                 $from_inode = $incumbent;
482                                                                                 $to_nlink = $candidate_nlink;
483                                                                                 $from_nlink = $incumbent_nlink;
484                                                                         }
485
486                                                                         eval {
487                                                                                 link_files($from_file, $to_file);
488                                                                                 $link_done = 1;
489                                                                         };
490
491                                                                         if ($@) {
492                                                                                 warn $@;
493                                                                                 $link_errors++;
494
495                                                                                 print STDERR "\t\t\t\t...retrying with swapped from/to files...\n" if $debug;
496                                                                                 $link_retries++;
497
498                                                                                 eval {
499                                                                                         ($from_file, $to_file) = ($to_file, $from_file);
500                                                                                         ($from_inode, $to_inode) = ($to_inode, $from_inode);
501                                                                                         ($from_nlink, $to_nlink) = ($to_nlink, $from_nlink);
502                                                                                         link_files($from_file, $to_file);
503                                                                                         $link_done = 1;
504                                                                                 };
505
506                                                                                 if ($@) {
507                                                                                         warn $@;
508                                                                                         $link_errors++;
509                                                                                 }
510                                                                         }
511
512                                                                         # Note since the files are presumably identical, they both have the same size.
513                                                                         # My random number generator chooses the incumbent's size.
514
515                                                                         if ($link_done) {
516                                                                                 delete $inode_to_file_name{$to_inode}->{$to_file};
517                                                                                 $inode_to_file_name{$from_inode}->{$to_file} = undef;
518                                                                                 $hash_to_inode{$digest} = [ $from_inode ];
519
520                                                                                 $hard_links++;
521                                                                                 if ($to_nlink == 1) {
522                                                                                         $recovered_files++;
523                                                                                         $recovered_bytes += $incumbent_size;
524                                                                                 }
525
526                                                                                 # FIXME:  Now we're really confused for some reason.
527                                                                                 # Start over to rebuild state.
528                                                                                 next link_start;
529                                                                         } else {
530                                                                                 warn "Could not hardlink '$incumbent_file' and '$candidate_file'";
531
532                                                                                 # FIXME:  This is a lame heuristic.  We really need to know if we've
533                                                                                 # tried all possible ways to hardlink the file out of existence first;
534                                                                                 # however, that is complex and only benefits a silly statistic.
535                                                                                 if ($to_nlink == 1 || $from_nlink == 1) {
536                                                                                         $lost_files++;
537                                                                                         $lost_bytes += $incumbent_size;
538                                                                                 }
539                                                                         }
540                                                                 }
541                                                         }
542                                                 }
543                                         }
544                                         $finished = 1;
545                                 }
546                         }
547                         unless ($incumbent_matched) {
548                                 print STDERR "\t\tNew hash entered\n" if $debug;
549                                 push(@$incumbent_list, $candidate);
550                         }
551                 } else {
552                         warn "No digests found for inode $candidate\n";
553                         delete $inode_to_file_name{$candidate};
554                 }
555         }
556
557 end_merge:
558
559         print STDERR '.' if $progress;
560         print STDERR "Merge done.\n" if $debug;
561         undef %inode_to_file_name;
562 }
563
564 while (<FIND>) {
565         my ($weak_key, $dev, $ino, $name) = m/^(\d+ \d+ \d+ \d+ -?[\d.]+) (\d+) (\d+) (.+)\0$/so;
566         die "read error: $!\nLast input line was '$_'" unless defined($name);
567
568         my $inode = format_inode($dev, $ino);
569
570         print STDERR "weak_key=$weak_key inode=$inode name=$name\n" if $debug;
571
572         unless (! (-l $name) && (-f _)) {
573                 warn "Bogon file " . tick_quote($name);
574                 $input_bogons++;
575                 next;
576         }
577
578         $input_links++;
579         merge_files if $weak_key ne $current_key;
580         $current_key = $weak_key;
581
582         $inode_to_file_name{$inode}->{$name} = undef;
583
584         print STDERR "$name\n" if $verbose;
585 }
586
587 merge_files;
588
589 my $stats_blob = <<STATS;
590 compare_bytes           $compare_bytes
591 compare_count           $compare_count
592 compare_differences     $compare_differences
593 compare_errors          $compare_errors
594 hard_links              $hard_links
595 hash_bytes              $hash_bytes
596 hash_errors             $hash_errors
597 hash_files              $hash_files
598 input_bogons            $input_bogons
599 input_files             $input_files
600 input_links             $input_links
601 link_errors             $link_errors
602 link_retries            $link_retries
603 lost_bytes              $lost_bytes
604 lost_files              $lost_files
605 merges_attempted        $merges_attempted
606 recovered_bytes         $recovered_bytes
607 recovered_files         $recovered_files
608 surprises               $surprises
609 trivially_unique        $trivially_unique
610 STATS
611
612 if ($humane) {
613         my $max_num_len = 0;
614
615         sub measure_numbers {
616                 my ($num) = @_;
617                 my $len = length($num);
618                 $len += int( (length($num) - 1) / 3);
619                 $max_num_len = $len if $len > $max_num_len;
620         }
621
622         (my $dummy = $stats_blob) =~ s/\d+/measure_numbers($&)/geos;
623
624         sub space_numbers {
625                 my ($num) = @_;
626                 1 while $num =~ s/(\d)(\d\d\d)( \d\d\d)*$/$1 $2$3/os;
627                 $num = ' ' x ($max_num_len - length($num)) . $num;
628                 return $num;
629         }
630
631         $stats_blob =~ s/\d+/space_numbers($&)/geos;
632 }
633
634 $stats_blob =~ s/([^\n]*\n[^\n]*? )(\s+)( [^\n]*\n)/$1 . ('.' x length($2)) . $3/oemg;
635
636 print STDERR $stats_blob;
637
638 exit(0);
639
640 __END__
641
642 #################################################################################
643 #                     GNU GENERAL PUBLIC LICENSE                                #
644 #                        Version 2, June 1991                                   #
645 #                                                                               #
646 #  Copyright (C) 1989, 1991 Free Software Foundation, Inc.                      #
647 #      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                  #
648 #  Everyone is permitted to copy and distribute verbatim copies                 #
649 #  of this license document, but changing it is not allowed.                    #
650 #                                                                               #
651 #                             Preamble                                          #
652 #                                                                               #
653 #   The licenses for most software are designed to take away your               #
654 # freedom to share and change it.  By contrast, the GNU General Public          #
655 # License is intended to guarantee your freedom to share and change free        #
656 # software--to make sure the software is free for all its users.  This          #
657 # General Public License applies to most of the Free Software                   #
658 # Foundation's software and to any other program whose authors commit to        #
659 # using it.  (Some other Free Software Foundation software is covered by        #
660 # the GNU Library General Public License instead.)  You can apply it to         #
661 # your programs, too.                                                           #
662 #                                                                               #
663 #   When we speak of free software, we are referring to freedom, not            #
664 # price.  Our General Public Licenses are designed to make sure that you        #
665 # have the freedom to distribute copies of free software (and charge for        #
666 # this service if you wish), that you receive source code or can get it         #
667 # if you want it, that you can change the software or use pieces of it          #
668 # in new free programs; and that you know you can do these things.              #
669 #                                                                               #
670 #   To protect your rights, we need to make restrictions that forbid            #
671 # anyone to deny you these rights or to ask you to surrender the rights.        #
672 # These restrictions translate to certain responsibilities for you if you       #
673 # distribute copies of the software, or if you modify it.                       #
674 #                                                                               #
675 #   For example, if you distribute copies of such a program, whether            #
676 # gratis or for a fee, you must give the recipients all the rights that         #
677 # you have.  You must make sure that they, too, receive or can get the          #
678 # source code.  And you must show them these terms so they know their           #
679 # rights.                                                                       #
680 #                                                                               #
681 #   We protect your rights with two steps: (1) copyright the software, and      #
682 # (2) offer you this license which gives you legal permission to copy,          #
683 # distribute and/or modify the software.                                        #
684 #                                                                               #
685 #   Also, for each author's protection and ours, we want to make certain        #
686 # that everyone understands that there is no warranty for this free             #
687 # software.  If the software is modified by someone else and passed on, we      #
688 # want its recipients to know that what they have is not the original, so       #
689 # that any problems introduced by others will not reflect on the original       #
690 # authors' reputations.                                                         #
691 #                                                                               #
692 #   Finally, any free program is threatened constantly by software              #
693 # patents.  We wish to avoid the danger that redistributors of a free           #
694 # program will individually obtain patent licenses, in effect making the        #
695 # program proprietary.  To prevent this, we have made it clear that any         #
696 # patent must be licensed for everyone's free use or not licensed at all.       #
697 #                                                                               #
698 #   The precise terms and conditions for copying, distribution and              #
699 # modification follow.                                                          #
700 #                                                                               #
701 #                     GNU GENERAL PUBLIC LICENSE                                #
702 #    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION            #
703 #                                                                               #
704 #   0. This License applies to any program or other work which contains         #
705 # a notice placed by the copyright holder saying it may be distributed          #
706 # under the terms of this General Public License.  The "Program", below,        #
707 # refers to any such program or work, and a "work based on the Program"         #
708 # means either the Program or any derivative work under copyright law:          #
709 # that is to say, a work containing the Program or a portion of it,             #
710 # either verbatim or with modifications and/or translated into another          #
711 # language.  (Hereinafter, translation is included without limitation in        #
712 # the term "modification".)  Each licensee is addressed as "you".               #
713 #                                                                               #
714 # Activities other than copying, distribution and modification are not          #
715 # covered by this License; they are outside its scope.  The act of              #
716 # running the Program is not restricted, and the output from the Program        #
717 # is covered only if its contents constitute a work based on the                #
718 # Program (independent of having been made by running the Program).             #
719 # Whether that is true depends on what the Program does.                        #
720 #                                                                               #
721 #   1. You may copy and distribute verbatim copies of the Program's             #
722 # source code as you receive it, in any medium, provided that you               #
723 # conspicuously and appropriately publish on each copy an appropriate           #
724 # copyright notice and disclaimer of warranty; keep intact all the              #
725 # notices that refer to this License and to the absence of any warranty;        #
726 # and give any other recipients of the Program a copy of this License           #
727 # along with the Program.                                                       #
728 #                                                                               #
729 # You may charge a fee for the physical act of transferring a copy, and         #
730 # you may at your option offer warranty protection in exchange for a fee.       #
731 #                                                                               #
732 #   2. You may modify your copy or copies of the Program or any portion         #
733 # of it, thus forming a work based on the Program, and copy and                 #
734 # distribute such modifications or work under the terms of Section 1            #
735 # above, provided that you also meet all of these conditions:                   #
736 #                                                                               #
737 #     a) You must cause the modified files to carry prominent notices           #
738 #     stating that you changed the files and the date of any change.            #
739 #                                                                               #
740 #     b) You must cause any work that you distribute or publish, that in        #
741 #     whole or in part contains or is derived from the Program or any           #
742 #     part thereof, to be licensed as a whole at no charge to all third         #
743 #     parties under the terms of this License.                                  #
744 #                                                                               #
745 #     c) If the modified program normally reads commands interactively          #
746 #     when run, you must cause it, when started running for such                #
747 #     interactive use in the most ordinary way, to print or display an          #
748 #     announcement including an appropriate copyright notice and a              #
749 #     notice that there is no warranty (or else, saying that you provide        #
750 #     a warranty) and that users may redistribute the program under             #
751 #     these conditions, and telling the user how to view a copy of this         #
752 #     License.  (Exception: if the Program itself is interactive but            #
753 #     does not normally print such an announcement, your work based on          #
754 #     the Program is not required to print an announcement.)                    #
755 #                                                                               #
756 # These requirements apply to the modified work as a whole.  If                 #
757 # identifiable sections of that work are not derived from the Program,          #
758 # and can be reasonably considered independent and separate works in            #
759 # themselves, then this License, and its terms, do not apply to those           #
760 # sections when you distribute them as separate works.  But when you            #
761 # distribute the same sections as part of a whole which is a work based         #
762 # on the Program, the distribution of the whole must be on the terms of         #
763 # this License, whose permissions for other licensees extend to the             #
764 # entire whole, and thus to each and every part regardless of who wrote it.     #
765 #                                                                               #
766 # Thus, it is not the intent of this section to claim rights or contest         #
767 # your rights to work written entirely by you; rather, the intent is to         #
768 # exercise the right to control the distribution of derivative or               #
769 # collective works based on the Program.                                        #
770 #                                                                               #
771 # In addition, mere aggregation of another work not based on the Program        #
772 # with the Program (or with a work based on the Program) on a volume of         #
773 # a storage or distribution medium does not bring the other work under          #
774 # the scope of this License.                                                    #
775 #                                                                               #
776 #   3. You may copy and distribute the Program (or a work based on it,          #
777 # under Section 2) in object code or executable form under the terms of         #
778 # Sections 1 and 2 above provided that you also do one of the following:        #
779 #                                                                               #
780 #     a) Accompany it with the complete corresponding machine-readable          #
781 #     source code, which must be distributed under the terms of Sections        #
782 #     1 and 2 above on a medium customarily used for software interchange; or,  #
783 #                                                                               #
784 #     b) Accompany it with a written offer, valid for at least three            #
785 #     years, to give any third party, for a charge no more than your            #
786 #     cost of physically performing source distribution, a complete             #
787 #     machine-readable copy of the corresponding source code, to be             #
788 #     distributed under the terms of Sections 1 and 2 above on a medium         #
789 #     customarily used for software interchange; or,                            #
790 #                                                                               #
791 #     c) Accompany it with the information you received as to the offer         #
792 #     to distribute corresponding source code.  (This alternative is            #
793 #     allowed only for noncommercial distribution and only if you               #
794 #     received the program in object code or executable form with such          #
795 #     an offer, in accord with Subsection b above.)                             #
796 #                                                                               #
797 # The source code for a work means the preferred form of the work for           #
798 # making modifications to it.  For an executable work, complete source          #
799 # code means all the source code for all modules it contains, plus any          #
800 # associated interface definition files, plus the scripts used to               #
801 # control compilation and installation of the executable.  However, as a        #
802 # special exception, the source code distributed need not include               #
803 # anything that is normally distributed (in either source or binary             #
804 # form) with the major components (compiler, kernel, and so on) of the          #
805 # operating system on which the executable runs, unless that component          #
806 # itself accompanies the executable.                                            #
807 #                                                                               #
808 # If distribution of executable or object code is made by offering              #
809 # access to copy from a designated place, then offering equivalent              #
810 # access to copy the source code from the same place counts as                  #
811 # distribution of the source code, even though third parties are not            #
812 # compelled to copy the source along with the object code.                      #
813 #                                                                               #
814 #   4. You may not copy, modify, sublicense, or distribute the Program          #
815 # except as expressly provided under this License.  Any attempt                 #
816 # otherwise to copy, modify, sublicense or distribute the Program is            #
817 # void, and will automatically terminate your rights under this License.        #
818 # However, parties who have received copies, or rights, from you under          #
819 # this License will not have their licenses terminated so long as such          #
820 # parties remain in full compliance.                                            #
821 #                                                                               #
822 #   5. You are not required to accept this License, since you have not          #
823 # signed it.  However, nothing else grants you permission to modify or          #
824 # distribute the Program or its derivative works.  These actions are            #
825 # prohibited by law if you do not accept this License.  Therefore, by           #
826 # modifying or distributing the Program (or any work based on the               #
827 # Program), you indicate your acceptance of this License to do so, and          #
828 # all its terms and conditions for copying, distributing or modifying           #
829 # the Program or works based on it.                                             #
830 #                                                                               #
831 #   6. Each time you redistribute the Program (or any work based on the         #
832 # Program), the recipient automatically receives a license from the             #
833 # original licensor to copy, distribute or modify the Program subject to        #
834 # these terms and conditions.  You may not impose any further                   #
835 # restrictions on the recipients' exercise of the rights granted herein.        #
836 # You are not responsible for enforcing compliance by third parties to          #
837 # this License.                                                                 #
838 #                                                                               #
839 #   7. If, as a consequence of a court judgment or allegation of patent         #
840 # infringement or for any other reason (not limited to patent issues),          #
841 # conditions are imposed on you (whether by court order, agreement or           #
842 # otherwise) that contradict the conditions of this License, they do not        #
843 # excuse you from the conditions of this License.  If you cannot                #
844 # distribute so as to satisfy simultaneously your obligations under this        #
845 # License and any other pertinent obligations, then as a consequence you        #
846 # may not distribute the Program at all.  For example, if a patent              #
847 # license would not permit royalty-free redistribution of the Program by        #
848 # all those who receive copies directly or indirectly through you, then         #
849 # the only way you could satisfy both it and this License would be to           #
850 # refrain entirely from distribution of the Program.                            #
851 #                                                                               #
852 # If any portion of this section is held invalid or unenforceable under         #
853 # any particular circumstance, the balance of the section is intended to        #
854 # apply and the section as a whole is intended to apply in other                #
855 # circumstances.                                                                #
856 #                                                                               #
857 # It is not the purpose of this section to induce you to infringe any           #
858 # patents or other property right claims or to contest validity of any          #
859 # such claims; this section has the sole purpose of protecting the              #
860 # integrity of the free software distribution system, which is                  #
861 # implemented by public license practices.  Many people have made               #
862 # generous contributions to the wide range of software distributed              #
863 # through that system in reliance on consistent application of that             #
864 # system; it is up to the author/donor to decide if he or she is willing        #
865 # to distribute software through any other system and a licensee cannot         #
866 # impose that choice.                                                           #
867 #                                                                               #
868 # This section is intended to make thoroughly clear what is believed to         #
869 # be a consequence of the rest of this License.                                 #
870 #                                                                               #
871 #   8. If the distribution and/or use of the Program is restricted in           #
872 # certain countries either by patents or by copyrighted interfaces, the         #
873 # original copyright holder who places the Program under this License           #
874 # may add an explicit geographical distribution limitation excluding            #
875 # those countries, so that distribution is permitted only in or among           #
876 # countries not thus excluded.  In such case, this License incorporates         #
877 # the limitation as if written in the body of this License.                     #
878 #                                                                               #
879 #   9. The Free Software Foundation may publish revised and/or new versions     #
880 # of the General Public License from time to time.  Such new versions will      #
881 # be similar in spirit to the present version, but may differ in detail to      #
882 # address new problems or concerns.                                             #
883 #                                                                               #
884 # Each version is given a distinguishing version number.  If the Program        #
885 # specifies a version number of this License which applies to it and "any       #
886 # later version", you have the option of following the terms and conditions     #
887 # either of that version or of any later version published by the Free          #
888 # Software Foundation.  If the Program does not specify a version number of     #
889 # this License, you may choose any version ever published by the Free Software  #
890 # Foundation.                                                                   #
891 #                                                                               #
892 #   10. If you wish to incorporate parts of the Program into other free         #
893 # programs whose distribution conditions are different, write to the author     #
894 # to ask for permission.  For software which is copyrighted by the Free         #
895 # Software Foundation, write to the Free Software Foundation; we sometimes      #
896 # make exceptions for this.  Our decision will be guided by the two goals       #
897 # of preserving the free status of all derivatives of our free software and     #
898 # of promoting the sharing and reuse of software generally.                     #
899 #                                                                               #
900 #                             NO WARRANTY                                       #
901 #                                                                               #
902 #   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY    #
903 # FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN      #
904 # OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES        #
905 # PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED    #
906 # OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF          #
907 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS     #
908 # TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE        #
909 # PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,      #
910 # REPAIR OR CORRECTION.                                                         #
911 #                                                                               #
912 #   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING   #
913 # WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR           #
914 # REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,    #
915 # INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING   #
916 # OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED     #
917 # TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY      #
918 # YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER    #
919 # PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE         #
920 # POSSIBILITY OF SUCH DAMAGES.                                                  #
921 #                                                                               #
922 #                      END OF TERMS AND CONDITIONS                              #
923 #                                                                               #
924 #             How to Apply These Terms to Your New Programs                     #
925 #                                                                               #
926 #   If you develop a new program, and you want it to be of the greatest         #
927 # possible use to the public, the best way to achieve this is to make it        #
928 # free software which everyone can redistribute and change under these terms.   #
929 #                                                                               #
930 #   To do so, attach the following notices to the program.  It is safest        #
931 # to attach them to the start of each source file to most effectively           #
932 # convey the exclusion of warranty; and each file should have at least          #
933 # the "copyright" line and a pointer to where the full notice is found.         #
934 #                                                                               #
935 #     <one line to give the program's name and a brief idea of what it does.>   #
936 #     Copyright (C) <year>  <name of author>                                    #
937 #                                                                               #
938 #     This program is free software; you can redistribute it and/or modify      #
939 #     it under the terms of the GNU General Public License as published by      #
940 #     the Free Software Foundation; either version 2 of the License, or         #
941 #     (at your option) any later version.                                       #
942 #                                                                               #
943 #     This program is distributed in the hope that it will be useful,           #
944 #     but WITHOUT ANY WARRANTY; without even the implied warranty of            #
945 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
946 #     GNU General Public License for more details.                              #
947 #                                                                               #
948 #     You should have received a copy of the GNU General Public License         #
949 #     along with this program; if not, write to the Free Software               #
950 #     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA #
951 #                                                                               #
952 #                                                                               #
953 # Also add information on how to contact you by electronic and paper mail.      #
954 #                                                                               #
955 # If the program is interactive, make it output a short notice like this        #
956 # when it starts in an interactive mode:                                        #
957 #                                                                               #
958 #     Gnomovision version 69, Copyright (C) year  name of author                #
959 #     Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. #
960 #     This is free software, and you are welcome to redistribute it             #
961 #     under certain conditions; type `show c' for details.                      #
962 #                                                                               #
963 # The hypothetical commands `show w' and `show c' should show the appropriate   #
964 # parts of the General Public License.  Of course, the commands you use may     #
965 # be called something other than `show w' and `show c'; they could even be      #
966 # mouse-clicks or menu items--whatever suits your program.                      #
967 #                                                                               #
968 # You should also get your employer (if you work as a programmer) or your       #
969 # school, if any, to sign a "copyright disclaimer" for the program, if          #
970 # necessary.  Here is a sample; alter the names:                                #
971 #                                                                               #
972 #   Yoyodyne, Inc., hereby disclaims all copyright interest in the program      #
973 #   `Gnomovision' (which makes passes at compilers) written by James Hacker.    #
974 #                                                                               #
975 #   <signature of Ty Coon>, 1 April 1989                                        #
976 #   Ty Coon, President of Vice                                                  #
977 #                                                                               #
978 # This General Public License does not permit incorporating your program into   #
979 # proprietary programs.  If your program is a subroutine library, you may       #
980 # consider it more useful to permit linking proprietary applications with the   #
981 # library.  If this is what you want to do, use the GNU Library General         #
982 # Public License instead of this License.                                       #
983 #################################################################################