Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #8034: fix(format): make Windows native-save publication durable

aiongg82df3f7fe674
diff --cc crates/format/src/store.rs
index b8d08e642,40b7c45e7..03a85cbc8
--- a/crates/format/src/store.rs
+++ b/crates/format/src/store.rs
@@@ -885,13 -813,15 +893,18 @@@ impl PcraftWriter
/// Save into a directory bundle: reuses only valid existing objects,
/// rewrites missing or damaged objects, then writes the manifest
- /// atomically and removes unreferenced objects.
+ /// atomically and removes unreferenced objects. The whole save holds the bundle's
+ /// `save.lock`, so saves of one bundle from other writers or processes wait their turn.
pub fn save_dir(&mut self, doc: &Document, dir: &Path, opts: &SaveOptions) -> Result<SaveStats> {
+ self.save_dir_with(doc, dir, opts, &crate::atomic::RealFs)
+ }
+
+ /// The same save path with an injectable publication barrier, for failure/GC ordering tests.
+ fn save_dir_with(&mut self, doc: &Document, dir: &Path, opts: &SaveOptions, fs: &dyn crate::atomic::AtomicFs) -> Result<SaveStats> {
let canonical_dir = bundle_root(dir)?;
let dir = canonical_dir.as_path();
+ // Held until the end of the save: listing, writing, publishing and cleanup (#4957).
+ let _lock = lock_bundle(dir, self.lock_wait)?;
let existing = list_objects(dir)?;
// Reject a hostile existing entry before writing any object or manifest. This also
// covers entries that this save would otherwise leave untouched or garbage-collect.
@@@ -964,14 -894,27 +977,24 @@@
// Pin all GC directories before removing stale previews or objects.
// Unix cleanup then stays within these directories even if a pathname is swapped.
let gc = BundleGc::new(dir)?;
- write_bundle_file(dir, &dir.join(MANIFEST), &p.manifest)?;
- for stale in [THUMB, COMPOSITE] {
- if !p.previews.iter().any(|(n, _)| *n == stale) {
- let path = dir.join(stale);
- validate_bundle_file(dir, &path)?;
- match remove_bundle_file(dir, &path, &gc) {
- Err(FormatError::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {}
- other => other?,
- }
- }
- }
+ #[cfg(windows)]
+ {
+ // A previous attempt may have published an object and then failed its directory
+ // flush. The retry can reuse those valid bytes (including cached ones), so flush
+ // both object directories again before publishing a manifest that references them.
+ // This also persists the fixed subdirectories of a newly created bundle.
+ for sub in ["tiles", "blobs"] {
+ fs.sync_dir(&dir.join(sub)).map_err(|e| crate::atomic::publication_error(e, &dir.join(sub)))?;
+ }
+ }
+ write_bundle_file(dir, &dir.join(MANIFEST), &p.manifest, fs)?;
+ // Omitted previews belong to the previous manifest until the new one commits.
+ // After commit, a locked leftover is only housekeeping, just like unused objects.
+ for stale in [THUMB, COMPOSITE] {
+ if !p.previews.iter().any(|(n, _)| *n == stale) {
+ let _ = remove_bundle_file(dir, &dir.join(stale), &gc);
+ }
+ }
// The manifest is committed: the save has succeeded. Removing objects it no longer
// references is best-effort housekeeping (a vanished or locked file must not turn a
// complete save into a reported failure); a leftover is retried on the next save.