/[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.5 - (show annotations)
Sat May 17 15:39:51 2025 UTC (10 months ago) by sysadm
Branch: MAIN
Changes since 1.4: +0 -1 lines
Content type: text/x-csrc
Refact bbs_cmd with trie_dict

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

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