authoravatarclover caruso<git@paperclover.net>2026-10-19 21:39:50-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:31:22-07:00
log9dc3da1b71afbdef3136fa4bf9a3ab40933a6d5b
tree39cdcf193a28db6cf8685d1392b74e256d11846f
parent198f258a1201fb5ea3088b6a4257fb2436e34368
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(notebook): a move or copy tells what happened when its last steps fail

A local rename now refuses, without a check beforehand, a name a file takes meanwhile, where the volume can rename so; a mounted share that can't, as macOS's smbfs, renames as before. A section file created in a local folder is flushed with a plain fsync where macOS's smbfs refuses F_FULLFSYNC, as onestore's commits are, so a notebook on a mounted share makes sections again, and a folder that can't sync its entries is not an error. A rename into the recycle bin that landed before a close or a folder's sync failed counts as a move, not as a section kept in both notebooks. A move reads the section's listing before copying, so a listing that changed since refuses it before any copy is made. The moved section's replica is deleted only while nothing holds it. A copy is written under a hidden name of its own, so two clients copying like-named sections into one folder never take each other's. Refs #105 Assisted-by: claude-opus-5.5

2 files changed, 94 insertions(+), 22 deletions(-)

crates/notebook/src/session.rs+61-21
......@@ -123,15 +123,31 @@ pub trait Storage: Send + Sync {
123123 }
124124}
125125
126/// Makes the entry naming `path` in its folder durable, where the system lets a folder sync.
127fn synced_folder(path: &Path) -> Result<()> {
126/// Makes the entry naming `path` in its folder durable, where the volume lets a folder sync;
127/// a mounted share may not.
128fn synced_folder(path: &Path) {
128129 #[cfg(unix)]
129 if let Some(folder) = path.parent() {
130 fs::File::open(folder)?.sync_all()?;
130 if let Some(folder) = path.parent()
131 && let Ok(folder) = fs::File::open(folder)
132 {
133 let _ = synced(&folder);
131134 }
132135 #[cfg(not(unix))]
133136 let _ = path;
134 Ok(())
137}
138
139/// `file.sync_all()`, which on a volume without macOS's F_FULLFSYNC, as an SMB mount is, falls
140/// back to a plain fsync, as `onestore`'s commits do.
141fn synced(file: &fs::File) -> io::Result<()> {
142 let result = file.sync_all();
143 #[cfg(target_vendor = "apple")]
144 if result
145 .as_ref()
146 .is_err_and(|error| error.raw_os_error() == Some(nix::libc::ENOTSUP))
147 {
148 return nix::unistd::fsync(file).map_err(io::Error::from);
149 }
150 result
135151}
136152
137153/// A section that changed while it moved, which stays where it was.
......@@ -227,8 +243,9 @@ impl Storage for Directory {
227243 }
228244 let mut file = fs::File::create_new(&path)?;
229245 file.write_all(bytes)?;
230 file.sync_all()?;
231 synced_folder(&path)
246 synced(&file)?;
247 synced_folder(&path);
248 Ok(())
232249 }
233250
234251 fn create_directory(&self, path: &str) -> Result<()> {
......@@ -286,8 +303,21 @@ impl Storage for Directory {
286303 {
287304 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());
288305 }
289 fs::rename(self.path(from), &to)?;
290 synced_folder(&to)
306 // A file takes a name nothing holds even should one appear meanwhile, where the volume
307 // can rename so; a mounted share that can't (macOS's smbfs refuses with ENOTSUP, and
308 // links none) takes a plain rename after the check above.
309 let from = self.path(from);
310 let renamed = match fs::metadata(&from)?.is_file() {
311 true => fs::rename_new(&from, &to),
312 false => fs::rename(&from, &to),
313 };
314 match renamed {
315 Err(error) if error.kind() == io::ErrorKind::AlreadyExists => return Err(error.into()),
316 Err(_) => fs::rename(&from, &to)?,
317 Ok(()) => {}
318 }
319 synced_folder(&to);
320 Ok(())
291321 }
292322
293323 /// A local file system shows no other writer's hold; one that refuses to rename a folder
......@@ -1192,9 +1222,15 @@ impl Notebook {
11921222 target = format!("{stem} ({attempt}).{extension}");
11931223 }
11941224 let binned = catalog_path(RECYCLE_BIN, &target);
1195 match base {
1196 Some(base) => self.storage.rename_unchanged(path, &binned, base)?,
1197 None => self.storage.rename(path, &binned)?,
1225 let renamed = match base {
1226 Some(base) => self.storage.rename_unchanged(path, &binned, base),
1227 None => self.storage.rename(path, &binned),
1228 };
1229 // A rename that landed before what followed it failed, a close or a folder's sync.
1230 if let Err(error) = renamed
1231 && (self.storage.exists(path) || !self.storage.exists(&binned))
1232 {
1233 return Err(error);
11981234 }
11991235 Ok((bin_toc, bin_identity, target))
12001236 }
......@@ -1539,12 +1575,11 @@ impl Notebook {
15391575 ) -> Result<Moved> {
15401576 let replica = Arc::clone(section.replica());
15411577 section.close()?;
1578 let (from, name) = split(path);
1579 let (entry, file) = (self.entry(from, name)?, self.replica_path(path));
15421580 let (image, stamp) = self.copied(path, &replica)?;
15431581 let newest = replica.pending()?.last().map(|edit| edit.id);
1544 let copy = target.place_copy(image, split(path).1, folder, after)?;
1545 let (from, name) = split(path);
1546 let entry = self.entry(from, name)?;
1547 let file = self.replica_path(path);
1582 let copy = target.place_copy(image, name, folder, after)?;
15481583 let left = (|| {
15491584 if replica.pending()?.last().map(|edit| edit.id) != newest {
15501585 return Err(changed());
......@@ -1570,7 +1605,10 @@ impl Notebook {
15701605 });
15711606 let _ = self.refresh();
15721607 drop(replica);
1573 if let Ok(file) = file {
1608 // Unless something else opened it meanwhile.
1609 if let Ok(file) = file
1610 && crate::closed(&file).is_ok()
1611 {
15741612 let _ = remove_replica(&file);
15751613 }
15761614 Ok(Moved {
......@@ -1641,10 +1679,12 @@ impl Notebook {
16411679 let (_, ancestor) = self.toc(folder)?;
16421680 onestore::place_image(&mut image, Some((ancestor, &name(1))))?;
16431681 let identity = onestore::Store::parse(&image)?.header.file_id;
1644 let written = catalog_path(folder, &format!(".{stem}.one.snowbound"));
1645 if self.storage.exists(&written) {
1646 self.storage.delete(&written)?;
1647 }
1682 // A name of its own, should another client copy a section so named here meanwhile.
1683 let mut unique = [0; 8];
1684 getrandom::fill(&mut unique)
1685 .map_err(|_| io::Error::other("System random source failed"))?;
1686 let unique: String = unique.iter().map(|byte| format!("{byte:02x}")).collect();
1687 let written = catalog_path(folder, &format!(".{stem}.one.{unique}.snowbound"));
16481688 self.storage.create(&written, &image)?;
16491689 let named = (|| {
16501690 if self.storage.read(&written)? != image {
crates/notebook/src/session/tests.rs+33-1
......@@ -196,6 +196,8 @@ struct FaultStorage {
196196 receipt: Option<Arc<Mutex<Receipt>>>,
197197 /// Placing a section file in its folder fails.
198198 misplace: bool,
199 /// A checked rename lands, then reports a failure, as a close failing after it does.
200 torn: bool,
199201}
200202
201203#[derive(Default)]
......@@ -300,7 +302,11 @@ impl Storage for FaultStorage {
300302 }
301303 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
302304 self.race(from);
303 self.inner.rename_unchanged(from, to, base)
305 self.inner.rename_unchanged(from, to, base)?;
306 match self.torn {
307 true => Err(io::Error::from(io::ErrorKind::BrokenPipe).into()),
308 false => Ok(()),
309 }
304310 }
305311}
306312
......@@ -354,6 +360,7 @@ fn a_section_changed_while_it_moves_stays_where_it_was() {
354360 revision: Mutex::new(revision),
355361 receipt: None,
356362 misplace: false,
363 torn: false,
357364 },
358365 true,
359366 );
......@@ -379,6 +386,7 @@ fn a_move_whose_bin_listing_fails_has_still_moved() {
379386 revision: Mutex::new(None),
380387 receipt: None,
381388 misplace: true,
389 torn: false,
382390 },
383391 false,
384392 );
......@@ -428,6 +436,7 @@ fn a_password_never_overwrites_a_concurrent_revision() {
428436 revision: Mutex::new(Some((path.clone(), revision))),
429437 receipt: None,
430438 misplace: false,
439 torn: false,
431440 };
432441 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
433442 let set = notebook.set_password(&path, None, Some("secret"));
......@@ -467,6 +476,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {
467476 revision: Mutex::new(None),
468477 receipt: Some(receipt.clone()),
469478 misplace: false,
479 torn: false,
470480 };
471481 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
472482 notebook
......@@ -539,6 +549,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {
539549 revision: Mutex::new(None),
540550 receipt: Some(receipt.clone()),
541551 misplace: false,
552 torn: false,
542553 };
543554 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
544555 assert!(
......@@ -975,3 +986,24 @@ fn a_cache_owned_while_closed_opens_for_nobody_else() {
975986 drop(lock);
976987 drop(Replica::open(&cache).unwrap());
977988}
989
990/// A section whose rename into the bin landed, though what followed it failed, has moved.
991#[test]
992fn a_move_whose_rename_lands_and_then_fails_has_still_moved() {
993 let temporary = tempfile::tempdir().unwrap();
994 let (mut source, mut target, path) = moving(
995 temporary.path(),
996 |inner, _| FaultStorage {
997 inner,
998 revision: Mutex::new(None),
999 receipt: None,
1000 misplace: false,
1001 torn: true,
1002 },
1003 false,
1004 );
1005 let section = source.section(&path, false, || {}).unwrap();
1006 let moved = (source.move_section(&path, section, &mut target, "", None)).unwrap();
1007 assert!(moved.kept.is_none(), "{:?}", moved.kept);
1008 assert!(source.storage.exists(&catalog_path(RECYCLE_BIN, &path)));
1009}