Atomically swap a directory: `rename(live, backup); rename(staged, live)`, fsyncing the parent directory once both renames have completed. `live`, `backup`, and `staged` MUST share the same parent directory. The caller is responsible for removing the backup directory once the new state is proven good — this helper does not delete anything.
(live: &Path, backup: &Path, staged: &Path)
| 74 | /// caller is responsible for removing the backup directory once the new |
| 75 | /// state is proven good — this helper does not delete anything. |
| 76 | pub fn atomic_swap_dirs_fsync(live: &Path, backup: &Path, staged: &Path) -> Result<()> { |
| 77 | let parent = live.parent().ok_or_else(|| { |
| 78 | WalError::Io(std::io::Error::new( |
| 79 | std::io::ErrorKind::InvalidInput, |
| 80 | "atomic_swap_dirs_fsync: live has no parent directory", |
| 81 | )) |
| 82 | })?; |
| 83 | |
| 84 | fs::rename(live, backup).map_err(WalError::Io)?; |
| 85 | fs::rename(staged, live).map_err(WalError::Io)?; |
| 86 | fsync_directory(parent)?; |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | /// Read a checkpoint file and advise the kernel to drop its pages from the |
| 91 | /// page cache. |