From 4336161cc9c631d40d00adee97dfc8161b6bec9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 12 Apr 2024 19:30:55 +0200 Subject: [PATCH] install: Don't skip syncing in the common case. In `copy()`, if no digest was requested (which is the common case), we use `copy_file_range()` to avoid needlessly copying the contents of the file into user space and back. When `copy_file_range()` returns successfully (which, again, is the common case), we simply return, and therefore never get to the point where we call `fsync()` if the `-S` option was specified. Fix this. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D44756 --- usr.bin/xinstall/xinstall.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index 6ab0a88d5cd7..d696a8429c88 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -1232,15 +1232,12 @@ copy(int from_fd, const char *from_name, int to_fd, const char *to_name, #ifndef BOOTSTRAP_XINSTALL /* Try copy_file_range() if no digest is requested */ if (digesttype == DIGEST_NONE) { - ret = 1; - while (ret > 0) { + do { ret = copy_file_range(from_fd, NULL, to_fd, NULL, SSIZE_MAX, 0); - } - if (ret == 0) { - /* DIGEST_NONE always returns NULL */ - return (NULL); - } + } while (ret > 0); + if (ret == 0) + goto done; if (errno != EINVAL) { serrno = errno; (void)unlink(to_name); @@ -1313,6 +1310,7 @@ copy(int from_fd, const char *from_name, int to_fd, const char *to_name, err(EX_OSERR, "%s", from_name); } } +done: if (safecopy && fsync(to_fd) == -1) { serrno = errno; (void)unlink(to_name);