hechengjin 发表于 2016-3-13 22:24:03

Mbox vs Maildir 两者的原理和区别

在开放源代码的世界里,电子邮件服务器最主流的目前有三种,分别是sendmail, qmail, postfix。而存储格式最流行的有两种,Mbox和Maildir,它们都是开放的存储格式,因此兼容性比较好。 而mbox和maildir的历史,却不为大多数用户所了解。今天和一个朋友聊到mbox和maildir哪个好,顺便搜集一下mbox和maildir的资料,谈谈这两者的原理和区别,以及应该如何做选择。 Mbox的历史较Maildir悠久,sendmail支持mbox,qmail和postfix都支持mbox,其主要特点就是“所有邮件都存放到一个文件里”。每个邮件之间以特定的标记分割。 而Maildir则相反,每一封邮件保存成一个文件,每个文件名称一般有一定的规律,例如会包含时间戳、pid及inode节点号等。 除了mbox和maildir格式外,还有一个叫mbx的格式,它是对mbox的改进版本。主要用在UW-IMAP server里。它最大的特点是有一个针对mbox文件的索引,能改善读/写性能。但依然需要file lock。 随着qmail/postfix的普及,以及mbox的一些问题暴露,maildir得到了越来越多的应用。其中mbox的最主要问题是文件缩定(file lock),其次是大多数update操作的效能问题。 以下是引自courier-mta.org上的mbox和maildir的测试对比文章,介绍了mbox和maildir: mbox mail storage formatThis is the traditional way to store mail on UNIX-based mail servers.Individual messages are simply concatenated together, and saved in asingle file. A special marker is placed where one message ends and thenext message begins. Only one process can access the mbox file inread/write mode. Concurrent access requires a locking mechanism. Anytimesomeone needs to update the mbox file, everyone else must wait for theupdate to complete. maildir mail storage formatMaildirs were originally implemented in the Qmail mail server, supposedlyto address the inadequacies of mbox files. Individual messages are savedin separate files, one file per message. There is a defined method fornaming each file. There's a defined procedure for adding new messages tothe maildir. No locking is required. Multiple processes can use maildirsat the same time. mbx mail storage formatThis is a slightly modified version of the original mbox format that's offered by the UW-IMAP server. mbx mailboxes stillrequire locking. Themain difference from the mbox format is that each message in the file ispreceded by a record that carries some message-specific metadata. Assuch, certain operations that used to require the entire mbox file to berewritten can now be implemented by updating the fixed-size headerrecord. This benchmark focuses mainly on the mbox and maildirs formats. In March of 2003 an unrelated party conducted a similarbenchmark for mbxformats. See http://www.decisionsoft.com/pdw/mailbench.htmlfor more details. Mbox vs Maildir之优缺点比较这里给出一个基本的特性对比,读者很容易就清楚根据自己的应用到底应该选什么存储格式:可靠性优选是Maildir,因为mbox只有一个文件,一旦出问题之后,所有邮件都将损毁。 更新速度这里主要指的是删除/增加邮件的能力,无疑Maildir完胜Mbox 搜索速度这点Mbox因为是单文件,因此搜索的能力要强于maildir 并发访问能力对于繁忙的邮件系统,多个进程同时访问同一封邮件是可能的事情,Mbox需要flock()的支持,而且如果某一个进程操作时间长,则其他所有进程都堵塞了。Maildir没有这个问题。在NFS等网络文件系统上,Maildir相对安全,Mbox不能用于此类型环境 扩充能力现在的邮箱已经不是5年前甚至10年前的1MB,2MB而是100,200甚至1G/2G,Mbox应付那么大的容量已力不从心,无疑Maildir是比较适合的。 文件系统依赖Maildir较依赖文件系统,尤其是依赖对目录的索引能力,用ReiserFS会比较快,对于超大型的maildir,读写性能将受到考验。相对而言Mbox则不存在这个问题。 综合结论推荐使用Maildir格式,安全可靠,绝大部分操作都远快于Mbox。而且现今支持Maildir的软件越来越多,qmail/Postfix 都支持。 一些有用的链接 原理标准http://cr.yp.to/proto/maildir.htmlhttp://www.qmail.org/qmail-manual-html/man5/mbox.html Benchmark/比较http://www.courier-mta.org/mbox-vs-maildir/http://www.decisionsoft.com/pdw/mailbench.html 转换工具http://www.qmail.org/mbox2maildirhttp://batleth.sapienti-sat.org/projects/mb2md/

hechengjin 发表于 2016-3-13 22:24:20

Maildir和Mailbox 的区别在开放源代码的世界里,电子邮件存储格式最流行的有两种,Mbox和Maildir,它们都是开放的存储格式,兼容性比较好
Mbox历史较Maildir悠久,sendmail支持mbox,qmail和postfix都支持mbox,其主要特点就是 "所有邮件都存放到一个文件里",每个邮件之间以特定的标记分割。
而Maildir则相反,每一封邮件保存成一个文件,每个文件名称一般有一定的规律,例如会包含时间戳、pid及inode节点号等。
除了mbox和maildir格式外,还有一个叫mbx的格式,它是对mbox的改进版本。主要用在UW-IMAP server里。
它最大的特点是有一个针对mbox文件的索引,能改善读/写性能。但依然需要file lock。
随着qmail/postfix的普及,以及mbox的一些问题暴露,maildir得到了越来越多的应用。
其中mbox的最主要问题是文件缩定(file lock),其次是大多数update操作的效能问题。原理:The Mbox FormatThis is the traditional way of storing mail messages in the Unix world. In this format, a regular text file which serves as the mail user’s mailbox file is created.http://s7.sinaimg.cn/mw690/5f9e364bgdd5a8da97d46&690的区别" title="Maildir和Mailbox 的区别" action-data="http%3A%2F%2Fs7.sinaimg.cn%2Fmw690%2F5f9e364bgdd5a8da97d46%26690" action-type="show-slide" style="border-width: 0px; border-style: initial; list-style: none;">

