fix: bring project back to a working state

This commit is contained in:
Theo Tappe
2026-07-04 17:55:03 +02:00
parent 75d5f91c6a
commit 4d6171d6bf
17 changed files with 315 additions and 226 deletions
+103
View File
@@ -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) {}