Skip to main content

shine_core/
persist.rs

1//! Shared atomic-write primitives.
2//!
3//! Several modules (config, app/sys/task manifests, ssh transfer, workspace
4//! env files, self-install) each hand-rolled their own "write to a temp file,
5//! then rename over the destination" sequence. This module is the single
6//! place that logic lives.
7
8use anyhow::{Context, Result};
9use std::path::Path;
10use tokio::io::AsyncWriteExt;
11
12/// Durably writes `contents` to `path`.
13///
14/// Creates the parent directory if missing, writes to a uniquely-named temp
15/// file in the same directory, fsyncs it, then renames it over `path`. If
16/// anything fails after the temp file is created, the temp file is removed.
17pub async fn atomic_write(path: &Path, contents: &[u8]) -> Result<()> {
18    let parent = path.parent().unwrap_or_else(|| Path::new("."));
19    tokio::fs::create_dir_all(parent)
20        .await
21        .with_context(|| format!("creating {}", parent.display()))?;
22    let temp = parent.join(format!(".shine-write-{}", uuid::Uuid::new_v4()));
23
24    if let Err(error) = write_temp(&temp, contents).await {
25        let _ = tokio::fs::remove_file(&temp).await;
26        return Err(error);
27    }
28
29    finalize_temp(&temp, path).await
30}
31
32/// Durably writes a private file with owner-only permissions on Unix.
33///
34/// The temporary file receives the restrictive mode before any content is
35/// written, so plaintext never has a wider visibility window before rename.
36pub async fn atomic_write_private(path: &Path, contents: &[u8]) -> Result<()> {
37    let parent = path.parent().unwrap_or_else(|| Path::new("."));
38    tokio::fs::create_dir_all(parent)
39        .await
40        .with_context(|| format!("creating {}", parent.display()))?;
41    let temp = parent.join(format!(".shine-write-{}", uuid::Uuid::new_v4()));
42
43    if let Err(error) = write_private_temp(&temp, contents).await {
44        let _ = tokio::fs::remove_file(&temp).await;
45        return Err(error);
46    }
47
48    finalize_temp(&temp, path).await
49}
50
51#[cfg(unix)]
52async fn write_private_temp(temp: &Path, contents: &[u8]) -> Result<()> {
53    let mut file = tokio::fs::OpenOptions::new()
54        .write(true)
55        .create_new(true)
56        .mode(0o600)
57        .open(temp)
58        .await
59        .with_context(|| format!("creating {}", temp.display()))?;
60    file.write_all(contents)
61        .await
62        .with_context(|| format!("writing {}", temp.display()))?;
63    file.sync_all()
64        .await
65        .with_context(|| format!("syncing {}", temp.display()))?;
66    Ok(())
67}
68
69#[cfg(not(unix))]
70async fn write_private_temp(temp: &Path, contents: &[u8]) -> Result<()> {
71    write_temp(temp, contents).await
72}
73
74async fn write_temp(temp: &Path, contents: &[u8]) -> Result<()> {
75    let mut file = tokio::fs::File::create(temp)
76        .await
77        .with_context(|| format!("creating {}", temp.display()))?;
78    file.write_all(contents)
79        .await
80        .with_context(|| format!("writing {}", temp.display()))?;
81    file.sync_all()
82        .await
83        .with_context(|| format!("syncing {}", temp.display()))?;
84    Ok(())
85}
86
87/// Renames `temp` over `dest`, for callers that already wrote/streamed their
88/// own temp file (e.g. large-file copies) and only need the finalize step.
89///
90/// On Windows, an existing `dest` is removed first (`rename` there doesn't
91/// replace an existing file). On any failure, `temp` is removed.
92pub async fn finalize_temp(temp: &Path, dest: &Path) -> Result<()> {
93    #[cfg(windows)]
94    if dest.exists() {
95        tokio::fs::remove_file(dest)
96            .await
97            .with_context(|| format!("removing {}", dest.display()))?;
98    }
99    if let Err(error) = tokio::fs::rename(temp, dest).await {
100        let _ = tokio::fs::remove_file(temp).await;
101        return Err(error).with_context(|| format!("replacing {}", dest.display()));
102    }
103    Ok(())
104}
105
106/// Loads and parses a TOML file at `path`, or returns `T::default()` if it
107/// doesn't exist yet. `what` is a human-readable label used in error
108/// messages (e.g. `"app manifest"`).
109pub async fn load_toml_or_default<T>(path: &Path, what: &str) -> Result<T>
110where
111    T: serde::de::DeserializeOwned + Default,
112{
113    match tokio::fs::read_to_string(path).await {
114        Ok(content) => toml::from_str(&content).with_context(|| format!("failed to parse {what}")),
115        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(T::default()),
116        Err(e) => Err(e).with_context(|| format!("failed to read {what}")),
117    }
118}
119
120/// Serializes `value` as pretty TOML and atomically writes it to `path`.
121/// `what` is a human-readable label used in error messages.
122pub async fn save_toml_atomic<T: serde::Serialize>(
123    value: &T,
124    path: &Path,
125    what: &str,
126) -> Result<()> {
127    let content =
128        toml::to_string_pretty(value).with_context(|| format!("failed to serialize {what}"))?;
129    atomic_write(path, content.as_bytes())
130        .await
131        .with_context(|| format!("failed to write {what}"))
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137
138    async fn make_temp_dir(label: &str) -> std::path::PathBuf {
139        let path = std::env::temp_dir().join(format!("{label}-{}", uuid::Uuid::new_v4()));
140        tokio::fs::create_dir_all(&path).await.unwrap();
141        path
142    }
143
144    #[tokio::test]
145    async fn atomic_write_creates_missing_parent_directories() {
146        let dir = make_temp_dir("shine-persist").await;
147        let path = dir.join("nested/deep/file.txt");
148
149        atomic_write(&path, b"hello").await.unwrap();
150
151        assert_eq!(tokio::fs::read(&path).await.unwrap(), b"hello");
152        tokio::fs::remove_dir_all(&dir).await.unwrap();
153    }
154
155    #[tokio::test]
156    async fn atomic_write_replaces_existing_file() {
157        let dir = make_temp_dir("shine-persist").await;
158        let path = dir.join("file.txt");
159        tokio::fs::write(&path, b"old").await.unwrap();
160
161        atomic_write(&path, b"new").await.unwrap();
162
163        assert_eq!(tokio::fs::read(&path).await.unwrap(), b"new");
164        tokio::fs::remove_dir_all(&dir).await.unwrap();
165    }
166
167    #[tokio::test]
168    async fn atomic_write_leaves_no_temp_file_behind_on_success() {
169        let dir = make_temp_dir("shine-persist").await;
170        let path = dir.join("file.txt");
171
172        atomic_write(&path, b"content").await.unwrap();
173
174        let mut entries = tokio::fs::read_dir(&dir).await.unwrap();
175        let mut names = Vec::new();
176        while let Some(entry) = entries.next_entry().await.unwrap() {
177            names.push(entry.file_name());
178        }
179        assert_eq!(names, vec![std::ffi::OsString::from("file.txt")]);
180        tokio::fs::remove_dir_all(&dir).await.unwrap();
181    }
182
183    #[cfg(unix)]
184    #[tokio::test]
185    async fn atomic_write_private_uses_owner_only_permissions() {
186        use std::os::unix::fs::PermissionsExt;
187
188        let dir = make_temp_dir("shine-persist-private").await;
189        let path = dir.join("secret.env");
190        tokio::fs::write(&path, b"old\n").await.unwrap();
191        tokio::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644))
192            .await
193            .unwrap();
194
195        atomic_write_private(&path, b"TOKEN=secret\n")
196            .await
197            .unwrap();
198
199        assert_eq!(tokio::fs::read(&path).await.unwrap(), b"TOKEN=secret\n");
200        let mode = tokio::fs::metadata(&path)
201            .await
202            .unwrap()
203            .permissions()
204            .mode();
205        assert_eq!(mode & 0o777, 0o600);
206        tokio::fs::remove_dir_all(&dir).await.unwrap();
207    }
208
209    #[tokio::test]
210    async fn finalize_temp_removes_temp_on_rename_failure() {
211        let dir = make_temp_dir("shine-persist").await;
212        let temp = dir.join(".shine-write-test");
213        tokio::fs::write(&temp, b"content").await.unwrap();
214        // A destination inside a nonexistent directory makes rename fail.
215        let dest = dir.join("missing-dir").join("dest.txt");
216
217        let result = finalize_temp(&temp, &dest).await;
218
219        assert!(result.is_err());
220        assert!(!temp.exists(), "temp file should be cleaned up on failure");
221        tokio::fs::remove_dir_all(&dir).await.unwrap();
222    }
223
224    #[derive(Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
225    struct SampleToml {
226        #[serde(default)]
227        name: String,
228        #[serde(default)]
229        count: u32,
230    }
231
232    #[tokio::test]
233    async fn load_toml_or_default_returns_default_when_file_missing() {
234        let dir = make_temp_dir("shine-persist").await;
235        let path = dir.join("sample.toml");
236
237        let value: SampleToml = load_toml_or_default(&path, "sample").await.unwrap();
238
239        assert_eq!(value, SampleToml::default());
240        tokio::fs::remove_dir_all(&dir).await.unwrap();
241    }
242
243    #[tokio::test]
244    async fn save_then_load_toml_round_trips() {
245        let dir = make_temp_dir("shine-persist").await;
246        let path = dir.join("sample.toml");
247        let value = SampleToml {
248            name: "hi".to_string(),
249            count: 3,
250        };
251
252        save_toml_atomic(&value, &path, "sample").await.unwrap();
253        let loaded: SampleToml = load_toml_or_default(&path, "sample").await.unwrap();
254
255        assert_eq!(loaded, value);
256        tokio::fs::remove_dir_all(&dir).await.unwrap();
257    }
258
259    #[tokio::test]
260    async fn save_toml_atomic_creates_missing_parent_directory() {
261        let dir = make_temp_dir("shine-persist").await;
262        let path = dir.join("nested/sample.toml");
263        let value = SampleToml::default();
264
265        save_toml_atomic(&value, &path, "sample").await.unwrap();
266
267        assert!(path.exists());
268        tokio::fs::remove_dir_all(&dir).await.unwrap();
269    }
270}