Skip to content

Commit 74f9c11

Browse files
committed
ln: simplify backup rollback by replacing closure with if/else
1 parent 0b437e9 commit 74f9c11

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

src/uu/ln/src/ln.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,34 +438,32 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
438438
}
439439
}
440440

441-
let res = (|| -> UResult<()> {
442-
if settings.symbolic {
443-
Ok(symlink(&source, dst)?)
441+
let res = if settings.symbolic {
442+
symlink(&source, dst).map_err(Into::into)
443+
} else {
444+
let p = if settings.logical && source.is_symlink() {
445+
fs::canonicalize(&source)
446+
.map_err_context(|| translate!("ln-failed-to-access", "file" => source.quote()))?
444447
} else {
445-
let p = if settings.logical && source.is_symlink() {
446-
fs::canonicalize(&source).map_err_context(
447-
|| translate!("ln-failed-to-access", "file" => source.quote()),
448-
)?
449-
} else {
450-
source.to_path_buf()
451-
};
452-
if let Err(e) = fs::hard_link(&p, dst) {
453-
if p.is_dir() {
454-
return Err(LnError::FailedToCreateHardLinkDir(source.to_path_buf()).into());
455-
}
456-
return Err(e).map_err_context(|| {
457-
translate!("ln-failed-to-create-hard-link", "source" => source.quote(), "dest" => dst.quote())
458-
});
448+
source.to_path_buf()
449+
};
450+
match fs::hard_link(&p, dst) {
451+
Ok(()) => Ok(()),
452+
Err(_) if p.is_dir() => {
453+
Err(LnError::FailedToCreateHardLinkDir(source.to_path_buf()).into())
459454
}
460-
Ok(())
455+
Err(e) => Err(e).map_err_context(|| {
456+
translate!("ln-failed-to-create-hard-link", "source" => source.quote(), "dest" => dst.quote())
457+
}),
461458
}
462-
})();
463-
if res.is_err() {
459+
};
460+
461+
if let Err(e) = res {
464462
if let Some(ref p) = backup_path {
465463
fs::rename(p, dst)
466464
.map_err_context(|| translate!("ln-cannot-backup", "file" => dst.quote()))?;
467465
}
468-
res?;
466+
return Err(e);
469467
}
470468

471469
if settings.verbose {

0 commit comments

Comments
 (0)