/[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.2 by sysadm, Fri May 30 12:51:00 2025 UTC Revision 1.3 by sysadm, Sat May 31 01:37:18 2025 UTC
# Line 42  inline static int article_cache_path(cha Line 42  inline static int article_cache_path(cha
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          {          {
# Line 70  int article_cache_generate(const char *c Line 75  int article_cache_generate(const char *c
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)
# Line 103  int article_cache_generate(const char *c Line 114  int article_cache_generate(const char *c
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          }          }
# Line 116  int article_cache_generate(const char *c Line 133  int article_cache_generate(const char *c
133                  return -2;                  return -2;
134          }          }
135    
         free(cache.p_data);  
   
136          return 0;          return 0;
137  }  }
138    


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

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