Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/backend/aqua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,13 @@ impl AquaBackend {
file::create_dir_all(parent)?;
}

// On case-insensitive filesystems src and dst can be different
// strings but the same on-disk file; without this guard the branches
// below would overwrite src with a self-referential link.
if link.dst.exists() && same_disk_entry(&link.src, &link.dst) {
return Ok(());
}

if link.hard || (cfg!(windows) && link.explicit_link) {
trace!("ln {} {}", link.src.display(), link.dst.display());
if link.dst.is_dir() {
Expand Down Expand Up @@ -2493,6 +2500,24 @@ impl AquaBackend {
}
}

fn same_disk_entry(a: &Path, b: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
match (fs::metadata(a), fs::metadata(b)) {
(Ok(am), Ok(bm)) => am.dev() == bm.dev() && am.ino() == bm.ino(),
_ => false,
}
}
#[cfg(not(unix))]
{
match (fs::canonicalize(a), fs::canonicalize(b)) {
(Ok(ac), Ok(bc)) => ac == bc,
_ => false,
}
}
}

fn relative_path(from: &Path, to: &Path) -> Option<PathBuf> {
let from_components = from.components().collect_vec();
let to_components = to.components().collect_vec();
Expand Down Expand Up @@ -3047,6 +3072,30 @@ mod tests {
Ok(())
}

#[cfg(unix)]
#[test]
fn test_create_file_link_skips_when_dst_aliases_src_inode() -> Result<()> {
let tmp = tempfile::tempdir()?;
let src = tmp.path().join("Godot");
fs::write(&src, b"binary contents")?;
// hard_link gives portable same-inode src/dst without needing a
// case-insensitive filesystem
let dst = tmp.path().join("Godot-alias");
fs::hard_link(&src, &dst)?;

AquaBackend::create_file_link(&AquaFileLink {
src: src.clone(),
dst: dst.clone(),
hard: false,
explicit_link: false,
})?;

assert!(dst.exists(), "dst must still exist after the early return");
assert!(!dst.is_symlink(), "dst must not be replaced with a symlink");
assert_eq!(fs::read(&src)?, b"binary contents");
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Ok(())
}

#[test]
fn test_unescape_regex_literal_no_backslash_is_borrowed() {
let result = unescape_regex_literal("astral-sh/ruff/.github/workflows/release.yml");
Expand Down
Loading