| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
file_loader.c - description |
/* |
| 3 |
------------------- |
* file_loader |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - shared memory based file loader |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "file_loader.h" |
#include "file_loader.h" |
|
#include "trie_dict.h" |
|
|
#include "str_process.h" |
|
| 14 |
#include "log.h" |
#include "log.h" |
| 15 |
#include <fcntl.h> |
#include "str_process.h" |
| 16 |
#include <errno.h> |
#include <errno.h> |
| 17 |
#include <unistd.h> |
#include <fcntl.h> |
| 18 |
#include <stdlib.h> |
#include <stdlib.h> |
| 19 |
|
#include <string.h> |
| 20 |
|
#include <time.h> |
| 21 |
|
#include <unistd.h> |
| 22 |
#include <sys/mman.h> |
#include <sys/mman.h> |
| 23 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 24 |
|
|
| 25 |
#define FILE_MMAP_COUNT_LIMIT 256 |
struct shm_header_t |
| 26 |
|
{ |
| 27 |
static FILE_MMAP *p_file_mmap_pool = NULL; |
size_t shm_size; |
| 28 |
static int file_mmap_count = 0; |
size_t data_len; |
| 29 |
static int file_mmap_free_index = -1; |
long line_total; |
| 30 |
static TRIE_NODE *p_trie_file_dict = NULL; |
}; |
| 31 |
|
|
| 32 |
int file_loader_init(int max_file_mmap_count) |
int load_file(const char *filename) |
| 33 |
{ |
{ |
| 34 |
if (max_file_mmap_count > FILE_MMAP_COUNT_LIMIT) |
char filepath[FILE_PATH_LEN]; |
| 35 |
|
char shm_name[FILE_NAME_LEN]; |
| 36 |
|
int fd; |
| 37 |
|
struct stat sb; |
| 38 |
|
void *p_data; |
| 39 |
|
size_t data_len; |
| 40 |
|
size_t size; |
| 41 |
|
void *p_shm; |
| 42 |
|
long line_total; |
| 43 |
|
long line_offsets[MAX_SPLIT_FILE_LINES]; |
| 44 |
|
long *p_line_offsets; |
| 45 |
|
|
| 46 |
|
if (filename == NULL) |
| 47 |
{ |
{ |
| 48 |
log_error("file_loader_init(%d) argument error\n", max_file_mmap_count); |
log_error("NULL pointer error"); |
| 49 |
return -1; |
return -1; |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
if (p_file_mmap_pool != NULL || p_trie_file_dict != NULL) |
if ((fd = open(filename, O_RDONLY)) < 0) |
| 53 |
{ |
{ |
| 54 |
log_error("File loader already initialized\n"); |
log_error("open(%s) error (%d)", filename, errno); |
| 55 |
return -1; |
return -1; |
| 56 |
} |
} |
| 57 |
|
|
| 58 |
p_file_mmap_pool = (FILE_MMAP *)calloc((size_t)max_file_mmap_count, sizeof(FILE_MMAP)); |
if (fstat(fd, &sb) < 0) |
|
if (p_file_mmap_pool == NULL) |
|
| 59 |
{ |
{ |
| 60 |
log_error("calloc(%d p_file_mmap_pool) error\n", max_file_mmap_count); |
log_error("fstat(fd) error (%d)", errno); |
| 61 |
return -2; |
close(fd); |
| 62 |
|
return -1; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
file_mmap_count = max_file_mmap_count; |
data_len = (size_t)sb.st_size; |
| 66 |
file_mmap_free_index = 0; |
p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L); |
| 67 |
|
if (p_data == MAP_FAILED) |
|
p_trie_file_dict = trie_dict_create(); |
|
|
if (p_trie_file_dict == NULL) |
|
| 68 |
{ |
{ |
| 69 |
log_error("trie_dict_create() error\n"); |
log_error("mmap() error (%d)", errno); |
| 70 |
|
close(fd); |
| 71 |
return -2; |
return -2; |
| 72 |
} |
} |
| 73 |
|
|
| 74 |
return 0; |
if (close(fd) < 0) |
|
} |
|
|
|
|
|
void file_loader_cleanup(void) |
|
|
{ |
|
|
if (p_trie_file_dict != NULL) |
|
| 75 |
{ |
{ |
| 76 |
trie_dict_destroy(p_trie_file_dict); |
log_error("close(fd) error (%d)", errno); |
| 77 |
p_trie_file_dict = NULL; |
return -1; |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
if (p_file_mmap_pool != NULL) |
line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, 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; |
|
|
} |
|
| 81 |
|
|
| 82 |
file_mmap_count = 0; |
// Allocate shared memory |
| 83 |
file_mmap_free_index = -1; |
size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1); |
|
} |
|
| 84 |
|
|
| 85 |
int load_file_mmap(const char *filename) |
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 86 |
{ |
filepath[sizeof(filepath) - 1] = '\0'; |
| 87 |
int fd; |
snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); |
|
struct stat sb; |
|
|
void *p_data; |
|
|
size_t size; |
|
|
int file_mmap_index = 0; |
|
| 88 |
|
|
| 89 |
if ((fd = open(filename, O_RDONLY)) < 0) |
if (shm_unlink(shm_name) == -1 && errno != ENOENT) |
| 90 |
{ |
{ |
| 91 |
log_error("open(%s) error (%d)\n", filename, errno); |
log_error("shm_unlink(%s) error (%d)", shm_name, errno); |
| 92 |
return -1; |
return -2; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
if (fstat(fd, &sb) < 0) |
if ((fd = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) |
| 96 |
{ |
{ |
| 97 |
log_error("fstat(fd) error (%d)\n", errno); |
log_error("shm_open(%s) error (%d)", shm_name, errno); |
| 98 |
return -1; |
return -2; |
| 99 |
|
} |
| 100 |
|
if (ftruncate(fd, (off_t)size) == -1) |
| 101 |
|
{ |
| 102 |
|
log_error("ftruncate(size=%d) error (%d)", size, errno); |
| 103 |
|
close(fd); |
| 104 |
|
return -2; |
| 105 |
} |
} |
| 106 |
|
|
| 107 |
size = (size_t)sb.st_size; |
p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); |
| 108 |
|
if (p_shm == MAP_FAILED) |
|
p_data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0L); |
|
|
if (p_data == MAP_FAILED) |
|
| 109 |
{ |
{ |
| 110 |
log_error("mmap() error (%d)\n", errno); |
log_error("mmap() error (%d)", errno); |
| 111 |
|
close(fd); |
| 112 |
return -2; |
return -2; |
| 113 |
} |
} |
| 114 |
|
|
| 115 |
if (close(fd) < 0) |
if (close(fd) < 0) |
| 116 |
{ |
{ |
| 117 |
log_error("close(fd) error (%d)\n", errno); |
log_error("close(fd) error (%d)", errno); |
| 118 |
return -1; |
return -1; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
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; |
| 122 |
|
((struct shm_header_t *)p_shm)->data_len = data_len; |
| 123 |
|
((struct shm_header_t *)p_shm)->line_total = line_total; |
| 124 |
|
memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len); |
| 125 |
|
|
| 126 |
|
if (munmap(p_data, data_len) < 0) |
| 127 |
{ |
{ |
| 128 |
if (file_mmap_free_index == -1) |
log_error("munmap() error (%d)", errno); |
| 129 |
{ |
munmap(p_shm, size); |
| 130 |
log_error("file_mmap_pool is depleted\n"); |
return -2; |
| 131 |
return -3; |
} |
|
} |
|
| 132 |
|
|
| 133 |
file_mmap_index = file_mmap_free_index; |
p_data = (char *)p_shm + sizeof(struct shm_header_t); |
| 134 |
|
p_line_offsets = (long *)((char *)p_data + data_len + 1); |
| 135 |
|
memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1)); |
| 136 |
|
|
| 137 |
if (trie_dict_set(p_trie_file_dict, filename, (int64_t)file_mmap_index) != 1) |
if (munmap(p_shm, size) < 0) |
| 138 |
{ |
{ |
| 139 |
log_error("trie_dict_set(%s) error\n", filename); |
log_error("munmap() error (%d)", errno); |
| 140 |
|
return -2; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
if (munmap(p_data, size) < 0) |
return 0; |
| 144 |
{ |
} |
|
log_error("munmap() error (%d)\n", errno); |
|
|
} |
|
| 145 |
|
|
| 146 |
return -2; |
int unload_file(const char *filename) |
| 147 |
} |
{ |
| 148 |
|
char filepath[FILE_PATH_LEN]; |
| 149 |
|
char shm_name[FILE_NAME_LEN]; |
| 150 |
|
|
| 151 |
do |
if (filename == NULL) |
|
{ |
|
|
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 |
|
| 152 |
{ |
{ |
| 153 |
// Unload existing data |
log_error("NULL pointer error"); |
| 154 |
if (munmap(p_file_mmap_pool[file_mmap_index].p_data, p_file_mmap_pool[file_mmap_index].size) < 0) |
return -1; |
|
{ |
|
|
log_error("munmap() error (%d)\n", errno); |
|
|
} |
|
| 155 |
} |
} |
| 156 |
|
|
| 157 |
p_file_mmap_pool[file_mmap_index].p_data = p_data; |
strncpy(filepath, filename, sizeof(filepath) - 1); |
| 158 |
p_file_mmap_pool[file_mmap_index].size = size; |
filepath[sizeof(filepath) - 1] = '\0'; |
| 159 |
|
snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); |
| 160 |
|
|
| 161 |
p_file_mmap_pool[file_mmap_index].line_total = |
if (shm_unlink(shm_name) == -1 && errno != ENOENT) |
| 162 |
split_data_lines(p_data, SCREEN_COLS, p_file_mmap_pool[file_mmap_index].line_offsets, MAX_FILE_LINES); |
{ |
| 163 |
|
log_error("shm_unlink(%s) error (%d)", shm_name, errno); |
| 164 |
|
return -2; |
| 165 |
|
} |
| 166 |
|
|
| 167 |
return 0; |
return 0; |
| 168 |
} |
} |
| 169 |
|
|
| 170 |
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) |
| 171 |
{ |
{ |
| 172 |
int file_mmap_index = 0; |
char filepath[FILE_PATH_LEN]; |
| 173 |
|
char shm_name[FILE_NAME_LEN]; |
| 174 |
|
int fd; |
| 175 |
|
void *p_shm = NULL; |
| 176 |
|
struct stat sb; |
| 177 |
|
size_t size; |
| 178 |
|
|
| 179 |
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) |
| 180 |
{ |
{ |
| 181 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("NULL pointer error"); |
| 182 |
return -1; |
return NULL; |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
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); |
| 186 |
|
filepath[sizeof(filepath) - 1] = '\0'; |
| 187 |
|
snprintf(shm_name, sizeof(shm_name), "/FILE_SHM_%s", basename(filepath)); |
| 188 |
|
|
| 189 |
|
if ((fd = shm_open(shm_name, O_RDONLY, 0600)) == -1) |
| 190 |
{ |
{ |
| 191 |
log_error("munmap() error (%d)\n", errno); |
log_error("shm_open(%s) error (%d)", shm_name, errno); |
| 192 |
|
return NULL; |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
p_file_mmap_pool[file_mmap_index].p_data = NULL; |
if (fstat(fd, &sb) < 0) |
| 196 |
p_file_mmap_pool[file_mmap_index].size = 0L; |
{ |
| 197 |
|
log_error("fstat(fd) error (%d)", errno); |
| 198 |
|
close(fd); |
| 199 |
|
return NULL; |
| 200 |
|
} |
| 201 |
|
|
| 202 |
if (file_mmap_free_index == -1) |
size = (size_t)sb.st_size; |
| 203 |
|
|
| 204 |
|
p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L); |
| 205 |
|
if (p_shm == MAP_FAILED) |
| 206 |
{ |
{ |
| 207 |
file_mmap_free_index = file_mmap_index; |
log_error("mmap() error (%d)", errno); |
| 208 |
|
close(fd); |
| 209 |
|
return NULL; |
| 210 |
} |
} |
| 211 |
|
|
| 212 |
if (trie_dict_del(p_trie_file_dict, filename) != 1) |
if (close(fd) < 0) |
| 213 |
{ |
{ |
| 214 |
log_error("trie_dict_del(%s) error\n", filename); |
log_error("close(fd) error (%d)", errno); |
| 215 |
return -2; |
return NULL; |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
return 0; |
if (((struct shm_header_t *)p_shm)->shm_size != size) |
| 219 |
|
{ |
| 220 |
|
log_error("Shared memory size mismatch (%ld != %ld)", ((struct shm_header_t *)p_shm)->shm_size, size); |
| 221 |
|
munmap(p_shm, size); |
| 222 |
|
return NULL; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
*p_data_len = ((struct shm_header_t *)p_shm)->data_len; |
| 226 |
|
*p_line_total = ((struct shm_header_t *)p_shm)->line_total; |
| 227 |
|
*pp_data = (char *)p_shm + sizeof(struct shm_header_t); |
| 228 |
|
*pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1); |
| 229 |
|
|
| 230 |
|
return p_shm; |
| 231 |
} |
} |
| 232 |
|
|
| 233 |
const FILE_MMAP *get_file_mmap(const char *filename) |
int detach_file_shm(void *p_shm) |
| 234 |
{ |
{ |
| 235 |
int file_mmap_index = file_mmap_free_index; |
size_t size; |
| 236 |
|
|
| 237 |
if (p_file_mmap_pool == NULL || p_trie_file_dict == NULL) |
if (p_shm == NULL) |
| 238 |
{ |
{ |
| 239 |
log_error("File loader not initialized\n"); |
return -1; |
|
return NULL; |
|
| 240 |
} |
} |
| 241 |
|
|
| 242 |
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; |
| 243 |
|
|
| 244 |
|
if (munmap(p_shm, size) < 0) |
| 245 |
{ |
{ |
| 246 |
log_error("trie_dict_get(%s) not found\n", filename); |
log_error("munmap() error (%d)", errno); |
| 247 |
return NULL; |
return -2; |
| 248 |
} |
} |
| 249 |
|
|
| 250 |
return (&(p_file_mmap_pool[file_mmap_index])); |
return 0; |
| 251 |
} |
} |