--- lbbs/src/test_file_loader.c 2025/05/17 05:54:42 1.2 +++ lbbs/src/test_file_loader.c 2025/11/11 00:28:05 1.13 @@ -1,26 +1,25 @@ -/*************************************************************************** - 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 "trie_dict.h" +#include #include #include -#include #include +static const char TRIE_DICT_SHM_FILE[] = "~trie_dict_shm.dat"; + const char *files[] = { "../data/welcome.txt", "../data/copyright.txt", @@ -35,7 +34,12 @@ int main(int argc, char *argv[]) { int ret; int i; - const void *p_file_shm; + const void *p_shm; + size_t data_len; + long line_total; + const void *p_data; + const long *p_line_offsets; + FILE *fp; if (log_begin("../log/bbsd.log", "../log/error.log") < 0) { @@ -43,8 +47,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) @@ -63,7 +80,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]); } @@ -71,17 +88,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); - if (shmdt(p_file_shm) == -1) + for (int j = 0; j < line_total; j++) { - log_error("shmdt() error (%d)\n", errno); + 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]); } } } @@ -90,7 +112,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 +120,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 +132,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,17 +143,17 @@ 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]); } } } @@ -140,6 +162,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;