Fig. 1: Mbox storage formatHow Mbox worksReceiving and storing a mail
[*]Lock the mailbox.
[*]Append the header (usually “From “) and the mail into the mailbox file.
[*]Unlock the mailbox.
Retrieving a mail
[*]Lock the mailbox.
[*]Locate and read the mail.
[*]Update the mail status flag.
[*]Unlock the mailbox.
Deleting a mail
[*]Lock the mailbox.
[*]Move the contents of the mailbox, beginning from the position right after the mail to be deleted until the end of the mailbox, into the position of the mail to be deleted.
[*]Reduce the size of the mailbox file by the size of the deleted mail.
[*]Unlock the mailbox.
Searching a mail
[*]Lock the mailbox.
[*]Search the mailbox.
[*]Unlock the mailbox.
Advantages
[*]Format is universally supported.
[*]Appending a new mail into the mailbox file is fast.
[*]Searching text inside a single mailbox file is fast.
Disadvantages
[*]Has file locking problems.
[*]Has problems when used with network file systems.
[*]Format is prone to corruption.
The Maildir FormatThis is a new way of storing mail messages. In this format, a directory usually named Maildir is created for each mail user. Under this directory are three more directories named new, cur and tmp.http://s16.sinaimg.cn/bmiddle/5f9e364bgdd5a910f233f&690Fig. 2: Maildir storage formatHow Maildir worksReceiving and storing a mail
[*]Create a unique file in the tmp directory.
[*]Write the mail into the newly created file.
[*]Move the completely written mail into the new directory.
Retrieving a mail
[*]Locate and read the mail.
[*]Move the mail from new into the cur directory and append the mail status flag into the filename.
Deleting a mail
[*]Delete the file containing the mail.
Searching a mail
[*]Search each and every mail file.
Advantages
[*]Locating, retrieving and deleting a specific mail is fast.
[*]Minimal to no file locking needed.
[*]Can be used on network file system.
[*]Immune to mailbox corruption (assuming the hardware will not fail).
Disadvantages
[*]Some filesystems may not efficiently handle a large number of small files.
[*]Searching text, which requires all mail files to be opened is slow.
Mbox vs Maildir之优缺点比较
这里给出一个基本的特性对比,读者很容易就清楚根据自己的应用到底应该选什么存储格式:

可靠性
优选是Maildir,因为mbox只有一个文件,一旦出问题之后,所有邮件都将损毁。

更新速度
这里主要指的是删除/增加邮件的能力,无疑Maildir完胜Mbox

搜索速度
这点Mbox因为是单文件,因此搜索的能力要强于maildir

并发访问能力
对于繁忙的邮件系统,多个进程同时访问同一封邮件是可能的事情,Mbox需要flock()的支持,而且如果某一个进程操作时间长,则其他所有进程都堵塞了。Maildir没有这个问题。在NFS等网络文件系统上,Maildir相对安全,Mbox不能用于此类型环境

扩充能力
现在的邮箱已经不是5年前甚至10年前的1MB,2MB而是100,200甚至1G/2G,Mbox应付那么大的容量已力不从心,无疑Maildir是比较适合的。

文件系统依赖
Maildir较依赖文件系统,尤其是依赖对目录的索引能力,用ReiserFS会比较快,对于超大型的maildir,读写性能将受到考验。相对而言Mbox则不存在这个问题。
综合结论
推荐使用Maildir格式,安全可靠,绝大部分操作都远快于Mbox。而且现今支持Maildir的软件越来越多,qmail/Postfix 都支持。?

一些有用的链接

原理标准
http://cr.yp.to/proto/maildir.html
http://www.qmail.org/qmail-manual-html/man5/mbox.html

Benchmark/比较
http://www.courier-mta.org/mbox-vs-maildir/
http://www.decisionsoft.com/pdw/mailbench.html

转换工具
http://www.qmail.org/mbox2maildir
http://batleth.sapienti-sat.org/projects/mb2md/

参考文档:
http://www.daxigua.com/archives/117
http://www.linuxmail.info/mbox-maildir-mail-storage-formats/
Maildir 格式Maildir邮箱格式不用于mbox之处,在于它使用目录结构来存贮邮件。Maildir的设计愿意是为了解决mbox格式的可靠性与文件锁定问题。例如:如果在邮件内容还没有完全写入mbox文件之前,系统就死机了,这时候可能只有部分内容在邮箱里。当系统恢复运行,MDA将邮件写入邮箱时,新的内容会接在前次残缺的内容的后面,造成问题。Mbox格式的另一个缺点,是发生在POP/IMAP server 与 SMTP server 试图同时开启同一个邮箱时。如果双方没有使用相同的锁定机制,邮箱文件可能因此受损。保护文件的锁定机制有好几种,但是并非所有邮件程序都使用锁定机制。如果使用maildir格式,则可以不使用文件保护锁,因为每一封邮件都是存放在单独的一个文件里。不同的邮件程序,不能同时访问同一个文件。
页: [1]
查看完整版本: Mbox vs Maildir 两者的原理和区别