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

Diff of /lbbs/src/article_cache.c

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

Revision 1.1 by sysadm, Thu May 29 09:44:01 2025 UTC Revision 1.4 by sysadm, Sat May 31 02:42:24 2025 UTC
# Line 15  Line 15 
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            struct stat sb;
144            void *p_mmap;
145            size_t mmap_len;
146    
147            if (p_cache == NULL || cache_dir == NULL || p_article == NULL)
148            {
149                    log_error("article_cache_load() NULL pointer error\n");
150                    return -1;
151            }
152    
153            if (article_cache_path(data_file, sizeof(data_file), cache_dir, p_article) < 0)
154            {
155                    log_error("article_cache_path(dir=%s, cid=%d) error\n", cache_dir, p_article->cid);
156                    return -1;
157            }
158    
159            if ((fd = open(data_file, O_RDONLY)) == -1)
160            {
161                    log_error("open(%s) error (%d)\n", data_file, errno);
162                    return -2;
163            }
164    
165            if (fstat(fd, &sb) < 0)
166            {
167                    log_error("fstat(fd) error (%d)\n", errno);
168                    return -2;
169            }
170    
171            mmap_len = (size_t)sb.st_size;
172    
173            p_mmap = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd, 0L);
174            if (p_mmap == MAP_FAILED)
175            {
176                    log_error("mmap(%s) error (%d)\n", data_file, errno);
177                    return -2;
178            }
179    
180            if (close(fd) == -1)
181            {
182                    log_error("close(%s) error (%d)\n", data_file, errno);
183                    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_mmap == NULL)
210            {
211                    log_error("article_cache_unload() NULL pointer error\n");
212                    return -1;
213            }
214    
215            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;
223    
224          return 0;          return 0;
225  }  }


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

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