Merge pull request 'Fix exit() in library code, add --include/--max-size/--min-size' (#12) from exit-and-select into main
Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
@@ -28,6 +28,9 @@ static void print_usage(void) {
|
|||||||
printf(" --progress Show transfer progress\n");
|
printf(" --progress Show transfer progress\n");
|
||||||
printf(" --delete Delete files on receiver not in source\n");
|
printf(" --delete Delete files on receiver not in source\n");
|
||||||
printf(" --exclude <pattern> Exclude files matching pattern\n");
|
printf(" --exclude <pattern> Exclude files matching pattern\n");
|
||||||
|
printf(" --include <pattern> Only include files matching pattern\n");
|
||||||
|
printf(" --max-size <n> Skip files larger than n bytes\n");
|
||||||
|
printf(" --min-size <n> Skip files smaller than n bytes\n");
|
||||||
printf(" -m Enable multithreading\n");
|
printf(" -m Enable multithreading\n");
|
||||||
printf(" -s Enable chunk serialization\n");
|
printf(" -s Enable chunk serialization\n");
|
||||||
printf(" -f Enable sendfile (TCP only, not with -c or -s)\n");
|
printf(" -f Enable sendfile (TCP only, not with -c or -s)\n");
|
||||||
@@ -78,6 +81,14 @@ int main(int argc, char *argv[]) {
|
|||||||
int idx = config->exclude_count++;
|
int idx = config->exclude_count++;
|
||||||
config->exclude_patterns = realloc(config->exclude_patterns, config->exclude_count * sizeof(char *));
|
config->exclude_patterns = realloc(config->exclude_patterns, config->exclude_count * sizeof(char *));
|
||||||
config->exclude_patterns[idx] = str_dup(argv[++i]);
|
config->exclude_patterns[idx] = str_dup(argv[++i]);
|
||||||
|
} else if (strcmp(argv[i], "--include") == 0 && i + 1 < argc) {
|
||||||
|
int idx = config->include_count++;
|
||||||
|
config->include_patterns = realloc(config->include_patterns, config->include_count * sizeof(char *));
|
||||||
|
config->include_patterns[idx] = str_dup(argv[++i]);
|
||||||
|
} else if (strcmp(argv[i], "--max-size") == 0 && i + 1 < argc) {
|
||||||
|
config->max_size = strtoull(argv[++i], NULL, 10);
|
||||||
|
} else if (strcmp(argv[i], "--min-size") == 0 && i + 1 < argc) {
|
||||||
|
config->min_size = strtoull(argv[++i], NULL, 10);
|
||||||
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "-z") == 0) {
|
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "-z") == 0) {
|
||||||
config->use_compression = true;
|
config->use_compression = true;
|
||||||
log_message(LOG_LEVEL_INFO, "Enabled Compression");
|
log_message(LOG_LEVEL_INFO, "Enabled Compression");
|
||||||
|
|||||||
+83
-26
@@ -20,26 +20,29 @@
|
|||||||
|
|
||||||
int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
||||||
if (config->use_chunk_serialization) {
|
if (config->use_chunk_serialization) {
|
||||||
send_status(client->file_descriptor, STATUS_CHUNK);
|
if (!send_status(client->file_descriptor, STATUS_CHUNK)) return -1;
|
||||||
Data *data;
|
Data *data;
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
data = chunk_compress(chunk, config->compression_level, config->use_metadata);
|
data = chunk_compress(chunk, config->compression_level, config->use_metadata);
|
||||||
} else {
|
} else {
|
||||||
data = chunk_serialize(chunk, config->use_metadata);
|
data = chunk_serialize(chunk, config->use_metadata);
|
||||||
}
|
}
|
||||||
send_data(client->file_descriptor, data);
|
if (data == NULL) return -1;
|
||||||
|
if (!send_data(client->file_descriptor, data)) { data_destroy(data); return -1; }
|
||||||
data_destroy(data);
|
data_destroy(data);
|
||||||
} else if (config->use_sendfile && !config->use_compression) {
|
} else if (config->use_sendfile && !config->use_compression) {
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
send_status(client->file_descriptor, STATUS_NEXT);
|
if (!send_status(client->file_descriptor, STATUS_NEXT)) return -1;
|
||||||
file_send_sendfile(chunk->items[i], client->file_descriptor, config->use_metadata);
|
if (!file_send_sendfile(chunk->items[i], client->file_descriptor, config->use_metadata))
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
send_status(client->file_descriptor, STATUS_NEXT);
|
if (!send_status(client->file_descriptor, STATUS_NEXT)) return -1;
|
||||||
file_send_single_calls(chunk->items[i], client->file_descriptor,
|
if (!file_send_single_calls(chunk->items[i], client->file_descriptor,
|
||||||
config->use_metadata,
|
config->use_metadata,
|
||||||
config->use_compression ? config->compression_level : 0);
|
config->use_compression ? config->compression_level : 0))
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -56,9 +59,17 @@ static int send_chunks_multithreaded(void *pipeline_context) {
|
|||||||
client = client_connect_ssh(context->config->ssh_destination, context->config->ssh_port);
|
client = client_connect_ssh(context->config->ssh_destination, context->config->ssh_port);
|
||||||
} else {
|
} else {
|
||||||
client = client_create();
|
client = client_create();
|
||||||
client_connect(client, server_host, server_port);
|
if (!client || !client_connect(client, server_host, server_port)) {
|
||||||
|
if (client) client_delete(client);
|
||||||
|
fprintf(stderr, "Error: could not connect to server\n");
|
||||||
|
return thrd_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!config_send(client->file_descriptor, context->config)) {
|
||||||
|
client_disconnect(client);
|
||||||
|
client_delete(client);
|
||||||
|
return thrd_error;
|
||||||
}
|
}
|
||||||
config_send(client->file_descriptor, context->config);
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Chunk *current_chunk = queue_dequeue_multithreaded(
|
Chunk *current_chunk = queue_dequeue_multithreaded(
|
||||||
@@ -74,14 +85,17 @@ static int send_chunks_multithreaded(void *pipeline_context) {
|
|||||||
(char *)context->manifest->items[i]);
|
(char *)context->manifest->items[i]);
|
||||||
}
|
}
|
||||||
send_status(client->file_descriptor, STATUS_FINISHED);
|
send_status(client->file_descriptor, STATUS_FINISHED);
|
||||||
int ok = receive_status(client->file_descriptor) == STATUS_OK;
|
Status s;
|
||||||
|
int ok = receive_status(client->file_descriptor, &s) && s == STATUS_OK;
|
||||||
client_disconnect(client);
|
client_disconnect(client);
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
return ok ? thrd_success : thrd_error;
|
return ok ? thrd_success : thrd_error;
|
||||||
}
|
}
|
||||||
if (send_chunk(client, current_chunk, context->config) != 0) {
|
if (send_chunk(client, current_chunk, context->config) != 0) {
|
||||||
perror("Something unexpected happend while sending the chunk");
|
fprintf(stderr, "Error: unexpected error while sending chunk\n");
|
||||||
exit(EXIT_FAILURE);
|
client_disconnect(client);
|
||||||
|
client_delete(client);
|
||||||
|
return thrd_error;
|
||||||
}
|
}
|
||||||
chunk_destroy(current_chunk);
|
chunk_destroy(current_chunk);
|
||||||
}
|
}
|
||||||
@@ -93,7 +107,9 @@ static int scan_directory_multithreaded(void *pipeline_context) {
|
|||||||
DirectoryScanner *scanner = directory_scanner_create(
|
DirectoryScanner *scanner = directory_scanner_create(
|
||||||
context->config->send_directory, context->config->use_metadata,
|
context->config->send_directory, context->config->use_metadata,
|
||||||
context->config->chunk_size, context->config->exclude_patterns,
|
context->config->chunk_size, context->config->exclude_patterns,
|
||||||
context->config->exclude_count);
|
context->config->exclude_count, context->config->include_patterns,
|
||||||
|
context->config->include_count, context->config->max_size,
|
||||||
|
context->config->min_size);
|
||||||
mtx_unlock(&context->mutex_scanner);
|
mtx_unlock(&context->mutex_scanner);
|
||||||
|
|
||||||
Chunk *current_chunk;
|
Chunk *current_chunk;
|
||||||
@@ -136,8 +152,13 @@ static int load_files_multithreaded(void *pipeline_context) {
|
|||||||
return thrd_success;
|
return thrd_success;
|
||||||
}
|
}
|
||||||
if (!context->config->use_sendfile) {
|
if (!context->config->use_sendfile) {
|
||||||
for (int i = 0; i < chunk->element_count; i++)
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
file_load_data(chunk->items[i]);
|
if (!file_load_data(chunk->items[i])) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to load file data, skipping");
|
||||||
|
file_destroy(chunk->items[i]);
|
||||||
|
chunk->items[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
queue_enqueue_multithreaded(context->queue_loader, chunk,
|
queue_enqueue_multithreaded(context->queue_loader, chunk,
|
||||||
&context->mutex_loader,
|
&context->mutex_loader,
|
||||||
@@ -150,7 +171,9 @@ int send_files(Config *config) {
|
|||||||
if (config->dry_run) {
|
if (config->dry_run) {
|
||||||
DirectoryScanner *scanner = directory_scanner_create(
|
DirectoryScanner *scanner = directory_scanner_create(
|
||||||
config->send_directory, config->use_metadata, config->chunk_size,
|
config->send_directory, config->use_metadata, config->chunk_size,
|
||||||
config->exclude_patterns, config->exclude_count);
|
config->exclude_patterns, config->exclude_count,
|
||||||
|
config->include_patterns, config->include_count,
|
||||||
|
config->max_size, config->min_size);
|
||||||
Chunk *chunk;
|
Chunk *chunk;
|
||||||
int file_count = 0;
|
int file_count = 0;
|
||||||
unsigned long long total_bytes = 0;
|
unsigned long long total_bytes = 0;
|
||||||
@@ -177,14 +200,25 @@ int send_files(Config *config) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
client = client_connect_ssh(config->ssh_destination, config->ssh_port);
|
client = client_connect_ssh(config->ssh_destination, config->ssh_port);
|
||||||
|
if (!client) return 1;
|
||||||
} else {
|
} else {
|
||||||
client = client_create();
|
client = client_create();
|
||||||
client_connect(client, server_host, server_port);
|
if (!client || !client_connect(client, server_host, server_port)) {
|
||||||
|
if (client) client_delete(client);
|
||||||
|
fprintf(stderr, "Error: could not connect to server\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!config_send(client->file_descriptor, config)) {
|
||||||
|
client_disconnect(client);
|
||||||
|
client_delete(client);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
config_send(client->file_descriptor, config);
|
|
||||||
DirectoryScanner *scanner = directory_scanner_create(
|
DirectoryScanner *scanner = directory_scanner_create(
|
||||||
config->send_directory, config->use_metadata, config->chunk_size,
|
config->send_directory, config->use_metadata, config->chunk_size,
|
||||||
config->exclude_patterns, config->exclude_count);
|
config->exclude_patterns, config->exclude_count,
|
||||||
|
config->include_patterns, config->include_count,
|
||||||
|
config->max_size, config->min_size);
|
||||||
Chunk *current_chunk;
|
Chunk *current_chunk;
|
||||||
unsigned long long total_bytes = 0;
|
unsigned long long total_bytes = 0;
|
||||||
time_t last_progress = 0;
|
time_t last_progress = 0;
|
||||||
@@ -201,10 +235,18 @@ int send_files(Config *config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!config->use_sendfile) {
|
if (!config->use_sendfile) {
|
||||||
for (int i = 0; i < current_chunk->element_count; i++)
|
for (int i = 0; i < current_chunk->element_count; i++) {
|
||||||
file_load_data(current_chunk->items[i]);
|
if (!file_load_data(current_chunk->items[i])) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to load file data");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (send_chunk(client, current_chunk, config) != 0) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to send chunk");
|
||||||
|
chunk_destroy(current_chunk);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
send_chunk(client, current_chunk, config);
|
|
||||||
if (config->show_progress) {
|
if (config->show_progress) {
|
||||||
total_bytes += chunk_bytes;
|
total_bytes += chunk_bytes;
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
@@ -226,7 +268,8 @@ int send_files(Config *config) {
|
|||||||
array_list_delete(manifest);
|
array_list_delete(manifest);
|
||||||
}
|
}
|
||||||
send_status(client->file_descriptor, STATUS_FINISHED);
|
send_status(client->file_descriptor, STATUS_FINISHED);
|
||||||
int ok = receive_status(client->file_descriptor) == STATUS_OK;
|
Status s;
|
||||||
|
int ok = receive_status(client->file_descriptor, &s) && s == STATUS_OK;
|
||||||
if (config->show_progress) {
|
if (config->show_progress) {
|
||||||
double elapsed = difftime(time(NULL), start);
|
double elapsed = difftime(time(NULL), start);
|
||||||
double rate = elapsed > 0 ? total_bytes / (1048576.0 * elapsed) : 0;
|
double rate = elapsed > 0 ? total_bytes / (1048576.0 * elapsed) : 0;
|
||||||
@@ -242,7 +285,9 @@ int send_files_multithreaded(Config *config) {
|
|||||||
if (config->dry_run) {
|
if (config->dry_run) {
|
||||||
DirectoryScanner *scanner = directory_scanner_create(
|
DirectoryScanner *scanner = directory_scanner_create(
|
||||||
config->send_directory, config->use_metadata, config->chunk_size,
|
config->send_directory, config->use_metadata, config->chunk_size,
|
||||||
config->exclude_patterns, config->exclude_count);
|
config->exclude_patterns, config->exclude_count,
|
||||||
|
config->include_patterns, config->include_count,
|
||||||
|
config->max_size, config->min_size);
|
||||||
Chunk *chunk;
|
Chunk *chunk;
|
||||||
int file_count = 0;
|
int file_count = 0;
|
||||||
unsigned long long total_bytes = 0;
|
unsigned long long total_bytes = 0;
|
||||||
@@ -262,9 +307,20 @@ int send_files_multithreaded(Config *config) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Queue *q1 = queue_create(100, chunk_destroy);
|
||||||
|
Queue *q2 = queue_create(100, chunk_destroy);
|
||||||
|
if (!q1 || !q2) {
|
||||||
|
if (q1) queue_destroy(q1);
|
||||||
|
if (q2) queue_destroy(q2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
PipelineContextSender *context =
|
PipelineContextSender *context =
|
||||||
pipeline_context_sender_create(config, queue_create(100, chunk_destroy),
|
pipeline_context_sender_create(config, q1, q2);
|
||||||
queue_create(100, chunk_destroy));
|
if (!context) {
|
||||||
|
queue_destroy(q1);
|
||||||
|
queue_destroy(q2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
if (config->use_delete)
|
if (config->use_delete)
|
||||||
context->manifest = array_list_create(free);
|
context->manifest = array_list_create(free);
|
||||||
|
|
||||||
@@ -275,6 +331,7 @@ int send_files_multithreaded(Config *config) {
|
|||||||
thrd_create(&sender, send_chunks_multithreaded, context) !=
|
thrd_create(&sender, send_chunks_multithreaded, context) !=
|
||||||
thrd_success) {
|
thrd_success) {
|
||||||
perror("Error creating threads.\n");
|
perror("Error creating threads.\n");
|
||||||
|
pipeline_context_sender_destroy(context);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+34
-3
@@ -11,7 +11,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
DirectoryScanner *directory_scanner_create(char *root_directory, bool use_metadata, unsigned long long chunk_size, char **exclude_patterns, int exclude_count) {
|
DirectoryScanner *directory_scanner_create(char *root_directory, bool use_metadata, unsigned long long chunk_size, char **exclude_patterns, int exclude_count, char **include_patterns, int include_count, unsigned long long max_size, unsigned long long min_size) {
|
||||||
DirectoryScanner *scanner = malloc(sizeof(DirectoryScanner));
|
DirectoryScanner *scanner = malloc(sizeof(DirectoryScanner));
|
||||||
scanner->directories = queue_create(100, free);
|
scanner->directories = queue_create(100, free);
|
||||||
scanner->current_dir = NULL;
|
scanner->current_dir = NULL;
|
||||||
@@ -20,6 +20,10 @@ DirectoryScanner *directory_scanner_create(char *root_directory, bool use_metada
|
|||||||
scanner->chunk_size = chunk_size > 0 ? chunk_size : DESIRED_CHUNK_SIZE;
|
scanner->chunk_size = chunk_size > 0 ? chunk_size : DESIRED_CHUNK_SIZE;
|
||||||
scanner->exclude_patterns = exclude_patterns;
|
scanner->exclude_patterns = exclude_patterns;
|
||||||
scanner->exclude_count = exclude_count;
|
scanner->exclude_count = exclude_count;
|
||||||
|
scanner->include_patterns = include_patterns;
|
||||||
|
scanner->include_count = include_count;
|
||||||
|
scanner->max_size = max_size;
|
||||||
|
scanner->min_size = min_size;
|
||||||
queue_enqueue(scanner->directories, str_dup(root_directory));
|
queue_enqueue(scanner->directories, str_dup(root_directory));
|
||||||
return scanner;
|
return scanner;
|
||||||
}
|
}
|
||||||
@@ -58,8 +62,10 @@ static int open_next_directory(DirectoryScanner *scanner) {
|
|||||||
scanner->current_path = (char *)queue_dequeue(scanner->directories);
|
scanner->current_path = (char *)queue_dequeue(scanner->directories);
|
||||||
scanner->current_dir = opendir(scanner->current_path);
|
scanner->current_dir = opendir(scanner->current_path);
|
||||||
if (scanner->current_dir == NULL) {
|
if (scanner->current_dir == NULL) {
|
||||||
perror("Could not open directory!");
|
perror("Could not open directory");
|
||||||
exit(EXIT_FAILURE);
|
free(scanner->current_path);
|
||||||
|
scanner->current_path = NULL;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -107,7 +113,32 @@ Chunk *directory_scanner_next(DirectoryScanner *scanner) {
|
|||||||
free(cur_path);
|
free(cur_path);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scanner->include_count > 0) {
|
||||||
|
bool included = false;
|
||||||
|
for (int i = 0; i < scanner->include_count; i++) {
|
||||||
|
if (glob_match(scanner->include_patterns[i], entry->d_name)) {
|
||||||
|
included = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!included) {
|
||||||
|
free(cur_path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((scanner->max_size > 0 && (unsigned long long)stats.st_size > scanner->max_size) ||
|
||||||
|
(scanner->min_size > 0 && (unsigned long long)stats.st_size < scanner->min_size)) {
|
||||||
|
free(cur_path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
File *file = file_create(cur_path);
|
File *file = file_create(cur_path);
|
||||||
|
if (file == NULL) {
|
||||||
|
free(cur_path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
file->data->size = stats.st_size;
|
file->data->size = stats.st_size;
|
||||||
if (scanner->use_metadata)
|
if (scanner->use_metadata)
|
||||||
file->metadata = file_metadata_create(&stats);
|
file->metadata = file_metadata_create(&stats);
|
||||||
|
|||||||
@@ -14,9 +14,13 @@ typedef struct {
|
|||||||
unsigned long long chunk_size;
|
unsigned long long chunk_size;
|
||||||
char **exclude_patterns;
|
char **exclude_patterns;
|
||||||
int exclude_count;
|
int exclude_count;
|
||||||
|
char **include_patterns;
|
||||||
|
int include_count;
|
||||||
|
unsigned long long max_size;
|
||||||
|
unsigned long long min_size;
|
||||||
} DirectoryScanner;
|
} DirectoryScanner;
|
||||||
|
|
||||||
DirectoryScanner *directory_scanner_create(char *root_directory, bool use_metadata, unsigned long long chunk_size, char **exclude_patterns, int exclude_count);
|
DirectoryScanner *directory_scanner_create(char *root_directory, bool use_metadata, unsigned long long chunk_size, char **exclude_patterns, int exclude_count, char **include_patterns, int include_count, unsigned long long max_size, unsigned long long min_size);
|
||||||
Chunk *directory_scanner_next(DirectoryScanner *scanner);
|
Chunk *directory_scanner_next(DirectoryScanner *scanner);
|
||||||
void directory_scanner_destroy(DirectoryScanner *scanner);
|
void directory_scanner_destroy(DirectoryScanner *scanner);
|
||||||
|
|
||||||
|
|||||||
+70
-19
@@ -18,14 +18,25 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int receive_files(Config *config, int file_descriptor) {
|
int receive_files(Config *config, int file_descriptor) {
|
||||||
Status status = receive_status(file_descriptor);
|
Status status;
|
||||||
|
if (!receive_status(file_descriptor, &status)) return -1;
|
||||||
while (status == STATUS_NEXT || status == STATUS_CHUNK) {
|
while (status == STATUS_NEXT || status == STATUS_CHUNK) {
|
||||||
if (status == STATUS_CHUNK) {
|
if (status == STATUS_CHUNK) {
|
||||||
Data *chunk_data = receive_data(file_descriptor);
|
Data *chunk_data = receive_data(file_descriptor);
|
||||||
|
if (chunk_data == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
Data *data_to_process = chunk_data;
|
Data *data_to_process = chunk_data;
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
data_to_process = data_decompress(chunk_data);
|
data_to_process = data_decompress(chunk_data);
|
||||||
data_destroy(chunk_data);
|
data_destroy(chunk_data);
|
||||||
|
if (data_to_process == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
||||||
data_destroy(data_to_process);
|
data_destroy(data_to_process);
|
||||||
@@ -38,33 +49,50 @@ int receive_files(Config *config, int file_descriptor) {
|
|||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
if (config->save_to_disk) {
|
if (config->save_to_disk) {
|
||||||
char *disk_path = path_cat(config->receive_root_directory, chunk->items[i]->path);
|
char *disk_path = path_cat(config->receive_root_directory, chunk->items[i]->path);
|
||||||
to_disk(disk_path, chunk->items[i]->data->data, chunk->items[i]->data->size);
|
if (disk_path) {
|
||||||
file_restore_metadata(disk_path, chunk->items[i]->metadata);
|
to_disk(disk_path, chunk->items[i]->data->data, chunk->items[i]->data->size);
|
||||||
free(disk_path);
|
file_restore_metadata(disk_path, chunk->items[i]->metadata);
|
||||||
|
free(disk_path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chunk_destroy(chunk);
|
chunk_destroy(chunk);
|
||||||
} else {
|
} else {
|
||||||
File *file = file_receive(config, file_descriptor);
|
File *file = file_receive(config, file_descriptor);
|
||||||
|
if (file == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to receive file");
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
if (config->save_to_disk) {
|
if (config->save_to_disk) {
|
||||||
char *disk_path = path_cat(config->receive_root_directory, file->path);
|
char *disk_path = path_cat(config->receive_root_directory, file->path);
|
||||||
to_disk(disk_path, file->data->data, file->data->size);
|
if (disk_path) {
|
||||||
file_restore_metadata(disk_path, file->metadata);
|
to_disk(disk_path, file->data->data, file->data->size);
|
||||||
free(disk_path);
|
file_restore_metadata(disk_path, file->metadata);
|
||||||
|
free(disk_path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
}
|
}
|
||||||
status = receive_status(file_descriptor);
|
if (!receive_status(file_descriptor, &status)) {
|
||||||
|
send_status(file_descriptor, STATUS_ERROR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (status == STATUS_MANIFEST) {
|
if (status == STATUS_MANIFEST) {
|
||||||
int count = receive_int(file_descriptor);
|
int count;
|
||||||
|
if (!receive_int(file_descriptor, &count)) return -1;
|
||||||
ArrayList *manifest = array_list_create(free);
|
ArrayList *manifest = array_list_create(free);
|
||||||
for (int i = 0; i < count; i++)
|
if (manifest) {
|
||||||
array_list_add(manifest, receive_str(file_descriptor));
|
for (int i = 0; i < count; i++) {
|
||||||
fprintf(stderr, "Deleting files not in manifest...\n");
|
char *s = receive_str(file_descriptor);
|
||||||
delete_extras(config->receive_root_directory, manifest);
|
if (s) array_list_add(manifest, s);
|
||||||
array_list_delete(manifest);
|
}
|
||||||
status = receive_status(file_descriptor);
|
fprintf(stderr, "Deleting files not in manifest...\n");
|
||||||
|
delete_extras(config->receive_root_directory, manifest);
|
||||||
|
array_list_delete(manifest);
|
||||||
|
}
|
||||||
|
if (!receive_status(file_descriptor, &status)) return -1;
|
||||||
}
|
}
|
||||||
if (status != STATUS_FINISHED) {
|
if (status != STATUS_FINISHED) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
|
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
|
||||||
@@ -77,19 +105,38 @@ int receive_files(Config *config, int file_descriptor) {
|
|||||||
|
|
||||||
void handler(int file_descriptor) {
|
void handler(int file_descriptor) {
|
||||||
Config *config = config_receive(file_descriptor);
|
Config *config = config_receive(file_descriptor);
|
||||||
|
if (config == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to receive config");
|
||||||
|
close(file_descriptor);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (config->use_multithreading) {
|
if (config->use_multithreading) {
|
||||||
|
Queue *q = queue_create(100, file_destroy);
|
||||||
|
if (q == NULL) {
|
||||||
|
config_delete(config);
|
||||||
|
close(file_descriptor);
|
||||||
|
return;
|
||||||
|
}
|
||||||
PipelineContextReceiver *context = pipeline_context_receiver_create(
|
PipelineContextReceiver *context = pipeline_context_receiver_create(
|
||||||
config, queue_create(100, file_destroy), file_descriptor);
|
config, q, file_descriptor);
|
||||||
|
if (context == NULL) {
|
||||||
|
queue_destroy(q);
|
||||||
|
config_delete(config);
|
||||||
|
close(file_descriptor);
|
||||||
|
return;
|
||||||
|
}
|
||||||
thrd_t receiver, writer;
|
thrd_t receiver, writer;
|
||||||
if (thrd_create(&receiver, receive_thread, context) != thrd_success ||
|
if (thrd_create(&receiver, receive_thread, context) != thrd_success ||
|
||||||
thrd_create(&writer, write_thread, context) != thrd_success) {
|
thrd_create(&writer, write_thread, context) != thrd_success) {
|
||||||
perror("Error creating Threads!");
|
perror("Error creating Threads");
|
||||||
exit(EXIT_FAILURE);
|
pipeline_context_receiver_destroy(context);
|
||||||
|
close(file_descriptor);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
thrd_join(receiver, NULL);
|
thrd_join(receiver, NULL);
|
||||||
thrd_join(writer, NULL);
|
thrd_join(writer, NULL);
|
||||||
pipeline_context_receiver_destroy(context);
|
|
||||||
send_status(file_descriptor, STATUS_OK);
|
send_status(file_descriptor, STATUS_OK);
|
||||||
|
pipeline_context_receiver_destroy(context);
|
||||||
} else
|
} else
|
||||||
receive_files(config, file_descriptor);
|
receive_files(config, file_descriptor);
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
@@ -107,6 +154,10 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Server *server = server_create(8080);
|
Server *server = server_create(8080);
|
||||||
|
if (server == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to create server");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
server_listen(server, handler);
|
server_listen(server, handler);
|
||||||
server_delete(&server);
|
server_delete(&server);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+16
-16
@@ -6,15 +6,14 @@
|
|||||||
ArrayList *array_list_create(void (*item_destroyer)(void *item)) {
|
ArrayList *array_list_create(void (*item_destroyer)(void *item)) {
|
||||||
ArrayList *list = (ArrayList *)malloc(sizeof(ArrayList));
|
ArrayList *list = (ArrayList *)malloc(sizeof(ArrayList));
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for array list struct");
|
perror("ERROR: Could not allocate memory for array list struct");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
list->items = malloc(INITIAL_ARRAY_SIZE * sizeof(void *));
|
list->items = malloc(INITIAL_ARRAY_SIZE * sizeof(void *));
|
||||||
if (list->items == NULL) {
|
if (list->items == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for list items");
|
|
||||||
free(list);
|
free(list);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
list->size = 0;
|
list->size = 0;
|
||||||
list->capacity = INITIAL_ARRAY_SIZE;
|
list->capacity = INITIAL_ARRAY_SIZE;
|
||||||
@@ -35,29 +34,30 @@ void array_list_delete(ArrayList *array_list) {
|
|||||||
free(array_list);
|
free(array_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void array_list_extend(ArrayList *array_list) {
|
bool array_list_extend(ArrayList *array_list) {
|
||||||
if (array_list == NULL)
|
if (array_list == NULL) return false;
|
||||||
return;
|
|
||||||
int new_capacity = array_list->capacity * 2;
|
int new_capacity = array_list->capacity * 2;
|
||||||
if (new_capacity == 0)
|
if (new_capacity == 0)
|
||||||
new_capacity = INITIAL_ARRAY_SIZE;
|
new_capacity = INITIAL_ARRAY_SIZE;
|
||||||
array_list->items = realloc(array_list->items, new_capacity * sizeof(void *));
|
void *new_items = realloc(array_list->items, new_capacity * sizeof(void *));
|
||||||
if (array_list->items == NULL) {
|
if (new_items == NULL) {
|
||||||
perror("FATAL ERROR: Could not reallocate memory for array list struct");
|
perror("ERROR: Could not reallocate memory for array list items");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
array_list->items = new_items;
|
||||||
array_list->capacity = new_capacity;
|
array_list->capacity = new_capacity;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void array_list_add(ArrayList *array_list, void *item) {
|
bool array_list_add(ArrayList *array_list, void *item) {
|
||||||
if (array_list == NULL) {
|
if (array_list == NULL) return false;
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (array_list->capacity == array_list->size) {
|
if (array_list->capacity == array_list->size) {
|
||||||
array_list_extend(array_list);
|
if (!array_list_extend(array_list))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
array_list->items[array_list->size] = item;
|
array_list->items[array_list->size] = item;
|
||||||
array_list->size += 1;
|
array_list->size += 1;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void **array_list_to_array(ArrayList *array_list) {
|
void **array_list_to_array(ArrayList *array_list) {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef ARRAY_LIST_H
|
#ifndef ARRAY_LIST_H
|
||||||
#define ARRAY_LIST_H
|
#define ARRAY_LIST_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define INITIAL_ARRAY_SIZE 100
|
#define INITIAL_ARRAY_SIZE 100
|
||||||
|
|
||||||
typedef struct ArrayList {
|
typedef struct ArrayList {
|
||||||
@@ -12,8 +14,8 @@ typedef struct ArrayList {
|
|||||||
|
|
||||||
ArrayList *array_list_create(void (*item_destroyer)(void *item));
|
ArrayList *array_list_create(void (*item_destroyer)(void *item));
|
||||||
void array_list_delete(ArrayList *array_list);
|
void array_list_delete(ArrayList *array_list);
|
||||||
void array_list_extend(ArrayList *array_list);
|
bool array_list_extend(ArrayList *array_list);
|
||||||
void array_list_add(ArrayList *array_list, void *item);
|
bool array_list_add(ArrayList *array_list, void *item);
|
||||||
void **array_list_to_array(ArrayList *array_list);
|
void **array_list_to_array(ArrayList *array_list);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+6
-6
@@ -14,16 +14,14 @@
|
|||||||
Chunk *chunk_create(File **items, int element_count) {
|
Chunk *chunk_create(File **items, int element_count) {
|
||||||
Chunk *chunk = (Chunk *)malloc(sizeof(Chunk));
|
Chunk *chunk = (Chunk *)malloc(sizeof(Chunk));
|
||||||
if (chunk == NULL) {
|
if (chunk == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for chunk structure");
|
perror("ERROR: Could not allocate memory for chunk structure");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk->items = (File **)malloc(element_count * sizeof(File *));
|
chunk->items = (File **)malloc(element_count * sizeof(File *));
|
||||||
if (chunk->items == NULL) {
|
if (chunk->items == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for items of chunk "
|
|
||||||
"structure");
|
|
||||||
free(chunk);
|
free(chunk);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < element_count; i++) {
|
for (int i = 0; i < element_count; i++) {
|
||||||
@@ -62,7 +60,7 @@ Data *chunk_serialize(Chunk *chunk, bool use_metadata) {
|
|||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR,
|
log_message(LOG_LEVEL_ERROR,
|
||||||
"Could not allocate memory for chunk serialization");
|
"Could not allocate memory for chunk serialization");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
char *data_pointer = data->data;
|
char *data_pointer = data->data;
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
@@ -172,8 +170,10 @@ Chunk *chunk_deserialize(Data *data, bool use_metadata) {
|
|||||||
Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata) {
|
Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata) {
|
||||||
log_message(LOG_LEVEL_DEBUG, "Starting to compress chunk");
|
log_message(LOG_LEVEL_DEBUG, "Starting to compress chunk");
|
||||||
Data *serialized = chunk_serialize(chunk, use_metadata);
|
Data *serialized = chunk_serialize(chunk, use_metadata);
|
||||||
|
if (serialized == NULL) return NULL;
|
||||||
Data *compressed = data_compress(serialized, compression_level);
|
Data *compressed = data_compress(serialized, compression_level);
|
||||||
data_destroy(serialized);
|
data_destroy(serialized);
|
||||||
|
if (compressed == NULL) return NULL;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Chunk successfully compressed");
|
log_message(LOG_LEVEL_DEBUG, "Chunk successfully compressed");
|
||||||
return compressed;
|
return compressed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ Data *data_compress(Data *data_to_compress, int compression_level) {
|
|||||||
log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
|
log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
|
||||||
size_t dst_size = ZSTD_compressBound(data_to_compress->size);
|
size_t dst_size = ZSTD_compressBound(data_to_compress->size);
|
||||||
Data *compressed_data = data_create_empty(dst_size);
|
Data *compressed_data = data_create_empty(dst_size);
|
||||||
|
if (compressed_data == NULL) return NULL;
|
||||||
|
|
||||||
ZSTD_CCtx *cctx = ZSTD_createCCtx();
|
ZSTD_CCtx *cctx = ZSTD_createCCtx();
|
||||||
if (!cctx) {
|
if (!cctx) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
|
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
|
||||||
exit(EXIT_FAILURE);
|
data_destroy(compressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
|
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
|
||||||
@@ -24,7 +26,9 @@ Data *data_compress(Data *data_to_compress, int compression_level) {
|
|||||||
if (ZSTD_isError(ret)) {
|
if (ZSTD_isError(ret)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
|
log_message(LOG_LEVEL_ERROR, "Compression failed: %s",
|
||||||
ZSTD_getErrorName(ret));
|
ZSTD_getErrorName(ret));
|
||||||
exit(EXIT_FAILURE);
|
ZSTD_freeCCtx(cctx);
|
||||||
|
data_destroy(compressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
} while (ret > 0);
|
||||||
|
|
||||||
@@ -43,16 +47,18 @@ Data *data_decompress(Data *compressed_data) {
|
|||||||
if (ZSTD_isError(dst_size)) {
|
if (ZSTD_isError(dst_size)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to get decompressed size: %s",
|
log_message(LOG_LEVEL_ERROR, "Failed to get decompressed size: %s",
|
||||||
ZSTD_getErrorName(dst_size));
|
ZSTD_getErrorName(dst_size));
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data *uncompressed_data = data_create_empty((size_t)dst_size);
|
Data *uncompressed_data = data_create_empty((size_t)dst_size);
|
||||||
|
if (uncompressed_data == NULL) return NULL;
|
||||||
|
|
||||||
ZSTD_DCtx *dctx = ZSTD_createDCtx();
|
ZSTD_DCtx *dctx = ZSTD_createDCtx();
|
||||||
if (!dctx) {
|
if (!dctx) {
|
||||||
log_message(LOG_LEVEL_ERROR,
|
log_message(LOG_LEVEL_ERROR,
|
||||||
"Failed to create ZSTD decompression context");
|
"Failed to create ZSTD decompression context");
|
||||||
exit(EXIT_FAILURE);
|
data_destroy(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
|
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
|
||||||
@@ -64,7 +70,9 @@ Data *data_decompress(Data *compressed_data) {
|
|||||||
if (ZSTD_isError(ret)) {
|
if (ZSTD_isError(ret)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s",
|
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s",
|
||||||
ZSTD_getErrorName(ret));
|
ZSTD_getErrorName(ret));
|
||||||
exit(EXIT_FAILURE);
|
ZSTD_freeDCtx(dctx);
|
||||||
|
data_destroy(uncompressed_data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
} while (ret > 0);
|
||||||
|
|
||||||
|
|||||||
+62
-26
@@ -1,4 +1,5 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "log.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -33,6 +34,10 @@ Config *config_create(char *version, char *send_directory,
|
|||||||
config->ssh_destination = NULL;
|
config->ssh_destination = NULL;
|
||||||
config->exclude_patterns = NULL;
|
config->exclude_patterns = NULL;
|
||||||
config->exclude_count = 0;
|
config->exclude_count = 0;
|
||||||
|
config->include_patterns = NULL;
|
||||||
|
config->include_count = 0;
|
||||||
|
config->max_size = 0;
|
||||||
|
config->min_size = 0;
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,42 +70,62 @@ void config_delete(Config *config) {
|
|||||||
for (int i = 0; i < config->exclude_count; i++)
|
for (int i = 0; i < config->exclude_count; i++)
|
||||||
free(config->exclude_patterns[i]);
|
free(config->exclude_patterns[i]);
|
||||||
free(config->exclude_patterns);
|
free(config->exclude_patterns);
|
||||||
|
for (int i = 0; i < config->include_count; i++)
|
||||||
|
free(config->include_patterns[i]);
|
||||||
|
free(config->include_patterns);
|
||||||
free(config);
|
free(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void config_send(int file_descriptor, Config *config) {
|
bool config_send(int file_descriptor, Config *config) {
|
||||||
send_str(file_descriptor, config->version);
|
if (!send_str(file_descriptor, config->version)) return false;
|
||||||
send_str(file_descriptor, config->send_directory);
|
if (!send_str(file_descriptor, config->send_directory)) return false;
|
||||||
send_str(file_descriptor, config->receive_root_directory);
|
if (!send_str(file_descriptor, config->receive_root_directory)) return false;
|
||||||
send_int(file_descriptor, config->save_to_disk);
|
if (!send_int(file_descriptor, config->save_to_disk)) return false;
|
||||||
send_int(file_descriptor, config->use_multithreading);
|
if (!send_int(file_descriptor, config->use_multithreading)) return false;
|
||||||
send_int(file_descriptor, config->use_chunk_serialization);
|
if (!send_int(file_descriptor, config->use_chunk_serialization)) return false;
|
||||||
send_int(file_descriptor, config->use_compression);
|
if (!send_int(file_descriptor, config->use_compression)) return false;
|
||||||
send_int(file_descriptor, config->use_metadata);
|
if (!send_int(file_descriptor, config->use_metadata)) return false;
|
||||||
send_int(file_descriptor, config->compression_level);
|
if (!send_int(file_descriptor, config->compression_level)) return false;
|
||||||
send_int(file_descriptor, (int)config->chunk_size);
|
if (!send_int(file_descriptor, (int)config->chunk_size)) return false;
|
||||||
send_int(file_descriptor, config->use_sendfile);
|
if (!send_int(file_descriptor, config->use_sendfile)) return false;
|
||||||
send_int(file_descriptor, config->use_delete);
|
if (!send_int(file_descriptor, config->use_delete)) return false;
|
||||||
if (receive_status(file_descriptor) != STATUS_OK) {
|
Status status;
|
||||||
perror("Error transmitting config!");
|
if (!receive_status(file_descriptor, &status)) return false;
|
||||||
exit(EXIT_FAILURE);
|
if (status != STATUS_OK) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Error transmitting config");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config *config_receive(int file_descriptor) {
|
Config *config_receive(int file_descriptor) {
|
||||||
Config *config = (Config *)malloc(sizeof(Config));
|
Config *config = (Config *)malloc(sizeof(Config));
|
||||||
|
if (config == NULL) return NULL;
|
||||||
config->version = receive_str(file_descriptor);
|
config->version = receive_str(file_descriptor);
|
||||||
|
if (!config->version) { free(config); return NULL; }
|
||||||
config->send_directory = receive_str(file_descriptor);
|
config->send_directory = receive_str(file_descriptor);
|
||||||
|
if (!config->send_directory) { free(config->version); free(config); return NULL; }
|
||||||
config->receive_root_directory = receive_str(file_descriptor);
|
config->receive_root_directory = receive_str(file_descriptor);
|
||||||
config->save_to_disk = receive_int(file_descriptor);
|
if (!config->receive_root_directory) { free(config->version); free(config->send_directory); free(config); return NULL; }
|
||||||
config->use_multithreading = receive_int(file_descriptor);
|
int tmp;
|
||||||
config->use_chunk_serialization = receive_int(file_descriptor);
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
config->use_compression = receive_int(file_descriptor);
|
config->save_to_disk = tmp;
|
||||||
config->use_metadata = receive_int(file_descriptor);
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
config->compression_level = receive_int(file_descriptor);
|
config->use_multithreading = tmp;
|
||||||
config->chunk_size = (unsigned long long)receive_int(file_descriptor);
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
config->use_sendfile = receive_int(file_descriptor);
|
config->use_chunk_serialization = tmp;
|
||||||
config->use_delete = receive_int(file_descriptor);
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->use_compression = tmp;
|
||||||
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->use_metadata = tmp;
|
||||||
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->compression_level = tmp;
|
||||||
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->chunk_size = (unsigned long long)tmp;
|
||||||
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->use_sendfile = tmp;
|
||||||
|
if (!receive_int(file_descriptor, &tmp)) goto error;
|
||||||
|
config->use_delete = tmp;
|
||||||
config->show_progress = false;
|
config->show_progress = false;
|
||||||
config->dry_run = false;
|
config->dry_run = false;
|
||||||
config->ssh_port = 22;
|
config->ssh_port = 22;
|
||||||
@@ -108,6 +133,17 @@ Config *config_receive(int file_descriptor) {
|
|||||||
config->ssh_destination = NULL;
|
config->ssh_destination = NULL;
|
||||||
config->exclude_patterns = NULL;
|
config->exclude_patterns = NULL;
|
||||||
config->exclude_count = 0;
|
config->exclude_count = 0;
|
||||||
send_status(file_descriptor, STATUS_OK);
|
config->include_patterns = NULL;
|
||||||
|
config->include_count = 0;
|
||||||
|
config->max_size = 0;
|
||||||
|
config->min_size = 0;
|
||||||
|
if (!send_status(file_descriptor, STATUS_OK)) goto error;
|
||||||
return config;
|
return config;
|
||||||
|
|
||||||
|
error:
|
||||||
|
free(config->version);
|
||||||
|
free(config->send_directory);
|
||||||
|
free(config->receive_root_directory);
|
||||||
|
free(config);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -28,6 +28,10 @@ typedef struct Config {
|
|||||||
char *ssh_destination;
|
char *ssh_destination;
|
||||||
char **exclude_patterns;
|
char **exclude_patterns;
|
||||||
int exclude_count;
|
int exclude_count;
|
||||||
|
char **include_patterns;
|
||||||
|
int include_count;
|
||||||
|
unsigned long long max_size;
|
||||||
|
unsigned long long min_size;
|
||||||
} Config;
|
} Config;
|
||||||
|
|
||||||
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
|
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
|
||||||
@@ -39,7 +43,7 @@ Config *config_create(char *version, char *send_directory,
|
|||||||
int compression_level, bool use_sendfile,
|
int compression_level, bool use_sendfile,
|
||||||
unsigned long long chunk_size);
|
unsigned long long chunk_size);
|
||||||
void config_delete(Config *config);
|
void config_delete(Config *config);
|
||||||
void config_send(int file_descriptor, Config *config);
|
bool config_send(int file_descriptor, Config *config);
|
||||||
Config *config_receive(int file_descriptor);
|
Config *config_receive(int file_descriptor);
|
||||||
bool is_remote_dest(const char *s);
|
bool is_remote_dest(const char *s);
|
||||||
void config_parse_ssh_dest(Config *config);
|
void config_parse_ssh_dest(Config *config);
|
||||||
|
|||||||
+4
-3
@@ -6,7 +6,7 @@ Data *data_create_empty(size_t data_size) {
|
|||||||
void *data = malloc(data_size);
|
void *data = malloc(data_size);
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for empty data");
|
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for empty data");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
return data_create(data, data_size);
|
return data_create(data, data_size);
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ Data *data_create_reserve(size_t size) {
|
|||||||
Data *d = malloc(sizeof(Data));
|
Data *d = malloc(sizeof(Data));
|
||||||
if (d == NULL) {
|
if (d == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for data");
|
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for data");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
d->data = NULL;
|
d->data = NULL;
|
||||||
d->size = size;
|
d->size = size;
|
||||||
@@ -26,7 +26,8 @@ Data *data_create(void *data, size_t data_size) {
|
|||||||
Data *new_data = malloc(sizeof(Data));
|
Data *new_data = malloc(sizeof(Data));
|
||||||
if (new_data == NULL) {
|
if (new_data == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for data");
|
log_message(LOG_LEVEL_ERROR, "Could not allocate memory for data");
|
||||||
exit(EXIT_FAILURE);
|
free(data);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
new_data->data = data;
|
new_data->data = data;
|
||||||
new_data->size = data_size;
|
new_data->size = data_size;
|
||||||
|
|||||||
+58
-30
@@ -21,20 +21,24 @@
|
|||||||
File *file_create(const char *path) {
|
File *file_create(const char *path) {
|
||||||
File *file = (File *)malloc(sizeof(File));
|
File *file = (File *)malloc(sizeof(File));
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for file struct");
|
perror("ERROR: Could not allocate memory for file struct");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int path_len = strlen(path);
|
int path_len = strlen(path);
|
||||||
file->path = (char *)malloc(path_len + 1);
|
file->path = (char *)malloc(path_len + 1);
|
||||||
if (file->path == NULL) {
|
if (file->path == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for path file string");
|
|
||||||
free(file);
|
free(file);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(file->path, path);
|
strcpy(file->path, path);
|
||||||
file->data = data_create_reserve(0);
|
file->data = data_create_reserve(0);
|
||||||
|
if (file->data == NULL) {
|
||||||
|
free(file->path);
|
||||||
|
free(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
file->metadata = NULL;
|
file->metadata = NULL;
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@@ -55,8 +59,8 @@ void file_destroy(void *item) {
|
|||||||
FileMetadata *file_metadata_create(struct stat *stats) {
|
FileMetadata *file_metadata_create(struct stat *stats) {
|
||||||
FileMetadata *m = malloc(sizeof(FileMetadata));
|
FileMetadata *m = malloc(sizeof(FileMetadata));
|
||||||
if (m == NULL) {
|
if (m == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for file metadata");
|
perror("ERROR: Could not allocate memory for file metadata");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
m->mode = stats->st_mode;
|
m->mode = stats->st_mode;
|
||||||
m->uid = stats->st_uid;
|
m->uid = stats->st_uid;
|
||||||
@@ -74,67 +78,79 @@ void file_metadata_destroy(void *metadata) {
|
|||||||
free(metadata);
|
free(metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_load_data(File *file) {
|
bool file_load_data(File *file) {
|
||||||
if (file == NULL)
|
if (file == NULL) return false;
|
||||||
return;
|
|
||||||
if (file->data->data == NULL) {
|
if (file->data->data == NULL) {
|
||||||
file->data->data = malloc(file->data->size);
|
file->data->data = malloc(file->data->size);
|
||||||
if (file->data->data == NULL) {
|
if (file->data->data == NULL) {
|
||||||
perror("Could not allocate memory for file data");
|
perror("Could not allocate memory for file data");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t bytes_read = file_content_to_buffer(file);
|
size_t bytes_read = file_content_to_buffer(file);
|
||||||
if (bytes_read != file->data->size) {
|
if (bytes_read != file->data->size) {
|
||||||
log_message(STATUS_ERROR, "Didnt read expected amount of bytes from file");
|
log_message(LOG_LEVEL_ERROR, "Did not read expected amount of bytes from file");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level) {
|
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level) {
|
||||||
if (compression_level > 0) {
|
if (compression_level > 0) {
|
||||||
Data *compressed_data = data_compress(file->data, compression_level);
|
Data *compressed_data = data_compress(file->data, compression_level);
|
||||||
|
if (compressed_data == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to compress file data");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
data_destroy(file->data);
|
data_destroy(file->data);
|
||||||
file->data = compressed_data;
|
file->data = compressed_data;
|
||||||
}
|
}
|
||||||
send_str(file_descriptor, file->path);
|
if (!send_str(file_descriptor, file->path)) return false;
|
||||||
if (use_metadata)
|
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false;
|
||||||
metadata_send(file_descriptor, file->metadata);
|
if (!send_data(file_descriptor, file->data)) return false;
|
||||||
send_data(file_descriptor, file->data);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void to_disk(const char *path, const void *data, unsigned long long data_size) {
|
bool to_disk(const char *path, const void *data, unsigned long long data_size) {
|
||||||
char *directory = str_dup(path);
|
char *directory = str_dup(path);
|
||||||
char *dir_to_free = directory;
|
char *dir_to_free = directory;
|
||||||
directory = dirname(directory);
|
directory = dirname(directory);
|
||||||
mkdir_r(directory);
|
if (!mkdir_r(directory)) {
|
||||||
|
free(dir_to_free);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
FILE *file_pointer = fopen(path, "wb");
|
FILE *file_pointer = fopen(path, "wb");
|
||||||
if (file_pointer == NULL) {
|
if (file_pointer == NULL) {
|
||||||
perror("Could not open File");
|
perror("Could not open File");
|
||||||
exit(EXIT_FAILURE);
|
free(dir_to_free);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
if (fwrite(data, 1, data_size, file_pointer) != data_size) {
|
if (fwrite(data, 1, data_size, file_pointer) != data_size) {
|
||||||
perror("Failed to write all data to disk");
|
perror("Failed to write all data to disk");
|
||||||
fclose(file_pointer);
|
fclose(file_pointer);
|
||||||
exit(EXIT_FAILURE);
|
free(dir_to_free);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
fclose(file_pointer);
|
fclose(file_pointer);
|
||||||
free(dir_to_free);
|
free(dir_to_free);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_send_sendfile(File *file, int file_descriptor, bool use_metadata) {
|
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata) {
|
||||||
send_str(file_descriptor, file->path);
|
if (!send_str(file_descriptor, file->path)) return false;
|
||||||
if (use_metadata)
|
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false;
|
||||||
metadata_send(file_descriptor, file->metadata);
|
|
||||||
|
|
||||||
int fd = open(file->path, O_RDONLY);
|
int fd = open(file->path, O_RDONLY);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror("Could not open file for sendfile");
|
perror("Could not open file for sendfile");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long long file_size = file->data->size;
|
unsigned long long file_size = file->data->size;
|
||||||
send_n_data(file_descriptor, &file_size, sizeof(unsigned long long));
|
if (!send_n_data(file_descriptor, &file_size, sizeof(unsigned long long))) {
|
||||||
|
close(fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
off_t offset = 0;
|
off_t offset = 0;
|
||||||
while (offset < file_size) {
|
while (offset < file_size) {
|
||||||
@@ -142,23 +158,35 @@ void file_send_sendfile(File *file, int file_descriptor, bool use_metadata) {
|
|||||||
if (sent == -1) {
|
if (sent == -1) {
|
||||||
perror("sendfile failed");
|
perror("sendfile failed");
|
||||||
close(fd);
|
close(fd);
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
File *file_receive(Config *config, int file_descriptor) {
|
File *file_receive(Config *config, int file_descriptor) {
|
||||||
char *path = (char *)receive_str(file_descriptor);
|
char *path = receive_str(file_descriptor);
|
||||||
|
if (path == NULL) return NULL;
|
||||||
File *file = file_create(path);
|
File *file = file_create(path);
|
||||||
free(path);
|
free(path);
|
||||||
if (config->use_metadata)
|
if (file == NULL) return NULL;
|
||||||
|
if (config->use_metadata) {
|
||||||
file->metadata = metadata_receive(file_descriptor);
|
file->metadata = metadata_receive(file_descriptor);
|
||||||
|
}
|
||||||
Data *file_data = receive_data(file_descriptor);
|
Data *file_data = receive_data(file_descriptor);
|
||||||
|
if (file_data == NULL) {
|
||||||
|
file_destroy(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (config->use_compression) {
|
if (config->use_compression) {
|
||||||
Data *file_data_uncompressed = data_decompress(file_data);
|
Data *file_data_uncompressed = data_decompress(file_data);
|
||||||
data_destroy(file_data);
|
data_destroy(file_data);
|
||||||
|
if (file_data_uncompressed == NULL) {
|
||||||
|
file_destroy(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
file_data = file_data_uncompressed;
|
file_data = file_data_uncompressed;
|
||||||
}
|
}
|
||||||
data_destroy(file->data);
|
data_destroy(file->data);
|
||||||
|
|||||||
+4
-4
@@ -22,13 +22,13 @@ typedef struct {
|
|||||||
|
|
||||||
File *file_create(const char *path);
|
File *file_create(const char *path);
|
||||||
void file_destroy(void *item);
|
void file_destroy(void *item);
|
||||||
void file_load_data(File *file);
|
bool file_load_data(File *file);
|
||||||
File *file_receive(Config *config, int file_descriptor);
|
File *file_receive(Config *config, int file_descriptor);
|
||||||
void file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level);
|
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level);
|
||||||
void file_send_sendfile(File *file, int file_descriptor, bool use_metadata);
|
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata);
|
||||||
size_t file_content_to_buffer(File *file);
|
size_t file_content_to_buffer(File *file);
|
||||||
FileMetadata *file_metadata_create(struct stat *stats);
|
FileMetadata *file_metadata_create(struct stat *stats);
|
||||||
void file_metadata_destroy(void *metadata);
|
void file_metadata_destroy(void *metadata);
|
||||||
void to_disk(const char *path, const void *data, unsigned long long data_size);
|
bool to_disk(const char *path, const void *data, unsigned long long data_size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+19
-15
@@ -36,32 +36,36 @@ FileMetadata *metadata_from_buf(char **buf) {
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void metadata_send(int file_descriptor, FileMetadata *m) {
|
bool metadata_send(int file_descriptor, FileMetadata *m) {
|
||||||
if (m == NULL) {
|
if (m == NULL) {
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
send_n_data(file_descriptor, &zero, sizeof(int));
|
return send_n_data(file_descriptor, &zero, sizeof(int));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
int present = 1;
|
int present = 1;
|
||||||
send_n_data(file_descriptor, &present, sizeof(int));
|
return send_n_data(file_descriptor, &present, sizeof(int)) &&
|
||||||
send_n_data(file_descriptor, &m->mode, sizeof(mode_t));
|
send_n_data(file_descriptor, &m->mode, sizeof(mode_t)) &&
|
||||||
send_n_data(file_descriptor, &m->uid, sizeof(uid_t));
|
send_n_data(file_descriptor, &m->uid, sizeof(uid_t)) &&
|
||||||
send_n_data(file_descriptor, &m->gid, sizeof(gid_t));
|
send_n_data(file_descriptor, &m->gid, sizeof(gid_t)) &&
|
||||||
send_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t));
|
send_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t)) &&
|
||||||
send_n_data(file_descriptor, &m->mtime_nsec, sizeof(long));
|
send_n_data(file_descriptor, &m->mtime_nsec, sizeof(long));
|
||||||
}
|
}
|
||||||
|
|
||||||
FileMetadata *metadata_receive(int file_descriptor) {
|
FileMetadata *metadata_receive(int file_descriptor) {
|
||||||
int present;
|
int present;
|
||||||
receive_n_data(file_descriptor, &present, sizeof(int));
|
if (!receive_n_data(file_descriptor, &present, sizeof(int)))
|
||||||
|
return NULL;
|
||||||
if (!present)
|
if (!present)
|
||||||
return NULL;
|
return NULL;
|
||||||
FileMetadata *m = malloc(sizeof(FileMetadata));
|
FileMetadata *m = malloc(sizeof(FileMetadata));
|
||||||
receive_n_data(file_descriptor, &m->mode, sizeof(mode_t));
|
if (m == NULL) return NULL;
|
||||||
receive_n_data(file_descriptor, &m->uid, sizeof(uid_t));
|
if (!receive_n_data(file_descriptor, &m->mode, sizeof(mode_t)) ||
|
||||||
receive_n_data(file_descriptor, &m->gid, sizeof(gid_t));
|
!receive_n_data(file_descriptor, &m->uid, sizeof(uid_t)) ||
|
||||||
receive_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t));
|
!receive_n_data(file_descriptor, &m->gid, sizeof(gid_t)) ||
|
||||||
receive_n_data(file_descriptor, &m->mtime_nsec, sizeof(long));
|
!receive_n_data(file_descriptor, &m->mtime_sec, sizeof(time_t)) ||
|
||||||
|
!receive_n_data(file_descriptor, &m->mtime_nsec, sizeof(long))) {
|
||||||
|
free(m);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
#define METADATA_H
|
#define METADATA_H
|
||||||
|
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#define FILE_METADATA_WIRE_SIZE (sizeof(mode_t) + sizeof(uid_t) + sizeof(gid_t) + sizeof(time_t) + sizeof(long))
|
#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);
|
void metadata_to_buf(char **buf, FileMetadata *m);
|
||||||
FileMetadata *metadata_from_buf(char **buf);
|
FileMetadata *metadata_from_buf(char **buf);
|
||||||
void metadata_send(int file_descriptor, FileMetadata *m);
|
bool metadata_send(int file_descriptor, FileMetadata *m);
|
||||||
FileMetadata *metadata_receive(int file_descriptor);
|
FileMetadata *metadata_receive(int file_descriptor);
|
||||||
void file_restore_metadata(const char *path, FileMetadata *metadata);
|
void file_restore_metadata(const char *path, FileMetadata *metadata);
|
||||||
|
|
||||||
|
|||||||
@@ -19,19 +19,22 @@ PipelineContextSender *pipeline_context_sender_create(Config *config,
|
|||||||
Queue *queue_scanner,
|
Queue *queue_scanner,
|
||||||
Queue *queue_loader) {
|
Queue *queue_loader) {
|
||||||
PipelineContextSender *context = malloc(sizeof(PipelineContextSender));
|
PipelineContextSender *context = malloc(sizeof(PipelineContextSender));
|
||||||
|
if (context == NULL) return NULL;
|
||||||
context->config = config;
|
context->config = config;
|
||||||
context->queue_scanner = queue_scanner;
|
context->queue_scanner = queue_scanner;
|
||||||
context->queue_loader = queue_loader;
|
context->queue_loader = queue_loader;
|
||||||
context->scanner_done = false;
|
context->scanner_done = false;
|
||||||
context->loader_done = false;
|
context->loader_done = false;
|
||||||
|
context->manifest = NULL;
|
||||||
if (mtx_init(&context->mutex_scanner, mtx_plain) != thrd_success ||
|
if (mtx_init(&context->mutex_scanner, mtx_plain) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_full_scanner) != thrd_success ||
|
cnd_init(&context->condition_not_full_scanner) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_empty_scanner) != thrd_success ||
|
cnd_init(&context->condition_not_empty_scanner) != thrd_success ||
|
||||||
mtx_init(&context->mutex_loader, mtx_plain) != thrd_success ||
|
mtx_init(&context->mutex_loader, mtx_plain) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_full_loader) != thrd_success ||
|
cnd_init(&context->condition_not_full_loader) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_empty_loader) != thrd_success) {
|
cnd_init(&context->condition_not_empty_loader) != thrd_success) {
|
||||||
perror("Error initializing synchronization objects!");
|
perror("Error initializing synchronization objects");
|
||||||
exit(EXIT_FAILURE);
|
free(context);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
@@ -56,6 +59,7 @@ PipelineContextReceiver *pipeline_context_receiver_create(Config *config,
|
|||||||
Queue *queue,
|
Queue *queue,
|
||||||
int file_descriptor) {
|
int file_descriptor) {
|
||||||
PipelineContextReceiver *context = malloc(sizeof(PipelineContextReceiver));
|
PipelineContextReceiver *context = malloc(sizeof(PipelineContextReceiver));
|
||||||
|
if (context == NULL) return NULL;
|
||||||
context->config = config;
|
context->config = config;
|
||||||
context->queue = queue;
|
context->queue = queue;
|
||||||
context->file_descriptor = file_descriptor;
|
context->file_descriptor = file_descriptor;
|
||||||
@@ -63,8 +67,9 @@ PipelineContextReceiver *pipeline_context_receiver_create(Config *config,
|
|||||||
if (mtx_init(&context->mutex, mtx_plain) != thrd_success ||
|
if (mtx_init(&context->mutex, mtx_plain) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_full) != thrd_success ||
|
cnd_init(&context->condition_not_full) != thrd_success ||
|
||||||
cnd_init(&context->condition_not_empty) != thrd_success) {
|
cnd_init(&context->condition_not_empty) != thrd_success) {
|
||||||
perror("Error initializing synchronization objects!");
|
perror("Error initializing synchronization objects");
|
||||||
exit(EXIT_FAILURE);
|
free(context);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
@@ -81,10 +86,18 @@ void pipeline_context_receiver_destroy(PipelineContextReceiver *context) {
|
|||||||
static void receive_chunk_enqueue(int file_descriptor,
|
static void receive_chunk_enqueue(int file_descriptor,
|
||||||
PipelineContextReceiver *context) {
|
PipelineContextReceiver *context) {
|
||||||
Data *chunk_data = receive_data(file_descriptor);
|
Data *chunk_data = receive_data(file_descriptor);
|
||||||
|
if (chunk_data == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
Data *data_to_process = chunk_data;
|
Data *data_to_process = chunk_data;
|
||||||
if (context->config->use_compression) {
|
if (context->config->use_compression) {
|
||||||
data_to_process = data_decompress(chunk_data);
|
data_to_process = data_decompress(chunk_data);
|
||||||
data_destroy(chunk_data);
|
data_destroy(chunk_data);
|
||||||
|
if (data_to_process == NULL) {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
||||||
data_destroy(data_to_process);
|
data_destroy(data_to_process);
|
||||||
@@ -111,28 +124,40 @@ int receive_thread(void *pipeline_context) {
|
|||||||
Config *config = context->config;
|
Config *config = context->config;
|
||||||
mtx_unlock(&context->mutex);
|
mtx_unlock(&context->mutex);
|
||||||
|
|
||||||
Status status = receive_status(file_descriptor);
|
Status status;
|
||||||
|
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||||
while (status == STATUS_NEXT || status == STATUS_CHUNK) {
|
while (status == STATUS_NEXT || status == STATUS_CHUNK) {
|
||||||
if (status == STATUS_CHUNK) {
|
if (status == STATUS_CHUNK) {
|
||||||
receive_chunk_enqueue(file_descriptor, context);
|
receive_chunk_enqueue(file_descriptor, context);
|
||||||
} else {
|
} else {
|
||||||
File *file = file_receive(config, file_descriptor);
|
File *file = file_receive(config, file_descriptor);
|
||||||
queue_enqueue_multithreaded(context->queue, file, &context->mutex,
|
if (file) {
|
||||||
&context->condition_not_empty,
|
queue_enqueue_multithreaded(context->queue, file, &context->mutex,
|
||||||
&context->condition_not_full);
|
&context->condition_not_empty,
|
||||||
|
&context->condition_not_full);
|
||||||
|
} else {
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Failed to receive file");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
status = receive_status(file_descriptor);
|
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||||
}
|
}
|
||||||
if (status == STATUS_MANIFEST) {
|
if (status == STATUS_MANIFEST) {
|
||||||
int count = receive_int(file_descriptor);
|
int count;
|
||||||
|
if (!receive_int(file_descriptor, &count)) return thrd_error;
|
||||||
ArrayList *manifest = array_list_create(free);
|
ArrayList *manifest = array_list_create(free);
|
||||||
for (int i = 0; i < count; i++)
|
if (manifest) {
|
||||||
array_list_add(manifest, receive_str(file_descriptor));
|
for (int i = 0; i < count; i++) {
|
||||||
delete_extras(context->config->receive_root_directory, manifest);
|
char *s = receive_str(file_descriptor);
|
||||||
for (int i = 0; i < manifest->size; i++)
|
if (s) {
|
||||||
free(manifest->items[i]);
|
array_list_add(manifest, s);
|
||||||
array_list_delete(manifest);
|
}
|
||||||
status = receive_status(file_descriptor);
|
}
|
||||||
|
delete_extras(context->config->receive_root_directory, manifest);
|
||||||
|
for (int i = 0; i < manifest->size; i++)
|
||||||
|
free(manifest->items[i]);
|
||||||
|
array_list_delete(manifest);
|
||||||
|
}
|
||||||
|
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||||
}
|
}
|
||||||
mtx_lock(&context->mutex);
|
mtx_lock(&context->mutex);
|
||||||
context->receiver_done = true;
|
context->receiver_done = true;
|
||||||
@@ -159,9 +184,11 @@ int write_thread(void *pipeline_context) {
|
|||||||
}
|
}
|
||||||
if (save_to_disk) {
|
if (save_to_disk) {
|
||||||
char *disk_path = path_cat(root_directory, file->path);
|
char *disk_path = path_cat(root_directory, file->path);
|
||||||
to_disk(disk_path, file->data->data, file->data->size);
|
if (disk_path) {
|
||||||
file_restore_metadata(disk_path, file->metadata);
|
to_disk(disk_path, file->data->data, file->data->size);
|
||||||
free(disk_path);
|
file_restore_metadata(disk_path, file->metadata);
|
||||||
|
free(disk_path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
file_destroy(file);
|
file_destroy(file);
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-31
@@ -17,7 +17,7 @@ static int io_fd(int dir_fd, int file_descriptor) {
|
|||||||
return (dir_fd != -1) ? dir_fd : file_descriptor;
|
return (dir_fd != -1) ? dir_fd : file_descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_n_data(int file_descriptor, void *data, size_t data_size) {
|
bool send_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||||
log_message(LOG_LEVEL_DEBUG, " Sending n Data: %zu", data_size);
|
log_message(LOG_LEVEL_DEBUG, " Sending n Data: %zu", data_size);
|
||||||
int fd = io_fd(io_write_fd, file_descriptor);
|
int fd = io_fd(io_write_fd, file_descriptor);
|
||||||
ssize_t total_bytes_send = 0;
|
ssize_t total_bytes_send = 0;
|
||||||
@@ -25,28 +25,33 @@ void send_n_data(int file_descriptor, void *data, size_t data_size) {
|
|||||||
ssize_t bytes_send =
|
ssize_t bytes_send =
|
||||||
write(fd, (char *)data + total_bytes_send, data_size - total_bytes_send);
|
write(fd, (char *)data + total_bytes_send, data_size - total_bytes_send);
|
||||||
if (bytes_send <= 0) {
|
if (bytes_send <= 0) {
|
||||||
perror("Could not send data!");
|
log_message(LOG_LEVEL_ERROR, "Could not send data");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
total_bytes_send += bytes_send;
|
total_bytes_send += bytes_send;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_DEBUG, " Send n Data: %zu", total_bytes_send);
|
log_message(LOG_LEVEL_DEBUG, " Send n Data: %zu", total_bytes_send);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void receive_n_data(int file_descriptor, void *data, size_t data_size) {
|
bool receive_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||||
log_message(LOG_LEVEL_DEBUG, " Receiving n Data: %zu", data_size);
|
log_message(LOG_LEVEL_DEBUG, " Receiving n Data: %zu", data_size);
|
||||||
int fd = io_fd(io_read_fd, file_descriptor);
|
int fd = io_fd(io_read_fd, file_descriptor);
|
||||||
size_t total_bytes_received = 0;
|
size_t total_bytes_received = 0;
|
||||||
while (total_bytes_received < data_size) {
|
while (total_bytes_received < data_size) {
|
||||||
ssize_t bytes_received =
|
ssize_t bytes_received =
|
||||||
read(fd, (char *)data + total_bytes_received, data_size - total_bytes_received);
|
read(fd, (char *)data + total_bytes_received, data_size - total_bytes_received);
|
||||||
if (bytes_received == -1 || bytes_received == 0) {
|
if (bytes_received <= 0) {
|
||||||
perror("Could not receive bytes!");
|
if (bytes_received == 0)
|
||||||
exit(EXIT_FAILURE);
|
log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data");
|
||||||
|
else
|
||||||
|
log_message(LOG_LEVEL_ERROR, "Could not receive bytes");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
total_bytes_received += bytes_received;
|
total_bytes_received += bytes_received;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_DEBUG, " Received n Data: %zu", total_bytes_received);
|
log_message(LOG_LEVEL_DEBUG, " Received n Data: %zu", total_bytes_received);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *status_to_string(Status status) {
|
static const char *status_to_string(Status status) {
|
||||||
@@ -66,59 +71,72 @@ static const char *status_to_string(Status status) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_str(int file_descriptor, char *data) {
|
bool send_str(int file_descriptor, char *data) {
|
||||||
size_t size = strlen(data);
|
size_t size = strlen(data);
|
||||||
send_n_data(file_descriptor, &size, sizeof(size_t));
|
if (!send_n_data(file_descriptor, &size, sizeof(size_t))) return false;
|
||||||
send_n_data(file_descriptor, data, size);
|
if (!send_n_data(file_descriptor, data, size)) return false;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Send String: %s", data);
|
log_message(LOG_LEVEL_DEBUG, "Send String: %s", data);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *receive_str(int file_descriptor) {
|
char *receive_str(int file_descriptor) {
|
||||||
size_t size;
|
size_t size;
|
||||||
receive_n_data(file_descriptor, &size, sizeof(size_t));
|
if (!receive_n_data(file_descriptor, &size, sizeof(size_t))) return NULL;
|
||||||
char *data = (char *)malloc(size + 1);
|
char *data = (char *)malloc(size + 1);
|
||||||
receive_n_data(file_descriptor, data, size);
|
if (data == NULL) return NULL;
|
||||||
|
if (!receive_n_data(file_descriptor, data, size)) {
|
||||||
|
free(data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
data[size] = '\0';
|
data[size] = '\0';
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received String: %s", data);
|
log_message(LOG_LEVEL_DEBUG, "Received String: %s", data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_data(int file_descriptor, Data *data) {
|
bool send_data(int file_descriptor, Data *data) {
|
||||||
unsigned long long data_size = data->size;
|
unsigned long long data_size = data->size;
|
||||||
send_n_data(file_descriptor, &data_size, sizeof(unsigned long long));
|
if (!send_n_data(file_descriptor, &data_size, sizeof(unsigned long long)))
|
||||||
send_n_data(file_descriptor, data->data, data_size);
|
return false;
|
||||||
|
if (!send_n_data(file_descriptor, data->data, data_size))
|
||||||
|
return false;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Send %lld data", data_size);
|
log_message(LOG_LEVEL_DEBUG, "Send %lld data", data_size);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data *receive_data(int file_descriptor) {
|
Data *receive_data(int file_descriptor) {
|
||||||
unsigned long long size = 0;
|
unsigned long long size = 0;
|
||||||
receive_n_data(file_descriptor, &size, sizeof(unsigned long long));
|
if (!receive_n_data(file_descriptor, &size, sizeof(unsigned long long)))
|
||||||
|
return NULL;
|
||||||
void *data = malloc((size_t)size);
|
void *data = malloc((size_t)size);
|
||||||
receive_n_data(file_descriptor, data, (size_t)size);
|
if (data == NULL) return NULL;
|
||||||
|
if (!receive_n_data(file_descriptor, data, (size_t)size)) {
|
||||||
|
free(data);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received %lld data", size);
|
log_message(LOG_LEVEL_DEBUG, "Received %lld data", size);
|
||||||
return data_create(data, (size_t)size);
|
return data_create(data, (size_t)size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_int(int file_descriptor, int data) {
|
bool send_int(int file_descriptor, int data) {
|
||||||
send_n_data(file_descriptor, &data, sizeof(int));
|
if (!send_n_data(file_descriptor, &data, sizeof(int))) return false;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Send Int: %d", data);
|
log_message(LOG_LEVEL_DEBUG, "Send Int: %d", data);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int receive_int(int file_descriptor) {
|
bool receive_int(int file_descriptor, int *data) {
|
||||||
int data;
|
if (!receive_n_data(file_descriptor, data, sizeof(int))) return false;
|
||||||
receive_n_data(file_descriptor, &data, sizeof(int));
|
log_message(LOG_LEVEL_DEBUG, "Received Int: %d", *data);
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received Int: %d", data);
|
return true;
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_status(int file_descriptor, Status status) {
|
bool send_status(int file_descriptor, Status status) {
|
||||||
send_n_data(file_descriptor, &status, sizeof(Status));
|
if (!send_n_data(file_descriptor, &status, sizeof(Status))) return false;
|
||||||
log_message(LOG_LEVEL_DEBUG, "Send Status: %s", status_to_string(status));
|
log_message(LOG_LEVEL_DEBUG, "Send Status: %s", status_to_string(status));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Status receive_status(int file_descriptor) {
|
bool receive_status(int file_descriptor, Status *status) {
|
||||||
Status data;
|
if (!receive_n_data(file_descriptor, status, sizeof(Status))) return false;
|
||||||
receive_n_data(file_descriptor, &data, sizeof(Status));
|
log_message(LOG_LEVEL_DEBUG, "Received Status: %s", status_to_string(*status));
|
||||||
log_message(LOG_LEVEL_DEBUG, "Received Status: %s", status_to_string(data));
|
return true;
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,23 @@
|
|||||||
#define PROTOCOL_H
|
#define PROTOCOL_H
|
||||||
|
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef int Status;
|
typedef int Status;
|
||||||
enum NET_STATUS { STATUS_OK, STATUS_ERROR, STATUS_FINISHED, STATUS_NEXT, STATUS_CHUNK, STATUS_MANIFEST };
|
enum NET_STATUS { STATUS_OK, STATUS_ERROR, STATUS_FINISHED, STATUS_NEXT, STATUS_CHUNK, STATUS_MANIFEST };
|
||||||
|
|
||||||
void io_set_fds(int read_fd, int write_fd);
|
void io_set_fds(int read_fd, int write_fd);
|
||||||
void send_n_data(int file_descriptor, void *data, size_t data_size);
|
bool send_n_data(int file_descriptor, void *data, size_t data_size);
|
||||||
void receive_n_data(int file_descriptor, void *data, size_t data_size);
|
bool receive_n_data(int file_descriptor, void *data, size_t data_size);
|
||||||
|
|
||||||
void send_str(int file_descriptor, char *data);
|
bool send_str(int file_descriptor, char *data);
|
||||||
char *receive_str(int file_descriptor);
|
char *receive_str(int file_descriptor);
|
||||||
void send_data(int file_descriptor, Data *data);
|
bool send_data(int file_descriptor, Data *data);
|
||||||
Data *receive_data(int file_descriptor);
|
Data *receive_data(int file_descriptor);
|
||||||
void send_int(int file_descriptor, int data);
|
bool send_int(int file_descriptor, int data);
|
||||||
int receive_int(int file_descriptor);
|
bool receive_int(int file_descriptor, int *data);
|
||||||
void send_status(int file_descriptor, Status status);
|
bool send_status(int file_descriptor, Status status);
|
||||||
Status receive_status(int file_descriptor);
|
bool receive_status(int file_descriptor, Status *status);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+16
-18
@@ -9,15 +9,14 @@
|
|||||||
Queue *queue_create(int capacity, void (*destroyer)(void *item)) {
|
Queue *queue_create(int capacity, void (*destroyer)(void *item)) {
|
||||||
Queue *queue = (Queue *)malloc(sizeof(Queue));
|
Queue *queue = (Queue *)malloc(sizeof(Queue));
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for queue structure");
|
perror("ERROR: Could not allocate memory for queue structure");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
queue->items = malloc(capacity * sizeof(void *));
|
queue->items = malloc(capacity * sizeof(void *));
|
||||||
if (queue->items == NULL) {
|
if (queue->items == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for queue items");
|
|
||||||
free(queue);
|
free(queue);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < capacity; ++i) {
|
for (int i = 0; i < capacity; ++i) {
|
||||||
@@ -59,17 +58,15 @@ bool queue_is_full(Queue *queue) {
|
|||||||
return queue->size == queue->capacity;
|
return queue->size == queue->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void queue_double_capacity(Queue *queue) {
|
static bool queue_double_capacity(Queue *queue) {
|
||||||
if (queue == NULL)
|
if (queue == NULL) return false;
|
||||||
return;
|
|
||||||
unsigned int new_capacity = queue->capacity * 2;
|
unsigned int new_capacity = queue->capacity * 2;
|
||||||
if (new_capacity <= 1)
|
if (new_capacity <= 1)
|
||||||
new_capacity = 100;
|
new_capacity = 100;
|
||||||
void **new_items = malloc(new_capacity * sizeof(void *));
|
void **new_items = malloc(new_capacity * sizeof(void *));
|
||||||
if (new_items == NULL) {
|
if (new_items == NULL) {
|
||||||
perror("FATAL ERROR: Could not allocate memory for doubling capacity of "
|
perror("ERROR: Could not allocate memory for doubling capacity of queue.");
|
||||||
"queue.");
|
return false;
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
for (int i = 0; i < queue->size; i++)
|
for (int i = 0; i < queue->size; i++)
|
||||||
new_items[i] = queue->items[(i + queue->front) % queue->capacity];
|
new_items[i] = queue->items[(i + queue->front) % queue->capacity];
|
||||||
@@ -78,29 +75,30 @@ static void queue_double_capacity(Queue *queue) {
|
|||||||
queue->front = 0;
|
queue->front = 0;
|
||||||
queue->rear = queue->size;
|
queue->rear = queue->size;
|
||||||
queue->capacity = new_capacity;
|
queue->capacity = new_capacity;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_enqueue(Queue *queue, void *item) {
|
bool queue_enqueue(Queue *queue, void *item) {
|
||||||
if (queue == NULL || item == NULL) {
|
if (queue == NULL || item == NULL) return false;
|
||||||
perror("ERROR: Cannot enqueue with a null queue or item.\n");
|
if (queue_is_full(queue)) {
|
||||||
exit(EXIT_FAILURE);
|
if (!queue_double_capacity(queue)) return false;
|
||||||
}
|
}
|
||||||
if (queue_is_full(queue))
|
|
||||||
queue_double_capacity(queue);
|
|
||||||
queue->items[queue->rear] = item;
|
queue->items[queue->rear] = item;
|
||||||
queue->rear = (queue->rear + 1) % queue->capacity;
|
queue->rear = (queue->rear + 1) % queue->capacity;
|
||||||
queue->size++;
|
queue->size++;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_enqueue_multithreaded(Queue *queue, void *item, mtx_t *mutex,
|
bool queue_enqueue_multithreaded(Queue *queue, void *item, mtx_t *mutex,
|
||||||
cnd_t *condition_not_empty,
|
cnd_t *condition_not_empty,
|
||||||
cnd_t *condition_not_full) {
|
cnd_t *condition_not_full) {
|
||||||
mtx_lock(mutex);
|
mtx_lock(mutex);
|
||||||
while (queue_is_full(queue))
|
while (queue_is_full(queue))
|
||||||
cnd_wait(condition_not_full, mutex);
|
cnd_wait(condition_not_full, mutex);
|
||||||
queue_enqueue(queue, item);
|
bool ok = queue_enqueue(queue, item);
|
||||||
cnd_signal(condition_not_empty);
|
cnd_signal(condition_not_empty);
|
||||||
mtx_unlock(mutex);
|
mtx_unlock(mutex);
|
||||||
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *queue_dequeue(Queue *queue) {
|
void *queue_dequeue(Queue *queue) {
|
||||||
|
|||||||
+2
-2
@@ -17,8 +17,8 @@ Queue *queue_create(int capacity, void (*destroyer)(void *item));
|
|||||||
void queue_destroy(Queue *queue);
|
void queue_destroy(Queue *queue);
|
||||||
bool queue_is_empty(Queue *queue);
|
bool queue_is_empty(Queue *queue);
|
||||||
bool queue_is_full(Queue *queue);
|
bool queue_is_full(Queue *queue);
|
||||||
void queue_enqueue(Queue *queue, void *item);
|
bool queue_enqueue(Queue *queue, void *item);
|
||||||
void queue_enqueue_multithreaded(Queue *queue, void *item, mtx_t *mutex,
|
bool queue_enqueue_multithreaded(Queue *queue, void *item, mtx_t *mutex,
|
||||||
cnd_t *condition_not_empty,
|
cnd_t *condition_not_empty,
|
||||||
cnd_t *condition_not_full);
|
cnd_t *condition_not_full);
|
||||||
void *queue_dequeue(Queue *queue);
|
void *queue_dequeue(Queue *queue);
|
||||||
|
|||||||
@@ -46,13 +46,13 @@ Client *client_connect_ssh(char *destination, int port) {
|
|||||||
RemoteDest r;
|
RemoteDest r;
|
||||||
if (parse_remote_dest(destination, &r) != 0) {
|
if (parse_remote_dest(destination, &r) != 0) {
|
||||||
fprintf(stderr, "Invalid remote destination: %s\n", destination);
|
fprintf(stderr, "Invalid remote destination: %s\n", destination);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sv[2];
|
int sv[2];
|
||||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
|
||||||
perror("socketpair failed");
|
perror("socketpair failed");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int buf_size = 1024 * 1024;
|
int buf_size = 1024 * 1024;
|
||||||
@@ -64,13 +64,16 @@ Client *client_connect_ssh(char *destination, int port) {
|
|||||||
int exec_pipe[2];
|
int exec_pipe[2];
|
||||||
if (pipe(exec_pipe) < 0) {
|
if (pipe(exec_pipe) < 0) {
|
||||||
perror("pipe failed");
|
perror("pipe failed");
|
||||||
exit(EXIT_FAILURE);
|
close(sv[0]); close(sv[1]);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
perror("fork failed");
|
perror("fork failed");
|
||||||
exit(EXIT_FAILURE);
|
close(sv[0]); close(sv[1]);
|
||||||
|
close(exec_pipe[0]); close(exec_pipe[1]);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
@@ -127,10 +130,15 @@ Client *client_connect_ssh(char *destination, int port) {
|
|||||||
close(sv[0]);
|
close(sv[0]);
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
fprintf(stderr, "Error: could not launch 'fastsync-server --stdio' on remote\n");
|
fprintf(stderr, "Error: could not launch 'fastsync-server --stdio' on remote\n");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client *client = malloc(sizeof(Client));
|
Client *client = malloc(sizeof(Client));
|
||||||
|
if (client == NULL) {
|
||||||
|
close(sv[0]);
|
||||||
|
waitpid(pid, NULL, 0);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
client->file_descriptor = sv[0];
|
client->file_descriptor = sv[0];
|
||||||
client->address.sin_family = AF_UNIX;
|
client->address.sin_family = AF_UNIX;
|
||||||
client->address_length = 0;
|
client->address_length = 0;
|
||||||
|
|||||||
+19
-12
@@ -12,13 +12,14 @@ Server *server_create(int port) {
|
|||||||
Server *server = (Server *)malloc(sizeof(Server));
|
Server *server = (Server *)malloc(sizeof(Server));
|
||||||
if (server == NULL) {
|
if (server == NULL) {
|
||||||
perror("Could not allocate space for Server");
|
perror("Could not allocate space for Server");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not create Socket!");
|
perror("Could not create Socket!");
|
||||||
exit(EXIT_FAILURE);
|
free(server);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
server->file_descriptor = file_descriptor;
|
server->file_descriptor = file_descriptor;
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
@@ -27,7 +28,7 @@ Server *server_create(int port) {
|
|||||||
perror("Error setting a socket option!");
|
perror("Error setting a socket option!");
|
||||||
close(server->file_descriptor);
|
close(server->file_descriptor);
|
||||||
free(server);
|
free(server);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
server->address.sin_family = AF_INET;
|
server->address.sin_family = AF_INET;
|
||||||
@@ -40,7 +41,7 @@ Server *server_create(int port) {
|
|||||||
perror("Could not bind server");
|
perror("Could not bind server");
|
||||||
close(server->file_descriptor);
|
close(server->file_descriptor);
|
||||||
free(server);
|
free(server);
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
@@ -52,12 +53,12 @@ void server_delete(Server **server) {
|
|||||||
*server = NULL;
|
*server = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
bool server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
||||||
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
||||||
server->address.sin_port);
|
server->address.sin_port);
|
||||||
if (listen(server->file_descriptor, 3) < 0) {
|
if (listen(server->file_descriptor, SOMAXCONN) < 0) {
|
||||||
perror("Could not listen on port!");
|
perror("Could not listen on port!");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int file_descriptor =
|
int file_descriptor =
|
||||||
@@ -65,22 +66,27 @@ void server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
|||||||
&server->address_length);
|
&server->address_length);
|
||||||
if (file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not accept the connection");
|
perror("Could not accept the connection");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
log_message(LOG_LEVEL_INFO, "Received Connection");
|
log_message(LOG_LEVEL_INFO, "Received Connection");
|
||||||
handler(file_descriptor);
|
handler(file_descriptor);
|
||||||
close(server->file_descriptor);
|
close(server->file_descriptor);
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client *client_create() {
|
Client *client_create() {
|
||||||
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (file_descriptor < 0) {
|
if (file_descriptor < 0) {
|
||||||
perror("Could not create Socket!");
|
perror("Could not create Socket!");
|
||||||
exit(EXIT_FAILURE);
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client *client = (Client *)malloc(sizeof(Client));
|
Client *client = (Client *)malloc(sizeof(Client));
|
||||||
|
if (client == NULL) {
|
||||||
|
close(file_descriptor);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
client->file_descriptor = file_descriptor;
|
client->file_descriptor = file_descriptor;
|
||||||
client->address.sin_family = AF_INET;
|
client->address.sin_family = AF_INET;
|
||||||
client->address_length = sizeof(client->address);
|
client->address_length = sizeof(client->address);
|
||||||
@@ -88,19 +94,20 @@ Client *client_create() {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_connect(Client *client, char *host, int port) {
|
bool client_connect(Client *client, char *host, int port) {
|
||||||
client->address.sin_port = htons(port);
|
client->address.sin_port = htons(port);
|
||||||
|
|
||||||
if (inet_pton(AF_INET, host, &client->address.sin_addr) <= 0) {
|
if (inet_pton(AF_INET, host, &client->address.sin_addr) <= 0) {
|
||||||
perror("Could not convert host address!");
|
perror("Could not convert host address!");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connect(client->file_descriptor, (struct sockaddr *)&client->address,
|
if (connect(client->file_descriptor, (struct sockaddr *)&client->address,
|
||||||
client->address_length) < 0) {
|
client->address_length) < 0) {
|
||||||
perror("Could not connect to Server!");
|
perror("Could not connect to Server!");
|
||||||
exit(EXIT_FAILURE);
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_disconnect(Client *client) {
|
void client_disconnect(Client *client) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define TRANSPORT_TCP_H
|
#define TRANSPORT_TCP_H
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
typedef struct Server {
|
typedef struct Server {
|
||||||
@@ -18,10 +19,10 @@ typedef struct Client {
|
|||||||
} Client;
|
} Client;
|
||||||
|
|
||||||
Server *server_create(int port);
|
Server *server_create(int port);
|
||||||
void server_listen(Server *server, void (*handler)(int file_descriptor));
|
bool server_listen(Server *server, void (*handler)(int file_descriptor));
|
||||||
void server_delete(Server **server);
|
void server_delete(Server **server);
|
||||||
Client *client_create();
|
Client *client_create();
|
||||||
void client_connect(Client *client, char *host, int port);
|
bool client_connect(Client *client, char *host, int port);
|
||||||
void client_disconnect(Client *client);
|
void client_disconnect(Client *client);
|
||||||
void client_delete(Client *client);
|
void client_delete(Client *client);
|
||||||
|
|
||||||
|
|||||||
+8
-2
@@ -8,10 +8,12 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void mkdir_r(char *path) {
|
bool mkdir_r(char *path) {
|
||||||
char *path_duplicate = malloc(strlen(path) + 1);
|
char *path_duplicate = malloc(strlen(path) + 1);
|
||||||
|
if (!path_duplicate) return false;
|
||||||
strcpy(path_duplicate, path);
|
strcpy(path_duplicate, path);
|
||||||
char *path_current = (char *)malloc((strlen(path) + 2) * sizeof(char));
|
char *path_current = (char *)malloc((strlen(path) + 2) * sizeof(char));
|
||||||
|
if (!path_current) { free(path_duplicate); return false; }
|
||||||
char *path_current_position = path_current;
|
char *path_current_position = path_current;
|
||||||
if (path[0] == '/') {
|
if (path[0] == '/') {
|
||||||
strcpy(path_current, "/");
|
strcpy(path_current, "/");
|
||||||
@@ -21,6 +23,7 @@ void mkdir_r(char *path) {
|
|||||||
}
|
}
|
||||||
const char *delimiter = "/";
|
const char *delimiter = "/";
|
||||||
char *part = strtok(path_duplicate, delimiter);
|
char *part = strtok(path_duplicate, delimiter);
|
||||||
|
bool ok = true;
|
||||||
while (part != NULL) {
|
while (part != NULL) {
|
||||||
strcpy(path_current_position, part);
|
strcpy(path_current_position, part);
|
||||||
path_current_position += strlen(part) * sizeof(char);
|
path_current_position += strlen(part) * sizeof(char);
|
||||||
@@ -30,13 +33,15 @@ void mkdir_r(char *path) {
|
|||||||
if (stat(path_current, &st) != 0) {
|
if (stat(path_current, &st) != 0) {
|
||||||
if (mkdir(path_current, 0755) != 0) {
|
if (mkdir(path_current, 0755) != 0) {
|
||||||
perror("Could not create directory");
|
perror("Could not create directory");
|
||||||
exit(EXIT_FAILURE);
|
ok = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
part = strtok(NULL, delimiter);
|
part = strtok(NULL, delimiter);
|
||||||
}
|
}
|
||||||
free(path_duplicate);
|
free(path_duplicate);
|
||||||
free(path_current);
|
free(path_current);
|
||||||
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *str_dup(const char *string) {
|
char *str_dup(const char *string) {
|
||||||
@@ -131,6 +136,7 @@ char *path_cat(char *path1, char *path2) {
|
|||||||
path2_len -= 1;
|
path2_len -= 1;
|
||||||
}
|
}
|
||||||
char *new_path = malloc(path1_len + path2_len + 2);
|
char *new_path = malloc(path1_len + path2_len + 2);
|
||||||
|
if (new_path == NULL) return NULL;
|
||||||
memcpy(new_path, path1, path1_len);
|
memcpy(new_path, path1, path1_len);
|
||||||
new_path[path1_len] = '/';
|
new_path[path1_len] = '/';
|
||||||
memcpy(new_path + path1_len + 1, path2_pointer, path2_len);
|
memcpy(new_path + path1_len + 1, path2_pointer, path2_len);
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
#include "array_list.h"
|
#include "array_list.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
void mkdir_r(char *path);
|
bool mkdir_r(char *path);
|
||||||
char *str_dup(const char *string);
|
char *str_dup(const char *string);
|
||||||
char *path_cat(char *path1, char *path2);
|
char *path_cat(char *path1, char *path2);
|
||||||
bool glob_match(const char *pattern, const char *str);
|
bool glob_match(const char *pattern, const char *str);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static void create_test_file(const char *path, const char *content) {
|
static void create_test_file(const char *path, const char *content) {
|
||||||
to_disk(path, content, strlen(content));
|
(void)to_disk(path, content, strlen(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_scanner_single_file() {
|
static void test_scanner_single_file() {
|
||||||
@@ -18,7 +18,7 @@ static void test_scanner_single_file() {
|
|||||||
mkdir(dir, 0755);
|
mkdir(dir, 0755);
|
||||||
create_test_file(file1, content1);
|
create_test_file(file1, content1);
|
||||||
|
|
||||||
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0);
|
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0, NULL, 0, 0, 0);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk *chunk = directory_scanner_next(scanner);
|
Chunk *chunk = directory_scanner_next(scanner);
|
||||||
@@ -46,7 +46,7 @@ static void test_scanner_multiple_files() {
|
|||||||
create_test_file(file1, content1);
|
create_test_file(file1, content1);
|
||||||
create_test_file(file2, content2);
|
create_test_file(file2, content2);
|
||||||
|
|
||||||
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0);
|
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0, NULL, 0, 0, 0);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk *chunk = directory_scanner_next(scanner);
|
Chunk *chunk = directory_scanner_next(scanner);
|
||||||
@@ -83,7 +83,7 @@ static void test_scanner_subdirectory() {
|
|||||||
create_test_file(root_file, content);
|
create_test_file(root_file, content);
|
||||||
create_test_file(sub_file, content);
|
create_test_file(sub_file, content);
|
||||||
|
|
||||||
DirectoryScanner *scanner = directory_scanner_create((char *)root, false, 0, NULL, 0);
|
DirectoryScanner *scanner = directory_scanner_create((char *)root, false, 0, NULL, 0, NULL, 0, 0, 0);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
int total_files = 0;
|
int total_files = 0;
|
||||||
@@ -106,7 +106,7 @@ static void test_scanner_empty_directory() {
|
|||||||
|
|
||||||
mkdir(dir, 0755);
|
mkdir(dir, 0755);
|
||||||
|
|
||||||
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0);
|
DirectoryScanner *scanner = directory_scanner_create((char *)dir, false, 0, NULL, 0, NULL, 0, 0, 0);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk *chunk = directory_scanner_next(scanner);
|
Chunk *chunk = directory_scanner_next(scanner);
|
||||||
|
|||||||
Reference in New Issue
Block a user