Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions crates/engine/src/store_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ pub(super) struct Writer {
lens: HashMap<u32, u64>,
/// 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<Ref>,
/// The pack an opening wrote into unflushed was flushed by [`Writer::sync`].
Expand All @@ -320,6 +323,7 @@ impl Writer {
placed: HashMap::new(),
lens: HashMap::new(),
reader: None,
check: None,
loose: Vec::new(),
synced_known: false,
wrote: 0,
Expand Down Expand Up @@ -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)?;
Expand All @@ -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);
Expand All @@ -475,21 +482,29 @@ 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<Loc> {
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))?;
at.checked_add(len).ok_or_else(|| std::io::Error::other("the pack is full"))?;
// (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);
Expand Down
51 changes: 51 additions & 0 deletions crates/engine/src/store_pack_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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<String> = 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.
///
Expand Down
1 change: 1 addition & 0 deletions docs/log.d/ROADMAP/progress/1871.md
Original file line number Diff line number Diff line change
@@ -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 |
1 change: 1 addition & 0 deletions docs/log.d/ROADMAP/revisions/1871.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| 2026-11-27 | patch: progress log row for saves in a Cryptomator vault (#1871). |
1 change: 1 addition & 0 deletions packaging/release-notes.d/fixes/1871.md
Original file line number Diff line number Diff line change
@@ -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)).