| 42 |
return 0; |
return 0; |
| 43 |
} |
} |
| 44 |
|
|
| 45 |
int article_cache_generate(const char *cache_dir, const ARTICLE *p_article, const char *content, int overwrite) |
int article_cache_generate(const char *cache_dir, const ARTICLE *p_article, const SECTION_LIST *p_section, const char *content, int overwrite) |
| 46 |
{ |
{ |
| 47 |
char data_file[FILE_PATH_LEN]; |
char data_file[FILE_PATH_LEN]; |
| 48 |
int fd; |
int fd; |
| 49 |
ARTICLE_CACHE cache; |
ARTICLE_CACHE cache; |
| 50 |
size_t buf_size; |
struct tm tm_sub_dt; |
| 51 |
|
char str_sub_dt[50]; |
| 52 |
|
char header[ARTICLE_HEADER_MAX_LEN]; |
| 53 |
|
size_t header_len; |
| 54 |
|
long header_line_cnt; |
| 55 |
|
long i; |
| 56 |
|
|
| 57 |
if (cache_dir == NULL || p_article == NULL || content == NULL) |
if (cache_dir == NULL || p_article == NULL || content == NULL) |
| 58 |
{ |
{ |
| 75 |
} |
} |
| 76 |
} |
} |
| 77 |
|
|
|
buf_size = ARTICLE_HEADER_MAX_LEN + strlen(content) + 1; |
|
|
cache.p_data = malloc(buf_size); |
|
|
if (cache.p_data == NULL) |
|
|
{ |
|
|
log_error("malloc(size=%ld) error OOM\n", buf_size); |
|
|
return -1; |
|
|
} |
|
|
|
|
| 78 |
if ((fd = open(data_file, O_WRONLY | O_CREAT | O_TRUNC, 0640)) == -1) |
if ((fd = open(data_file, O_WRONLY | O_CREAT | O_TRUNC, 0640)) == -1) |
| 79 |
{ |
{ |
| 80 |
log_error("open(%s) error (%d)\n", data_file, errno); |
log_error("open(%s) error (%d)\n", data_file, errno); |
| 81 |
return -2; |
return -2; |
| 82 |
} |
} |
| 83 |
|
|
| 84 |
// TODO: Generate article header |
bzero(&cache, sizeof(cache)); |
| 85 |
cache.data_len = (size_t)snprintf(cache.p_data, |
|
| 86 |
ARTICLE_HEADER_MAX_LEN + strlen(content) + 1, |
// Generate article header |
| 87 |
"%s", |
localtime_r(&(p_article->sub_dt), &tm_sub_dt); |
| 88 |
content); |
strftime(str_sub_dt, sizeof(str_sub_dt), "%c", &tm_sub_dt); |
| 89 |
|
|
| 90 |
|
snprintf(header, sizeof(header), "发布者: %s (%s), 版块: %s (%s)\n标 题: %s\n发布于: %s (%s)\n\n", |
| 91 |
|
p_article->username, p_article->nickname, p_section->sname, p_section->stitle, p_article->title, BBS_name, str_sub_dt); |
| 92 |
|
|
| 93 |
|
header_len = strnlen(header, sizeof(header)); |
| 94 |
|
cache.data_len = header_len + strlen(content); |
| 95 |
|
|
| 96 |
|
header_line_cnt = split_data_lines(header, SCREEN_COLS, cache.line_offsets, MAX_SPLIT_FILE_LINES); |
| 97 |
|
cache.line_total = header_line_cnt + |
| 98 |
|
split_data_lines(content, SCREEN_COLS, cache.line_offsets + header_line_cnt, MAX_SPLIT_FILE_LINES - header_line_cnt); |
| 99 |
|
|
|
bzero(cache.line_offsets, sizeof(cache.line_offsets)); |
|
|
cache.line_total = split_data_lines(cache.p_data, SCREEN_COLS, cache.line_offsets, MAX_SPLIT_FILE_LINES); |
|
| 100 |
if (cache.line_total >= MAX_SPLIT_FILE_LINES) |
if (cache.line_total >= MAX_SPLIT_FILE_LINES) |
| 101 |
{ |
{ |
| 102 |
log_error("split_data_lines(%s) truncated over limit lines\n", data_file); |
log_error("split_data_lines(%s) truncated over limit lines %d >= %d\n", data_file, cache.line_total, MAX_SPLIT_FILE_LINES); |
| 103 |
|
cache.line_total = MAX_SPLIT_FILE_LINES - 1; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
for (i = header_line_cnt; i <= cache.line_total; i++) |
| 107 |
|
{ |
| 108 |
|
cache.line_offsets[i] += (long)header_len; |
| 109 |
} |
} |
| 110 |
|
|
| 111 |
if (write(fd, &cache, sizeof(cache)) == -1) |
if (write(fd, &cache, sizeof(cache)) == -1) |
| 114 |
close(fd); |
close(fd); |
| 115 |
return -3; |
return -3; |
| 116 |
} |
} |
| 117 |
if (write(fd, cache.p_data, cache.data_len) == -1) |
if (write(fd, header, header_len) == -1) |
| 118 |
|
{ |
| 119 |
|
log_error("write(%s, header) error (%d)\n", data_file, errno); |
| 120 |
|
close(fd); |
| 121 |
|
return -3; |
| 122 |
|
} |
| 123 |
|
if (write(fd, content, cache.data_len - header_len) == -1) |
| 124 |
{ |
{ |
| 125 |
log_error("write(%s, data) error (%d)\n", data_file, errno); |
log_error("write(%s, content) error (%d)\n", data_file, errno); |
| 126 |
close(fd); |
close(fd); |
| 127 |
return -3; |
return -3; |
| 128 |
} |
} |
| 133 |
return -2; |
return -2; |
| 134 |
} |
} |
| 135 |
|
|
|
free(cache.p_data); |
|
|
|
|
| 136 |
return 0; |
return 0; |
| 137 |
} |
} |
| 138 |
|
|
| 140 |
{ |
{ |
| 141 |
char data_file[FILE_PATH_LEN]; |
char data_file[FILE_PATH_LEN]; |
| 142 |
int fd; |
int fd; |
| 143 |
|
struct stat sb; |
| 144 |
|
void *p_mmap; |
| 145 |
|
size_t mmap_len; |
| 146 |
|
|
| 147 |
if (p_cache == NULL || cache_dir == NULL || p_article == NULL) |
if (p_cache == NULL || cache_dir == NULL || p_article == NULL) |
| 148 |
{ |
{ |
| 162 |
return -2; |
return -2; |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
if (read(fd, (void *)p_cache, sizeof(ARTICLE_CACHE)) == -1) |
if (fstat(fd, &sb) < 0) |
| 166 |
{ |
{ |
| 167 |
log_error("read(%s, cache) error (%d)\n", data_file, errno); |
log_error("fstat(fd) error (%d)\n", errno); |
| 168 |
close(fd); |
return -2; |
|
return -3; |
|
| 169 |
} |
} |
| 170 |
|
|
| 171 |
p_cache->p_data = malloc(p_cache->data_len + 1); |
mmap_len = (size_t)sb.st_size; |
|
if (p_cache->p_data == NULL) |
|
|
{ |
|
|
log_error("malloc(size=%ld) error OOM\n", p_cache->data_len + 1); |
|
|
close(fd); |
|
|
return -1; |
|
|
} |
|
| 172 |
|
|
| 173 |
if (read(fd, p_cache->p_data, p_cache->data_len) == -1) |
p_mmap = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd, 0L); |
| 174 |
|
if (p_mmap == MAP_FAILED) |
| 175 |
{ |
{ |
| 176 |
log_error("read(%s, data) error (%d)\n", data_file, errno); |
log_error("mmap(%s) error (%d)\n", data_file, errno); |
| 177 |
close(fd); |
return -2; |
|
return -3; |
|
| 178 |
} |
} |
|
p_cache->p_data[p_cache->data_len] = '\0'; |
|
| 179 |
|
|
| 180 |
if (close(fd) == -1) |
if (close(fd) == -1) |
| 181 |
{ |
{ |
| 183 |
return -2; |
return -2; |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
|
memcpy((void *)p_cache, p_mmap, sizeof(ARTICLE_CACHE)); |
| 187 |
|
|
| 188 |
|
if (sizeof(ARTICLE_CACHE) + p_cache->data_len != mmap_len) |
| 189 |
|
{ |
| 190 |
|
log_error("Inconsistent length, cache_len(%ld) + data_len(%ld) != mmap_len(%ld)\n", |
| 191 |
|
sizeof(ARTICLE_CACHE), p_cache->data_len, mmap_len); |
| 192 |
|
|
| 193 |
|
if (munmap(p_mmap, mmap_len) < 0) |
| 194 |
|
{ |
| 195 |
|
log_error("munmap() error (%d)\n", errno); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
return -3; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
p_cache->p_mmap = p_mmap; |
| 202 |
|
p_cache->p_data = p_mmap + sizeof(ARTICLE_CACHE); |
| 203 |
|
|
| 204 |
return 0; |
return 0; |
| 205 |
} |
} |
| 206 |
|
|
| 207 |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
| 208 |
{ |
{ |
| 209 |
if (p_cache == NULL || p_cache->p_data == NULL) |
if (p_cache == NULL || p_cache->p_mmap == NULL) |
| 210 |
{ |
{ |
| 211 |
log_error("article_cache_unload() NULL pointer error\n"); |
log_error("article_cache_unload() NULL pointer error\n"); |
| 212 |
return -1; |
return -1; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
free(p_cache->p_data); |
if (munmap(p_cache->p_mmap, sizeof(ARTICLE_CACHE) + p_cache->data_len) < 0) |
| 216 |
|
{ |
| 217 |
|
log_error("munmap() error (%d)\n", errno); |
| 218 |
|
return -2; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
p_cache->p_mmap = NULL; |
| 222 |
p_cache->p_data = NULL; |
p_cache->p_data = NULL; |
| 223 |
|
|
| 224 |
return 0; |
return 0; |