fix: bring project back to a working state
This commit is contained in:
@@ -7,6 +7,9 @@ set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
add_compile_options(-Wall -g -O3)
|
||||
# add_compile_options(-Wall -g -O1 -fsanitize=address)
|
||||
|
||||
# add_link_options(-fsanitize=address)
|
||||
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
@@ -14,7 +14,10 @@ pkgs.mkShell {
|
||||
zstd
|
||||
];
|
||||
|
||||
NIX_ENFORCE_PURITY = 0;
|
||||
|
||||
shellHook = ''
|
||||
./tmux.sh
|
||||
export NIX_ENFORCE_PURITY=0
|
||||
cmake -B build
|
||||
'';
|
||||
}
|
||||
|
||||
+63
-41
@@ -6,6 +6,8 @@
|
||||
|
||||
#include "chunk.h"
|
||||
#include "config.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "multiprocessing.h"
|
||||
#include "queue.h"
|
||||
@@ -15,13 +17,19 @@
|
||||
#include <dirent.h>
|
||||
|
||||
int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
||||
if (config->use_compression) {
|
||||
if (config->use_compression && config->use_chunk_serialization) {
|
||||
Data *data = chunk_compress(chunk, config->compression_level);
|
||||
send_data(client->file_descriptor, data->data, data->size);
|
||||
} else {
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
send_status(client->file_descriptor, NEXT);
|
||||
send_status(client->file_descriptor, STATUS_NEXT);
|
||||
File *file = chunk->items[i];
|
||||
if (config->use_compression) {
|
||||
Data *compressed_data =
|
||||
data_compress(file->data, config->compression_level);
|
||||
data_destroy(file->data);
|
||||
file->data = compressed_data;
|
||||
}
|
||||
file_send_single_calls(file, client->file_descriptor);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +84,11 @@ int load_files_multithreaded(void *pipeline_context) {
|
||||
int send_chunks_multithreaded(void *pipeline_context) {
|
||||
PipelineContextSender *context = (PipelineContextSender *)pipeline_context;
|
||||
Client *client = client_create();
|
||||
client_connect(client, "127.0.0.1", 8080);
|
||||
const char *env_ip = getenv("FASTSYNC_SERVER_IP");
|
||||
const char *ip = env_ip ? env_ip : "127.0.0.1";
|
||||
const char *env_port = getenv("FASTSYNC_SERVER_PORT");
|
||||
int port = env_port ? atoi(env_port) : 8080;
|
||||
client_connect(client, (char *)ip, port);
|
||||
config_send(client->file_descriptor, context->config);
|
||||
|
||||
while (true) {
|
||||
@@ -85,7 +97,7 @@ int send_chunks_multithreaded(void *pipeline_context) {
|
||||
&context->condition_not_empty_loader,
|
||||
&context->condition_not_full_loader, &context->loader_done);
|
||||
if (current_chunk == NULL) {
|
||||
send_status(client->file_descriptor, FINISHED);
|
||||
send_status(client->file_descriptor, STATUS_FINISHED);
|
||||
client_disconnect(client);
|
||||
client_delete(client);
|
||||
return thrd_success;
|
||||
@@ -100,7 +112,11 @@ int send_chunks_multithreaded(void *pipeline_context) {
|
||||
|
||||
int send_files(Config *config) {
|
||||
Client *client = client_create();
|
||||
client_connect(client, "127.0.0.1", 8080);
|
||||
const char *env_ip = getenv("FASTSYNC_SERVER_IP");
|
||||
const char *ip = env_ip ? env_ip : "127.0.0.1";
|
||||
const char *env_port = getenv("FASTSYNC_SERVER_PORT");
|
||||
int port = env_port ? atoi(env_port) : 8080;
|
||||
client_connect(client, (char *)ip, port);
|
||||
config_send(client->file_descriptor, config);
|
||||
DirectoryScanner *scanner = directory_scanner_create(config->send_directory);
|
||||
Chunk *current_chunk;
|
||||
@@ -110,24 +126,15 @@ int send_files(Config *config) {
|
||||
send_chunk(client, current_chunk, config);
|
||||
chunk_destroy(current_chunk);
|
||||
}
|
||||
send_status(client->file_descriptor, FINISHED);
|
||||
if (receive_status(client->file_descriptor) != OK)
|
||||
send_status(client->file_descriptor, STATUS_FINISHED);
|
||||
if (receive_status(client->file_descriptor) != STATUS_OK)
|
||||
return -1;
|
||||
printf("FINISHED");
|
||||
directory_scanner_destroy(scanner);
|
||||
client_disconnect(client);
|
||||
client_delete(client);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_arg(char *argument_given, char *argument_to_set, bool *result,
|
||||
char *message) {
|
||||
if (strcmp(argument_given, argument_to_set) == 0) {
|
||||
*result = true;
|
||||
log_message(LOG_LEVEL_INFO, message);
|
||||
}
|
||||
}
|
||||
|
||||
int send_files_multithreaded(Config *config) {
|
||||
PipelineContextSender *context =
|
||||
pipeline_context_sender_create(config, queue_create(100, chunk_destroy),
|
||||
@@ -151,40 +158,55 @@ int send_files_multithreaded(Config *config) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// int send_files_multiprocessed(Config *config) {
|
||||
// int cores = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
// int pid = fork();
|
||||
// if (pid == -1) {
|
||||
// perror("Error Forking!");
|
||||
// return 1;
|
||||
// } else if (pid == 0) {
|
||||
// }
|
||||
// for (int i = 0; i < cores; i++) {
|
||||
// int pid = fork();
|
||||
// if (pid == -1) {
|
||||
// perror("Error forking!");
|
||||
// return 1;
|
||||
// } else if (pid == 0) {
|
||||
// }
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
void handle_arg(char *argument_given, char *argument_to_set, bool *result,
|
||||
char *message) {
|
||||
if (strcmp(argument_given, argument_to_set) == 0) {
|
||||
*result = true;
|
||||
log_message(LOG_LEVEL_INFO, message);
|
||||
}
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
Config *config = config_create(
|
||||
str_dup("1.0.0"), str_dup("/home/taptap/Nextcloud/Uni/moodle/MINT-Raum"),
|
||||
str_dup("./data_copied"), false, false, false, false, 5, 1);
|
||||
const char *env_source = getenv("FASTSYNC_SOURCE_DIR");
|
||||
const char *env_dest = getenv("FASTSYNC_DEST_DIR");
|
||||
const char *env_save = getenv("FASTSYNC_SAVE_TO_DISK");
|
||||
|
||||
char *source_dir =
|
||||
env_source ? str_dup((char *)env_source)
|
||||
: str_dup("/home/taptap/Nextcloud/Uni/moodle/B. Schnor: "
|
||||
"Konzepte Paralleler Programmierung, SoSe 2026");
|
||||
char *dest_dir =
|
||||
env_dest ? str_dup((char *)env_dest) : str_dup("./data_copied");
|
||||
bool save_to_disk = false;
|
||||
if (env_save &&
|
||||
(strcmp(env_save, "true") == 0 || strcmp(env_save, "1") == 0)) {
|
||||
save_to_disk = true;
|
||||
}
|
||||
|
||||
Config *config = config_create(str_dup("1.0.0"), source_dir, dest_dir,
|
||||
save_to_disk, false, false, false, 5, 20);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-c") == 0) {
|
||||
config->use_compression = true;
|
||||
log_message(LOG_LEVEL_INFO, "Enabled Compression");
|
||||
|
||||
if (i + 1 < argc) {
|
||||
char *end_ptr;
|
||||
int level = strtol(argv[i + 1], &end_ptr, 10);
|
||||
if (*end_ptr == '\0') {
|
||||
config->compression_level = level;
|
||||
log_message(LOG_LEVEL_INFO, "Set Compression level to %d",
|
||||
config->compression_level);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
handle_arg(argv[i], "-m", &config->use_multithreading,
|
||||
"Enabled Multithreading");
|
||||
handle_arg(argv[i], "-s", &config->use_chunk_serialization,
|
||||
"Enabled Chunk Serialization");
|
||||
handle_arg(argv[i], "-c", &config->use_compression, "Enabled Compression");
|
||||
}
|
||||
}
|
||||
|
||||
if (config->use_multithreading)
|
||||
return send_files_multithreaded(config);
|
||||
// else if (config->use_multiprocessing)
|
||||
// return send_files_multiprocessed(config);
|
||||
return send_files(config);
|
||||
}
|
||||
|
||||
+19
-9
@@ -1,5 +1,7 @@
|
||||
#include "chunk.h"
|
||||
#include "config.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "multiprocessing.h"
|
||||
#include "queue.h"
|
||||
#include "socket.h"
|
||||
@@ -9,9 +11,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <threads.h>
|
||||
|
||||
FileReceive *receive_file_receive(int file_descriptor) {
|
||||
FileReceive *receive_file_receive(Config *config, int file_descriptor) {
|
||||
char *path = (char *)receive_str(file_descriptor);
|
||||
Data *file_data = receive_data(file_descriptor);
|
||||
if (config->use_compression) {
|
||||
Data *file_data_uncompressed = data_decompress(file_data);
|
||||
free(file_data);
|
||||
file_data = file_data_uncompressed;
|
||||
}
|
||||
FileReceive *file = file_receive_create(path, file_data);
|
||||
return file;
|
||||
}
|
||||
@@ -21,10 +28,11 @@ int receive_thread(void *pipeline_context) {
|
||||
(PipelineContextReceiver *)pipeline_context;
|
||||
mtx_lock(&context->mutex);
|
||||
int file_descriptor = context->file_descriptor;
|
||||
Config *config = context->config;
|
||||
mtx_unlock(&context->mutex);
|
||||
|
||||
while (receive_status(file_descriptor) == NEXT) {
|
||||
FileReceive *file = receive_file_receive(file_descriptor);
|
||||
while (receive_status(file_descriptor) == STATUS_NEXT) {
|
||||
FileReceive *file = receive_file_receive(config, file_descriptor);
|
||||
queue_enqueue_multithreaded(context->queue, file, &context->mutex,
|
||||
&context->condition_not_empty,
|
||||
&context->condition_not_full);
|
||||
@@ -60,19 +68,21 @@ int write_thread(void *pipeline_context) {
|
||||
|
||||
int receive_files(Config *config, int file_descriptor) {
|
||||
Status status = receive_status(file_descriptor);
|
||||
while (status == NEXT) {
|
||||
FileReceive *file = receive_file_receive(file_descriptor);
|
||||
while (status == STATUS_NEXT) {
|
||||
FileReceive *file = receive_file_receive(config, file_descriptor);
|
||||
if (config->save_to_disk)
|
||||
to_disk(path_cat(config->receive_root_directory, file->path),
|
||||
file->data->data, file->data->size);
|
||||
file_receive_destroy(file);
|
||||
// send_status(file_descriptor, STATUS_OK);
|
||||
status = receive_status(file_descriptor);
|
||||
}
|
||||
if (status != FINISHED) {
|
||||
send_status(file_descriptor, ERROR);
|
||||
if (status != STATUS_FINISHED) {
|
||||
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED or NEXT Status");
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
send_status(file_descriptor, OK);
|
||||
send_status(file_descriptor, STATUS_OK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+40
-117
@@ -6,99 +6,11 @@
|
||||
#include <string.h>
|
||||
#include <zstd.h>
|
||||
|
||||
#include "array_list.h"
|
||||
#include "chunk.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "socket.h"
|
||||
|
||||
File *file_create(const char *path, struct stat *stats) {
|
||||
File *file = (File *)malloc(sizeof(File));
|
||||
if (file == NULL) {
|
||||
perror("FATAL ERROR: Could not allocate memory for file struct");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
file->stats = *stats;
|
||||
|
||||
int path_len = strlen(path);
|
||||
file->path = (char *)malloc(path_len + 1);
|
||||
if (file->path == NULL) {
|
||||
perror("FATAL ERROR: Could not allocate memory for path file string");
|
||||
free(file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
strcpy(file->path, path);
|
||||
file->data = NULL;
|
||||
return file;
|
||||
}
|
||||
|
||||
void file_destroy(void *item) {
|
||||
if (item == NULL)
|
||||
return;
|
||||
File *file = (File *)item;
|
||||
free(file->data);
|
||||
file->data = NULL;
|
||||
free(file->path);
|
||||
file->path = NULL;
|
||||
free(file);
|
||||
}
|
||||
|
||||
void file_load_data(File *file) {
|
||||
if (file == NULL)
|
||||
return;
|
||||
file->data = malloc(file->stats.st_size);
|
||||
if (file->data == NULL) {
|
||||
perror("Could not allocate memeor y for file data!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
file_content_to_buffer(file, file->data);
|
||||
}
|
||||
|
||||
void file_print(void *item) {
|
||||
if (item == NULL)
|
||||
return;
|
||||
printf("%s\n", ((File *)item)->path);
|
||||
}
|
||||
|
||||
void file_send_single_calls(File *file, int file_descriptor) {
|
||||
send_str(file_descriptor, file->path);
|
||||
send_data(file_descriptor, file->data, file->stats.st_size);
|
||||
}
|
||||
|
||||
void file_content_to_buffer(File *file, char *buffer) {
|
||||
if (buffer == NULL) {
|
||||
perror("Buffer is to write file content to is NULL!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
FILE *file_pointer = fopen(file->path, "rb");
|
||||
if (file_pointer == NULL) {
|
||||
perror("Could not open the file!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t bytes_read = fread(buffer, 1, file->stats.st_size, file_pointer);
|
||||
if (bytes_read != (size_t)file->stats.st_size) {
|
||||
perror("Read to many or to less bytes from File!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose(file_pointer);
|
||||
}
|
||||
|
||||
FileReceive *file_receive_create(char *path, Data *data) {
|
||||
FileReceive *file = malloc(sizeof(FileReceive));
|
||||
file->path = path;
|
||||
file->data_fragment = data;
|
||||
return file;
|
||||
}
|
||||
|
||||
void file_receive_destroy(void *file_receive) {
|
||||
if (file_receive == NULL)
|
||||
return;
|
||||
FileReceive *file = (FileReceive *)file_receive;
|
||||
data_destroy(file->data_fragment);
|
||||
free(file->path);
|
||||
free(file);
|
||||
}
|
||||
|
||||
Chunk *chunk_create(File **items, int element_count) {
|
||||
Chunk *chunk = (Chunk *)malloc(sizeof(Chunk));
|
||||
@@ -160,23 +72,23 @@ 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
|
||||
// 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);
|
||||
@@ -202,27 +114,38 @@ Data *chunk_compress(Chunk *chunk, int compression_level) {
|
||||
char *data_pointer = data->data;
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
// path length
|
||||
unsigned long long path_len = strlen(chunk->items[i]->path);
|
||||
memcpy(data_pointer, &path_len, sizeof(unsigned long long));
|
||||
data_pointer += sizeof(unsigned long long);
|
||||
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(unsigned long long));
|
||||
data_pointer += sizeof(unsigned long long);
|
||||
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 compress_data(data, compression_level);
|
||||
return data_compress(data, compression_level);
|
||||
}
|
||||
|
||||
Chunk *chunk_decompress(Data *data, int compression_level) {
|
||||
// ArrayList *files = array_list_create(file_destroy);
|
||||
// size_t data_size = ZSTD_getFrameContentSize(const void *src, size_t
|
||||
// srcSize);
|
||||
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) {
|
||||
|
||||
+2
-22
@@ -2,44 +2,24 @@
|
||||
#define CHUNK_H
|
||||
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define DESIRED_CHUNK_SIZE 10 * 1024 * 1024
|
||||
#define FILE_PATH_SEPERATOR "#&&SEPP&&#"
|
||||
#define FILE_PATH_DATA_SEPERATOR "#&&SEPD&&#"
|
||||
|
||||
typedef struct {
|
||||
char *path;
|
||||
struct stat stats;
|
||||
char *data;
|
||||
} File;
|
||||
|
||||
typedef struct {
|
||||
char *path;
|
||||
Data *data;
|
||||
} FileReceive;
|
||||
|
||||
typedef struct {
|
||||
File **items;
|
||||
int element_count;
|
||||
} Chunk;
|
||||
|
||||
File *file_create(const char *path, struct stat *stats);
|
||||
void file_destroy(void *item);
|
||||
void file_load_data(File *file);
|
||||
void file_print(void *item);
|
||||
void file_send_single_calls(File *file, int file_descriptor);
|
||||
void file_content_to_buffer(File *file, char *buffer);
|
||||
|
||||
FileReceive *file_receive_create(char *path, Data *data);
|
||||
void file_receive_destroy(void *file_receive);
|
||||
|
||||
Chunk *chunk_create(File **items, int element_count);
|
||||
void chunk_destroy(void *chunk);
|
||||
void chunk_print(void *chunk);
|
||||
Data *chunk_format(Chunk *chunk);
|
||||
Data *chunk_compress(Chunk *chunk, int compression_level);
|
||||
Chunk *chunk_decompress(Data *data, int compression_level);
|
||||
Chunk *chunk_decompress(Data *compressed_data);
|
||||
|
||||
Data *chunk_data_create(void *data, unsigned long long data_size);
|
||||
void chunk_data_delete(void *chunk);
|
||||
|
||||
+2
-2
@@ -40,7 +40,7 @@ void config_send(int file_descriptor, Config *config) {
|
||||
send_int(file_descriptor, config->use_compression);
|
||||
send_int(file_descriptor, config->use_compression);
|
||||
send_int(file_descriptor, config->num_connections);
|
||||
if (receive_status(file_descriptor) != OK) {
|
||||
if (receive_status(file_descriptor) != STATUS_OK) {
|
||||
perror("Error transmitting config!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -57,6 +57,6 @@ Config *config_receive(int file_descriptor) {
|
||||
config->use_compression = receive_int(file_descriptor);
|
||||
config->compression_level = receive_int(file_descriptor);
|
||||
config->num_connections = receive_int(file_descriptor);
|
||||
send_status(file_descriptor, OK);
|
||||
send_status(file_descriptor, STATUS_OK);
|
||||
return config;
|
||||
}
|
||||
|
||||
+5
-4
@@ -21,7 +21,7 @@ Data *data_create(void *data, size_t data_size) {
|
||||
}
|
||||
new_data->data = data;
|
||||
new_data->size = data_size;
|
||||
return data;
|
||||
return new_data;
|
||||
}
|
||||
|
||||
void data_destroy(Data *data) {
|
||||
@@ -29,7 +29,7 @@ void data_destroy(Data *data) {
|
||||
free(data);
|
||||
}
|
||||
|
||||
Data *compress_data(Data *data_to_compress, int compression_level) {
|
||||
Data *data_compress(Data *data_to_compress, int compression_level) {
|
||||
log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
|
||||
Data *compressed_data =
|
||||
data_create_empty(ZSTD_compressBound(data_to_compress->size));
|
||||
@@ -43,11 +43,12 @@ Data *compress_data(Data *data_to_compress, int compression_level) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
log_message(LOG_LEVEL_DEBUG, "Data succesfully compressed");
|
||||
log_message(LOG_LEVEL_DEBUG, "Data succesfully compressed from %zu to %zu",
|
||||
data_to_compress->size, compressed_data->size);
|
||||
return compressed_data;
|
||||
}
|
||||
|
||||
Data *decompress_data(Data *compressed_data) {
|
||||
Data *data_decompress(Data *compressed_data) {
|
||||
log_message(LOG_LEVEL_DEBUG, "Start to decompress data");
|
||||
Data *uncompressed_data = data_create_empty(
|
||||
ZSTD_getFrameContentSize(compressed_data->data, compressed_data->size));
|
||||
|
||||
+2
-2
@@ -11,7 +11,7 @@ typedef struct {
|
||||
Data *data_create_empty(size_t data_size);
|
||||
Data *data_create(void *data, size_t data_size);
|
||||
void data_destroy(Data *data);
|
||||
Data *compress_data(Data *data_to_compress, int compression_level);
|
||||
Data *decompress_data(Data *compressed_data);
|
||||
Data *data_compress(Data *data_to_compress, int compression_level);
|
||||
Data *data_decompress(Data *compressed_data);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <dirent.h>
|
||||
#include <libgen.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zstd.h>
|
||||
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "socket.h"
|
||||
|
||||
File *file_create(const char *path, struct stat *stats) {
|
||||
File *file = (File *)malloc(sizeof(File));
|
||||
if (file == NULL) {
|
||||
perror("FATAL ERROR: Could not allocate memory for file struct");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
file->stats = *stats;
|
||||
|
||||
int path_len = strlen(path);
|
||||
file->path = (char *)malloc(path_len + 1);
|
||||
if (file->path == NULL) {
|
||||
perror("FATAL ERROR: Could not allocate memory for path file string");
|
||||
free(file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
strcpy(file->path, path);
|
||||
file->data = NULL;
|
||||
return file;
|
||||
}
|
||||
|
||||
void file_destroy(void *item) {
|
||||
if (item == NULL)
|
||||
return;
|
||||
File *file = (File *)item;
|
||||
free(file->data);
|
||||
file->data = NULL;
|
||||
free(file->path);
|
||||
file->path = NULL;
|
||||
free(file);
|
||||
}
|
||||
|
||||
void file_load_data(File *file) {
|
||||
if (file == NULL)
|
||||
return;
|
||||
file->data = data_create_empty(file->stats.st_size);
|
||||
printf("%ld is file big", file->data->size);
|
||||
size_t bytes_read = file_content_to_buffer(file);
|
||||
if (bytes_read != file->data->size) {
|
||||
log_message(STATUS_ERROR, "Didnt read expected amount of bytes from file");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void file_print(void *item) {
|
||||
if (item == NULL)
|
||||
return;
|
||||
printf("%s\n", ((File *)item)->path);
|
||||
}
|
||||
|
||||
void file_send_single_calls(File *file, int file_descriptor) {
|
||||
send_str(file_descriptor, file->path);
|
||||
printf("Sending File: %ld", file->data->size);
|
||||
send_data(file_descriptor, file->data->data, file->data->size);
|
||||
}
|
||||
|
||||
size_t file_content_to_buffer(File *file) {
|
||||
FILE *file_pointer = fopen(file->path, "rb");
|
||||
if (file_pointer == NULL) {
|
||||
perror("Could not open the file!");
|
||||
return 0;
|
||||
}
|
||||
size_t bytes_read =
|
||||
fread(file->data->data, 1, file->stats.st_size, file_pointer);
|
||||
if (bytes_read != (size_t)file->stats.st_size) {
|
||||
perror("Read to many or to less bytes from File!");
|
||||
return 0;
|
||||
}
|
||||
fclose(file_pointer);
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
FileReceive *file_receive_create(char *path, Data *data) {
|
||||
FileReceive *file = malloc(sizeof(FileReceive));
|
||||
file->path = path;
|
||||
file->data = data;
|
||||
return file;
|
||||
}
|
||||
|
||||
void file_receive_destroy(void *file_receive) {
|
||||
if (file_receive == NULL)
|
||||
return;
|
||||
FileReceive *file = (FileReceive *)file_receive;
|
||||
data_destroy(file->data);
|
||||
free(file->path);
|
||||
free(file);
|
||||
}
|
||||
|
||||
FileReceive *file_receive_from_buffer(void *buffer) {}
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include "data.h"
|
||||
#include <sys/stat.h>
|
||||
|
||||
typedef struct {
|
||||
char *path;
|
||||
struct stat stats;
|
||||
Data *data;
|
||||
} File;
|
||||
|
||||
typedef struct {
|
||||
char *path;
|
||||
Data *data;
|
||||
} FileReceive;
|
||||
|
||||
File *file_create(const char *path, struct stat *stats);
|
||||
void file_destroy(void *item);
|
||||
void file_load_data(File *file);
|
||||
void file_print(void *item);
|
||||
void file_send_single_calls(File *file, int file_descriptor);
|
||||
size_t file_content_to_buffer(File *file);
|
||||
Data *file_compress(File *file);
|
||||
|
||||
FileReceive *file_receive_create(char *path, Data *data);
|
||||
void file_receive_destroy(void *file_receive);
|
||||
FileReceive *file_receive_from_buffer(void *buffer);
|
||||
FileReceive *file_receive_decompress(void *FileReceive);
|
||||
|
||||
#endif
|
||||
+10
-9
@@ -110,18 +110,19 @@ void client_delete(Client *client) {
|
||||
|
||||
void send_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||
log_message(LOG_LEVEL_DEBUG, " Sending n Data: %d", data_size);
|
||||
size_t total_bytes_send = 0;
|
||||
ssize_t total_bytes_send = 0;
|
||||
while (total_bytes_send < data_size) {
|
||||
long long bytes_send =
|
||||
send(file_descriptor, (char *)data + total_bytes_send,
|
||||
printf("Trying: %zu\n", data_size - total_bytes_send);
|
||||
ssize_t bytes_send = send(file_descriptor, (char *)data + total_bytes_send,
|
||||
data_size - total_bytes_send, 0);
|
||||
if (bytes_send == 0) {
|
||||
printf("Bytes send: %zd\n", bytes_send);
|
||||
if (bytes_send <= 0) {
|
||||
perror("Could not send data!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
total_bytes_send += bytes_send;
|
||||
}
|
||||
log_message(LOG_LEVEL_DEBUG, " Send n Data: %d", total_bytes_send);
|
||||
log_message(LOG_LEVEL_DEBUG, " Send n Data: %zu", total_bytes_send);
|
||||
}
|
||||
|
||||
void receive_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||
@@ -186,13 +187,13 @@ int receive_int(int file_descriptor) {
|
||||
|
||||
const char *status_to_string(Status status) {
|
||||
switch (status) {
|
||||
case OK:
|
||||
case STATUS_OK:
|
||||
return "OK";
|
||||
case ERROR:
|
||||
case STATUS_ERROR:
|
||||
return "ERROR";
|
||||
case FINISHED:
|
||||
case STATUS_FINISHED:
|
||||
return "FINISHED";
|
||||
case NEXT:
|
||||
case STATUS_NEXT:
|
||||
return "NEXT";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
#include <netinet/in.h>
|
||||
|
||||
typedef int Status;
|
||||
enum NET_STATUS { OK, ERROR, FINISHED, NEXT };
|
||||
enum NET_STATUS { STATUS_OK, STATUS_ERROR, STATUS_FINISHED, STATUS_NEXT };
|
||||
|
||||
typedef struct Server {
|
||||
struct sockaddr_in address;
|
||||
|
||||
+6
-1
@@ -10,9 +10,14 @@ void mkdir_r(char *path) {
|
||||
strcpy(path_duplicate, path);
|
||||
char *path_current = (char *)malloc((strlen(path) + 2) * sizeof(char));
|
||||
char *path_current_position = path_current;
|
||||
if (path[0] == '/') {
|
||||
strcpy(path_current, "/");
|
||||
path_current_position += 1;
|
||||
} else {
|
||||
path_current[0] = '\0';
|
||||
}
|
||||
const char *delimiter = "/";
|
||||
char *part = strtok(path_duplicate, delimiter);
|
||||
// struct stat st;
|
||||
while (part != NULL) {
|
||||
strcpy(path_current_position, part);
|
||||
path_current_position += strlen(part) * sizeof(char);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
@@ -35,15 +36,18 @@ CLIENT_CMD_PREFIX = [
|
||||
TEST_CASES = [
|
||||
{"name": "Standard (Single-threaded)", "flags": []},
|
||||
{"name": "Multithreading (-m)", "flags": ["-m"]},
|
||||
{"name": "Compression (-c)", "flags": ["-c"]},
|
||||
{"name": "Chunk Serialization (-s)", "flags": ["-s"]},
|
||||
{"name": "Compression (-c -5)", "flags": ["-c 20"]},
|
||||
{"name": "Compression (-c 0)", "flags": ["-c 0"]},
|
||||
{"name": "Compression (-c 10)", "flags": ["-c 10"]},
|
||||
{"name": "Compression (-c 20)", "flags": ["-c 20"]},
|
||||
# {"name": "Chunk Serialization (-s)", "flags": ["-s"]},
|
||||
{"name": "Multithreading + Compression (-m -c)", "flags": ["-m", "-c"]},
|
||||
{"name": "Multithreading + Chunk Serialization (-m -s)", "flags": ["-m", "-s"]},
|
||||
{"name": "Compression + Chunk Serialization (-c -s)", "flags": ["-c", "-s"]},
|
||||
{
|
||||
"name": "Multithreading + Compression + Chunk Serialization (-m -c -s)",
|
||||
"flags": ["-m", "-c", "-s"],
|
||||
},
|
||||
# {"name": "Multithreading + Chunk Serialization (-m -s)", "flags": ["-m", "-s"]},
|
||||
# {"name": "Compression + Chunk Serialization (-c -s)", "flags": ["-c", "-s"]},
|
||||
# {
|
||||
# "name": "Multithreading + Compression + Chunk Serialization (-m -c -s)",
|
||||
# "flags": ["-m", "-c", "-s"],
|
||||
# },
|
||||
]
|
||||
|
||||
|
||||
@@ -179,6 +183,9 @@ def run_suite(env_name, apply_limits):
|
||||
return results
|
||||
|
||||
|
||||
os.system("cmake -B build -S .")
|
||||
os.system("cd build && make")
|
||||
|
||||
# --- Main Execution ---
|
||||
all_results = []
|
||||
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
#include "test_chunk.h"
|
||||
|
||||
#include "chunk.h"
|
||||
#include "test_utils.h"
|
||||
#include "utils.h"
|
||||
@@ -44,8 +44,8 @@ static void test_file_receive_operations() {
|
||||
FileReceive *fr = file_receive_create(path, df);
|
||||
EXPECT_NOT_NULL(fr);
|
||||
EXPECT_EQ_STR(fr->path, "temp_receive.txt");
|
||||
EXPECT_NOT_NULL(fr->data_fragment);
|
||||
EXPECT_EQ_STR(fr->data_fragment->data, "receive data content");
|
||||
EXPECT_NOT_NULL(fr->data);
|
||||
EXPECT_EQ_STR(fr->data->data, "receive data content");
|
||||
|
||||
file_receive_destroy(fr);
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ for file in path.glob("**/*.h"):
|
||||
for file in path.glob("**/*.c"):
|
||||
text += "--- " + str(file) + " ---\n\n"
|
||||
text += file.read_text()
|
||||
for file in [Path("Makefile")]:
|
||||
for file in [Path("CMakeLists.txt")]:
|
||||
text += "--- " + str(file) + " ---\n\n"
|
||||
text += file.read_text()
|
||||
Path("all.txt").write_text(text)
|
||||
|
||||
Reference in New Issue
Block a user