Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions src/uu/ln/src/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,16 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings)
}

fn relative_path<'a>(src: &'a Path, dst: &Path) -> Cow<'a, Path> {
if let Ok(src_abs) = canonicalize(src, MissingHandling::Missing, ResolveMode::Physical) {
if let Ok(dst_abs) = canonicalize(
dst.parent().unwrap(),
MissingHandling::Missing,
ResolveMode::Physical,
) {
// `dst.parent()` is None for a destination with no parent (`/`, `""`, or a
// bare Windows prefix). Fall through to the non-relative `src` rather than
// unwrapping it; the caller then reports the usual error.
if let (Ok(src_abs), Some(dst_parent)) = (
canonicalize(src, MissingHandling::Missing, ResolveMode::Physical),
dst.parent(),
) {
if let Ok(dst_abs) =
canonicalize(dst_parent, MissingHandling::Missing, ResolveMode::Physical)
{
return make_path_relative_to(src_abs, dst_abs).into();
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/by-util/test_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ fn test_relative_requires_symbolic() {
new_ucmd!().args(&["-r", "foo", "bar"]).fails();
}

#[test]
fn test_relative_target_with_no_parent() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("src", "data");
ucmd.args(&["-rsfT", "src", ""]).fails_with_code(1);
}

#[test]
fn test_relative_dst_already_symlink() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading