Skip to content

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

same_file: compare file identity on Windows, so a hard link is the same file

Khawar Zamman Wani9eef6aa51e3c
diff --git a/Cargo.lock b/Cargo.lock
index 26920a968..132863df3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4812,6 +4812,7 @@ dependencies = [
"photocraft-vector",
"rayon",
"roxmltree",
+ "same-file",
"serde",
"serde_json",
"tempfile",
diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml
index 65dd1e16e..fb3bc3fbe 100644
--- a/crates/engine/Cargo.toml
+++ b/crates/engine/Cargo.toml
@@ -43,6 +43,11 @@ roxmltree = "0.20"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tempfile = "3"
+# `file_cmds::same_file` on Windows: the volume serial number and file index, so a hard link to a
+# file is that file (std's `MetadataExt::file_index` is unstable).
+[target.'cfg(windows)'.dependencies]
+same-file = "1"
+
# Printing on Unix: an IPP client for CUPS (pure Rust); the web build cannot print.
[target.'cfg(all(not(target_arch = "wasm32"), not(target_os = "windows")))'.dependencies]
photocraft-ipp = { workspace = true }
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs
index f99aefcff..3db9a662c 100644
--- a/crates/engine/src/file_cmds.rs
+++ b/crates/engine/src/file_cmds.rs
@@ -234,7 +234,12 @@ pub fn same_file(a: &str, b: &str) -> bool {
}
#[cfg(windows)]
{
- // NTFS and FAT compare names without case.
+ // The volume serial number and file index of each, as unix compares dev and inode, so a
+ // hard link (another name for the file) is the same file.
+ if let Ok(same) = same_file::is_same_file(a, b) {
+ return same;
+ }
+ // Should a file not open: NTFS and FAT compare names without case.
if let (Ok(x), Ok(y)) = (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
return x.to_string_lossy().to_lowercase() == y.to_string_lossy().to_lowercase();
}
diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rs
index 594fe8d49..bd55a549c 100644
--- a/crates/engine/src/web_cmds/tests.rs
+++ b/crates/engine/src/web_cmds/tests.rs
@@ -476,11 +476,8 @@ fn same_file_sees_through_other_names_for_one_file() {
assert!(crate::file_cmds::same_file(&a, &format!("{dir}/./a.png")));
assert!(!crate::file_cmds::same_file(&a, &c));
assert!(!crate::file_cmds::same_file(&a, &b), "a missing file is not the same file");
- #[cfg(unix)]
- {
- std::fs::hard_link(&a, &b).unwrap();
- assert!(crate::file_cmds::same_file(&a, &b), "a hard link is the same file");
- }
+ std::fs::hard_link(&a, &b).unwrap();
+ assert!(crate::file_cmds::same_file(&a, &b), "a hard link is the same file");
#[cfg(windows)]
assert!(crate::file_cmds::same_file(&a, &format!("{dir}/A.PNG")), "NTFS names ignore case");
}