par_term/atomic_save.rs
1//! Crash-safe atomic file writes — re-exported from `par-term-config`.
2//!
3//! The implementation lives in [`par_term_config::atomic_save`]. It had to move
4//! out of the root crate because `par-term-config`, `par-term-settings-ui` and
5//! `par-term-update` all write files that must be atomic and none of them can
6//! depend on the root crate; a Layer-1 home is the only one all four can reach.
7//! Duplicating a security-sensitive write path into a second crate was the
8//! alternative and was rejected.
9//!
10//! This module stays so that existing `crate::atomic_save::…` call sites keep
11//! working. See the upstream module for the durability and permission
12//! contracts, including when to prefer [`save_string_atomic_preserving_mode`]
13//! over the `0o600`-forcing variants.
14
15pub use par_term_config::atomic_save::{
16 save_bytes_atomic, save_bytes_atomic_preserving_mode, save_string_atomic,
17 save_string_atomic_preserving_mode, save_yaml_atomic,
18};
19
20#[cfg(test)]
21mod tests {
22 use super::*;
23 use std::fs;
24 use tempfile::tempdir;
25
26 /// Referenced by name from `src/profile/storage.rs`, and the one case worth
27 /// re-running through the re-export: that a failed save leaves the previous
28 /// file byte-for-byte intact. The rest of the contract is covered in
29 /// `par_term_config::atomic_save`.
30 #[test]
31 fn failed_save_leaves_the_previous_file_intact() {
32 let temp = tempdir().expect("tempdir");
33 let path = temp.path().join("data.yaml");
34
35 save_string_atomic(&path, "good data").expect("initial save");
36
37 // A directory in the way makes the rename fail after the payload has
38 // already been written and fsynced.
39 let blocked = temp.path().join("blocked");
40 fs::create_dir(&blocked).expect("blocking directory");
41 assert!(save_string_atomic(&blocked, "payload").is_err());
42
43 assert_eq!(fs::read_to_string(&path).expect("read"), "good data");
44 }
45}