From 279777fc8b0289f660d614acb1681bd7a87c4b72 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Sat, 8 Nov 2025 02:43:41 +0100 Subject: [PATCH] Progress --- .gitignore | 1 + src/chunk_server.c | 6 ++++-- src/client.c | 19 +++++++++++++------ src/system.c | 5 ++++- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9cc18da..512eab8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.exe *.out +chunk_server_data_* diff --git a/src/chunk_server.c b/src/chunk_server.c index aac83b4..99b47da 100644 --- a/src/chunk_server.c +++ b/src/chunk_server.c @@ -76,8 +76,8 @@ static void append_hex_as_str(char *out, SHA256 hash) { char table[] = "0123456789abcdef"; for (int i = 0; i < (int) sizeof(hash); i++) { - out[(i << 1) + 0] = table[hash.data[i] >> 4]; - out[(i << 1) + 1] = table[hash.data[i] & 0xF]; + out[(i << 1) + 0] = table[(uint8_t) hash.data[i] >> 4]; + out[(i << 1) + 1] = table[(uint8_t) hash.data[i] & 0xF]; } } @@ -699,6 +699,8 @@ process_client_upload_chunk(ChunkServer *state, int conn_idx, ByteView msg) ByteQueue *output = tcp_output_buffer(&state->tcp, conn_idx); message_writer_init(&writer, output, MESSAGE_TYPE_UPLOAD_CHUNK_SUCCESS); + message_write(&writer, &new_hash, sizeof(new_hash)); + if (!message_writer_free(&writer)) return -1; return 0; diff --git a/src/client.c b/src/client.c index e178db6..6ecb4cd 100644 --- a/src/client.c +++ b/src/client.c @@ -1534,22 +1534,29 @@ static void process_event_for_write(TinyDFS *tdfs, return; } + SHA256 hash; + uint16_t expected_type; + if (tdfs->operations[opidx].uploads[found].chunk_index >= tdfs->operations[opidx].num_hashes) + expected_type = MESSAGE_TYPE_CREATE_CHUNK_SUCCESS; + else { + expected_type = MESSAGE_TYPE_UPLOAD_CHUNK_SUCCESS; + + if (!binary_read(&reader, &hash, sizeof(hash))) { + // TODO + return; + } + } // Check that there is nothing else to read if (binary_read(&reader, NULL, 1)) { // TODO return; } - uint16_t expected_type; - if (tdfs->operations[opidx].uploads[found].chunk_index >= tdfs->operations[opidx].num_hashes) - expected_type = MESSAGE_TYPE_CREATE_CHUNK_SUCCESS; - else - expected_type = MESSAGE_TYPE_UPLOAD_CHUNK_SUCCESS; - if (type != expected_type) { tdfs->operations[opidx].uploads[found].status = UPLOAD_FAILED; } else { tdfs->operations[opidx].uploads[found].status = UPLOAD_COMPLETED; + tdfs->operations[opidx].uploads[found].final_hash = hash; for (int i = 0; i < tdfs->operations[opidx].num_uploads; i++) { if (tdfs->operations[opidx].uploads[i].status == UPLOAD_WAITING diff --git a/src/system.c b/src/system.c index 26de745..369b5af 100644 --- a/src/system.c +++ b/src/system.c @@ -1590,7 +1590,10 @@ int mock_fstat(int fd, struct stat *buf) int mock_mkstemp(char *path) { - return mkstemp(path); + int fd = mkstemp(path); + if (fd < 0) return fd; + + return wrap_native_file_into_desc(fd); } char* mock_realpath(char *path, char *dst)