From f76168a3ea395154b0439265e6767d157f8c69aa Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 4 Nov 2019 16:34:25 +0100 Subject: [PATCH] FileManager: Make copying faster with ftruncate() and a bigger buffer Apply the same techniques from "cp" to make copying files much faster. --- Applications/FileManager/FileUtils.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Applications/FileManager/FileUtils.cpp b/Applications/FileManager/FileUtils.cpp index 393f5b11b2..f36ae11fd6 100644 --- a/Applications/FileManager/FileUtils.cpp +++ b/Applications/FileManager/FileUtils.cpp @@ -74,8 +74,15 @@ bool copy_file(const String& src_path, const String& dst_path, const struct stat } } + if (src_stat.st_size > 0) { + if (ftruncate(dst_fd, src_stat.st_size) < 0) { + perror("cp: ftruncate"); + return false; + } + } + for (;;) { - char buffer[BUFSIZ]; + char buffer[32768]; ssize_t nread = read(src_fd, buffer, sizeof(buffer)); if (nread < 0) { return false; @@ -130,4 +137,4 @@ String get_duplicate_name(const String& path, int duplicate_count) } return duplicated_name.build(); } -} \ No newline at end of file +}