The Maildir e-mail format is a common way of storing e-mail messages, where each message is kept in a separate file with a unique name, and each folder is a directory. The local filesystem handles file locking as messages are added, moved and deleted. A major design goal of Maildir is to eliminate program code having to handle locking, which is often difficult.
A Maildir directory (often named Maildir
) usually has three subdirectories named tmp
, new
, and cur
.
The original Maildir specification was written by Daniel J. Bernstein, the author of qmail, djbdns, and other software. Although the original specification was written specifically for Bernstein's qmail, it is general enough to be implemented in many programs.
Sam Varshavchik, the author of the Courier Mail Server and other software, wrote an extension to the Maildir format called Maildir++ to support subfolders and mail quotas. Maildir++ directories contain subdirectories with names that start with a '.' (dot) that are also Maildir++ folders. This extension is not a violation of the Maildir specification, which explicitly provides for the possibility to add more than tmp, new, cur to a maildir.
The program that delivers an email message, a mail delivery agent, writes it to a file in the tmp
directory with a unique filename. Algorithms for generating unique filenames can combine time, host name, and a number of pseudo-random parameters to ensure uniqueness, taking into account that multiple messages can be delivered at the same time.
The delivery process stores the message in the maildir by creating and writing to tmp/unique
, and then moving this file to new/unique
. The moving can be done using rename, which is atomic in many systems. Alternatively, it can be done by hard linking the file to new
and then unlinking the file from tmp
. Any leftover file will eventually be deleted. This sequence guarantees that a maildir-reading program will not see a partially written message. There can be multiple programs reading a maildir at the same time. They range from mail user agents (MUAs) which access the server's file system directly, through or servers acting on behalf or remote MUAs, to utilities such as biff and rsync, which may or may not be aware of the maildir structure. Readers should never look in tmp
.