--- lbbs/src/file_loader.c 2025/05/20 12:29:26 1.9 +++ lbbs/src/file_loader.c 2025/11/19 13:35:38 1.24 @@ -1,108 +1,53 @@ -/*************************************************************************** - 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 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 -#include -#include - -#define MAX_SPLIT_FILE_LINES 65536 struct shm_header_t { - int shmid; + size_t shm_size; size_t data_len; long line_total; }; -static TRIE_NODE *p_trie_file_dict = NULL; - -int file_loader_init() -{ - if (p_trie_file_dict != NULL) - { - log_error("File loader already initialized\n"); - return -1; - } - - p_trie_file_dict = trie_dict_create(); - if (p_trie_file_dict == NULL) - { - log_error("trie_dict_create() error\n"); - return -2; - } - - return 0; -} - -static void trie_file_dict_cleanup_cb(const char *filename, int64_t value) -{ - const void *p_shm = (const void *)value; - int shmid = *((int *)p_shm); - - if (shmdt(p_shm) == -1) - { - log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); - } - - if (shmctl(shmid, IPC_RMID, NULL) == -1) - { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); - } -} - -void file_loader_cleanup(void) -{ - if (p_trie_file_dict == NULL) - { - return; - } - - 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) { + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_PATH_LEN]; int fd; struct stat sb; void *p_data; size_t data_len; - int proj_id; - key_t key; size_t size; - int shmid; void *p_shm; long line_total; long line_offsets[MAX_SPLIT_FILE_LINES]; long *p_line_offsets; - int64_t shmid_old; - void *p_shm_old; + + if (filename == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } if ((fd = open(filename, O_RDONLY)) < 0) { @@ -113,6 +58,7 @@ int load_file_shm(const char *filename) if (fstat(fd, &sb) < 0) { log_error("fstat(fd) error (%d)\n", errno); + close(fd); return -1; } @@ -121,6 +67,7 @@ int load_file_shm(const char *filename) if (p_data == MAP_FAILED) { log_error("mmap() error (%d)\n", errno); + close(fd); return -2; } @@ -130,148 +77,175 @@ 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) + line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL); + + // Allocate shared memory + size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); + + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); + + if (shm_unlink(shm_name) == -1 && errno != ENOENT) { - log_error("split_data_lines() truncated over limit lines\n"); + log_error("shm_unlink(%s) error (%d)\n", shm_name, errno); + return -2; } - // Allocate shared memory - proj_id = (int)(time(NULL) % getpid()); - key = ftok(filename, proj_id); - if (key == -1) + if ((fd = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) { - log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno); + log_error("shm_open(%s) error (%d)\n", shm_name, errno); + return -2; + } + if (ftruncate(fd, (off_t)size) == -1) + { + log_error("ftruncate(size=%d) error (%d)\n", size, errno); + close(fd); return -2; } - 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) + p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) { - log_error("shmget(size = %d) error (%d)\n", size, errno); - return -3; + log_error("mmap() error (%d)\n", errno); + close(fd); + return -2; } - p_shm = shmat(shmid, NULL, 0); - if (p_shm == (void *)-1) + + if (close(fd) < 0) { - log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); - return -3; + log_error("close(fd) error (%d)\n", errno); + return -1; } - ((struct shm_header_t *)p_shm)->shmid = shmid; + ((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(p_shm + sizeof(struct shm_header_t), p_data, data_len); + memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len); if (munmap(p_data, data_len) < 0) { log_error("munmap() error (%d)\n", errno); + munmap(p_shm, size); return -2; } - p_data = p_shm + sizeof(struct shm_header_t); - 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)); - // Re-mount shm in read-only mode - if (shmdt(p_shm) == -1) + if (munmap(p_shm, size) < 0) { - log_error("shmdt() error (%d)\n", errno); - return -3; - } - - p_shm = shmat(shmid, NULL, SHM_RDONLY); - if (p_shm == (void *)-1) - { - log_error("shmat(shmid=%d) error (%d)\n", shmid, errno); - return -3; + log_error("munmap() error (%d)\n", errno); + return -2; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1) - { - shmid_old = *((int *)p_shm_old); - - if (shmdt(p_shm_old) == -1) - { - log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno); - return -3; - } + return 0; +} - if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1) - { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno); - return -2; - } - } +int unload_file(const char *filename) +{ + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_PATH_LEN]; - if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1) + if (filename == NULL) { - log_error("trie_dict_set(%s) error\n", filename); + log_error("NULL pointer error\n"); + return -1; + } - if (shmctl(shmid, IPC_RMID, NULL) == -1) - { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); - } + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); - return -4; + if (shm_unlink(shm_name) == -1 && errno != ENOENT) + { + log_error("shm_unlink(%s) error (%d)\n", shm_name, errno); + return -2; } return 0; } -int unload_file_shm(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) { - const void *p_shm; - int shmid; + char filepath[FILE_PATH_LEN]; + char shm_name[FILE_PATH_LEN]; + int fd; + void *p_shm = NULL; + struct stat sb; + size_t size; - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 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\n"); + return NULL; } - shmid = *((int *)p_shm); + strncpy(filepath, filename, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); - if (shmdt(p_shm) == -1) + if ((fd = shm_open(shm_name, O_RDONLY, 0600)) == -1) { - log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno); + log_error("shm_open(%s) error (%d)\n", shm_name, errno); + return NULL; } - if (shmctl((int)shmid, IPC_RMID, NULL) == -1) + if (fstat(fd, &sb) < 0) { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno); + log_error("fstat(fd) error (%d)\n", errno); + close(fd); + return NULL; } - if (trie_dict_del(p_trie_file_dict, filename) != 1) + size = (size_t)sb.st_size; + + p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) { - log_error("trie_dict_del(%s) error\n", filename); - return -2; + log_error("mmap() error (%d)\n", errno); + close(fd); + return NULL; } - return 0; -} - -const void *get_file_shm(const char *filename, size_t *p_data_len, long *p_line_total, const void **pp_data, const long **pp_line_offsets) -{ - const void *p_shm; - - if (p_trie_file_dict == NULL) + if (close(fd) < 0) { - log_error("File loader not initialized\n"); + log_error("close(fd) error (%d)\n", errno); return NULL; } - if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) // Not exist + if (((struct shm_header_t *)p_shm)->shm_size != size) { - log_error("trie_dict_get(%s) not found\n", filename); + log_error("Shared memory size mismatch (%ld != %ld)\n", ((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 = p_shm + sizeof(struct shm_header_t); - *pp_line_offsets = *pp_data + *p_data_len + 1; + *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(void *p_shm) +{ + size_t size; + + if (p_shm == NULL) + { + return -1; + } + + size = ((struct shm_header_t *)p_shm)->shm_size; + + if (munmap(p_shm, size) < 0) + { + log_error("munmap() error (%d)\n", errno); + return -2; + } + + return 0; +}