Maildir Explained

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 is that it be created, deleted or have its status changed in some way.

__TOC__

Specifications

A Maildir directory (often named Maildir) usually has three subdirectories named tmp, new, and cur.[1]

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:

  1. read the current Unix time
  2. read the current process identifier (PID)
  3. read the current hostname
  4. concatenate the above three values into a string separated by the period character; this is the new filename
  5. if stat reports that the filename exists, then wait two seconds
  6. go to previous step until the filename does not exist
  7. 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:

This 2003 algorithm was criticised in 2006 as being unnecessarily complex by Timo Sirainen, the creator of Dovecot.

As of November 2023, qmail author Daniel Bernstein had made no further changes to the 2003 filename generation recommendations. On modern POSIX systems, temporary files can be safely created with the [[mkstemp]] C library function.

The delivery process stores the message in the maildir by creating and writing to tmp/''uniquefilename'', and then moving this file to new/''uniquefilename''. The moving can be done using [[rename (computing)|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 Internet Message Access Protocol or Post Office Protocol servers acting on behalf of 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.

When a cognizant maildir-reading process (either a POP or IMAP server, or a mail user agent acting locally) finds messages in the new directory, it must move them to cur. It is just a means to notify the user "you have X new messages". This moving needs to be done using the atomic filesystem rename, as the alternative link-then-unlink technique is non-atomic and may result in duplicated messages. An informational suffix is appended to filenames at this stage. It consists of a colon (to separate the unique part of the filename from the actual information), a "2", a comma and various flags. The "2" specifies the version of the information that follows the comma. "2" is the only currently officially specified version, "1" being an experimental version. The specification defines flags that show whether the message has been read, deleted and so on: the initial (capital) letter of "Passed", "Replied", "Seen", "Trashed", "Draft", and "Flagged". Applications often choose to supplement this very limited set of flags, for example notmuch[2] offers flag synchronization in addition to arbitrary user-defined flags,[3] while Dovecot uses lowercase letters to match 26 IMAP keywords, which may include keywords such as $MDNSent or user-defined flags.

Although Maildir was intended to allow lockless usage, in practice some software that uses Maildirs also uses locks, such as Dovecot.

File-system compatibility issues

The Maildir standard can only be implemented on systems that accept colons in filenames. [4]

Systems that don't allow colons in filenames (this includes Microsoft Windows and some configurations of Novell Storage Services) can use a non-standard alternative separator, such as ";" or "-". It is often trivial to patch free and open-source software to use a different separator.

As there is currently no agreement on what character this alternative separator should be, there can be interoperability difficulties between different Maildir-supporting programs on these systems. However, not all Maildir-related software needs to know what the separator character is, because not all Maildir-related software needs to be able to read or modify the flags of a message ("read", "replied to" etc.); software that merely delivers to a Maildir or archives old messages from it based only on date, should work no matter what separator is in use. If only the MUA needs to read or modify message flags, and only one MUA is used, then non-standard alternative separators may be used without interoperability problems.

Software that supports Maildir directly

See also

External links

Notes and References

  1. Book: Blum, Richard . Postfix . 2001 . Sams Publishing . 978-0-672-32114-6 . en.
  2. Web site: Notmuch mail system homepage. notmuchmail.org. 2019-06-22.
  3. Web site: notmuch 0.38.3 documentation . notmuch-config . 2024-04-17.
  4. Web site: mailbox — Manipulate mailboxes in various formats . 2023-06-19 . Python documentation.
  5. Web site: aerc - the world's best email client homepage. aerc-mail.org.
  6. Web site: Notmuch mail system homepage. notmuchmail.org. 2019-06-22.
  7. Web site: Maildir in Thunderbird. mozilla.org. 2020-12-06.