--- lbbs/src/test_file_loader.c 2025/05/17 05:54:42 1.2 +++ lbbs/src/test_file_loader.c 2025/11/20 01:09:27 1.15 @@ -1,24 +1,22 @@ -/*************************************************************************** - file_loader.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * test_file_loader + * - tester for shared memory based file loader + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "file_loader.h" #include "log.h" +#include +#include #include +#include #include -#include #include const char *files[] = { @@ -33,9 +31,12 @@ int files_cnt = 6; int main(int argc, char *argv[]) { - int ret; int i; - const void *p_file_shm; + void *p_shm; + size_t data_len; + long line_total; + const void *p_data; + const long *p_line_offsets; if (log_begin("../log/bbsd.log", "../log/error.log") < 0) { @@ -43,27 +44,14 @@ int main(int argc, char *argv[]) return -1; } - log_std_redirect(STDOUT_FILENO); - log_err_redirect(STDERR_FILENO); - - ret = file_loader_init(); - if (ret < 0) - { - printf("file_loader_init() error (%d)\n", ret); - return ret; - } - - ret = file_loader_init(); - if (ret == 0) - { - printf("Rerun file_loader_init() error\n"); - } + log_common_redir(STDOUT_FILENO); + log_error_redir(STDERR_FILENO); printf("Testing #1\n"); for (i = 0; i < files_cnt; i++) { - if (load_file_shm(files[i]) < 0) + if (load_file(files[i]) < 0) { printf("Load file %s error\n", files[i]); } @@ -71,17 +59,22 @@ int main(int argc, char *argv[]) for (i = 0; i < files_cnt; i++) { - if ((p_file_shm = get_file_shm(files[i])) == NULL) + if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) { printf("Get file shm %s error\n", files[i]); } else { - printf("File: %s size: %ld lines: %ld\n", files[i], *((size_t *)p_file_shm), *((size_t *)(p_file_shm + sizeof(size_t)))); + printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total); + + for (int j = 0; j < line_total; j++) + { + printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]); + } - if (shmdt(p_file_shm) == -1) + if (detach_file_shm(p_shm) < 0) { - log_error("shmdt() error (%d)\n", errno); + log_error("detach_file_shm(%s) error\n", files[i]); } } } @@ -90,7 +83,7 @@ int main(int argc, char *argv[]) for (i = 0; i < files_cnt; i++) { - if (unload_file_shm(files[i]) < 0) + if (unload_file(files[i]) < 0) { printf("Unload file %s error\n", files[i]); } @@ -98,7 +91,7 @@ int main(int argc, char *argv[]) for (i = 0; i < files_cnt; i++) { - if (load_file_shm(files[i]) < 0) + if (load_file(files[i]) < 0) { printf("Load file %s error\n", files[i]); } @@ -110,7 +103,7 @@ int main(int argc, char *argv[]) { if (i % 2 == 0) { - if (unload_file_shm(files[i]) < 0) + if (unload_file(files[i]) < 0) { printf("Unload file %s error\n", files[i]); } @@ -121,25 +114,22 @@ int main(int argc, char *argv[]) { if (i % 2 != 0) { - if ((p_file_shm = get_file_shm(files[i])) == NULL) + if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) { printf("Get file shm %s error\n", files[i]); } else { - printf("File: %s size: %ld lines: %ld\n", files[i], *((size_t *)p_file_shm), *((size_t *)(p_file_shm + sizeof(size_t)))); + printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total); - if (shmdt(p_file_shm) == -1) + if (detach_file_shm(p_shm) < 0) { - log_error("shmdt() error (%d)\n", errno); + log_error("detach_file_shm(%s) error\n", files[i]); } } } } - file_loader_cleanup(); - file_loader_cleanup(); - log_end(); return 0;