


maildir(5)                FILE FORMATS                 maildir(5)



NAME
     maildir - directory for incoming mail messages

INTRODUCTION
     _m_a_i_l_d_i_r is a structure for directories of incoming mail mes-
     sages.   It solves the reliability problems that plague _m_b_o_x
     files and _m_h folders.

RELIABILITY ISSUES
     A machine may crash while it is delivering a  message.   For
     both  _m_b_o_x  files and _m_h folders this means that the message
     will be silently truncated.  Even worse: for _m_b_o_x format, if
     the message is truncated in the middle of a line, it will be
     silently joined to the next  message.   The  mail  transport
     agent will try again later to deliver the message, but it is
     unacceptable that a corrupted message should show up at all.
     In  _m_a_i_l_d_i_r,  every  message  is  guaranteed  complete  upon
     delivery.

     A machine may have two  programs  simultaneously  delivering
     mail  to the same user.  The _m_b_o_x and _m_h formats require the
     programs to update a single central file.  If  the  programs
     do  not use some locking mechanism, the central file will be
     corrupted.  There are several _m_b_o_x and  _m_h  locking  mechan-
     isms,  none  of  which  work portably and reliably.  In con-
     trast, in _m_a_i_l_d_i_r, no locks are ever  necessary.   Different
     delivery processes never touch the same file.

     A user may try to delete messages from his  mailbox  at  the
     same  moment  that  the machine delivers a new message.  For
     _m_b_o_x and _m_h formats, the user's  mail-reading  program  must
     know  what locking mechanism the mail-delivery programs use.
     In contrast, in _m_a_i_l_d_i_r, any delivered message can be safely
     updated or deleted by a mail-reading program.

     Many sites use Sun's Network Failure System  (NFS),  presum-
     ably because the operating system vendor does not offer any-
     thing else.  NFS exacerbates  all  of  the  above  problems.
     Some  NFS implementations don't provide any reliable locking
     mechanism.  With  _m_b_o_x  and  _m_h  formats,  if  two  machines
     deliver  mail to the same user, or if a user reads mail any-
     where except the delivery machine, the  user's  mail  is  at
     risk.  _m_a_i_l_d_i_r works without trouble over NFS.

THE MAILDIR STRUCTURE
     A directory in _m_a_i_l_d_i_r format has three subdirectories,  all
     on the same filesystem: tmp, new, and cur.

     Each file in new is a newly  delivered  mail  message.   The
     modification  time  of  the file is the delivery date of the
     message.  The message is delivered _w_i_t_h_o_u_t  an  extra  UUCP-
     style  From_ line, _w_i_t_h_o_u_t any >From quoting, and _w_i_t_h_o_u_t an



Sun Release 4.1           Last change:                          1






maildir(5)                FILE FORMATS                 maildir(5)



     extra blank line at the end.  The message is normally in RFC
     822   format,   starting  with  a  Return-Path  line  and  a
     Delivered-To line, but it  could  contain  arbitrary  binary
     data.  It might not even end with a newline.

     Files in cur are just like files in new.  The big difference
     is  that files in cur are no longer new mail: they have been
     seen by the user's mail-reading program.

HOW A MESSAGE IS DELIVERED
     The tmp directory is used to ensure  reliable  delivery,  as
     discussed here.

     A program delivers a mail message in six steps.   First,  it
     chdir()s  to  the _m_a_i_l_d_i_r directory.  Second, it stat()s the
     name tmp/_t_i_m_e._p_i_d._h_o_s_t, where _t_i_m_e is the number of  seconds
     since  the  beginning of 1970 GMT, _p_i_d is the program's pro-
     cess ID, and _h_o_s_t  is  the  host  name.   Third,  if  stat()
     returned  anything other than ENOENT, the program sleeps for
     two seconds, updates _t_i_m_e, and tries  the  stat()  again,  a
     limited  number  of  times.   Fourth,  the  program  creates
     tmp/_t_i_m_e._p_i_d._h_o_s_t.  Fifth, the program _N_F_S-_w_r_i_t_e_s  the  mes-
     sage  to  the  file.  Sixth, the program link()s the file to
     new/_t_i_m_e._p_i_d._h_o_s_t.  At that instant  the  message  has  been
     successfully delivered.

     The delivery program is required to start  a  24-hour  timer
     before creating tmp/_t_i_m_e._p_i_d._h_o_s_t, and to abort the delivery
     if the timer expires.  Upon error, timeout, or  normal  com-
     pletion,  the  delivery  program  may  attempt  to  unlink()
     tmp/_t_i_m_e._p_i_d._h_o_s_t.

     _N_F_S-_w_r_i_t_i_n_g means (1) as usual, checking the number of bytes
     returned  from  each  write()  call; (2) calling fsync() and
     checking its return value; (3) calling close() and  checking
     its  return  value.   (Standard  NFS  implementations handle
     fsync() incorrectly but make up for it by abusing close().)

HOW A MESSAGE IS READ
     A mail reader operates as follows.

     It looks through the new directory for  new  messages.   Say
     there  is  a new message, new/_u_n_i_q_u_e.  The reader may freely
     display the contents of new/_u_n_i_q_u_e,  delete  new/_u_n_i_q_u_e,  or
     rename      new/_u_n_i_q_u_e      as     cur/_u_n_i_q_u_e:_i_n_f_o.      See
     http://pobox.com/~djb/maildir.html for the meaning of _i_n_f_o.

     The reader is also expected to look through the  tmp  direc-
     tory  and  to clean up any old files found there.  A file in
     tmp may be safely removed if it has not been accessed in  36
     hours.




Sun Release 4.1           Last change:                          2






maildir(5)                FILE FORMATS                 maildir(5)



     It is a good idea for readers to skip all filenames  in  new
     and  cur  starting  with  a  dot.   Other than this, readers
     should not attempt to parse filenames.

ENVIRONMENT VARIABLES
     Mail readers supporting _m_a_i_l_d_i_r use the MAILDIR  environment
     variable as the name of the user's primary mail directory.

SEE ALSO
     mbox(5), qmail-local(8)













































Sun Release 4.1           Last change:                          3



