Module Name:    src
Committed By:   darcy
Date:           Tue Jul 17 13:04:58 UTC 2018

Modified Files:
        src/bin/cp: utils.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/bin/cp/utils.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/cp/utils.c
diff -u src/bin/cp/utils.c:1.45 src/bin/cp/utils.c:1.46
--- src/bin/cp/utils.c:1.45	Mon Feb 29 04:22:21 2016
+++ src/bin/cp/utils.c	Tue Jul 17 13:04:58 2018
@@ -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.

Reply via email to