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

Diff of /lbbs/src/section_list.c

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

Revision 1.22 by sysadm, Sun May 25 23:47:36 2025 UTC Revision 1.46 by sysadm, Thu Oct 23 05:54:16 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
17  #include "log.h"  #include "log.h"
18    #include "section_list.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
20    #include "user_list.h"
21    #include <errno.h>
22    #include <signal.h>
23  #include <stdio.h>  #include <stdio.h>
24    #include <stdlib.h>
25  #include <string.h>  #include <string.h>
 #include <signal.h>  
26  #include <unistd.h>  #include <unistd.h>
27  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
28  #include <sys/param.h>  #include <sys/param.h>
29  #include <sys/sem.h>  #include <sys/sem.h>
30  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
31    
32  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
33  union semun  union semun
# Line 41  union semun Line 40  union semun
40  };  };
41  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
42    
43  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
44  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define SECTION_TRY_LOCK_TIMES 10
45    
46    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
47    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
48  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
49    
50  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections
51    
52  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
53    
# Line 74  typedef struct article_block_pool_t ARTI Line 76  typedef struct article_block_pool_t ARTI
76    
77  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
78    
79  struct section_list_pool_t  SECTION_LIST_POOL *p_section_list_pool = NULL;
 {  
         int shmid;  
         SECTION_LIST sections[BBS_max_section];  
         int section_count;  
         int semid;  
         TRIE_NODE *p_trie_dict_section_by_name;  
         TRIE_NODE *p_trie_dict_section_by_sid;  
 };  
 typedef struct section_list_pool_t SECTION_LIST_POOL;  
   
 static SECTION_LIST_POOL *p_section_list_pool = NULL;  
80    
81  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
82  {  {
# Line 105  int article_block_init(const char *filen Line 96  int article_block_init(const char *filen
96                  return -1;                  return -1;
97          }          }
98    
99          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
100          {          {
101                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
102                  return -2;                  return -2;
# Line 233  void article_block_cleanup(void) Line 224  void article_block_cleanup(void)
224          p_article_block_pool = NULL;          p_article_block_pool = NULL;
225  }  }
226    
227    int set_article_block_shm_readonly(void)
228    {
229            int shmid;
230            void *p_shm;
231            int i;
232    
233            if (p_article_block_pool == NULL)
234            {
235                    log_error("article_block_pool not initialized\n");
236                    return -1;
237            }
238    
239            for (i = 0; i < p_article_block_pool->shm_count; i++)
240            {
241                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
242    
243                    // Remap shared memory in read-only mode
244                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
245                    if (p_shm == (void *)-1)
246                    {
247                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
248                            return -2;
249                    }
250            }
251    
252            return 0;
253    }
254    
255    int detach_article_block_shm(void)
256    {
257            int shmid;
258    
259            if (p_article_block_pool == NULL)
260            {
261                    return -1;
262            }
263    
264            for (int i = 0; i < p_article_block_pool->shm_count; i++)
265            {
266                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
267                    {
268                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
269                            return -2;
270                    }
271            }
272    
273            shmid = p_article_block_pool->shmid;
274    
275            if (shmdt(p_article_block_pool) == -1)
276            {
277                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
278                    return -3;
279            }
280    
281            p_article_block_pool = NULL;
282    
283            return 0;
284    }
285    
286  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
287  {  {
288          ARTICLE_BLOCK *p_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
# Line 337  ARTICLE *article_block_find_by_aid(int32 Line 387  ARTICLE *article_block_find_by_aid(int32
387                  }                  }
388          }          }
389    
390          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
391            {
392                    return NULL;
393            }
394    
395            return (p_block->articles + left); // found
396  }  }
397    
398  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 449  extern int section_list_init(const char Line 504  extern int section_list_init(const char
504          return 0;          return 0;
505  }  }
506    
507    void section_list_cleanup(void)
508    {
509            int shmid;
510    
511            if (p_section_list_pool == NULL)
512            {
513                    return;
514            }
515    
516            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
517            {
518                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
519                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
520            }
521    
522            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
523            {
524                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
525                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
526            }
527    
528            shmid = p_section_list_pool->shmid;
529    
530            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
531            {
532                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
533            }
534    
535            if (shmdt(p_section_list_pool) == -1)
536            {
537                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
538            }
539    
540            if (shmctl(shmid, IPC_RMID, NULL) == -1)
541            {
542                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
543            }
544    
545            p_section_list_pool = NULL;
546    }
547    
548    int set_section_list_shm_readonly(void)
549    {
550            int shmid;
551            void *p_shm;
552    
553            if (p_section_list_pool == NULL)
554            {
555                    log_error("p_section_list_pool not initialized\n");
556                    return -1;
557            }
558    
559            shmid = p_section_list_pool->shmid;
560    
561            // Remap shared memory in read-only mode
562            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
563            if (p_shm == (void *)-1)
564            {
565                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
566                    return -3;
567            }
568    
569            p_section_list_pool = p_shm;
570    
571            return 0;
572    }
573    
574    int detach_section_list_shm(void)
575    {
576            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
577            {
578                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
579                    return -1;
580            }
581    
582            p_section_list_pool = NULL;
583    
584            return 0;
585    }
586    
587  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
588  {  {
589          uint32_t u_sid;          uint32_t u_sid;
# Line 463  inline static void sid_to_str(int32_t si Line 598  inline static void sid_to_str(int32_t si
598          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
599  }  }
600    
601  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
602  {  {
603          SECTION_LIST *p_section;          SECTION_LIST *p_section;
604          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
# Line 485  SECTION_LIST *section_list_create(int32_ Line 620  SECTION_LIST *section_list_create(int32_
620          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
621    
622          p_section->sid = sid;          p_section->sid = sid;
623            p_section->ex_menu_tm = 0;
624    
625          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
626          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
627    
628          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
629          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
630    
631          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
632          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
633    
634          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
635          {          {
# Line 514  SECTION_LIST *section_list_create(int32_ Line 650  SECTION_LIST *section_list_create(int32_
650          return p_section;          return p_section;
651  }  }
652    
653  void section_list_reset_articles(SECTION_LIST *p_section)  int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
 {  
         p_section->article_count = 0;  
         p_section->topic_count = 0;  
         p_section->visible_article_count = 0;  
         p_section->visible_topic_count = 0;  
         p_section->p_article_head = NULL;  
         p_section->p_article_tail = NULL;  
   
         p_section->page_count = 0;  
         p_section->last_page_visible_article_count = 0;  
 }  
   
 void section_list_cleanup(void)  
654  {  {
655          int shmid;          int64_t index;
656    
657          if (p_section_list_pool == NULL)          if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
658          {          {
659                  return;                  log_error("NULL pointer error\n");
660                    return -1;
661          }          }
662    
663          if (p_section_list_pool->p_trie_dict_section_by_name != NULL)          index = get_section_index(p_section);
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);  
                 p_section_list_pool->p_trie_dict_section_by_name = NULL;  
         }  
664    
665          if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
666          {          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);  
                 p_section_list_pool->p_trie_dict_section_by_sid = NULL;  
         }  
667    
668          if (p_section_list_pool != NULL)          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
669            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
670    
671            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
672            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
673    
674            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
675          {          {
676                  if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)                  log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
677                  {                  return -2;
678                          log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);          }
                 }  
