Skip to content
Merged
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
51 changes: 24 additions & 27 deletions src/uu/ln/src/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings)
let mut all_successful = true;
for srcpath in files {
let targetpath = if settings.no_dereference && target_dir.is_symlink() {
let remove_target = || -> UResult<()> {
let remove_target = || {
// In that case, we don't want to do link resolution
// We need to clean the target
if target_dir.is_file() {
Expand All @@ -318,7 +318,6 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings)
);
}
}
Ok(())
};
match settings.overwrite {
OverwriteMode::NoClobber => {}
Expand All @@ -327,11 +326,11 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings)
"{}",
translate!("ln-prompt-replace", "file" => target_dir.quote())
) {
remove_target()?;
remove_target();
}
}
OverwriteMode::Force => {
remove_target()?;
remove_target();
}
}
target_dir.to_path_buf()
Expand Down Expand Up @@ -415,7 +414,7 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
return Err(LnError::SomeLinksFailed.into());
}

if fs::remove_file(dst).is_ok() {}
let _ = fs::remove_file(dst);
// In case of error, don't do anything
}
OverwriteMode::Force => {
Expand All @@ -432,40 +431,38 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
return Err(LnError::SameFile(src.to_owned(), dst.to_owned()).into());
}
}
if fs::remove_file(dst).is_ok() {}
let _ = fs::remove_file(dst);
// In case of error, don't do anything
}
}
}

let res = (|| -> UResult<()> {
if settings.symbolic {
Ok(symlink(&source, dst)?)
let res = if settings.symbolic {
symlink(&source, dst).map_err(Into::into)
} else {
let p = if settings.logical && source.is_symlink() {
fs::canonicalize(&source)
.map_err_context(|| translate!("ln-failed-to-access", "file" => source.quote()))?
} else {
let p = if settings.logical && source.is_symlink() {
fs::canonicalize(&source).map_err_context(
|| translate!("ln-failed-to-access", "file" => source.quote()),
)?
} else {
source.to_path_buf()
};
if let Err(e) = fs::hard_link(&p, dst) {
if p.is_dir() {
return Err(LnError::FailedToCreateHardLinkDir(source.to_path_buf()).into());
}
return Err(e).map_err_context(|| {
translate!("ln-failed-to-create-hard-link", "source" => source.quote(), "dest" => dst.quote())
});
source.to_path_buf()
};
match fs::hard_link(&p, dst) {
Ok(()) => Ok(()),
Err(_) if p.is_dir() => {
Err(LnError::FailedToCreateHardLinkDir(source.to_path_buf()).into())
}
Ok(())
Err(e) => Err(e).map_err_context(|| {
translate!("ln-failed-to-create-hard-link", "source" => source.quote(), "dest" => dst.quote())
}),
}
})();
if res.is_err() {
};

if let Err(e) = res {
if let Some(ref p) = backup_path {
fs::rename(p, dst)
.map_err_context(|| translate!("ln-cannot-backup", "file" => dst.quote()))?;
}
res?;
return Err(e);
}

if settings.verbose {
Expand Down
Loading