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

Diff of /lbbs/src/section_list_loader.c

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

Revision 1.12 by sysadm, Fri May 30 12:51:00 2025 UTC Revision 1.33 by sysadm, Mon Jun 23 13:14:15 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list_loader.h"  
17  #include "article_cache.h"  #include "article_cache.h"
18  #include "log.h"  #include "bbs.h"
19  #include "database.h"  #include "database.h"
20    #include "log.h"
21  #include "menu.h"  #include "menu.h"
22  #include <stdio.h>  #include "section_list_loader.h"
 #include <string.h>  
23  #include <errno.h>  #include <errno.h>
24  #include <signal.h>  #include <signal.h>
25    #include <stdio.h>
26  #include <stdlib.h>  #include <stdlib.h>
 #include <strings.h>  
 #include <unistd.h>  
   
 #define _POSIX_C_SOURCE 200809L  
27  #include <string.h>  #include <string.h>
28    #include <unistd.h>
 #define SECTION_LIST_LOAD_INTERVAL 10 // second  
29    
30  int section_list_loader_pid;  int section_list_loader_pid;
31  int last_article_op_log_mid;  int last_article_op_log_mid;
32    
33  int load_section_config_from_db(void)  int load_section_config_from_db(void)
34  {  {
35          MYSQL *db;          MYSQL *db = NULL;
36          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
37          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
38          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
39          int32_t sid;          int32_t sid;
40          char master_list[(BBS_username_max_len + 1) * 3 + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
41          SECTION_LIST *p_section;          SECTION_LIST *p_section;
42          int ret;          int ret = 0;
43    
44          db = db_open();          db = db_open();
45          if (db == NULL)          if (db == NULL)
46          {          {
47                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
48                  return -2;                  ret = -1;
49                    goto cleanup;
50          }          }
51    
52          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 62  int load_section_config_from_db(void) Line 58  int load_section_config_from_db(void)
58          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
59          {          {
60                  log_error("Query section_list error: %s\n", mysql_error(db));                  log_error("Query section_list error: %s\n", mysql_error(db));
61                  return -3;                  ret = -2;
62                    goto cleanup;
63          }          }
64          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
65          {          {
66                  log_error("Get section_list data failed\n");                  log_error("Get section_list data failed\n");
67                  return -3;                  ret = -2;
68                    goto cleanup;
69          }          }
70    
71          ret = 0;          ret = 0;
# Line 103  int load_section_config_from_db(void) Line 101  int load_section_config_from_db(void)
101                          strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));                          strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
102                  }                  }
103                  mysql_free_result(rs2);                  mysql_free_result(rs2);
104                    rs2 = NULL;
105    
106                  p_section = section_list_find_by_sid(sid);                  p_section = section_list_find_by_sid(sid);
107    
# Line 132  int load_section_config_from_db(void) Line 131  int load_section_config_from_db(void)
131                                  break;                                  break;
132                          }                          }
133    
134                          strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);                          strncpy(p_section->sname, row[0], sizeof(p_section->sname) - 1);
135                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
136                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
137                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
# Line 152  int load_section_config_from_db(void) Line 151  int load_section_config_from_db(void)
151                          break;                          break;
152                  }                  }
153          }          }
         mysql_free_result(rs);  
