optimize file copy to sendfile on Linux

This commit is contained in:
nothingTVatYT
2021-10-18 04:11:28 +02:00
parent 7660ef59ff
commit 94c0dad4bc

View File

@@ -12,6 +12,7 @@
#include "Engine/Utilities/StringConverter.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <stdio.h>
#include <cerrno>
@@ -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)