#! /usr/bin/perl # # This script looks at all files in the directories that contain # references to attachments and compares this to the file in the # Attach directory. # $debug = 0; $verbose = 0; $outname = ""; $list_orphans = 0; $move_orphans = 0; $LISTFD = STDOUT; $attachdir = "Attach"; $newdir = "Attach-old2"; sub ListAttachments { my $filename = $_[0]; print "Checking $filename...\n" if $verbose; open (FD, "<$filename" ) || die "Could not open $filename\n"; # There are a number of formats for the lising of an attachment: # ^Content-Disposition: attachment;[\s\n]filename="?.*"? # Note the location of the allowed newline, and the optional quotes # May also have a : followed by other data, e.g., # : CHEP summary.doc: 00000001,1fe..... # (but this may be an invalid name that does not have an # attachment?) # This should match an entry of the form # ^Attachment Converted: "?name"? # Note that the quotes are optional while () { if (/^Attachment [Cc]onverted:\s+(\S.*)/) { $candidate = $1; if ($candidate =~ /\"([^\"]*)\"/) { $attachment = $1; } else { $attachment = $candidate; } print "$attachment\n" if $verbose; # Remove any directories $attachment =~ /.*\\([^\\]*)/; $shortname = $1; print $LISTFD "$shortname\n" if $verbose; $allfiles{$shortname} = 1; } # elsif (/Content-[Dd]isposition:\s+attachment;\s+filename=\"?(.*)\"?/) { # $attachment = $1; # print "$attachment\n" if $verbose; # # Remove any directories # $attachment =~ /.*\\([^\\]*)/; # $shortname = $1; # print $LISTFD "$shortname\n" if $verbose; # $allfiles{$shortname} = 1; # } } } sub ProcessDir { my $dirname = $_[0]; print "Processing directory $dirname\n" if $verbose; opendir( DIR, $dirname ) || die "Could not open directory $dirname\n"; my @files = readdir( DIR ); closedir DIR; foreach my $file (@files) { if ($file eq "." || $file eq "..") { next; } if (-d "$dirname/$file") { &ProcessDir( "$dirname/$file" ); } elsif ($file =~ /\.mbx/) { print "# file $dirname/$file\n" if $debug; &ListAttachments( "$dirname/$file" ); } } } # # -outfile=file-for-list -attach=dir -move -movedir=dir directory-to-check # where -move moves orphans foreach $_ (@ARGV) { if (/^-outfile=(.*)/) { $outname = $1; open( LISTFD, ">$outname" ) || die "could not open $outname\n"; $LISTFD = LISTFD; next; } if (/^-attach=(.*)/) { $attachdir = $1; opendir (DIRIN,$attachdir); foreach $file (readdir(DIRIN)) { $allfiles{$file} = 0; } closedir (DIRIN); $list_orphans = 1; next; } if (/^-movedir=(.*)/) { $newdir = $1; $move_orphans = 1; next; } # # Need to replace this with a read of the $attachdir directory # if (/^-inlist=(.*)/) { # $infile = $1; # open (FDIN, "<$infile" ) || die "Cannot open $infile\n"; # $list_orphans = 1; # while () { # s/\r\n//; # $allfiles{$_} = 0; # } # next; # } if (/^-move/) { $move_orphans = 1; next; } if (/^-quiet/) { $verbose = 0; $list_orphans = 0; next; } $filename = $_; print "Looking at $filename\n" if $debug; if (-d $filename) { print "Entering directory $filename...\n" if $debug; &ProcessDir( $filename ); } else { print "# file $filename\n" if $debug; &ListAttachments( $filename ); } } if ($list_orphans) { my $totsize = 0; foreach $file (keys(%allfiles)) { if ($allfiles{$file} == 0) { ($d,$d,$d,$d,$d,$d,$d,$size,$d,$d,$d,$d,$d) = stat "$attachdir/$file"; print "Orphan\t\"$file\"\t$size\n"; $totsize += $size; } } print "Total size of orphans = $totsize bytes\n"; } if ($move_orphans) { if (! -d $newdir) { mkdir $newdir || die "Cannot make $newdir"; } foreach $file (keys(%allfiles)) { if ($allfiles{$file} == 0) { if (-f "$attachdir/$file") { rename "$attachdir/$file","$newdir/$file"; } } } }