154    
155    cleanup:
156            mysql_free_result(rs2);
157            mysql_free_result(rs);
158          mysql_close(db);          mysql_close(db);
159    
160          return ret;          return ret;
# Line 161  int load_section_config_from_db(void) Line 162  int load_section_config_from_db(void)
162    
163  int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)  int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
164  {  {
165          MYSQL *db;          MYSQL *db = NULL;
166          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
167          MYSQL_ROW row;          MYSQL_ROW row;
168          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
169          ARTICLE article;          ARTICLE article;
170          ARTICLE *p_topic;          ARTICLE *p_topic;
171          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
172          int32_t last_sid = 0;          int32_t last_sid = 0;
173            char sub_ip[IP_ADDR_LEN];
174          int article_count = 0;          int article_count = 0;
175          int ret = 0;          int ret = 0;
176          int i;          int i;
# Line 177  int append_articles_from_db(int32_t star Line 179  int append_articles_from_db(int32_t star
179          if (db == NULL)          if (db == NULL)
180          {          {
181                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
182                  return -2;                  ret = -1;
183                    goto cleanup;
184          }          }
185    
186          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
187                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
188                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
189                           "bbs_content.content "                           "sub_ip, bbs_content.content "
190                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
191                           "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",                           "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
192                           start_aid, article_count_limit);                           start_aid, article_count_limit);
# Line 191  int append_articles_from_db(int32_t star Line 194  int append_articles_from_db(int32_t star
194          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
195          {          {
196                  log_error("Query article list error: %s\n", mysql_error(db));                  log_error("Query article list error: %s\n", mysql_error(db));
197                  return -3;                  ret = -2;
198                    goto cleanup;
199          }          }
200          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
201          {          {
202                  log_error("Get article list data failed\n");                  log_error("Get article list data failed\n");
203                  return -3;                  ret = -2;
204                    goto cleanup;
205          }          }
206    
207          // acquire global lock          // acquire global lock
208          if (global_lock)          if (global_lock && (ret = section_list_rw_lock(NULL)) < 0)
209          {          {
210                  if ((ret = section_list_rw_lock(NULL)) < 0)                  log_error("section_list_rw_lock(sid = 0) error\n");
211                  {                  goto cleanup;
                         log_error("section_list_rw_lock(sid = 0) error\n");  
                         goto cleanup;  
                 }  
212          }          }
213    
214          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
215          {          {
216                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
217    
218                  // copy data of article                  // copy data of article
219                  i = 0;                  i = 0;
# Line 236  int append_articles_from_db(int32_t star Line 238  int append_articles_from_db(int32_t star
238    
239                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
240    
241                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
242                    sub_ip[sizeof(sub_ip) - 1] = '\0';
243                    ip_mask(sub_ip, 1, '*');
244    
245                  // release lock of last section if different from current one                  // release lock of last section if different from current one
246                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
247                  {                  {
# Line 288  int append_articles_from_db(int32_t star Line 294  int append_articles_from_db(int32_t star
294    
295                  article_count++;                  article_count++;
296    
297                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, row[i++], 0) < 0)                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
298                  {                  {
299                          log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);                          log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
300                          ret = -4;                          ret = -4;
# Line 306  int append_articles_from_db(int32_t star Line 312  int append_articles_from_db(int32_t star
312          }          }
313    
314          // release global lock          // release global lock
315          if (global_lock)          if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0)
316          {          {
317                  if ((ret = section_list_rw_unlock(NULL)) < 0)                  log_error("section_list_rw_unlock(sid=0) error\n");
                 {  
                         log_error("section_list_rw_unlock(sid=0) error\n");  
                 }  
318          }          }
319    
320  cleanup:  cleanup:
321          mysql_free_result(rs);          mysql_free_result(rs);
   
