| ... | @@ -123,15 +123,31 @@ pub trait Storage: Send + Sync { | ... | @@ -123,15 +123,31 @@ pub trait Storage: Send + Sync { |
| 123 | } | 123 | } |
| 124 | } | 124 | } |
| 125 | | 125 | |
| 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; |
| 127 | fn synced_folder(path: &Path) -> Result<()> { | 127 | /// a mounted share may not. |
| | 128 | fn 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. |
| | 141 | fn 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 | } |
| 136 | | 152 | |
| 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 | } |
| 233 | | 250 | |
| 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 | } |
| 292 | | 322 | |
| 293 | /// A local file system shows no other writer's hold; one that refuses to rename a folder | 323 | /// 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 { |