Use kernel accelerated sendfile to copy files on Linux
This commit is contained in:
parent
5d0222b1c1
commit
34534a9653
|
@ -97,11 +97,6 @@ bool copyFile(const char *from, const char *to) {
|
||||||
fclose(ffrom);
|
fclose(ffrom);
|
||||||
return false;
|
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
|
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
|
//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);
|
fclose(fto);
|
||||||
return false;
|
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
|
//update to the same uid/gid
|
||||||
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
|
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
|
||||||
fclose(ffrom);
|
fclose(ffrom);
|
||||||
|
|
|
@ -1022,11 +1022,6 @@ bool linuxMoveFile(const char *from, const char *to) {
|
||||||
fclose(ffrom);
|
fclose(ffrom);
|
||||||
return false;
|
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
|
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
|
//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);
|
fclose(fto);
|
||||||
return false;
|
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
|
//update to the same uid/gid
|
||||||
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
|
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
|
||||||
fclose(ffrom);
|
fclose(ffrom);
|
||||||
|
|
Loading…
Reference in New Issue