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.
This commit is contained in:
Andreas Kling 2019-11-02 10:00:18 +01:00
parent 845094f9e4
commit f9d679ae37
Notes: sideshowbarker 2024-07-19 11:28:50 +09:00

View File

@ -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;
}
}