fix: bring project back to a working state
This commit is contained in:
+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
|
||||
+11
-10
@@ -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,
|
||||
data_size - total_bytes_send, 0);
|
||||
if (bytes_send == 0) {
|
||||
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);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user