git: d114dc0e2260 - stable/15 - pw: Use copy_file_range() when copying skeleton files

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sat, 04 Oct 2025 19:35:15 UTC
The branch stable/15 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=d114dc0e226011272738bcf35b72ce9ae2bfb98d

commit d114dc0e226011272738bcf35b72ce9ae2bfb98d
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-09-18 22:39:39 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-10-04 19:34:57 +0000

    pw: Use copy_file_range() when copying skeleton files
    
    As a bonus, check for I/O errors and print them to standard error.
    
    Reviewed by:    bapt, emaste
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D52588
    
    (cherry picked from commit a03d150a3d44c49344ebd0862defd1a5ad1f524e)
---
 usr.sbin/pw/cpdir.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/usr.sbin/pw/cpdir.c b/usr.sbin/pw/cpdir.c
index 63068cab000c..3839a039495a 100644
--- a/usr.sbin/pw/cpdir.c
+++ b/usr.sbin/pw/cpdir.c
@@ -36,10 +36,10 @@
 #include "pw.h"
 
 void
-copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
+copymkdir(int rootfd, char const *dir, int skelfd, mode_t mode, uid_t uid,
     gid_t gid, int flags)
 {
-	char		*p, lnk[MAXPATHLEN], copybuf[4096];
+	char		*p, lnk[MAXPATHLEN];
 	int		len, homefd, srcfd, destfd;
 	ssize_t		sz;
 	struct stat     st;
@@ -120,8 +120,12 @@ copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
 			continue;
 		}
 
-		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
-			write(destfd, copybuf, sz);
+		do {
+			sz = copy_file_range(srcfd, NULL, destfd, NULL,
+			    SSIZE_MAX, 0);
+		} while (sz > 0);
+		if (sz < 0)
+			warn("copy_file_range");
 
 		close(srcfd);
 		/*