refactor: deduplicate server receive logic, extract shared helpers

- Extract file_save_to_disk() helper (replaces 4x duplicated disk-save boilerplate)
- Extract receive_incremental_check() shared helper (deduplicates STATUS_CHECK
  handling between server.c single-threaded and multiprocessing.c multi-threaded paths)
- Break receive_files() into receive_chunk() and receive_manifest() sub-handlers
- Add incremental sync test case to test.py
- Rebase onto main
This commit is contained in:
2026-07-18 16:47:39 +02:00
parent 03d6ba0da5
commit 5ed12f2bf0
5 changed files with 162 additions and 200 deletions
+6 -56
View File
@@ -6,14 +6,12 @@
#include "data.h"
#include "file.h"
#include "log.h"
#include "metadata.h"
#include "protocol.h"
#include "queue.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <threads.h>
PipelineContextSender *pipeline_context_sender_create(Config *config,
@@ -129,52 +127,10 @@ int receive_thread(void *pipeline_context) {
if (!receive_status(file_descriptor, &status)) return thrd_error;
while (status == STATUS_NEXT || status == STATUS_CHUNK || status == STATUS_CHECK) {
if (status == STATUS_CHECK) {
char *check_path = receive_str(file_descriptor);
if (check_path == NULL) { send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
unsigned long long check_size;
long long check_mtime;
if (!receive_n_data(file_descriptor, &check_size, sizeof(check_size)) ||
!receive_n_data(file_descriptor, &check_mtime, sizeof(check_mtime))) {
free(check_path);
send_status(file_descriptor, STATUS_ERROR);
return thrd_error;
}
char *full_path = path_cat(config->receive_root_directory, check_path);
struct stat st;
bool match = false;
if (full_path && stat(full_path, &st) == 0 &&
(unsigned long long)st.st_size == check_size &&
(long long)st.st_mtime == check_mtime) {
match = true;
}
free(full_path);
if (match) {
if (!send_status(file_descriptor, STATUS_OK)) { free(check_path); return thrd_error; }
free(check_path);
} else {
if (!send_status(file_descriptor, STATUS_NEXT)) { free(check_path); return thrd_error; }
File *file = file_create(check_path);
free(check_path);
if (file == NULL) { send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
if (config->use_metadata) {
int meta_ok = 1;
file->metadata = metadata_receive(file_descriptor, &meta_ok);
if (!meta_ok) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
}
Data *file_data = receive_data(file_descriptor);
if (file_data == NULL) {
file_destroy(file);
send_status(file_descriptor, STATUS_ERROR);
return thrd_error;
}
if (config->use_compression) {
Data *uncompressed = data_decompress(file_data);
data_destroy(file_data);
if (uncompressed == NULL) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
file_data = uncompressed;
}
data_destroy(file->data);
file->data = file_data;
bool skipped;
File *file = receive_incremental_check(file_descriptor, config, &skipped);
if (!skipped) {
if (file == NULL) return thrd_error;
queue_enqueue_multithreaded(context->queue, file, &context->mutex,
&context->condition_not_empty,
&context->condition_not_full);
@@ -234,14 +190,8 @@ int write_thread(void *pipeline_context) {
free(root_directory);
return thrd_success;
}
if (save_to_disk) {
char *disk_path = path_cat(root_directory, file->path);
if (disk_path) {
to_disk(disk_path, file->data->data, file->data->size);
file_restore_metadata(disk_path, file->metadata);
free(disk_path);
}
}
if (save_to_disk)
file_save_to_disk(root_directory, file);
file_destroy(file);
}
}