Relocatable relative-launch saves, fsync before rename
Browse files at this commit9fa0211665b0b02872b7d0a8dbefb9fcea041d73Parent: 7f4c921- fix
- relocatable relative-launch saves, fsync before rename
1 changed file +63 -5
- crates/engine/src/edith.rs +63 -5
crates/engine/src/edith.rsmodified+63 -5
| @@ -24,9 +24,12 @@ | ||
| 24 | 24 | //! timeline is [`crate::PlaybackSession::open_project`]'s business. |
| 25 | 25 | //! |
| 26 | 26 | //! Writing goes through `<path>.part` and a rename, as an export does, so an |
| 27 | -//! interrupted save cannot destroy the previous version of the project. | |
| 27 | +//! interrupted save cannot destroy the previous version of the project. The | |
| 28 | +//! bytes are fsynced before the rename and the directory after it, so what a | |
| 29 | +//! power loss can lose is a whole save, never half of one under the real name. | |
| 28 | 30 | |
| 29 | 31 | use std::ffi::OsString; |
| 32 | +use std::io::Write; | |
| 30 | 33 | use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 31 | 34 | use std::path::{Path, PathBuf}; |
| 32 | 35 | |
| @@ -49,15 +52,21 @@ pub struct Document { | ||
| 49 | 52 | /// Writes the project to `path`, atomically. `sources` should already be |
| 50 | 53 | /// orphan-free ([`crate::Project::without_orphan_sources`]). |
| 51 | 54 | pub fn save(path: &Path, sources: &[PathBuf], clips: &[Clip], playhead: u32) -> crate::Result<()> { |
| 52 | - let dir = path.parent().unwrap_or(Path::new("")); | |
| 55 | + let dir = project_dir(path); | |
| 53 | 56 | let mut part = path.to_path_buf().into_os_string(); |
| 54 | 57 | part.push(".part"); |
| 55 | 58 | let part = PathBuf::from(part); |
| 56 | 59 | // The rename publishes the file under the caller's name in one step, on the |
| 57 | 60 | // same directory; until it happens the old project file is still the whole |
| 58 | - // truth. | |
| 59 | - let result = std::fs::write(&part, emit(dir, sources, clips, playhead)) | |
| 60 | - .and_then(|()| std::fs::rename(&part, path)); | |
| 61 | + // truth. `sync_all` puts the bytes on the disk before that name exists, and | |
| 62 | + // the second one puts the name itself there. | |
| 63 | + let result = std::fs::File::create(&part) | |
| 64 | + .and_then(|mut f| { | |
| 65 | + f.write_all(&emit(&dir, sources, clips, playhead))?; | |
| 66 | + f.sync_all() | |
| 67 | + }) | |
| 68 | + .and_then(|()| std::fs::rename(&part, path)) | |
| 69 | + .and_then(|()| std::fs::File::open(&dir)?.sync_all()); | |
| 61 | 70 | if result.is_err() { |
| 62 | 71 | let _ = std::fs::remove_file(&part); |
| 63 | 72 | } |
| @@ -69,6 +78,20 @@ pub fn load(path: &Path) -> crate::Result<Document> { | ||
| 69 | 78 | parse(&data, path.parent().unwrap_or(Path::new(""))) |
| 70 | 79 | } |
| 71 | 80 | |
| 81 | +/// The directory a project file's relative source lines are measured from, in | |
| 82 | +/// the same canonical form [`crate::Project`] keeps its sources in -- without | |
| 83 | +/// that the prefix simply never matches and every line comes out absolute, | |
| 84 | +/// which is what `cd dir && edith clip.mp4` + save used to write (the parent of | |
| 85 | +/// a bare filename is `""`). Canonicalizing fails only if the directory does | |
| 86 | +/// not exist, and then the write is about to fail anyway. | |
| 87 | +fn project_dir(path: &Path) -> PathBuf { | |
| 88 | + let dir = path | |
| 89 | + .parent() | |
| 90 | + .filter(|p| !p.as_os_str().is_empty()) | |
| 91 | + .unwrap_or(Path::new(".")); | |
| 92 | + dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf()) | |
| 93 | +} | |
| 94 | + | |
| 72 | 95 | fn emit(dir: &Path, sources: &[PathBuf], clips: &[Clip], playhead: u32) -> Vec<u8> { |
| 73 | 96 | let mut out = Vec::new(); |
| 74 | 97 | out.extend_from_slice(MAGIC); |
| @@ -273,6 +296,41 @@ mod tests { | ||
| 273 | 296 | assert_eq!(emit(&dir, &back.sources, &back.clips, back.playhead), bytes); |
| 274 | 297 | } |
| 275 | 298 | |
| 299 | + /// The relocatability promise, from the two directions a project path | |
| 300 | + /// reaches [`save`] non-canonical: a bare filename (`cd dir && edith | |
| 301 | + /// clip.mp4`, whose parent is `""`) and a directory reached through a | |
| 302 | + /// symlink. Both used to emit absolute source lines. | |
| 303 | + #[test] | |
| 304 | + fn a_non_canonical_project_path_still_writes_relative_sources() { | |
| 305 | + assert_eq!( | |
| 306 | + project_dir(Path::new("a.edith")), | |
| 307 | + std::env::current_dir().expect("cwd"), | |
| 308 | + "a bare filename is saved into the working directory" | |
| 309 | + ); | |
| 310 | + | |
| 311 | + let dir = std::env::temp_dir().join(format!("ve_edith_link_{}", std::process::id())); | |
| 312 | + let _ = std::fs::remove_dir_all(&dir); | |
| 313 | + std::fs::create_dir_all(dir.join("real")).expect("scratch dir"); | |
| 314 | + let dir = std::fs::canonicalize(&dir).expect("canonical scratch dir"); | |
| 315 | + std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).expect("symlink"); | |
| 316 | + let source = dir.join("real/a.mp4"); | |
| 317 | + let path = dir.join("link/p.edith"); | |
| 318 | + | |
| 319 | + save(&path, &[source], &[clip(0, 30, 0)], 0).expect("save"); | |
| 320 | + let bytes = std::fs::read(&path).expect("read back"); | |
| 321 | + assert_eq!( | |
| 322 | + String::from_utf8_lossy(&bytes), | |
| 323 | + "edith 1\nplayhead 0\nsource a.mp4\nclip 0 30 0\n" | |
| 324 | + ); | |
| 325 | + // Loading rejoins the *given* directory, so the file is reached by the | |
| 326 | + // way the project was opened -- the same file, through the link. | |
| 327 | + assert_eq!( | |
| 328 | + load(&path).expect("load").sources, | |
| 329 | + vec![dir.join("link/a.mp4")] | |
| 330 | + ); | |
| 331 | + std::fs::remove_dir_all(&dir).expect("cleanup"); | |
| 332 | + } | |
| 333 | + | |
| 276 | 334 | #[test] |
| 277 | 335 | fn escapes_survive_the_bytes_json_would_not() { |
| 278 | 336 | let dir = PathBuf::from("/proj"); |