322          mysql_close(db);          mysql_close(db);
323    
324            article_cache_cleanup();
325    
326          return (ret < 0 ? ret : article_count);          return (ret < 0 ? ret : article_count);
327  }  }
328    
329  int set_last_article_op_log_from_db(void)  int set_last_article_op_log_from_db(void)
330  {  {
331          MYSQL *db;          MYSQL *db = NULL;
332          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
333          MYSQL_ROW row;          MYSQL_ROW row;
334          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
335            int ret = 0;
336    
337          db = db_open();          db = db_open();
338          if (db == NULL)          if (db == NULL)
339          {          {
340                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
341                  return -1;                  ret = -1;
342                    goto cleanup;
343          }          }
344    
345          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 342  int set_last_article_op_log_from_db(void Line 348  int set_last_article_op_log_from_db(void
348          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
349          {          {
350                  log_error("Query article op error: %s\n", mysql_error(db));                  log_error("Query article op error: %s\n", mysql_error(db));
351                  return -2;                  ret = -2;
352                    goto cleanup;
353          }          }
354          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
355          {          {
356                  log_error("Get article op data failed\n");                  log_error("Get article op data failed\n");
357                  return -2;                  ret = -2;
358                    goto cleanup;
359          }          }
360    
361          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 355  int set_last_article_op_log_from_db(void Line 363  int set_last_article_op_log_from_db(void
363                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
364          }          }
365    
366    cleanup:
367          mysql_free_result(rs);          mysql_free_result(rs);
   
368          mysql_close(db);          mysql_close(db);
369    
370          return last_article_op_log_mid;          return (ret < 0 ? ret : last_article_op_log_mid);
371  }  }
372    
373  int apply_article_op_log_from_db(int op_count_limit)  int apply_article_op_log_from_db(int op_count_limit)
374  {  {
375          MYSQL *db;          MYSQL *db = NULL;
376          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
377          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
378          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
379          ARTICLE *p_article;          ARTICLE *p_article;
380          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
381          SECTION_LIST *p_section_dest;          SECTION_LIST *p_section_dest;
382            char sub_ip[IP_ADDR_LEN];
383          int32_t last_sid = 0;          int32_t last_sid = 0;
384          int32_t sid_dest;          int32_t sid_dest;
385          int op_count = 0;          int op_count = 0;
# Line 380  int apply_article_op_log_from_db(int op_ Line 389  int apply_article_op_log_from_db(int op_
389          if (db == NULL)          if (db == NULL)
390          {          {
391                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
392                  return -3;                  ret = -1;
393                    goto cleanup;
394          }          }
395    
396          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 392  int apply_article_op_log_from_db(int op_ Line 402  int apply_article_op_log_from_db(int op_
402          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
403          {          {
404                  log_error("Query article log error: %s\n", mysql_error(db));                  log_error("Query article log error: %s\n", mysql_error(db));
405                  return -3;                  ret = -2;
406                    goto cleanup;
407          }          }
408          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
409          {          {
410                  log_error("Get article log data failed\n");                  log_error("Get article log data failed\n");
411                  return -3;                  ret = -2;
412                    goto cleanup;
413          }          }
414    
415          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
# Line 445  int apply_article_op_log_from_db(int op_ Line 457  int apply_article_op_log_from_db(int op_
457                          break;                          break;
458                  case 'D': // Delete article                  case 'D': // Delete article
459                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
460                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
461                          {                          {
462                                  // Set articles in the topic to be invisible                                  log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1]));
463                                  do                          }
464                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
465                                          p_article = p_article->p_topic_next;                          {
466                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
467                          }                          }
468                          break;                          break;
469                  case 'S': // Restore article                  case 'S': // Restore article
470                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
471                            {
472                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
473                            }
474                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
475                            {
476                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
477                            }
478                          break;                          break;
479                  case 'L': // Lock article                  case 'L': // Lock article
480                          p_article->lock = 1;                          p_article->lock = 1;
# Line 467  int apply_article_op_log_from_db(int op_ Line 484  int apply_article_op_log_from_db(int op_
484                          break;                          break;
485                  case 'M': // Modify article                  case 'M': // Modify article
486                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
487                                           "SELECT bbs.CID, bbs_content.content "                                           "SELECT bbs.CID, sub_ip, bbs_content.content "
488                                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "                                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
489                                           "WHERE bbs.AID = %d",                                           "WHERE bbs.AID = %d",
490                                           p_article->aid);                                           p_article->aid);
# Line 488  int apply_article_op_log_from_db(int op_ Line 505  int apply_article_op_log_from_db(int op_
505                          {                          {
506                                  p_article->cid = atoi(row2[0]);                                  p_article->cid = atoi(row2[0]);
507    
508                                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, row2[1], 0) < 0)                                  strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
509                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
510                                    ip_mask(sub_ip, 1, '*');
511    
512                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
513                                  {                                  {
514                                          log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);                                          log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
515                                          ret = -4;                                          ret = -4;
# Line 500  int apply_article_op_log_from_db(int op_ Line 521  int apply_article_op_log_from_db(int op_
521                                  ret = -4;                                  ret = -4;
522                          }                          }
523                          mysql_free_result(rs2);                          mysql_free_result(rs2);
524                            rs2 = NULL;
525    
526                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
527                          break;                          break;
528                  case 'T': // Move article                  case 'T': // Move article
529                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 528  int apply_article_op_log_from_db(int op_ Line 552  int apply_article_op_log_from_db(int op_
552                                  ret = -4;                                  ret = -4;
553                          }                          }
554                          mysql_free_result(rs2);                          mysql_free_result(rs2);
555                            rs2 = NULL;
556    
557                          if (sid_dest > 0 && sid_dest != p_article->sid)                          if (sid_dest > 0 && sid_dest != p_article->sid)
558                          {                          {
# Line 564  int apply_article_op_log_from_db(int op_ Line 589  int apply_article_op_log_from_db(int op_
589                          p_article->excerption = 0;                          p_article->excerption = 0;
590                          break;                          break;
591                  case 'F': // Set article on top                  case 'F': // Set article on top
                         p_article->ontop = 1;  
                         break;  
592                  case 'V': // Unset article on top                  case 'V': // Unset article on top
593                          p_article->ontop = 0;                          p_article->ontop = (row[2][0] == 'F' ? 1 : 0);
594                            if (section_list_update_article_ontop(p_section, p_article) < 0)
595                            {
596                                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
597                                                      p_section->sid, p_article->aid);
598                            }
599                          break;                          break;
600                  case 'Z': // Set article as trnasship                  case 'Z': // Set article as trnasship
601                          p_article->transship = 1;                          p_article->transship = 1;
# Line 597  int apply_article_op_log_from_db(int op_ Line 625  int apply_article_op_log_from_db(int op_
625                  }                  }
626          }          }
627    
628    cleanup:
629            mysql_free_result(rs2);
630          mysql_free_result(rs);          mysql_free_result(rs);
   
