--- lbbs/src/test_file_loader.c 2025/05/18 08:17:25 1.4 +++ lbbs/src/test_file_loader.c 2025/11/04 13:49:51 1.10 @@ -1,26 +1,21 @@ -/*************************************************************************** - 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 by Leaflet + */ #include "file_loader.h" #include "log.h" +#include "trie_dict.h" +#include #include #include -#include #include +#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" + const char *files[] = { "../data/welcome.txt", "../data/copyright.txt", @@ -40,6 +35,7 @@ int main(int argc, char *argv[]) long line_total; const void *p_data; const long *p_line_offsets; + FILE *fp; if (log_begin("../log/bbsd.log", "../log/error.log") < 0) { @@ -47,8 +43,21 @@ int main(int argc, char *argv[]) return -1; } - log_std_redirect(STDOUT_FILENO); - log_err_redirect(STDERR_FILENO); + log_common_redir(STDOUT_FILENO); + log_error_redir(STDERR_FILENO); + + if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL) + { + log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE); + return -1; + } + fclose(fp); + + if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) + { + printf("trie_dict_init failed\n"); + return -1; + } ret = file_loader_init(); if (ret < 0) @@ -67,7 +76,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]); } @@ -75,7 +84,7 @@ int main(int argc, char *argv[]) for (i = 0; i < files_cnt; i++) { - if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == 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]); } @@ -87,6 +96,11 @@ int main(int argc, char *argv[]) { printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]); } + + if (detach_file_shm(p_shm) < 0) + { + log_error("detach_file_shm(%s) error\n", files[i]); + } } } @@ -94,7 +108,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]); } @@ -102,7 +116,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]); } @@ -114,7 +128,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]); } @@ -125,13 +139,18 @@ int main(int argc, char *argv[]) { if (i % 2 != 0) { - if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == 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], data_len, line_total); + + if (detach_file_shm(p_shm) < 0) + { + log_error("detach_file_shm(%s) error\n", files[i]); + } } } } @@ -139,6 +158,14 @@ int main(int argc, char *argv[]) file_loader_cleanup(); file_loader_cleanup(); + trie_dict_cleanup(); + + if (unlink(TRIE_DICT_SHM_FILE) < 0) + { + log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); + return -1; + } + log_end(); return 0;