debian/rules: Use `git' potty wrapper.
[qmail] / maildir.5
CommitLineData
2117e02e
MW
1.TH maildir 5
2.SH "NAME"
3maildir \- directory for incoming mail messages
4.SH "INTRODUCTION"
5.I maildir
6is a structure for
7directories of incoming mail messages.
8It solves the reliability problems that plague
9.I mbox
10files and
11.I mh
12folders.
13.SH "RELIABILITY ISSUES"
14A machine may crash while it is delivering a message.
15For both
16.I mbox
17files and
18.I mh
19folders this means that the message will be silently truncated.
20Even worse: for
21.I mbox
22format, if the message is truncated in the middle of a line,
23it will be silently joined to the next message.
24The mail transport agent will try again later to deliver the message,
25but it is unacceptable that a corrupted message should show up at all.
26In
27.IR maildir ,
28every message is guaranteed complete upon delivery.
29
30A machine may have two programs simultaneously delivering mail
31to the same user.
32The
33.I mbox
34and
35.I mh
36formats require the programs to update a single central file.
37If the programs do not use some locking mechanism,
38the central file will be corrupted.
39There are several
40.I mbox
41and
42.I mh
43locking mechanisms,
44none of which work portably and reliably.
45In contrast, in
46.IR maildir ,
47no locks are ever necessary.
48Different delivery processes never touch the same file.
49
50A user may try to delete messages from his mailbox at the same
51moment that the machine delivers a new message.
52For
53.I mbox
54and
55.I mh
56formats, the user's mail-reading program must know
57what locking mechanism the mail-delivery programs use.
58In contrast, in
59.IR maildir ,
60any delivered message
61can be safely updated or deleted by a mail-reading program.
62
63Many sites use Sun's
64.B Network F\fPa\fBil\fPur\fBe System
65(NFS),
66presumably because the operating system vendor does not offer
67anything else.
68NFS exacerbates all of the above problems.
69Some NFS implementations don't provide
70.B any
71reliable locking mechanism.
72With
73.I mbox
74and
75.I mh
76formats,
77if two machines deliver mail to the same user,
78or if a user reads mail anywhere except the delivery machine,
79the user's mail is at risk.
80.I maildir
81works without trouble over NFS.
82.SH "THE MAILDIR STRUCTURE"
83A directory in
84.I maildir
85format has three subdirectories,
86all on the same filesystem:
87.BR tmp ,
88.BR new ,
89and
90.BR cur .
91
92Each file in
93.B new
94is a newly delivered mail message.
95The modification time of the file is the delivery date of the message.
96The message is delivered
97.I without
98an extra UUCP-style
99.B From_
100line,
101.I without
102any
103.B >From
104quoting,
105and
106.I without
107an extra blank line at the end.
108The message is normally in RFC 822 format,
109starting with a
110.B Return-Path
111line and a
112.B Delivered-To
113line,
114but it could contain arbitrary binary data.
115It might not even end with a newline.
116
117Files in
118.B cur
119are just like files in
120.BR new .
121The big difference is that files in
122.B cur
123are no longer new mail:
124they have been seen by the user's mail-reading program.
125.SH "HOW A MESSAGE IS DELIVERED"
126The
127.B tmp
128directory is used to ensure reliable delivery,
129as discussed here.
130
131A program delivers a mail message in six steps.
132First, it
133.B chdir()\fPs
134to the
135.I maildir
136directory.
137Second, it
138.B stat()s
139the name
140.BR tmp/\fItime.pid.host ,
141where
142.I time
143is the number of seconds since the beginning of 1970 GMT,
144.I pid
145is the program's process ID,
146and
147.I host
148is the host name.
149Third, if
150.B stat()
151returned anything other than ENOENT,
152the program sleeps for two seconds, updates
153.IR time ,
154and tries the
155.B stat()
156again, a limited number of times.
157Fourth, the program
158creates
159.BR tmp/\fItime.pid.host .
160Fifth, the program
161.I NFS-writes
162the message to the file.
163Sixth, the program
164.BR link() s
165the file to
166.BR new/\fItime.pid.host .
167At that instant the message has been successfully delivered.
168
169The delivery program is required to start a 24-hour timer before
170creating
171.BR tmp/\fItime.pid.host ,
172and to abort the delivery
173if the timer expires.
174Upon error, timeout, or normal completion,
175the delivery program may attempt to
176.B unlink()
177.BR tmp/\fItime.pid.host .
178
179.I NFS-writing
180means
181(1) as usual, checking the number of bytes returned from each
182.B write()
183call;
184(2) calling
185.B fsync()
186and checking its return value;
187(3) calling
188.B close()
189and checking its return value.
190(Standard NFS implementations handle
191.B fsync()
192incorrectly
193but make up for it by abusing
194.BR close() .)
195.SH "HOW A MESSAGE IS READ"
196A mail reader operates as follows.
197
198It looks through the
199.B new
200directory for new messages.
201Say there is a new message,
202.BR new/\fIunique .
203The reader may freely display the contents of
204.BR new/\fIunique ,
205delete
206.BR new/\fIunique ,
207or rename
208.B new/\fIunique
209as
210.BR cur/\fIunique:info .
211See
212b6f5d 212.B http://pobox.com/~djb/proto/maildir.html
2117e02e
MW
213for the meaning of
214.IR info .
215
216The reader is also expected to look through the
217.B tmp
218directory and to clean up any old files found there.
219A file in
220.B tmp
221may be safely removed if it
222has not been accessed in 36 hours.
223
224It is a good idea for readers to skip all filenames in
225.B new
226and
227.B cur
228starting with a dot.
229Other than this, readers should not attempt to parse filenames.
230.SH "ENVIRONMENT VARIABLES"
231Mail readers supporting
232.I maildir
233use the
234.B MAILDIR
235environment variable
236as the name of the user's primary mail directory.
237.SH "SEE ALSO"
238mbox(5),
239qmail-local(8)