From: Karel Zak Date: Wed, 1 Dec 2021 14:38:50 +0000 (+0100) Subject: hardlink: simplify file_link() X-Git-Tag: v2.38-rc1~112 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd1e57a79e90a88213e0b55c3ad232d5312ee25c;p=thirdparty%2Futil-linux.git hardlink: simplify file_link() Signed-off-by: Karel Zak --- diff --git a/misc-utils/hardlink.c b/misc-utils/hardlink.c index 2aca5ad603..f363ce766f 100644 --- a/misc-utils/hardlink.c +++ b/misc-utils/hardlink.c @@ -634,26 +634,23 @@ static int file_link(struct file *a, struct file *b) free(ssz); if (!opts.dry_run) { - size_t len = - strlen(b->links->path) + strlen(".hardlink-temporary") + 1; - char *new_path = xmalloc(len); + char *new_path; + int failed = 1; - snprintf(new_path, len, "%s.hardlink-temporary", - b->links->path); + xasprintf(&new_path, "%s.hardlink-temporary", b->links->path); + + if (link(a->links->path, new_path) != 0) + warn(_("cannot link %s to %s"), a->links->path, new_path); + + else if (rename(new_path, b->links->path) != 0) { + warn(_("cannot rename %s to %s"), a->links->path, new_path); + unlink(new_path); + } else + failed = 0; - if (link(a->links->path, new_path) != 0) { - warn(_("cannot link %s to %s"), a->links->path, - new_path); - free(new_path); - return FALSE; - } else if (rename(new_path, b->links->path) != 0) { - warn(_("cannot rename %s to %s"), a->links->path, - new_path); - unlink(new_path); /* cleanup failed rename */ - free(new_path); - return FALSE; - } free(new_path); + if (failed) + return FALSE; } /* Update statistics */