#include #include #include #include #include #include #include #include "array_list.h" #include "chunk.h" #include "data.h" #include "file.h" #include "log.h" Chunk *chunk_create(File **items, int element_count) { Chunk *chunk = (Chunk *)malloc(sizeof(Chunk)); if (chunk == NULL) { perror("FATAL ERROR: Could not allocate memory for chunk structure"); exit(EXIT_FAILURE); } chunk->items = (File **)malloc(element_count * sizeof(File *)); if (chunk->items == NULL) { perror("FATAL ERROR: Could not allocate memory for items of chunk " "structure"); free(chunk); exit(EXIT_FAILURE); } for (int i = 0; i < element_count; i++) { chunk->items[i] = items[i]; } chunk->element_count = element_count; return chunk; } void chunk_destroy(void *item) { if (item == NULL) { return; } Chunk *chunk = (Chunk *)item; for (int i = 0; i < chunk->element_count; ++i) { if (chunk->items[i] != NULL) { file_destroy(chunk->items[i]); } } free(chunk->items); free(chunk); } void chunk_print(void *item) { if (item == NULL) return; Chunk *chunk = (Chunk *)item; for (int i = 0; i < chunk->element_count; ++i) if (chunk->items[i] != NULL) file_print(chunk->items[i]); } Data *chunk_format(Chunk *chunk) { unsigned long long buffer_size = 0; for (int i = 0; i < chunk->element_count; ++i) { buffer_size += sizeof(int); buffer_size += strlen(chunk->items[i]->path); buffer_size += sizeof(unsigned long long); buffer_size += chunk->items[i]->stats.st_size; } char *data = malloc(buffer_size); if (data == NULL) { perror("Could not allocate data for ChunkFormated!"); 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; // } if (current_data_pointer - data != (long)(long)buffer_size) { perror("Buffer of Chunk wasn't filled enough!"); exit(EXIT_FAILURE); } return chunk_data_create(data, buffer_size); } Data *chunk_compress(Chunk *chunk, int compression_level) { log_message(LOG_LEVEL_DEBUG, "Starting to gather data for chunk compression"); unsigned long long data_size = 0; for (int i = 0; i < chunk->element_count; i++) { data_size += sizeof(unsigned long long); data_size += strlen(chunk->items[i]->path); data_size += sizeof(unsigned long long); data_size += chunk->items[i]->stats.st_size; } Data *data = data_create_empty(data_size); if (data == NULL) { log_message(LOG_LEVEL_ERROR, "Could not allocate memory for chunk compression"); exit(EXIT_FAILURE); } char *data_pointer = data->data; for (int i = 0; i < chunk->element_count; i++) { // path length size_t path_len = strlen(chunk->items[i]->path); memcpy(data_pointer, &path_len, sizeof(size_t)); data_pointer += sizeof(size_t); memcpy(data_pointer, chunk->items[i]->path, path_len); data_pointer += path_len; // file data 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); data_pointer += data_size; } log_message(LOG_LEVEL_DEBUG, "Chunk succesfully compressed"); return data_compress(data, 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; } log_message(LOG_LEVEL_DEBUG, "Chunk succesfully decompressed"); return NULL; } Data *chunk_data_create(void *data, unsigned long long data_size) { Data *chunk_formated = malloc(sizeof(Data)); if (chunk_formated == NULL) { perror("Could not allocate memory for ChunkFormated"); exit(EXIT_FAILURE); } chunk_formated->data = data; chunk_formated->size = data_size; return chunk_formated; } void chunk_data_delete(void *chunk) { Data *chunk_data = (Data *)chunk; free(chunk_data->data); free(chunk_data); } // void chunk_data_to_disk(ChunkData *chunk_formated, char *root_directory) { // char *current_data_pointer = chunk_formated->data; // while (current_data_pointer - (char *)chunk_formated->data < // chunk_formated->data_size) { // // get path length // int path_length = 0; // memcpy(&path_length, (int *)current_data_pointer, sizeof(int)); // current_data_pointer += sizeof(int); // // get path // int path_dir_size = // (strlen(root_directory) + path_length + 1) * sizeof(char); // char *path = (char *)malloc(path_dir_size); // if (path == NULL) { // perror("Could not allocate memory for path!"); // exit(EXIT_FAILURE); // } // snprintf(path, path_dir_size, "%s%.*s", root_directory, path_length, // current_data_pointer); // current_data_pointer += sizeof(char) * path_length; // // get data length // unsigned long long data_size = 0; // memcpy(&data_size, (unsigned long long *)current_data_pointer, // sizeof(unsigned long long)); // current_data_pointer += sizeof(unsigned long long); // // create File Receive // FileReceive *file = // file_receive_create(path, data_size, current_data_pointer); // file_receive_print(file); // file_receive_to_disk(file); // current_data_pointer += sizeof(char) * data_size; // } // }