Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/uu/ln/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ln-error-extra-operand = extra operand {$operand}
Try '{$program} --help' for more information.
ln-error-could-not-update = Could not update {$target}: {$error}
ln-error-will-not-overwrite = will not overwrite just-created {$target} with {$source}
ln-error-cannot-overwrite-directory = {$dest}: cannot overwrite directory
ln-prompt-replace = replace {$file}?
ln-cannot-backup = cannot backup {$file}
ln-failed-to-access = failed to access {$file}
Expand Down
1 change: 1 addition & 0 deletions src/uu/ln/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ln-error-extra-operand = opérande supplémentaire {$operand}
Essayez « {$program} --help » pour plus d'informations.
ln-error-could-not-update = Impossible de mettre à jour {$target} : {$error}
ln-error-will-not-overwrite = ne remplacera pas le fichier {$target} qui vient d'être créé par {$source}
ln-error-cannot-overwrite-directory = {$dest} : impossible d'écraser le répertoire
ln-prompt-replace = remplacer {$file} ?
ln-cannot-backup = impossible de sauvegarder {$file}
ln-failed-to-access = échec d'accès à {$file}
Expand Down
10 changes: 10 additions & 0 deletions src/uu/ln/src/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub enum LnError {

#[error("{}", translate!("ln-failed-to-create-hard-link-dir", "source" => _0.to_string_lossy()))]
FailedToCreateHardLinkDir(PathBuf),

#[error("{}", translate!("ln-error-cannot-overwrite-directory", "dest" => _0.maybe_quote()))]
CannotOverwriteDirectory(PathBuf),
}

impl UError for LnError {
Expand Down Expand Up @@ -405,6 +408,13 @@ pub fn link(src: &Path, dst: &Path, settings: &Settings) -> LnResult<()> {
};

if dst.is_symlink() || dst.exists() {
// Never move a directory aside or replace it to make room for a link,
// matching GNU. A symlink to a directory is a link and is replaced.
if (settings.backup != BackupMode::None || settings.overwrite != OverwriteMode::NoClobber)
&& dst.symlink_metadata().is_ok_and(|m| m.is_dir())
{
return Err(LnError::CannotOverwriteDirectory(dst.to_owned()));
}
backup_path = backup_control::get_backup_path(settings.backup, dst, &settings.suffix);
if settings.backup != BackupMode::None && !settings.symbolic {
// `ln --backup f f`: the backup would move `f` away before the
Expand Down
57 changes: 57 additions & 0 deletions tests/by-util/test_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,63 @@ fn test_backup_simple_numbered_same_file() {
}
}

#[test]
fn test_backup_force_interactive_refuse_directory() {
for args in [
vec!["-s", "-b", "-t", "site", "src/static"],
vec!["-s", "--backup=numbered", "-t", "site", "src/static"],
vec!["-s", "-S", ".bak", "-t", "site", "src/static"],
vec!["-s", "-f", "-b", "-t", "site", "src/static"],
vec!["-s", "-r", "-b", "-t", "site", "src/static"],
vec!["-s", "-f", "-t", "site", "src/static"],
vec!["-s", "-i", "-t", "site", "src/static"],
vec!["-s", "-b", "-T", "src/static", "site/static"],
vec!["-b", "-t", "site", "static"],
] {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir_all("site/static");
at.write("site/static/app.js", "original");
at.mkdir_all("src/static");
at.touch("static");

// ln refuses before reading the answer, so it may exit before the
// input is written.
ucmd.args(&args)
.pipe_in("y\n")
.ignore_stdin_write_error()
.fails_with_code(1)
.stderr_contains("static: cannot overwrite directory");

assert!(at.dir_exists("site/static"));
assert_eq!(at.read("site/static/app.js"), "original");
assert!(!at.dir_exists("site/static~"));
assert!(!at.dir_exists("site/static.~1~"));
assert!(!at.dir_exists("site/static.bak"));
}
}

#[test]
fn test_backup_symlink_to_directory() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir_all("site");
at.mkdir_all("src/static");
at.mkdir("other");
at.relative_symlink_dir("../other", "site/static");

ucmd.args(&["-s", "-b", "-t", "site", "src/static"])
.succeeds()
.no_stderr();

assert_eq!(
at.read_symlink("site/static"),
"src/static".replace('/', MAIN_SEPARATOR_STR)
);
assert_eq!(
at.read_symlink("site/static~"),
"../other".replace('/', MAIN_SEPARATOR_STR)
);
}

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