mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-16 14:17:12 +02:00
Use kernel accelerated sendfile to copy files on Linux
This commit is contained in:
parent
5d0222b1c1
commit
34534a9653
2 changed files with 33 additions and 10 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue