diff --git a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp index d0d58f69c..14412a10b 100644 --- a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp +++ b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp @@ -12,6 +12,7 @@ #include "Engine/Utilities/StringConverter.h" #include #include +#include #include #include #include @@ -342,6 +343,7 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) char buffer[4096]; ssize_t readSize; int cachedError; + off_t offset = 0; srcFile = open(srcANSI.Get(), O_RDONLY); if (srcFile < 0) @@ -350,24 +352,37 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) if (dstFile < 0) goto out_error; - while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0) + // first try the kernel method + struct stat statBuf; + fstat(srcFile, &statBuf); + readSize = 1; + while (readSize > 0) { - char* ptr = buffer; - ssize_t writeSize; - - do + readSize = sendfile(dstFile, srcFile, 0, statBuf.st_size); + } + // sendfile could fail for example if the input file is not nmap'able + // in this case we fall back to the read/write loop + if (readSize < 0) + { + while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0) { - writeSize = write(dstFile, ptr, readSize); - if (writeSize >= 0) + char* ptr = buffer; + ssize_t writeSize; + + do { - readSize -= writeSize; - ptr += writeSize; - } - else if (errno != EINTR) - { - goto out_error; - } - } while (readSize > 0); + writeSize = write(dstFile, ptr, readSize); + if (writeSize >= 0) + { + readSize -= writeSize; + ptr += writeSize; + } + else if (errno != EINTR) + { + goto out_error; + } + } while (readSize > 0); + } } if (readSize == 0)