PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Preset store: a store directory that doesn't exist yet reads as empty on Windows too (#878)
Tanish Manku6f39aba7f2d4
diff --git a/crates/engine/src/preset_store.rs b/crates/engine/src/preset_store.rsindex b5c4b5980..593e2db44 100644--- a/crates/engine/src/preset_store.rs+++ b/crates/engine/src/preset_store.rs@@ -179,7 +179,11 @@ impl PresetBackend for DirBackend {}fn read(&self, name: &str, max: u64) -> Result<Vec<u8>, String> {let path = self.path(name)?;- let f = std::fs::File::open(&path).map_err(|e| format!("{name}: {e}"))?;+ // A missing file, or a store directory that doesn't exist yet (Windows reports that as+ // "path not found", os error 3), reads as "not found", the wording `missing_file` and the+ // in-memory backend share, so it is an empty file rather than a warning.+ let f = std::fs::File::open(&path)+ .map_err(|e| if e.kind() == std::io::ErrorKind::NotFound { format!("{name}: not found") } else { format!("{name}: {e}") })?;let mut out = Vec::new();f.take(max.saturating_add(1)).read_to_end(&mut out).map_err(|e| format!("{name}: {e}"))?;if out.len() as u64 > max {diff --git a/crates/engine/src/preset_store/tests.rs b/crates/engine/src/preset_store/tests.rsindex 1e66a7756..6cf17d1ef 100644--- a/crates/engine/src/preset_store/tests.rs+++ b/crates/engine/src/preset_store/tests.rs@@ -359,3 +359,18 @@ fn bench_load_500_sampled_presets() {let rename = t0.elapsed();eprintln!("500 sampled presets: {:.1} MB on disk; write {write:?}, load {load:?}, rename {rename:?}", bytes as f64 / 1e6);}++#[cfg(not(target_arch = "wasm32"))]+#[test]+fn a_store_directory_that_does_not_exist_yet_opens_without_warnings() {+ // Windows reports a file under a missing directory as "path not found" (os error 3), not+ // "file not found"; both mean an empty store, as on a first launch.+ let parent = TempDir::new("missing-store");+ let dir = parent.0.join("not-created-yet");+ assert!(!dir.exists());+ let opened = open_dir(&dir);+ assert!(opened.warnings.is_empty(), "{:?}", opened.warnings);+ assert!(opened.actions.is_empty());+ let err = DirBackend::new(&dir).read(ACTIONS_FILE, MAX_ACTIONS_BYTES).unwrap_err();+ assert!(missing_file(&err), "{err}");+}