Fix critical issues: implement chunk_decompress and fix file descriptor check in server_listen
This commit is contained in:
+86
-11
@@ -134,18 +134,93 @@ Data *chunk_compress(Chunk *chunk, int compression_level) {
|
|||||||
Chunk *chunk_decompress(Data *compressed_data) {
|
Chunk *chunk_decompress(Data *compressed_data) {
|
||||||
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
||||||
Data *uncompressed_data = data_decompress(compressed_data);
|
Data *uncompressed_data = data_decompress(compressed_data);
|
||||||
ArrayList *files = array_list_create(file_destroy);
|
if (uncompressed_data == NULL) {
|
||||||
size_t *data_pointer = uncompressed_data->data;
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk data");
|
||||||
while (data_pointer <
|
return NULL;
|
||||||
(size_t *)uncompressed_data->data + uncompressed_data->size) {
|
|
||||||
size_t path_len = data_pointer[0];
|
|
||||||
|
|
||||||
printf("%zu, testing", path_len);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log_message(LOG_LEVEL_DEBUG, "Chunk succesfully decompressed");
|
ArrayList *files = array_list_create(file_destroy);
|
||||||
return NULL;
|
char *data_pointer = uncompressed_data->data;
|
||||||
|
size_t remaining_size = uncompressed_data->size;
|
||||||
|
|
||||||
|
while (remaining_size > 0) {
|
||||||
|
if (remaining_size < sizeof(size_t)) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path length");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t path_len = *(size_t *)data_pointer;
|
||||||
|
data_pointer += sizeof(size_t);
|
||||||
|
remaining_size -= sizeof(size_t);
|
||||||
|
|
||||||
|
if (remaining_size < path_len) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *path = malloc(path_len + 1);
|
||||||
|
if (path == NULL) {
|
||||||
|
perror("Could not allocate memory for file path");
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memcpy(path, data_pointer, path_len);
|
||||||
|
path[path_len] = '\0';
|
||||||
|
data_pointer += path_len;
|
||||||
|
remaining_size -= path_len;
|
||||||
|
|
||||||
|
if (remaining_size < sizeof(size_t)) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for data size");
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t data_size = *(size_t *)data_pointer;
|
||||||
|
data_pointer += sizeof(size_t);
|
||||||
|
remaining_size -= sizeof(size_t);
|
||||||
|
|
||||||
|
if (remaining_size < data_size) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for file content");
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
File *file = file_create(path, data_size);
|
||||||
|
if (file == NULL) {
|
||||||
|
free(path);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(file->data, data_pointer, data_size);
|
||||||
|
data_pointer += data_size;
|
||||||
|
remaining_size -= data_size;
|
||||||
|
|
||||||
|
array_list_add(files, file);
|
||||||
|
free(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the chunk from the files
|
||||||
|
File **file_array = (File **)array_list_to_array(files);
|
||||||
|
Chunk *chunk = chunk_create(file_array, array_list_size(files));
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
free(file_array);
|
||||||
|
array_list_destroy(files);
|
||||||
|
data_delete(uncompressed_data);
|
||||||
|
|
||||||
|
log_message(LOG_LEVEL_DEBUG, "Chunk successfully decompressed");
|
||||||
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data *chunk_data_create(void *data, unsigned long long data_size) {
|
Data *chunk_data_create(void *data, unsigned long long data_size) {
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
|||||||
int file_descriptor =
|
int file_descriptor =
|
||||||
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
||||||
&server->address_length);
|
&server->address_length);
|
||||||
if (server->file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not accept the connection");
|
perror("Could not accept the connection");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user