munchymail a mail archive that keeps every copy of every message

Identity and duplicates

How munchymail decides that two stored copies are the same email, what it does with copies that differ, and how damaged or out-of-spec mail behaves. The rules here are what the code does as of 0.2.0 (src/key.rs, src/rekey.rs; src/import.rs Artifacts::put; src/mbox.rs split; src/takeout.rs compare, Linker). It was written for review, and the reviews’ findings are folded in; measurements on a real archive are kept outside the repository.

Terms

Term Meaning
origin a place mail is read from: an mbox tree or file, a Gmail account (API), an IMAP server, a Google Takeout file
copy (artifact) one exact stored message: its header bytes and body bytes, each kept by SHA-256
sighting a copy seen at a path (folder) in an origin during an import or sync run
message one email, however many copies of it were stored; what search counts and mN names
identity the key that puts a copy into a message (below)
fold an explicit, reasoned link putting a copy into a message although its identity differs

What is stored

How copies are cut out of mbox files (mbox::split)

Message identity

A copy belongs to the message whose key equals its own; if no active message has it, a new message is created. The key (key::key) is a hash of five things, all of which must match:

Dovecot’s “FOLDER INTERNAL DATA” placeholder, which only holds a folder’s UID state, is stored like any copy but classified as not mail (artifact.kind = folder-data): readable by id, and left out of every listing, count, report and index. A copy its owner knows is damaged (mmadm copy damaged, artifact.kind = damaged) follows the same rule, keeps its marks, and can point to the copy that repaired it (mmadm copy link REPAIRED --repaired-from ID). Dovecot’s per-message bookkeeping fields (Status, X-Status, X-Keywords, X-UID, Content-Length, X-IMAPbase) stay in the stored bytes and are left out of read’s header list.

Other header fields are not part of the key: copies that differ only in a mail server’s added fields, Dovecot’s own fields, line endings or Gmail’s Takeout fields are copies of one message.

Why each reading

Reading Because Where it shows
CRLF as LF Mail’s canonical form (RFC 5322) and IMAP’s wire form (RFC 3501/9051) use CRLF; Unix mbox files store LF; Dovecot converts between them (mail_save_crlf = no, the default, stores LF). Gmail stores CRLF, so its API and Takeout copies are CRLF. the same email from an mbox file and over IMAP
NUL as 0x80 Ordinary IMAP literals can’t carry NUL (only literal8, with the BINARY extension, can); Dovecot sends each NUL as 0x80 in a normal BODY[] fetch. mail with NUL in its body, from an mbox file and over IMAP
missing final newline added An mbox must end a message with a newline (above), so a message that had none (common in mail composed in Gmail’s web interface) gains one in the file; the original (API) has none. Gmail API vs Takeout. (Between Dovecot’s mbox file and its IMAP view it joined only one pair, a damaged message IMAP serves one byte short: a coincidence, not support.)

All three are conversions a store or protocol performs on the same email, not differences between emails. One consequence: an empty body and a body of a single \n have the same identity. They apply to every source, not only to the pair that showed them: a deliberate tradeoff, since a false merge through them also needs equal Message-ID, Date and MIME reading. Every source goes through the same function: mbox and Takeout imports (Importer), Gmail API sync (gmail.rs), IMAP sync (imap.rs); there is no other code path that creates a message.

What the key deliberately does not absorb

Where identity can’t join two copies of one email, a fold can, with a recorded reason. Today only Google Takeout copies are folded, to the Gmail API copy of the same message (takeout::compare, run at Takeout import and Gmail sync, and by mmadm reconcile). A Takeout copy folds to an API copy when:

The folded copy moves into the API copy’s message; the message it leaves, if then empty, is kept as merged into it (below), its derived indexes dropped. With the final-newline reading, final-crlf pairs now also share an identity, so the Takeout copy joins at once; the fold is still recorded, marking the API copy as the message’s own.

Which copy is shown

A message’s copies are all kept and listed (read shows every place each was seen). One copy, its preferred copy, is used for content: the earliest stored copy that isn’t folded (or, if all are, the earliest). It changes only when the message gains a better copy, never because of when copies were seen, and never by origin: a live source can hold the worse copy.

Operations on messages (indexing of parts and text, read mN, show mN, search --unique, export) find the matching copies, then their messages, then each message’s preferred copy, whichever copy matched; export says in its index how many messages matched through another copy. Operations on copies (read N, show N --raw, search without --unique) use exactly the copies named or matched.

Message ids, and when the rule changes

Messages are never deleted, so an id (mN) is never reused. A message whose copies all join another keeps its row with merged_into set; it no longer counts or appears in search, and every command given its mN follows it to the message it joined and says so (read: “m4 was merged into m10”).

A database records the rule its keys follow (meta key identity, now key::RULE). When a newer program opens it, before the next import, sync or index stores anything, it regroups (rekey), in one transaction:

  1. every copy that isn’t folded is re-keyed (all keys first, so nothing is compared with a key under the old rule); folded copies follow the copy they are folded to;
  2. copies with one key are one group;
  3. each group takes the id of the old message whose preferred copy it holds; if it holds several, the lowest id survives and the others are merged_into it; a group holding none is a new message with split_from the old message most of its copies came from;
  4. messages whose copies or preferred copy changed are indexed again.

An interrupted regrouping changes nothing; a second one changes nothing. Stored bytes never change.

Damaged and out-of-spec mail

munchymail keeps damaged mail as it finds it, and dedup never hides damage: a damaged copy and an intact copy of one email have different identities, so both are kept as separate messages unless explicitly folded.

What munchymail notices is recorded on the copy as a mark (copy_mark; read lists them, search --mark NAME finds them). A mark states what was measured, not a verdict, and never changes whether a copy is mail, which message it is in, or which copy is shown:

Mark Set when Detail
content-length the copy has a Content-Length field and the body isn’t that long, line endings read as stored or as LF (a copy served with CRLF keeps the field its mbox wrote) both numbers, and whether the body is shorter or longer
unclosed-boundary a multipart/* body never has --<boundary>-- the boundary
prefix-of another mail copy with the same valid Message-ID and Date, in another message, has a body that this one’s is a strict prefix of (line endings read alike) the longer copy, both lengths, and how this one ends: mid-line (a sign of truncation), at a line the longer continues past (often a mailing list’s footer on one delivery), or short only of newlines

Marks are set when a copy is stored, and once for copies stored earlier (meta key marks_version; a new detector version replaces its marks).

The detectors, and why:

Risks, as we see them

Open questions

  1. NUL as 0x80 and the final newline: global (today) or only between the sources that showed them, as folds?
  2. Should a prefix-of copy that stops mid-line be folded into the longer copy’s message? Today it is only marked: a short copy can be a prefix of two different longer ones, and a prefix alone doesn’t prove one email.
  3. Should copies differing only by a mailing list’s footer, or by trailing blank lines, be one message? Today they are separate, and marked prefix-of.