From 6809e7413743ae51013f7f487844abfe793645c6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 22 Oct 2026 22:26:00 +0200 Subject: [PATCH] cultcache-rs 0.3.1: push_all reads the store stamp under the lock and refuses a format it does not read One owner of the stamp rule, refuse_unread_format, serves read_all_unlocked and push_all. A file push_all cannot read is written as before. Builds. Co-Authored-By: Claude Sonnet 5.5 --- packages/cultcache-rs/CHANGELOG.md | 12 ++++++++ packages/cultcache-rs/Cargo.toml | 2 +- packages/cultcache-rs/src/lib.rs | 49 ++++++++++++++++++++---------- 3 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 packages/cultcache-rs/CHANGELOG.md diff --git a/packages/cultcache-rs/CHANGELOG.md b/packages/cultcache-rs/CHANGELOG.md new file mode 100644 index 00000000..4fa680b9 --- /dev/null +++ b/packages/cultcache-rs/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to `cultcache-rs` are recorded here, following +[Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Earlier releases are recorded +only in git history. + +## [0.3.1] + +### Fixed +- `push_all` reads the store's header under the lock and refuses a format this runtime does + not read, with `CultCacheStoreUnreadable` `UnsupportedFormat`, leaving the file as it was. + A file with no readable stamp is still replaced, so a whole write heals it. diff --git a/packages/cultcache-rs/Cargo.toml b/packages/cultcache-rs/Cargo.toml index 9610aeb6..7954370c 100644 --- a/packages/cultcache-rs/Cargo.toml +++ b/packages/cultcache-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cultcache-rs" -version = "0.3.0" +version = "0.3.1" edition = "2024" license = "MIT" publish = false diff --git a/packages/cultcache-rs/src/lib.rs b/packages/cultcache-rs/src/lib.rs index 6c2c9030..2818c638 100644 --- a/packages/cultcache-rs/src/lib.rs +++ b/packages/cultcache-rs/src/lib.rs @@ -1261,28 +1261,40 @@ impl SingleFileMessagePackBackingStore { .context(format!("failed to read {}", self.path.display()))); } }; - let unreadable = |kind, detail: anyhow::Error| { - // The detail is rebuilt as text that names no value read from the bytes. The - // decoder reports a short read as an io::Error, and dropping it keeps an - // io::Error in the chain meaning only that the file itself could not be read. - anyhow!(describe_undecodable(&detail)).context(CultCacheStoreUnreadable { - path: self.path.clone(), - kind, - }) - }; - match store_header(&bytes) { - Some(header) if header == STORE_FORMAT_V1 => decode_store_snapshot(&bytes) - .map_err(|error| unreadable(CultCacheStoreUnreadableKind::Undecodable, error)), - Some(header) if header.starts_with("cultcache.store.") => Err(unreadable( + self.refuse_unread_format(&bytes)?; + if store_header(&bytes).as_deref() == Some(STORE_FORMAT_V1) { + decode_store_snapshot(&bytes) + .map_err(|error| self.unreadable(CultCacheStoreUnreadableKind::Undecodable, error)) + } else { + decode_grown(&bytes) + .context("failed to decode legacy CultCache envelope array") + .map_err(|error| self.unreadable(CultCacheStoreUnreadableKind::Undecodable, error)) + } + } + + // The stamp rule, owned once for the reader and the whole-store writer: a + // cultcache.store.* header other than v1 is a format this runtime does not read. Only + // the header is judged, so a newer store cut short is still a newer store. + fn refuse_unread_format(&self, bytes: &[u8]) -> Result<()> { + match store_header(bytes) { + Some(header) if header != STORE_FORMAT_V1 && header.starts_with("cultcache.store.") => Err(self.unreadable( CultCacheStoreUnreadableKind::UnsupportedFormat, anyhow!("its header is {}; this runtime reads {STORE_FORMAT_V1:?} only", describe_header(&header)), )), - _ => decode_grown(&bytes) - .context("failed to decode legacy CultCache envelope array") - .map_err(|error| unreadable(CultCacheStoreUnreadableKind::Undecodable, error)), + _ => Ok(()), } } + fn unreadable(&self, kind: CultCacheStoreUnreadableKind, detail: anyhow::Error) -> anyhow::Error { + // The detail is rebuilt as text that names no value read from the bytes. The + // decoder reports a short read as an io::Error, and dropping it keeps an + // io::Error in the chain meaning only that the file itself could not be read. + anyhow!(describe_undecodable(&detail)).context(CultCacheStoreUnreadable { + path: self.path.clone(), + kind, + }) + } + /// Reads one filesystem snapshot without creating or opening the sibling /// CultCache lock file. /// @@ -1571,6 +1583,11 @@ impl CacheBackingStore for SingleFileMessagePackBackingStore { fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> { self.with_exclusive_lock(|| { + // A file it cannot read is written as before (a dangling link is replaced, R3; a + // directory is a typed write failure); only a stamp it can read is judged. + if let Ok(bytes) = fs::read(&self.path) { + self.refuse_unread_format(&bytes)?; + } let mut entries = entries.to_vec(); entries.sort_by_key(entry_id); self.write_all_unlocked(&entries)