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