munchymail a mail archive that keeps every copy of every message

Reading sources safely

Addendum to docs/architecture.md.

Writes

munchymail writes only under its data root (data/ here; see README.md), one directory per account:

data/ADDRESS/email.sqlite
data/ADDRESS/content/aa/aa23…   large immutable objects, by SHA-256
data/ADDRESS/text/              the text index

Sources are never written: no dotlocks, temp files, renames, or atime updates.

Reading an mbox (src/source.rs)

Matches a Dovecot reader with the default mbox_read_locks = fcntl:

  1. open(O_RDONLY|O_NOATIME|O_CLOEXEC); drop O_NOATIME on EPERM (file not ours).
  2. Whole-file fcntl(F_SETLK, F_RDLCK), retrying every 100–200 ms (Dovecot’s jitter) up to a timeout. Dovecot writers (mbox_write_locks, here fcntl) take F_WRLCK, so we never read a half-written append or rewrite.
  3. If the path’s dev/ino no longer match the fd, reopen (as mbox_file_open_latest).
  4. Read the whole file into memory, fstat again, close (releases the lock). Any size/mtime/ctime change is reported as changed_during_read.

Each read is logged (stderr, tracing) with bytes, lock_wait, lock_held and open_for; busy locks, timeouts and files that changed under the lock are logged too. Measured on the 3.3 GB live inbox with cold caches: lock held 0.89 s.

Before that, import peeks at each file’s first 4 KiB without a lock (same read-only open) to skip files that don’t start with a valid From line.

No mmap, so a truncating rewrite can’t SIGBUS us. POSIX locks drop when any fd for the file closes in this process, so a file is opened exactly once per read.

Verified with strace: the only syscalls touching the source are the openat and one fcntl(F_SETLK, F_RDLCK); a Python lockf(LOCK_EX) holder makes the reader wait until release.

Splitting (src/mbox.rs)

Follows istream-raw-mbox.c / mbox-from.c:

Known Dovecot divergence: +HHMM in From lines is parsed as hours and minutes; Dovecot reads the last two digits as seconds.

Dovecot-written mboxes carry its own headers (X-UID, Status, X-Keywords, Content-Length, and an X-IMAP pseudo-message first). They stay in the raw header object; copies from other sources will share the body hash but not the header hash.

Origins

Many backups will hold the same files, so provenance is tracked in layers: