diff --git a/crates/engine/src/cmd/fileio/export_names.rs b/crates/engine/src/cmd/fileio/export_names.rs index c9e34a5ca8..ec749cd737 100644 --- a/crates/engine/src/cmd/fileio/export_names.rs +++ b/crates/engine/src/cmd/fileio/export_names.rs @@ -110,14 +110,18 @@ pub(super) fn write_encoded(path: Option<&str>, name: &str, doc: &vectorcraft_do Ok(merge(out, extra)) } -/// An error when two of `paths` are the same file. A path whose real path cannot be found (a folder -/// that may not be read, a link that leads nowhere) is compared as written, and its write then -/// reports its own error. +/// An error when two of `paths` are the same file. A link that leads nowhere is compared by the file +/// its write creates; another path whose real path cannot be found (a folder that may not be read) +/// is compared as written, and its write then reports its own error. #[cfg(not(target_arch = "wasm32"))] pub(super) fn distinct_write_targets<'a>(paths: impl IntoIterator) -> Result<()> { let mut targets = std::collections::HashSet::new(); for path in paths { - let real = crate::file_access::resolve(path).or_else(|_| std::path::absolute(path)).unwrap_or_else(|_| std::path::PathBuf::from(path)); + let real = crate::file_access::resolve(path) + .ok() + .or_else(|| dangling_target(path)) + .or_else(|| std::path::absolute(path).ok()) + .unwrap_or_else(|| std::path::PathBuf::from(path)); if !targets.insert(real) { return Err(EngineError::Other("output destinations resolve to the same file".into())); } @@ -125,6 +129,38 @@ pub(super) fn distinct_write_targets<'a>(paths: impl IntoIterator Option { + let mut at = std::path::PathBuf::from(path); + let mut followed = false; + for _ in 0..40 { + if !std::fs::symlink_metadata(&at).is_ok_and(|m| m.file_type().is_symlink()) { + break; + } + let next = std::fs::read_link(&at).ok()?; + at = match at.parent() { + Some(parent) if next.is_relative() => parent.join(next), + _ => next, + }; + followed = true; + } + if !followed { + return None; + } + // Resolved as the primary destination is; a name that is not UTF-8 keeps its bytes, with its + // folder's links followed. + match at.to_str() { + Some(text) => crate::file_access::resolve(text).ok(), + None => { + let folder = std::fs::canonicalize(at.parent()?).ok()?; + Some(folder.join(at.file_name()?)) + } + } + .or_else(|| std::path::absolute(&at).ok()) +} + /// `a` with the fields of `b`. pub(crate) fn merge(mut a: Value, b: Value) -> Value { if let (Some(a), Value::Object(b)) = (a.as_object_mut(), b) { diff --git a/crates/engine/src/tests/script_save_open.rs b/crates/engine/src/tests/script_save_open.rs index 5345d58562..1efa9e255f 100644 --- a/crates/engine/src/tests/script_save_open.rs +++ b/crates/engine/src/tests/script_save_open.rs @@ -111,13 +111,22 @@ fn svg_save_and_export_refuse_resolved_destination_collisions() { assert_eq!(images.len(), 1); std::fs::remove_file(&images[0]).unwrap(); std::os::unix::fs::symlink(&svg, &images[0]).unwrap(); - for call in [export, format!("app.activeDocument.saveAs(new File({}));", quoted(&svg))] { + for call in [export.clone(), format!("app.activeDocument.saveAs(new File({}));", quoted(&svg))] { std::fs::write(&svg, "original").unwrap(); let error = failure(&mut s, &base, &format!("Folder.userData; {call}")); assert!(error.contains("resolve to the same file"), "{error}"); assert_eq!(std::fs::read(&svg).unwrap(), b"original"); assert!(std::fs::symlink_metadata(&images[0]).unwrap().file_type().is_symlink()); } + // With no primary file yet, the image's link leads nowhere but still names the primary. + std::fs::remove_file(&svg).unwrap(); + let error = s.execute("file.saveAs", &json!({"path": svg.to_string_lossy(), "format": "svg", "images": "link"})).unwrap_err().to_string(); + assert!(error.contains("resolve to the same file"), "{error}"); + assert!(!svg.exists()); + // A script may not write through a link that leads nowhere at all. + let error = failure(&mut s, &base, &format!("Folder.userData; {export}")); + assert!(error.contains("resolve to the same file") || error.contains("access denied"), "{error}"); + assert!(!svg.exists()); let _ = std::fs::remove_dir_all(base); }