Merge pull request 'Fix logic/correctness bugs (#73, #68, #67, #66, #59, #58, #54, #48, #53)' (#82) from fix/logic-correctness into main
CI / lint (push) Failing after 8s
CI / build-and-test (push) Has been skipped
CI / sanitizers (address) (push) Has been skipped
CI / sanitizers (undefined) (push) Has been skipped
CI / fuzz-build (push) Has been skipped
CI / coverage (push) Has been skipped
CI / valgrind (push) Has been skipped
CI / lint (push) Failing after 8s
CI / build-and-test (push) Has been skipped
CI / sanitizers (address) (push) Has been skipped
CI / sanitizers (undefined) (push) Has been skipped
CI / fuzz-build (push) Has been skipped
CI / coverage (push) Has been skipped
CI / valgrind (push) Has been skipped
This commit was merged in pull request #82.
This commit is contained in:
+16
-2
@@ -92,7 +92,14 @@ 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) {
|
||||
config->ssh_port = atoi(argv[++i]);
|
||||
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;
|
||||
} else if (strcmp(argv[i], "--delete") == 0) {
|
||||
config->use_delete = true;
|
||||
} else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) {
|
||||
@@ -169,7 +176,14 @@ 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) {
|
||||
config->server_port = atoi(argv[++i]);
|
||||
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;
|
||||
} else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) {
|
||||
char* end;
|
||||
errno = 0;
|
||||
|
||||
+8
-4
@@ -113,13 +113,11 @@ 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;
|
||||
if (g_server) {
|
||||
server_delete(&g_server);
|
||||
}
|
||||
_exit(0);
|
||||
g_server_cleanup_requested = 1;
|
||||
}
|
||||
|
||||
static void print_server_usage(void) {
|
||||
@@ -209,5 +207,11 @@ 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;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,18 @@
|
||||
|
||||
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)
|
||||
|
||||
+43
-42
@@ -155,6 +155,43 @@ 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)
|
||||
@@ -270,34 +307,16 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (!receive_and_assign_metadata(fd, config, file))
|
||||
return NULL;
|
||||
|
||||
Data* file_data = receive_data(fd);
|
||||
Data* file_data = receive_and_decompress(fd, config);
|
||||
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;
|
||||
@@ -375,34 +394,16 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (!receive_and_assign_metadata(fd, config, file))
|
||||
return NULL;
|
||||
|
||||
Data* file_data = receive_data(fd);
|
||||
Data* file_data = receive_and_decompress(fd, config);
|
||||
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;
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ void set_log_level(LogLevel level) {
|
||||
current_log_level = level;
|
||||
}
|
||||
|
||||
void log_message(LogLevel log_level, char* format, ...) {
|
||||
void log_message(LogLevel log_level, const char* format, ...) {
|
||||
if (log_level < current_log_level)
|
||||
return;
|
||||
time_t now = time(NULL);
|
||||
|
||||
+1
-1
@@ -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, char* message, ...);
|
||||
void log_message(LogLevel log_level, const char* message, ...);
|
||||
void set_log_level(LogLevel level);
|
||||
|
||||
#endif
|
||||
|
||||
+20
-4
@@ -1,6 +1,7 @@
|
||||
#include "protocol.h"
|
||||
#include "log.h"
|
||||
#include <errno.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -34,11 +35,14 @@ static void bw_throttle(size_t bytes_written) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
||||
long long elapsed_ns =
|
||||
(now.tv_sec - bw_last_refill.tv_sec) * 1000000000LL + (now.tv_nsec - bw_last_refill.tv_nsec);
|
||||
/* 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);
|
||||
bw_last_refill = now;
|
||||
|
||||
long long tokens_to_add = (long long)((double)io_bwlimit * elapsed_ns / 1000000000.0);
|
||||
long long tokens_to_add = (long long)((double)io_bwlimit * (double)elapsed_ns / 1000000000.0);
|
||||
bw_tokens += tokens_to_add;
|
||||
if (bw_tokens > (long long)io_bwlimit)
|
||||
bw_tokens = (long long)io_bwlimit;
|
||||
@@ -46,7 +50,9 @@ 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) / io_bwlimit * 1000000000.0);
|
||||
long long deficit_ns = (long long)((double)(-bw_tokens) / (double)io_bwlimit * 1000000000.0);
|
||||
if (deficit_ns < 0)
|
||||
deficit_ns = 0;
|
||||
struct timespec sleep_time, remaining;
|
||||
sleep_time.tv_sec = deficit_ns / 1000000000LL;
|
||||
sleep_time.tv_nsec = deficit_ns % 1000000000LL;
|
||||
@@ -79,6 +85,11 @@ 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;
|
||||
}
|
||||
@@ -102,6 +113,11 @@ 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
|
||||
|
||||
+27
-1
@@ -70,6 +70,23 @@ 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))
|
||||
@@ -83,8 +100,17 @@ bool glob_match(const char* pattern, const char* str) {
|
||||
pattern++;
|
||||
str++;
|
||||
} else {
|
||||
if (*pattern != *str)
|
||||
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);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
pattern++;
|
||||
str++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user