maildirmake - create maildirs, and maildir folders

SYNOPSIS

maildirmake [ -f folder ] maildir

DESCRIPTION

The maildirmake command is used to create new Maildirs, and Maildir folders.

A Maildir is a directory that's used to store mail messages. Not just any directory will do - a Maildir must be of a specific structure, and have specific access permissions. The maildirmake command is a convenient way to create a new Maildir, or a new folder within an existing Maildir, with the correct permission and structure.

By itself, maildirmake makes a new subdirectory maildir, and creates all the necessary structures. The -f option creates a new "folder" within an existing maildir. maildir must already exist, and the maildirmake command will create a new folder inside it.

Folders are simply subdirectories inside the main maildir whose names start with a period, and which are themselves maildirs. For example, the command "maildirmake -f Drafts mail/Maildir" creates mail/Maildir/.Drafts, with the usual tmp, new, and cur. You MUST use the -f option, instead of specifying mail/Maildir/.Drafts directly, in order to correctly initialize certain enhanced maildir features.

Folders cannot be created directly within other folders. Running maildirmake -f Urgent mail/Maildir/.Drafts will not work. Instead, the period character is designated as a hierarchy separator, run maildirmake -f Drafts.Urgent mail/Maildir instead. This creates mail/Maildir/.Drafts.Urgent, and all mail software that supports enhanced maildirs will interpret it as a subfolder Urgent of the Drafts folder.

MAILDIR STRUCTURE

The rest of this manual page is a technical description of Maildirs. The first implementation and definition of Maildirs were in Dan Bernstein's Qmail mail server, and it described a single, flat, mail repository. Since then, independent developers added additional features on top of it, such as folders and voluntary mail quotas. This document describes the enhanced Maildir implementation without noting the specific enhancements to the original Maildir implementation. See http://www.qmail.org/man/man5/maildir.html for the original specifications of Maildirs.

Maildirs offer several improvements over traditional mailbox files. Unlike mailbox files, no locking is necessary in order to deliver new messages to maildirs, or to read mail in maildirs. Multiple agents can deliver new mail to maildirs, in parallel, and read mail from maildirs. Locking is often problematic with certain network file system. Maildirs require no locking, and can be implemented on top of almost any file system.

A maildir is an ordinary subdirectory, with group and world access permissions revoked, of course. A maildir contains three subdirectories: tmp, new, and cur. Folders are additional subdirectories whose names begin with a period: such as .Drafts or .Sent. Each folder itself contains the same three subdirectories, tmp, new, and cur, and can be thought of as a maildir in of itself, except that it itself cannot contain nested folder subdirectories, as explained above.

A folder subdirectory also contains an extra, zero-length file named maildirfolder, whose purpose is to simply inform any mail delivery agent that it really is delivery directly to a subfolder, and that the mail delivery agent should look in the parent directory for any required housekeeping information. Software that uses maildirs may also create additional files besides the tmp, new, and cur subdirectories -- in the main maildir or a folder "submaildir" -- for their own use.

E-mail messages are stored in separate, individual files as defined below, one E-mail message per file. The tmp subdirectories is used to temporarily store E-mail messages that are in the process of being delivered to this maildir. It can also be used for storing other kinds of temporary files, as long as they follow the same naming convention for creating new files in tmp. The new subdirectory stores messages that have been delivered to this maildir, but have not yet been seen by any mail application. The cur subdirectory stores messages that have already been seen by mail applications.

DELIVERING NEW MAIL TO A MAILDIR

The following process is used to deliver mail to a maildir:

A new unique filename is created using one of two possible forms: "time.pid.host", or "time.pid_unique.host". "time" is the current system time, in seconds. "pid" is the process number of the process that is delivering this message to the maildir. "host" is the name of the machine where the mail is being delivered. In the event that the same process will create multiple messages, it should append a unique suffix to the process id, preferrably an underscore, followed by an increasing counter. This applies whether messages created by a process are all in the same, or different, maildirs. This protocol allows multiple processes running on multiple machines on the same network to simultaneously create new messages without stomping on each other.

The filename created in the previous step is checked for existence by executing the stat(2) system call, against the tmp subdirectory. If the result of the stat system call is ANYTHING OTHER than the system error ENOENT, the process must sleep for two seconds, then go back and create a new unique filename. This is an extra step to insure that each new message has a completely unique filename.

Other applications that wish to use tmp for temporary storage should observe the same protocol (but see READING MAIL FROM MAILDIRS below, because old files in tmp will be eventually deleted).

If the stat system call returned ENOENT, the process can go ahead, create the file in the tmp subdirectory, and save the entire message in the new file. The message saved MUST NOT have the traditional "From_" line that is used to separate individual messages in the traditional mailbox files. The message also MUST NOT have any "From_" lines in the contents of the message prefixed by the ">" character, as messages in traditional mailbox files are.

Saving the message means to reliably save it in a file. The number of bytes returned by the write system call must be checked, in order to make sure that the complete message has been written out.

After the message is saved, the file is closed and is then immediately moved/renamed into the new subdirectory. The name of the file should be the same name it had in the tmp subdirectory.

It is HIGHLY RECOMMENDED that new implementations of software that creates messages in maildirs would also append the suffix ",S=nnn" to the name of the file when it is moved into the new subdirectory, where "nnn" represents the size of the message, in bytes. This optimizes the voluntary maildir quota enhancement, as it permits the size of all the mail stored in the maildir to be added up without issuing the stat system call for each individual message (which can be quite a performance drain with certain network filesystems).

READING MAIL FROM MAILDIRS

Applications that read mail from maildirs should do it in the following order:

When opening a maildir or a maildir folder, read the tmp subdirectory and delete anything in there that's at least 36 hours old.

Look for new messages in the new subdirectory. For each new message found, new/filename, rename it as cur/filename:2,info, where info is used to represent the state of the message, and it consists of zero or more boolean flags chosen from the following: R - this message has been replied to, S - this message has been viewed (seen), T - this message has been marked to be deleted (trashed), but is not yet completely removed (messages are removed from maildirs simply by deleting their file), F - this message has been marked by the user, for some purpose. These flags must be stored in alphabetic order. New messages contain only the :2, suffix, with no flags, indicating that the messages haven't been seen, replied, marked, or deleted.

Maildirs may have maximum size quotas defined, but these quotas are purely voluntary. If you need to implement mandatory quotas, you should use any quota facilities provided by the underlying filesystem that is used to store the maildirs. The maildir quota enhancement is designed to be used in certain situations where filesystem-based quotas cannot be used for some reason. The implementation is designed to avoid the use of any locking. As such, at certain times the calculated quota may be imprecise, and certain anomalous situations may result in the maildir actually going over the stated quota. One such situation would be when applications create messages without updating the quota estimate for the maildir. Eventually it will be precisely recalculated, but wherever possible new messages should be created in compliance with the voluntary quota protocol.

The voluntary quota protocol involves some additional procedures that must be followed when creating or deleting messages within a given maildir or its subfolders. The deliverquota(8) command is a tiny application that delivers a single message to a maildir using the voluntary quota protocol, and hopefully it can be used as a measure of last resort. Alternatively, applications can use the libmaildir.a library to handle all the low-level dirty details for them. The voluntary quota enhancement is specified in a separate README.

SEE ALSO

maildrop(1), maildirquota(8), deliverquota(8), maildropfilter(1), http://www.qmail.org/man/man5/maildir.html, maildir(5).