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 {...@@ -123,15 +123,31 @@ pub trait Storage: Send + Sync {
123 }123 }
124}124}
125125
126/// Makes the entry naming `path` in its folder durable, where the system lets a folder sync.126/// Makes the entry naming `path` in its folder durable, where the volume lets a folder sync;
127fn synced_folder(path: &Path) -> Result<()> {127/// a mounted share may not.
128fn synced_folder(path: &Path) {
128 #[cfg(unix)]129 #[cfg(unix)]
129 if let Some(folder) = path.parent() {130 if let Some(folder) = path.parent()
130 fs::File::open(folder)?.sync_all()?;131 && let Ok(folder) = fs::File::open(folder)
132 {
133 let _ = synced(&folder);
131 }134 }
132 #[cfg(not(unix))]135 #[cfg(not(unix))]
133 let _ = path;136 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
135}151}
136152
137/// A section that changed while it moved, which stays where it was.153/// A section that changed while it moved, which stays where it was.
...@@ -227,8 +243,9 @@ impl Storage for Directory {...@@ -227,8 +243,9 @@ impl Storage for Directory {
227 }243 }
228 let mut file = fs::File::create_new(&path)?;244 let mut file = fs::File::create_new(&path)?;
229 file.write_all(bytes)?;245 file.write_all(bytes)?;
230 file.sync_all()?;246 synced(&file)?;
231 synced_folder(&path)247 synced_folder(&path);
248 Ok(())
232 }249 }
233250
234 fn create_directory(&self, path: &str) -> Result<()> {251 fn create_directory(&self, path: &str) -> Result<()> {
...@@ -286,8 +303,21 @@ impl Storage for Directory {...@@ -286,8 +303,21 @@ impl Storage for Directory {
286 {303 {
287 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());304 return Err(io::Error::from(io::ErrorKind::AlreadyExists).into());
288 }305 }
289 fs::rename(self.path(from), &to)?;306 // A file takes a name nothing holds even should one appear meanwhile, where the volume
290 synced_folder(&to)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(())
291 }321 }
292322
293 /// A local file system shows no other writer's hold; one that refuses to rename a folder323 /// A local file system shows no other writer's hold; one that refuses to rename a folder
...@@ -1192,9 +1222,15 @@ impl Notebook {...@@ -1192,9 +1222,15 @@ impl Notebook {
1192 target = format!("{stem} ({attempt}).{extension}");1222 target = format!("{stem} ({attempt}).{extension}");
1193 }1223 }
1194 let binned = catalog_path(RECYCLE_BIN, &target);1224 let binned = catalog_path(RECYCLE_BIN, &target);
1195 match base {1225 let renamed = match base {
1196 Some(base) => self.storage.rename_unchanged(path, &binned, base)?,1226 Some(base) => self.storage.rename_unchanged(path, &binned, base),
1197 None => self.storage.rename(path, &binned)?,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);
1198 }1234 }
1199 Ok((bin_toc, bin_identity, target))1235 Ok((bin_toc, bin_identity, target))
1200 }1236 }
...@@ -1539,12 +1575,11 @@ impl Notebook {...@@ -1539,12 +1575,11 @@ impl Notebook {
1539 ) -> Result<Moved> {1575 ) -> Result<Moved> {
1540 let replica = Arc::clone(section.replica());1576 let replica = Arc::clone(section.replica());
1541 section.close()?;1577 section.close()?;
1578 let (from, name) = split(path);
1579 let (entry, file) = (self.entry(from, name)?, self.replica_path(path));
1542 let (image, stamp) = self.copied(path, &replica)?;1580 let (image, stamp) = self.copied(path, &replica)?;
1543 let newest = replica.pending()?.last().map(|edit| edit.id);1581 let newest = replica.pending()?.last().map(|edit| edit.id);
1544 let copy = target.place_copy(image, split(path).1, folder, after)?;1582 let copy = target.place_copy(image, name, folder, after)?;
1545 let (from, name) = split(path);
1546 let entry = self.entry(from, name)?;
1547 let file = self.replica_path(path);
1548 let left = (|| {1583 let left = (|| {
1549 if replica.pending()?.last().map(|edit| edit.id) != newest {1584 if replica.pending()?.last().map(|edit| edit.id) != newest {
1550 return Err(changed());1585 return Err(changed());
...@@ -1570,7 +1605,10 @@ impl Notebook {...@@ -1570,7 +1605,10 @@ impl Notebook {
1570 });1605 });
1571 let _ = self.refresh();1606 let _ = self.refresh();
1572 drop(replica);1607 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 {
1574 let _ = remove_replica(&file);1612 let _ = remove_replica(&file);
1575 }1613 }
1576 Ok(Moved {1614 Ok(Moved {
...@@ -1641,10 +1679,12 @@ impl Notebook {...@@ -1641,10 +1679,12 @@ impl Notebook {
1641 let (_, ancestor) = self.toc(folder)?;1679 let (_, ancestor) = self.toc(folder)?;
1642 onestore::place_image(&mut image, Some((ancestor, &name(1))))?;1680 onestore::place_image(&mut image, Some((ancestor, &name(1))))?;
1643 let identity = onestore::Store::parse(&image)?.header.file_id;1681 let identity = onestore::Store::parse(&image)?.header.file_id;
1644 let written = catalog_path(folder, &format!(".{stem}.one.snowbound"));1682 // A name of its own, should another client copy a section so named here meanwhile.
1645 if self.storage.exists(&written) {1683 let mut unique = [0; 8];
1646 self.storage.delete(&written)?;1684 getrandom::fill(&mut unique)
1647 }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"));
1648 self.storage.create(&written, &image)?;1688 self.storage.create(&written, &image)?;
1649 let named = (|| {1689 let named = (|| {
1650 if self.storage.read(&written)? != image {1690 if self.storage.read(&written)? != image {
crates/notebook/src/session/tests.rs+33-1
...@@ -196,6 +196,8 @@ struct FaultStorage {...@@ -196,6 +196,8 @@ struct FaultStorage {
196 receipt: Option<Arc<Mutex<Receipt>>>,196 receipt: Option<Arc<Mutex<Receipt>>>,
197 /// Placing a section file in its folder fails.197 /// Placing a section file in its folder fails.
198 misplace: bool,198 misplace: bool,
199 /// A checked rename lands, then reports a failure, as a close failing after it does.
200 torn: bool,
199}201}
200202
201#[derive(Default)]203#[derive(Default)]
...@@ -300,7 +302,11 @@ impl Storage for FaultStorage {...@@ -300,7 +302,11 @@ impl Storage for FaultStorage {
300 }302 }
301 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {303 fn rename_unchanged(&self, from: &str, to: &str, base: &Stamp) -> Result<()> {
302 self.race(from);304 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 }
304 }310 }
305}311}
306312
...@@ -354,6 +360,7 @@ fn a_section_changed_while_it_moves_stays_where_it_was() {...@@ -354,6 +360,7 @@ fn a_section_changed_while_it_moves_stays_where_it_was() {
354 revision: Mutex::new(revision),360 revision: Mutex::new(revision),
355 receipt: None,361 receipt: None,
356 misplace: false,362 misplace: false,
363 torn: false,
357 },364 },
358 true,365 true,
359 );366 );
...@@ -379,6 +386,7 @@ fn a_move_whose_bin_listing_fails_has_still_moved() {...@@ -379,6 +386,7 @@ fn a_move_whose_bin_listing_fails_has_still_moved() {
379 revision: Mutex::new(None),386 revision: Mutex::new(None),
380 receipt: None,387 receipt: None,
381 misplace: true,388 misplace: true,
389 torn: false,
382 },390 },
383 false,391 false,
384 );392 );
...@@ -428,6 +436,7 @@ fn a_password_never_overwrites_a_concurrent_revision() {...@@ -428,6 +436,7 @@ fn a_password_never_overwrites_a_concurrent_revision() {
428 revision: Mutex::new(Some((path.clone(), revision))),436 revision: Mutex::new(Some((path.clone(), revision))),
429 receipt: None,437 receipt: None,
430 misplace: false,438 misplace: false,
439 torn: false,
431 };440 };
432 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();441 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
433 let set = notebook.set_password(&path, None, Some("secret"));442 let set = notebook.set_password(&path, None, Some("secret"));
...@@ -467,6 +476,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {...@@ -467,6 +476,7 @@ fn recycling_confirms_uncertain_toc_and_page_appends_and_undo() {
467 revision: Mutex::new(None),476 revision: Mutex::new(None),
468 receipt: Some(receipt.clone()),477 receipt: Some(receipt.clone()),
469 misplace: false,478 misplace: false,
479 torn: false,
470 };480 };
471 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();481 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
472 notebook482 notebook
...@@ -539,6 +549,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {...@@ -539,6 +549,7 @@ fn recycling_rejects_unconfirmed_appends_and_can_retry_each_stage() {
539 revision: Mutex::new(None),549 revision: Mutex::new(None),
540 receipt: Some(receipt.clone()),550 receipt: Some(receipt.clone()),
541 misplace: false,551 misplace: false,
552 torn: false,
542 };553 };
543 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();554 let mut notebook = Notebook::with(Box::new(storage), Some(root), &cache).unwrap();
544 assert!(555 assert!(
...@@ -975,3 +986,24 @@ fn a_cache_owned_while_closed_opens_for_nobody_else() {...@@ -975,3 +986,24 @@ fn a_cache_owned_while_closed_opens_for_nobody_else() {
975 drop(lock);986 drop(lock);
976 drop(Replica::open(&cache).unwrap());987 drop(Replica::open(&cache).unwrap());
977}988}
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}