Author: asomers
Date: Thu Sep 10 02:48:55 2020
New Revision: 365549
URL: https://svnweb.freebsd.org/changeset/base/365549

Log:
  cp: use copy_file_range(2)
  
  This has three advantages over write(2)/read(2):
  
  * Fewer context switches and data copies
  * Mostly preserves a file's sparseness
  * On some file systems (currently NFS 4.2) the file system will perform the
    copy in an especially efficient way.
  
  Reviewed by:  rmacklem
  MFC after:    2 weeks
  Sponsored by: Axcient
  Differential Revision:        https://reviews.freebsd.org/D26377

Modified:
  head/bin/cp/utils.c

Modified: head/bin/cp/utils.c
==============================================================================
--- head/bin/cp/utils.c Thu Sep 10 01:49:53 2020        (r365548)
+++ head/bin/cp/utils.c Thu Sep 10 02:48:55 2020        (r365549)
@@ -212,27 +212,16 @@ copy_file(const FTSENT *entp, int dne)
                                        err(1, "Not enough memory");
                        }
                        wtotal = 0;
-                       while ((rcount = read(from_fd, buf, bufsize)) > 0) {
-                               for (bufp = buf, wresid = rcount; ;
-                                   bufp += wcount, wresid -= wcount) {
-                                       wcount = write(to_fd, bufp, wresid);
-                                       if (wcount <= 0)
-                                               break;
-                                       wtotal += wcount;
-                                       if (info) {
-                                               info = 0;
-                                               (void)fprintf(stderr,
-                                                   "%s -> %s %3d%%\n",
-                                                   entp->fts_path, to.p_path,
-                                                   cp_pct(wtotal, 
fs->st_size));
-                                       }
-                                       if (wcount >= (ssize_t)wresid)
-                                               break;
-                               }
-                               if (wcount != (ssize_t)wresid) {
-                                       warn("%s", to.p_path);
-                                       rval = 1;
-                                       break;
+                       while ((rcount = copy_file_range(from_fd, NULL,
+                           to_fd, NULL, bufsize, 0)) > 0)
+                       {
+                               wtotal += rcount;
+                               if (info) {
+                                       info = 0;
+                                       (void)fprintf(stderr,
+                                           "%s -> %s %3d%%\n",
+                                           entp->fts_path, to.p_path,
+                                           cp_pct(wtotal, fs->st_size));
                                }
                        }
                        if (rcount < 0) {
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to