/[LeafOK_CVS]/lbbs/src/file_loader.c
ViewVC logotype

Contents of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Thu May 29 09:44:01 2025 UTC (9 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.11: +0 -2 lines
Content type: text/x-csrc
Add article_cache

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 <string.h>
26 #include <time.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <sys/shm.h>
30 #include <sys/ipc.h>
31
32 struct shm_header_t
33 {
34 int shmid;
35 size_t data_len;
36 long line_total;
37 };
38
39 static TRIE_NODE *p_trie_file_dict = NULL;
40
41 int file_loader_init()
42 {
43 if (p_trie_file_dict != NULL)
44 {
45 log_error("File loader already initialized\n");
46 return -1;
47 }
48
49 p_trie_file_dict = trie_dict_create();
50 if (p_trie_file_dict == NULL)
51 {
52 log_error("trie_dict_create() error\n");
53 return -2;
54 }
55
56 return 0;
57 }
58
59 static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
60 {
61 int shmid = (int)value;
62
63 if (shmctl(shmid, IPC_RMID, NULL) == -1)
64 {
65 log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
66 }
67 }
68
69 void file_loader_cleanup(void)
70 {
71 if (p_trie_file_dict == NULL)
72 {
73 return;
74 }
75
76 trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
77 trie_dict_destroy(p_trie_file_dict);
78
79 p_trie_file_dict = NULL;
80 }
81
82 int load_file(const char *filename)
83 {
84 int fd;
85 struct stat sb;
86 void *p_data;
87 size_t data_len;
88 int proj_id;
89 key_t key;
90 size_t size;
91 int shmid;
92 void *p_shm;
93 long line_total;
94 long line_offsets[MAX_SPLIT_FILE_LINES];
95 long *p_line_offsets;
96 int64_t shmid_old;
97
98 if ((fd = open(filename, O_RDONLY)) < 0)
99 {
100 log_error("open(%s) error (%d)\n", filename, errno);
101 return -1;
102 }
103
104 if (fstat(fd, &sb) < 0)
105 {
106 log_error("fstat(fd) error (%d)\n", errno);
107 return -1;
108 }
109
110 data_len = (size_t)sb.st_size;
111 p_data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, 0L);
112 if (p_data == MAP_FAILED)
113 {
114 log_error("mmap() error (%d)\n", errno);
115 return -2;
116 }
117
118 if (close(fd) < 0)
119 {
120 log_error("close(fd) error (%d)\n", errno);
121 return -1;
122 }
123
124 line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES);
125 if (line_total >= MAX_SPLIT_FILE_LINES)
126 {
127 log_error("split_data_lines() truncated over limit lines\n");
128 }
129
130 // Allocate shared memory
131 proj_id = (int)(time(NULL) % getpid());
132 key = ftok(filename, proj_id);
133 if (key == -1)
134 {
135 log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
136 return -2;
137 }
138
139 size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
140 shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
141 if (shmid == -1)
142 {
143 log_error("shmget(size = %d) error (%d)\n", size, errno);
144 return -3;
145 }
146 p_shm = shmat(shmid, NULL, 0);
147 if (p_shm == (void *)-1)
148 {
149 log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
150 return -3;
151 }
152
153 ((struct shm_header_t *)p_shm)->shmid = shmid;
154 ((struct shm_header_t *)p_shm)->data_len = data_len;
155 ((struct shm_header_t *)p_shm)->line_total = line_total;
156 memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);
157
158 if (munmap(p_data, data_len) < 0)
159 {
160 log_error("munmap() error (%d)\n", errno);
161 return -2;
162 }
163
164 p_data = p_shm + sizeof(struct shm_header_t);
165 p_line_offsets = p_data + data_len + 1;
166 memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
167
168 if (shmdt(p_shm) == -1)
169 {
170 log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
171 return -3;
172 }
173
174 if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)
175 {
176 if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
177 {
178 log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
179 return -2;
180 }
181 }
182
183 if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)
184 {
185 log_error("trie_dict_set(%s) error\n", filename);
186
187 if (shmctl(shmid, IPC_RMID, NULL) == -1)
188 {
189 log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
190 }
191
192 return -4;
193 }
194
195 return 0;
196 }
197
198 int unload_file(const char *filename)
199 {
200 int64_t shmid;
201
202 if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1)
203 {
204 log_error("trie_dict_get(%s) not found\n", filename);
205 return -1;
206 }
207
208 if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
209 {
210 log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
211 }
212
213 if (trie_dict_del(p_trie_file_dict, filename) != 1)
214 {
215 log_error("trie_dict_del(%s) error\n", filename);
216 return -2;
217 }
218
219 return 0;
220 }
221
222 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)
223 {
224 int64_t shmid;
225 const void *p_shm;
226
227 if (p_trie_file_dict == NULL)
228 {
229 log_error("File loader not initialized\n");
230 return NULL;
231 }
232
233 if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist
234 {
235 log_error("trie_dict_get(%s) not found\n", filename);
236 return NULL;
237 }
238
239 p_shm = shmat((int)shmid, NULL, SHM_RDONLY);
240 if (p_shm == (void *)-1)
241 {
242 log_error("shmat(shmid=%d) error (%d)\n", (int)shmid, errno);
243 return NULL;
244 }
245
246 *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
247 *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
248 *pp_data = p_shm + sizeof(struct shm_header_t);
249 *pp_line_offsets = *pp_data + *p_data_len + 1;
250
251 return p_shm;
252 }
253
254 int detach_file_shm(const void *p_shm)
255 {
256 if (p_shm == NULL)
257 {
258 return -2;
259 }
260
261 if (shmdt(p_shm) == -1)
262 {
263 log_error("shmdt() error (%d)\n", errno);
264 return -1;
265 }
266
267 return 0;
268 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1