mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
ln: add error handling for hard link creation on directories
This commit is contained in:
parent
baf0a737f7
commit
4b02a38be5
4 changed files with 26 additions and 0 deletions
|
|
@ -35,4 +35,5 @@ ln-prompt-replace = replace {$file}?
|
|||
ln-cannot-backup = cannot backup {$file}
|
||||
ln-failed-to-access = failed to access {$file}
|
||||
ln-failed-to-create-hard-link = failed to create hard link {$source} => {$dest}
|
||||
ln-failed-to-create-hard-link-dir = {$source}: hard link not allowed for directory
|
||||
ln-backup = backup: {$backup}
|
||||
|
|
|
|||
|
|
@ -36,4 +36,5 @@ ln-prompt-replace = remplacer {$file} ?
|
|||
ln-cannot-backup = impossible de sauvegarder {$file}
|
||||
ln-failed-to-access = échec d'accès à {$file}
|
||||
ln-failed-to-create-hard-link = échec de création du lien physique {$source} => {$dest}
|
||||
ln-failed-to-create-hard-link-dir = {$source} : lien physique non autorisé pour un répertoire
|
||||
ln-backup = sauvegarde : {$backup}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,9 @@ enum LnError {
|
|||
|
||||
#[error("{}", translate!("ln-error-extra-operand", "operand" => _0.to_string_lossy(), "program" => _1.clone()))]
|
||||
ExtraOperand(OsString, String),
|
||||
|
||||
#[error("{}", translate!("ln-failed-to-create-hard-link-dir", "source" => _0.to_string_lossy()))]
|
||||
FailedToCreateHardLinkDir(PathBuf),
|
||||
}
|
||||
|
||||
impl UError for LnError {
|
||||
|
|
@ -431,6 +434,11 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
|
|||
if settings.symbolic {
|
||||
symlink(&source, dst)?;
|
||||
} else {
|
||||
// Cannot create hard link to a directory
|
||||
if src.is_dir() {
|
||||
return Err(LnError::FailedToCreateHardLinkDir(source.to_path_buf()).into());
|
||||
}
|
||||
|
||||
let p = if settings.logical && source.is_symlink() {
|
||||
// if we want to have an hard link,
|
||||
// source is a symlink and -L is passed
|
||||
|
|
|
|||
|
|
@ -934,3 +934,19 @@ fn test_ln_non_utf8_paths() {
|
|||
let symlink_path = at.plus(symlink_name);
|
||||
assert!(symlink_path.is_symlink());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ln_hard_link_dir() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
|
||||
at.mkdir("dir");
|
||||
|
||||
let result = scene.ucmd().args(&["dir", "dir_link"]).fails();
|
||||
|
||||
assert!(
|
||||
result
|
||||
.stderr_str()
|
||||
.contains("hard link not allowed for directory")
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue