feat: add -f/--sendfile for zero-copy file transfer

- Add file_send_sendfile() using sendfile() syscall to send file
  content directly from fd to socket, bypassing userspace memory
- Add use_sendfile field to Config struct (default false)
- Add -f / --sendfile flag parsing in client.c
- sendfile path in send_chunk() used when -f is set without -c or -s
- send_files_multithreaded() falls back to single-threaded when
  sendfile is enabled (loader pipeline becomes unnecessary)
- Add Sendfile (-f) test case to test.py
This commit is contained in:
2026-07-05 16:25:20 +02:00
parent e7c23c135b
commit 01045d7d14
7 changed files with 53 additions and 2 deletions
+1
View File
@@ -20,6 +20,7 @@ Config *config_create(char *version, char *send_directory,
config->use_compression = use_compression;
config->compression_level = compression_level;
config->num_connections = num_connections;
config->use_sendfile = false;
return config;
}
+1
View File
@@ -11,6 +11,7 @@ typedef struct Config {
bool use_multithreading;
bool use_chunk_serialization;
bool use_compression;
bool use_sendfile;
bool use_single_send_per_file;
int compression_level;
int num_connections;
+28
View File
@@ -1,9 +1,12 @@
#include <dirent.h>
#include <fcntl.h>
#include <libgen.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <zstd.h>
#include "data.h"
@@ -68,6 +71,31 @@ void file_send_single_calls(File *file, int file_descriptor) {
send_data(file_descriptor, file->data->data, file->data->size);
}
void file_send_sendfile(File *file, int file_descriptor) {
send_str(file_descriptor, file->path);
int fd = open(file->path, O_RDONLY);
if (fd == -1) {
perror("Could not open file for sendfile");
exit(EXIT_FAILURE);
}
unsigned long long file_size = file->stats.st_size;
send_n_data(file_descriptor, &file_size, sizeof(unsigned long long));
off_t offset = 0;
while (offset < file_size) {
ssize_t sent = sendfile(file_descriptor, fd, &offset, file_size - offset);
if (sent == -1) {
perror("sendfile failed");
close(fd);
exit(EXIT_FAILURE);
}
}
close(fd);
}
size_t file_content_to_buffer(File *file) {
FILE *file_pointer = fopen(file->path, "rb");
if (file_pointer == NULL) {
+1
View File
@@ -20,6 +20,7 @@ void file_destroy(void *item);
void file_load_data(File *file);
void file_print(void *item);
void file_send_single_calls(File *file, int file_descriptor);
void file_send_sendfile(File *file, int file_descriptor);
size_t file_content_to_buffer(File *file);
FileReceive *file_receive_create(char *path, Data *data);