| ... | ... | @@ -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. |
| 127 | | fn 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. |
| 128 | fn synced_folder(path: &Path) { |
| 128 | 129 | #[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); |
| 131 | 134 | } |
| 132 | 135 | #[cfg(not(unix))] |
| 133 | 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 | 153 | /// A section that changed while it moved, which stays where it was. |
| ... | ... | @@ -227,8 +243,9 @@ impl Storage for Directory { |
| 227 | 243 | } |
| 228 | 244 | let mut file = fs::File::create_new(&path)?; |
| 229 | 245 | file.write_all(bytes)?; |
| 230 | | file.sync_all()?; |
| 231 | | synced_folder(&path) |
| 246 | synced(&file)?; |
| 247 | synced_folder(&path); |
| 248 | Ok(()) |
| 232 | 249 | } |
| 233 | 250 | |
| 234 | 251 | fn create_directory(&self, path: &str) -> Result<()> { |
| ... | ... | @@ -286,8 +303,21 @@ impl Storage for Directory { |
| 286 | 303 | { |
| 287 | 304 | return Err(io::Error::from(io::ErrorKind::AlreadyExists).into()); |
| 288 | 305 | } |
| 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(()) |
| 291 | 321 | } |
| 292 | 322 | |
| 293 | 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 | 1222 | target = format!("{stem} ({attempt}).{extension}"); |
| 1193 | 1223 | } |
| 1194 | 1224 | 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); |
| 1198 | 1234 | } |
| 1199 | 1235 | Ok((bin_toc, bin_identity, target)) |
| 1200 | 1236 | } |
| ... | ... | @@ -1539,12 +1575,11 @@ impl Notebook { |
| 1539 | 1575 | ) -> Result<Moved> { |
| 1540 | 1576 | let replica = Arc::clone(section.replica()); |
| 1541 | 1577 | section.close()?; |
| 1578 | let (from, name) = split(path); |
| 1579 | let (entry, file) = (self.entry(from, name)?, self.replica_path(path)); |
| 1542 | 1580 | let (image, stamp) = self.copied(path, &replica)?; |
| 1543 | 1581 | 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)?; |
| 1548 | 1583 | let left = (|| { |
| 1549 | 1584 | if replica.pending()?.last().map(|edit| edit.id) != newest { |
| 1550 | 1585 | return Err(changed()); |
| ... | ... | @@ -1570,7 +1605,10 @@ impl Notebook { |
| 1570 | 1605 | }); |
| 1571 | 1606 | let _ = self.refresh(); |
| 1572 | 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 | 1612 | let _ = remove_replica(&file); |
| 1575 | 1613 | } |
| 1576 | 1614 | Ok(Moved { |
| ... | ... | @@ -1641,10 +1679,12 @@ impl Notebook { |
| 1641 | 1679 | let (_, ancestor) = self.toc(folder)?; |
| 1642 | 1680 | onestore::place_image(&mut image, Some((ancestor, &name(1))))?; |
| 1643 | 1681 | 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")); |
| 1648 | 1688 | self.storage.create(&written, &image)?; |
| 1649 | 1689 | let named = (|| { |
| 1650 | 1690 | if self.storage.read(&written)? != image { |