Use file identities for Unix alias checks

This commit is contained in:
gloamxun committed 2026-11-19 18:43:34 +08:00
1 parent 3faac740bd
commit 3ffa452fd1
2 files changed
+43 -24

No files matched your search

+18 -24
View File
@@ -140,30 +140,7 @@ impl HostPreparation {
result
}
pub(crate) fn same_file(&mut self, a: &Path, b: &Path) -> bool {
let same = if a == b {
true
} else if self
.canonicalize(a)
.ok()
.zip(self.canonicalize(b).ok())
.is_some_and(|(a, b)| a == b)
{
true
} else {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
if let (Ok(a), Ok(b)) = (self.metadata(a), self.metadata(b)) {
a.dev() == b.dev() && a.ino() == b.ino()
} else {
false
}
}
#[cfg(not(unix))]
{
false
}
};
let same = a == b || self.same_file_target(a, b);
self.observations.push(HostObservation::Alias {
left: a.into(),
right: b.into(),
@@ -171,6 +148,23 @@ impl HostPreparation {
});
same
}
fn same_file_target(&mut self, a: &Path, b: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
// Metadata follows symlinks. The device and inode also identify
// hard links, so existing Unix paths need no canonicalization.
match (self.metadata(a), self.metadata(b)) {
(Ok(a), Ok(b)) => return a.dev() == b.dev() && a.ino() == b.ino(),
(Err(e), _) | (_, Err(e)) if e.kind() == io::ErrorKind::NotFound => return false,
_ => {}
}
}
self.canonicalize(a)
.ok()
.zip(self.canonicalize(b).ok())
.is_some_and(|(a, b)| a == b)
}
pub(crate) fn output(&mut self, cwd: &Path, path: &Path) -> Result<PathBuf, String> {
let path = cwd.join(path);
if self.metadata(&path).is_ok() {