/[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.16 by sysadm, Sat May 31 12:00:00 2025 UTC Revision 1.30 by sysadm, Sat Jun 21 02:15:18 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(rs);
157          mysql_close(db);          mysql_close(db);
158    
159          return ret;          return ret;
# Line 161  int load_section_config_from_db(void) Line 161  int load_section_config_from_db(void)
161    
162  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)
163  {  {
164          MYSQL *db;          MYSQL *db = NULL;
165          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
166          MYSQL_ROW row;          MYSQL_ROW row;
167          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
168          ARTICLE article;          ARTICLE article;
# Line 178  int append_articles_from_db(int32_t star Line 178  int append_articles_from_db(int32_t star
178          if (db == NULL)          if (db == NULL)
179          {          {
180                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
181                  return -2;                  ret = -1;
182                    goto cleanup;
183          }          }
184    
185          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 192  int append_articles_from_db(int32_t star Line 193  int append_articles_from_db(int32_t star
193          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
194          {          {
195                  log_error("Query article list error: %s\n", mysql_error(db));                  log_error("Query article list error: %s\n", mysql_error(db));
196                  return -3;                  ret = -2;
197                    goto cleanup;
198          }          }
199          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
200          {          {
201                  log_error("Get article list data failed\n");                  log_error("Get article list data failed\n");
202                  return -3;                  ret = -2;
203                    goto cleanup;
204          }          }
205    
206          // acquire global lock          // acquire global lock
207          if (global_lock)          if (global_lock && (ret = section_list_rw_lock(NULL)) < 0)
208          {          {
209                  if ((ret = section_list_rw_lock(NULL)) < 0)                  log_error("section_list_rw_lock(sid = 0) error\n");
210                  {                  goto cleanup;
                         log_error("section_list_rw_lock(sid = 0) error\n");  
                         goto cleanup;  
                 }  
211          }          }
212    
213          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
214          {          {
215                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
216    
217                  // copy data of article                  // copy data of article
218                  i = 0;                  i = 0;
# Line 311  int append_articles_from_db(int32_t star Line 311  int append_articles_from_db(int32_t star
311          }          }
312    
313          // release global lock          // release global lock
314          if (global_lock)          if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0)
315          {          {
316                  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");  
                 }  
317          }          }
318    
319  cleanup:  cleanup:
320          mysql_free_result(rs);          mysql_free_result(rs);
   
321          mysql_close(db);          mysql_close(db);
322    
323          return (ret < 0 ? ret : article_count);          return (ret < 0 ? ret : article_count);
# Line 329  cleanup: Line 325  cleanup:
325    
326  int set_last_article_op_log_from_db(void)  int set_last_article_op_log_from_db(void)
327  {  {
328          MYSQL *db;          MYSQL *db = NULL;
329          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
330          MYSQL_ROW row;          MYSQL_ROW row;
331          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
332            int ret = 0;
333    
334          db = db_open();          db = db_open();
335          if (db == NULL)          if (db == NULL)
336          {          {
337                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
338                  return -1;                  ret = -1;
339                    goto cleanup;
340          }          }
341    
342          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 347  int set_last_article_op_log_from_db(void Line 345  int set_last_article_op_log_from_db(void
345          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
346          {          {
347                  log_error("Query article op error: %s\n", mysql_error(db));                  log_error("Query article op error: %s\n", mysql_error(db));
348                  return -2;                  ret = -2;
349                    goto cleanup;
350          }          }
351          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
352          {          {
353                  log_error("Get article op data failed\n");                  log_error("Get article op data failed\n");
354                  return -2;                  ret = -2;
355                    goto cleanup;
356          }          }
357    
358          if ((row = mysql_fetch_row(rs)))          if ((row = mysql_fetch_row(rs)))
# Line 360  int set_last_article_op_log_from_db(void Line 360  int set_last_article_op_log_from_db(void
360                  last_article_op_log_mid = atoi(row[0]);                  last_article_op_log_mid = atoi(row[0]);
361          }          }
362    
363    cleanup:
364          mysql_free_result(rs);          mysql_free_result(rs);
   
365          mysql_close(db);          mysql_close(db);
366    
367          return last_article_op_log_mid;          return (ret < 0 ? ret : last_article_op_log_mid);
368  }  }
369    
370  int apply_article_op_log_from_db(int op_count_limit)  int apply_article_op_log_from_db(int op_count_limit)
371  {  {
372          MYSQL *db;          MYSQL *db = NULL;
373          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
374          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
375          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
376          ARTICLE *p_article;          ARTICLE *p_article;
# Line 386  int apply_article_op_log_from_db(int op_ Line 386  int apply_article_op_log_from_db(int op_
386          if (db == NULL)          if (db == NULL)
387          {          {
388                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
389                  return -3;                  ret = -1;
390                    goto cleanup;
391          }          }
392    
393          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
# Line 398  int apply_article_op_log_from_db(int op_ Line 399  int apply_article_op_log_from_db(int op_
399          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
400          {          {
401                  log_error("Query article log error: %s\n", mysql_error(db));                  log_error("Query article log error: %s\n", mysql_error(db));
402                  return -3;                  ret = -2;
403                    goto cleanup;
404          }          }
405          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
406          {          {
407                  log_error("Get article log data failed\n");                  log_error("Get article log data failed\n");
408                  return -3;                  ret = -2;
409                    goto cleanup;
410          }          }
411    
412          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
# Line 451  int apply_article_op_log_from_db(int op_ Line 454  int apply_article_op_log_from_db(int op_
454                          break;                          break;
455                  case 'D': // Delete article                  case 'D': // Delete article
456                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
457                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
458                          {                          {
459                                  // 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]));
460                                  do                          }
461                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
462                                          p_article = p_article->p_topic_next;                          {
463                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
464                          }                          }
465                          break;                          break;
466                  case 'S': // Restore article                  case 'S': // Restore article
467                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
468                            {
469                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
470                            }
471                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
472                            {
473                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
474                            }
475                          break;                          break;
476                  case 'L': // Lock article                  case 'L': // Lock article
477                          p_article->lock = 1;                          p_article->lock = 1;
# Line 510  int apply_article_op_log_from_db(int op_ Line 518  int apply_article_op_log_from_db(int op_
518                                  ret = -4;                                  ret = -4;
519                          }                          }
520                          mysql_free_result(rs2);                          mysql_free_result(rs2);
521                            rs2 = NULL;
522    
523                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
524                          break;                          break;
525                  case 'T': // Move article                  case 'T': // Move article
526                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 538  int apply_article_op_log_from_db(int op_ Line 549  int apply_article_op_log_from_db(int op_
549                                  ret = -4;                                  ret = -4;
550                          }                          }
551                          mysql_free_result(rs2);                          mysql_free_result(rs2);
552                            rs2 = NULL;
553    
554                          if (sid_dest > 0 && sid_dest != p_article->sid)                          if (sid_dest > 0 && sid_dest != p_article->sid)
555                          {                          {
# Line 607  int apply_article_op_log_from_db(int op_ Line 619  int apply_article_op_log_from_db(int op_
619                  }                  }
620          }          }
621    
622    cleanup:
623            mysql_free_result(rs2);
624          mysql_free_result(rs);          mysql_free_result(rs);
   
