state, profile, ui, names: one atomic write with a per-process temp file (profiles and the names cache were written in place)
This commit is contained in:
1 parent
fb5c99d1f2
commit
10a1d94fdd
4 files changed
+25
-16
No files matched your search
@@ -18,8 +18,7 @@ pub fn load() -> Names {
|
||||
}
|
||||
|
||||
pub fn save(n: &Names) -> Result<()> {
|
||||
let p = path()?; std::fs::create_dir_all(p.parent().unwrap())?;
|
||||
std::fs::write(p, serde_json::to_string(n)?)?; Ok(())
|
||||
crate::state::write_atomic(&path()?, serde_json::to_string(n)?.as_bytes())
|
||||
}
|
||||
|
||||
/// Parse one appdetails response body for `id`.
|
||||
|
||||
@@ -46,8 +46,7 @@ pub fn load(id: &str) -> Result<Profile> {
|
||||
|
||||
pub fn save(id: &str, p: &Profile) -> Result<PathBuf> {
|
||||
let f = file(id)?;
|
||||
std::fs::create_dir_all(f.parent().unwrap())?;
|
||||
std::fs::write(&f, toml::to_string_pretty(p)?)?;
|
||||
crate::state::write_atomic(&f, toml::to_string_pretty(p)?.as_bytes())?;
|
||||
Ok(f)
|
||||
}
|
||||
|
||||
|
||||
@@ -33,12 +33,17 @@ pub fn load() -> State {
|
||||
path().ok().and_then(|p| std::fs::read_to_string(p).ok()).and_then(|s| serde_json::from_str(&s).ok()).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn save(s: &State) -> Result<()> {
|
||||
let p = path()?;
|
||||
std::fs::create_dir_all(p.parent().unwrap())?;
|
||||
let tmp = p.with_extension("tmp");
|
||||
std::fs::write(&tmp, serde_json::to_string_pretty(s)?)?;
|
||||
std::fs::rename(tmp, p)?; // atomic: a crash mid-write never corrupts the file
|
||||
pub fn save(s: &State) -> Result<()> { write_atomic(&path()?, serde_json::to_string_pretty(s)?.as_bytes()) }
|
||||
|
||||
/// Write through a temporary file and rename it over `p`: a crash mid-write never leaves half a file. The temporary
|
||||
/// name carries the process id, so the launcher and a `summit run` saving at the same moment do not write into one
|
||||
/// temporary file (each rename is whole; the later one wins).
|
||||
pub fn write_atomic(p: &std::path::Path, bytes: &[u8]) -> Result<()> {
|
||||
if let Some(d) = p.parent() { std::fs::create_dir_all(d)?; }
|
||||
let mut tmp = p.as_os_str().to_os_string();
|
||||
tmp.push(format!(".{}.tmp", std::process::id()));
|
||||
std::fs::write(&tmp, bytes)?;
|
||||
std::fs::rename(&tmp, p).inspect_err(|_| { let _ = std::fs::remove_file(&tmp); })?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -121,5 +126,15 @@ mod tests {
|
||||
assert_eq!(path_text(std::path::Path::new("/games/café/run.sh")).unwrap(), "/games/café/run.sh");
|
||||
}
|
||||
#[test]
|
||||
fn write_atomic_replaces_and_leaves_no_temp() {
|
||||
let d = std::env::temp_dir().join(format!("summit-state-atomic-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
let p = d.join("sub/state.json");
|
||||
write_atomic(&p, b"one").unwrap();
|
||||
write_atomic(&p, b"two").unwrap();
|
||||
assert_eq!(std::fs::read(&p).unwrap(), b"two");
|
||||
assert_eq!(std::fs::read_dir(d.join("sub")).unwrap().count(), 1);
|
||||
}
|
||||
#[test]
|
||||
fn pin_idempotent() { let mut s = State::default(); s.pin("a"); s.pin("a"); assert_eq!(s.pinned.len(), 1); s.unpin("a"); assert!(s.pinned.is_empty()); }
|
||||
}
|
||||
@@ -87,11 +87,7 @@ pub fn load_from(p: &Path) -> UiSettings {
|
||||
}
|
||||
|
||||
pub fn save_to(p: &Path, s: &UiSettings) -> Result<()> {
|
||||
if let Some(d) = p.parent() { std::fs::create_dir_all(d)?; }
|
||||
let tmp = p.with_extension("toml.tmp");
|
||||
std::fs::write(&tmp, toml::to_string_pretty(&s.clone().sanitized())?)?;
|
||||
std::fs::rename(tmp, p)?; // atomic, like state.json
|
||||
Ok(())
|
||||
crate::state::write_atomic(p, toml::to_string_pretty(&s.clone().sanitized())?.as_bytes())
|
||||
}
|
||||
|
||||
pub fn load() -> UiSettings { path().map(|p| load_from(&p)).unwrap_or_default() }
|
||||
@@ -122,7 +118,7 @@ mod tests {
|
||||
let s = UiSettings { layout: "list".into(), theme: "violet".into(), anim_speed: 0.0, anim_style: "snappy".into(), ui_scale: 1.25, replay_seconds: 600, clip_seconds: 180, shortcuts: default_shortcuts() };
|
||||
save_to(&p, &s).unwrap();
|
||||
assert_eq!(load_from(&p), s);
|
||||
assert!(!p.with_extension("toml.tmp").exists());
|
||||
assert_eq!(std::fs::read_dir(p.parent().unwrap()).unwrap().count(), 1, "no temporary file left");
|
||||
}
|
||||
#[test]
|
||||
fn garbage_file_gives_defaults() {
|
||||
|
||||
Reference in new issue
Block a user