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

Diff of /lbbs/src/file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Sat May 17 15:39:51 2025 UTC Revision 1.9 by sysadm, Tue May 20 12:29:26 2025 UTC
# Line 31  Line 31 
31    
32  #define MAX_SPLIT_FILE_LINES 65536  #define MAX_SPLIT_FILE_LINES 65536
33    
34    struct shm_header_t
35    {
36            int shmid;
37            size_t data_len;
38            long line_total;
39    };
40    
41  static TRIE_NODE *p_trie_file_dict = NULL;  static TRIE_NODE *p_trie_file_dict = NULL;
42    
43  int file_loader_init()  int file_loader_init()
# Line 51  int file_loader_init() Line 58  int file_loader_init()
58          return 0;          return 0;
59  }  }
60    
61  static void trie_file_dict_cleanup_cb(const char *filename, int64_t shmid)  static void trie_file_dict_cleanup_cb(const char *filename, int64_t value)
62  {  {
63          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          const void *p_shm = (const void *)value;
64            int shmid = *((int *)p_shm);
65    
66            if (shmdt(p_shm) == -1)
67          {          {
68                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
69            }
70    
71            if (shmctl(shmid, IPC_RMID, NULL) == -1)
72            {
73                    log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
74          }          }
75  }  }
76    
# Line 67  void file_loader_cleanup(void) Line 82  void file_loader_cleanup(void)
82          }          }
83    
84          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);          trie_dict_traverse(p_trie_file_dict, trie_file_dict_cleanup_cb);
85    
86          trie_dict_destroy(p_trie_file_dict);          trie_dict_destroy(p_trie_file_dict);
87            p_trie_file_dict = NULL;
88  }  }
89    
90  int load_file_shm(const char *filename)  int load_file_shm(const char *filename)
# Line 80  int load_file_shm(const char *filename) Line 97  int load_file_shm(const char *filename)
97          key_t key;          key_t key;
98          size_t size;          size_t size;
99          int shmid;          int shmid;
         int64_t shmid_old;  
