refactor: split monolithic modules and extract shared components

- Remove dead socket.c/socket.h (accumulated duplicate symbols)
- Extract compression.h/c (data_compress/decompress from data.c)
- Extract io.h/c (low-level I/O from protocol.c)
- Extract metadata.h/c (unified metadata wire format from file.c, chunk.c, utils.c)
- Split client.c into client_cli.c (CLI parsing) + client_send.c (send logic)
- Move receiver pipeline threads from server.c to multiprocessing.c
- Move to_disk/file_restore_metadata from utils.c to file.c/metadata.c
- Fix const qualifiers on to_disk and str_dup signatures
- Suppress chown unused-result warning
This commit is contained in:
2026-07-15 19:36:26 +02:00
parent b3f69208ce
commit 02619cca5c
29 changed files with 942 additions and 832 deletions
+82
View File
@@ -0,0 +1,82 @@
#include "metadata.h"
#include "file.h"
#include "io.h"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define FILE_METADATA_WIRE_SIZE (sizeof(mode_t) + sizeof(uid_t) + sizeof(gid_t) + sizeof(time_t) + sizeof(long))
void metadata_to_buf(char **buf, FileMetadata *m) {
int present = (m != NULL) ? 1 : 0;
memcpy(*buf, &present, sizeof(int));
*buf += sizeof(int);
if (m == NULL)
return;
memcpy(*buf, &m->mode, sizeof(mode_t)); *buf += sizeof(mode_t);
memcpy(*buf, &m->uid, sizeof(uid_t)); *buf += sizeof(uid_t);
memcpy(*buf, &m->gid, sizeof(gid_t)); *buf += sizeof(gid_t);
memcpy(*buf, &m->mtime_sec, sizeof(time_t)); *buf += sizeof(time_t);
memcpy(*buf, &m->mtime_nsec, sizeof(long)); *buf += sizeof(long);
}
FileMetadata *metadata_from_buf(char **buf) {
int present;
memcpy(&present, *buf, sizeof(int));
*buf += sizeof(int);
if (!present)
return NULL;
FileMetadata *m = malloc(sizeof(FileMetadata));
memcpy(&m->mode, *buf, sizeof(mode_t)); *buf += sizeof(mode_t);
memcpy(&m->uid, *buf, sizeof(uid_t)); *buf += sizeof(uid_t);
memcpy(&m->gid, *buf, sizeof(gid_t)); *buf += sizeof(gid_t);
memcpy(&m->mtime_sec, *buf, sizeof(time_t)); *buf += sizeof(time_t);
memcpy(&m->mtime_nsec, *buf, sizeof(long)); *buf += sizeof(long);
return m;
}
void metadata_send(int file_descriptor, FileMetadata *m) {
if (m == NULL) {
int zero = 0;
send_n_data(file_descriptor, &zero, sizeof(int));
return;
}
int present = 1;
send_n_data(file_descriptor, &present, sizeof(int));
send_n_data(file_descriptor, &m->mode, sizeof(mode_t));
send_n_data(file_descriptor, &m->uid, sizeof(uid_t));
send_n_data(file_descriptor, &m->gid, sizeof(gid_t));
send_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t));
send_n_data(file_descriptor, &m->mtime_nsec, sizeof(long));
}
FileMetadata *metadata_receive(int file_descriptor) {
int present;
receive_n_data(file_descriptor, &present, sizeof(int));
if (!present)
return NULL;
FileMetadata *m = malloc(sizeof(FileMetadata));
receive_n_data(file_descriptor, &m->mode, sizeof(mode_t));
receive_n_data(file_descriptor, &m->uid, sizeof(uid_t));
receive_n_data(file_descriptor, &m->gid, sizeof(gid_t));
receive_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t));
receive_n_data(file_descriptor, &m->mtime_nsec, sizeof(long));
return m;
}
void file_restore_metadata(const char *path, FileMetadata *metadata) {
if (metadata == NULL)
return;
chmod(path, metadata->mode & 07777);
int chown_ret = chown(path, metadata->uid, metadata->gid);
(void)chown_ret;
struct timespec times[2];
times[0].tv_sec = 0;
times[0].tv_nsec = UTIME_OMIT;
times[1].tv_sec = metadata->mtime_sec;
times[1].tv_nsec = metadata->mtime_nsec;
utimensat(AT_FDCWD, path, times, 0);
}