Hey all, I'm writing a perl script to fix corrupted sendmail mailboxes. Basically, the very first thing in a mailbox has to be the word From, otherwise users can't log in. From time to time bits of garbage get entered in to the start of the mailbox files so I've written a perl script to remove those line of garbage.

The script work fine on my machine (debian) but when I stick it on my mail server (BSD) it throws a hissy fit, and instead of removing garbage it will sometimes work and sometimes wipe the entire mailbox file. It's kind of driving me nuts so any help would be really appreciated..

Here's the script :

# -------------------------------------------------------------------- +---------------------------------------------------------- # This perl script is designed to run through all of the mailboxes in +/mnt/mail on # mail.visp.co.nz removing all corrupted data from the start of any co +rrupted # mailboxes. # # Coded By : Oliver Sneyd # When : February 2006 # Contact : oliver.sneyd@mail.iconz.net # -------------------------------------------------------------------- +--------------------------------------------------------- # Include the file statistics object so that the script can check the +filesizes of each mailbox use File::stat; # Path to the maildir, /mnt/mail for mail.visp.co.nz $path = "./mail/"; $backupPath = "./backup/"; # Open up a directory handle print "\n\tGenerating mailbox list ...\n"; opendir(MAILDIR, $path); # Read the names of each entry in the maildir in to an arrays @filenames = readdir(MAILDIR); # Create an array to hold the mailboxes my @mailboxes = (); # Loop through the results returned by the directory handle for($i = 0; $i < @filenames; $i++) { # If the result returned by directory handle is NOT a directory if(not(-d ($path . $filenames[$i]))) { # Work out the file-size of the current mailbox $size = stat($path . $filenames[$i]); # If the filesize is greater than 0, add it to the mailbox lis +t if($size->size > 0) { push(@mailboxes, $filenames[$i]); } } } # Loop through the mailboxes print "\tChecking for corrupt mailboxes ...\n\n"; while(@mailboxes > 0) { $mailbox = pop(@mailboxes); checkMailbox($mailbox); } print "\n\tDone.\n\n"; # Close the directory handle closedir(MAILDIR); # ------------------------------------------------------------ FUNCTIO +NS --------------------------------------------------- sub checkMailbox { # Set a corrupt variable to be true $corrupt = 1; # Loop untill corrupt is false $initial = 0; while($corrupt == 1) { # Open up the mailbox open(MAILBOX, ($path . $_[0])); # Read in the first line of the mailbox $line = <MAILBOX>; # Get the index of the string "From" $idx = index($line, "From"); # If the index of "From" is 0, the mailbox isn't corrupted any + more if($idx == 0) { # So set corrupted to false $corrupt = 0; } else { # Make a bacukp of the corrupted mailbox, just in case if($initial == 0) { print "\tFixing $_[0] ...\n"; system("cp $path" . $_[0] . " " . $backupPath . "."); $initial = 1; } # And remove the first line of the mailbox system("sed -e '1d' $path" . "$_[0] | more > $path" . $_[ +0]); } # Close the mailbox close(MAILBOX); } }

2006-02-03 Retitled by planetscape, as per Monastery guidelines
Original title: 'Strange Problem'


In reply to Strange problem trying to clean garbage from start of mailbox file by capoeiraolly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.