diff --git a/src/client/client_cli.c b/src/client/client_cli.c index de570b9..c0758e3 100644 --- a/src/client/client_cli.c +++ b/src/client/client_cli.c @@ -92,14 +92,7 @@ int main(int argc, char* argv[]) { } else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--dry-run") == 0) { config->dry_run = true; } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { - char* end; - long p = strtol(argv[++i], &end, 10); - if (*end != '\0' || p <= 0 || p > 65535) { - fprintf(stderr, "Error: invalid SSH port '%s' (must be 1-65535)\n", argv[i]); - exit_code = 1; - goto cleanup; - } - config->ssh_port = (int)p; + config->ssh_port = atoi(argv[++i]); } else if (strcmp(argv[i], "--delete") == 0) { config->use_delete = true; } else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) { @@ -176,14 +169,7 @@ int main(int argc, char* argv[]) { free(config->server_host); config->server_host = str_dup(argv[++i]); } else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) { - char* end; - long p = strtol(argv[++i], &end, 10); - if (*end != '\0' || p <= 0 || p > 65535) { - fprintf(stderr, "Error: invalid server port '%s' (must be 1-65535)\n", argv[i]); - exit_code = 1; - goto cleanup; - } - config->server_port = (int)p; + config->server_port = atoi(argv[++i]); } else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) { char* end; errno = 0; diff --git a/src/server/server.c b/src/server/server.c index 2a902bb..43e3819 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -113,11 +113,13 @@ void handler(int file_descriptor) { } static Server* g_server = NULL; -static volatile sig_atomic_t g_server_cleanup_requested = 0; static void cleanup(int sig) { (void)sig; - g_server_cleanup_requested = 1; + if (g_server) { + server_delete(&g_server); + } + _exit(0); } static void print_server_usage(void) { @@ -207,11 +209,5 @@ int main(int argc, char* argv[]) { } else { server_listen(g_server, handler); } - - /* Graceful shutdown: if a signal requested cleanup, delete the server */ - if (g_server_cleanup_requested) { - log_message(LOG_LEVEL_INFO, "Shutdown requested, cleaning up"); - server_delete(&g_server); - } return 0; } diff --git a/src/shared/compression.c b/src/shared/compression.c index 7c60686..01b558f 100644 --- a/src/shared/compression.c +++ b/src/shared/compression.c @@ -9,18 +9,6 @@ Data* data_compress(Data* data_to_compress, int compression_level) { log_message(LOG_LEVEL_DEBUG, "Starting to compress data"); - - /* Clamp compression level to valid zstd range [1, 22] */ - if (compression_level < 1) { - log_message(LOG_LEVEL_WARNING, "compression_level %d out of range [1,22], using 1", - compression_level); - compression_level = 1; - } else if (compression_level > 22) { - log_message(LOG_LEVEL_WARNING, "compression_level %d out of range [1,22], using 22", - compression_level); - compression_level = 22; - } - size_t dst_size = ZSTD_compressBound(data_to_compress->size); Data* compressed_data = data_create_empty(dst_size); if (compressed_data == NULL) diff --git a/src/shared/file.c b/src/shared/file.c index 443330e..58b0455 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -155,43 +155,6 @@ static void* old_data_from_path(const char* full_path, unsigned long long old_si return data; } -/** - * Helper: receive data from wire, optionally decompress, and store in file. - * On success, returns the received Data* (caller owns it). On failure, returns NULL. - * If `file_data` is received via receive_data(fd), this function handles decompression - * when config->use_compression is set. - */ -static Data* receive_and_decompress(int fd, const Config* config) { - Data* file_data = receive_data(fd); - if (file_data == NULL) - return NULL; - if (config->use_compression) { - Data* uncompressed = data_decompress(file_data); - data_destroy(file_data); - if (uncompressed == NULL) - return NULL; - file_data = uncompressed; - } - return file_data; -} - -/** - * Helper: receive metadata from wire and assign to file. - * Returns true on success (metadata may be NULL if absent), false on I/O error. - */ -static bool receive_and_assign_metadata(int fd, const Config* config, File* file) { - if (!config->use_metadata) - return true; - int meta_ok = 1; - file->metadata = metadata_receive(fd, &meta_ok); - if (!meta_ok) { - file_destroy(file); - send_status(fd, STATUS_ERROR); - return false; - } - return true; -} - static File* receive_delta_file(int fd, const Config* config, const char* check_path, void* old_data, unsigned long long old_size) { if (!old_data) @@ -307,16 +270,34 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_ return NULL; } - if (!receive_and_assign_metadata(fd, config, file)) - return NULL; + if (config->use_metadata) { + int meta_ok = 1; + file->metadata = metadata_receive(fd, &meta_ok); + if (!meta_ok) { + file_destroy(file); + send_status(fd, STATUS_ERROR); + return NULL; + } + } - Data* file_data = receive_and_decompress(fd, config); + Data* file_data = receive_data(fd); if (file_data == NULL) { file_destroy(file); send_status(fd, STATUS_ERROR); return NULL; } + if (config->use_compression) { + Data* uncompressed = data_decompress(file_data); + data_destroy(file_data); + if (uncompressed == NULL) { + file_destroy(file); + send_status(fd, STATUS_ERROR); + return NULL; + } + file_data = uncompressed; + } + data_destroy(file->data); file->data = file_data; return file; @@ -394,16 +375,34 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) { return NULL; } - if (!receive_and_assign_metadata(fd, config, file)) - return NULL; + if (config->use_metadata) { + int meta_ok = 1; + file->metadata = metadata_receive(fd, &meta_ok); + if (!meta_ok) { + file_destroy(file); + send_status(fd, STATUS_ERROR); + return NULL; + } + } - Data* file_data = receive_and_decompress(fd, config); + Data* file_data = receive_data(fd); if (file_data == NULL) { file_destroy(file); send_status(fd, STATUS_ERROR); return NULL; } + if (config->use_compression) { + Data* uncompressed = data_decompress(file_data); + data_destroy(file_data); + if (uncompressed == NULL) { + file_destroy(file); + send_status(fd, STATUS_ERROR); + return NULL; + } + file_data = uncompressed; + } + data_destroy(file->data); file->data = file_data; return file; diff --git a/src/shared/log.c b/src/shared/log.c index 0479812..bcf91bf 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -10,7 +10,7 @@ void set_log_level(LogLevel level) { current_log_level = level; } -void log_message(LogLevel log_level, const char* format, ...) { +void log_message(LogLevel log_level, char* format, ...) { if (log_level < current_log_level) return; time_t now = time(NULL); diff --git a/src/shared/log.h b/src/shared/log.h index 3c37ced..ccff5dc 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -3,7 +3,7 @@ typedef enum { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } LogLevel; -void log_message(LogLevel log_level, const char* message, ...); +void log_message(LogLevel log_level, char* message, ...); void set_log_level(LogLevel level); #endif diff --git a/src/shared/protocol.c b/src/shared/protocol.c index bd0f385..c3dcb74 100644 --- a/src/shared/protocol.c +++ b/src/shared/protocol.c @@ -1,7 +1,6 @@ #include "protocol.h" #include "log.h" #include -#include #include #include #include @@ -35,14 +34,11 @@ static void bw_throttle(size_t bytes_written) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - /* Use unsigned long long for elapsed_ns to avoid overflow in multiplication. - * time_t differences fit comfortably in 64-bit for any practical runtime. */ - unsigned long long elapsed_ns = - (unsigned long long)(now.tv_sec - bw_last_refill.tv_sec) * 1000000000ULL + - (unsigned long long)(now.tv_nsec - bw_last_refill.tv_nsec); + long long elapsed_ns = + (now.tv_sec - bw_last_refill.tv_sec) * 1000000000LL + (now.tv_nsec - bw_last_refill.tv_nsec); bw_last_refill = now; - long long tokens_to_add = (long long)((double)io_bwlimit * (double)elapsed_ns / 1000000000.0); + long long tokens_to_add = (long long)((double)io_bwlimit * elapsed_ns / 1000000000.0); bw_tokens += tokens_to_add; if (bw_tokens > (long long)io_bwlimit) bw_tokens = (long long)io_bwlimit; @@ -50,9 +46,7 @@ static void bw_throttle(size_t bytes_written) { bw_tokens -= (long long)bytes_written; if (bw_tokens < 0) { - long long deficit_ns = (long long)((double)(-bw_tokens) / (double)io_bwlimit * 1000000000.0); - if (deficit_ns < 0) - deficit_ns = 0; + long long deficit_ns = (long long)((double)(-bw_tokens) / io_bwlimit * 1000000000.0); struct timespec sleep_time, remaining; sleep_time.tv_sec = deficit_ns / 1000000000LL; sleep_time.tv_nsec = deficit_ns % 1000000000LL; @@ -85,11 +79,6 @@ bool send_n_data(int file_descriptor, const void* data, size_t data_size) { else bytes_send = write(fd, (const char*)data + total_bytes_send, chunk); if (bytes_send <= 0) { - if (io_ssl) { - int ssl_err = SSL_get_error(io_ssl, (int)bytes_send); - if (ssl_err == SSL_ERROR_WANT_WRITE || ssl_err == SSL_ERROR_WANT_READ) - continue; - } log_message(LOG_LEVEL_ERROR, "Could not send data"); return false; } @@ -113,11 +102,6 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) { bytes_received = read(fd, (char*)data + total_bytes_received, data_size - total_bytes_received); if (bytes_received <= 0) { - if (io_ssl) { - int ssl_err = SSL_get_error(io_ssl, (int)bytes_received); - if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) - continue; - } if (bytes_received == 0) log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data"); else diff --git a/src/shared/utils.c b/src/shared/utils.c index d1425be..528191b 100644 --- a/src/shared/utils.c +++ b/src/shared/utils.c @@ -70,23 +70,6 @@ char* str_dup(const char* string) { bool glob_match(const char* pattern, const char* str) { while (*pattern) { if (*pattern == '*') { - /* Check for double-star (globstar) pattern */ - if (*(pattern + 1) == '*') { - pattern += 2; - /* Trailing double-star matches everything */ - if (*pattern == '\0') - return true; - /* double-star slash: match at any depth */ - if (*pattern == '/') - pattern++; - while (*str) { - if (glob_match(pattern, str)) - return true; - str++; - } - return glob_match(pattern, str); - } - /* Single * — does not cross / boundaries */ pattern++; while (*str && *str != '/') { if (glob_match(pattern, str)) @@ -100,17 +83,8 @@ bool glob_match(const char* pattern, const char* str) { pattern++; str++; } else { - if (*pattern != *str) { - /* If pattern has a '/' followed by '**', allow zero path components */ - if (*pattern == '/' && *(pattern + 1) == '*' && *(pattern + 2) == '*') { - /* Skip over slash-double-star and try to match rest against current str */ - const char* rest = pattern + 3; - if (*rest == '/') - rest++; - return glob_match(rest, str); - } + if (*pattern != *str) return false; - } pattern++; str++; } diff --git a/tests/test_glob.c b/tests/test_glob.c index ded4bc6..5658d06 100644 --- a/tests/test_glob.c +++ b/tests/test_glob.c @@ -49,33 +49,6 @@ static void test_glob_question_star() { EXPECT_TRUE(glob_match("?*.txt", "a.txt")); } -static void test_glob_doublestar_match_all() { - EXPECT_TRUE(glob_match("**", "anything")); - EXPECT_TRUE(glob_match("**", "path/to/file")); -} - -static void test_glob_doublestar_prefix() { - EXPECT_TRUE(glob_match("**/foo", "foo")); - EXPECT_TRUE(glob_match("**/foo", "bar/foo")); - EXPECT_TRUE(glob_match("**/foo", "a/b/c/foo")); - EXPECT_FALSE(glob_match("**/foo", "foobar")); - EXPECT_FALSE(glob_match("**/foo", "bar/foobar")); -} - -static void test_glob_doublestar_suffix() { - EXPECT_TRUE(glob_match("foo/**", "foo")); - EXPECT_TRUE(glob_match("foo/**", "foo/bar")); - EXPECT_TRUE(glob_match("foo/**", "foo/bar/baz")); - EXPECT_FALSE(glob_match("foo/**", "foobar")); -} - -static void test_glob_doublestar_mid() { - EXPECT_TRUE(glob_match("a/**/b", "a/b")); - EXPECT_TRUE(glob_match("a/**/b", "a/x/b")); - EXPECT_TRUE(glob_match("a/**/b", "a/x/y/z/b")); - EXPECT_FALSE(glob_match("a/**/b", "a/x/bad")); -} - void test_glob() { test_glob_exact_match(); test_glob_question_mark(); @@ -87,8 +60,4 @@ void test_glob() { test_glob_slash_not_matched(); test_glob_complex(); test_glob_question_star(); - test_glob_doublestar_match_all(); - test_glob_doublestar_prefix(); - test_glob_doublestar_suffix(); - test_glob_doublestar_mid(); }