From 62ff1d3929bc5d277542ba637c99daf7e9c546f4 Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 20 Jul 2026 20:11:01 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20review=20fixes=20=E2=80=94=20test=20clea?= =?UTF-8?q?nup,=20wire=20protocol,=20clang-format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shared/file.c | 21 +++++++++++++++++++++ tests/test_file.c | 3 +++ tests/test_file_sendfile.c | 19 +++++++++++-------- tests/test_log.c | 2 +- tests/test_multiprocessing.c | 20 ++++++++++---------- tests/test_scanner.c | 11 ++++------- 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/shared/file.c b/src/shared/file.c index 58b0455..e7c43bc 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -118,6 +118,11 @@ bool file_send_single_calls(File* file, int file_descriptor, bool use_metadata, data_destroy(compressed_data); return false; } + int ft = 0; // FILE_TYPE_REGULAR + if (!send_int(file_descriptor, ft)) { + data_destroy(compressed_data); + return false; + } if (!send_data(file_descriptor, data_to_send)) { data_destroy(compressed_data); return false; @@ -385,6 +390,13 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) { } } + int file_type; + if (!receive_int(fd, &file_type)) { + file_destroy(file); + send_status(fd, STATUS_ERROR); + return NULL; + } + Data* file_data = receive_data(fd); if (file_data == NULL) { file_destroy(file); @@ -462,6 +474,10 @@ bool file_send_sendfile(File* file, int file_descriptor, bool use_metadata, int if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false; + int ft = 0; // FILE_TYPE_REGULAR + if (!send_int(file_descriptor, ft)) + return false; + int fd = open(file->path, O_RDONLY); if (fd == -1) { perror("Could not open file for sendfile"); @@ -504,6 +520,11 @@ File* file_receive(const Config* config, int file_descriptor) { return NULL; } } + int file_type; + if (!receive_int(file_descriptor, &file_type)) { + file_destroy(file); + return NULL; + } Data* file_data = receive_data(file_descriptor); if (file_data == NULL) { file_destroy(file); diff --git a/tests/test_file.c b/tests/test_file.c index 1a65a05..d8cc86d 100644 --- a/tests/test_file.c +++ b/tests/test_file.c @@ -217,6 +217,9 @@ static void test_file_send_no_path() { pid_t pid = fork(); if (pid == 0) { close(p[1]); + int file_type; + EXPECT_TRUE(receive_int(p[0], &file_type)); + EXPECT_EQ_INT(file_type, 0); /* FILE_TYPE_REGULAR */ Data* received = receive_data(p[0]); close(p[0]); diff --git a/tests/test_file_sendfile.c b/tests/test_file_sendfile.c index 871fc73..4b32326 100644 --- a/tests/test_file_sendfile.c +++ b/tests/test_file_sendfile.c @@ -22,8 +22,8 @@ static void test_sendfile_basic() { /* Set the size so file_send_sendfile can report it */ file->data->size = len; - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; @@ -80,8 +80,8 @@ static void test_sendfile_empty_file() { EXPECT_NOT_NULL(file); file->data->size = 0; - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; @@ -161,8 +161,8 @@ static void test_sendfile_compression_fallback() { file->data->size = (size_t)st.st_size; EXPECT_TRUE(file_load_data(file)); - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, true, false, 3, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, true, false, 3, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; @@ -224,8 +224,11 @@ static void test_sendfile_no_path() { pid_t pid = fork(); if (pid == 0) { close(p[1]); - /* When send_path is false, the sender sends raw data (size + bytes) only. - * We need to receive just the Data, not a File. */ + /* When send_path is false, the sender sends file_type + raw data (size + bytes). + * Read file_type first with assertion, then receive data. */ + int file_type; + EXPECT_TRUE(receive_int(p[0], &file_type)); + EXPECT_EQ_INT(file_type, 0); /* FILE_TYPE_REGULAR */ Data* received = receive_data(p[0]); close(p[0]); diff --git a/tests/test_log.c b/tests/test_log.c index 4fc4798..dc73e77 100644 --- a/tests/test_log.c +++ b/tests/test_log.c @@ -47,7 +47,7 @@ static void test_log_set_level_info() { set_log_level(LOG_LEVEL_INFO); /* INFO level should show INFO, WARNING, ERROR but not DEBUG */ - log_message(LOG_LEVEL_DEBUG, "debug should be filtered"); /* filtered */ + log_message(LOG_LEVEL_DEBUG, "debug should be filtered"); /* filtered */ log_message(LOG_LEVEL_INFO, "info should show"); log_message(LOG_LEVEL_WARNING, "warning should show"); log_message(LOG_LEVEL_ERROR, "error should show"); diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c index 95bc3ad..741bcf0 100644 --- a/tests/test_multiprocessing.c +++ b/tests/test_multiprocessing.c @@ -8,8 +8,8 @@ /* Test pipeline_context_sender_create/destroy with valid arguments */ static void test_sender_create_destroy() { - Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q_scanner = queue_create(5, NULL); @@ -32,8 +32,8 @@ static void test_sender_create_destroy() { /* Test pipeline_context_receiver_create/destroy with valid arguments */ static void test_receiver_create_destroy() { - Config* cfg = config_create(str_dup("2.0"), str_dup("/src"), str_dup("/dst"), - true, true, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("2.0"), str_dup("/src"), str_dup("/dst"), true, true, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q = queue_create(20, NULL); @@ -51,8 +51,8 @@ static void test_receiver_create_destroy() { /* Test that create handles various 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, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("3.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); /* Single-element queues */ @@ -67,8 +67,8 @@ static void test_sender_queue_capacities() { /* Test that create handles zero-capacity queues */ static void test_sender_zero_capacity() { - Config* cfg = config_create(str_dup("4.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("4.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q1 = queue_create(0, NULL); @@ -82,8 +82,8 @@ static void test_sender_zero_capacity() { /* Test receiver with zero file_descriptor */ static void test_receiver_fd_zero() { - Config* cfg = config_create(str_dup("5.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("5.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); Queue* q = queue_create(5, NULL); PipelineContextReceiver* ctx = pipeline_context_receiver_create(cfg, q, 0); EXPECT_NOT_NULL(ctx); diff --git a/tests/test_scanner.c b/tests/test_scanner.c index aaa8fcf..0f0ea43 100644 --- a/tests/test_scanner.c +++ b/tests/test_scanner.c @@ -248,9 +248,6 @@ static void test_scanner_max_size() { const char* dir = "test_scan_max"; const char* small = "test_scan_max/small.txt"; const char* large = "test_scan_max/large.txt"; - create_test_file(small, "tiny"); - create_test_file(large, "this_content_is_longer_than_ten_chars"); - mkdir(dir, 0755); create_test_file(small, "tiny"); create_test_file(large, "this_content_is_longer_than_ten_chars"); @@ -336,10 +333,10 @@ static void test_scanner_size_range() { static void test_scanner_mixed_patterns() { /* Combine exclude, include, and size filters together */ const char* dir = "test_scan_mixed"; - const char* a_txt = "test_scan_mixed/a.txt"; /* size ~= 5 */ - const char* b_bin = "test_scan_mixed/b.bin"; /* size ~= 13 */ - const char* c_txt = "test_scan_mixed/c.txt"; /* size ~= 5 */ - const char* d_bak = "test_scan_mixed/d.bak"; /* size ~= 42 */ + const char* a_txt = "test_scan_mixed/a.txt"; /* size ~= 5 */ + const char* b_bin = "test_scan_mixed/b.bin"; /* size ~= 13 */ + const char* c_txt = "test_scan_mixed/c.txt"; /* size ~= 5 */ + const char* d_bak = "test_scan_mixed/d.bak"; /* size ~= 42 */ mkdir(dir, 0755); create_test_file(a_txt, "aaaaa");