--- lbbs/src/file_loader.c 2025/05/16 12:22:35 1.1 +++ lbbs/src/file_loader.c 2026/01/03 10:27:14 1.27 @@ -1,237 +1,251 @@ -/*************************************************************************** - 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-2026 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "file_loader.h" -#include "trie_dict.h" -#include "str_process.h" #include "log.h" -#include +#include "str_process.h" #include -#include +#include #include +#include +#include +#include #include #include -#define FILE_MMAP_COUNT_LIMIT 256 - -static FILE_MMAP *p_file_mmap_pool = NULL; -static int file_mmap_count = 0; -static int file_mmap_free_index = -1; -static TRIE_NODE *p_trie_file_dict = NULL; +struct shm_header_t +{ + size_t shm_size; + size_t data_len; + long line_total; +}; -int file_loader_init(int max_file_mmap_count) +int load_file(const char *filename) { - if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT) + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_NAME_LEN]; + int fd; + struct stat sb; + void *p_data; + size_t data_len; + size_t size; + void *p_shm; + long line_total; + long line_offsets[MAX_SPLIT_FILE_LINES]; + long *p_line_offsets; + + if (filename == NULL) { - log_error("file_loader_init(%d) argument error\n", max_file_mmap_count); + log_error("NULL pointer error"); return -1; } - if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL) + if ((fd = open(filename, O_RDONLY)) < 0) { - log_error("File loader already initialized\n"); + log_error("open(%s) error (%d)", filename, errno); return -1; } - p_file_mmap_pool = (FILE_MMAP *)calloc((size_t)max_file_mmap_count, sizeof(FILE_MMAP)); - if (p_file_mmap_pool == NULL) + if (fstat(fd, &sb) < 0) { - log_error("calloc(%d p_file_mmap_pool) error\n", max_file_mmap_count); - return -2; + log_error("fstat(fd) error (%d)", errno); + close(fd); + return -1; } - file_mmap_count = max_file_mmap_count; - file_mmap_free_index = 0; - - p_trie_file_dict = trie_dict_create(); - if (p_trie_file_dict == NULL) + data_len = (size_t)sb.st_size; + p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L); + if (p_data == MAP_FAILED) { - log_error("trie_dict_create() error\n"); + log_error("mmap() error (%d)", errno); + close(fd); return -2; } - return 0; -} - -void file_loader_cleanup(void) -{ - if (p_trie_file_dict != NULL) + if (close(fd) < 0) { - trie_dict_destroy(p_trie_file_dict); - p_trie_file_dict = NULL; + log_error("close(fd) error (%d)", errno); + return -1; } - if (p_file_mmap_pool != NULL) - { - for (int i = 0; i < file_mmap_count; i++) - { - if (p_file_mmap_pool[i].p_data == NULL) - { - continue; - } - - if (munmap(p_file_mmap_pool[i].p_data, p_file_mmap_pool[i].size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } - } - free(p_file_mmap_pool); - p_file_mmap_pool = NULL; - } + line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL); - file_mmap_count = 0; - file_mmap_free_index = -1; -} + // Allocate shared memory + size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); -int load_file_mmap(const char *filename) -{ - int fd; - struct stat sb; - void *p_data; - size_t size; - int file_mmap_index = 0; + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); - if ((fd = open(filename, O_RDONLY)) < 0) + if (shm_unlink(shm_name) == -1 && errno != ENOENT) { - log_error("open(%s) error (%d)\n", filename, errno); - return -1; + log_error("shm_unlink(%s) error (%d)", shm_name, errno); + return -2; } - if (fstat(fd, &sb) < 0) + if ((fd = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) { - log_error("fstat(fd) error (%d)\n", errno); - return -1; + log_error("shm_open(%s) error (%d)", shm_name, errno); + return -2; + } + if (ftruncate(fd, (off_t)size) == -1) + { + log_error("ftruncate(size=%d) error (%d)", size, errno); + close(fd); + return -2; } - size = (size_t)sb.st_size; - - p_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0L); - if (p_data == MAP_FAILED) + p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) { - log_error("mmap() error (%d)\n", errno); + log_error("mmap() error (%d)", errno); + close(fd); return -2; } if (close(fd) < 0) { - log_error("close(fd) error (%d)\n", errno); + log_error("close(fd) error (%d)", errno); return -1; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) == 0) // Not exist + ((struct shm_header_t *)p_shm)->shm_size = size; + ((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) { - if (file_mmap_free_index == -1) - { - log_error("file_mmap_pool is depleted\n"); - return -3; - } + log_error("munmap() error (%d)", errno); + munmap(p_shm, size); + return -2; + } - file_mmap_index = file_mmap_free_index; + 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 (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1) - { - log_error("trie_dict_set(%s) error\n", filename); + if (munmap(p_shm, size) < 0) + { + log_error("munmap() error (%d)", errno); + return -2; + } - if (munmap(p_data, size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } + return 0; +} - return -2; - } +int unload_file(const char *filename) +{ + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_NAME_LEN]; - do - { - file_mmap_free_index++; - if (file_mmap_free_index >= file_mmap_count) - { - file_mmap_free_index = 0; - } - if (file_mmap_free_index == file_mmap_index) // loop - { - file_mmap_free_index = -1; - break; - } - } while (p_file_mmap_pool[file_mmap_free_index].p_data != NULL); - } - else + if (filename == NULL) { - // Unload existing data - if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) - { - log_error("munmap() error (%d)\n", errno); - } + log_error("NULL pointer error"); + return -1; } - p_file_mmap_pool[file_mmap_index].p_data = p_data; - p_file_mmap_pool[file_mmap_index].size = size; + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); - p_file_mmap_pool[file_mmap_index].line_total = - split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES); + if (shm_unlink(shm_name) == -1 && errno != ENOENT) + { + log_error("shm_unlink(%s) error (%d)", shm_name, errno); + return -2; + } return 0; } -int unload_file_mmap(const char *filename) +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) { - int file_mmap_index = 0; + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_NAME_LEN]; + int fd; + void *p_shm = NULL; + struct stat sb; + size_t size; - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) + if (filename == NULL || p_data_len == NULL || p_line_total == NULL || pp_data == NULL || pp_line_offsets == NULL) { - log_error("trie_dict_get(%s) not found\n", filename); - return -1; + log_error("NULL pointer error"); + return NULL; } - if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); + + if ((fd = shm_open(shm_name, O_RDONLY, 0600)) == -1) { - log_error("munmap() error (%d)\n", errno); + log_error("shm_open(%s) error (%d)", shm_name, errno); + return NULL; } - p_file_mmap_pool[file_mmap_index].p_data = NULL; - p_file_mmap_pool[file_mmap_index].size = 0L; + if (fstat(fd, &sb) < 0) + { + log_error("fstat(fd) error (%d)", errno); + close(fd); + return NULL; + } - if (file_mmap_free_index == -1) + size = (size_t)sb.st_size; + + p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) { - file_mmap_free_index = file_mmap_index; + log_error("mmap() error (%d)", errno); + close(fd); + return NULL; } - if (trie_dict_del(p_trie_file_dict, filename) != 1) + if (close(fd) < 0) { - log_error("trie_dict_del(%s) error\n", filename); - return -2; + log_error("close(fd) error (%d)", errno); + return NULL; } - return 0; + if (((struct shm_header_t *)p_shm)->shm_size != size) + { + log_error("Shared memory size mismatch (%ld != %ld)", ((struct shm_header_t *)p_shm)->shm_size, size); + munmap(p_shm, size); + 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; } -const FILE_MMAP *get_file_mmap(const char *filename) +int detach_file_shm(void *p_shm) { - int file_mmap_index = file_mmap_free_index; + size_t size; - if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL) + if (p_shm == NULL) { - log_error("File loader not initialized\n"); - return NULL; + return -1; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&file_mmap_index) != 1) // Not exist + size = ((struct shm_header_t *)p_shm)->shm_size; + + if (munmap(p_shm, size) < 0) { - log_error("trie_dict_get(%s) not found\n", filename); - return NULL; + log_error("munmap() error (%d)", errno); + return -2; } - return (&(p_file_mmap_pool[file_mmap_index])); + return 0; }