From 01045d7d14d5e92d5d1890e21e4ba43f30d7f448 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 5 Jul 2026 16:25:20 +0200 Subject: [PATCH 1/4] 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 --- README.md | 4 ++++ src/client/client.c | 19 +++++++++++++++++-- src/shared/config.c | 1 + src/shared/config.h | 1 + src/shared/file.c | 28 ++++++++++++++++++++++++++++ src/shared/file.h | 1 + test.py | 1 + 7 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 770d14e..6a83048 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ The client-server communication uses the following status codes: | `-m` | Enable multithreading mode | | `-c [level]` | Enable compression with optional level (1-22, default: 5) | | `-s` | Enable chunk serialization (batch-transfer all files per chunk) | +| `-f` | Enable sendfile (zero-copy file transfer, bypasses userspace memory) | | `--source-dir ` | Source directory to sync (overrides `FASTSYNC_SOURCE_DIR`) | | `--dest-dir ` | Server-side destination directory (overrides `FASTSYNC_DEST_DIR`) | | `--save-to-disk` | Persist received files to disk | @@ -118,6 +119,9 @@ make # Multithreaded with compressed chunk serialization ./build/client -m -s -c 3 + +# Sendfile (zero-copy, bypasses userspace for large files) +./build/client -f ``` ## Testing diff --git a/src/client/client.c b/src/client/client.c index a026458..25bcf41 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -29,6 +29,11 @@ int send_chunk(Client *client, Chunk *chunk, Config *config) { } send_data(client->file_descriptor, data->data, data->size); data_destroy(data); + } else if (config->use_sendfile && !config->use_compression) { + for (int i = 0; i < chunk->element_count; i++) { + send_status(client->file_descriptor, STATUS_NEXT); + file_send_sendfile(chunk->items[i], client->file_descriptor); + } } else { for (int i = 0; i < chunk->element_count; i++) { send_status(client->file_descriptor, STATUS_NEXT); @@ -130,8 +135,10 @@ int send_files(Config *config) { DirectoryScanner *scanner = directory_scanner_create(config->send_directory); Chunk *current_chunk; while ((current_chunk = directory_scanner_next(scanner)) != NULL) { - for (int i = 0; i < current_chunk->element_count; i++) - file_load_data(current_chunk->items[i]); + if (!config->use_sendfile) { + for (int i = 0; i < current_chunk->element_count; i++) + file_load_data(current_chunk->items[i]); + } send_chunk(client, current_chunk, config); chunk_destroy(current_chunk); } @@ -145,6 +152,11 @@ int send_files(Config *config) { } int send_files_multithreaded(Config *config) { + if (config->use_sendfile) { + log_message(LOG_LEVEL_INFO, "Sendfile enabled, falling back to single-threaded"); + return send_files(config); + } + PipelineContextSender *context = pipeline_context_sender_create(config, queue_create(100, chunk_destroy), queue_create(100, chunk_destroy)); @@ -215,6 +227,9 @@ int main(int argc, char *argv[]) { config->receive_root_directory = str_dup(argv[++i]); } else if (strcmp(argv[i], "--save-to-disk") == 0) { config->save_to_disk = true; + } else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--sendfile") == 0) { + config->use_sendfile = true; + log_message(LOG_LEVEL_INFO, "Enabled sendfile"); } else { handle_arg(argv[i], "-m", &config->use_multithreading, "Enabled Multithreading"); diff --git a/src/shared/config.c b/src/shared/config.c index 560fd88..3bb891f 100644 --- a/src/shared/config.c +++ b/src/shared/config.c @@ -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; } diff --git a/src/shared/config.h b/src/shared/config.h index cd8a881..c58c2d3 100644 --- a/src/shared/config.h +++ b/src/shared/config.h @@ -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; diff --git a/src/shared/file.c b/src/shared/file.c index 9343349..5a2f19b 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -1,9 +1,12 @@ #include +#include #include #include #include #include #include +#include +#include #include #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) { diff --git a/src/shared/file.h b/src/shared/file.h index 2bf85ea..169ca66 100644 --- a/src/shared/file.h +++ b/src/shared/file.h @@ -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); diff --git a/test.py b/test.py index d6dd0b5..6534921 100755 --- a/test.py +++ b/test.py @@ -48,6 +48,7 @@ TEST_CASES = [ "name": "Multithreading + Compression + Chunk Serialization (-m -c -s)", "flags": ["-m", "-c", "-s"], }, + {"name": "Sendfile (-f)", "flags": ["-f"]}, ] From 2fb7203c821a6dbb667f539c4b72c6ab461c5537 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 5 Jul 2026 16:31:08 +0200 Subject: [PATCH 2/4] feat: sendfile works with -m, error on -f + -c/-s - load_files_multithreaded skips file_load_data when sendfile is active - -f + -m works: sendfile bypasses loader, sender uses sendfile directly - -f combined with -c or -s prints error and exits with rc=1 - Add Sendfile + Multithreading test case --- README.md | 2 +- src/client/client.c | 16 +++++++++------- test.py | 1 + 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6a83048..95d69c9 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ The client-server communication uses the following status codes: | `-m` | Enable multithreading mode | | `-c [level]` | Enable compression with optional level (1-22, default: 5) | | `-s` | Enable chunk serialization (batch-transfer all files per chunk) | -| `-f` | Enable sendfile (zero-copy file transfer, bypasses userspace memory) | +| `-f` | Enable sendfile (zero-copy file transfer, bypasses userspace memory). Can be combined with `-m`. Incompatible with `-c` and `-s`. | | `--source-dir ` | Source directory to sync (overrides `FASTSYNC_SOURCE_DIR`) | | `--dest-dir ` | Server-side destination directory (overrides `FASTSYNC_DEST_DIR`) | | `--save-to-disk` | Persist received files to disk | diff --git a/src/client/client.c b/src/client/client.c index 25bcf41..65b2500 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -86,8 +86,10 @@ int load_files_multithreaded(void *pipeline_context) { mtx_unlock(&context->mutex_loader); return thrd_success; } - for (int i = 0; i < chunk->element_count; i++) - file_load_data(chunk->items[i]); + if (!context->config->use_sendfile) { + for (int i = 0; i < chunk->element_count; i++) + file_load_data(chunk->items[i]); + } queue_enqueue_multithreaded(context->queue_loader, chunk, &context->mutex_loader, &context->condition_not_empty_loader, @@ -152,11 +154,6 @@ int send_files(Config *config) { } int send_files_multithreaded(Config *config) { - if (config->use_sendfile) { - log_message(LOG_LEVEL_INFO, "Sendfile enabled, falling back to single-threaded"); - return send_files(config); - } - PipelineContextSender *context = pipeline_context_sender_create(config, queue_create(100, chunk_destroy), queue_create(100, chunk_destroy)); @@ -238,6 +235,11 @@ int main(int argc, char *argv[]) { } } + if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression)) { + fprintf(stderr, "Error: -f/--sendfile cannot be combined with -c (compression) or -s (chunk serialization)\n"); + return 1; + } + if (config->use_multithreading) return send_files_multithreaded(config); return send_files(config); diff --git a/test.py b/test.py index 6534921..62790f1 100755 --- a/test.py +++ b/test.py @@ -49,6 +49,7 @@ TEST_CASES = [ "flags": ["-m", "-c", "-s"], }, {"name": "Sendfile (-f)", "flags": ["-f"]}, + {"name": "Sendfile + Multithreading (-f -m)", "flags": ["-f", "-m"]}, ] From d0c57ba2bbcd21a1ee893d0988fc5f2eee7812b3 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 5 Jul 2026 16:33:21 +0200 Subject: [PATCH 3/4] docs: update README with sendfile details and examples --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 95d69c9..bcf8e29 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ FastFileTransfer is a C implementation of a file synchronization system that: 4. Utilizes multithreading for parallel file processing 5. Implements producer-consumer patterns with thread-safe queues 6. Provides both in-memory and disk-based storage options +7. Supports `sendfile()` for zero-copy file transfer ## System Architecture @@ -23,6 +24,7 @@ The system consists of two main components: - Compresses data using zstd algorithm - Serializes chunks into a compact binary format for batch transfer - Sends files to server using custom protocol +- Supports sendfile for zero-copy file transfer (`-f`) - Supports both single-threaded and multi-threaded operation ### Server @@ -122,6 +124,9 @@ make # Sendfile (zero-copy, bypasses userspace for large files) ./build/client -f + +# Sendfile with multithreading +./build/client -f -m ``` ## Testing @@ -154,8 +159,9 @@ tests/ # Unit tests 1. Chunk size (10MB default) affects memory usage and transfer efficiency 2. Compression level (1-22) trades CPU usage for space savings -3. Multithreading improves performance on multi-core systems -4. Thread-safe queues minimize contention between producer/consumer threads +3. `sendfile()` (`-f`) bypasses userspace memory, ~2x faster on localhost for large files +4. Multithreading improves performance on multi-core systems +5. Thread-safe queues minimize contention between producer/consumer threads ## Extensibility From a48b3e3a17b7850a81bfc823014c07b3a2930312 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 5 Jul 2026 16:38:13 +0200 Subject: [PATCH 4/4] refactor: pass use_sendfile to config_create --- src/client/client.c | 2 +- src/shared/config.c | 4 ++-- src/shared/config.h | 2 +- tests/test_config.c | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/client/client.c b/src/client/client.c index 65b2500..68c3d56 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -201,7 +201,7 @@ int main(int argc, char *argv[]) { } Config *config = config_create(str_dup("1.0.0"), source_dir, dest_dir, - save_to_disk, false, false, false, 5, 20); + save_to_disk, false, false, false, 5, 20, false); for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-c") == 0) { config->use_compression = true; diff --git a/src/shared/config.c b/src/shared/config.c index 3bb891f..b2ed396 100644 --- a/src/shared/config.c +++ b/src/shared/config.c @@ -8,7 +8,7 @@ Config *config_create(char *version, char *send_directory, char *receive_directory, bool save_to_disk, bool use_multithreading, bool use_chunk_serialization, bool use_compression, int compression_level, - int num_connections) { + int num_connections, bool use_sendfile) { Config *config = malloc(sizeof(Config)); config->version = version; @@ -20,7 +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; + config->use_sendfile = use_sendfile; return config; } diff --git a/src/shared/config.h b/src/shared/config.h index c58c2d3..f991fda 100644 --- a/src/shared/config.h +++ b/src/shared/config.h @@ -21,7 +21,7 @@ Config *config_create(char *version, char *send_directory, char *receive_directory, bool save_to_disk, bool use_multithreading, bool use_chunk_serialization, bool use_compression, int compression_level, - int num_connections); + int num_connections, bool use_sendfile); void config_delete(Config *config); void config_send(int file_descriptor, Config *config); Config *config_receive(int file_descriptor); diff --git a/tests/test_config.c b/tests/test_config.c index f5aa760..7c587f8 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -8,7 +8,7 @@ static void test_config_lifecycle() { Config *cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), - true, true, false, false, 1, 4); + true, true, false, false, 1, 4, false); EXPECT_NOT_NULL(cfg); EXPECT_EQ_STR(cfg->version, "1.0"); EXPECT_EQ_STR(cfg->send_directory, "/src"); @@ -23,7 +23,7 @@ static void test_config_lifecycle() { static void test_pipeline_sender_lifecycle() { Config *cfg = config_create(str_dup("2.0"), str_dup("/src2"), - str_dup("/dst2"), false, false, true, true, 1, 8); + str_dup("/dst2"), false, false, true, true, 1, 8, false); Queue *q1 = queue_create(5, NULL); Queue *q2 = queue_create(15, NULL); @@ -40,7 +40,7 @@ static void test_pipeline_sender_lifecycle() { static void test_pipeline_receiver_lifecycle() { Config *cfg = config_create(str_dup("3.0"), str_dup("/src3"), - str_dup("/dst3"), true, true, true, true, 1, 2); + str_dup("/dst3"), true, true, true, true, 1, 2, false); Queue *q = queue_create(20, NULL); PipelineContextReceiver *pcr = pipeline_context_receiver_create(cfg, q, 42);