rtoss - Blame information for rev 215

Subversion Repositories:
Rev:
Rev Author Line No. Line
84 roytam 1 #!/usr/bin/perl -w
2  
3 use strict;
4 use File::Find;
5 use Digest::MD5;
212 roytam 6  
7 my $printSize = 1;
8  
9  
84 roytam 10 my %files;
211 roytam 11 my $totalpossibledupfiles;
212 roytam 12 my $totalpossibledupfilessize;
211 roytam 13 my $possibledupfilescount;
212 roytam 14 my $possibledupfilescountsize;
215 roytam 15  
16 ($#ARGV == 0) && push @ARGV, ".";
84 roytam 17 print STDERR "Searching files...\n";
215 roytam 18 find(\&check_file, @ARGV);
84 roytam 19  
20 local $" = ", ";
211 roytam 21  
22 $possibledupfilescount = 0;
23 $totalpossibledupfiles = 0;
212 roytam 24 $totalpossibledupfilessize = 0;
25 $possibledupfilescountsize = 0;
211 roytam 26 foreach my $size (keys %files) {
27   next unless @{$files{$size}} > 1;
28   $totalpossibledupfiles += $#{$files{$size}}+1;
212 roytam 29   $totalpossibledupfilessize += $size*($#{$files{$size}}+1);
211 roytam 30 }
31  
84 roytam 32 foreach my $size (sort {$b <=> $a} keys %files) {
33   next unless @{$files{$size}} > 1;
34   my %md5;
35   my $scnt = 1;
36   foreach my $file (@{$files{$size}}) {
212 roytam 37     ++$possibledupfilescount;
38     $possibledupfilescountsize+=$size;
39     if($printSize) {
40       print STDERR "[",properSize($possibledupfilescountsize),"/",properSize($totalpossibledupfilessize)," ",(sprintf "%.2f", $possibledupfilescountsize*100/$totalpossibledupfilessize),"%] ";
41     } else {
42       print STDERR "[$possibledupfilescount/$totalpossibledupfiles ",(sprintf "%.2f", $possibledupfilescount*100/$totalpossibledupfiles),"%] ";
43     }
44     print STDERR "($size ",$scnt++,"/",$#{$files{$size}}+1,") Hashing $file ...\n";
84 roytam 45     open(FILE, $file) or next;
46     binmode(FILE);
47     push @{$md5{Digest::MD5->new->addfile(*FILE)->hexdigest}},$file;
48   }
49   foreach my $hash (keys %md5) {
50     next unless @{$md5{$hash}} > 1;
51     if($size) {
52       foreach my $file (@{$md5{$hash}}) {
53         print substr($hash,0,8),"\t$size\t$file\n";
54       }
55     }
56   }
57 }
58  
59 sub check_file {
60   -f && push @{$files{(stat(_))[7]}}, $File::Find::name;
61 }
62  
212 roytam 63 sub properSize {
64  my $size = shift;
65  my $unit = shift || 3;
66  my $dp   = shift || 2;
67  
68  my @units = ("","KB","MB","GB","TB","PB","EB");
69  my $i = 0;
70  my $out = "";
71  
72  for($i = 0; $i < $unit && $size > 1024; $i++) {
73   $size /= 1024;
74  }
75  
76  $out = sprintf("%.*f",$i?$dp:0,$size);
77  
78  # add thousand separators
79  $out = reverse $out;
80  $out =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g;
81  $out = reverse $out;
82  
83  $out.$units[$i];
84 }