From f9d679ae37a6c4dd43057c3d821d5513da327edc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 2 Nov 2019 10:00:18 +0100 Subject: [PATCH] cp: Try to pre-size the destination file to the final size up front Since we usually know how many bytes we're going to write, we can be nice to the kernel and ftruncate() the destination to the expected size up front, reducing the amount of FS churn. --- Userland/cp.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Userland/cp.cpp b/Userland/cp.cpp index 1a4a5d554e..d2e12e0153 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -89,6 +89,11 @@ bool copy_file(String src_path, String dst_path, struct stat src_stat, int src_f } } + if (src_stat.st_size > 0) { + // NOTE: This is primarily an optimization, so it's not the end if it fails. + ftruncate(dst_fd, src_stat.st_size); + } + for (;;) { char buffer[BUFSIZ]; ssize_t nread = read(src_fd, buffer, sizeof(buffer)); @@ -151,4 +156,4 @@ bool copy_directory(String src_path, String dst_path) } } return true; -} \ No newline at end of file +}