Use kernel accelerated sendfile to copy files on Linux

This commit is contained in:
Ilya Fedin 2021-03-11 13:11:46 +04:00 committed by John Preston
parent 5d0222b1c1
commit 34534a9653
2 changed files with 33 additions and 10 deletions

View File

@ -97,11 +97,6 @@ bool copyFile(const char *from, const char *to) {
fclose(ffrom);
return false;
}
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
fwrite(buf, 1, size, fto);
}
struct stat fst; // from http://stackoverflow.com/questions/5486774/keeping-fileowner-and-permissions-after-copying-file-in-c
//let's say this wont fail since you already worked OK on that fp
@ -110,6 +105,21 @@ bool copyFile(const char *from, const char *to) {
fclose(fto);
return false;
}
ssize_t copied = sendfile(
fileno(ffrom),
fileno(fto),
nullptr,
fst.st_size);
if (copied == -1) {
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
fwrite(buf, 1, size, fto);
}
}
//update to the same uid/gid
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
fclose(ffrom);

View File

@ -1022,11 +1022,6 @@ bool linuxMoveFile(const char *from, const char *to) {
fclose(ffrom);
return false;
}
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
fwrite(buf, 1, size, fto);
}
struct stat fst; // from http://stackoverflow.com/questions/5486774/keeping-fileowner-and-permissions-after-copying-file-in-c
//let's say this wont fail since you already worked OK on that fp
@ -1035,6 +1030,24 @@ bool linuxMoveFile(const char *from, const char *to) {
fclose(fto);
return false;
}
ssize_t copied = -1;
#ifdef Q_OS_LINUX
copied = sendfile(
fileno(ffrom),
fileno(fto),
nullptr,
fst.st_size);
#endif // Q_OS_LINUX
if (copied == -1) {
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
fwrite(buf, 1, size, fto);
}
}
//update to the same uid/gid
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
fclose(ffrom);