From 233d9e9e51ffb32e0eece509c23e95d2dac83386 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 12:16:32 +0100 Subject: [PATCH] refactor(core): atomic file writes from proton-base-rs Config, machine id and mount-failure files go through proton_base_rs::atomic. Same modes as before (config 0600, the others 0644); the mount-failure file is now synced before its rename. --- Cargo.lock | 25 +++++++++++++++++------- Cargo.toml | 1 + crates/pdfs-core/Cargo.toml | 1 + crates/pdfs-core/src/config.rs | 32 +++---------------------------- crates/pdfs-core/src/mountfail.rs | 4 +--- docs/CHANGELOG.md | 3 +++ 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae17aa2..41af14a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1476,7 +1476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1931,7 +1931,7 @@ dependencies = [ "gobject-sys", "libc", "system-deps 9.0.1", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -3633,6 +3633,7 @@ dependencies = [ "libc", "parking_lot 0.13.0", "proton-auth-rs", + "proton-base-rs", "proton-drive-rs", "proton-gallery-rs", "proton-sdk", @@ -3938,6 +3939,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "proton-base-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451151bb1025ea5cb362f69e2bfb2d45afed603e5c6daa3dc752333a7c29c878" +dependencies = [ + "rusqlite", + "thiserror 2.0.21", +] + [[package]] name = "proton-drive-rs" version = "0.12.1" @@ -4155,7 +4166,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4610,7 +4621,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4667,7 +4678,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -5175,7 +5186,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -5840,7 +5851,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f3010ae..bf0cfef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ keyring-core = "1" # The keyring store all three apps share: reconnects after a Secret Service # restart (B285) and replaces items instead of editing them (B291). proton-auth-rs = "0.1" +proton-base-rs = "0.1" dbus-secret-service-keyring-store = { version = "1", features = ["crypto-rust"] } # The same Secret Service client the store wraps, used directly to replace an # item instead of editing it, which oo7 before 0.7.0 kept only in memory (B291). diff --git a/crates/pdfs-core/Cargo.toml b/crates/pdfs-core/Cargo.toml index 9d4733c..47cdd42 100644 --- a/crates/pdfs-core/Cargo.toml +++ b/crates/pdfs-core/Cargo.toml @@ -26,6 +26,7 @@ sha2.workspace = true rusqlite.workspace = true keyring-core.workspace = true proton-auth-rs = { workspace = true, features = ["keyring"] } +proton-base-rs.workspace = true dbus-secret-service-keyring-store.workspace = true dbus-secret-service.workspace = true parking_lot.workspace = true diff --git a/crates/pdfs-core/src/config.rs b/crates/pdfs-core/src/config.rs index 2b93b00..7400bd4 100644 --- a/crates/pdfs-core/src/config.rs +++ b/crates/pdfs-core/src/config.rs @@ -457,26 +457,7 @@ fn load_config_at(path: &Path) -> AppConfig { /// [`AppDirs::save_config`] for the file at `path`. fn save_config_at(path: &Path, config: &AppConfig) -> Result<()> { refuse_to_replace_unreadable(path)?; - let parent = path - .parent() - .ok_or_else(|| Error::Other("configuration path has no parent".into()))?; - std::fs::create_dir_all(parent)?; - let nonce = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|duration| duration.as_nanos()) - .unwrap_or(0); - let tmp = path.with_extension(format!("json.tmp-{}-{nonce}", std::process::id())); - use std::os::unix::fs::OpenOptionsExt as _; - let mut file = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .mode(0o600) - .open(&tmp)?; - use std::io::Write as _; - file.write_all(&serde_json::to_vec_pretty(config)?)?; - file.sync_all()?; - std::fs::rename(&tmp, path)?; - std::fs::File::open(parent)?.sync_all()?; + proton_base_rs::atomic::write(path, &serde_json::to_vec_pretty(config)?)?; Ok(()) } @@ -621,15 +602,8 @@ impl AppDirs { use std::io::Read as _; std::fs::File::open("/dev/urandom")?.read_exact(&mut bytes)?; let id: String = bytes.iter().map(|b| format!("{b:02x}")).collect(); - let parent = path - .parent() - .ok_or_else(|| Error::Other("machine id path has no parent".into()))?; - std::fs::create_dir_all(parent)?; - let tmp = path.with_extension(format!("tmp-{}", std::process::id())); - std::fs::write(&tmp, format!("{id}\n"))?; - std::fs::File::open(&tmp)?.sync_all()?; - std::fs::rename(&tmp, &path)?; - std::fs::File::open(parent)?.sync_all()?; + // Not secret, and other tools may read it; keep it world-readable. + proton_base_rs::atomic::write_with_mode(&path, format!("{id}\n").as_bytes(), 0o644)?; Ok(id) } diff --git a/crates/pdfs-core/src/mountfail.rs b/crates/pdfs-core/src/mountfail.rs index e99f245..277d98d 100644 --- a/crates/pdfs-core/src/mountfail.rs +++ b/crates/pdfs-core/src/mountfail.rs @@ -40,9 +40,7 @@ pub fn path(state_dir: &Path) -> PathBuf { /// and renamed, so a reader never sees half of it. pub fn record(state_dir: &Path, failure: &MountFailure) -> std::io::Result<()> { let json = serde_json::to_vec(failure).map_err(std::io::Error::other)?; - let tmp = state_dir.join(format!("{FILE_NAME}.tmp")); - std::fs::write(&tmp, json)?; - std::fs::rename(&tmp, path(state_dir)) + proton_base_rs::atomic::write_with_mode(&path(state_dir), &json, 0o644) } /// Forget the failure: a mount is up, or a new daemon has started. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3e48117..c9f5905 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -22,6 +22,9 @@ scratch (user data in `staging/` and `recovery/` is never touched by this). - The keyring code (one Secret Service connection that reconnects after the provider restarts, B285, and replacing the session item instead of editing it, B291) now comes from `proton-auth-rs`, shared with Proton Gallery and Proton Stream. No behaviour change. +- Config, machine-id and mount-failure files are written through `proton-base-rs`'s + atomic writer (temporary file, sync, rename, directory sync). The mount-failure file + is now synced before the rename too, so a crash cannot leave it empty. ## [3.9.0] — 2026-11-19