public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] copy.c: use `sendfile()` for in-kernel file copying on Linux
@ 2026-02-13 12:46 George Hu
  2026-02-13 15:36 ` Chris Torek
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: George Hu @ 2026-02-13 12:46 UTC (permalink / raw)
  To: git; +Cc: George Hu, Junio C Hamano, Johannes Schindelin

The `sendfile()` system call copies data between one file descriptor
and another within the kernel, which is more efficient than the
combination of `read()` and `write()`.

Signed-off-by: George Hu <integral@archlinux.org>
---
 copy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/copy.c b/copy.c
index b668209b6c..d4b7cde764 100644
--- a/copy.c
+++ b/copy.c
@@ -7,8 +7,23 @@
 #include "strbuf.h"
 #include "abspath.h"
 
+#ifdef __linux__
+# include <sys/sendfile.h>
+#endif
+
 int copy_fd(int ifd, int ofd)
 {
+#ifdef __linux__
+	struct stat ifd_st;
+	size_t ifd_len;
+	ssize_t ret = 0;
+
+	fstat(ifd, &ifd_st);
+	ifd_len = ifd_st.st_size;
+
+	while (ifd_len && (ret = sendfile(ofd, ifd, NULL, ifd_len)) > 0)
+		ifd_len -= (size_t)ret;
+#else
 	while (1) {
 		char buffer[8192];
 		ssize_t len = xread(ifd, buffer, sizeof(buffer));
@@ -19,6 +34,8 @@ int copy_fd(int ifd, int ofd)
 		if (write_in_full(ofd, buffer, len) < 0)
 			return COPY_WRITE_ERROR;
 	}
+#endif
+
 	return 0;
 }
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-02-20 16:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-13 12:46 [PATCH] copy.c: use `sendfile()` for in-kernel file copying on Linux George Hu
2026-02-13 15:36 ` Chris Torek
2026-02-14  9:21   ` George Hu
2026-02-14 16:50     ` Chris Torek
2026-02-20 16:35       ` Ed Maste
2026-02-20 16:48         ` Collin Funk
2026-02-14 16:43 ` Phillip Wood
2026-02-15  6:23   ` George Hu
2026-02-15  7:43 ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox