Skip to content

Commit

Permalink
improve the symlink probing by simplifying it
Browse files Browse the repository at this point in the history
Less IO operations, and less that can go wrong.
  • Loading branch information
Byron committed May 13, 2024
1 parent 78dedeb commit db2acf9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
13 changes: 2 additions & 11 deletions gix-fs/src/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,13 @@ impl Capabilities {
}

fn probe_symlink(root: &Path) -> std::io::Result<bool> {
let src_path = root.join("__link_src_file");
std::fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(&src_path)?;
let link_path = root.join("__file_link");
if crate::symlink::create(&src_path, &link_path).is_err() {
std::fs::remove_file(&src_path)?;
if crate::symlink::create("dangling".as_ref(), &link_path).is_err() {
return Ok(false);
}

let res = std::fs::symlink_metadata(&link_path).map(|m| m.file_type().is_symlink());

let cleanup = crate::symlink::remove(&link_path).or_else(|_| std::fs::remove_file(&link_path));
std::fs::remove_file(&src_path).and(cleanup)?;

crate::symlink::remove(&link_path).or_else(|_| std::fs::remove_file(&link_path))?;
res
}
}
2 changes: 2 additions & 0 deletions gix-fs/src/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{io, io::ErrorKind::AlreadyExists, path::Path};

#[cfg(not(windows))]
/// Create a new symlink at `link` which points to `original`.
///
/// Note that `original` doesn't have to exist.
pub fn create(original: &Path, link: &Path) -> io::Result<()> {
std::os::unix::fs::symlink(original, link)
}
Expand Down
6 changes: 5 additions & 1 deletion gix-fs/tests/capabilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
fn probe() {
let dir = tempfile::tempdir().unwrap();
std::fs::File::create(dir.path().join("config")).unwrap();
gix_fs::Capabilities::probe(dir.path());
let caps = gix_fs::Capabilities::probe(dir.path());

let entries: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
Expand All @@ -15,4 +15,8 @@ fn probe() {
0,
"there should be no left-over files after probing, found {entries:?}"
);
if cfg!(unix) {
assert!(caps.symlink, "Unix should always be able to create symlinks");
assert!(caps.executable_bit, "Unix should always honor executable bits");
}
}

0 comments on commit db2acf9

Please sign in to comment.