679    
680                  shmid = p_section_list_pool->shmid;          return 0;
681    }
682    
683                  if (shmdt(p_section_list_pool) == -1)  void section_list_reset_articles(SECTION_LIST *p_section)
684                  {  {
685                          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);          p_section->article_count = 0;
686                  }          p_section->topic_count = 0;
687            p_section->visible_article_count = 0;
688            p_section->visible_topic_count = 0;
689            p_section->p_article_head = NULL;
690            p_section->p_article_tail = NULL;
691    
692                  if (shmctl(shmid, IPC_RMID, NULL) == -1)          p_section->page_count = 0;
693                  {          p_section->last_page_visible_article_count = 0;
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
694    
695                  p_section_list_pool = NULL;          p_section->ontop_article_count = 0;
         }  
696  }  }
697    
698  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
699  {  {
700          int64_t index;          int64_t index;
701            int ret;
702    
703          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
704          {          {
# Line 581  SECTION_LIST *section_list_find_by_name( Line 706  SECTION_LIST *section_list_find_by_name(
706                  return NULL;                  return NULL;
707          }          }
708    
709          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
710            if (ret < 0)
711          {          {
712                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
713                  return NULL;                  return NULL;
714          }          }
715            else if (ret == 0)
716            {
717                    return NULL;
718            }
719    
720          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
721  }  }
# Line 593  SECTION_LIST *section_list_find_by_name( Line 723  SECTION_LIST *section_list_find_by_name(
723  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
724  {  {
725          int64_t index;          int64_t index;
726            int ret;
727          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
728    
729          if (p_section_list_pool == NULL)          if (p_section_list_pool == NULL)
# Line 603  SECTION_LIST *section_list_find_by_sid(i Line 734  SECTION_LIST *section_list_find_by_sid(i
734    
735          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
736    
737          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
738            if (ret < 0)
739          {          {
740                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
741                  return NULL;                  return NULL;
742          }          }
743            else if (ret == 0)
744            {
745                    return NULL;
746            }
747    
748          return (p_section_list_pool->sections + index);          return (p_section_list_pool->sections + index);
749  }  }
# Line 622  int section_list_append_article(SECTION_ Line 758  int section_list_append_article(SECTION_
758    
759          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
760          {          {
761                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
762                  return -1;                  return -1;
763          }          }
764    
# Line 747  int section_list_append_article(SECTION_ Line 883  int section_list_append_article(SECTION_
883                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
884          }          }
885    
886            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
887            {
888                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
889                                      p_section->sid, p_article->aid);
890                    return -5;
891            }
892    
893          return 0;          return 0;
894  }  }
895    
# Line 758  int section_list_set_article_visible(SEC Line 901  int section_list_set_article_visible(SEC
901    
902          if (p_section == NULL)          if (p_section == NULL)
903          {          {
904                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
905                  return -2;                  return -1;
906          }          }
907    
908          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 770  int section_list_set_article_visible(SEC Line 913  int section_list_set_article_visible(SEC
913    
914          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
915          {          {
916                  log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
917                  return -2;                  return -2;
918          }          }
919    
# Line 783  int section_list_set_article_visible(SEC Line 926  int section_list_set_article_visible(SEC
926          {          {
927                  p_section->visible_article_count--;                  p_section->visible_article_count--;
928    
929                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
930                    {
931                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
932                    }
933    
934                  if (p_article->tid == 0)                  if (p_article->tid == 0)
935                  {                  {
936                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 801  int section_list_set_article_visible(SEC Line 949  int section_list_set_article_visible(SEC
949                                          p_reply->visible = 0;                                          p_reply->visible = 0;
950                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
951                                          affected_count++;                                          affected_count++;
952    
953                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
954                                            {
955                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
956                                            }
957                                  }                                  }
958                          }                          }
959                  }                  }
# Line 813  int section_list_set_article_visible(SEC Line 966  int section_list_set_article_visible(SEC
966                  {                  {
967                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
968                  }                  }
969    
970                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
971                    {
972                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
973                    }
974          }          }
975    
976          p_article->visible = visible;          p_article->visible = visible;
# Line 821  int section_list_set_article_visible(SEC Line 979  int section_list_set_article_visible(SEC
979          return affected_count;          return affected_count;
980  }  }
981    
982    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
983    {
984            int i;
985    
986            if (p_section == NULL || p_article == NULL)
987            {
988                    log_error("NULL pointer error\n");
989                    return -1;
990            }
991    
992            if (p_section->sid != p_article->sid)
993            {
994                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
995                    return -2;
996            }
997    
998            if (p_article->ontop)
999            {
1000                    for (i = 0; i < p_section->ontop_article_count; i++)
1001                    {
1002                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1003                            {
1004                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
1005                                    return 0;
1006                            }
1007                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1008                            {
1009                                    break;
1010                            }
1011                    }
1012    
1013                    // Remove the oldest one if the array of ontop articles is full
1014                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1015                    {
1016                            if (i == 0) // p_article is the oldest one
1017                            {
1018                                    return 0;
1019                            }
1020                            memmove((void *)(p_section->p_ontop_articles),
1021                                            (void *)(p_section->p_ontop_articles + 1),
1022                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1023                            p_section->ontop_article_count--;
1024                            i--;
1025                    }
1026                    else
1027                    {
1028                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1029                                            (void *)(p_section->p_ontop_articles + i),
1030                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1031                    }
1032    
1033                    p_section->p_ontop_articles[i] = p_article;
1034                    p_section->ontop_article_count++;
1035    
1036                    // TODO: debug
1037            }
1038            else // ontop == 0
1039            {
1040                    for (i = 0; i < p_section->ontop_article_count; i++)
1041                    {
1042                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1043                            {
1044                                    break;
1045                            }
1046                    }
1047                    if (i == p_section->ontop_article_count) // not found
1048                    {
1049                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1050                            return 0;
1051                    }
1052    
1053                    memmove((void *)(p_section->p_ontop_articles + i),
1054                                    (void *)(p_section->p_ontop_articles + i + 1),
1055                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1056                    p_section->ontop_article_count--;
1057            }
1058    
1059            return 0;
1060    }
1061    
1062    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1063    {
1064            int page_count;
1065    
1066            if (p_section == NULL)
1067            {
1068                    log_error("NULL pointer error\n");
1069                    return -1;
1070            }
1071    
1072            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1073                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1074                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1075    
1076            return page_count;
1077    }
1078    
1079    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1080    {
1081            if (p_section == NULL)
1082            {
1083                    log_error("NULL pointer error\n");
1084                    return -1;
1085            }
1086    
1087            if (page_id < p_section->page_count - 1)
1088            {
1089                    return BBS_article_limit_per_page;
1090            }
1091            else // if (page_id >= p_section->page_count - 1)
1092            {
1093                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1094                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1095            }
1096    }
1097    
1098  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
1099  {  {
1100          ARTICLE *p_article;          ARTICLE *p_article;
# Line 834  ARTICLE *section_list_find_article_with_ Line 1108  ARTICLE *section_list_find_article_with_
1108    
1109          if (p_section == NULL)          if (p_section == NULL)
1110          {          {
1111                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1112                  return NULL;                  return NULL;
1113          }          }
1114    
# Line 926  int section_list_calculate_page(SECTION_ Line 1200  int section_list_calculate_page(SECTION_
1200    
1201          if (p_section == NULL)          if (p_section == NULL)
1202          {          {
1203                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1204                  return -1;                  return -1;
1205          }          }
1206    
# Line 1020  int section_list_calculate_page(SECTION_ Line 1294  int section_list_calculate_page(SECTION_
1294          return 0;          return 0;
1295  }  }
1296    
1297    int32_t article_block_last_aid(void)
1298    {
1299            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1300            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1301    
1302            return last_aid;
1303    }
1304    
1305    int article_block_article_count(void)
1306    {
1307            int ret;
1308    
1309            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1310            {
1311                    return -1;
1312            }
1313    
1314            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1315                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1316    
1317            return ret;
1318    }
1319    
1320  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1321  {  {
1322          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1060  int section_list_move_topic(SECTION_LIST Line 1357  int section_list_move_topic(SECTION_LIST
1357          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1358          int move_counter;          int move_counter;
1359    
1360          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1361          {          {
1362                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1363                    return -1;
1364            }
1365    
1366            if (p_section_src->sid == p_section_dest->sid)
1367            {
1368                    log_error("section_list_move_topic() src and dest section are the same\n");
1369                  return -1;                  return -1;
1370          }          }
1371    
1372          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1373          {          {
1374                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1375                  return -2;                  return -2;
1376          }          }
1377    
# Line 1109  int section_list_move_topic(SECTION_LIST Line 1412  int section_list_move_topic(SECTION_LIST
1412          {          {
1413                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1414                  {                  {
1415                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1416                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1417                          return -2;                          p_article = p_article->p_topic_next;
1418                            continue;
1419                  }                  }
1420    
1421                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1137  int section_list_move_topic(SECTION_LIST Line 1441  int section_list_move_topic(SECTION_LIST
1441    
1442                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1443                  {                  {
1444                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1445                          return -4;                          p_article = p_article->p_topic_next;
1446                            continue;
1447                  }                  }
1448    
1449                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1216  int section_list_move_topic(SECTION_LIST Line 1521  int section_list_move_topic(SECTION_LIST
1521    
1522          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1523          {          {
1524                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1525                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1526          }          }
1527    
# Line 1406  int section_list_rw_unlock(SECTION_LIST Line 1711  int section_list_rw_unlock(SECTION_LIST
1711          }          }
1712    
1713          return ret;          return ret;
1714    }
1715    
1716    int section_list_rd_lock(SECTION_LIST *p_section)
1717    {
1718            int timer = 0;
1719            int sid = (p_section == NULL ? 0 : p_section->sid);
1720            int ret = -1;
1721    
1722            while (!SYS_server_exit)
1723            {
1724                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1725                    if (ret == 0) // success
1726                    {
1727                            break;
1728                    }
1729                    else if (errno == EAGAIN || errno == EINTR) // retry
1730                    {
1731                            timer++;
1732                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1733                            {
1734                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1735                            }
1736                    }
1737                    else // failed
1738                    {
1739                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1740                            break;
1741                    }
1742            }
1743    
1744            return ret;
1745    }
1746    
1747    int section_list_rw_lock(SECTION_LIST *p_section)
1748    {
1749            int timer = 0;
1750            int sid = (p_section == NULL ? 0 : p_section->sid);
1751            int ret = -1;
1752    
1753            while (!SYS_server_exit)
1754            {
1755                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1756                    if (ret == 0) // success
1757                    {
1758                            break;
1759                    }
1760                    else if (errno == EAGAIN || errno == EINTR) // retry
1761                    {
1762                            timer++;
1763                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1764                            {
1765                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1766                            }
1767                    }
1768                    else // failed
1769                    {
1770                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1771                            break;
1772                    }
1773            }
1774    
1775            return ret;
1776  }  }


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

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