/[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.13 by sysadm, Sun Jun 1 14:04:19 2025 UTC Revision 1.21 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
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-2025  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 "trie_dict.h"
17  #include <errno.h>  #include <errno.h>
18  #include <unistd.h>  #include <fcntl.h>
19  #include <stdlib.h>  #include <stdlib.h>
20  #include <string.h>  #include <string.h>
21  #include <time.h>  #include <time.h>
22    #include <unistd.h>
23    #include <sys/ipc.h>
24  #include <sys/mman.h>  #include <sys/mman.h>
 #include <sys/stat.h>  
25  #include <sys/shm.h>  #include <sys/shm.h>
26  #include <sys/ipc.h>  #include <sys/stat.h>
27    
28  struct shm_header_t  struct shm_header_t
29  {  {
# Line 60  static void trie_file_dict_cleanup_cb(co Line 56  static void trie_file_dict_cleanup_cb(co
56  {  {
57          int shmid = (int)value;          int shmid = (int)value;
58    
59          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
60          {          {
61                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
62          }          }
# Line 121  int load_file(const char *filename) Line 117  int load_file(const char *filename)
117                  return -1;                  return -1;
118          }          }
119    
120          line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES);          line_total = split_data_lines(p_data, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL);
121    
122          // Allocate shared memory          // Allocate shared memory
123          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 149  int load_file(const char *filename) Line 145  int load_file(const char *filename)
145          ((struct shm_header_t *)p_shm)->shmid = shmid;          ((struct shm_header_t *)p_shm)->shmid = shmid;
146          ((struct shm_header_t *)p_shm)->data_len = data_len;          ((struct shm_header_t *)p_shm)->data_len = data_len;
147          ((struct shm_header_t *)p_shm)->line_total = line_total;          ((struct shm_header_t *)p_shm)->line_total = line_total;
148          memcpy(p_shm + sizeof(struct shm_header_t), p_data, data_len);          memcpy((char *)p_shm + sizeof(struct shm_header_t), p_data, data_len);
149    
150          if (munmap(p_data, data_len) < 0)          if (munmap(p_data, data_len) < 0)
151          {          {
# Line 157  int load_file(const char *filename) Line 153  int load_file(const char *filename)
153                  return -2;                  return -2;
154          }          }
155    
156          p_data = p_shm + sizeof(struct shm_header_t);          p_data = (char *)p_shm + sizeof(struct shm_header_t);
157          p_line_offsets = p_data + data_len + 1;          p_line_offsets = (long *)((char *)p_data + data_len + 1);
158          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));
159    
160          if (shmdt(p_shm) == -1)          if (shmdt(p_shm) == -1)
# Line 180  int load_file(const char *filename) Line 176  int load_file(const char *filename)
176          {          {
177                  log_error("trie_dict_set(%s) error\n", filename);                  log_error("trie_dict_set(%s) error\n", filename);
178    
179                  if (shmctl(shmid, IPC_RMID, NULL) == -1)                  if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
180                  {                  {
181                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                          log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
182                  }                  }
# Line 241  const void *get_file_shm_readonly(const Line 237  const void *get_file_shm_readonly(const
237    
238          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;          *p_data_len = ((struct shm_header_t *)p_shm)->data_len;
239          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;          *p_line_total = ((struct shm_header_t *)p_shm)->line_total;
240          *pp_data = p_shm + sizeof(struct shm_header_t);          *pp_data = (char *)p_shm + sizeof(struct shm_header_t);
241          *pp_line_offsets = *pp_data + *p_data_len + 1;          *pp_line_offsets = (const long *)((const char *)(*pp_data) + *p_data_len + 1);
242    
243          return p_shm;          return p_shm;
244  }  }


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

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