/[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.41 by sysadm, Tue Oct 14 00:57:06 2025 UTC Revision 1.54 by sysadm, Wed Nov 5 03:01:14 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     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "log.h"  #include "log.h"
10  #include "section_list.h"  #include "section_list.h"
11  #include "trie_dict.h"  #include "trie_dict.h"
12    #include "user_list.h"
13  #include <errno.h>  #include <errno.h>
14  #include <signal.h>  #include <signal.h>
15  #include <stdio.h>  #include <stdio.h>
# Line 52  union semun Line 45  union semun
45    
46  struct article_block_t  struct article_block_t
47  {  {
48          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
49          int article_count;          int article_count;
50          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
51  };  };
# Line 215  void article_block_cleanup(void) Line 208  void article_block_cleanup(void)
208                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
209          }          }
210    
211          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
212          {          {
213                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
214          }          }
# Line 342  ARTICLE *article_block_find_by_aid(int32 Line 335  ARTICLE *article_block_find_by_aid(int32
335          }          }
336    
337          left = 0;          left = 0;
338          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
339    
340          // 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] ]
341          while (left < right - 1)          while (left < right)
342          {          {
343                  // 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
344                  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;  
                 }  
345    
346                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
347                  {                  {
348                          right = mid;                          right = mid - 1;
349                  }                  }
350                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
351                  {                  {
352                          left = mid;                          left = mid;
353                  }                  }
# Line 404  ARTICLE *article_block_find_by_index(int Line 391  ARTICLE *article_block_find_by_index(int
391                  return NULL;                  return NULL;
392          }          }
393    
394          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)
395          {          {
396                  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);
397                  return NULL;                  return NULL;
398          }          }
399    
400          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];
401    
402          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
403          {          {
404                  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);
405                  return NULL;                  return NULL;
406          }          }
407    
408          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
409  }  }
410    
411  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 536  void section_list_cleanup(void) Line 523  void section_list_cleanup(void)
523                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
524          }          }
525    
526          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
527          {          {
528                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
529          }          }
# Line 649  SECTION_LIST *section_list_create(int32_ Line 636  SECTION_LIST *section_list_create(int32_
636          return p_section;          return p_section;
637  }  }
638    
639    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
640    {
641            int64_t index;
642    
643            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
644            {
645                    log_error("NULL pointer error\n");
646                    return -1;
647            }
648    
649            index = get_section_index(p_section);
650    
651            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
652            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
653    
654            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
655            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
656    
657            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
658            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
659    
660            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
661            {
662                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
663                    return -2;
664            }
665    
666            return 0;
667    }
668    
669  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
670  {  {
671          p_section->article_count = 0;          p_section->article_count = 0;
# Line 727  int section_list_append_article(SECTION_ Line 744  int section_list_append_article(SECTION_
744    
745          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
746          {          {
747                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
748                  return -1;                  return -1;
749          }          }
750    
# Line 750  int section_list_append_article(SECTION_ Line 767  int section_list_append_article(SECTION_
767          }          }
768    
769          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
770                  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)
771          {          {
772                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
773                  {                  {
# Line 760  int section_list_append_article(SECTION_ Line 777  int section_list_append_article(SECTION_
777    
778                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
779                  {                  {
780                          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;
781                  }                  }
782    
783                  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 839  int section_list_append_article(SECTION_ Line 856  int section_list_append_article(SECTION_
856          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
857    
858          // Update page          // Update page
859          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) ||
860                  p_section->article_count == 1)                  p_section->page_count == 0)
861          {          {
862                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
863                  p_section->page_count++;                  p_section->page_count++;
# Line 895  int section_list_set_article_visible(SEC Line 912  int section_list_set_article_visible(SEC
912          {          {
913                  p_section->visible_article_count--;                  p_section->visible_article_count--;
914    
915                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
916                    {
917                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
918                    }
919    
920                  if (p_article->tid == 0)                  if (p_article->tid == 0)
921                  {                  {
922                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 913  int section_list_set_article_visible(SEC Line 935  int section_list_set_article_visible(SEC
935                                          p_reply->visible = 0;                                          p_reply->visible = 0;
936                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
937                                          affected_count++;                                          affected_count++;
938    
939                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
940                                            {
941                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
942                                            }
943                                  }                                  }
944                          }                          }
945                  }                  }
# Line 925  int section_list_set_article_visible(SEC Line 952  int section_list_set_article_visible(SEC
952                  {                  {
953                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
954                  }                  }
955    
956                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
957                    {
958                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
959                    }
960          }          }
961    
962          p_article->visible = visible;          p_article->visible = visible;
# Line 1023  int section_list_page_count_with_ontop(S Line 1055  int section_list_page_count_with_ontop(S
1055                  return -1;                  return -1;
1056          }          }
1057    
1058          page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +          page_count = p_section->page_count - 1 +
1059                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +                                   (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1060                                   ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);                                   ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0);
1061    
1062            if (page_count < 0)
1063            {
1064                    page_count = 0;
1065            }
1066    
1067          return page_count;          return page_count;
1068  }  }
# Line 1044  int section_list_page_article_count_with Line 1081  int section_list_page_article_count_with
1081          }          }
1082          else // if (page_id >= p_section->page_count - 1)          else // if (page_id >= p_section->page_count - 1)
1083          {          {
1084                  return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -                  return MIN(MAX(0,
1085                                             BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));                                             (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1086                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1087                                       BBS_article_limit_per_page);
1088          }          }
1089  }  }
1090    
# Line 1062  ARTICLE *section_list_find_article_with_ Line 1101  ARTICLE *section_list_find_article_with_
1101    
1102          if (p_section == NULL)          if (p_section == NULL)
1103          {          {
1104                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1105                  return NULL;                  return NULL;
1106          }          }
1107    
# Line 1074  ARTICLE *section_list_find_article_with_ Line 1113  ARTICLE *section_list_find_article_with_
1113          }          }
1114    
1115          left = 0;          left = 0;
1116          right = p_section->page_count;          right = p_section->page_count - 1;
1117    
1118          // 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] ]
1119          while (left < right - 1)          while (left < right)
1120          {          {
1121                  // 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
1122                  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;  
                 }  
1123    
1124                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1125                  {                  {
1126                          right = mid;                          right = mid - 1;
1127                  }                  }
1128                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1129                  {                  {
1130                          left = mid;                          left = mid;
1131                  }                  }
# Line 1154  int section_list_calculate_page(SECTION_ Line 1187  int section_list_calculate_page(SECTION_
1187    
1188          if (p_section == NULL)          if (p_section == NULL)
1189          {          {
1190                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1191                  return -1;                  return -1;
1192          }          }
1193    
# Line 1243  int section_list_calculate_page(SECTION_ Line 1276  int section_list_calculate_page(SECTION_
1276          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1277    
1278          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1279          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1280                                                                                                              ? visible_article_count
1281                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1282    
1283          return 0;          return 0;
1284  }  }
# Line 1265  int article_block_article_count(void) Line 1300  int article_block_article_count(void)
1300                  return -1;                  return -1;
1301          }          }
1302    
1303          ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +          ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1304                    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;
1305    
1306          return ret;          return ret;
# Line 1313  int section_list_move_topic(SECTION_LIST Line 1348  int section_list_move_topic(SECTION_LIST
1348    
1349          if (p_section_src == NULL || p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1350          {          {
1351                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1352                  return -1;                  return -1;
1353          }          }
1354    
# Line 1526  int get_section_index(SECTION_LIST *p_se Line 1561  int get_section_index(SECTION_LIST *p_se
1561          return index;          return index;
1562  }  }
1563    
1564    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1565    {
1566            if (p_section == NULL)
1567            {
1568                    log_error("NULL pointer error\n");
1569                    return -1;
1570            }
1571    
1572            if (section_list_rd_lock(p_section) < 0)
1573            {
1574                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1575                    return -2;
1576            }
1577    
1578            if (sname != NULL)
1579            {
1580                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1581            }
1582            if (stitle != NULL)
1583            {
1584                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1585            }
1586            if (master_list != NULL)
1587            {
1588                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1589            }
1590    
1591            // release lock of section
1592            if (section_list_rd_unlock(p_section) < 0)
1593            {
1594                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1595                    return -2;
1596            }
1597    
1598            return 0;
1599    }
1600    
1601  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)
1602  {  {
1603          int index;          int index;
# Line 1685  int section_list_rd_lock(SECTION_LIST *p Line 1757  int section_list_rd_lock(SECTION_LIST *p
1757                          timer++;                          timer++;
1758                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1759                          {                          {
1760                                  log_error("section_list_try_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);
1761                          }                          }
1762                  }                  }
1763                  else // failed                  else // failed
# Line 1716  int section_list_rw_lock(SECTION_LIST *p Line 1788  int section_list_rw_lock(SECTION_LIST *p
1788                          timer++;                          timer++;
1789                          if (timer % SECTION_TRY_LOCK_TIMES == 0)                          if (timer % SECTION_TRY_LOCK_TIMES == 0)
1790                          {                          {
1791                                  log_error("section_list_try_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);
1792                          }                          }
1793                  }                  }
1794                  else // failed                  else // failed


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

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