100          void *p_shm;          void *p_shm;
101          long line_total;          long line_total;
102          long line_offsets[MAX_SPLIT_FILE_LINES];          long line_offsets[MAX_SPLIT_FILE_LINES];
103          long *p_line_offsets;          long *p_line_offsets;
104            int64_t shmid_old;
105            void *p_shm_old;
106    
107          if ((fd = open(filename, O_RDONLY)) < 0)          if ((fd = open(filename, O_RDONLY)) < 0)
108          {          {
# Line 127  int load_file_shm(const char *filename) Line 145  int load_file_shm(const char *filename)
145                  return -2;                  return -2;
146          }          }
147    
148          size = sizeof(data_len) + sizeof(line_total) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);          size = sizeof(struct shm_header_t) + data_len + 1 + sizeof(long) * (size_t)(line_total + 1);
149          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
150          if (shmid == -1)          if (shmid == -1)
151          {          {
# Line 137  int load_file_shm(const char *filename) Line 155  int load_file_shm(const char *filename)
155          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
156          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
157          {          {
158                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
159                  return -3;                  return -3;
160          }          }
161    
162          *((size_t *)p_shm) = data_len;          ((struct shm_header_t *)p_shm)->shmid = shmid;
163          *((long *)(p_shm + sizeof(data_len))) = line_total;          ((struct shm_header_t *)p_shm)->data_len = data_len;
164          memcpy(p_shm + sizeof(data_len) + sizeof(line_total), p_data, data_len);          ((struct shm_header_t *)p_shm)->line_total = line_total;
165            memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);
166    
167          if (munmap(p_data, data_len) < 0)          if (munmap(p_data, data_len) < 0)
168          {          {
# Line 151  int load_file_shm(const char *filename) Line 170  int load_file_shm(const char *filename)
170                  return -2;                  return -2;
171          }          }
172    
173          p_data = p_shm + sizeof(data_len) + sizeof(line_total);          p_data = p_shm + sizeof(struct shm_header_t);
174          p_line_offsets = p_data + data_len + 1;          p_line_offsets = p_data + data_len + 1;
175          memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));          memcpy(p_line_offsets, line_offsets, sizeof(long) * (size_t)(line_total + 1));
176    
177            // Re-mount shm in read-only mode
178          if (shmdt(p_shm) == -1)          if (shmdt(p_shm) == -1)
179          {          {
180                  log_error("shmdt() error (%d)\n", errno);                  log_error("shmdt() error (%d)\n", errno);
181                  return -3;                  return -3;
182          }          }
183    
184          if (trie_dict_get(p_trie_file_dict, filename, &shmid_old) == 1)          p_shm = shmat(shmid, NULL, SHM_RDONLY);
185            if (p_shm == (void *)-1)
186          {          {
187                    log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);
188                    return -3;
189            }
190    
191            if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm_old) == 1)
192            {
193                    shmid_old = *((int *)p_shm_old);
194    
195                    if (shmdt(p_shm_old) == -1)
196                    {
197                            log_error("shmdt(shmid=%d) error (%d)\n", shmid_old, errno);
198                            return -3;
199                    }
200    
201                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)                  if (shmctl((int)shmid_old, IPC_RMID, NULL) == -1)
202                  {                  {
203                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid_old, errno);
# Line 170  int load_file_shm(const char *filename) Line 205  int load_file_shm(const char *filename)
205                  }                  }
206          }          }
207    
208          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)shmid) != 1)          if (trie_dict_set(p_trie_file_dict, filename, (int64_t)p_shm) != 1)
209          {          {
210                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("trie_dict_set(%s) error\n", filename);
211    
# Line 187  int load_file_shm(const char *filename) Line 222  int load_file_shm(const char *filename)
222    
223  int unload_file_shm(const char *filename)  int unload_file_shm(const char *filename)
224  {  {
225          int64_t shmid = 0;          const void *p_shm;
226            int shmid;
227    
228          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&shmid) != 1)          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1)
229          {          {
230                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
231                  return -1;                  return -1;
232          }          }
233    
234            shmid = *((int *)p_shm);
235    
236            if (shmdt(p_shm) == -1)
237            {
238                    log_error("shmdt(shmid=%d) error (%d)\n", shmid, errno);
239            }
240    
241          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)          if (shmctl((int)shmid, IPC_RMID, NULL) == -1)
242          {          {
243                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", (int)shmid, errno);
# Line 209  int unload_file_shm(const char *filename Line 252  int unload_file_shm(const char *filename
252          return 0;          return 0;
253  }  }
254    
255  const void *get_file_shm(const char *filename)  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)
256  {  {
         int64_t shmid;  
257          const void *p_shm;          const void *p_shm;
258    
259          if (p_trie_file_dict == NULL)          if (p_trie_file_dict == NULL)
# Line 220  const void *get_file_shm(const char *fil Line 262  const void *get_file_shm(const char *fil
262                  return NULL;                  return NULL;
263          }          }
264    
265          if (trie_dict_get(p_trie_file_dict, filename, &shmid) != 1) // Not exist          if (trie_dict_get(p_trie_file_dict, filename, (int64_t *)&p_shm) != 1) // Not exist
266          {          {
267                  log_error("trie_dict_get(%s) not found\n", filename);                  log_error("trie_dict_get(%s) not found\n", filename);
268                  return NULL;                  return NULL;
269          }          }
270    
271          p_shm = shmat((int)shmid, NULL, SHM_RDONLY);          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
272          if (p_shm == (void *)-1)          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
273          {          *pp_data = p_shm + sizeof(struct shm_header_t);
274                  log_error("shmat() error (%d)\n", errno);          *pp_line_offsets = *pp_data + *p_data_len + 1;
                 return NULL;  
         }  
275    
276          return p_shm;          return p_shm;
277  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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