From 22057652ff9903cdf04676daea0d1e01b00bf0d3 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 17 Jul 2018 13:04:58 +0000 Subject: [PATCH] Re-order the code to short circuit all the opens and closes if linking. Discussed on tech-userlevel and agreed to by christos@. Testing suggests an order of magnitude improvement when linking. --- bin/cp/utils.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 3c9f4e11c2cb..6275fc6bf626 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -1,4 +1,4 @@ -/* $NetBSD: utils.c,v 1.45 2016/02/29 04:22:21 mrg Exp $ */ +/* $NetBSD: utils.c,v 1.46 2018/07/17 13:04:58 darcy Exp $ */ /*- * Copyright (c) 1991, 1993, 1994 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94"; #else -__RCSID("$NetBSD: utils.c,v 1.45 2016/02/29 04:22:21 mrg Exp $"); +__RCSID("$NetBSD: utils.c,v 1.46 2018/07/17 13:04:58 darcy Exp $"); #endif #endif /* not lint */ @@ -99,7 +99,17 @@ copy_file(FTSENT *entp, int dne) int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount; char *p; off_t ptotal = 0; - + + /* if hard linking then simply link and return */ + if (lflag) { + (void)unlink(to.p_path); + if (link(entp->fts_path, to.p_path)) { + warn("%s", to.p_path); + return (1); + } + return (0); + } + if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { warn("%s", entp->fts_path); return (1); @@ -164,18 +174,6 @@ copy_file(FTSENT *entp, int dne) rval = 0; - /* if hard linking then simply close the open fds, link and return */ - if (lflag) { - (void)close(from_fd); - (void)close(to_fd); - (void)unlink(to.p_path); - if (link(entp->fts_path, to.p_path)) { - warn("%s", to.p_path); - return (1); - } - return (0); - } - /* * There's no reason to do anything other than close the file * now if it's empty, so let's not bother.