The Maildir e-mail format is a common way of storing email messages on a file system, rather than in a database. Each message is assigned a file with a unique name, and each mail folder is a file system directory containing these files. Maildir was designed by Daniel J. Bernstein circa 1995, with a major goal of eliminating the need for program code to handle file locking and unlocking through use of the local filesystem. Maildir design reflects the fact that the only operations valid for an email message are that it be created, deleted, or have its status changed in some way.
Specifications A Maildir directory (often named Maildir) usually has three subdirectories named tmp, new, and cur.
The tmp subdirectory temporarily stores e-mail messages that are in the process of being delivered. This subdirectory may also store other kinds of temporary files. The new subdirectory stores messages that have been delivered, but have not yet been seen by any mail application. The cur subdirectory stores messages that have already been seen by mail applications.
Maildir++ Sam Varshavchik, the author of the Courier Mail Server and other software, defined the Maildir++ extension to the Maildir format to support subfolders and mail quotas. Maildir++ directories contain subdirectories with names that start with a '.' (dot) which are also Maildir++ folders. The extension complies with the original Maildir specification, which allows for subdirectories in addition to tmp, new and cur.
Technical operation A mail delivery agent is a program that delivers an email message into a Maildir. The mail delivery agent creates a new file with a unique filename in the tmp directory. At the time of its invention guaranteeing unique filenames efficiently was difficult. The original qmail algorithm for unique names was:
read the current Unix time read the current process identifier (PID) read the current hostname concatenate the above three values into a string separated by the period character; this is the new filename if stat() reports that the filename exists, then wait two seconds go to previous step until the filename does not exist create a file with the unique filename and write the message contents to the new file By 2000, the qmail author recommended in an updated specification to append the value of a per-process counter to the PID, whose value should be incremented after each delivery. The rate-limiting recommendation to "wait two seconds" was dropped. By 2003, the recommendations had been further amended to require that instead of the PID and counter, the middle part of the filename should be created by "concatenating enough of the following strings to guarantee uniqueness" even in the face of multiple simultaneous deliveries to the same maildir from one or more processes:
#n, where n is (in hexadecimal) the output of the operating system's unix_sequencenumber() system call, which returns a number that increases by 1 every time it is called, starting from 0 after reboot. Xn, where n is (in hexadecimal) the output of the operating system's unix_bootnumber() system call, which reports the number of times that the system has been booted. Together with #, this guarantees uniqueness; unfortunately, most operating systems don't support unix_sequencenumber() and unix_bootnumber(). Rn, where n is (in hexadecimal) the output of the operating system's unix_cryptorandomnumber() system call or an equivalent source, such as /dev/urandom. Unfortunately, some operating systems don't include cryptographic random number generators. In, where n is (in hexadecimal) the UNIX inode number of this file. Unfortunately, inode numbers aren't always available through NFS. Vn, where n is (in hexadecimal) the UNIX device number of this file. Unfortunately, device numbers aren't always available through NFS. (Device numbers are also not helpful with the standard UNIX filesystem: a maildir has to be within a single UNIX device for link() and rename() to work.) Mn, where n is (in decimal) the microsecond counter from the same gettimeofday() used for the left part of the unique name. Pn, where n is (in decimal) the process ID. Qn, where n is (in decimal) the number of deliveries made by this process.
… excerpt ends here. Continue reading the full article.