631          mysql_close(db);          mysql_close(db);
632    
633          return (ret < 0 ? ret : op_count);          return (ret < 0 ? ret : op_count);
# Line 626  int section_list_loader_launch(void) Line 655  int section_list_loader_launch(void)
655          {          {
656                  SYS_child_process_count++;                  SYS_child_process_count++;
657                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
658                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
659                  return 0;                  return 0;
660          }          }
661          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 657  int section_list_loader_launch(void) Line 686  int section_list_loader_launch(void)
686                          }                          }
687                          else                          else
688                          {                          {
689                                  log_error("Reload section config successfully\n");                                  log_common("Reload section config successfully\n");
690                          }                          }
691                  }                  }
692    
# Line 682  int section_list_loader_launch(void) Line 711  int section_list_loader_launch(void)
711                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
712                  if (load_count > 0)                  if (load_count > 0)
713                  {                  {
714                          log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());                          log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
715                  }                  }
716    
717                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 708  int section_list_loader_launch(void) Line 737  int section_list_loader_launch(void)
737    
738                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
739                  {                  {
740                          log_std("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);                          log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
741                  }                  }
742    
743                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 716  int section_list_loader_launch(void) Line 745  int section_list_loader_launch(void)
745                          continue;                          continue;
746                  }                  }
747    
748                  for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++)                  for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)
749                  {                  {
750                          sleep(1);                          sleep(1);
751                  }                  }
# Line 724  int section_list_loader_launch(void) Line 753  int section_list_loader_launch(void)
753    
754          // Child process exit          // Child process exit
755    
756            article_cache_cleanup();
757    
758          // Detach data pools shm          // Detach data pools shm
759          detach_section_list_shm();          detach_section_list_shm();
760          detach_article_block_shm();          detach_article_block_shm();
761          detach_trie_dict_shm();          detach_trie_dict_shm();
762    
763          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
764          log_end();          log_end();
765    
766          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 756  int section_list_loader_reload(void) Line 787  int section_list_loader_reload(void)
787          return 0;          return 0;
788  }  }
789    
790  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[],
791                                                       int *p_article_count, int *p_page_count, int *p_ontop_start_offset)
792  {  {
793          ARTICLE *p_article;          ARTICLE *p_article;
794          ARTICLE *p_next_page_first_article;          ARTICLE *p_next_page_first_article;
795          int ret = 0;          int ret = 0;
796            int i;
797    
798          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)          if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL || p_ontop_start_offset == NULL)
799          {          {
800                  log_error("query_section_articles() NULL pointer error\n");                  log_error("query_section_articles() NULL pointer error\n");
801                  return -1;                  return -1;
# Line 775  int query_section_articles(SECTION_LIST Line 808  int query_section_articles(SECTION_LIST
808                  return -2;                  return -2;
809          }          }
810    
811          *p_page_count = p_section->page_count;          *p_page_count = section_list_page_count_with_ontop(p_section);
812            *p_article_count = 0;
813            *p_ontop_start_offset = BBS_article_limit_per_page;
814    
815          if (p_section->visible_article_count == 0)          if (p_section->visible_article_count == 0)
816          {          {
817                  *p_article_count = 0;                  // empty section
818          }          }
819          else if (page_id < 0 || page_id >= p_section->page_count)          else if (page_id < 0 || page_id >= *p_page_count)
820          {          {
821                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);                  log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, *p_page_count);
822                  ret = -3;                  ret = -3;
823          }          }
824          else          else
825          {          {
826                  ret = page_id;                  ret = page_id;
                 p_article = p_section->p_page_first_article[page_id];  
                 p_next_page_first_article =  
                         (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);  
                 *p_article_count = 0;  
827    
828                  do                  if (page_id <= p_section->page_count - 1)
829                  {                  {
830                          if (p_article->visible)                          p_article = p_section->p_page_first_article[page_id];
831                            p_next_page_first_article =
832                                    (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
833    
834                            do
835                          {                          {
836                                  p_articles[*p_article_count] = p_article;                                  if (p_article->visible)
837                                  (*p_article_count)++;                                  {
838                                            p_articles[*p_article_count] = p_article;
839                                            (*p_article_count)++;
840                                    }
841                                    p_article = p_article->p_next;
842                            } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
843    
844                            if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
845                            {
846                                    log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
847                          }                          }
848                          p_article = p_article->p_next;                  }
                 } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);  
849    
850                  if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))                  *p_ontop_start_offset = *p_article_count;
851    
852                    // Append ontop articles
853                    if (page_id >= p_section->page_count - 1)
854                  {                  {
855                          log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);                          i = (page_id == p_section->page_count - 1
856                                             ? 0
857                                             : (page_id - p_section->page_count + 1) * BBS_article_limit_per_page - p_section->last_page_visible_article_count);
858                            while (*p_article_count < BBS_article_limit_per_page && i < p_section->ontop_article_count)
859                            {
860                                    p_articles[(*p_article_count)++] = p_section->p_ontop_articles[i++];
861                            }
862                  }                  }
863          }          }
864    
# Line 819  int query_section_articles(SECTION_LIST Line 871  int query_section_articles(SECTION_LIST
871    
872          return ret;          return ret;
873  }  }
874    
875    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step,
876                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
877    {
878            const ARTICLE *p_article = NULL;
879            ARTICLE *p_tmp;
880            int32_t aid = 0;
881            int page_id = 0;
882            int offset = 0;
883            int ret = 0;
884            int i;
885    
886            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
887            {
888                    log_error("locate_article_in_section() NULL pointer error\n");
889                    return -1;
890            }
891    
892            // acquire lock of section
893            if ((ret = section_list_rd_lock(p_section)) < 0)
894            {
895                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
896                    return -2;
897            }
898    
899            if (direction == 0)
900            {
901                    aid = p_article_cur->aid;
902            }
903            else if (direction == 1)
904            {
905                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_next->aid > p_article_cur->aid; p_article = p_article->p_topic_next)
906                    {
907                            if (p_article->visible)
908                            {
909                                    step--;
910                            }
911                    }
912    
913                    aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0);
914            }
915            else if (direction == -1)
916            {
917                    for (p_article = p_article_cur; step > 0 && p_article->p_topic_prior->aid < p_article_cur->aid; p_article = p_article->p_topic_prior)
918                    {
919                            if (p_article->visible)
920                            {
921                                    step--;
922                            }
923                    }
924    
925                    aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0);
926            }
927    
928            p_article = NULL;
929    
930            if (aid > 0)
931            {
932                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
933                    if (p_article != NULL)
934                    {
935                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
936    
937                            p_article = p_section->p_page_first_article[page_id];
938                            for (i = 0; i < *p_article_count && p_article != NULL;)
939                            {
940                                    if (p_article->visible)
941                                    {
942                                            if (p_article->aid == aid)
943                                            {
944                                                    *p_page_id = page_id;
945                                                    *p_visible_offset = i;
946                                                    break;
947                                            }
948                                            i++;
949                                            if (i >= *p_article_count)
950                                            {
951                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
952                                                    p_article = NULL;
953                                                    break;
954                                            }
955                                    }
956                                    p_article = p_article->p_next;
957                            }
958    
959                            // Include ontop articles
960                            *p_article_count = section_list_page_article_count_with_ontop(p_section, page_id);
961                    }
962            }
963    
964            // release lock of section
965            if (section_list_rd_unlock(p_section) < 0)
966            {
967                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
968                    ret = -2;
969            }
970    
971            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
972    }


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

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