#!/usr/bin/perl use Try::Tiny; use File::Find; use Time::HiRes qw.tv_interval gettimeofday.; use Archive::Extract; my $dir = shift; die "You must supply a diretory" unless $dir && -d $dir; # # Estas estruturas de dados precisam de limpeza. # my %kept; # # 1. Tipos que reconhecemos como bons my @known = qw.application/pdf text/html.; # # 2. Tipos que reconhecemos como maus my @todelete = (qw!image/jpeg image/svg+xml image/x-ico image/gif image/png image/tiff image/x-ms-bmp text/x-c++ text/x-pascal text/x-c application/x-font-ttf application/vnd.ms-cab-compressed audio/midi audio/mpeg application/vnd.rn-realmedia application/x-shockwave-flash application/octet-stream application/vnd.ms-excel application/x-msaccess application/vnd.ms-office application/msword video/mp4 video/x-flv video/quicktime!, 'CDF V2 Document, No summary info'); # # 3. Tipos duvidosos, que apagamos baseando-nos na extensão my %extbymime = ( 'text/plain' => qr/(rss|css|log|js)/ ); my @zips = qw.application/zip application/x-compress.; # # 4. Initialize structures my (%known, %todelete, %zip); @known{@known} = @known; @todelete{@todelete} = @todelete; @zip{@zips} = @zips; my $foundzips = 0; ## ------------ our ($deleted, $total) = (0, 0); my $t0 = [gettimeofday]; finddepth \&processor => $dir; my $elapsed = tv_interval ( $t0, [gettimeofday]); ## ------------ print STDERR "\r",(" " x 75),"\n"; if (%kept) { print STDERR "Unknown files:",(" " x 60),"\n"; print STDERR "\n"; for my $type (keys %kept) { printf STDERR "%20s => %d\n\t(%s,...)\n" => ($type, scalar(@{$kept{$type}}), $kept{$type}[0]); } } print STDERR "\nFound some zip files. Probably a good idea to rerun.\n" if $foundzips; print STDERR "\nDeleted $deleted files from a total of $total files ($elapsed seconds)\n\n"; ## ------------ sub processor { my $filename = $_; ++$total; if (-d $_) { if (empty_dir($_)) { printf STDERR "\r * %-63s [deleted]\n" => $_; rmdir $_ } } else { $filename =~ s/^.// while length($filename) > 60; $filename = "...$filename" unless $_ eq $filename; printf STDERR " + %-63s" => $filename; my $quoted = $_; $quoted =~ s/'/'\\''/g; chomp(my $type = `file -bi '$quoted'`); $type =~ s/;.*$//; if (-z $_ || exists($todelete{$type}) || (exists($extbymime{$type}) && /\.$extbymime{$type}$/)) { ++$deleted; unlink $_; print STDERR " [deleted]\n"; } else { if (exists($zip{$type})) { $foundzips++; try { my $ae = Archive::Extract->new( archive => $_ ); $ae->extract; print STDERR " [extracted]\n"; } catch { print STDERR " [fail/deleted]\n"; }; unlink $_; } elsif (!exists($known{$type})) { push @{$kept{$type}} => $File::Find::name } print STDERR "\r"; } } } sub empty_dir { my $x; opendir $x => shift; return !scalar(grep !/^\.{1,2}$/ => readdir($x)); }