Revert "Use kernel accelerated sendfile to copy files on Linux"

This reverts commit 34534a9653.
This commit is contained in:
Ilya Fedin 2021-12-01 16:54:38 +04:00 committed by John Preston
parent 0b308aebb2
commit 8592326a3c
2 changed files with 10 additions and 65 deletions

View File

@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <cstdio>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sendfile.h>
#include <cstdlib>
#include <unistd.h>
#include <dirent.h>
@ -98,6 +97,11 @@ bool copyFile(const char *from, const char *to, bool writeprotected) {
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
@ -106,33 +110,6 @@ bool copyFile(const char *from, const char *to, bool writeprotected) {
fclose(fto);
return false;
}
ssize_t copied = sendfile(
fileno(fto),
fileno(ffrom),
nullptr,
fst.st_size);
if (copied == -1) {
writeLog(
"Copy by sendfile '%s' to '%s' failed, error: %d, fallback now.",
from,
to,
int(errno));
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
fwrite(buf, 1, size, fto);
}
} else {
writeLog(
"Copy by sendfile '%s' to '%s' done, size: %d, result: %d.",
from,
to,
int(fst.st_size),
int(copied));
}
//update to the same uid/gid
if (!writeprotected && fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
fclose(ffrom);

View File

@ -55,9 +55,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <sys/stat.h>
#include <sys/types.h>
#ifdef Q_OS_LINUX
#include <sys/sendfile.h>
#endif // Q_OS_LINUX
#include <cstdlib>
#include <unistd.h>
#include <dirent.h>
@ -878,14 +875,6 @@ void psNewVersion() {
void psSendToMenu(bool send, bool silent) {
}
void sendfileFallback(FILE *out, FILE *in) {
static const int BufSize = 65536;
char buf[BufSize];
while (size_t size = fread(buf, 1, BufSize, in)) {
fwrite(buf, 1, size, out);
}
}
bool linuxMoveFile(const char *from, const char *to) {
FILE *ffrom = fopen(from, "rb"), *fto = fopen(to, "wb");
if (!ffrom) {
@ -896,6 +885,11 @@ 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
@ -904,32 +898,6 @@ bool linuxMoveFile(const char *from, const char *to) {
fclose(fto);
return false;
}
#ifdef Q_OS_LINUX
ssize_t copied = sendfile(
fileno(fto),
fileno(ffrom),
nullptr,
fst.st_size);
if (copied == -1) {
DEBUG_LOG(("Update Error: "
"Copy by sendfile '%1' to '%2' failed, error: %3, fallback now."
).arg(from
).arg(to
).arg(errno));
sendfileFallback(fto, ffrom);
} else {
DEBUG_LOG(("Update Info: "
"Copy by sendfile '%1' to '%2' done, size: %3, result: %4."
).arg(from
).arg(to
).arg(fst.st_size
).arg(copied));
}
#else // Q_OS_LINUX
sendfileFallback(fto, ffrom);
#endif // Q_OS_LINUX
//update to the same uid/gid
if (fchown(fileno(fto), fst.st_uid, fst.st_gid) != 0) {
fclose(ffrom);