mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 20:28:56 +00:00
moved file copy to kernel space to speed up transfer
This commit is contained in:
parent
907f928c2f
commit
2986a26c43
1 changed files with 32 additions and 3 deletions
35
src/util.c
35
src/util.c
|
@ -4,6 +4,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
@ -134,14 +136,41 @@ int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread) {
|
||||||
return pthread_create(thread, NULL, &fileCopyThread, files);
|
return pthread_create(thread, NULL, &fileCopyThread, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define FILE_COPY_BUFFER_SIZE (1024)
|
||||||
|
|
||||||
|
size_t fileCopyThreadFallback(struct fileCopy* files) {
|
||||||
|
|
||||||
|
char c[FILE_COPY_BUFFER_SIZE];
|
||||||
|
|
||||||
|
size_t total = 0;
|
||||||
|
int tmp;
|
||||||
|
while((tmp = read(files->readFd, c, FILE_COPY_BUFFER_SIZE)) > 0) {
|
||||||
|
write(files->writeFd, c, tmp);
|
||||||
|
total += tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
void* fileCopyThread(void* data) {
|
void* fileCopyThread(void* data) {
|
||||||
struct fileCopy* files = (struct fileCopy*) data;
|
struct fileCopy* files = (struct fileCopy*) data;
|
||||||
char c;
|
|
||||||
|
|
||||||
while(read(files->readFd, &c, 1) > 0) {
|
errno = EACCES;
|
||||||
write(files->writeFd, &c, 1);
|
|
||||||
|
size_t total = 0;
|
||||||
|
int tmp;
|
||||||
|
while((tmp = splice(files->readFd, NULL, files->writeFd, NULL, FILE_COPY_BUFFER_SIZE, 0)) > 0) {
|
||||||
|
total += tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (errno != EACCES) {
|
||||||
|
debug("util: splice: %s", strerror(errno));
|
||||||
|
debug("util: falling back to userland copy");
|
||||||
|
total = fileCopyThreadFallback(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug("util: filecopy: %d bytes copied", total);
|
||||||
|
|
||||||
if (files->closeWriteFd)
|
if (files->closeWriteFd)
|
||||||
close(files->writeFd);
|
close(files->writeFd);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue