| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "article_cache.h" |
#include "article_cache.h" |
| 18 |
|
#include "log.h" |
| 19 |
|
#include <errno.h> |
| 20 |
|
#include <stdio.h> |
| 21 |
|
#include <fcntl.h> |
| 22 |
|
#include <stdlib.h> |
| 23 |
|
#include <unistd.h> |
| 24 |
|
#include <sys/mman.h> |
| 25 |
|
#include <sys/stat.h> |
| 26 |
|
#include <strings.h> |
| 27 |
|
#define _POSIX_C_SOURCE 200809L |
| 28 |
|
#include <string.h> |
| 29 |
|
|
| 30 |
int article_cache_generate(const char *cache_dir, const ARTICLE *p_article, const char *content) |
#define ARTICLE_HEADER_MAX_LEN 4096 |
| 31 |
|
|
| 32 |
|
inline static int article_cache_path(char *file_path, size_t buf_len, const char *cache_dir, const ARTICLE *p_article) |
| 33 |
{ |
{ |
| 34 |
|
if (file_path == NULL || cache_dir == NULL || p_article == NULL) |
| 35 |
|
{ |
| 36 |
|
log_error("article_cache_path() NULL pointer error\n"); |
| 37 |
|
return -1; |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
snprintf(file_path, buf_len, "%s%d", cache_dir, p_article->cid); |
| 41 |
|
|
| 42 |
|
return 0; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
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]; |
| 48 |
|
int fd; |
| 49 |
|
ARTICLE_CACHE cache; |
| 50 |
|
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) |
| 58 |
|
{ |
| 59 |
|
log_error("article_cache_generate() NULL pointer error\n"); |
| 60 |
|
return -1; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
if (article_cache_path(data_file, sizeof(data_file), cache_dir, p_article) < 0) |
| 64 |
|
{ |
| 65 |
|
log_error("article_cache_path(dir=%s, cid=%d) error\n", cache_dir, p_article->cid); |
| 66 |
|
return -1; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
if (!overwrite) |
| 70 |
|
{ |
| 71 |
|
if ((fd = open(data_file, O_RDONLY)) != -1) // check data file |
| 72 |
|
{ |
| 73 |
|
close(fd); |
| 74 |
|
return 0; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
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); |
| 81 |
|
return -2; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
bzero(&cache, sizeof(cache)); |
| 85 |
|
|
| 86 |
|
// Generate article header |
| 87 |
|
localtime_r(&(p_article->sub_dt), &tm_sub_dt); |
| 88 |
|
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 |
|
|
| 100 |
|
if (cache.line_total >= MAX_SPLIT_FILE_LINES) |
| 101 |
|
{ |
| 102 |
|
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) |
| 112 |
|
{ |
| 113 |
|
log_error("write(%s, cache) error (%d)\n", data_file, errno); |
| 114 |
|
close(fd); |
| 115 |
|
return -3; |
| 116 |
|
} |
| 117 |
|
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, content) error (%d)\n", data_file, errno); |
| 126 |
|
close(fd); |
| 127 |
|
return -3; |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
if (close(fd) == -1) |
| 131 |
|
{ |
| 132 |
|
log_error("close(%s) error (%d)\n", data_file, errno); |
| 133 |
|
return -2; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
return 0; |
return 0; |
| 137 |
} |
} |
| 138 |
|
|
| 139 |
int article_cache_load(ARTICLE_CACHE *p_cache, const char *cache_dir, const ARTICLE *p_article) |
int article_cache_load(ARTICLE_CACHE *p_cache, const char *cache_dir, const ARTICLE *p_article) |
| 140 |
{ |
{ |
| 141 |
|
char data_file[FILE_PATH_LEN]; |
| 142 |
|
int fd; |
| 143 |
|
|
| 144 |
|
if (p_cache == NULL || cache_dir == NULL || p_article == NULL) |
| 145 |
|
{ |
| 146 |
|
log_error("article_cache_load() NULL pointer error\n"); |
| 147 |
|
return -1; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
if (article_cache_path(data_file, sizeof(data_file), cache_dir, p_article) < 0) |
| 151 |
|
{ |
| 152 |
|
log_error("article_cache_path(dir=%s, cid=%d) error\n", cache_dir, p_article->cid); |
| 153 |
|
return -1; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
if ((fd = open(data_file, O_RDONLY)) == -1) |
| 157 |
|
{ |
| 158 |
|
log_error("open(%s) error (%d)\n", data_file, errno); |
| 159 |
|
return -2; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
if (read(fd, (void *)p_cache, sizeof(ARTICLE_CACHE)) == -1) |
| 163 |
|
{ |
| 164 |
|
log_error("read(%s, cache) error (%d)\n", data_file, errno); |
| 165 |
|
close(fd); |
| 166 |
|
return -3; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
p_cache->p_data = malloc(p_cache->data_len + 1); |
| 170 |
|
if (p_cache->p_data == NULL) |
| 171 |
|
{ |
| 172 |
|
log_error("malloc(size=%ld) error OOM\n", p_cache->data_len + 1); |
| 173 |
|
close(fd); |
| 174 |
|
return -1; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
if (read(fd, p_cache->p_data, p_cache->data_len) == -1) |
| 178 |
|
{ |
| 179 |
|
log_error("read(%s, data) error (%d)\n", data_file, errno); |
| 180 |
|
close(fd); |
| 181 |
|
return -3; |
| 182 |
|
} |
| 183 |
|
p_cache->p_data[p_cache->data_len] = '\0'; |
| 184 |
|
|
| 185 |
|
if (close(fd) == -1) |
| 186 |
|
{ |
| 187 |
|
log_error("close(%s) error (%d)\n", data_file, errno); |
| 188 |
|
return -2; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
return 0; |
return 0; |
| 192 |
} |
} |
| 193 |
|
|
| 194 |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
| 195 |
{ |
{ |
| 196 |
|
if (p_cache == NULL || p_cache->p_data == NULL) |
| 197 |
|
{ |
| 198 |
|
log_error("article_cache_unload() NULL pointer error\n"); |
| 199 |
|
return -1; |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
free(p_cache->p_data); |
| 203 |
|
p_cache->p_data = NULL; |
| 204 |
|
|
| 205 |
return 0; |
return 0; |
| 206 |
} |
} |