/[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.31 by sysadm, Thu Jun 5 11:12:11 2025 UTC Revision 1.61 by sysadm, Tue Nov 18 15:15:18 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                             section_list.c  -  description  /*
3                                                           -------------------   * section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
13  #include "log.h"  #include "log.h"
14    #include "section_list.h"
15  #include "trie_dict.h"  #include "trie_dict.h"
16    #include "user_list.h"
17    #include <errno.h>
18    #include <signal.h>
19  #include <stdio.h>  #include <stdio.h>
20    #include <stdlib.h>
21  #include <string.h>  #include <string.h>
 #include <signal.h>  
22  #include <unistd.h>  #include <unistd.h>
23  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
24  #include <sys/param.h>  #include <sys/param.h>
25  #include <sys/sem.h>  #include <sys/sem.h>
26  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
27    
28  #ifdef _SEM_SEMUN_UNDEFINED  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__)
29  union semun  union semun
30  {  {
31          int val;                           /* Value for SETVAL */          int val;                           /* Value for SETVAL */
# Line 39  union semun Line 34  union semun
34          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
35                                                            (Linux-specific) */                                                            (Linux-specific) */
36  };  };
37  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #if defined(_SEM_SEMUN_UNDEFINED)
38    
39  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second  enum _section_list_constant_t
40  #define SECTION_TRY_LOCK_TIMES 10  {
41            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
42            SECTION_TRY_LOCK_TIMES = 10,
43    
44  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate          ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
45  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)          ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
46  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)          ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
47    
48  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
49    
50  #define SID_STR_LEN 5 // 32-bit + NULL          SID_STR_LEN = 5, // 32-bit + NULL
51    };
52    
53  struct article_block_t  struct article_block_t
54  {  {
55          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
56          int article_count;          int article_count;
57          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
58  };  };
# Line 77  typedef struct article_block_pool_t ARTI Line 75  typedef struct article_block_pool_t ARTI
75    
76  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77    
78  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;  
79    
80  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
81  {  {
# Line 228  void article_block_cleanup(void) Line 215  void article_block_cleanup(void)
215                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216          }          }
217    
218          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
219          {          {
220                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221          }          }
# Line 238  void article_block_cleanup(void) Line 225  void article_block_cleanup(void)
225    
226  int set_article_block_shm_readonly(void)  int set_article_block_shm_readonly(void)
227  {  {
228    #ifndef __CYGWIN__
229          int shmid;          int shmid;
230          void *p_shm;          void *p_shm;
231          int i;          int i;
# Line 260  int set_article_block_shm_readonly(void) Line 248  int set_article_block_shm_readonly(void)
248                          return -2;                          return -2;
249                  }                  }
250          }          }
251    #endif
252    
253          return 0;          return 0;
254  }  }
# Line 355  ARTICLE *article_block_find_by_aid(int32 Line 344  ARTICLE *article_block_find_by_aid(int32
344          }          }
345    
346          left = 0;          left = 0;
347          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
348    
349          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of blocks[left], tail aid of blocks[right] ]
350          while (left < right - 1)          while (left < right)
351          {          {
352                  // get block offset no less than mid value of left and right block offsets                  // get block offset no less than mid value of left and right block offsets
353                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_article_block_pool->block_count)  
                 {  
                         log_error("block(mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
354    
355                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
356                  {                  {
357                          right = mid;                          right = mid - 1;
358                  }                  }
359                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
360                  {                  {
361                          left = mid;                          left = mid;
362                  }                  }
# Line 417  ARTICLE *article_block_find_by_index(int Line 400  ARTICLE *article_block_find_by_index(int
400                  return NULL;                  return NULL;
401          }          }
402    
403          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
404          {          {
405                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);                  log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
406                  return NULL;                  return NULL;
407          }          }
408    
409          p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];          p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block];
410    
411          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
412          {          {
413                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);                  log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
414                  return NULL;                  return NULL;
415          }          }
416    
417          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
418  }  }
419    
420  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 549  void section_list_cleanup(void) Line 532  void section_list_cleanup(void)
532                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
533          }          }
534    
535          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
536          {          {
537                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
538          }          }
# Line 559  void section_list_cleanup(void) Line 542  void section_list_cleanup(void)
542    
543  int set_section_list_shm_readonly(void)  int set_section_list_shm_readonly(void)
544  {  {
545    #ifndef __CYGWIN__
546          int shmid;          int shmid;
547          void *p_shm;          void *p_shm;
548    
# Line 579  int set_section_list_shm_readonly(void) Line 563  int set_section_list_shm_readonly(void)
563          }          }
564    
565          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
566    #endif
567    
568          return 0;          return 0;
569  }  }
# Line 632  SECTION_LIST *section_list_create(int32_ Line 617  SECTION_LIST *section_list_create(int32_
617          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;
618    
619          p_section->sid = sid;          p_section->sid = sid;
620            p_section->ex_menu_tm = 0;
621    
622          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
623          p_section->sname[sizeof(p_section->sname) - 1] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
# Line 661  SECTION_LIST *section_list_create(int32_ Line 647  SECTION_LIST *section_list_create(int32_
647          return p_section;          return p_section;
648  }  }
649    
650    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
651    {
652            int64_t index;
653    
654            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
655            {
656                    log_error("NULL pointer error\n");
657                    return -1;
658            }
659    
660            index = get_section_index(p_section);
661    
662            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
663            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
664    
665            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
666            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
667    
668            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
669            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
670    
671            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
672            {
673                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
674                    return -2;
675            }
676    
677            return 0;
678    }
679    
680  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
681  {  {
682          p_section->article_count = 0;          p_section->article_count = 0;
# Line 672  void section_list_reset_articles(SECTION Line 688  void section_list_reset_articles(SECTION
688    
689          p_section->page_count = 0;          p_section->page_count = 0;
690          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
691    
692            p_section->ontop_article_count = 0;
693  }  }
694    
695  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
# Line 737  int section_list_append_article(SECTION_ Line 755  int section_list_append_article(SECTION_
755    
756          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
757          {          {
758                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
759                  return -1;                  return -1;
760          }          }
761    
# Line 760  int section_list_append_article(SECTION_ Line 778  int section_list_append_article(SECTION_
778          }          }
779    
780          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
781                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block)
782          {          {
783                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
784                  {                  {
# Line 770  int section_list_append_article(SECTION_ Line 788  int section_list_append_article(SECTION_
788    
789                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
790                  {                  {
791                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid;
792                  }                  }
793    
794                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
# Line 849  int section_list_append_article(SECTION_ Line 867  int section_list_append_article(SECTION_
867          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
868    
869          // Update page          // Update page
870          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||          if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) ||
871                  p_section->article_count == 1)                  p_section->page_count == 0)
872          {          {
873                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
874                  p_section->page_count++;                  p_section->page_count++;
# Line 862  int section_list_append_article(SECTION_ Line 880  int section_list_append_article(SECTION_
880                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
881          }          }
882    
883            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
884            {
885                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
886                                      p_section->sid, p_article->aid);
887                    return -5;
888            }
889    
890          return 0;          return 0;
891  }  }
892    
# Line 873  int section_list_set_article_visible(SEC Line 898  int section_list_set_article_visible(SEC
898    
899          if (p_section == NULL)          if (p_section == NULL)
900          {          {
901                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
902                  return -2;                  return -1;
903          }          }
904    
905          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 885  int section_list_set_article_visible(SEC Line 910  int section_list_set_article_visible(SEC
910    
911          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
912          {          {
913                  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);
914                  return -2;                  return -2;
915          }          }
916    
# Line 898  int section_list_set_article_visible(SEC Line 923  int section_list_set_article_visible(SEC
923          {          {
924                  p_section->visible_article_count--;                  p_section->visible_article_count--;
925    
926                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
927                    {
928                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
929                    }
930    
931                  if (p_article->tid == 0)                  if (p_article->tid == 0)
932                  {                  {
933                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 916  int section_list_set_article_visible(SEC Line 946  int section_list_set_article_visible(SEC
946                                          p_reply->visible = 0;                                          p_reply->visible = 0;
947                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
948                                          affected_count++;                                          affected_count++;
949    
950                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
951                                            {
952                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
953                                            }
954                                  }                                  }
955                          }                          }
956                  }                  }
# Line 928  int section_list_set_article_visible(SEC Line 963  int section_list_set_article_visible(SEC
963                  {                  {
964                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
965                  }                  }
966    
967                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
968                    {
969                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
970                    }
971          }          }
972    
973          p_article->visible = visible;          p_article->visible = visible;
# Line 936  int section_list_set_article_visible(SEC Line 976  int section_list_set_article_visible(SEC
976          return affected_count;          return affected_count;
977  }  }
978    
979    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
980    {
981            int i;
982    
983            if (p_section == NULL || p_article == NULL)
984            {
985                    log_error("NULL pointer error\n");
986                    return -1;
987            }
988    
989            if (p_section->sid != p_article->sid)
990            {
991                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
992                    return -2;
993            }
994    
995            if (p_article->ontop)
996            {
997                    for (i = 0; i < p_section->ontop_article_count; i++)
998                    {
999                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1000                            {
1001                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
1002                                    return 0;
1003                            }
1004                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1005                            {
1006                                    break;
1007                            }
1008                    }
1009    
1010                    // Remove the oldest one if the array of ontop articles is full
1011                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1012                    {
1013                            if (i == 0) // p_article is the oldest one
1014                            {
1015                                    return 0;
1016                            }
1017                            memmove((void *)(p_section->p_ontop_articles),
1018                                            (void *)(p_section->p_ontop_articles + 1),
1019                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1020                            p_section->ontop_article_count--;
1021                            i--;
1022                    }
1023                    else
1024                    {
1025                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1026                                            (void *)(p_section->p_ontop_articles + i),
1027                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1028                    }
1029    
1030                    p_section->p_ontop_articles[i] = p_article;
1031                    p_section->ontop_article_count++;
1032    
1033                    // TODO: debug
1034            }
1035            else // ontop == 0
1036            {
1037                    for (i = 0; i < p_section->ontop_article_count; i++)
1038                    {
1039                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1040                            {
1041                                    break;
1042                            }
1043                    }
1044                    if (i == p_section->ontop_article_count) // not found
1045                    {
1046                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1047                            return 0;
1048                    }
1049    
1050                    memmove((void *)(p_section->p_ontop_articles + i),
1051                                    (void *)(p_section->p_ontop_articles + i + 1),
1052                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1053                    p_section->ontop_article_count--;
1054            }
1055    
1056            return 0;
1057    }
1058    
1059    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1060    {
1061            int page_count;
1062    
1063            if (p_section == NULL)
1064            {
1065                    log_error("NULL pointer error\n");
1066                    return -1;
1067            }
1068    
1069            page_count = p_section->page_count - 1 +
1070                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1071                                             BBS_article_limit_per_page;
1072    
1073            if (page_count < 0)
1074            {
1075                    page_count = 0;
1076            }
1077    
1078            return page_count;
1079    }
1080    
1081    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1082    {
1083            if (p_section == NULL)
1084            {
1085                    log_error("NULL pointer error\n");
1086                    return -1;
1087            }
1088    
1089            if (page_id < p_section->page_count - 1)
1090            {
1091                    return BBS_article_limit_per_page;
1092            }
1093            else // if (page_id >= p_section->page_count - 1)
1094            {
1095                    return MIN(MAX(0,
1096                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1097                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1098                                       BBS_article_limit_per_page);
1099            }
1100    }
1101    
1102  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)
1103  {  {
1104          ARTICLE *p_article;          ARTICLE *p_article;
# Line 949  ARTICLE *section_list_find_article_with_ Line 1112  ARTICLE *section_list_find_article_with_
1112    
1113          if (p_section == NULL)          if (p_section == NULL)
1114          {          {
1115                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1116                  return NULL;                  return NULL;
1117          }          }
1118    
# Line 961  ARTICLE *section_list_find_article_with_ Line 1124  ARTICLE *section_list_find_article_with_
1124          }          }
1125    
1126          left = 0;          left = 0;
1127          right = p_section->page_count;          right = p_section->page_count - 1;
1128    
1129          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right] ]
1130          while (left < right - 1)          while (left < right)
1131          {          {
1132                  // get page id no less than mid value of left page id and right page id                  // get page id no less than mid value of left page id and right page id
1133                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_section->page_count)  
                 {  
                         log_error("page id (mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
1134    
1135                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1136                  {                  {
1137                          right = mid;                          right = mid - 1;
1138                  }                  }
1139                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1140                  {                  {
1141                          left = mid;                          left = mid;
1142                  }                  }
# Line 1041  int section_list_calculate_page(SECTION_ Line 1198  int section_list_calculate_page(SECTION_
1198    
1199          if (p_section == NULL)          if (p_section == NULL)
1200          {          {
1201                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1202                  return -1;                  return -1;
1203          }          }
1204    
# Line 1130  int section_list_calculate_page(SECTION_ Line 1287  int section_list_calculate_page(SECTION_
1287          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1288    
1289          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1290          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1291                                                                                                              ? visible_article_count
1292                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1293    
1294          return 0;          return 0;
1295  }  }
# Line 1152  int article_block_article_count(void) Line 1311  int article_block_article_count(void)
1311                  return -1;                  return -1;
1312          }          }
1313    
1314          ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +          ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1315                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;                    p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1316    
1317          return ret;          return ret;
# Line 1200  int section_list_move_topic(SECTION_LIST Line 1359  int section_list_move_topic(SECTION_LIST
1359    
1360          if (p_section_src == NULL || 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;                  return -1;
1364          }          }
1365    
# Line 1413  int get_section_index(SECTION_LIST *p_se Line 1572  int get_section_index(SECTION_LIST *p_se
1572          return index;          return index;
1573  }  }
1574    
1575    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1576    {
1577            if (p_section == NULL)
1578            {
1579                    log_error("NULL pointer error\n");
1580                    return -1;
1581            }
1582    
1583            if (section_list_rd_lock(p_section) < 0)
1584            {
1585                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1586                    return -2;
1587            }
1588    
1589            if (sname != NULL)
1590            {
1591                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1592            }
1593            if (stitle != NULL)
1594            {
1595                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1596            }
1597            if (master_list != NULL)
1598            {
1599                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1600            }
1601    
1602            // release lock of section
1603            if (section_list_rd_unlock(p_section) < 0)
1604            {
1605                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1606                    return -2;
1607            }
1608    
1609            return 0;
1610    }
1611    
1612  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1613  {  {
1614          int index;          int index;
1615          struct sembuf sops[4];          struct sembuf sops[4];
1616    #ifndef __CYGWIN__
1617          struct timespec timeout;          struct timespec timeout;
1618    #endif
1619          int ret;          int ret;
1620    
1621          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1448  int section_list_try_rd_lock(SECTION_LIS Line 1646  int section_list_try_rd_lock(SECTION_LIS
1646                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1647          }          }
1648    
1649    #ifdef __CYGWIN__
1650            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4));
1651    #else
1652          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1653          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1654    
1655          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1656    #endif
1657          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1658          {          {
1659                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);                  log_error("semop(index = %d, lock read) error %d\n", index, errno);
1660          }          }
1661    
1662          return ret;          return ret;
# Line 1464  int section_list_try_rw_lock(SECTION_LIS Line 1666  int section_list_try_rw_lock(SECTION_LIS
1666  {  {
1667          int index;          int index;
1668          struct sembuf sops[3];          struct sembuf sops[3];
1669    #ifndef __CYGWIN__
1670          struct timespec timeout;          struct timespec timeout;
1671    #endif
1672          int ret;          int ret;
1673    
1674          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1485  int section_list_try_rw_lock(SECTION_LIS Line 1689  int section_list_try_rw_lock(SECTION_LIS
1689          sops[2].sem_op = 0;                                                        // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1690          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
1691    
1692    #ifdef __CYGWIN__
1693            ret = semop(p_section_list_pool->semid, sops, 3);
1694    #else
1695          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1696          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1697    
1698          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1699    #endif
1700          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1701          {          {
1702                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);                  log_error("semop(index = %d, lock write) error %d\n", index, errno);
1703          }          }
1704    
1705          return ret;          return ret;
# Line 1572  int section_list_rd_lock(SECTION_LIST *p Line 1780  int section_list_rd_lock(SECTION_LIST *p
1780                          timer++;                          timer++;
1781                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1782                          {                          {
1783                                  log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);                                  log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1784                          }                          }
1785                  }                  }
1786                  else // failed                  else // failed
1787                  {                  {
1788                          log_error("section_list_rd_lock() failed on section %d\n", sid);                          log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1789                          break;                          break;
1790                  }                  }
1791          }          }
# Line 1603  int section_list_rw_lock(SECTION_LIST *p Line 1811  int section_list_rw_lock(SECTION_LIST *p
1811                          timer++;                          timer++;
1812                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1813                          {                          {
1814                                  log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);                                  log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1815                          }                          }
1816                  }                  }
1817                  else // failed                  else // failed
1818                  {                  {
1819                          log_error("acquire_section_rw_lock() failed on section %d\n", sid);                          log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1820                          break;                          break;
1821                  }                  }
1822          }          }


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

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