--- lbbs/src/file_loader.c 2025/05/17 15:39:51 1.5 +++ lbbs/src/file_loader.c 2025/11/04 14:30:44 1.19 @@ -1,35 +1,32 @@ -/*************************************************************************** - 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 */ +/* + * file_loader + * - shared memory based file loader + * + * Copyright (C) 2004-2025 by Leaflet + */ #include "file_loader.h" -#include "trie_dict.h" -#include "str_process.h" #include "log.h" -#include +#include "str_process.h" +#include "trie_dict.h" #include -#include +#include #include #include #include +#include +#include #include -#include #include -#include +#include -#define MAX_SPLIT_FILE_LINES 65536 +struct shm_header_t +{ + int shmid; + size_t data_len; + long line_total; +}; static TRIE_NODE *p_trie_file_dict = NULL; @@ -51,11 +48,13 @@ int file_loader_init() return 0; } -static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid) +static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) { - if (shmctl((int)shmid, IPC_RMID, NULL) == -1) + int shmid = (int)value; + + if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); + log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); } } @@ -68,9 +67,11 @@ void file_loader_cleanup(void) trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb); trie_dict_destroy(p_trie_file_dict); + + p_trie_file_dict = NULL; } -int load_file_shm(const char *filename) +int load_file(const char *filename) { int fd; struct stat sb; @@ -80,11 +81,11 @@ int load_file_shm(const char *filename) key_t key; size_t size; int shmid; - int64_t shmid_old; void *p_shm; long line_total; long line_offsets[MAX_SPLIT_FILE_LINES]; long *p_line_offsets; + int64_t shmid_old; if ((fd = open(filename, O_RDONLY)) < 0) { @@ -112,11 +113,7 @@ int load_file_shm(const char *filename) return -1; } - line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES); - if (line_total >= MAX_SPLIT_FILE_LINES) - { - log_error("split_data_lines() truncated over limit lines\n"); - } + line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL); // Allocate shared memory proj_id = (int)(time(NULL) % getpid()); @@ -127,7 +124,7 @@ int load_file_shm(const char *filename) return -2; } - size = sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); + size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); if (shmid == -1) { @@ -137,13 +134,14 @@ int load_file_shm(const char *filename) p_shm = shmat(shmid, NULL, 0); if (p_shm == (void *)-1) { - log_error("shmat() error (%d)\n", errno); + log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); return -3; } - *((size_t *)p_shm) = data_len; - *((long *)(p_shm + sizeof(data_len))) = line_total; - memcpy(p_shm + sizeof(data_len) + sizeof(line_total), p_data, data_len); + ((struct shm_header_t *)p_shm)->shmid = shmid; + ((struct shm_header_t *)p_shm)->data_len = data_len; + ((struct shm_header_t *)p_shm)->line_total = line_total; + memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len); if (munmap(p_data, data_len) < 0) { @@ -151,13 +149,13 @@ int load_file_shm(const char *filename) return -2; } - p_data = p_shm + sizeof(data_len) + sizeof(line_total); - p_line_offsets = p_data + data_len + 1; + p_data = (char *)p_shm + sizeof(struct shm_header_t); + p_line_offsets = (long *)((char *)p_data + data_len + 1); memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); if (shmdt(p_shm) == -1) { - log_error("shmdt() error (%d)\n", errno); + log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); return -3; } @@ -174,7 +172,7 @@ int load_file_shm(const char *filename) { log_error("trie_dict_set(%s) error\n", filename); - if (shmctl(shmid, IPC_RMID, NULL) == -1) + if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) { log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); } @@ -185,11 +183,11 @@ int load_file_shm(const char *filename) return 0; } -int unload_file_shm(const char *filename) +int unload_file(const char *filename) { - int64_t shmid = 0; + int64_t shmid; - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&shmid) != 1) + if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) { log_error("trie_dict_get(%s) not found\n", filename); return -1; @@ -209,7 +207,7 @@ int unload_file_shm(const char *filename return 0; } -const void *get_file_shm(const char *filename) +const void *get_file_shm_readonly(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) { int64_t shmid; const void *p_shm; @@ -229,9 +227,30 @@ const void *get_file_shm(const char *fil p_shm = shmat((int)shmid, NULL, SHM_RDONLY); if (p_shm == (void *)-1) { - log_error("shmat() error (%d)\n", errno); + log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno); return NULL; } + *p_data_len = ((struct shm_header_t *)p_shm)->data_len; + *p_line_total = ((struct shm_header_t *)p_shm)->line_total; + *pp_data = (char *)p_shm + sizeof(struct shm_header_t); + *pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1); + return p_shm; } + +int detach_file_shm(const void *p_shm) +{ + if (p_shm == NULL) + { + return -2; + } + + if (shmdt(p_shm) == -1) + { + log_error("shmdt() error (%d)\n", errno); + return -1; + } + + return 0; +}