Batch fix: security, bugs, quality, and features (v2) #198
@@ -25,13 +25,16 @@
|
|||||||
|
|
||||||
#define STREAM_THRESHOLD (64ULL * 1024 * 1024)
|
#define STREAM_THRESHOLD (64ULL * 1024 * 1024)
|
||||||
|
|
||||||
|
/* Forward declaration for progress-reporting thread used in multithreaded send. */
|
||||||
|
static int progress_thread_fn(void* arg);
|
||||||
|
|
||||||
/* Print dry-run manifest showing files that would be transferred. Returns 0 on success. */
|
/* Print dry-run manifest showing files that would be transferred. Returns 0 on success. */
|
||||||
static int send_dry_run_manifest(Config* config) {
|
static int send_dry_run_manifest(Config* config) {
|
||||||
DirectoryScanner* scanner = directory_scanner_create(
|
DirectoryScanner* scanner = directory_scanner_create(
|
||||||
config->send_directory, config->use_metadata, config->chunk_size, config->exclude_patterns,
|
config->send_directory, config->use_metadata, config->chunk_size, config->exclude_patterns,
|
||||||
config->exclude_count, config->include_patterns, config->include_count, config->max_size,
|
config->exclude_count, config->include_patterns, config->include_count, config->max_size,
|
||||||
config->min_size, config->max_depth, config->follow_symlinks, config->copy_links,
|
config->min_size, config->max_depth, config->follow_symlinks, config->copy_links,
|
||||||
config->safe_links, config->copy_unsafe_links);
|
config->safe_links, config->copy_unsafe_links, false);
|
||||||
if (!scanner)
|
if (!scanner)
|
||||||
return -1;
|
return -1;
|
||||||
Chunk* chunk;
|
Chunk* chunk;
|
||||||
@@ -163,7 +166,7 @@ static int send_single_file(Client* client, File* file, Config* config, bool use
|
|||||||
if (!use_incremental) {
|
if (!use_incremental) {
|
||||||
if (use_sendfile) {
|
if (use_sendfile) {
|
||||||
return send_file_direct_sendfile(file, client->file_descriptor, config->use_metadata) ? 0
|
return send_file_direct_sendfile(file, client->file_descriptor, config->use_metadata) ? 0
|
||||||
: -1;
|
: -1;
|
||||||
}
|
}
|
||||||
return send_file_direct(file, client->file_descriptor, config->use_metadata, compression_level)
|
return send_file_direct(file, client->file_descriptor, config->use_metadata, compression_level)
|
||||||
? 0
|
? 0
|
||||||
@@ -271,6 +274,9 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
|||||||
if (context->config->transport == TRANSPORT_SSH) {
|
if (context->config->transport == TRANSPORT_SSH) {
|
||||||
if (context->config->use_sendfile) {
|
if (context->config->use_sendfile) {
|
||||||
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
|
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
client = client_connect_ssh(context->config->ssh_destination, context->config->ssh_port,
|
client = client_connect_ssh(context->config->ssh_destination, context->config->ssh_port,
|
||||||
@@ -278,11 +284,14 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
|||||||
} else if (context->config->use_tls) {
|
} else if (context->config->use_tls) {
|
||||||
client = client_create();
|
client = client_create();
|
||||||
if (!client || !client_connect_tls(client, context->config->server_host,
|
if (!client || !client_connect_tls(client, context->config->server_host,
|
||||||
context->config->server_port, context->config->tls_cert,
|
context->config->server_port, context->config->tls_cert,
|
||||||
context->config->tls_key, context->config->tls_ca)) {
|
context->config->tls_key, context->config->tls_ca)) {
|
||||||
if (client)
|
if (client)
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return thrd_error;
|
return thrd_error;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -292,12 +301,18 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
|||||||
if (client)
|
if (client)
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
fprintf(stderr, "Error: could not connect to server\n");
|
fprintf(stderr, "Error: could not connect to server\n");
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return thrd_error;
|
return thrd_error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!config_send(client->file_descriptor, context->config)) {
|
if (!config_send(client->file_descriptor, context->config)) {
|
||||||
client_disconnect(client);
|
client_disconnect(client);
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return thrd_error;
|
return thrd_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,19 +331,38 @@ static int send_chunks_multithreaded(void* pipeline_context) {
|
|||||||
int ok = receive_status(client->file_descriptor, &s) && s == STATUS_OK;
|
int ok = receive_status(client->file_descriptor, &s) && s == STATUS_OK;
|
||||||
client_disconnect(client);
|
client_disconnect(client);
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return ok ? thrd_success : thrd_error;
|
return ok ? thrd_success : thrd_error;
|
||||||
|
|
||||||
send_fail:
|
send_fail:
|
||||||
client_disconnect(client);
|
client_disconnect(client);
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return thrd_error;
|
return thrd_error;
|
||||||
}
|
}
|
||||||
if (send_chunk(client, current_chunk, context->config) != 0) {
|
if (send_chunk(client, current_chunk, context->config) != 0) {
|
||||||
fprintf(stderr, "Error: unexpected error while sending chunk\n");
|
fprintf(stderr, "Error: unexpected error while sending chunk\n");
|
||||||
client_disconnect(client);
|
client_disconnect(client);
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
return thrd_error;
|
return thrd_error;
|
||||||
}
|
}
|
||||||
|
if (context->config->show_progress) {
|
||||||
|
unsigned long long chunk_bytes = 0;
|
||||||
|
for (int i = 0; i < current_chunk->element_count; i++) {
|
||||||
|
if (current_chunk->items[i] && current_chunk->items[i]->data)
|
||||||
|
chunk_bytes += current_chunk->items[i]->data->size;
|
||||||
|
}
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->progress_bytes += chunk_bytes;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
|
}
|
||||||
chunk_destroy(current_chunk);
|
chunk_destroy(current_chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -340,7 +374,7 @@ static int scan_directory_multithreaded(void* pipeline_context) {
|
|||||||
context->config->exclude_patterns, context->config->exclude_count,
|
context->config->exclude_patterns, context->config->exclude_count,
|
||||||
context->config->include_patterns, context->config->include_count, context->config->max_size,
|
context->config->include_patterns, context->config->include_count, context->config->max_size,
|
||||||
context->config->min_size, context->config->max_depth, 4, context->config->follow_symlinks,
|
context->config->min_size, context->config->max_depth, 4, context->config->follow_symlinks,
|
||||||
context->config->copy_links, context->config->safe_links, context->config->copy_unsafe_links);
|
context->config->copy_links, context->config->safe_links, context->config->copy_unsafe_links, false);
|
||||||
|
|
||||||
Chunk* current_chunk;
|
Chunk* current_chunk;
|
||||||
while ((current_chunk = parallel_scanner_next(scanner)) != NULL) {
|
while ((current_chunk = parallel_scanner_next(scanner)) != NULL) {
|
||||||
@@ -398,6 +432,42 @@ static int load_files_multithreaded(void* pipeline_context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Progress-reporting thread for multithreaded send. Runs in parallel with
|
||||||
|
the scanner/loader/sender threads and prints periodic progress to stderr. */
|
||||||
|
static int progress_thread_fn(void* arg) {
|
||||||
|
PipelineContextSender* context = (PipelineContextSender*)arg;
|
||||||
|
time_t last_progress = 0;
|
||||||
|
time_t start = time(NULL);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
bool done = context->sender_done;
|
||||||
|
unsigned long long total = context->progress_bytes;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
time_t now = time(NULL);
|
||||||
|
double elapsed = difftime(now, start);
|
||||||
|
double rate = elapsed > 0.0 ? total / (1048576.0 * elapsed) : 0.0;
|
||||||
|
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) Done.\n", total / 1048576.0, rate);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
time_t now = time(NULL);
|
||||||
|
if (now - last_progress >= 1) {
|
||||||
|
last_progress = now;
|
||||||
|
double elapsed = difftime(now, start);
|
||||||
|
double rate = elapsed > 0.0 ? total / (1048576.0 * elapsed) : 0.0;
|
||||||
|
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) ", total / 1048576.0, rate);
|
||||||
|
fflush(stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct timespec ts = {0, 100 * 1000000L}; /* 100 ms */
|
||||||
|
thrd_sleep(&ts, NULL);
|
||||||
|
}
|
||||||
|
return thrd_success;
|
||||||
|
}
|
||||||
|
|
||||||
int send_files(Config* config) {
|
int send_files(Config* config) {
|
||||||
if (config->dry_run)
|
if (config->dry_run)
|
||||||
return send_dry_run_manifest(config);
|
return send_dry_run_manifest(config);
|
||||||
@@ -415,7 +485,7 @@ int send_files(Config* config) {
|
|||||||
} else if (config->use_tls) {
|
} else if (config->use_tls) {
|
||||||
client = client_create();
|
client = client_create();
|
||||||
if (!client || !client_connect_tls(client, config->server_host, config->server_port,
|
if (!client || !client_connect_tls(client, config->server_host, config->server_port,
|
||||||
config->tls_cert, config->tls_key, config->tls_ca)) {
|
config->tls_cert, config->tls_key, config->tls_ca)) {
|
||||||
if (client)
|
if (client)
|
||||||
client_delete(client);
|
client_delete(client);
|
||||||
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
||||||
@@ -439,7 +509,7 @@ int send_files(Config* config) {
|
|||||||
config->send_directory, config->use_metadata, config->chunk_size, config->exclude_patterns,
|
config->send_directory, config->use_metadata, config->chunk_size, config->exclude_patterns,
|
||||||
config->exclude_count, config->include_patterns, config->include_count, config->max_size,
|
config->exclude_count, config->include_patterns, config->include_count, config->max_size,
|
||||||
config->min_size, config->max_depth, config->follow_symlinks, config->copy_links,
|
config->min_size, config->max_depth, config->follow_symlinks, config->copy_links,
|
||||||
config->safe_links, config->copy_unsafe_links);
|
config->safe_links, config->copy_unsafe_links, false);
|
||||||
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;
|
||||||
@@ -547,7 +617,7 @@ int send_files_multithreaded(Config* config) {
|
|||||||
if (config->use_delete)
|
if (config->use_delete)
|
||||||
context->manifest = array_list_create(free);
|
context->manifest = array_list_create(free);
|
||||||
|
|
||||||
thrd_t scanner, loader, sender;
|
thrd_t scanner, loader, sender, progress;
|
||||||
if (thrd_create(&scanner, scan_directory_multithreaded, context) != thrd_success ||
|
if (thrd_create(&scanner, scan_directory_multithreaded, context) != thrd_success ||
|
||||||
thrd_create(&loader, load_files_multithreaded, context) != thrd_success ||
|
thrd_create(&loader, load_files_multithreaded, context) != thrd_success ||
|
||||||
thrd_create(&sender, send_chunks_multithreaded, context) != thrd_success) {
|
thrd_create(&sender, send_chunks_multithreaded, context) != thrd_success) {
|
||||||
@@ -556,11 +626,26 @@ int send_files_multithreaded(Config* config) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config->show_progress) {
|
||||||
|
if (thrd_create(&progress, progress_thread_fn, context) != thrd_success) {
|
||||||
|
perror("Error creating progress thread.\n");
|
||||||
|
/* Non-fatal; continue without progress reporting */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int sender_result;
|
int sender_result;
|
||||||
thrd_join(scanner, NULL);
|
thrd_join(scanner, NULL);
|
||||||
thrd_join(loader, NULL);
|
thrd_join(loader, NULL);
|
||||||
thrd_join(sender, &sender_result);
|
thrd_join(sender, &sender_result);
|
||||||
|
|
||||||
|
if (config->show_progress) {
|
||||||
|
/* Signal progress thread to exit if it hasn't already */
|
||||||
|
mtx_lock(&context->mutex_progress);
|
||||||
|
context->sender_done = true;
|
||||||
|
mtx_unlock(&context->mutex_progress);
|
||||||
|
thrd_join(progress, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
pipeline_context_sender_destroy(context);
|
pipeline_context_sender_destroy(context);
|
||||||
return sender_result == thrd_success ? 0 : 1;
|
return sender_result == thrd_success ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-8
@@ -40,7 +40,7 @@ DirectoryScanner* directory_scanner_create(const char* root_directory, bool use_
|
|||||||
int include_count, unsigned long long max_size,
|
int include_count, unsigned long long max_size,
|
||||||
unsigned long long min_size, int max_depth,
|
unsigned long long min_size, int max_depth,
|
||||||
bool follow_symlinks, bool copy_links, bool safe_links,
|
bool follow_symlinks, bool copy_links, bool safe_links,
|
||||||
bool copy_unsafe_links) {
|
bool copy_unsafe_links, bool checksum) {
|
||||||
DirectoryScanner* scanner = malloc(sizeof(DirectoryScanner));
|
DirectoryScanner* scanner = malloc(sizeof(DirectoryScanner));
|
||||||
if (scanner == NULL)
|
if (scanner == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -61,6 +61,7 @@ DirectoryScanner* directory_scanner_create(const char* root_directory, bool use_
|
|||||||
scanner->copy_links = copy_links;
|
scanner->copy_links = copy_links;
|
||||||
scanner->safe_links = safe_links;
|
scanner->safe_links = safe_links;
|
||||||
scanner->copy_unsafe_links = copy_unsafe_links;
|
scanner->copy_unsafe_links = copy_unsafe_links;
|
||||||
|
scanner->checksum = checksum;
|
||||||
queue_enqueue(scanner->directories, dir_entry_create(root_directory, 0));
|
queue_enqueue(scanner->directories, dir_entry_create(root_directory, 0));
|
||||||
return scanner;
|
return scanner;
|
||||||
}
|
}
|
||||||
@@ -276,6 +277,7 @@ typedef struct {
|
|||||||
bool copy_links;
|
bool copy_links;
|
||||||
bool safe_links;
|
bool safe_links;
|
||||||
bool copy_unsafe_links;
|
bool copy_unsafe_links;
|
||||||
|
bool checksum;
|
||||||
} ParallelWorkerArg;
|
} ParallelWorkerArg;
|
||||||
|
|
||||||
static int parallel_worker_thread(void* arg) {
|
static int parallel_worker_thread(void* arg) {
|
||||||
@@ -284,7 +286,7 @@ static int parallel_worker_thread(void* arg) {
|
|||||||
DirectoryScanner* ds = directory_scanner_create(
|
DirectoryScanner* ds = directory_scanner_create(
|
||||||
wa->dirs[i], wa->use_metadata, wa->chunk_size, wa->exclude_patterns, wa->exclude_count,
|
wa->dirs[i], wa->use_metadata, wa->chunk_size, wa->exclude_patterns, wa->exclude_count,
|
||||||
wa->include_patterns, wa->include_count, wa->max_size, wa->min_size, wa->max_depth,
|
wa->include_patterns, wa->include_count, wa->max_size, wa->min_size, wa->max_depth,
|
||||||
wa->follow_symlinks, wa->copy_links, wa->safe_links, wa->copy_unsafe_links);
|
wa->follow_symlinks, wa->copy_links, wa->safe_links, wa->copy_unsafe_links, wa->checksum);
|
||||||
Chunk* chunk;
|
Chunk* chunk;
|
||||||
while ((chunk = directory_scanner_next(ds)) != NULL) {
|
while ((chunk = directory_scanner_next(ds)) != NULL) {
|
||||||
queue_enqueue_multithreaded(wa->ps->result_queue, chunk, &wa->ps->result_mutex,
|
queue_enqueue_multithreaded(wa->ps->result_queue, chunk, &wa->ps->result_mutex,
|
||||||
@@ -307,12 +309,12 @@ static int parallel_worker_thread(void* arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata,
|
ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata,
|
||||||
unsigned long long chunk_size, char** exclude_patterns,
|
unsigned long long chunk_size, char** exclude_patterns,
|
||||||
int exclude_count, char** include_patterns,
|
int exclude_count, char** include_patterns,
|
||||||
int include_count, unsigned long long max_size,
|
int include_count, unsigned long long max_size,
|
||||||
unsigned long long min_size, int max_depth,
|
unsigned long long min_size, int max_depth,
|
||||||
int num_threads, bool follow_symlinks, bool copy_links,
|
int num_threads, bool follow_symlinks, bool copy_links,
|
||||||
bool safe_links, bool copy_unsafe_links) {
|
bool safe_links, bool copy_unsafe_links, bool checksum) {
|
||||||
ParallelScanner* ps = calloc(1, sizeof(ParallelScanner));
|
ParallelScanner* ps = calloc(1, sizeof(ParallelScanner));
|
||||||
if (!ps)
|
if (!ps)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -475,6 +477,7 @@ ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata
|
|||||||
wa->copy_links = copy_links;
|
wa->copy_links = copy_links;
|
||||||
wa->safe_links = safe_links;
|
wa->safe_links = safe_links;
|
||||||
wa->copy_unsafe_links = copy_unsafe_links;
|
wa->copy_unsafe_links = copy_unsafe_links;
|
||||||
|
wa->checksum = checksum;
|
||||||
start += count;
|
start += count;
|
||||||
if (thrd_create(&ps->threads[t], parallel_worker_thread, wa) != thrd_success) {
|
if (thrd_create(&ps->threads[t], parallel_worker_thread, wa) != thrd_success) {
|
||||||
for (int j = 0; j < count; j++)
|
for (int j = 0; j < count; j++)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ typedef struct {
|
|||||||
bool copy_links;
|
bool copy_links;
|
||||||
bool safe_links;
|
bool safe_links;
|
||||||
bool copy_unsafe_links;
|
bool copy_unsafe_links;
|
||||||
|
bool checksum;
|
||||||
} DirectoryScanner;
|
} DirectoryScanner;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -45,17 +46,17 @@ DirectoryScanner* directory_scanner_create(const char* root_directory, bool use_
|
|||||||
int include_count, unsigned long long max_size,
|
int include_count, unsigned long long max_size,
|
||||||
unsigned long long min_size, int max_depth,
|
unsigned long long min_size, int max_depth,
|
||||||
bool follow_symlinks, bool copy_links, bool safe_links,
|
bool follow_symlinks, bool copy_links, bool safe_links,
|
||||||
bool copy_unsafe_links);
|
bool copy_unsafe_links, bool checksum);
|
||||||
Chunk* directory_scanner_next(DirectoryScanner* scanner);
|
Chunk* directory_scanner_next(DirectoryScanner* scanner);
|
||||||
void directory_scanner_destroy(DirectoryScanner* scanner);
|
void directory_scanner_destroy(DirectoryScanner* scanner);
|
||||||
|
|
||||||
ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata,
|
ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata,
|
||||||
unsigned long long chunk_size, char** exclude_patterns,
|
unsigned long long chunk_size, char** exclude_patterns,
|
||||||
int exclude_count, char** include_patterns,
|
int exclude_count, char** include_patterns,
|
||||||
int include_count, unsigned long long max_size,
|
int include_count, unsigned long long max_size,
|
||||||
unsigned long long min_size, int max_depth,
|
unsigned long long min_size, int max_depth,
|
||||||
int num_threads, bool follow_symlinks, bool copy_links,
|
int num_threads, bool follow_symlinks, bool copy_links,
|
||||||
bool safe_links, bool copy_unsafe_links);
|
bool safe_links, bool copy_unsafe_links, bool checksum);
|
||||||
Chunk* parallel_scanner_next(ParallelScanner* scanner);
|
Chunk* parallel_scanner_next(ParallelScanner* scanner);
|
||||||
void parallel_scanner_destroy(ParallelScanner* scanner);
|
void parallel_scanner_destroy(ParallelScanner* scanner);
|
||||||
|
|
||||||
|
|||||||
@@ -89,6 +89,18 @@ Config* config_create(void) {
|
|||||||
config->compare_dest = NULL;
|
config->compare_dest = NULL;
|
||||||
config->copy_dest = NULL;
|
config->copy_dest = NULL;
|
||||||
config->link_dest = NULL;
|
config->link_dest = NULL;
|
||||||
|
config->partial_dir = NULL;
|
||||||
|
config->suffix = NULL;
|
||||||
|
config->delete_before = false;
|
||||||
|
config->address = NULL;
|
||||||
|
config->bind_address = NULL;
|
||||||
|
config->ipv6 = false;
|
||||||
|
config->ipv4 = false;
|
||||||
|
config->daemon = false;
|
||||||
|
config->daemon_config = NULL;
|
||||||
|
config->server_mode = false;
|
||||||
|
config->checksum = false;
|
||||||
|
config->compress_choice = NULL;
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,9 +152,18 @@ void config_delete(Config* config) {
|
|||||||
free(config->rsh_command);
|
free(config->rsh_command);
|
||||||
free(config->rsync_path);
|
free(config->rsync_path);
|
||||||
free(config->temp_dir);
|
free(config->temp_dir);
|
||||||
|
free(config->partial_dir);
|
||||||
|
free(config->suffix);
|
||||||
|
free(config->compress_choice);
|
||||||
free(config->compare_dest);
|
free(config->compare_dest);
|
||||||
free(config->copy_dest);
|
free(config->copy_dest);
|
||||||
free(config->link_dest);
|
free(config->link_dest);
|
||||||
|
free(config->partial_dir);
|
||||||
|
free(config->suffix);
|
||||||
|
free(config->address);
|
||||||
|
free(config->bind_address);
|
||||||
|
free(config->daemon_config);
|
||||||
|
free(config->compress_choice);
|
||||||
if (config->filters) {
|
if (config->filters) {
|
||||||
array_list_delete(config->filters);
|
array_list_delete(config->filters);
|
||||||
}
|
}
|
||||||
@@ -225,6 +246,18 @@ bool config_send(int file_descriptor, const Config* config) {
|
|||||||
if (!send_str(file_descriptor, config->temp_dir ? config->temp_dir : ""))
|
if (!send_str(file_descriptor, config->temp_dir ? config->temp_dir : ""))
|
||||||
return false;
|
return false;
|
||||||
Status status;
|
Status status;
|
||||||
|
if (!send_int(file_descriptor, config->partial))
|
||||||
|
return false;
|
||||||
|
if (!send_str(file_descriptor, config->partial_dir ? config->partial_dir : ""))
|
||||||
|
return false;
|
||||||
|
if (!send_str(file_descriptor, config->suffix ? config->suffix : ""))
|
||||||
|
return false;
|
||||||
|
if (!send_int(file_descriptor, config->delete_before))
|
||||||
|
return false;
|
||||||
|
if (!send_int(file_descriptor, config->checksum))
|
||||||
|
return false;
|
||||||
|
if (!send_str(file_descriptor, config->compress_choice ? config->compress_choice : ""))
|
||||||
|
return false;
|
||||||
if (!receive_status(file_descriptor, &status))
|
if (!receive_status(file_descriptor, &status))
|
||||||
return false;
|
return false;
|
||||||
if (status != STATUS_OK) {
|
if (status != STATUS_OK) {
|
||||||
@@ -359,6 +392,18 @@ Config* config_receive(int file_descriptor) {
|
|||||||
config->compare_dest = NULL;
|
config->compare_dest = NULL;
|
||||||
config->copy_dest = NULL;
|
config->copy_dest = NULL;
|
||||||
config->link_dest = NULL;
|
config->link_dest = NULL;
|
||||||
|
config->partial_dir = NULL;
|
||||||
|
config->suffix = NULL;
|
||||||
|
config->delete_before = false;
|
||||||
|
config->address = NULL;
|
||||||
|
config->bind_address = NULL;
|
||||||
|
config->ipv6 = false;
|
||||||
|
config->ipv4 = false;
|
||||||
|
config->daemon = false;
|
||||||
|
config->daemon_config = NULL;
|
||||||
|
config->server_mode = false;
|
||||||
|
config->checksum = false;
|
||||||
|
config->compress_choice = NULL;
|
||||||
if (!receive_int(file_descriptor, &tmp))
|
if (!receive_int(file_descriptor, &tmp))
|
||||||
goto error;
|
goto error;
|
||||||
config->backup = tmp;
|
config->backup = tmp;
|
||||||
@@ -434,6 +479,9 @@ error:
|
|||||||
free(config->server_host);
|
free(config->server_host);
|
||||||
free(config->backup_dir);
|
free(config->backup_dir);
|
||||||
free(config->temp_dir);
|
free(config->temp_dir);
|
||||||
|
free(config->partial_dir);
|
||||||
|
free(config->suffix);
|
||||||
|
free(config->compress_choice);
|
||||||
free(config);
|
free(config);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,32 @@ typedef struct Config {
|
|||||||
char* compare_dest;
|
char* compare_dest;
|
||||||
char* copy_dest;
|
char* copy_dest;
|
||||||
char* link_dest;
|
char* link_dest;
|
||||||
|
|
||||||
|
// PR #174: Partial transfer resumption
|
||||||
|
char* partial_dir;
|
||||||
|
|
||||||
|
// PR #178: Backup versioning
|
||||||
|
char* suffix;
|
||||||
|
|
||||||
|
// PR #179: Delete policies
|
||||||
|
bool delete_before;
|
||||||
|
|
||||||
|
// PR #181: IPv6 and bind address
|
||||||
|
char* address;
|
||||||
|
char* bind_address;
|
||||||
|
bool ipv6;
|
||||||
|
bool ipv4;
|
||||||
|
|
||||||
|
// PR #182: Daemon/server mode
|
||||||
|
bool daemon;
|
||||||
|
char* daemon_config;
|
||||||
|
bool server_mode;
|
||||||
|
|
||||||
|
// PR #183: Checksum comparison
|
||||||
|
bool checksum;
|
||||||
|
|
||||||
|
// PR #184: Compression algorithm negotiation
|
||||||
|
char* compress_choice;
|
||||||
} Config;
|
} Config;
|
||||||
|
|
||||||
#define PROTOCOL_VERSION "1.3.0"
|
#define PROTOCOL_VERSION "1.3.0"
|
||||||
|
|||||||
+102
-21
@@ -129,22 +129,28 @@ bool file_send_single_calls(File* file, int file_descriptor, bool use_metadata,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool file_save_to_disk(const char* root_directory, File* file, const Config* config) {
|
bool file_save_to_disk(const char* root_directory, File* file, const Config* config) {
|
||||||
(void)config;
|
bool backup_enabled = config && config->backup;
|
||||||
|
bool inplace = config && config->inplace;
|
||||||
|
bool sparse = config && config->preserve_sparse;
|
||||||
|
const char* backup_suffix = (config && config->suffix) ? config->suffix : "~";
|
||||||
|
const char* backup_dir = (config && config->backup_dir) ? config->backup_dir : NULL;
|
||||||
|
const char* partial_dir = (config && config->partial_dir) ? config->partial_dir : NULL;
|
||||||
|
|
||||||
if (has_path_traversal(file->path)) {
|
if (has_path_traversal(file->path)) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Path traversal detected in file path: %s", file->path);
|
log_message(LOG_LEVEL_ERROR, "Path traversal detected in file path: %s", file->path);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve the destination root to its real path, preventing symlink-based escapes.
|
char* resolved_root = NULL;
|
||||||
// If the root does not yet exist, try to create it so realpath can succeed.
|
const char* actual_root = (partial_dir && config && config->partial) ? partial_dir : root_directory;
|
||||||
char* resolved_root = realpath(root_directory, NULL);
|
resolved_root = realpath(actual_root, NULL);
|
||||||
if (resolved_root == NULL) {
|
if (resolved_root == NULL) {
|
||||||
if (mkdir_r(root_directory)) {
|
if (mkdir_r(actual_root)) {
|
||||||
resolved_root = realpath(root_directory, NULL);
|
resolved_root = realpath(actual_root, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (resolved_root == NULL) {
|
if (resolved_root == NULL) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Failed to resolve destination root: %s", root_directory);
|
log_message(LOG_LEVEL_ERROR, "Failed to resolve destination root: %s", actual_root);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +160,43 @@ bool file_save_to_disk(const char* root_directory, File* file, const Config* con
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the target directory exists so the parent can be resolved for path safety.
|
if (backup_enabled) {
|
||||||
|
struct stat backup_stat;
|
||||||
|
if (stat(disk_path, &backup_stat) == 0) {
|
||||||
|
char* backup_path = NULL;
|
||||||
|
if (backup_dir) {
|
||||||
|
char* resolved_backup_dir = realpath(backup_dir, NULL);
|
||||||
|
if (!resolved_backup_dir) {
|
||||||
|
mkdir_r(backup_dir);
|
||||||
|
resolved_backup_dir = realpath(backup_dir, NULL);
|
||||||
|
}
|
||||||
|
if (resolved_backup_dir) {
|
||||||
|
backup_path = path_cat(resolved_backup_dir, file->path);
|
||||||
|
free(resolved_backup_dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!backup_path) {
|
||||||
|
size_t path_len = strlen(disk_path);
|
||||||
|
size_t suffix_len = strlen(backup_suffix);
|
||||||
|
backup_path = malloc(path_len + suffix_len + 1);
|
||||||
|
if (backup_path) {
|
||||||
|
memcpy(backup_path, disk_path, path_len);
|
||||||
|
memcpy(backup_path + path_len, backup_suffix, suffix_len + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (backup_path) {
|
||||||
|
char* backup_dir_path = str_dup(backup_path);
|
||||||
|
if (backup_dir_path) {
|
||||||
|
char* bdir = dirname(backup_dir_path);
|
||||||
|
mkdir_r(bdir);
|
||||||
|
free(backup_dir_path);
|
||||||
|
}
|
||||||
|
rename(disk_path, backup_path);
|
||||||
|
free(backup_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char* dir_dup = str_dup(disk_path);
|
char* dir_dup = str_dup(disk_path);
|
||||||
if (!dir_dup) {
|
if (!dir_dup) {
|
||||||
free(resolved_root);
|
free(resolved_root);
|
||||||
@@ -162,7 +204,6 @@ bool file_save_to_disk(const char* root_directory, File* file, const Config* con
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
char* dir_str = dirname(dir_dup);
|
char* dir_str = dirname(dir_dup);
|
||||||
// Create the directory if needed (no-op if it already exists) so realpath can resolve it.
|
|
||||||
if (!mkdir_r(dir_str)) {
|
if (!mkdir_r(dir_str)) {
|
||||||
free(dir_dup);
|
free(dir_dup);
|
||||||
free(resolved_root);
|
free(resolved_root);
|
||||||
@@ -178,13 +219,11 @@ bool file_save_to_disk(const char* root_directory, File* file, const Config* con
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the resolved directory is inside the resolved root.
|
|
||||||
// Both are canonical absolute paths — this prevents symlink-based escapes.
|
|
||||||
size_t root_len = strlen(resolved_root);
|
size_t root_len = strlen(resolved_root);
|
||||||
if (strncmp(resolved_dir, resolved_root, root_len) != 0 ||
|
if (strncmp(resolved_dir, resolved_root, root_len) != 0 ||
|
||||||
(resolved_dir[root_len] != '\0' && resolved_dir[root_len] != '/')) {
|
(resolved_dir[root_len] != '\0' && resolved_dir[root_len] != '/')) {
|
||||||
log_message(LOG_LEVEL_ERROR, "Path escape detected: %s is outside %s", disk_path,
|
log_message(LOG_LEVEL_ERROR, "Path escape detected: %s is outside %s", disk_path,
|
||||||
root_directory);
|
actual_root);
|
||||||
free(resolved_dir);
|
free(resolved_dir);
|
||||||
free(resolved_root);
|
free(resolved_root);
|
||||||
free(disk_path);
|
free(disk_path);
|
||||||
@@ -193,7 +232,7 @@ bool file_save_to_disk(const char* root_directory, File* file, const Config* con
|
|||||||
free(resolved_dir);
|
free(resolved_dir);
|
||||||
free(resolved_root);
|
free(resolved_root);
|
||||||
|
|
||||||
bool ok = to_disk(disk_path, file->data->data, file->data->size);
|
bool ok = to_disk(disk_path, file->data->data, file->data->size, inplace, sparse);
|
||||||
if (ok)
|
if (ok)
|
||||||
file_restore_metadata(disk_path, file->metadata);
|
file_restore_metadata(disk_path, file->metadata);
|
||||||
free(disk_path);
|
free(disk_path);
|
||||||
@@ -478,7 +517,8 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool 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,
|
||||||
|
bool inplace, bool sparse) {
|
||||||
char* tmp_path = NULL;
|
char* tmp_path = NULL;
|
||||||
char* directory = NULL;
|
char* directory = NULL;
|
||||||
|
|
||||||
@@ -495,6 +535,39 @@ bool to_disk(const char* path, const void* data, unsigned long long data_size) {
|
|||||||
if (!mkdir_r(directory))
|
if (!mkdir_r(directory))
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
|
if (inplace) {
|
||||||
|
FILE* file_pointer = fopen(path, "wb");
|
||||||
|
if (file_pointer == NULL) {
|
||||||
|
perror("Could not open file for inplace write");
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (sparse && data_size > 0) {
|
||||||
|
if (fseek(file_pointer, data_size - 1, SEEK_SET) != 0) {
|
||||||
|
perror("Failed to seek for sparse file");
|
||||||
|
fclose(file_pointer);
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (fwrite("", 1, 1, file_pointer) != 1) {
|
||||||
|
perror("Failed to write sparse file");
|
||||||
|
fclose(file_pointer);
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
rewind(file_pointer);
|
||||||
|
}
|
||||||
|
if (data_size > 0 && fwrite(data, 1, data_size, file_pointer) != data_size) {
|
||||||
|
perror("Failed to write all data to file");
|
||||||
|
fclose(file_pointer);
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
fclose(file_pointer);
|
||||||
|
free(directory);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
size_t path_len = strlen(path);
|
size_t path_len = strlen(path);
|
||||||
tmp_path = malloc(path_len + 5);
|
tmp_path = malloc(path_len + 5);
|
||||||
if (!tmp_path) {
|
if (!tmp_path) {
|
||||||
@@ -510,6 +583,21 @@ bool to_disk(const char* path, const void* data, unsigned long long data_size) {
|
|||||||
ok = false;
|
ok = false;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
if (sparse && data_size > 0) {
|
||||||
|
if (fseek(file_pointer, data_size - 1, SEEK_SET) != 0) {
|
||||||
|
perror("Failed to seek for sparse file");
|
||||||
|
fclose(file_pointer);
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (fwrite("", 1, 1, file_pointer) != 1) {
|
||||||
|
perror("Failed to write sparse file");
|
||||||
|
fclose(file_pointer);
|
||||||
|
ok = false;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
rewind(file_pointer);
|
||||||
|
}
|
||||||
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 temporary file");
|
perror("Failed to write all data to temporary file");
|
||||||
fclose(file_pointer);
|
fclose(file_pointer);
|
||||||
@@ -534,13 +622,6 @@ done:
|
|||||||
|
|
||||||
bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int compression_level,
|
bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int compression_level,
|
||||||
bool send_path) {
|
bool send_path) {
|
||||||
// sendfile is incompatible with compression (kernel zero-copy).
|
|
||||||
// If compression is requested, fall back to the regular send path.
|
|
||||||
// NOTE: This is a safety net only — callers must ensure compression_level == 0
|
|
||||||
// before calling file_send_sendfile. The fallback to file_send_single_calls
|
|
||||||
// preserves the send_path contract, but callers should not rely on it for
|
|
||||||
// correctness (the --sendfile flag is validated to be mutually exclusive with
|
|
||||||
// -c/--compress at the CLI layer).
|
|
||||||
if (compression_level > 0)
|
if (compression_level > 0)
|
||||||
return file_send_single_calls(file, file_descriptor, use_metadata, compression_level,
|
return file_send_single_calls(file, file_descriptor, use_metadata, compression_level,
|
||||||
send_path);
|
send_path);
|
||||||
|
|||||||
+2
-1
@@ -34,7 +34,8 @@ bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int
|
|||||||
size_t file_content_to_buffer(File* file);
|
size_t file_content_to_buffer(File* file);
|
||||||
FileMetadata* file_metadata_create(const struct stat* stats);
|
FileMetadata* file_metadata_create(const struct stat* stats);
|
||||||
void file_metadata_destroy(void* metadata);
|
void file_metadata_destroy(void* metadata);
|
||||||
bool 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,
|
||||||
|
bool inplace, bool sparse);
|
||||||
bool file_save_to_disk(const char* root_directory, File* file, const Config* config);
|
bool file_save_to_disk(const char* root_directory, File* file, const Config* config);
|
||||||
File* receive_incremental_check(int fd, const Config* config, bool* skipped);
|
File* receive_incremental_check(int fd, const Config* config, bool* skipped);
|
||||||
int receive_manifest(int fd, const Config* config, int* next_status);
|
int receive_manifest(int fd, const Config* config, int* next_status);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
#include <threads.h>
|
#include <threads.h>
|
||||||
|
|
||||||
PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner,
|
PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner,
|
||||||
Queue* queue_loader) {
|
Queue* queue_loader) {
|
||||||
PipelineContextSender* context = malloc(sizeof(PipelineContextSender));
|
PipelineContextSender* context = malloc(sizeof(PipelineContextSender));
|
||||||
if (context == NULL)
|
if (context == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -24,11 +24,14 @@ PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* que
|
|||||||
context->scanner_done = false;
|
context->scanner_done = false;
|
||||||
context->loader_done = false;
|
context->loader_done = false;
|
||||||
context->manifest = NULL;
|
context->manifest = NULL;
|
||||||
|
context->progress_bytes = 0;
|
||||||
|
context->sender_done = false;
|
||||||
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 ||
|
||||||
|
mtx_init(&context->mutex_progress, mtx_plain) != 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");
|
||||||
free(context);
|
free(context);
|
||||||
@@ -50,11 +53,12 @@ void pipeline_context_sender_destroy(PipelineContextSender* context) {
|
|||||||
mtx_destroy(&context->mutex_loader);
|
mtx_destroy(&context->mutex_loader);
|
||||||
cnd_destroy(&context->condition_not_full_loader);
|
cnd_destroy(&context->condition_not_full_loader);
|
||||||
cnd_destroy(&context->condition_not_empty_loader);
|
cnd_destroy(&context->condition_not_empty_loader);
|
||||||
|
mtx_destroy(&context->mutex_progress);
|
||||||
free(context);
|
free(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue,
|
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue,
|
||||||
int file_descriptor, SSL* ssl) {
|
int file_descriptor, SSL* ssl) {
|
||||||
PipelineContextReceiver* context = malloc(sizeof(PipelineContextReceiver));
|
PipelineContextReceiver* context = malloc(sizeof(PipelineContextReceiver));
|
||||||
if (context == NULL)
|
if (context == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ typedef struct {
|
|||||||
cnd_t condition_not_empty_loader;
|
cnd_t condition_not_empty_loader;
|
||||||
bool loader_done;
|
bool loader_done;
|
||||||
ArrayList* manifest;
|
ArrayList* manifest;
|
||||||
|
mtx_t mutex_progress;
|
||||||
|
unsigned long long progress_bytes;
|
||||||
|
bool sender_done;
|
||||||
} PipelineContextSender;
|
} PipelineContextSender;
|
||||||
|
|
||||||
typedef struct PipelineContextReceiver {
|
typedef struct PipelineContextReceiver {
|
||||||
@@ -37,10 +40,10 @@ typedef struct PipelineContextReceiver {
|
|||||||
} PipelineContextReceiver;
|
} PipelineContextReceiver;
|
||||||
|
|
||||||
PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner,
|
PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner,
|
||||||
Queue* queue_loader);
|
Queue* queue_loader);
|
||||||
void pipeline_context_sender_destroy(PipelineContextSender* context);
|
void pipeline_context_sender_destroy(PipelineContextSender* context);
|
||||||
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue_receiver,
|
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue_receiver,
|
||||||
int file_descriptor, SSL* ssl);
|
int file_descriptor, SSL* ssl);
|
||||||
void pipeline_context_receiver_destroy(PipelineContextReceiver* context);
|
void pipeline_context_receiver_destroy(PipelineContextReceiver* context);
|
||||||
int receive_thread(void* pipeline_context);
|
int receive_thread(void* pipeline_context);
|
||||||
int write_thread(void* pipeline_context);
|
int write_thread(void* pipeline_context);
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ Client* client_connect_ssh(const char* destination, int port, const char* server
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
client->file_descriptor = sv[0];
|
client->file_descriptor = sv[0];
|
||||||
client->address.sin_family = AF_UNIX;
|
client->address.ss_family = AF_UNIX;
|
||||||
client->address_length = 0;
|
client->address_length = 0;
|
||||||
client->ssh_child_pid = pid;
|
client->ssh_child_pid = pid;
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
|
|||||||
+50
-22
@@ -3,6 +3,7 @@
|
|||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -143,6 +144,10 @@ void tcp_set_timeouts(int timeout_sec, int contimeout_sec) {
|
|||||||
g_contimeout_sec = contimeout_sec;
|
g_contimeout_sec = contimeout_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tcp_get_contimeout_sec(void) {
|
||||||
|
return g_contimeout_sec;
|
||||||
|
}
|
||||||
|
|
||||||
static void tcp_apply_socket_timeout(int fd) {
|
static void tcp_apply_socket_timeout(int fd) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = g_timeout_sec;
|
tv.tv_sec = g_timeout_sec;
|
||||||
@@ -152,19 +157,12 @@ static void tcp_apply_socket_timeout(int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Client* client_create() {
|
Client* client_create() {
|
||||||
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (file_descriptor < 0) {
|
|
||||||
perror("Could not create Socket!");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Client* client = (Client*)malloc(sizeof(Client));
|
Client* client = (Client*)malloc(sizeof(Client));
|
||||||
if (client == NULL) {
|
if (client == NULL) {
|
||||||
close(file_descriptor);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
client->file_descriptor = file_descriptor;
|
client->file_descriptor = -1;
|
||||||
client->address.sin_family = AF_INET;
|
memset(&client->address, 0, sizeof(client->address));
|
||||||
client->address_length = sizeof(client->address);
|
client->address_length = sizeof(client->address);
|
||||||
client->ssh_child_pid = -1;
|
client->ssh_child_pid = -1;
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
@@ -173,23 +171,50 @@ Client* client_create() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool client_connect(Client* client, char* host, int port) {
|
bool client_connect(Client* client, char* host, int port) {
|
||||||
client->address.sin_port = htons(port);
|
struct addrinfo hints;
|
||||||
client->address.sin_family = AF_INET;
|
struct addrinfo* result;
|
||||||
client->address_length = sizeof(client->address);
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = AF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
|
|
||||||
if (inet_pton(AF_INET, host, &client->address.sin_addr) <= 0) {
|
char port_str[16];
|
||||||
perror("Could not convert host address!");
|
snprintf(port_str, sizeof(port_str), "%d", port);
|
||||||
|
|
||||||
|
int err = getaddrinfo(host, port_str, &hints, &result);
|
||||||
|
if (err != 0 || result == NULL) {
|
||||||
|
fprintf(stderr, "Could not resolve host: %s (%s)\n", host, gai_strerror(err));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timeval ct;
|
struct addrinfo* rp;
|
||||||
ct.tv_sec = g_contimeout_sec;
|
bool connected = false;
|
||||||
ct.tv_usec = 0;
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
||||||
setsockopt(client->file_descriptor, SOL_SOCKET, SO_RCVTIMEO, &ct, sizeof(ct));
|
if (client->file_descriptor >= 0)
|
||||||
setsockopt(client->file_descriptor, SOL_SOCKET, SO_SNDTIMEO, &ct, sizeof(ct));
|
close(client->file_descriptor);
|
||||||
|
|
||||||
if (connect(client->file_descriptor, (struct sockaddr*)&client->address, client->address_length) <
|
client->file_descriptor = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
0) {
|
if (client->file_descriptor < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
struct timeval ct;
|
||||||
|
ct.tv_sec = g_contimeout_sec;
|
||||||
|
ct.tv_usec = 0;
|
||||||
|
setsockopt(client->file_descriptor, SOL_SOCKET, SO_RCVTIMEO, &ct, sizeof(ct));
|
||||||
|
setsockopt(client->file_descriptor, SOL_SOCKET, SO_SNDTIMEO, &ct, sizeof(ct));
|
||||||
|
|
||||||
|
memcpy(&client->address, rp->ai_addr, rp->ai_addrlen);
|
||||||
|
client->address_length = rp->ai_addrlen;
|
||||||
|
|
||||||
|
if (connect(client->file_descriptor, (struct sockaddr*)&client->address,
|
||||||
|
client->address_length) == 0) {
|
||||||
|
connected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeaddrinfo(result);
|
||||||
|
|
||||||
|
if (!connected) {
|
||||||
perror("Could not connect to Server!");
|
perror("Could not connect to Server!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -205,7 +230,10 @@ void client_disconnect(Client* client) {
|
|||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
io_set_ssl(NULL);
|
io_set_ssl(NULL);
|
||||||
}
|
}
|
||||||
close(client->file_descriptor);
|
if (client->file_descriptor >= 0) {
|
||||||
|
close(client->file_descriptor);
|
||||||
|
client->file_descriptor = -1;
|
||||||
|
}
|
||||||
if (client->ssh_child_pid > 0) {
|
if (client->ssh_child_pid > 0) {
|
||||||
int status;
|
int status;
|
||||||
waitpid(client->ssh_child_pid, &status, 0);
|
waitpid(client->ssh_child_pid, &status, 0);
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ typedef struct Server {
|
|||||||
} Server;
|
} Server;
|
||||||
|
|
||||||
typedef struct Client {
|
typedef struct Client {
|
||||||
struct sockaddr_in address;
|
struct sockaddr_storage address;
|
||||||
unsigned int address_length;
|
unsigned int address_length;
|
||||||
int file_descriptor;
|
int file_descriptor;
|
||||||
pid_t ssh_child_pid;
|
pid_t ssh_child_pid;
|
||||||
@@ -33,5 +33,6 @@ 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);
|
||||||
void tcp_set_timeouts(int timeout_sec, int contimeout_sec);
|
void tcp_set_timeouts(int timeout_sec, int contimeout_sec);
|
||||||
|
int tcp_get_contimeout_sec(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "transport_tcp.h"
|
#include "transport_tcp.h"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
@@ -148,13 +149,50 @@ bool server_listen_tls(Server* server, void (*handler)(int file_descriptor)) {
|
|||||||
|
|
||||||
bool client_connect_tls(Client* client, char* host, int port, const char* cert_path,
|
bool client_connect_tls(Client* client, char* host, int port, const char* cert_path,
|
||||||
const char* key_path, const char* ca_path) {
|
const char* key_path, const char* ca_path) {
|
||||||
client->address.sin_port = htons(port);
|
struct addrinfo hints;
|
||||||
if (inet_pton(AF_INET, host, &client->address.sin_addr) <= 0) {
|
struct addrinfo* result;
|
||||||
perror("Could not convert host address!");
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = AF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_protocol = IPPROTO_TCP;
|
||||||
|
|
||||||
|
char port_str[16];
|
||||||
|
snprintf(port_str, sizeof(port_str), "%d", port);
|
||||||
|
|
||||||
|
int err = getaddrinfo(host, port_str, &hints, &result);
|
||||||
|
if (err != 0 || result == NULL) {
|
||||||
|
fprintf(stderr, "Could not resolve host: %s (%s)\n", host, gai_strerror(err));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (connect(client->file_descriptor, (struct sockaddr*)&client->address, client->address_length) <
|
|
||||||
0) {
|
struct addrinfo* rp;
|
||||||
|
bool connected = false;
|
||||||
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
||||||
|
if (client->file_descriptor >= 0)
|
||||||
|
close(client->file_descriptor);
|
||||||
|
|
||||||
|
client->file_descriptor = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
|
if (client->file_descriptor < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
struct timeval ct;
|
||||||
|
ct.tv_sec = tcp_get_contimeout_sec();
|
||||||
|
ct.tv_usec = 0;
|
||||||
|
setsockopt(client->file_descriptor, SOL_SOCKET, SO_RCVTIMEO, &ct, sizeof(ct));
|
||||||
|
setsockopt(client->file_descriptor, SOL_SOCKET, SO_SNDTIMEO, &ct, sizeof(ct));
|
||||||
|
|
||||||
|
memcpy(&client->address, rp->ai_addr, rp->ai_addrlen);
|
||||||
|
client->address_length = rp->ai_addrlen;
|
||||||
|
|
||||||
|
if (connect(client->file_descriptor, (struct sockaddr*)&client->address,
|
||||||
|
client->address_length) == 0) {
|
||||||
|
connected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeaddrinfo(result);
|
||||||
|
|
||||||
|
if (!connected) {
|
||||||
perror("Could not connect to Server!");
|
perror("Could not connect to Server!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-12
@@ -19,7 +19,7 @@ static void test_scanner_single_file() {
|
|||||||
create_test_file(file1, content1);
|
create_test_file(file1, content1);
|
||||||
|
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
||||||
0, false, false, false, false);
|
0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -48,7 +48,7 @@ static void test_scanner_multiple_files() {
|
|||||||
create_test_file(file2, content2);
|
create_test_file(file2, content2);
|
||||||
|
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
||||||
0, false, false, false, false);
|
0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
const Chunk* chunk = directory_scanner_next(scanner);
|
const Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -88,7 +88,7 @@ static void test_scanner_subdirectory() {
|
|||||||
create_test_file(sub_file, content);
|
create_test_file(sub_file, content);
|
||||||
|
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)root, false, 0, NULL, 0, NULL, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)root, false, 0, NULL, 0, NULL, 0, 0,
|
||||||
0, 0, false, false, false, false);
|
0, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
int total_files = 0;
|
int total_files = 0;
|
||||||
@@ -112,7 +112,7 @@ static void test_scanner_empty_directory() {
|
|||||||
EXPECT_EQ_INT(mkdir(dir, 0755), 0);
|
EXPECT_EQ_INT(mkdir(dir, 0755), 0);
|
||||||
|
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
||||||
0, false, false, false, false);
|
0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
const Chunk* chunk = directory_scanner_next(scanner);
|
const Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -136,7 +136,7 @@ static void test_scanner_exclude_pattern() {
|
|||||||
|
|
||||||
char* exclude[] = {"*.tmp"};
|
char* exclude[] = {"*.tmp"};
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, NULL, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, NULL, 0, 0,
|
||||||
0, 0, false, false, false, false);
|
0, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -169,7 +169,7 @@ static void test_scanner_exclude_subdirectory() {
|
|||||||
|
|
||||||
char* exclude[] = {"*.tmp"};
|
char* exclude[] = {"*.tmp"};
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)root, false, 0, exclude, 1, NULL, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)root, false, 0, exclude, 1, NULL, 0,
|
||||||
0, 0, 0, false, false, false, false);
|
0, 0, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
@@ -207,7 +207,7 @@ static void test_scanner_include_and_exclude() {
|
|||||||
char* exclude[] = {"*.bak"};
|
char* exclude[] = {"*.bak"};
|
||||||
char* include[] = {"*.txt", "*.log"};
|
char* include[] = {"*.txt", "*.log"};
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, include, 2,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, include, 2,
|
||||||
0, 0, 0, false, false, false, false);
|
0, 0, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -244,7 +244,7 @@ static void test_scanner_max_size() {
|
|||||||
|
|
||||||
/* max_size = 10 — only files <= 10 bytes */
|
/* max_size = 10 — only files <= 10 bytes */
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 10,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 10,
|
||||||
0, 0, false, false, false, false);
|
0, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -272,7 +272,7 @@ static void test_scanner_min_size() {
|
|||||||
|
|
||||||
/* min_size = 1 — only files >= 1 byte */
|
/* min_size = 1 — only files >= 1 byte */
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 1,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 1,
|
||||||
0, false, false, false, false);
|
0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -302,7 +302,7 @@ static void test_scanner_size_range() {
|
|||||||
|
|
||||||
/* Only files between 3 and 20 bytes */
|
/* Only files between 3 and 20 bytes */
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 20,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 20,
|
||||||
3, 0, false, false, false, false);
|
3, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -338,7 +338,7 @@ static void test_scanner_mixed_patterns() {
|
|||||||
char* exclude[] = {"*.bak"};
|
char* exclude[] = {"*.bak"};
|
||||||
char* include[] = {"*.txt"};
|
char* include[] = {"*.txt"};
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, include, 1,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, exclude, 1, include, 1,
|
||||||
10, 3, 0, false, false, false, false);
|
10, 3, 0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
@@ -369,7 +369,7 @@ static void test_scanner_no_patterns() {
|
|||||||
create_test_file(f2, "second");
|
create_test_file(f2, "second");
|
||||||
|
|
||||||
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
DirectoryScanner* scanner = directory_scanner_create((char*)dir, false, 0, NULL, 0, NULL, 0, 0, 0,
|
||||||
0, false, false, false, false);
|
0, false, false, false, false, false);
|
||||||
EXPECT_NOT_NULL(scanner);
|
EXPECT_NOT_NULL(scanner);
|
||||||
|
|
||||||
Chunk* chunk = directory_scanner_next(scanner);
|
Chunk* chunk = directory_scanner_next(scanner);
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ static void test_server_delete_null() {
|
|||||||
static void test_client_create() {
|
static void test_client_create() {
|
||||||
Client* c = client_create();
|
Client* c = client_create();
|
||||||
EXPECT_NOT_NULL(c);
|
EXPECT_NOT_NULL(c);
|
||||||
EXPECT_TRUE(c->file_descriptor >= 0);
|
EXPECT_TRUE(c->file_descriptor == -1);
|
||||||
EXPECT_EQ_INT(c->address.sin_family, AF_INET);
|
EXPECT_EQ_INT(c->address.ss_family, 0);
|
||||||
EXPECT_EQ_INT(c->ssh_child_pid, -1);
|
EXPECT_EQ_INT(c->ssh_child_pid, -1);
|
||||||
EXPECT_NULL(c->ssl);
|
EXPECT_NULL(c->ssl);
|
||||||
EXPECT_NULL(c->ssl_ctx);
|
EXPECT_NULL(c->ssl_ctx);
|
||||||
|
|||||||
Reference in New Issue
Block a user