Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/file_save.rs

4.0 KiB, 1 run

created by r3025735233:77620, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A save of a JDAT file replaces it whole, or leaves the old one as it was.
2//!
3//! `JdatMapFile::save` and `JdatFile::save` once truncated the file in place and then wrote it a
4//! line at a time, so a save that stopped part way left a configuration that no longer loaded.
5//! They now write a sibling temporary and rename it over the file. A hard link to the old file
6//! shows the difference: a rewrite in place changes what the link reads, and a rename does not.
7#![cfg(unix)]
8
9use oxedyne_fe2o3_core::{
10 prelude::*,
11 file as core_file,
12};
13use oxedyne_fe2o3_jdat::{
14 prelude::*,
15 cfg::Config,
16 file::{
17 JdatFile,
18 JdatMapFile,
19 },
20 string::enc::EncoderConfig,
21};
22
23use std::{
24 collections::BTreeMap,
25 fs,
26 os::unix::fs::PermissionsExt,
27 path::PathBuf,
28};
29
30#[derive(Clone, Debug, Default, Eq, PartialEq, FromDatMap, ToDatMap)]
31struct Settings {
32 name: String,
33 size: u64,
34}
35
36impl Config for Settings {
37 fn check_and_fix(&mut self) -> Outcome<()> { Ok(()) }
38}
39
40#[derive(Clone, Debug)]
41struct Doc(Dat);
42
43impl ToDat for Doc {
44 fn to_dat(&self) -> Outcome<Dat> { Ok(self.0.clone()) }
45}
46
47impl FromDat for Doc {
48 fn from_dat(dat: Dat) -> Outcome<Self> { Ok(Self(dat)) }
49}
50
51impl JdatFile for Doc {}
52
53// A fresh directory of this test's own under the target's scratch area.
54fn scratch(label: &str) -> Outcome<PathBuf> {
55 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
56 .join(fmt!("jdat_file_save_{}_{}", std::process::id(), label));
57 let _ = fs::remove_dir_all(&dir);
58 res!(fs::create_dir_all(&dir));
59 Ok(dir)
60}
61
62fn old_settings() -> Settings { Settings { name: "old".to_string(), size: 1 } }
63fn new_settings() -> Settings { Settings { name: "a new and longer name".to_string(), size: 2 } }
64
65#[test]
66fn a_map_file_save_replaces_the_file_rather_than_rewriting_it() -> Outcome<()> {
67 let dir = res!(scratch("map_link"));
68 let path = dir.join("settings.jdat");
69 let twin = dir.join("twin.jdat");
70 res!(old_settings().save(&path, "\t", true));
71 let before = res!(fs::read(&path));
72 res!(fs::hard_link(&path, &twin));
73
74 res!(new_settings().save(&path, "\t", true));
75
76 // The link still names the old inode, untouched and whole.
77 assert_eq!(res!(fs::read(&twin)), before);
78 assert_eq!(res!(Settings::load(&twin)), old_settings());
79 assert_eq!(res!(Settings::load(&path)), new_settings());
80 let _ = fs::remove_dir_all(&dir);
81 Ok(())
82}
83
84#[test]
85fn a_dat_file_save_replaces_the_file_rather_than_rewriting_it() -> Outcome<()> {
86 let dir = res!(scratch("dat_link"));
87 let path = dir.join("doc.jdat");
88 let twin = dir.join("twin.jdat");
89 let cfg = || Some(EncoderConfig::<(), ()>::default());
90 res!(Doc(Dat::Str("old".to_string())).save(&path, "\t", cfg()));
91 let before = res!(fs::read(&path));
92 res!(fs::hard_link(&path, &twin));
93
94 res!(Doc(Dat::Str("a new and longer text".to_string())).save(&path, "\t", cfg()));
95
96 assert_eq!(res!(fs::read(&twin)), before);
97 let doc = res!(Doc::load(&path, None::<oxedyne_fe2o3_jdat::string::dec::DecoderConfig<(), ()>>));
98 assert_eq!(doc.0, Dat::Str("a new and longer text".to_string()));
99 let _ = fs::remove_dir_all(&dir);
100 Ok(())
101}
102
103// A save that cannot finish must leave the old file whole. A directory the caller cannot write
104// stops the save before the rename, while the file itself stays writable, as a rewrite in place
105// would need.
106#[test]
107fn a_save_that_cannot_finish_leaves_the_old_file_whole() -> Outcome<()> {
108 if res!(core_file::effective_uid()) == 0 {
109 // Root writes a read-only directory regardless, so the failure cannot be staged.
110 return Ok(());
111 }
112 let dir = res!(scratch("map_fail"));
113 let path = dir.join("settings.jdat");
114 res!(old_settings().save(&path, "\t", true));
115 let before = res!(fs::read(&path));
116 res!(fs::set_permissions(&dir, fs::Permissions::from_mode(0o555)));
117
118 let saved = new_settings().save(&path, "\t", true);
119
120 res!(fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)));
121 let after = res!(fs::read(&path));
122 let _ = fs::remove_dir_all(&dir);
123 assert!(saved.is_err(), "A save into a directory it cannot write should fail.");
124 assert_eq!(after, before);
125 Ok(())
126}