/[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.29 by sysadm, Wed May 28 07:30:23 2025 UTC Revision 1.59 by sysadm, Mon Nov 17 12:16:48 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(__MSYS__) || defined(__MINGW32__)
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 move topic          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 253  int set_article_block_shm_readonly(void) Line 240  int set_article_block_shm_readonly(void)
240                  shmid = (p_article_block_pool->shm_pool + i)->shmid;                  shmid = (p_article_block_pool->shm_pool + i)->shmid;
241    
242                  // Remap shared memory in read-only mode                  // Remap shared memory in read-only mode
243    #if defined(__MSYS__) || defined(__MINGW32__)
244                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
245                    {
246                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
247                            return -2;
248                    }
249                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY);
250    #else
251                  p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);                  p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
252    #endif
253                  if (p_shm == (void *)-1)                  if (p_shm == (void *)-1)
254                  {                  {
255                          log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);                          log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
# Line 355  ARTICLE *article_block_find_by_aid(int32 Line 351  ARTICLE *article_block_find_by_aid(int32
351          }          }
352    
353          left = 0;          left = 0;
354          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
355    
356          // 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] ]
357          while (left < right - 1)          while (left < right)
358          {          {
359                  // 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
360                  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;  
                 }  
361    
362                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
363                  {                  {
364                          right = mid;                          right = mid - 1;
365                  }                  }
366                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
367                  {                  {
368                          left = mid;                          left = mid;
369                  }                  }
# Line 417  ARTICLE *article_block_find_by_index(int Line 407  ARTICLE *article_block_find_by_index(int
407                  return NULL;                  return NULL;
408          }          }
409    
410          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)
411          {          {
412                  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);
413                  return NULL;                  return NULL;
414          }          }
415    
416          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];
417    
418          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
419          {          {
420                  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);
421                  return NULL;                  return NULL;
422          }          }
423    
424          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
425  }  }
426    
427  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 549  void section_list_cleanup(void) Line 539  void section_list_cleanup(void)
539                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
540          }          }
541    
542          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
543          {          {
544                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
545          }          }
# Line 571  int set_section_list_shm_readonly(void) Line 561  int set_section_list_shm_readonly(void)
561          shmid = p_section_list_pool->shmid;          shmid = p_section_list_pool->shmid;
562    
563          // Remap shared memory in read-only mode          // Remap shared memory in read-only mode
564    #if defined(__MSYS__) || defined(__MINGW32__)
565            if (shmdt(p_section_list_pool) == -1)
566            {
567                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
568                    return -1;
569            }
570            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY);
571    #else
572          p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);          p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
573    #endif
574          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
575          {          {
576                  log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
# Line 610  inline static void sid_to_str(int32_t si Line 609  inline static void sid_to_str(int32_t si
609          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
610  }  }
611    
612  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)
613  {  {
614          SECTION_LIST *p_section;          SECTION_LIST *p_section;
615          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
# Line 632  SECTION_LIST *section_list_create(int32_ Line 631  SECTION_LIST *section_list_create(int32_
631          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;
632    
633          p_section->sid = sid;          p_section->sid = sid;
634            p_section->ex_menu_tm = 0;
635    
636          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
637          p_section->sname[sizeof(p_section->sname) - 1] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
# Line 639  SECTION_LIST *section_list_create(int32_ Line 639  SECTION_LIST *section_list_create(int32_
639          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
640          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
641    
642          strncpy(p_section->master_list, master_name, sizeof(p_section->master_list) - 1);          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
643          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
644    
645          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)
# Line 661  SECTION_LIST *section_list_create(int32_ Line 661  SECTION_LIST *section_list_create(int32_
661          return p_section;          return p_section;
662  }  }
663    
664    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
665    {
666            int64_t index;
667    
668            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
669            {
670                    log_error("NULL pointer error\n");
671                    return -1;
672            }
673    
674            index = get_section_index(p_section);
675    
676            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
677            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
678    
679            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
680            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
681    
682            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
683            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
684    
685            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
686            {
687                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
688                    return -2;
689            }
690    
691            return 0;
692    }
693    
694  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
695  {  {
696          p_section->article_count = 0;          p_section->article_count = 0;
# Line 672  void section_list_reset_articles(SECTION Line 702  void section_list_reset_articles(SECTION
702    
703          p_section->page_count = 0;          p_section->page_count = 0;
704          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
705    
706            p_section->ontop_article_count = 0;
707  }  }
708    
709  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 769  int section_list_append_article(SECTION_
769    
770          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
771          {          {
772                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
773                  return -1;                  return -1;
774          }          }
775    
# Line 760  int section_list_append_article(SECTION_ Line 792  int section_list_append_article(SECTION_
792          }          }
793    
794          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
795                  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)
796          {          {
797                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
798                  {                  {
# Line 770  int section_list_append_article(SECTION_ Line 802  int section_list_append_article(SECTION_
802    
803                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
804                  {                  {
805                          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;
806                  }                  }
807    
808                  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 881  int section_list_append_article(SECTION_
881          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
882    
883          // Update page          // Update page
884          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) ||
885                  p_section->article_count == 1)                  p_section->page_count == 0)
886          {          {
887                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
888                  p_section->page_count++;                  p_section->page_count++;
# Line 862  int section_list_append_article(SECTION_ Line 894  int section_list_append_article(SECTION_
894                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
895          }          }
896    
897            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
898            {
899                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
900                                      p_section->sid, p_article->aid);
901                    return -5;
902            }
903    
904          return 0;          return 0;
905  }  }
906    
# Line 873  int section_list_set_article_visible(SEC Line 912  int section_list_set_article_visible(SEC
912    
913          if (p_section == NULL)          if (p_section == NULL)
914          {          {
915                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
916                  return -2;                  return -1;
917          }          }
918    
919          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 924  int section_list_set_article_visible(SEC
924    
925          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
926          {          {
927                  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);
928                  return -2;                  return -2;
929          }          }
930    
# Line 898  int section_list_set_article_visible(SEC Line 937  int section_list_set_article_visible(SEC
937          {          {
938                  p_section->visible_article_count--;                  p_section->visible_article_count--;
939    
940                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
941                    {
942                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
943                    }
944    
945                  if (p_article->tid == 0)                  if (p_article->tid == 0)
946                  {                  {
947                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 916  int section_list_set_article_visible(SEC Line 960  int section_list_set_article_visible(SEC
960                                          p_reply->visible = 0;                                          p_reply->visible = 0;
961                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
962                                          affected_count++;                                          affected_count++;
963    
964                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
965                                            {
966                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
967                                            }
968                                  }                                  }
969                          }                          }
970                  }                  }
# Line 928  int section_list_set_article_visible(SEC Line 977  int section_list_set_article_visible(SEC
977                  {                  {
978                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
979                  }                  }
980    
981                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
982                    {
983                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
984                    }
985          }          }
986    
987          p_article->visible = visible;          p_article->visible = visible;
# Line 936  int section_list_set_article_visible(SEC Line 990  int section_list_set_article_visible(SEC
990          return affected_count;          return affected_count;
991  }  }
992    
993    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
994    {
995            int i;
996    
997            if (p_section == NULL || p_article == NULL)
998            {
999                    log_error("NULL pointer error\n");
1000                    return -1;
1001            }
1002    
1003            if (p_section->sid != p_article->sid)
1004            {
1005                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
1006                    return -2;
1007            }
1008    
1009            if (p_article->ontop)
1010            {
1011                    for (i = 0; i < p_section->ontop_article_count; i++)
1012                    {
1013                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1014                            {
1015                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
1016                                    return 0;
1017                            }
1018                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1019                            {
1020                                    break;
1021                            }
1022                    }
1023    
1024                    // Remove the oldest one if the array of ontop articles is full
1025                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1026                    {
1027                            if (i == 0) // p_article is the oldest one
1028                            {
1029                                    return 0;
1030                            }
1031                            memmove((void *)(p_section->p_ontop_articles),
1032                                            (void *)(p_section->p_ontop_articles + 1),
1033                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1034                            p_section->ontop_article_count--;
1035                            i--;
1036                    }
1037                    else
1038                    {
1039                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1040                                            (void *)(p_section->p_ontop_articles + i),
1041                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1042                    }
1043    
1044                    p_section->p_ontop_articles[i] = p_article;
1045                    p_section->ontop_article_count++;
1046    
1047                    // TODO: debug
1048            }
1049            else // ontop == 0
1050            {
1051                    for (i = 0; i < p_section->ontop_article_count; i++)
1052                    {
1053                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1054                            {
1055                                    break;
1056                            }
1057                    }
1058                    if (i == p_section->ontop_article_count) // not found
1059                    {
1060                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1061                            return 0;
1062                    }
1063    
1064                    memmove((void *)(p_section->p_ontop_articles + i),
1065                                    (void *)(p_section->p_ontop_articles + i + 1),
1066                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1067                    p_section->ontop_article_count--;
1068            }
1069    
1070            return 0;
1071    }
1072    
1073    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1074    {
1075            int page_count;
1076    
1077            if (p_section == NULL)
1078            {
1079                    log_error("NULL pointer error\n");
1080                    return -1;
1081            }
1082    
1083            page_count = p_section->page_count - 1 +
1084                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1085                                             BBS_article_limit_per_page;
1086    
1087            if (page_count < 0)
1088            {
1089                    page_count = 0;
1090            }
1091    
1092            return page_count;
1093    }
1094    
1095    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1096    {
1097            if (p_section == NULL)
1098            {
1099                    log_error("NULL pointer error\n");
1100                    return -1;
1101            }
1102    
1103            if (page_id < p_section->page_count - 1)
1104            {
1105                    return BBS_article_limit_per_page;
1106            }
1107            else // if (page_id >= p_section->page_count - 1)
1108            {
1109                    return MIN(MAX(0,
1110                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1111                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1112                                       BBS_article_limit_per_page);
1113            }
1114    }
1115    
1116  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)
1117  {  {
1118          ARTICLE *p_article;          ARTICLE *p_article;
# Line 949  ARTICLE *section_list_find_article_with_ Line 1126  ARTICLE *section_list_find_article_with_
1126    
1127          if (p_section == NULL)          if (p_section == NULL)
1128          {          {
1129                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1130                  return NULL;                  return NULL;
1131          }          }
1132    
# Line 961  ARTICLE *section_list_find_article_with_ Line 1138  ARTICLE *section_list_find_article_with_
1138          }          }
1139    
1140          left = 0;          left = 0;
1141          right = p_section->page_count;          right = p_section->page_count - 1;
1142    
1143          // 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] ]
1144          while (left < right - 1)          while (left < right)
1145          {          {
1146                  // 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
1147                  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;  
                 }  
1148    
1149                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1150                  {                  {
1151                          right = mid;                          right = mid - 1;
1152                  }                  }
1153                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1154                  {                  {
1155                          left = mid;                          left = mid;
1156                  }                  }
# Line 1041  int section_list_calculate_page(SECTION_ Line 1212  int section_list_calculate_page(SECTION_
1212    
1213          if (p_section == NULL)          if (p_section == NULL)
1214          {          {
1215                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1216                  return -1;                  return -1;
1217          }          }
1218    
# Line 1130  int section_list_calculate_page(SECTION_ Line 1301  int section_list_calculate_page(SECTION_
1301          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1302    
1303          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1304          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1305                                                                                                              ? visible_article_count
1306                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1307    
1308          return 0;          return 0;
1309  }  }
# Line 1152  int article_block_article_count(void) Line 1325  int article_block_article_count(void)
1325                  return -1;                  return -1;
1326          }          }
1327    
1328          ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +          ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1329                    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;
1330    
1331          return ret;          return ret;
# Line 1200  int section_list_move_topic(SECTION_LIST Line 1373  int section_list_move_topic(SECTION_LIST
1373    
1374          if (p_section_src == NULL || p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1375          {          {
1376                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1377                  return -1;                  return -1;
1378          }          }
1379    
# Line 1413  int get_section_index(SECTION_LIST *p_se Line 1586  int get_section_index(SECTION_LIST *p_se
1586          return index;          return index;
1587  }  }
1588    
1589    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1590    {
1591            if (p_section == NULL)
1592            {
1593                    log_error("NULL pointer error\n");
1594                    return -1;
1595            }
1596    
1597            if (section_list_rd_lock(p_section) < 0)
1598            {
1599                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1600                    return -2;
1601            }
1602    
1603            if (sname != NULL)
1604            {
1605                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1606            }
1607            if (stitle != NULL)
1608            {
1609                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1610            }
1611            if (master_list != NULL)
1612            {
1613                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1614            }
1615    
1616            // release lock of section
1617            if (section_list_rd_unlock(p_section) < 0)
1618            {
1619                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1620                    return -2;
1621            }
1622    
1623            return 0;
1624    }
1625    
1626  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)
1627  {  {
1628          int index;          int index;
1629          struct sembuf sops[4];          struct sembuf sops[4];
1630    #if !defined(__MSYS__) && !defined(__MINGW32__)
1631          struct timespec timeout;          struct timespec timeout;
1632    #endif
1633          int ret;          int ret;
1634    
1635          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1448  int section_list_try_rd_lock(SECTION_LIS Line 1660  int section_list_try_rd_lock(SECTION_LIS
1660                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate                  sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1661          }          }
1662    
1663    #if defined(__MSYS__) || defined(__MINGW32__)
1664            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4));
1665    #else
1666          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1667          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1668    
1669          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);
1670    #endif
1671          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1672          {          {
1673                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);                  log_error("semop(index = %d, lock read) error %d\n", index, errno);
1674          }          }
1675    
1676          return ret;          return ret;
# Line 1464  int section_list_try_rw_lock(SECTION_LIS Line 1680  int section_list_try_rw_lock(SECTION_LIS
1680  {  {
1681          int index;          int index;
1682          struct sembuf sops[3];          struct sembuf sops[3];
1683    #if !defined(__MSYS__) && !defined(__MINGW32__)
1684          struct timespec timeout;          struct timespec timeout;
1685    #endif
1686          int ret;          int ret;
1687    
1688          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1485  int section_list_try_rw_lock(SECTION_LIS Line 1703  int section_list_try_rw_lock(SECTION_LIS
1703          sops[2].sem_op = 0;                                                        // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1704          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
1705    
1706    #if defined(__MSYS__) || defined(__MINGW32__)
1707            ret = semop(p_section_list_pool->semid, sops, 3);
1708    #else
1709          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1710          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1711    
1712          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1713    #endif
1714          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1715          {          {
1716                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);                  log_error("semop(index = %d, lock write) error %d\n", index, errno);
1717          }          }
1718    
1719          return ret;          return ret;
# Line 1572  int section_list_rd_lock(SECTION_LIST *p Line 1794  int section_list_rd_lock(SECTION_LIST *p
1794                          timer++;                          timer++;
1795                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1796                          {                          {
1797                                  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);
1798                          }                          }
1799                  }                  }
1800                  else // failed                  else // failed
1801                  {                  {
1802                          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);
1803                          break;                          break;
1804                  }                  }
1805          }          }
# Line 1603  int section_list_rw_lock(SECTION_LIST *p Line 1825  int section_list_rw_lock(SECTION_LIST *p
1825                          timer++;                          timer++;
1826                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1827                          {                          {
1828                                  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);
1829                          }                          }
1830                  }                  }
1831                  else // failed                  else // failed
1832                  {                  {
1833                          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);
1834                          break;                          break;
1835                  }                  }
1836          }          }


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

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