mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Use sendfile only on Linux.
This commit is contained in:
parent
99e70f7783
commit
7444f17c4e
1 changed files with 15 additions and 9 deletions
|
@ -52,7 +52,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#ifdef Q_OS_LINUX
|
||||||
#include <sys/sendfile.h>
|
#include <sys/sendfile.h>
|
||||||
|
#endif // Q_OS_LINUX
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -1013,6 +1015,14 @@ void psAutoStart(bool start, bool silent) {
|
||||||
void psSendToMenu(bool send, bool silent) {
|
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) {
|
bool linuxMoveFile(const char *from, const char *to) {
|
||||||
FILE *ffrom = fopen(from, "rb"), *fto = fopen(to, "wb");
|
FILE *ffrom = fopen(from, "rb"), *fto = fopen(to, "wb");
|
||||||
if (!ffrom) {
|
if (!ffrom) {
|
||||||
|
@ -1032,26 +1042,19 @@ bool linuxMoveFile(const char *from, const char *to) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t copied = -1;
|
|
||||||
#ifdef Q_OS_LINUX
|
#ifdef Q_OS_LINUX
|
||||||
copied = sendfile(
|
ssize_t copied = sendfile(
|
||||||
fileno(fto),
|
fileno(fto),
|
||||||
fileno(ffrom),
|
fileno(ffrom),
|
||||||
nullptr,
|
nullptr,
|
||||||
fst.st_size);
|
fst.st_size);
|
||||||
#endif // Q_OS_LINUX
|
|
||||||
|
|
||||||
if (copied == -1) {
|
if (copied == -1) {
|
||||||
DEBUG_LOG(("Update Error: "
|
DEBUG_LOG(("Update Error: "
|
||||||
"Copy by sendfile '%1' to '%2' failed, error: %3, fallback now."
|
"Copy by sendfile '%1' to '%2' failed, error: %3, fallback now."
|
||||||
).arg(from
|
).arg(from
|
||||||
).arg(to
|
).arg(to
|
||||||
).arg(errno));
|
).arg(errno));
|
||||||
static const int BufSize = 65536;
|
sendfileFallback(fto, ffrom);
|
||||||
char buf[BufSize];
|
|
||||||
while (size_t size = fread(buf, 1, BufSize, ffrom)) {
|
|
||||||
fwrite(buf, 1, size, fto);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
DEBUG_LOG(("Update Info: "
|
DEBUG_LOG(("Update Info: "
|
||||||
"Copy by sendfile '%1' to '%2' done, size: %3, result: %4."
|
"Copy by sendfile '%1' to '%2' done, size: %3, result: %4."
|
||||||
|
@ -1060,6 +1063,9 @@ bool linuxMoveFile(const char *from, const char *to) {
|
||||||
).arg(fst.st_size
|
).arg(fst.st_size
|
||||||
).arg(copied));
|
).arg(copied));
|
||||||
}
|
}
|
||||||
|
#else // Q_OS_LINUX
|
||||||
|
sendfileFallback(fto, ffrom);
|
||||||
|
#endif // Q_OS_LINUX
|
||||||
|
|
||||||
//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) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue