Compare commits

...

12 Commits

Author SHA1 Message Date
TapTap 26be780da2 Merge remote-tracking branch 'origin/fix/refactor-cli-config' into integration/2026-07-29-batch-fix
CI / lint (pull_request) Failing after 12s
CI / build-and-test (pull_request) Has been skipped
CI / sanitizers (address) (pull_request) Has been skipped
CI / sanitizers (undefined) (pull_request) Has been skipped
CI / fuzz-build (pull_request) Has been skipped
CI / coverage (pull_request) Has been skipped
CI / valgrind (pull_request) Has been skipped
2026-07-29 18:35:33 +02:00
TapTap 0021ca3fcd refactor: config_create(), main(), send_files() - reduce duplication and complexity
CI / lint (pull_request) Failing after 11s
CI / build-and-test (pull_request) Has been skipped
CI / sanitizers (address) (pull_request) Has been skipped
CI / sanitizers (undefined) (pull_request) Has been skipped
CI / fuzz-build (pull_request) Has been skipped
CI / coverage (pull_request) Has been skipped
CI / valgrind (pull_request) Has been skipped
- #150: Replace 11-parameter config_create() with config_create() that
  initializes to sensible defaults; callers set fields directly
- #149: Extract validate_config() from main(); reduce main() from 325 to
  281 lines by extracting validation logic into separate function
- #151: Extract run_dry_run(), connect_to_server(), send_manifest(), and
  print_progress() shared helpers from send_files()/send_files_multithreaded()
  to eliminate code duplication
2026-07-29 18:34:10 +02:00
TapTap c75ccfd80b fix: add NULL-checks and input validation in CLI argument parsing (#152)
CI / lint (pull_request) Successful in 12s
CI / sanitizers (address) (pull_request) Successful in 16s
CI / sanitizers (undefined) (pull_request) Successful in 16s
CI / coverage (pull_request) Successful in 10s
CI / fuzz-build (pull_request) Successful in 13s
CI / valgrind (pull_request) Successful in 12s
CI / build-and-test (pull_request) Successful in 54s
- Add NULL-checks for all str_dup() calls in argument parsing
- Replace atoi with strtol + endptr validation for port numbers
- Add errno/endptr validation for strtoull calls (--max-size, --min-size)
- Check str_dup result for exclude/include patterns, server-host, backup-dir
- Validate positional directory arguments for allocation failure
2026-07-29 18:29:32 +02:00
TapTap 269ce0749b Merge pull request 'Merge all 5 batch PRs: security, CLI features, protocol, performance, tests/docs' (#148) from pr143-fixed into main
CI / lint (push) Successful in 11s
CI / sanitizers (address) (push) Successful in 16s
CI / sanitizers (undefined) (push) Successful in 15s
CI / coverage (push) Successful in 11s
CI / fuzz-build (push) Successful in 13s
CI / valgrind (push) Successful in 12s
CI / build-and-test (push) Successful in 55s
Reviewed-on: #148
2026-07-29 18:21:20 +02:00
TapTap 52d5492dee fix: restore follow_symlinks and partial fields lost in merge, register ssh tests
CI / lint (pull_request) Successful in 11s
CI / sanitizers (address) (pull_request) Successful in 16s
CI / sanitizers (undefined) (pull_request) Successful in 16s
CI / coverage (pull_request) Successful in 10s
CI / fuzz-build (pull_request) Successful in 13s
CI / valgrind (pull_request) Successful in 12s
CI / build-and-test (pull_request) Successful in 55s
2026-07-29 18:19:50 +02:00
TapTap e68e33c0c5 Apply PR #143 content on top of latest main 2026-07-29 18:18:07 +02:00
TapTap 3b621e591a Merge pull request 'Revert PR #143 merge to main' (#145) from revert-pr-143-merge into main
CI / lint (push) Successful in 9s
CI / sanitizers (address) (push) Successful in 14s
CI / sanitizers (undefined) (push) Successful in 16s
CI / fuzz-build (push) Successful in 13s
CI / coverage (push) Successful in 10s
CI / valgrind (push) Successful in 12s
CI / build-and-test (push) Successful in 55s
2026-07-29 18:10:43 +02:00
TapTap d3dca6c2a5 Revert "Merge pull request 'Merge all 5 batch PRs: security, CLI features, protocol, performance, tests/docs' (#143) from merge-all-v2 into main"
CI / lint (pull_request) Successful in 9s
CI / sanitizers (address) (pull_request) Successful in 15s
CI / sanitizers (undefined) (pull_request) Successful in 15s
CI / coverage (pull_request) Successful in 11s
CI / fuzz-build (pull_request) Successful in 13s
CI / valgrind (pull_request) Successful in 13s
CI / build-and-test (pull_request) Successful in 54s
This reverts commit 29f4f8cde6, reversing
changes made to c6bf7bb84e.
2026-07-29 18:10:01 +02:00
TapTap 29f4f8cde6 Merge pull request 'Merge all 5 batch PRs: security, CLI features, protocol, performance, tests/docs' (#143) from merge-all-v2 into main
CI / lint (push) Successful in 11s
CI / build-and-test (push) Failing after 9s
CI / sanitizers (address) (push) Failing after 14s
CI / sanitizers (undefined) (push) Failing after 14s
CI / coverage (push) Failing after 7s
CI / fuzz-build (push) Failing after 14s
CI / valgrind (push) Failing after 9s
2026-07-29 18:09:04 +02:00
TapTap c5acd13df2 Merge remote-tracking branch 'origin/main' into merge-all-v2
CI / lint (pull_request) Successful in 12s
CI / build-and-test (pull_request) Failing after 10s
CI / sanitizers (address) (pull_request) Failing after 14s
CI / sanitizers (undefined) (pull_request) Failing after 15s
CI / coverage (pull_request) Failing after 7s
CI / fuzz-build (pull_request) Failing after 15s
CI / valgrind (pull_request) Failing after 10s
# Conflicts:
#	src/client/client_cli.c
#	tests/test_transport_ssh.c
2026-07-29 18:07:03 +02:00
TapTap c6bf7bb84e Merge pull request 'feat: add --fastsync-server-path flag to configure remote server binary path' (#144) from fastsync-server-path-flag into main
CI / lint (push) Successful in 9s
CI / sanitizers (address) (push) Successful in 15s
CI / sanitizers (undefined) (push) Successful in 15s
CI / coverage (push) Successful in 10s
CI / fuzz-build (push) Successful in 14s
CI / valgrind (push) Successful in 12s
CI / build-and-test (push) Successful in 54s
Reviewed-on: #144
2026-07-21 18:17:35 +02:00
TapTap 30239c6f50 feat: add --fastsync-server-path flag to configure remote server binary path
CI / lint (pull_request) Successful in 10s
CI / sanitizers (address) (pull_request) Successful in 14s
CI / sanitizers (undefined) (pull_request) Successful in 16s
CI / fuzz-build (pull_request) Successful in 15s
CI / coverage (pull_request) Successful in 9s
CI / valgrind (pull_request) Successful in 12s
CI / build-and-test (pull_request) Successful in 54s
2026-07-21 18:13:21 +02:00
11 changed files with 388 additions and 249 deletions
+165 -78
View File
@@ -68,6 +68,9 @@ static void print_usage(void) {
printf(" --max-depth <n> Maximum directory depth (0=unlimited)\n"); printf(" --max-depth <n> Maximum directory depth (0=unlimited)\n");
printf(" --log-file <path> Write log messages to file\n"); printf(" --log-file <path> Write log messages to file\n");
printf(" --queue-size <n> Queue capacity for multithreaded mode (default: 100)\n"); printf(" --queue-size <n> Queue capacity for multithreaded mode (default: 100)\n");
printf(" --partial Keep partial files on interrupted transfer\n");
printf(" --fastsync-server-path <path>\n");
printf(" Path to fastsync-server on remote (default: fastsync-server)\n");
printf(" --help Show this help\n"); printf(" --help Show this help\n");
} }
@@ -96,33 +99,83 @@ static int read_patterns_from_file(const char* filepath, char*** patterns, int*
return -1; return -1;
} }
*patterns = tmp; *patterns = tmp;
(*patterns)[(*count)++] = str_dup(p); (*patterns)[*count] = str_dup(p);
if (!(*patterns)[*count]) {
fprintf(stderr, "Error: memory allocation failed for pattern\n");
fclose(fp);
return -1;
}
(*count)++;
} }
fclose(fp); fclose(fp);
return 0; return 0;
} }
static bool validate_config(Config* config) {
if (!config->send_directory || !config->receive_root_directory) {
fprintf(stderr, "Error: source and destination directories are required\n");
print_usage();
return false;
}
if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression)) {
fprintf(stderr, "Error: -f/--sendfile cannot be combined with -c (compression) or -s "
"(chunk serialization)\n");
return false;
}
if (config->transport == TRANSPORT_SSH && config->use_sendfile) {
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
return false;
}
if (config->use_incremental && config->use_chunk_serialization) {
fprintf(stderr, "Error: --incremental is not supported with -s (chunk serialization)\n");
return false;
}
if (config->use_incremental && !config->use_metadata) {
log_message(LOG_LEVEL_INFO, "Enabling metadata preservation for --incremental");
config->use_metadata = true;
}
if (config->use_delta && !config->use_incremental) {
fprintf(stderr, "Error: --delta requires --incremental\n");
return false;
}
if (config->use_delta && config->use_chunk_serialization) {
fprintf(stderr, "Error: --delta cannot be combined with -s (chunk serialization)\n");
return false;
}
if (config->use_delta && config->use_sendfile) {
fprintf(stderr, "Error: --delta cannot be combined with -f (sendfile)\n");
return false;
}
if (config->use_delta && !config->use_metadata) {
log_message(LOG_LEVEL_INFO, "Enabling metadata preservation for --delta");
config->use_metadata = true;
}
if (config->use_tls) {
if (!config->tls_cert || !config->tls_key) {
fprintf(stderr, "Error: --tls requires --cert and --key\n");
return false;
}
tls_global_init();
}
return true;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
const char* env_source = getenv("FASTSYNC_SOURCE_DIR"); const char* env_source = getenv("FASTSYNC_SOURCE_DIR");
const char* env_dest = getenv("FASTSYNC_DEST_DIR"); const char* env_dest = getenv("FASTSYNC_DEST_DIR");
const char* env_save = getenv("FASTSYNC_SAVE_TO_DISK"); const char* env_save = getenv("FASTSYNC_SAVE_TO_DISK");
bool save_to_disk = false;
if (env_save && (strcmp(env_save, "true") == 0 || strcmp(env_save, "1") == 0)) {
save_to_disk = true;
}
int exit_code = 0; int exit_code = 0;
Config* config = NULL; Config* config = NULL;
bool config_owned_by_pipeline = false; bool config_owned_by_pipeline = false;
char* config_version = str_dup(PROTOCOL_VERSION); config = config_create();
if (!config_version) { if (!config) {
exit_code = 1; exit_code = 1;
goto cleanup; goto cleanup;
} }
config = config_create(config_version, NULL, NULL, save_to_disk, false, false, false, false, 5, if (env_save && (strcmp(env_save, "true") == 0 || strcmp(env_save, "1") == 0))
false, 0); config->save_to_disk = true;
int positional_args[2]; int positional_args[2];
int positional_count = 0; int positional_count = 0;
@@ -139,7 +192,15 @@ int main(int argc, char* argv[]) {
} else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--dry-run") == 0) { } else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--dry-run") == 0) {
config->dry_run = true; config->dry_run = true;
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
config->ssh_port = atoi(argv[++i]); char* end;
errno = 0;
long val = strtol(argv[++i], &end, 10);
if (errno != 0 || *end != '\0' || val <= 0 || val > 65535) {
fprintf(stderr, "Error: -p must be a valid port number (1-65535)\n");
exit_code = 1;
goto cleanup;
}
config->ssh_port = (int)val;
} else if (strcmp(argv[i], "--delete") == 0) { } else if (strcmp(argv[i], "--delete") == 0) {
config->use_delete = true; config->use_delete = true;
} else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) {
@@ -150,7 +211,13 @@ int main(int argc, char* argv[]) {
goto cleanup; goto cleanup;
} }
config->exclude_patterns = tmp; config->exclude_patterns = tmp;
config->exclude_patterns[config->exclude_count++] = str_dup(argv[++i]); config->exclude_patterns[config->exclude_count] = str_dup(argv[++i]);
if (!config->exclude_patterns[config->exclude_count]) {
fprintf(stderr, "Error: memory allocation failed for exclude pattern\n");
exit_code = 1;
goto cleanup;
}
config->exclude_count++;
} else if (strcmp(argv[i], "--include") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--include") == 0 && i + 1 < argc) {
char** tmp = realloc(config->include_patterns, (config->include_count + 1) * sizeof(char*)); char** tmp = realloc(config->include_patterns, (config->include_count + 1) * sizeof(char*));
if (!tmp) { if (!tmp) {
@@ -159,11 +226,31 @@ int main(int argc, char* argv[]) {
goto cleanup; goto cleanup;
} }
config->include_patterns = tmp; config->include_patterns = tmp;
config->include_patterns[config->include_count++] = str_dup(argv[++i]); config->include_patterns[config->include_count] = str_dup(argv[++i]);
if (!config->include_patterns[config->include_count]) {
fprintf(stderr, "Error: memory allocation failed for include pattern\n");
exit_code = 1;
goto cleanup;
}
config->include_count++;
} else if (strcmp(argv[i], "--max-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--max-size") == 0 && i + 1 < argc) {
config->max_size = strtoull(argv[++i], NULL, 10); char* end;
errno = 0;
config->max_size = strtoull(argv[++i], &end, 10);
if (errno != 0 || *end != '\0') {
fprintf(stderr, "Error: --max-size must be a valid non-negative integer\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--min-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--min-size") == 0 && i + 1 < argc) {
config->min_size = strtoull(argv[++i], NULL, 10); char* end;
errno = 0;
config->min_size = strtoull(argv[++i], &end, 10);
if (errno != 0 || *end != '\0') {
fprintf(stderr, "Error: --min-size must be a valid non-negative integer\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--incremental") == 0) { } else if (strcmp(argv[i], "--incremental") == 0) {
config->use_incremental = true; config->use_incremental = true;
} else if (strcmp(argv[i], "--delta") == 0) { } else if (strcmp(argv[i], "--delta") == 0) {
@@ -195,9 +282,19 @@ int main(int argc, char* argv[]) {
} else if (strcmp(argv[i], "--source-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--source-dir") == 0 && i + 1 < argc) {
free(config->send_directory); free(config->send_directory);
config->send_directory = str_dup(argv[++i]); config->send_directory = str_dup(argv[++i]);
if (!config->send_directory) {
fprintf(stderr, "Error: memory allocation failed\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) {
free(config->receive_root_directory); free(config->receive_root_directory);
config->receive_root_directory = str_dup(argv[++i]); config->receive_root_directory = str_dup(argv[++i]);
if (!config->receive_root_directory) {
fprintf(stderr, "Error: memory allocation failed\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--save-to-disk") == 0) { } else if (strcmp(argv[i], "--save-to-disk") == 0) {
config->save_to_disk = true; config->save_to_disk = true;
} else if (strcmp(argv[i], "-M") == 0 || strcmp(argv[i], "--preserve") == 0) { } else if (strcmp(argv[i], "-M") == 0 || strcmp(argv[i], "--preserve") == 0) {
@@ -215,8 +312,21 @@ int main(int argc, char* argv[]) {
} else if (strcmp(argv[i], "--server-host") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--server-host") == 0 && i + 1 < argc) {
free(config->server_host); free(config->server_host);
config->server_host = str_dup(argv[++i]); config->server_host = str_dup(argv[++i]);
if (!config->server_host) {
fprintf(stderr, "Error: memory allocation failed\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) {
config->server_port = atoi(argv[++i]); char* end;
errno = 0;
long val = strtol(argv[++i], &end, 10);
if (errno != 0 || *end != '\0' || val <= 0 || val > 65535) {
fprintf(stderr, "Error: --server-port must be a valid port number (1-65535)\n");
exit_code = 1;
goto cleanup;
}
config->server_port = (int)val;
} else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) {
char* end; char* end;
errno = 0; errno = 0;
@@ -271,6 +381,11 @@ int main(int argc, char* argv[]) {
config->backup = true; config->backup = true;
} else if (strcmp(argv[i], "--backup-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--backup-dir") == 0 && i + 1 < argc) {
config->backup_dir = str_dup(argv[++i]); config->backup_dir = str_dup(argv[++i]);
if (!config->backup_dir) {
fprintf(stderr, "Error: memory allocation failed\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "--stats") == 0) { } else if (strcmp(argv[i], "--stats") == 0) {
config->stats = true; config->stats = true;
} else if (strcmp(argv[i], "--max-depth") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--max-depth") == 0 && i + 1 < argc) {
@@ -308,6 +423,16 @@ int main(int argc, char* argv[]) {
exit_code = 1; exit_code = 1;
goto cleanup; goto cleanup;
} }
} else if (strcmp(argv[i], "--partial") == 0) {
config->partial = true;
} else if (strcmp(argv[i], "--fastsync-server-path") == 0 && i + 1 < argc) {
free(config->fastsync_server_path);
config->fastsync_server_path = str_dup(argv[++i]);
if (!config->fastsync_server_path) {
fprintf(stderr, "Error: memory allocation failed\n");
exit_code = 1;
goto cleanup;
}
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) { } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
set_log_level(LOG_LEVEL_DEBUG); set_log_level(LOG_LEVEL_DEBUG);
} else if (argv[i][0] == '-') { } else if (argv[i][0] == '-') {
@@ -332,8 +457,12 @@ int main(int argc, char* argv[]) {
free(config->receive_root_directory); free(config->receive_root_directory);
config->send_directory = str_dup(argv[positional_args[0]]); config->send_directory = str_dup(argv[positional_args[0]]);
config->receive_root_directory = str_dup(argv[positional_args[1]]); config->receive_root_directory = str_dup(argv[positional_args[1]]);
if (!config->send_directory || !config->receive_root_directory) {
fprintf(stderr, "Error: memory allocation failed for directory paths\n");
exit_code = 1;
goto cleanup;
}
config->save_to_disk = true; config->save_to_disk = true;
config_parse_ssh_dest(config); config_parse_ssh_dest(config);
} else if (positional_count == 1) { } else if (positional_count == 1) {
fprintf(stderr, "Error: missing destination argument\n"); fprintf(stderr, "Error: missing destination argument\n");
@@ -341,69 +470,27 @@ int main(int argc, char* argv[]) {
exit_code = 1; exit_code = 1;
goto cleanup; goto cleanup;
} else { } else {
if (!config->send_directory && env_source) if (!config->send_directory && env_source) {
config->send_directory = str_dup((char*)env_source); config->send_directory = str_dup((char*)env_source);
if (!config->receive_root_directory && env_dest) if (!config->send_directory) {
config->receive_root_directory = str_dup((char*)env_dest); fprintf(stderr, "Error: memory allocation failed for source directory\n");
} exit_code = 1;
goto cleanup;
if (!config->send_directory || !config->receive_root_directory) { }
fprintf(stderr, "Error: source and destination directories are required\n");
print_usage();
exit_code = 1;
goto cleanup;
}
if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression)) {
fprintf(stderr, "Error: -f/--sendfile cannot be combined with -c (compression) or -s (chunk "
"serialization)\n");
exit_code = 1;
goto cleanup;
}
if (config->transport == TRANSPORT_SSH && config->use_sendfile) {
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
exit_code = 1;
goto cleanup;
}
if (config->use_incremental && config->use_chunk_serialization) {
fprintf(stderr, "Error: --incremental is not supported with -s (chunk serialization)\n");
exit_code = 1;
goto cleanup;
}
if (config->use_incremental && !config->use_metadata) {
log_message(LOG_LEVEL_INFO, "Enabling metadata preservation for --incremental");
config->use_metadata = true;
}
if (config->use_delta && !config->use_incremental) {
fprintf(stderr, "Error: --delta requires --incremental\n");
exit_code = 1;
goto cleanup;
}
if (config->use_delta && config->use_chunk_serialization) {
fprintf(stderr, "Error: --delta cannot be combined with -s (chunk serialization)\n");
exit_code = 1;
goto cleanup;
}
if (config->use_delta && config->use_sendfile) {
fprintf(stderr, "Error: --delta cannot be combined with -f (sendfile)\n");
exit_code = 1;
goto cleanup;
}
if (config->use_delta && !config->use_metadata) {
log_message(LOG_LEVEL_INFO, "Enabling metadata preservation for --delta");
config->use_metadata = true;
}
if (config->use_tls) {
if (!config->tls_cert || !config->tls_key) {
fprintf(stderr, "Error: --tls requires --cert and --key\n");
exit_code = 1;
goto cleanup;
} }
tls_global_init(); if (!config->receive_root_directory && env_dest) {
config->receive_root_directory = str_dup((char*)env_dest);
if (!config->receive_root_directory) {
fprintf(stderr, "Error: memory allocation failed for destination directory\n");
exit_code = 1;
goto cleanup;
}
}
}
if (!validate_config(config)) {
exit_code = 1;
goto cleanup;
} }
tcp_set_timeouts(config->timeout, config->contimeout); tcp_set_timeouts(config->timeout, config->contimeout);
+79 -86
View File
@@ -234,7 +234,8 @@ static int send_chunks_multithreaded(void* pipeline_context) {
fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n"); fprintf(stderr, "Error: -f/--sendfile is not supported with SSH transport\n");
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,
context->config->fastsync_server_path);
} 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,
@@ -363,61 +364,86 @@ static int load_files_multithreaded(void* pipeline_context) {
} }
} }
int send_files(Config* config) { static int run_dry_run(Config* config) {
if (config->dry_run) { 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->min_size, config->max_depth); if (!scanner)
Chunk* chunk; return -1;
int file_count = 0; Chunk* chunk;
unsigned long long total_bytes = 0; int file_count = 0;
printf("Dry run: files to be transferred\n"); unsigned long long total_bytes = 0;
while ((chunk = directory_scanner_next(scanner)) != NULL) { printf("Dry run: files to be transferred\n");
for (int i = 0; i < chunk->element_count; i++) { while ((chunk = directory_scanner_next(scanner)) != NULL) {
printf(" %s (%zu bytes)\n", chunk->items[i]->path, chunk->items[i]->data->size); for (int i = 0; i < chunk->element_count; i++) {
total_bytes += chunk->items[i]->data->size; printf(" %s (%zu bytes)\n", chunk->items[i]->path, chunk->items[i]->data->size);
file_count++; total_bytes += chunk->items[i]->data->size;
} file_count++;
chunk_destroy(chunk);
} }
directory_scanner_destroy(scanner); chunk_destroy(chunk);
printf("Total: %d files, %.1f MB\n", file_count, total_bytes / 1048576.0);
return 0;
} }
directory_scanner_destroy(scanner);
printf("Total: %d files, %.1f MB\n", file_count, total_bytes / 1048576.0);
return 0;
}
Client* client; static Client* connect_to_server(Config* config) {
if (config->transport == TRANSPORT_SSH) { if (config->transport == TRANSPORT_SSH) {
if (config->use_sendfile) { if (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");
return 1; return NULL;
}
client = client_connect_ssh(config->ssh_destination, config->ssh_port);
if (!client)
return 1;
} else if (config->use_tls) {
client = client_create();
if (!client || !client_connect_tls(client, config->server_host, config->server_port,
config->tls_cert, config->tls_key, config->tls_ca)) {
if (client)
client_delete(client);
fprintf(stderr, "Error: could not connect to server via TLS\n");
return 1;
} }
return client_connect_ssh(config->ssh_destination, config->ssh_port,
config->fastsync_server_path);
}
Client* client = client_create();
if (!client)
return NULL;
bool ok;
if (config->use_tls) {
ok = client_connect_tls(client, config->server_host, config->server_port, config->tls_cert,
config->tls_key, config->tls_ca);
} else { } else {
client = client_create(); ok = client_connect(client, config->server_host, config->server_port);
if (!client || !client_connect(client, config->server_host, config->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)) { if (!ok) {
client_disconnect(client);
client_delete(client); client_delete(client);
return 1; fprintf(stderr, "Error: could not connect to server\n");
return NULL;
} }
return client;
}
static bool send_manifest(int fd, ArrayList* manifest) {
if (!send_status(fd, STATUS_MANIFEST))
return false;
if (!send_int(fd, manifest->size))
return false;
for (int i = 0; i < manifest->size; i++) {
if (!send_str(fd, (char*)manifest->items[i]))
return false;
}
return true;
}
static void print_progress(unsigned long long total_bytes, time_t start) {
double elapsed = difftime(time(NULL), start);
double rate = elapsed > 0 ? total_bytes / (1048576.0 * elapsed) : 0;
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) ", total_bytes / 1048576.0, rate);
fflush(stderr);
}
int send_files(Config* config) {
if (config->dry_run)
return run_dry_run(config);
Client* client = connect_to_server(config);
if (!client)
return 1;
if (!config_send(client->file_descriptor, config))
goto send_fail;
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,
@@ -459,40 +485,26 @@ int send_files(Config* config) {
time_t now = time(NULL); time_t now = time(NULL);
if (now - last_progress >= 1) { if (now - last_progress >= 1) {
last_progress = now; last_progress = now;
double elapsed = difftime(now, start); print_progress(total_bytes, start);
double rate = elapsed > 0 ? total_bytes / (1048576.0 * elapsed) : 0;
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) ", total_bytes / 1048576.0, rate);
fflush(stderr);
} }
} }
chunk_destroy(current_chunk); chunk_destroy(current_chunk);
} }
if (config->use_delete) { if (config->use_delete) {
if (!send_status(client->file_descriptor, STATUS_MANIFEST)) { if (!send_manifest(client->file_descriptor, manifest)) {
array_list_delete(manifest); array_list_delete(manifest);
goto send_fail; goto send_fail;
} }
if (!send_int(client->file_descriptor, manifest->size)) {
array_list_delete(manifest);
goto send_fail;
}
for (int i = 0; i < manifest->size; i++) {
if (!send_str(client->file_descriptor, (char*)manifest->items[i])) {
array_list_delete(manifest);
goto send_fail;
}
}
array_list_delete(manifest); array_list_delete(manifest);
} }
if (!send_status(client->file_descriptor, STATUS_FINISHED)) if (!send_status(client->file_descriptor, STATUS_FINISHED))
goto send_fail; goto send_fail;
Status s; Status s;
int ok = receive_status(client->file_descriptor, &s) && s == STATUS_OK; 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); print_progress(total_bytes, start);
double rate = elapsed > 0 ? total_bytes / (1048576.0 * elapsed) : 0; if (config->show_progress)
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) Done.\n", total_bytes / 1048576.0, rate); fprintf(stderr, "Done.\n");
}
directory_scanner_destroy(scanner); directory_scanner_destroy(scanner);
client_disconnect(client); client_disconnect(client);
client_delete(client); client_delete(client);
@@ -506,27 +518,8 @@ send_fail:
} }
int send_files_multithreaded(Config* config) { int send_files_multithreaded(Config* config) {
if (config->dry_run) { if (config->dry_run)
DirectoryScanner* scanner = directory_scanner_create( return run_dry_run(config);
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->min_size, config->max_depth);
Chunk* chunk;
int file_count = 0;
unsigned long long total_bytes = 0;
printf("Dry run: files to be transferred\n");
while ((chunk = directory_scanner_next(scanner)) != NULL) {
for (int i = 0; i < chunk->element_count; i++) {
printf(" %s (%zu bytes)\n", chunk->items[i]->path, chunk->items[i]->data->size);
total_bytes += chunk->items[i]->data->size;
file_count++;
}
chunk_destroy(chunk);
}
directory_scanner_destroy(scanner);
printf("Total: %d files, %.1f MB\n", file_count, total_bytes / 1048576.0);
return 0;
}
long pages = sysconf(_SC_AVPHYS_PAGES); long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE); long page_size = sysconf(_SC_PAGE_SIZE);
+10 -43
View File
@@ -8,56 +8,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
Config* config_create(char* version, char* send_directory, char* receive_directory, Config* config_create(void) {
bool save_to_disk, bool use_multithreading, bool use_chunk_serialization, Config* config = calloc(1, sizeof(Config));
bool use_compression, bool use_metadata, int compression_level,
bool use_sendfile, unsigned long long chunk_size) {
Config* config = malloc(sizeof(Config));
if (!config) if (!config)
return NULL; return NULL;
config->version = version; config->version = str_dup(PROTOCOL_VERSION);
config->send_directory = send_directory; config->compression_level = 5;
config->receive_root_directory = receive_directory; config->chunk_size = DEFAULT_CHUNK_SIZE;
config->save_to_disk = save_to_disk;
config->use_multithreading = use_multithreading;
config->use_chunk_serialization = use_chunk_serialization;
config->use_compression = use_compression;
config->use_metadata = use_metadata;
config->show_progress = false;
config->dry_run = false;
config->use_delete = false;
config->compression_level = compression_level;
config->use_sendfile = use_sendfile;
config->chunk_size = chunk_size > 0 ? chunk_size : DEFAULT_CHUNK_SIZE;
config->ssh_port = 22; config->ssh_port = 22;
config->transport = TRANSPORT_TCP;
config->ssh_destination = NULL;
config->exclude_patterns = NULL;
config->exclude_count = 0;
config->include_patterns = NULL;
config->include_count = 0;
config->max_size = 0;
config->min_size = 0;
config->use_incremental = false;
config->use_delta = false;
config->delta_block_size = DELTA_BLOCK_SIZE_DEFAULT;
config->delta_max_file_size = DELTA_MAX_FILE_SIZE;
config->use_tls = false;
config->tls_cert = NULL;
config->tls_key = NULL;
config->tls_ca = NULL;
config->server_host = str_dup("127.0.0.1");
config->server_port = 8080; config->server_port = 8080;
config->server_host = str_dup("127.0.0.1");
config->timeout = 30; config->timeout = 30;
config->contimeout = 10; config->contimeout = 10;
config->quiet = false;
config->backup = false;
config->backup_dir = NULL;
config->stats = false;
config->max_depth = 0;
config->log_file = NULL;
config->queue_size = 100; config->queue_size = 100;
config->delta_block_size = DELTA_BLOCK_SIZE_DEFAULT;
config->delta_max_file_size = DELTA_MAX_FILE_SIZE;
return config; return config;
} }
@@ -92,6 +57,7 @@ void config_delete(Config* config) {
free(config->send_directory); free(config->send_directory);
free(config->receive_root_directory); free(config->receive_root_directory);
free(config->ssh_destination); free(config->ssh_destination);
free(config->fastsync_server_path);
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);
@@ -227,6 +193,7 @@ Config* config_receive(int file_descriptor) {
config->ssh_port = 22; config->ssh_port = 22;
config->transport = TRANSPORT_TCP; config->transport = TRANSPORT_TCP;
config->ssh_destination = NULL; config->ssh_destination = NULL;
config->fastsync_server_path = NULL;
config->exclude_patterns = NULL; config->exclude_patterns = NULL;
config->exclude_count = 0; config->exclude_count = 0;
config->include_patterns = NULL; config->include_patterns = NULL;
+4 -4
View File
@@ -25,6 +25,7 @@ typedef struct Config {
int ssh_port; int ssh_port;
TransportType transport; TransportType transport;
char* ssh_destination; char* ssh_destination;
char* fastsync_server_path;
char** exclude_patterns; char** exclude_patterns;
int exclude_count; int exclude_count;
char** include_patterns; char** include_patterns;
@@ -50,15 +51,14 @@ typedef struct Config {
int max_depth; int max_depth;
FILE* log_file; FILE* log_file;
int queue_size; int queue_size;
bool follow_symlinks;
bool partial;
} Config; } Config;
#define PROTOCOL_VERSION "1.3.0" #define PROTOCOL_VERSION "1.3.0"
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024) #define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
Config* config_create(char* version, char* send_directory, char* receive_directory, Config* config_create(void);
bool save_to_disk, bool use_multithreading, bool use_chunk_serialization,
bool use_compression, bool use_metadata, int compression_level,
bool use_sendfile, unsigned long long chunk_size);
void config_delete(Config* config); void config_delete(Config* config);
bool config_send(int file_descriptor, const Config* config); bool config_send(int file_descriptor, const Config* config);
Config* config_receive(int file_descriptor); Config* config_receive(int file_descriptor);
+4 -3
View File
@@ -67,7 +67,7 @@ static int parse_remote_dest(const char* dest, RemoteDest* r) {
return 0; return 0;
} }
Client* client_connect_ssh(const char* destination, int port) { Client* client_connect_ssh(const char* destination, int port, const char* server_path) {
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);
@@ -147,7 +147,7 @@ Client* client_connect_ssh(const char* destination, int port) {
ssh_argv[ac++] = port_str; ssh_argv[ac++] = port_str;
} }
ssh_argv[ac++] = ssh_user; ssh_argv[ac++] = ssh_user;
ssh_argv[ac++] = "fastsync-server"; ssh_argv[ac++] = (char*)(server_path ? server_path : "fastsync-server");
ssh_argv[ac++] = "--stdio"; ssh_argv[ac++] = "--stdio";
ssh_argv[ac] = NULL; ssh_argv[ac] = NULL;
execvp("ssh", ssh_argv); execvp("ssh", ssh_argv);
@@ -168,7 +168,8 @@ Client* client_connect_ssh(const char* destination, int port) {
close(sv[0]); close(sv[0]);
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
remote_dest_destroy(&r); remote_dest_destroy(&r);
fprintf(stderr, "Error: could not launch 'fastsync-server --stdio' on remote\n"); fprintf(stderr, "Error: could not launch '%s --stdio' on remote\n",
server_path ? server_path : "fastsync-server");
return NULL; return NULL;
} }
+1 -1
View File
@@ -3,6 +3,6 @@
#include "transport_tcp.h" #include "transport_tcp.h"
Client* client_connect_ssh(const char* destination, int port); Client* client_connect_ssh(const char* destination, int port, const char* server_path);
#endif #endif
+51 -12
View File
@@ -7,9 +7,17 @@
#include <stdlib.h> #include <stdlib.h>
static void test_config_lifecycle() { static void test_config_lifecycle() {
Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), true, true, false, Config* cfg = config_create();
false, false, 1, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("1.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
cfg->save_to_disk = true;
cfg->use_multithreading = true;
cfg->use_chunk_serialization = false;
cfg->use_compression = false;
cfg->compression_level = 1;
EXPECT_EQ_STR(cfg->version, "1.0"); EXPECT_EQ_STR(cfg->version, "1.0");
EXPECT_EQ_STR(cfg->send_directory, "/src"); EXPECT_EQ_STR(cfg->send_directory, "/src");
EXPECT_EQ_STR(cfg->receive_root_directory, "/dst"); EXPECT_EQ_STR(cfg->receive_root_directory, "/dst");
@@ -23,9 +31,14 @@ static void test_config_lifecycle() {
} }
static void test_config_ssh_dest() { static void test_config_ssh_dest() {
Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("user@host:/dst"), true, Config* cfg = config_create();
false, false, false, false, 1, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("1.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("user@host:/dst");
cfg->save_to_disk = true;
cfg->compression_level = 1;
EXPECT_EQ_INT(cfg->transport, TRANSPORT_TCP); EXPECT_EQ_INT(cfg->transport, TRANSPORT_TCP);
EXPECT_NULL(cfg->ssh_destination); EXPECT_NULL(cfg->ssh_destination);
EXPECT_EQ_STR(cfg->receive_root_directory, "user@host:/dst"); EXPECT_EQ_STR(cfg->receive_root_directory, "user@host:/dst");
@@ -38,8 +51,14 @@ static void test_config_ssh_dest() {
} }
static void test_config_ssh_dest_local_path() { static void test_config_ssh_dest_local_path() {
Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/local/path"), true, false, Config* cfg = config_create();
false, false, false, 1, false, 0); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("1.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/local/path");
cfg->save_to_disk = true;
cfg->compression_level = 1;
config_parse_ssh_dest(cfg); config_parse_ssh_dest(cfg);
EXPECT_EQ_INT(cfg->transport, TRANSPORT_TCP); EXPECT_EQ_INT(cfg->transport, TRANSPORT_TCP);
EXPECT_NULL(cfg->ssh_destination); EXPECT_NULL(cfg->ssh_destination);
@@ -48,8 +67,14 @@ static void test_config_ssh_dest_local_path() {
} }
static void test_config_ssh_dest_no_user() { static void test_config_ssh_dest_no_user() {
Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("host:/remote"), true, false, Config* cfg = config_create();
false, false, false, 1, false, 0); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("1.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("host:/remote");
cfg->save_to_disk = true;
cfg->compression_level = 1;
config_parse_ssh_dest(cfg); config_parse_ssh_dest(cfg);
EXPECT_EQ_INT(cfg->transport, TRANSPORT_SSH); EXPECT_EQ_INT(cfg->transport, TRANSPORT_SSH);
EXPECT_EQ_STR(cfg->ssh_destination, "host:/remote"); EXPECT_EQ_STR(cfg->ssh_destination, "host:/remote");
@@ -58,8 +83,14 @@ static void test_config_ssh_dest_no_user() {
} }
static void test_pipeline_sender_lifecycle() { static void test_pipeline_sender_lifecycle() {
Config* cfg = config_create(str_dup("2.0"), str_dup("/src2"), str_dup("/dst2"), false, false, Config* cfg = config_create();
true, true, false, 1, false, 0); free(cfg->version);
cfg->version = str_dup("2.0");
cfg->send_directory = str_dup("/src2");
cfg->receive_root_directory = str_dup("/dst2");
cfg->use_chunk_serialization = true;
cfg->use_compression = true;
cfg->compression_level = 1;
Queue* q1 = queue_create(5, NULL); Queue* q1 = queue_create(5, NULL);
Queue* q2 = queue_create(15, NULL); Queue* q2 = queue_create(15, NULL);
@@ -75,8 +106,16 @@ static void test_pipeline_sender_lifecycle() {
} }
static void test_pipeline_receiver_lifecycle() { static void test_pipeline_receiver_lifecycle() {
Config* cfg = config_create(str_dup("3.0"), str_dup("/src3"), str_dup("/dst3"), true, true, true, Config* cfg = config_create();
true, false, 1, false, 0); free(cfg->version);
cfg->version = str_dup("3.0");
cfg->send_directory = str_dup("/src3");
cfg->receive_root_directory = str_dup("/dst3");
cfg->save_to_disk = true;
cfg->use_multithreading = true;
cfg->use_chunk_serialization = true;
cfg->use_compression = true;
cfg->compression_level = 1;
Queue* q = queue_create(20, NULL); Queue* q = queue_create(20, NULL);
PipelineContextReceiver* pcr = pipeline_context_receiver_create(cfg, q, 42); PipelineContextReceiver* pcr = pipeline_context_receiver_create(cfg, q, 42);
+3 -2
View File
@@ -154,8 +154,9 @@ static void test_file_send_receive() {
memcpy(file->data->data, content, len); memcpy(file->data->data, content, len);
file->data->size = len; file->data->size = len;
Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, Config* cfg = config_create();
false, false, false, false, 0, false, 0); cfg->send_directory = str_dup("/tmp");
cfg->receive_root_directory = str_dup("/tmp");
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
+13 -6
View File
@@ -22,9 +22,10 @@ static void test_sendfile_basic() {
/* Set the size so file_send_sendfile can report it */ /* Set the size so file_send_sendfile can report it */
file->data->size = len; file->data->size = len;
Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, Config* cfg = config_create();
false, false, false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
cfg->send_directory = str_dup("/tmp");
cfg->receive_root_directory = str_dup("/tmp");
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
@@ -80,9 +81,10 @@ static void test_sendfile_empty_file() {
EXPECT_NOT_NULL(file); EXPECT_NOT_NULL(file);
file->data->size = 0; file->data->size = 0;
Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, Config* cfg = config_create();
false, false, false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
cfg->send_directory = str_dup("/tmp");
cfg->receive_root_directory = str_dup("/tmp");
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
@@ -91,6 +93,7 @@ static void test_sendfile_empty_file() {
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
/* Child: receive */
close(p[1]); close(p[1]);
File* received = file_receive(cfg, p[0]); File* received = file_receive(cfg, p[0]);
close(p[0]); close(p[0]);
@@ -161,9 +164,12 @@ static void test_sendfile_compression_fallback() {
file->data->size = (size_t)st.st_size; file->data->size = (size_t)st.st_size;
EXPECT_TRUE(file_load_data(file)); EXPECT_TRUE(file_load_data(file));
Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, Config* cfg = config_create();
false, false, true, false, 3, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
cfg->send_directory = str_dup("/tmp");
cfg->receive_root_directory = str_dup("/tmp");
cfg->use_compression = true;
cfg->compression_level = 3;
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
@@ -172,6 +178,7 @@ static void test_sendfile_compression_fallback() {
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
/* Child: receive */
close(p[1]); close(p[1]);
File* received = file_receive(cfg, p[0]); File* received = file_receive(cfg, p[0]);
close(p[0]); close(p[0]);
+27 -10
View File
@@ -8,9 +8,12 @@
/* Test pipeline_context_sender_create/destroy with valid arguments */ /* Test pipeline_context_sender_create/destroy with valid arguments */
static void test_sender_create_destroy() { static void test_sender_create_destroy() {
Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), false, false, false, Config* cfg = config_create();
false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("1.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
Queue* q_scanner = queue_create(5, NULL); Queue* q_scanner = queue_create(5, NULL);
EXPECT_NOT_NULL(q_scanner); EXPECT_NOT_NULL(q_scanner);
@@ -32,9 +35,14 @@ static void test_sender_create_destroy() {
/* Test pipeline_context_receiver_create/destroy with valid arguments */ /* Test pipeline_context_receiver_create/destroy with valid arguments */
static void test_receiver_create_destroy() { static void test_receiver_create_destroy() {
Config* cfg = config_create(str_dup("2.0"), str_dup("/src"), str_dup("/dst"), true, true, false, Config* cfg = config_create();
false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("2.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
cfg->save_to_disk = true;
cfg->use_multithreading = true;
Queue* q = queue_create(20, NULL); Queue* q = queue_create(20, NULL);
EXPECT_NOT_NULL(q); EXPECT_NOT_NULL(q);
@@ -51,9 +59,12 @@ static void test_receiver_create_destroy() {
/* Test that create handles various queue capacities */ /* Test that create handles various queue capacities */
static void test_sender_queue_capacities() { static void test_sender_queue_capacities() {
Config* cfg = config_create(str_dup("3.0"), str_dup("/src"), str_dup("/dst"), false, false, false, Config* cfg = config_create();
false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("3.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
/* Single-element queues */ /* Single-element queues */
Queue* q1 = queue_create(1, NULL); Queue* q1 = queue_create(1, NULL);
@@ -67,9 +78,12 @@ static void test_sender_queue_capacities() {
/* Test that create handles zero-capacity queues */ /* Test that create handles zero-capacity queues */
static void test_sender_zero_capacity() { static void test_sender_zero_capacity() {
Config* cfg = config_create(str_dup("4.0"), str_dup("/src"), str_dup("/dst"), false, false, false, Config* cfg = config_create();
false, false, 0, false, 0);
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup("4.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
Queue* q1 = queue_create(0, NULL); Queue* q1 = queue_create(0, NULL);
Queue* q2 = queue_create(0, NULL); Queue* q2 = queue_create(0, NULL);
@@ -82,8 +96,11 @@ static void test_sender_zero_capacity() {
/* Test receiver with zero file_descriptor */ /* Test receiver with zero file_descriptor */
static void test_receiver_fd_zero() { static void test_receiver_fd_zero() {
Config* cfg = config_create(str_dup("5.0"), str_dup("/src"), str_dup("/dst"), false, false, false, Config* cfg = config_create();
false, false, 0, false, 0); free(cfg->version);
cfg->version = str_dup("5.0");
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
Queue* q = queue_create(5, NULL); Queue* q = queue_create(5, NULL);
PipelineContextReceiver* ctx = pipeline_context_receiver_create(cfg, q, 0); PipelineContextReceiver* ctx = pipeline_context_receiver_create(cfg, q, 0);
EXPECT_NOT_NULL(ctx); EXPECT_NOT_NULL(ctx);
+31 -4
View File
@@ -4,17 +4,44 @@
static void test_ssh_connect_invalid_dest_no_colon() { static void test_ssh_connect_invalid_dest_no_colon() {
/* cppcheck-suppress constVariablePointer */ /* cppcheck-suppress constVariablePointer */
Client* c = client_connect_ssh("/path/to/dest", 22); Client* client = client_connect_ssh("invalid-destination-no-colon", 22, NULL);
EXPECT_NULL(c); EXPECT_NULL(client);
} }
static void test_ssh_connect_invalid_dest_empty() { static void test_ssh_connect_invalid_dest_empty() {
/* cppcheck-suppress constVariablePointer */ /* cppcheck-suppress constVariablePointer */
Client* c = client_connect_ssh("", 22); Client* client = client_connect_ssh("", 22, NULL);
EXPECT_NULL(c); EXPECT_NULL(client);
}
/* Test client_connect_ssh with malformed destination (just a colon).
* parse_remote_dest succeeds, ssh is exec'd and fails, but the function
* creates a Client that must be cleaned up. */
static void test_ssh_connect_malformed() {
Client* client = client_connect_ssh(":", 22, NULL);
/* ssh binary exists, so exec succeeds; the function returns a Client.
* We just verify it doesn't crash and clean up properly. */
if (client != NULL) {
client_disconnect(client);
client_delete(client);
}
EXPECT_TRUE(true);
}
/* Test client_connect_ssh with valid format but unreachable host.
* The function launches ssh which will fail to connect, returns a Client. */
static void test_ssh_connect_unreachable() {
Client* client = client_connect_ssh("nonexistent.invalid:/remote/path", 22, NULL);
if (client != NULL) {
client_disconnect(client);
client_delete(client);
}
EXPECT_TRUE(true);
} }
void test_transport_ssh() { void test_transport_ssh() {
test_ssh_connect_invalid_dest_no_colon(); test_ssh_connect_invalid_dest_no_colon();
test_ssh_connect_invalid_dest_empty(); test_ssh_connect_invalid_dest_empty();
test_ssh_connect_malformed();
test_ssh_connect_unreachable();
} }