Phase 1+2: bug fixes and dead code cleanup
Phase 1 — Bugs: - receive_data: fix unsigned long long vs size_t mismatch - chunk_deserialize: add NULL check in multiprocessing.c and server.c - file_content_to_buffer: add missing fclose on error path - send_files_multithreaded: propagate sender thread result - server: add SIGPIPE handler - to_disk: check fwrite return value - scanner: use S_ISDIR instead of !S_ISREG - scanner: free cur_path before early return Phase 2 — Cleanup: - Remove unused chunk_decompress function - Remove unused array_list_clear function - Remove unused num_connections config field - Remove duplicate FILE_METADATA_WIRE_SIZE macro - Update tests for removed APIs
This commit is contained in:
@@ -46,7 +46,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
Config *config = config_create(str_dup("1.0.0"), NULL, NULL,
|
||||
save_to_disk, false, false, false, false, 5, 20, false);
|
||||
save_to_disk, false, false, false, false, 5, false);
|
||||
|
||||
int positional_args[2];
|
||||
int positional_count = 0;
|
||||
|
||||
@@ -171,10 +171,11 @@ int send_files_multithreaded(Config *config) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sender_result;
|
||||
thrd_join(scanner, NULL);
|
||||
thrd_join(loader, NULL);
|
||||
thrd_join(sender, NULL);
|
||||
thrd_join(sender, &sender_result);
|
||||
|
||||
pipeline_context_sender_destroy(context);
|
||||
return 0;
|
||||
return sender_result == thrd_success ? 0 : -1;
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ Chunk *directory_scanner_next(DirectoryScanner *scanner) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!S_ISREG(stats.st_mode)) {
|
||||
if (S_ISDIR(stats.st_mode)) {
|
||||
queue_enqueue(scanner->directories, (void *)cur_path);
|
||||
} else {
|
||||
File *file = file_create(cur_path);
|
||||
@@ -99,8 +99,10 @@ Chunk *directory_scanner_next(DirectoryScanner *scanner) {
|
||||
file->metadata = file_metadata_create(&stats);
|
||||
array_list_add(chunk_data, file);
|
||||
chunk_data_size += file->data->size;
|
||||
if (chunk_data_size > DESIRED_CHUNK_SIZE)
|
||||
if (chunk_data_size > DESIRED_CHUNK_SIZE) {
|
||||
free(cur_path);
|
||||
return chunk_data_to_chunk(chunk_data);
|
||||
}
|
||||
free(cur_path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "transport_tcp.h"
|
||||
#include "unistd.h"
|
||||
#include "utils.h"
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -27,6 +28,11 @@ int receive_files(Config *config, int file_descriptor) {
|
||||
}
|
||||
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
||||
data_destroy(data_to_process);
|
||||
if (chunk == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk, skipping");
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
if (config->save_to_disk) {
|
||||
@@ -79,6 +85,7 @@ void handler(int file_descriptor) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--stdio") == 0) {
|
||||
io_set_fds(STDIN_FILENO, STDOUT_FILENO);
|
||||
|
||||
@@ -35,14 +35,6 @@ void array_list_delete(ArrayList *array_list) {
|
||||
free(array_list);
|
||||
}
|
||||
|
||||
void array_list_clear(ArrayList *array_list) {
|
||||
if (array_list == NULL)
|
||||
return;
|
||||
for (int i = 0; i < array_list->size; i++)
|
||||
array_list->items[i] = NULL;
|
||||
array_list->size = 0;
|
||||
}
|
||||
|
||||
void array_list_extend(ArrayList *array_list) {
|
||||
if (array_list == NULL)
|
||||
return;
|
||||
|
||||
@@ -12,7 +12,6 @@ typedef struct ArrayList {
|
||||
|
||||
ArrayList *array_list_create(void (*item_destroyer)(void *item));
|
||||
void array_list_delete(ArrayList *array_list);
|
||||
void array_list_clear(ArrayList *array_list);
|
||||
void array_list_extend(ArrayList *array_list);
|
||||
void array_list_add(ArrayList *array_list, void *item);
|
||||
void **array_list_to_array(ArrayList *array_list);
|
||||
|
||||
@@ -178,24 +178,5 @@ Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata) {
|
||||
return compressed;
|
||||
}
|
||||
|
||||
Chunk *chunk_decompress(Data *compressed_data, bool use_metadata) {
|
||||
log_message(LOG_LEVEL_DEBUG, "Starting to decompress chunk");
|
||||
Data *uncompressed_data = data_decompress(compressed_data);
|
||||
if (uncompressed_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk data");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Chunk *chunk = chunk_deserialize(uncompressed_data, use_metadata);
|
||||
if (chunk == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk data");
|
||||
data_destroy(uncompressed_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data_destroy(uncompressed_data);
|
||||
log_message(LOG_LEVEL_DEBUG, "Chunk successfully decompressed");
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,5 @@ void chunk_destroy(void *chunk);
|
||||
Data *chunk_serialize(Chunk *chunk, bool use_metadata);
|
||||
Chunk *chunk_deserialize(Data *data, bool use_metadata);
|
||||
Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata);
|
||||
Chunk *chunk_decompress(Data *compressed_data, bool use_metadata);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+1
-4
@@ -10,7 +10,7 @@ Config *config_create(char *version, char *send_directory,
|
||||
char *receive_directory, bool save_to_disk,
|
||||
bool use_multithreading, bool use_chunk_serialization,
|
||||
bool use_compression, bool use_metadata,
|
||||
int compression_level, int num_connections, bool use_sendfile) {
|
||||
int compression_level, bool use_sendfile) {
|
||||
|
||||
Config *config = malloc(sizeof(Config));
|
||||
config->version = version;
|
||||
@@ -22,7 +22,6 @@ Config *config_create(char *version, char *send_directory,
|
||||
config->use_compression = use_compression;
|
||||
config->use_metadata = use_metadata;
|
||||
config->compression_level = compression_level;
|
||||
config->num_connections = num_connections;
|
||||
config->use_sendfile = use_sendfile;
|
||||
config->transport = TRANSPORT_TCP;
|
||||
config->ssh_destination = NULL;
|
||||
@@ -68,7 +67,6 @@ void config_send(int file_descriptor, Config *config) {
|
||||
send_int(file_descriptor, config->use_compression);
|
||||
send_int(file_descriptor, config->use_metadata);
|
||||
send_int(file_descriptor, config->compression_level);
|
||||
send_int(file_descriptor, config->num_connections);
|
||||
send_int(file_descriptor, config->use_sendfile);
|
||||
if (receive_status(file_descriptor) != STATUS_OK) {
|
||||
perror("Error transmitting config!");
|
||||
@@ -87,7 +85,6 @@ Config *config_receive(int file_descriptor) {
|
||||
config->use_compression = receive_int(file_descriptor);
|
||||
config->use_metadata = receive_int(file_descriptor);
|
||||
config->compression_level = receive_int(file_descriptor);
|
||||
config->num_connections = receive_int(file_descriptor);
|
||||
config->use_sendfile = receive_int(file_descriptor);
|
||||
config->transport = TRANSPORT_TCP;
|
||||
config->ssh_destination = NULL;
|
||||
|
||||
+1
-2
@@ -19,7 +19,6 @@ typedef struct Config {
|
||||
bool use_sendfile;
|
||||
bool use_metadata;
|
||||
int compression_level;
|
||||
int num_connections;
|
||||
TransportType transport;
|
||||
char *ssh_destination;
|
||||
} Config;
|
||||
@@ -28,7 +27,7 @@ Config *config_create(char *version, char *send_directory,
|
||||
char *receive_directory, bool save_to_disk,
|
||||
bool use_multithreading, bool use_chunk_serialization,
|
||||
bool use_compression, bool use_metadata,
|
||||
int compression_level, int num_connections, bool use_sendfile);
|
||||
int compression_level, bool use_sendfile);
|
||||
void config_delete(Config *config);
|
||||
void config_send(int file_descriptor, Config *config);
|
||||
Config *config_receive(int file_descriptor);
|
||||
|
||||
+7
-2
@@ -113,7 +113,11 @@ void to_disk(const char *path, const void *data, unsigned long long data_size) {
|
||||
perror("Could not open File");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fwrite(data, 1, data_size, file_pointer);
|
||||
if (fwrite(data, 1, data_size, file_pointer) != data_size) {
|
||||
perror("Failed to write all data to disk");
|
||||
fclose(file_pointer);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose(file_pointer);
|
||||
free(dir_to_free);
|
||||
}
|
||||
@@ -171,7 +175,8 @@ size_t file_content_to_buffer(File *file) {
|
||||
size_t bytes_read =
|
||||
fread(file->data->data, 1, file->data->size, file_pointer);
|
||||
if (bytes_read != (size_t)file->data->size) {
|
||||
perror("Read to many or to less bytes from File!");
|
||||
fclose(file_pointer);
|
||||
perror("Read unexpected number of bytes from File!");
|
||||
return 0;
|
||||
}
|
||||
fclose(file_pointer);
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FILE_METADATA_WIRE_SIZE (sizeof(mode_t) + sizeof(uid_t) + sizeof(gid_t) + sizeof(time_t) + sizeof(long))
|
||||
|
||||
void metadata_to_buf(char **buf, FileMetadata *m) {
|
||||
int present = (m != NULL) ? 1 : 0;
|
||||
memcpy(*buf, &present, sizeof(int));
|
||||
|
||||
@@ -84,6 +84,10 @@ static void receive_chunk_enqueue(int file_descriptor,
|
||||
}
|
||||
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
||||
data_destroy(data_to_process);
|
||||
if (chunk == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk, skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
File *file = chunk->items[i];
|
||||
|
||||
@@ -91,12 +91,12 @@ void send_data(int file_descriptor, Data *data) {
|
||||
}
|
||||
|
||||
Data *receive_data(int file_descriptor) {
|
||||
size_t size = 0;
|
||||
unsigned long long size = 0;
|
||||
receive_n_data(file_descriptor, &size, sizeof(unsigned long long));
|
||||
void *data = malloc(size);
|
||||
receive_n_data(file_descriptor, data, size);
|
||||
void *data = malloc((size_t)size);
|
||||
receive_n_data(file_descriptor, data, (size_t)size);
|
||||
log_message(LOG_LEVEL_DEBUG, "Received %lld data", size);
|
||||
return data_create(data, size);
|
||||
return data_create(data, (size_t)size);
|
||||
}
|
||||
|
||||
void send_int(int file_descriptor, int data) {
|
||||
|
||||
Reference in New Issue
Block a user