Skip to content

Commit 7a3c583

Browse files
committed
improve the symlink probing by simplifying it
Less IO operations, and less that can go wrong.
1 parent 31d02a8 commit 7a3c583

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

Diff for: gix-fs/src/capabilities.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,13 @@ impl Capabilities {
101101
}
102102

103103
fn probe_symlink(root: &Path) -> std::io::Result<bool> {
104-
let src_path = root.join("__link_src_file");
105-
std::fs::OpenOptions::new()
106-
.create_new(true)
107-
.write(true)
108-
.open(&src_path)?;
109104
let link_path = root.join("__file_link");
110-
if crate::symlink::create(&src_path, &link_path).is_err() {
111-
std::fs::remove_file(&src_path)?;
105+
if crate::symlink::create("dangling".as_ref(), &link_path).is_err() {
112106
return Ok(false);
113107
}
114108

115109
let res = std::fs::symlink_metadata(&link_path).map(|m| m.file_type().is_symlink());
116-
117-
let cleanup = crate::symlink::remove(&link_path).or_else(|_| std::fs::remove_file(&link_path));
118-
std::fs::remove_file(&src_path).and(cleanup)?;
119-
110+
crate::symlink::remove(&link_path).or_else(|_| std::fs::remove_file(&link_path))?;
120111
res
121112
}
122113
}

Diff for: gix-fs/src/symlink.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::{io, io::ErrorKind::AlreadyExists, path::Path};
22

33
#[cfg(not(windows))]
44
/// Create a new symlink at `link` which points to `original`.
5+
///
6+
/// Note that `original` doesn't have to exist.
57
pub fn create(original: &Path, link: &Path) -> io::Result<()> {
68
std::os::unix::fs::symlink(original, link)
79
}

Diff for: gix-fs/tests/capabilities/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
fn probe() {
33
let dir = tempfile::tempdir().unwrap();
44
std::fs::File::create(dir.path().join("config")).unwrap();
5-
gix_fs::Capabilities::probe(dir.path());
5+
let caps = gix_fs::Capabilities::probe(dir.path());
66

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

0 commit comments

Comments
 (0)