Advertisement
mikelieman

bin/image-db-analysis-01.pl

Dec 7th, 2014
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.75 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.12;
  4. use warnings;
  5. use DateTime;
  6. use Text::CSV;
  7.  
  8. my $csv = Text::CSV->new ( { binary => 1 } )  
  9.         or die "Cannot use CSV: ".Text::CSV->error_diag ();
  10.  
  11. $csv->column_names ( qw( fullpath filename mtime size digest ) );
  12.  
  13. my $database = "fileinfo.db";
  14.  
  15. open my $FH, q{<:encoding(utf8)}, $database or die "$database: $!";
  16.  
  17. say "connected to database '$database' successfully";
  18.  
  19. my %files_by_sha256;
  20. my $f_sha256ref = \%files_by_sha256;
  21.  
  22. while ( my $hr = $csv->getline_hr( $FH ) ) {
  23.  
  24. #       next if $hr->{fullpath} =~ m/tmp\.oldest/;
  25.    
  26.     push @{ $f_sha256ref->{ $hr->{digest} }}, $hr ;
  27.  
  28. };
  29.  
  30. $csv->eof or $csv->error_diag();
  31. close $FH;
  32.  
  33. foreach my $key ( sort keys %files_by_sha256 ) {
  34.  
  35.     next if scalar @{ $f_sha256ref->{$key} } == 1;
  36.  
  37.     my %agesort;
  38.  
  39.     foreach my $hr ( @{ $f_sha256ref->{$key} } ) {
  40.  
  41.                 my $timestamp = DateTime->from_epoch( epoch => $hr->{mtime} );
  42.  
  43.         $agesort{ $hr->{fullpath} }->{'DATETIME'} = $timestamp->datetime();
  44.         $agesort{ $hr->{fullpath} }->{'MTIME'}    = $hr->{mtime};
  45.         $agesort{ $hr->{fullpath} }->{'SIZE'}     = $hr->{size};
  46.         $agesort{ $hr->{fullpath} }->{'YEAR'}     = $timestamp->year();
  47.  
  48.     }
  49.  
  50.     # Now loop over the sorted list by mtime.
  51.     my $first_mtime = 0;
  52.  
  53.     foreach my $instance (
  54.         sort { $agesort{$a}->{'MTIME'} <=> $agesort{$b}->{'MTIME'} }
  55.         keys %agesort )
  56.     {
  57.  
  58.         say "$agesort{$instance}->{'DATETIME'} $instance";
  59.  
  60.         if ( $first_mtime == 0 ) {
  61.             $first_mtime = $agesort{$instance}->{'MTIME'};
  62.         }
  63.         else {
  64. #            say q{rm -v "} . $agesort{$instance}->{'FILENAME'} . q{"};
  65.         }
  66.  
  67.     }
  68.  
  69.     say "\n";
  70.  
  71. }    # foreach $key
  72.  
  73. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement