/[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.11 by sysadm, Thu May 29 09:44:01 2025 UTC Revision 1.22 by sysadm, Mon Jun 16 14:30:44 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "section_list_loader.h"  #include "section_list_loader.h"
20  #include "article_cache.h"  #include "article_cache.h"
21    #include "bbs.h"
22  #include "log.h"  #include "log.h"
23  #include "database.h"  #include "database.h"
24  #include "menu.h"  #include "menu.h"
# Line 24  Line 27 
27  #include <errno.h>  #include <errno.h>
28  #include <signal.h>  #include <signal.h>
29  #include <stdlib.h>  #include <stdlib.h>
30    #include <string.h>
31  #include <strings.h>  #include <strings.h>
32  #include <unistd.h>  #include <unistd.h>
33    
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
   
 #define SECTION_LIST_LOAD_INTERVAL 10 // second  
   
34  int section_list_loader_pid;  int section_list_loader_pid;
35  int last_article_op_log_mid;  int last_article_op_log_mid;
36    
# Line 169  int append_articles_from_db(int32_t star Line 168  int append_articles_from_db(int32_t star
168          ARTICLE *p_topic;          ARTICLE *p_topic;
169          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
170          int32_t last_sid = 0;          int32_t last_sid = 0;
171            char sub_ip[IP_ADDR_LEN];
172          int article_count = 0;          int article_count = 0;
173          int ret = 0;          int ret = 0;
174          int i;          int i;
# Line 183  int append_articles_from_db(int32_t star Line 183  int append_articles_from_db(int32_t star
183          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
184                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
185                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
186                           "bbs_content.content "                           "sub_ip, bbs_content.content "
187                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
188                           "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",                           "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
189                           start_aid, article_count_limit);                           start_aid, article_count_limit);
# Line 211  int append_articles_from_db(int32_t star Line 211  int append_articles_from_db(int32_t star
211    
212          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
213          {          {
214                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
215    
216                  // copy data of article                  // copy data of article
217                  i = 0;                  i = 0;
# Line 236  int append_articles_from_db(int32_t star Line 236  int append_articles_from_db(int32_t star
236    
237                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
238    
239                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
240                    sub_ip[sizeof(sub_ip) - 1] = '\0';
241                    ip_mask(sub_ip, 1, '*');
242    
243                  // release lock of last section if different from current one                  // release lock of last section if different from current one
244                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
245                  {                  {
# Line 288  int append_articles_from_db(int32_t star Line 292  int append_articles_from_db(int32_t star
292    
293                  article_count++;                  article_count++;
294    
295                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, row[i++]) < 0)                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
296                  {                  {
297                          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);
298                          ret = -4;                          ret = -4;
# Line 371  int apply_article_op_log_from_db(int op_ Line 375  int apply_article_op_log_from_db(int op_
375          ARTICLE *p_article;          ARTICLE *p_article;
376          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
377          SECTION_LIST *p_section_dest;          SECTION_LIST *p_section_dest;
378            char sub_ip[IP_ADDR_LEN];
379          int32_t last_sid = 0;          int32_t last_sid = 0;
380          int32_t sid_dest;          int32_t sid_dest;
381          int op_count = 0;          int op_count = 0;
# Line 445  int apply_article_op_log_from_db(int op_ Line 450  int apply_article_op_log_from_db(int op_
450                          break;                          break;
451                  case 'D': // Delete article                  case 'D': // Delete article
452                  case 'X': // Delete article by Admin                  case 'X': // Delete article by Admin
453                          p_article->visible = 0;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
                         if (p_article->tid == 0)  
454                          {                          {
455                                  // 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]));
456                                  do                          }
457                                  {                          if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
458                                          p_article = p_article->p_topic_next;                          {
459                                          p_article->visible = 0;                                  log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
                                 } while (p_article->tid != 0);  
460                          }                          }
461                          break;                          break;
462                  case 'S': // Restore article                  case 'S': // Restore article
463                          p_article->visible = 1;                          if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
464                            {
465                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
466                            }
467                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
468                            {
469                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
470                            }
471                          break;                          break;
472                  case 'L': // Lock article                  case 'L': // Lock article
473                          p_article->lock = 1;                          p_article->lock = 1;
# Line 467  int apply_article_op_log_from_db(int op_ Line 477  int apply_article_op_log_from_db(int op_
477                          break;                          break;
478                  case 'M': // Modify article                  case 'M': // Modify article
479                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
480                                           "SELECT bbs.CID, bbs_content.content "                                           "SELECT bbs.CID, sub_ip, bbs_content.content "
481                                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "                                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
482                                           "WHERE bbs.AID = %d",                                           "WHERE bbs.AID = %d",
483                                           p_article->aid);                                           p_article->aid);
# Line 478  int apply_article_op_log_from_db(int op_ Line 488  int apply_article_op_log_from_db(int op_
488                                  ret = -3;                                  ret = -3;
489                                  break;                                  break;
490                          }                          }
491                          if ((rs2 = mysql_store_result(db)) == NULL)                          if ((rs2 = mysql_use_result(db)) == NULL)
492                          {                          {
493                                  log_error("Get article data failed\n");                                  log_error("Get article data failed\n");
494                                  ret = -3;                                  ret = -3;
# Line 488  int apply_article_op_log_from_db(int op_ Line 498  int apply_article_op_log_from_db(int op_
498                          {                          {
499                                  p_article->cid = atoi(row2[0]);                                  p_article->cid = atoi(row2[0]);
500    
501                                  if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, row2[1]) < 0)                                  strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
502                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
503                                    ip_mask(sub_ip, 1, '*');
504    
505                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
506                                  {                                  {
507                                          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);
508                                          ret = -4;                                          ret = -4;
# Line 500  int apply_article_op_log_from_db(int op_ Line 514  int apply_article_op_log_from_db(int op_
514                                  ret = -4;                                  ret = -4;
515                          }                          }
516                          mysql_free_result(rs2);                          mysql_free_result(rs2);
517    
518                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
519                          break;                          break;
520                  case 'T': // Move article                  case 'T': // Move article
521                          snprintf(sql, sizeof(sql),                          snprintf(sql, sizeof(sql),
# Line 626  int section_list_loader_launch(void) Line 642  int section_list_loader_launch(void)
642          {          {
643                  SYS_child_process_count++;                  SYS_child_process_count++;
644                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
645                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
646                  return 0;                  return 0;
647          }          }
648          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 682  int section_list_loader_launch(void) Line 698  int section_list_loader_launch(void)
698                  load_count = article_block_article_count() - article_count;                  load_count = article_block_article_count() - article_count;
699                  if (load_count > 0)                  if (load_count > 0)
700                  {                  {
701                          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());
702                  }                  }
703    
704                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 708  int section_list_loader_launch(void) Line 724  int section_list_loader_launch(void)
724    
725                  if (last_article_op_log_mid > last_mid)                  if (last_article_op_log_mid > last_mid)
726                  {                  {
727                          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);
728                  }                  }
729    
730                  if (SYS_section_list_reload)                  if (SYS_section_list_reload)
# Line 716  int section_list_loader_launch(void) Line 732  int section_list_loader_launch(void)
732                          continue;                          continue;
733                  }                  }
734    
735                  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++)
736                  {                  {
737                          sleep(1);                          sleep(1);
738                  }                  }
# Line 729  int section_list_loader_launch(void) Line 745  int section_list_loader_launch(void)
745          detach_article_block_shm();          detach_article_block_shm();
746          detach_trie_dict_shm();          detach_trie_dict_shm();
747    
748          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
749          log_end();          log_end();
750    
751          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 819  int query_section_articles(SECTION_LIST Line 835  int query_section_articles(SECTION_LIST
835    
836          return ret;          return ret;
837  }  }
838    
839    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,
840                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
841    {
842            const ARTICLE *p_article = NULL;
843            ARTICLE *p_tmp;
844            int32_t aid = 0;
845            int page_id;
846            int offset;
847            int ret = 0;
848            int i;
849    
850            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
851            {
852                    log_error("locate_article_in_section() NULL pointer error\n");
853                    return -1;
854            }
855    
856            // acquire lock of section
857            if ((ret = section_list_rd_lock(p_section)) < 0)
858            {
859                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
860                    return -2;
861            }
862    
863            if (direction == 0)
864            {
865                    aid = p_article_cur->aid;
866            }
867            else if (direction == 1)
868            {
869                    p_article = p_article_cur;
870                    do
871                    {
872                            p_article = p_article->p_topic_next;
873                    } while (p_article != p_article_cur && p_article->visible == 0);
874    
875                    aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0);
876            }
877            else if (direction == -1)
878            {
879                    p_article = p_article_cur;
880                    do
881                    {
882                            p_article = p_article->p_topic_prior;
883                    } while (p_article != p_article_cur && p_article->visible == 0);
884    
885                    aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0);
886            }
887    
888            p_article = NULL;
889    
890            if (aid > 0)
891            {
892                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
893                    if (p_article != NULL)
894                    {
895                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
896    
897                            p_article = p_section->p_page_first_article[page_id];
898                            for (i = 0; i < *p_article_count;)
899                            {
900                                    if (p_article->visible)
901                                    {
902                                            if (p_article->aid == aid)
903                                            {
904                                                    *p_page_id = page_id;
905                                                    *p_visible_offset = i;
906                                                    break;
907                                            }
908                                            i++;
909                                            if (i >= *p_article_count)
910                                            {
911                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
912                                                    p_article = NULL;
913                                            }
914                                    }
915                                    p_article = p_article->p_next;
916                            }
917                    }
918            }
919    
920            // release lock of section
921            if (section_list_rd_unlock(p_section) < 0)
922            {
923                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
924                    ret = -2;
925            }
926    
927            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
928    }


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

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