Export: a dangling link to the primary file counts as a collision

When an export's linked image path was a link to the primary file that did
not exist yet, the destination check compared the link as written. The
primary was written first, the link then led to it, and the image replaced
it. The check now compares such a link by the file it leads to.
This commit is contained in:
dotCooCoo committed 2026-12-01 07:31:36 -08:00
1 parent 56dd6d07e5
commit c0411d4e4b
2 files changed
+50 -5

No files matched your search

+40 -4
View File
@@ -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<Item = &'a str>) -> 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<Item = &'a str
Ok(())
}
/// The file a write to `path` creates when `path` is a link (or a chain of at most 40 links) that
/// leads to a file that does not exist yet.
#[cfg(not(target_arch = "wasm32"))]
fn dangling_target(path: &str) -> Option<std::path::PathBuf> {
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) {
+10 -1
View File
@@ -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);
}