From fe1efb2544aa04ae548d68f692cb270cf509a1cd Mon Sep 17 00:00:00 2001 From: codeiou <349858077+codeiou@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:46:35 +1000 Subject: [PATCH] #1871: a mailbox in a Cryptomator vault is saved every time, not only the first Since #1376 a pack of header lines was opened for reading and appending through one handle, so that a save could read back through it what it wrote. Cryptomator's file system won't give one handle both for a file that is there (its FUSE volume says EIO), so with the mailbox in a vault every save after the one that made the pack failed, the save at quit too, and what changed after it was lost. - store_pack: a pack is opened for appending only, the one there and a new one alike; what was written is read back through a handle of its own that only reads (open_pack, which never follows a link), opened before anything is written and kept for that pack. - test: the handles a later save and a whole rewrite hold on their pack, as the kernel records them (/proc/self/fdinfo, Linux): none both reads and appends. - release-note, progress-log and revision fragments. The other places that open a file to append to it (sync_pack, the mailbox file's own append, the update download) already append only; wipe reads and writes without appending, which the vault allows. Human-reviewed, AI-assisted, nobody's mailbox harmed. --- crates/engine/src/store_pack.rs | 29 ++++++++++---- crates/engine/src/store_pack_tests.rs | 51 +++++++++++++++++++++++++ docs/log.d/ROADMAP/progress/1871.md | 1 + docs/log.d/ROADMAP/revisions/1871.md | 1 + packaging/release-notes.d/fixes/1871.md | 1 + 5 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 docs/log.d/ROADMAP/progress/1871.md create mode 100644 docs/log.d/ROADMAP/revisions/1871.md create mode 100644 packaging/release-notes.d/fixes/1871.md diff --git a/crates/engine/src/store_pack.rs b/crates/engine/src/store_pack.rs index 3059b9a2af..480834c445 100644 --- a/crates/engine/src/store_pack.rs +++ b/crates/engine/src/store_pack.rs @@ -300,6 +300,9 @@ pub(super) struct Writer { lens: HashMap, /// The pack blocks are brought in from, kept open from one to the next. reader: Option<(u32, (fs::File, u64))>, + /// The pack written to, opened again to read back what was written ([`Writer::append`]): the + /// handle written through only appends (#1871). + check: Option<(u32, (fs::File, u64))>, /// Blocks read from content files of their own, to remove once a pack holds them. loose: Vec, /// The pack an opening wrote into unflushed was flushed by [`Writer::sync`]. @@ -320,6 +323,7 @@ impl Writer { placed: HashMap::new(), lens: HashMap::new(), reader: None, + check: None, loose: Vec::new(), synced_known: false, wrote: 0, @@ -438,7 +442,10 @@ impl Writer { Err(_) => false, }; let mut open = fs::OpenOptions::new(); - open.read(true).append(true).create(!there); + // Appending only, never reading as well: some file systems refuse one handle both for + // a file that is there (a Cryptomator vault, #1871). What is written is read back + // through a handle of its own ([`Writer::append`]). + open.append(true).create(!there); #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut open, 0o600); let f = open.open(&file)?; @@ -458,7 +465,7 @@ impl Writer { } let file = self.dir.join(name(n).ok_or_else(|| std::io::Error::other("no such pack"))?); let mut open = fs::OpenOptions::new(); - open.read(true).append(true).create_new(true); + open.append(true).create_new(true); // Mail is private: nobody else on the machine reads it. #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut open, 0o600); @@ -475,11 +482,18 @@ impl Writer { } /// Write `bytes` at the end of the pack; where they went. Where the end is, is asked of the - /// file itself, and what is there afterwards read back: a program without the window's lock - /// (the command line, an agent) appending to the same pack at the same moment makes this save - /// fail, never a mailbox file name the wrong bytes. + /// file itself, and what is there afterwards read back through a handle that only reads (the + /// one written through only appends, #1871): a program without the window's lock (the command + /// line, an agent) appending to the same pack at the same moment makes this save fail, never a + /// mailbox file name the wrong bytes. fn append(&mut self, bytes: &[u8]) -> std::io::Result { let pack = self.open()?; + // Opened before anything is written: a pack that can't be read back is written nothing. + if self.check.as_ref().is_none_or(|(n, _)| *n != pack) { + let file = self.dir.join(name(pack).ok_or_else(|| std::io::Error::other("no such pack"))?); + let back = open_pack(&file)?.ok_or_else(|| std::io::Error::other("the pack isn't a plain file"))?; + self.check = Some((pack, back)); + } let len = bytes.len() as u64; let file = self.file.as_mut().ok_or_else(|| std::io::Error::other("the pack isn't open"))?; let at = file.seek(SeekFrom::End(0))?; @@ -487,9 +501,10 @@ impl Writer { // (What was written of a block that failed is named by nothing; the next goes after it.) file.write_all(bytes)?; let on_disk = file.metadata()?.len(); - let mut there = (file.try_clone()?, on_disk); + let there = &mut self.check.as_mut().ok_or_else(|| std::io::Error::other("the pack isn't open"))?.1; + there.1 = on_disk; let r = Ref { hash: Digest::of(bytes), len }; - if read_from(&mut there, Loc { pack, at }, &r)?.is_none() { + if read_from(there, Loc { pack, at }, &r)?.is_none() { return Err(std::io::Error::other("the pack was written to by another program at the same time")); } self.wrote = self.wrote.saturating_add(len); diff --git a/crates/engine/src/store_pack_tests.rs b/crates/engine/src/store_pack_tests.rs index 2be84b2da6..e0806c85f1 100644 --- a/crates/engine/src/store_pack_tests.rs +++ b/crates/engine/src/store_pack_tests.rs @@ -707,6 +707,57 @@ fn a_block_is_named_where_it_went_though_another_program_appended_meanwhile() { assert_eq!(pack::read_at(&dir, then, &two).as_deref(), Some(&b"two block"[..])); } +/// #1871: some file systems won't give one handle both reading a file that is there and +/// appending to it (Cryptomator's vault refuses it, and its FUSE volume says EIO). The pack a save +/// appends to was opened that way, so with the mailbox in such a vault every save after the one +/// that made the pack failed, and what changed after it was lost when MailCraft closed. A pack is +/// appended to through a handle that only appends, the one there and a new one alike; what was +/// written is read back through a handle of its own. The kernel's record of each open handle +/// says which (Linux). +#[cfg(target_os = "linux")] +#[test] +fn a_pack_is_appended_to_through_a_handle_that_never_reads() { + let mb = with_exchange_headers(2); + let tmp = Temp::new("pack-append-only"); + let path = tmp.file(); + save(&path, &mb).unwrap(); + let dir = blob_dir(&path); + let (there, _) = the_pack(&path); + // A later save, appending to the pack that is there. + let mut later = pack::Writer::appending(&dir); + let block = b"the header lines of mail a later save brings"; + let at = later.place(&Ref::of(block), Some(block)).unwrap().unwrap(); + assert_eq!(at.pack, 1); + assert_handles_never_read_and_append(&there); + // A whole rewrite, appending to a pack it makes. + let mut fresh = pack::Writer::compacting(&dir, true); + let other = b"the header lines a fresh pack is made with"; + let made = fresh.place(&Ref::of(other), Some(other)).unwrap().unwrap(); + assert_ne!(made.pack, 1); + assert_handles_never_read_and_append(&dir.join(pack::name(made.pack).unwrap())); +} + +/// Every handle this process has open on `file` either doesn't read or doesn't append, as the +/// kernel was asked when it was opened (`/proc/self/fdinfo`; the access mode and `O_APPEND` of +/// Linux's generic ABI). And there is one: the writer holds the pack open. +#[cfg(target_os = "linux")] +fn assert_handles_never_read_and_append(file: &Path) { + const ACCESS: u32 = 0o3; + const WRITE_ONLY: u32 = 0o1; + const APPEND: u32 = 0o2000; + let file = fs::canonicalize(file).unwrap(); + let flags: Vec = fs::read_dir("/proc/self/fd") + .unwrap() + .flatten() + .filter(|fd| fs::read_link(fd.path()).is_ok_and(|to| to == file)) + .filter_map(|fd| fs::read_to_string(Path::new("/proc/self/fdinfo").join(fd.file_name())).ok()) + .filter_map(|info| info.lines().find_map(|l| l.strip_prefix("flags:")).and_then(|f| u32::from_str_radix(f.trim(), 8).ok())) + .collect(); + assert!(!flags.is_empty(), "nothing has {} open", file.display()); + let both: Vec = flags.iter().filter(|f| *f & APPEND != 0 && *f & ACCESS != WRITE_ONLY).map(|f| format!("{f:o}")).collect(); + assert!(both.is_empty(), "{} is open for reading and appending at once (flags {both:?})", file.display()); +} + /// What 20,000 messages with Exchange-like headers and short bodies take beside the mailbox file: /// how many files, and how much of the disk. /// diff --git a/docs/log.d/ROADMAP/progress/1871.md b/docs/log.d/ROADMAP/progress/1871.md new file mode 100644 index 0000000000..3b937d76b0 --- /dev/null +++ b/docs/log.d/ROADMAP/progress/1871.md @@ -0,0 +1 @@ +| 2026-11-27 | #1871: a pack of header lines is appended to through a handle that only appends, and what was written is read back through one that only reads: a file system that won't give one handle both for a file that is there (a Cryptomator vault) failed every save after the one that made the pack since #1376 (0.13.0), and what changed after it was lost at quit | diff --git a/docs/log.d/ROADMAP/revisions/1871.md b/docs/log.d/ROADMAP/revisions/1871.md new file mode 100644 index 0000000000..bcbab99286 --- /dev/null +++ b/docs/log.d/ROADMAP/revisions/1871.md @@ -0,0 +1 @@ +| 2026-11-27 | patch: progress log row for saves in a Cryptomator vault (#1871). | diff --git a/packaging/release-notes.d/fixes/1871.md b/packaging/release-notes.d/fixes/1871.md new file mode 100644 index 0000000000..3e5318d51c --- /dev/null +++ b/packaging/release-notes.d/fixes/1871.md @@ -0,0 +1 @@ +- A mailbox kept in an encrypted folder, such as a Cryptomator vault, is saved every time again, not only the first time: every later save failed there, and what had changed since was lost when MailCraft closed ([#1871](https://github.com/GITHUB_REPOSITORY/issues/1871)).