Merge pull request 'Fix critical issues and all tests' (#2) from fix-critical-issues into main
Reviewed-on: #2
This commit is contained in:
+108
-28
@@ -6,8 +6,8 @@
|
||||
#include <string.h>
|
||||
#include <zstd.h>
|
||||
|
||||
#include "array_list.h"
|
||||
#include "chunk.h"
|
||||
#include "array_list.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
@@ -72,23 +72,26 @@ Data *chunk_format(Chunk *chunk) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
char *current_data_pointer = data;
|
||||
// for (int i = 0; i < chunk->element_count; ++i) {
|
||||
// File *file = chunk->items[i];
|
||||
// // add path len
|
||||
// int path_length = (int)strlen(file->path);
|
||||
// memcpy(current_data_pointer, &path_length, sizeof(int));
|
||||
// current_data_pointer += sizeof(int);
|
||||
// // add path
|
||||
// memcpy(current_data_pointer, file->path, path_length);
|
||||
// current_data_pointer += path_length;
|
||||
// // add file data len
|
||||
// unsigned long long file_length = file->stats.st_size;
|
||||
// memcpy(current_data_pointer, &file_length, sizeof(unsigned long long));
|
||||
// current_data_pointer += sizeof(unsigned long long);
|
||||
// // add file data
|
||||
// file_content_to_buffer(file, current_data_pointer);
|
||||
// current_data_pointer += file_length;
|
||||
// }
|
||||
for (int i = 0; i < chunk->element_count; ++i) {
|
||||
File *file = chunk->items[i];
|
||||
// add path len
|
||||
int path_length = (int)strlen(file->path);
|
||||
memcpy(current_data_pointer, &path_length, sizeof(int));
|
||||
current_data_pointer += sizeof(int);
|
||||
// add path
|
||||
memcpy(current_data_pointer, file->path, path_length);
|
||||
current_data_pointer += path_length;
|
||||
// add file data len
|
||||
unsigned long long file_length = file->stats.st_size;
|
||||
memcpy(current_data_pointer, &file_length, sizeof(unsigned long long));
|
||||
current_data_pointer += sizeof(unsigned long long);
|
||||
// add file data
|
||||
if (file->data == NULL) {
|
||||
file_load_data(file);
|
||||
}
|
||||
memcpy(current_data_pointer, file->data->data, file_length);
|
||||
current_data_pointer += file_length;
|
||||
}
|
||||
if (current_data_pointer - data != (long)(long)buffer_size) {
|
||||
perror("Buffer of Chunk wasn't filled enough!");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -123,7 +126,7 @@ Data *chunk_compress(Chunk *chunk, int compression_level) {
|
||||
unsigned long long data_size = chunk->items[i]->stats.st_size;
|
||||
memcpy(data_pointer, &data_size, sizeof(size_t));
|
||||
data_pointer += sizeof(size_t);
|
||||
memcpy(data_pointer, chunk->items[i]->data, data_size);
|
||||
memcpy(data_pointer, chunk->items[i]->data->data, data_size);
|
||||
data_pointer += data_size;
|
||||
}
|
||||
|
||||
@@ -134,18 +137,95 @@ Data *chunk_compress(Chunk *chunk, int compression_level) {
|
||||
Chunk *chunk_decompress(Data *compressed_data) {
|
||||
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
||||
Data *uncompressed_data = data_decompress(compressed_data);
|
||||
ArrayList *files = array_list_create(file_destroy);
|
||||
size_t *data_pointer = uncompressed_data->data;
|
||||
while (data_pointer <
|
||||
(size_t *)uncompressed_data->data + uncompressed_data->size) {
|
||||
size_t path_len = data_pointer[0];
|
||||
|
||||
printf("%zu, testing", path_len);
|
||||
break;
|
||||
if (uncompressed_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
log_message(LOG_LEVEL_DEBUG, "Chunk succesfully decompressed");
|
||||
ArrayList *files = array_list_create((void (*)(void *))data_destroy);
|
||||
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_delete(files);
|
||||
data_destroy(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_delete(files);
|
||||
data_destroy(uncompressed_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *path = malloc(path_len + 1);
|
||||
if (path == NULL) {
|
||||
perror("Could not allocate memory for file path");
|
||||
array_list_delete(files);
|
||||
data_destroy(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_delete(files);
|
||||
data_destroy(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_delete(files);
|
||||
data_destroy(uncompressed_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct stat st = {0};
|
||||
st.st_size = data_size;
|
||||
File *file = file_create(path, &st);
|
||||
if (file == NULL) {
|
||||
free(path);
|
||||
array_list_delete(files);
|
||||
data_destroy(uncompressed_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file->data = data_create(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, files->size);
|
||||
|
||||
// Clean up
|
||||
free(file_array);
|
||||
array_list_delete(files);
|
||||
data_destroy(uncompressed_data);
|
||||
|
||||
log_message(LOG_LEVEL_DEBUG, "Chunk successfully decompressed");
|
||||
return chunk;
|
||||
}
|
||||
|
||||
Data *chunk_data_create(void *data, unsigned long long data_size) {
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ void file_destroy(void *item) {
|
||||
if (item == NULL)
|
||||
return;
|
||||
File *file = (File *)item;
|
||||
free(file->data);
|
||||
data_destroy(file->data);
|
||||
file->data = NULL;
|
||||
free(file->path);
|
||||
file->path = NULL;
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
||||
int file_descriptor =
|
||||
accept(server->file_descriptor, (struct sockaddr *)&server->address,
|
||||
&server->address_length);
|
||||
if (server->file_descriptor < 0) {
|
||||
if (file_descriptor < 0) {
|
||||
perror("Could not accept the connection");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ static void test_file_operations() {
|
||||
|
||||
file_load_data(f);
|
||||
EXPECT_NOT_NULL(f->data);
|
||||
EXPECT_EQ_INT(memcmp(f->data, test_content, test_len), 0);
|
||||
EXPECT_EQ_INT(memcmp(f->data->data, test_content, test_len), 0);
|
||||
|
||||
file_destroy(f);
|
||||
unlink(test_path);
|
||||
|
||||
Reference in New Issue
Block a user