Monthly Archives: October 2012

Quick and dirty fix for corrupted mailbox

I run an old mail server using exim4 which stores its files in mbox format.

My inbox isn’t exactly small these days and I get a large volume of mail, mostly spam unfortunately. Sometimes exim4 has a glitch and corrupts the mailbox. The symptoms are always the same, the start of the mbox file is messed up with some random binary garbage. It always occurs at the very start of the file like this example:

LÍfO@+Yðe
~Ó¹ôêÞ:[çknñPùJ꣺U´-ëüµOi;õ¨ÿ-R$!8öµD¨%wÇßC`
(MʱÔ)wiÎ}1Þméõx¤¾GÊ6DFx£MVLZ¦B{TåQ}­ÃÈÍ ©F!¿ã¶Ùìããé!÷EúýD»Ü>W~sÃ.131.81])
        by adamserver with esmtp (Exim 4.69)
        (envelope-from )
        id 1O7eM3-000229-SH
        for adam@siliconsparrow.com; Fri, 30 Apr 2010 10:45:20 +1000
Received: by p14.nnj.simupt.com (Postfix, from userid 33)
        id 41A4CA7E0; Fri, 30 Apr 2010 00:36:38 +0000 (UTC)
To: adam@siliconsparrow.com
Subject: Hello
...

Now trying to edit a 2GB mbox file using vi or nano is akin to torture so here’s my quick and dirty method which simply deletes the first message in the inbox using the commandline tool awk. I have used this technique 5 or 6 times in the last year with success.

NOTE: Make sure you shut down your SMTP server, POP3 and IMAP servers before you run this.

cd /var/spool/mail
mv adam adam-corrupt
tail -c +500 adam-corrupt | awk '/^From /,0' > adam
chown adam adam

How to cycle through the values of an Enum in Java

Here’s a quick little trick. I needed to advance my variable to the next value in an Enum. I found this can be done using the values and ordinal built-in members. This version wraps around to the beginning when you reach the end.

Here’s the code:

private enum Direction { NORTH, EAST, SOUTH, WEST;
public Direction getNext() {
return values()[(ordinal() + 1) % values().length];
}
};

Here’s an example of how to use it:

private Direction currentDirection = Direction.NORTH;

public void turn() {
currentDirection = currentDirection.getNext();
}