| 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" |
| 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); |
| 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 |
|
|
| 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 |
} |
} |
| 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 |
} |
} |
| 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 |
|
|
| 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); |
| 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 |
} |
} |
| 225 |
// Make another copy of the data file so that the user editored copy will be kept after upgrade / reinstall |
// 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) |
if ((fd = open(data_file_list[i].path, O_WRONLY | O_CREAT | O_TRUNC, 0640)) < 0) |
| 227 |
{ |
{ |
| 228 |
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); |
| 229 |
goto cleanup; |
goto cleanup; |
| 230 |
} |
} |
| 231 |
|
|
| 232 |
if (write(fd, p_data_new, (size_t)len_data_new) < 0) |
if (write(fd, p_data_new, (size_t)len_data_new) < 0) |
| 233 |
{ |
{ |
| 234 |
log_error("write(len=%ld) error (%d)\n", len_data_new, errno); |
log_error("write(len=%ld) error (%d)", len_data_new, errno); |
| 235 |
goto cleanup; |
goto cleanup; |
| 236 |
} |
} |
| 237 |
|
|
| 238 |
if (close(fd) < 0) |
if (close(fd) < 0) |
| 239 |
{ |
{ |
| 240 |
log_error("close(fd) error (%d)\n", errno); |
log_error("close(fd) error (%d)", errno); |
| 241 |
fd = -1; |
fd = -1; |
| 242 |
goto cleanup; |
goto cleanup; |
| 243 |
} |
} |
| 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; |