forge
Explore
Log in

Don't you dare go Hollow

Relocatable relative-launch saves, fsync before rename

Browse files at this commit

9fa0211665b0b02872b7d0a8dbefb9fcea041d73Ahmet Kaan GümüşParent: 7f4c921
  • fix
    • relocatable relative-launch saves, fsync before rename
Show full message
🐛 fix: relocatable relative-launch saves, fsync before rename

A project path with a non-canonical parent -- a bare filename from
`cd dir && edith clip.mp4`, or a directory reached through a symlink --
never matched the canonical source prefix, so every source line came out
absolute and the folder could not be moved. The save also published the
rename without an fsync, which a power loss can turn into a short file
under the real name.
crates/engine/src/edith.rsmodified+63 -5
@@ -24,9 +24,12 @@
2424 //! timeline is [`crate::PlaybackSession::open_project`]'s business.
2525 //!
2626 //! 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.
2830
2931 use std::ffi::OsString;
32+use std::io::Write;
3033 use std::os::unix::ffi::{OsStrExt, OsStringExt};
3134 use std::path::{Path, PathBuf};
3235
@@ -49,15 +52,21 @@ pub struct Document {
4952 /// Writes the project to `path`, atomically. `sources` should already be
5053 /// orphan-free ([`crate::Project::without_orphan_sources`]).
5154 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);
5356 let mut part = path.to_path_buf().into_os_string();
5457 part.push(".part");
5558 let part = PathBuf::from(part);
5659 // The rename publishes the file under the caller's name in one step, on the
5760 // 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());
6170 if result.is_err() {
6271 let _ = std::fs::remove_file(&part);
6372 }
@@ -69,6 +78,20 @@ pub fn load(path: &Path) -> crate::Result<Document> {
6978 parse(&data, path.parent().unwrap_or(Path::new("")))
7079 }
7180
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+
7295 fn emit(dir: &Path, sources: &[PathBuf], clips: &[Clip], playhead: u32) -> Vec<u8> {
7396 let mut out = Vec::new();
7497 out.extend_from_slice(MAGIC);
@@ -273,6 +296,41 @@ mod tests {
273296 assert_eq!(emit(&dir, &back.sources, &back.clips, back.playhead), bytes);
274297 }
275298
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+
276334 #[test]
277335 fn escapes_survive_the_bytes_json_would_not() {
278336 let dir = PathBuf::from("/proj");