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

Diff of /lbbs/src/data_file_mgr.c

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

Revision 1.1 by sysadm, Fri Nov 7 13:56:11 2025 UTC Revision 1.6 by sysadm, Sat Jan 3 10:27:14 2026 UTC
# Line 3  Line 3 
3   * data_file_mgr   * data_file_mgr
4   *   - Edit / Preview data files   *   - Edit / Preview data files
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9    #ifdef HAVE_CONFIG_H
10    #include "config.h"
11    #endif
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_cmd.h"  #include "bbs_cmd.h"
15  #include "common.h"  #include "common.h"
# Line 42  static const DATA_FILE_ITEM data_file_li Line 46  static const DATA_FILE_ITEM data_file_li
46          {"活动看板", DATA_ACTIVE_BOARD},          {"活动看板", DATA_ACTIVE_BOARD},
47          {"浏览帮助", DATA_READ_HELP},          {"浏览帮助", DATA_READ_HELP},
48          {"编辑帮助", DATA_EDITOR_HELP},          {"编辑帮助", DATA_EDITOR_HELP},
49            {"敏感词表", CONF_BWF},
50            {"穿梭配置", CONF_BBSNET},
51  };  };
52    
53  static const int data_file_cnt = sizeof(data_file_list) / sizeof(DATA_FILE_ITEM);  static const int data_file_cnt = sizeof(data_file_list) / sizeof(DATA_FILE_ITEM);
# Line 84  int data_file_mgr(void *param) Line 90  int data_file_mgr(void *param)
90          if ((fd = open(path, O_RDONLY)) < 0 &&          if ((fd = open(path, O_RDONLY)) < 0 &&
91                  (fd = open(data_file_list[i].path, O_RDONLY)) < 0)                  (fd = open(data_file_list[i].path, O_RDONLY)) < 0)
92          {          {
93                  log_error("open(%s) error (%d)\n", data_file_list[i].path, errno);                  log_error("open(%s) error (%d)", data_file_list[i].path, errno);
94                  return REDRAW;                  return REDRAW;
95          }          }
96    
97          if (fstat(fd, &sb) < 0)          if (fstat(fd, &sb) < 0)
98          {          {
99                  log_error("fstat(fd) error (%d)\n", errno);                  log_error("fstat(fd) error (%d)", errno);
100                  goto cleanup;                  goto cleanup;
101          }          }
102    
# Line 98  int data_file_mgr(void *param) Line 104  int data_file_mgr(void *param)
104          p_data = mmap(NULL, len_data, PROT_READ, MAP_SHARED, fd, 0L);          p_data = mmap(NULL, len_data, PROT_READ, MAP_SHARED, fd, 0L);
105          if (p_data == MAP_FAILED)          if (p_data == MAP_FAILED)
106          {          {
107                  log_error("mmap() error (%d)\n", errno);                  log_error("mmap() error (%d)", errno);
108                  goto cleanup;                  goto cleanup;
109          }          }
110    
111          if (close(fd) < 0)          if (close(fd) < 0)
112          {          {
113                  log_error("close(fd) error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
114                  fd = -1;                  fd = -1;
115                  goto cleanup;                  goto cleanup;
116          }          }
# Line 113  int data_file_mgr(void *param) Line 119  int data_file_mgr(void *param)
119          p_editor_data = editor_data_load(p_data);          p_editor_data = editor_data_load(p_data);
120          if (p_editor_data == NULL)          if (p_editor_data == NULL)
121          {          {
122                  log_error("editor_data_load(data) error\n");                  log_error("editor_data_load(data) error");
123                  goto cleanup;                  goto cleanup;
124          }          }
125    
126          if (munmap(p_data, len_data) < 0)          if (munmap(p_data, len_data) < 0)
127          {          {
128                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
129                  p_data = NULL;                  p_data = NULL;
130                  goto cleanup;                  goto cleanup;
131          }          }
# Line 128  int data_file_mgr(void *param) Line 134  int data_file_mgr(void *param)
134          p_data_new = malloc(DATA_FILE_MAX_LEN);          p_data_new = malloc(DATA_FILE_MAX_LEN);
135          if (p_data_new == NULL)          if (p_data_new == NULL)
136          {          {
137                  log_error("malloc(content) error: OOM\n");                  log_error("malloc(content) error: OOM");
138                  goto cleanup;                  goto cleanup;
139          }          }
140    
# Line 163  int data_file_mgr(void *param) Line 169  int data_file_mgr(void *param)
169                          len_data_new = editor_data_save(p_editor_data, p_data_new, DATA_FILE_MAX_LEN);                          len_data_new = editor_data_save(p_editor_data, p_data_new, DATA_FILE_MAX_LEN);
170                          if (len_data_new < 0)                          if (len_data_new < 0)
171                          {                          {
172                                  log_error("editor_data_save() error\n");                                  log_error("editor_data_save() error");
173                                  goto cleanup;                                  goto cleanup;
174                          }                          }
175                          line_total = split_data_lines(p_data_new, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL);                          line_total = split_data_lines(p_data_new, SCREEN_COLS, line_offsets, MAX_SPLIT_FILE_LINES, 1, NULL);
# Line 192  int data_file_mgr(void *param) Line 198  int data_file_mgr(void *param)
198                  break;                  break;
199          }          }
200    
201            if (SYS_server_exit) // Do not save data on shutdown
202            {
203                    goto cleanup;
204            }
205    
206          if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640)) < 0)          if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640)) < 0)
207          {          {
208                  log_error("open(%s) error (%d)\n", path, errno);                  log_error("open(%s) error (%d)", path, errno);
209                  goto cleanup;                  goto cleanup;
210          }          }
211    
212          if (write(fd, p_data_new, (size_t)len_data_new) < 0)          if (write(fd, p_data_new, (size_t)len_data_new) < 0)
213          {          {
214                  log_error("write(len=%ld) error (%d)\n", len_data_new, errno);                  log_error("write(len=%ld) error (%d)", len_data_new, errno);
215                  goto cleanup;                  goto cleanup;
216          }          }
217    
218          if (close(fd) < 0)          if (close(fd) < 0)
219          {          {
220                  log_error("close(fd) error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
221                  fd = -1;                  fd = -1;
222                  goto cleanup;                  goto cleanup;
223          }          }
         fd = -1;  
224    
225          if (unlink(data_file_list[i].path) < 0)          // Make another copy of the data file so that the user editored copy will be kept after upgrade / reinstall
226            if ((fd = open(data_file_list[i].path, O_WRONLY | O_CREAT | O_TRUNC, 0640)) < 0)
227            {
228                    log_error("open(%s) error (%d)", data_file_list[i].path, errno);
229                    goto cleanup;
230            }
231    
232            if (write(fd, p_data_new, (size_t)len_data_new) < 0)
233          {          {
234                  log_error("unlink(%s) error (%d)\n", data_file_list[i].path, errno);                  log_error("write(len=%ld) error (%d)", len_data_new, errno);
235                  goto cleanup;                  goto cleanup;
236          }          }
237    
238          if (link(path, data_file_list[i].path) < 0)          if (close(fd) < 0)
239          {          {
240                  log_error("link(%s, %s) error (%d)\n", path, data_file_list[i].path, errno);                  log_error("close(fd) error (%d)", errno);
241                    fd = -1;
242                  goto cleanup;                  goto cleanup;
243          }          }
244            fd = -1;
245    
246          clearscr();          clearscr();
247          moveto(1, 1);          moveto(1, 1);
# Line 242  cleanup: Line 261  cleanup:
261    
262          if (p_data != NULL && munmap(p_data, len_data) < 0)          if (p_data != NULL && munmap(p_data, len_data) < 0)
263          {          {
264                  log_error("munmap() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
265          }          }
266    
267          if (fd >= 0 && close(fd) < 0)          if (fd >= 0 && close(fd) < 0)
268          {          {
269                  log_error("close(fd) error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
270          }          }
271    
272          return REDRAW;          return REDRAW;


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

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