/[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.2 by sysadm, Fri May 30 12:51:00 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 char *content, int overwrite)
46  {  {
47            char data_file[FILE_PATH_LEN];
48            int fd;
49            ARTICLE_CACHE cache;
50            size_t buf_size;
51    
52            if (cache_dir == NULL || p_article == NULL || content == NULL)
53            {
54                    log_error("article_cache_generate() NULL pointer error\n");
55                    return -1;
56            }
57    
58            if (article_cache_path(data_file, sizeof(data_file), cache_dir, p_article) < 0)
59            {
60                    log_error("article_cache_path(dir=%s, cid=%d) error\n", cache_dir, p_article->cid);
61                    return -1;
62            }
63    
64            if (!overwrite)
65            {
66                    if ((fd = open(data_file, O_RDONLY)) != -1) // check data file
67                    {
68                            close(fd);
69                            return 0;
70                    }
71            }
72    
73            buf_size = ARTICLE_HEADER_MAX_LEN + strlen(content) + 1;
74            cache.p_data = malloc(buf_size);
75            if (cache.p_data == NULL)
76            {
77                    log_error("malloc(size=%ld) error OOM\n", buf_size);
78                    return -1;
79            }
80    
81            if ((fd = open(data_file, O_WRONLY | O_CREAT | O_TRUNC, 0640)) == -1)
82            {
83                    log_error("open(%s) error (%d)\n", data_file, errno);
84                    return -2;
85            }
86    
87            // TODO: Generate article header
88            cache.data_len = (size_t)snprintf(cache.p_data,
89                                                                              ARTICLE_HEADER_MAX_LEN + strlen(content) + 1,
90                                                                              "%s",
91                                                                              content);
92    
93            bzero(cache.line_offsets, sizeof(cache.line_offsets));
94            cache.line_total = split_data_lines(cache.p_data, SCREEN_COLS, cache.line_offsets, MAX_SPLIT_FILE_LINES);
95            if (cache.line_total >= MAX_SPLIT_FILE_LINES)
96            {
97                    log_error("split_data_lines(%s) truncated over limit lines\n", data_file);
98            }
99    
100            if (write(fd, &cache, sizeof(cache)) == -1)
101            {
102                    log_error("write(%s, cache) error (%d)\n", data_file, errno);
103                    close(fd);
104                    return -3;
105            }
106            if (write(fd, cache.p_data, cache.data_len) == -1)
107            {
108                    log_error("write(%s, data) error (%d)\n", data_file, errno);
109                    close(fd);
110                    return -3;
111            }
112    
113            if (close(fd) == -1)
114            {
115                    log_error("close(%s) error (%d)\n", data_file, errno);
116                    return -2;
117            }
118    
119            free(cache.p_data);
120    
121          return 0;          return 0;
122  }  }
123    
124  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)
125  {  {
126            char data_file[FILE_PATH_LEN];
127            int fd;
128    
129            if (p_cache == NULL || cache_dir == NULL || p_article == NULL)
130            {
131                    log_error("article_cache_load() NULL pointer error\n");
132                    return -1;
133            }
134    
135            if (article_cache_path(data_file, sizeof(data_file), cache_dir, p_article) < 0)
136            {
137                    log_error("article_cache_path(dir=%s, cid=%d) error\n", cache_dir, p_article->cid);
138                    return -1;
139            }
140    
141            if ((fd = open(data_file, O_RDONLY)) == -1)
142            {
143                    log_error("open(%s) error (%d)\n", data_file, errno);
144                    return -2;
145            }
146    
147            if (read(fd, (void *)p_cache, sizeof(ARTICLE_CACHE)) == -1)
148            {
149                    log_error("read(%s, cache) error (%d)\n", data_file, errno);
150                    close(fd);
151                    return -3;
152            }
153    
154            p_cache->p_data = malloc(p_cache->data_len + 1);
155            if (p_cache->p_data == NULL)
156            {
157                    log_error("malloc(size=%ld) error OOM\n", p_cache->data_len + 1);
158                    close(fd);
159                    return -1;
160            }
161    
162            if (read(fd, p_cache->p_data, p_cache->data_len) == -1)
163            {
164                    log_error("read(%s, data) error (%d)\n", data_file, errno);
165                    close(fd);
166                    return -3;
167            }
168            p_cache->p_data[p_cache->data_len] = '\0';
169    
170            if (close(fd) == -1)
171            {
172                    log_error("close(%s) error (%d)\n", data_file, errno);
173                    return -2;
174            }
175    
176          return 0;          return 0;
177  }  }
178    
179  int article_cache_unload(ARTICLE_CACHE *p_cache)  int article_cache_unload(ARTICLE_CACHE *p_cache)
180  {  {
181            if (p_cache == NULL || p_cache->p_data == NULL)
182            {
183                    log_error("article_cache_unload() NULL pointer error\n");
184                    return -1;
185            }
186    
187            free(p_cache->p_data);
188            p_cache->p_data = NULL;
189    
190          return 0;          return 0;
191  }  }


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

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