625          mysql_close(db);          mysql_close(db);
626    
627          return (ret < 0 ? ret : op_count);          return (ret < 0 ? ret : op_count);
# Line 636  int section_list_loader_launch(void) Line 649  int section_list_loader_launch(void)
649          {          {
650                  SYS_child_process_count++;                  SYS_child_process_count++;
651                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
652                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
653                  return 0;                  return 0;
654          }          }
655          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 667  int section_list_loader_launch(void) Line 680  int section_list_loader_launch(void)
680                          }                          }
681                          else                          else
682                          {                          {
683                                  log_error("Reload section config successfully\n");                                  log_common("Reload section config successfully\n");
684                          }                          }
685                  }                  }
686    
# Line 692  int section_list_loader_launch(void) Line 705  int section_list_loader_launch(void)
705                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
706                  if (load_count > 0)                  if (load_count > 0)
707                  {                  {
708                          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());
709                  }                  }
710    
711                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 718  int section_list_loader_launch(void) Line 731  int section_list_loader_launch(void)
731    
732                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
733                  {                  {
734                          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);
735                  }                  }
736    
737                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 726  int section_list_loader_launch(void) Line 739  int section_list_loader_launch(void)
739                          continue;                          continue;
740                  }                  }
741    
742                  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++)
743                  {                  {
744                          sleep(1);                          sleep(1);
745                  }                  }
# Line 739  int section_list_loader_launch(void) Line 752  int section_list_loader_launch(void)
752          detach_article_block_shm();          detach_article_block_shm();
753          detach_trie_dict_shm();          detach_trie_dict_shm();
754    
755          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
756          log_end();          log_end();
757    
758          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 830  int query_section_articles(SECTION_LIST Line 843  int query_section_articles(SECTION_LIST
843          return ret;          return ret;
844  }  }
845    
846  int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,  int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step,
847                                                            int *p_page_id, int *p_visible_offset, int *p_article_count)                                                            int *p_page_id, int *p_visible_offset, int *p_article_count)
848  {  {
849          const ARTICLE *p_article = NULL;          const ARTICLE *p_article = NULL;
850          ARTICLE *p_tmp;          ARTICLE *p_tmp;
851          int32_t aid = 0;          int32_t aid = 0;
852          int page_id;          int page_id = 0;
853          int offset;          int offset = 0;
854          int ret = 0;          int ret = 0;
855          int i;          int i;
856    
# Line 860  int locate_article_in_section(SECTION_LI Line 873  int locate_article_in_section(SECTION_LI
873          }          }
874          else if (direction == 1)          else if (direction == 1)
875          {          {
876                  p_article = p_article_cur;                  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)
                 do  
877                  {                  {
878                          p_article = p_article->p_topic_next;                          if (p_article->visible)
879                  } while (p_article != p_article_cur && p_article->visible == 0);                          {
880                                    step--;
881                            }
882                    }
883    
884                  aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0);                  aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0);
885          }          }
886          else if (direction == -1)          else if (direction == -1)
887          {          {
888                  p_article = p_article_cur;                  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)
                 do  
889                  {                  {
890                          p_article = p_article->p_topic_prior;                          if (p_article->visible)
891                  } while (p_article != p_article_cur && p_article->visible == 0);                          {
892                                    step--;
893                            }
894                    }
895    
896                  aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0);                  aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0);
897          }          }
898    
899          p_article = NULL;          p_article = NULL;
# Line 889  int locate_article_in_section(SECTION_LI Line 906  int locate_article_in_section(SECTION_LI
906                          *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);                          *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
907    
908                          p_article = p_section->p_page_first_article[page_id];                          p_article = p_section->p_page_first_article[page_id];
909                          for (i = 0; i < *p_article_count;)                          for (i = 0; i < *p_article_count && p_article != NULL;)
910                          {                          {
911                                  if (p_article->visible)                                  if (p_article->visible)
912                                  {                                  {
# Line 904  int locate_article_in_section(SECTION_LI Line 921  int locate_article_in_section(SECTION_LI
921                                          {                                          {
922                                                  log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);                                                  log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
923                                                  p_article = NULL;                                                  p_article = NULL;
924                                                    break;
925                                          }                                          }
926                                  }                                  }
927                                  p_article = p_article->p_next;                                  p_article = p_article->p_next;


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

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