/[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.8 by sysadm, Thu May 22 11:10:19 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    
 #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>
26  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
27    
28  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__CYGWIN__)
29  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  union semun
30  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  {
31            int val;                           /* Value for SETVAL */
32            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
33            unsigned short *array; /* Array for GETALL, SETALL */
34            struct seminfo *__buf; /* Buffer for IPC_INFO
35                                                              (Linux-specific) */
36    };
37    #endif // #if defined(_SEM_SEMUN_UNDEFINED)
38    
39  struct article_block_t  enum _section_list_constant_t
40  {  {
41          ARTICLE articles[ARTICLE_PER_BLOCK];          SECTION_TRY_LOCK_WAIT_TIME = 1, // second
42          int32_t article_count;          SECTION_TRY_LOCK_TIMES = 10,
43          struct article_block_t *p_next_block;  
44            ARTICLE_BLOCK_PER_SHM = 1000,           // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
45            ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id)
46            ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT),
47    
48            CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
49    
50            SID_STR_LEN = 5, // 32-bit + NULL
51  };  };
 typedef struct article_block_t ARTICLE_BLOCK;  
52    
53  struct article_block_shm_t  struct article_block_t
54  {  {
55          int shmid;          ARTICLE articles[BBS_article_count_per_block];
56          void *p_shm;          int article_count;
57            struct article_block_t *p_next_block;
58  };  };
59  typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  typedef struct article_block_t ARTICLE_BLOCK;
60    
61  struct article_block_pool_t  struct article_block_pool_t
62  {  {
63          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
64            struct
65            {
66                    int shmid;
67                    void *p_shm;
68            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
69          int shm_count;          int shm_count;
70          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
71          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
72          int32_t block_count;          int block_count;
73  };  };
74  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
75    
76  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77    
78  static SECTION_LIST *p_section_list_pool = NULL;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static int section_list_count = 0;  
 static TRIE_NODE *p_trie_dict_section_list = 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 80  int article_block_init(const char *filen Line 95  int article_block_init(const char *filen
95                  return -1;                  return -1;
96          }          }
97    
98          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
99          {          {
100                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
101                  return -2;                  return -2;
102          }          }
103    
104          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
105          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
106            key = ftok(filename, proj_id);
107            if (key == -1)
108          {          {
109                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
110                  return -2;                  return -3;
111          }          }
112    
113          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
114            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
115            if (shmid == -1)
116            {
117                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
118                    return -3;
119            }
120            p_shm = shmat(shmid, NULL, 0);
121            if (p_shm == (void *)-1)
122            {
123                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
124                    return -3;
125            }
126    
127            p_article_block_pool = p_shm;
128            p_article_block_pool->shmid = shmid;
129    
130          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
131          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
132    
# Line 102  int article_block_init(const char *filen Line 135  int article_block_init(const char *filen
135                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
136                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
137    
138                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
139                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
140                  if (key == -1)                  if (key == -1)
141                  {                  {
# Line 111  int article_block_init(const char *filen Line 144  int article_block_init(const char *filen
144                          return -3;                          return -3;
145                  }                  }
146    
147                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
148                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
149                  if (shmid == -1)                  if (shmid == -1)
150                  {                  {
# Line 155  int article_block_init(const char *filen Line 188  int article_block_init(const char *filen
188    
189  void article_block_cleanup(void)  void article_block_cleanup(void)
190  {  {
191          if (p_article_block_pool != NULL)          int shmid;
192    
193            if (p_article_block_pool == NULL)
194          {          {
195                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  return;
196            }
197    
198            for (int i = 0; i < p_article_block_pool->shm_count; i++)
199            {
200                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
201                  {                  {
202                          if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                          log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
203                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
204    
205                          if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
206                          {                  {
207                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
                         }  
208                  }                  }
209            }
210    
211            shmid = p_article_block_pool->shmid;
212    
213            if (shmdt(p_article_block_pool) == -1)
214            {
215                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216            }
217    
218                  free(p_article_block_pool);          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
219                  p_article_block_pool = NULL;          {
220                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221          }          }
222    
223            p_article_block_pool = NULL;
224    }
225    
226    int set_article_block_shm_readonly(void)
227    {
228    #ifndef __CYGWIN__
229            int shmid;
230            void *p_shm;
231            int i;
232    
233            if (p_article_block_pool == NULL)
234            {
235                    log_error("article_block_pool not initialized\n");
236                    return -1;
237            }
238    
239            for (i = 0; i < p_article_block_pool->shm_count; i++)
240            {
241                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
242    
243                    // Remap shared memory in read-only mode
244                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
245                    if (p_shm == (void *)-1)
246                    {
247                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
248                            return -2;
249                    }
250            }
251    #endif
252    
253            return 0;
254    }
255    
256    int detach_article_block_shm(void)
257    {
258            int shmid;
259    
260            if (p_article_block_pool == NULL)
261            {
262                    return -1;
263            }
264    
265            for (int i = 0; i < p_article_block_pool->shm_count; i++)
266            {
267                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
268                    {
269                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
270                            return -2;
271                    }
272            }
273    
274            shmid = p_article_block_pool->shmid;
275    
276            if (shmdt(p_article_block_pool) == -1)
277            {
278                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
279                    return -3;
280            }
281    
282            p_article_block_pool = NULL;
283    
284            return 0;
285  }  }
286    
287  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 235  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 279  ARTICLE *article_block_find_by_aid(int32 Line 382  ARTICLE *article_block_find_by_aid(int32
382                  }                  }
383          }          }
384    
385          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
386            {
387                    return NULL;
388            }
389    
390            return (p_block->articles + left); // found
391  }  }
392    
393  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 292  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("section_data_find_article_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("section_data_find_article_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  SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name)  extern int section_list_init(const char *filename)
421  {  {
422          SECTION_LIST *p_section;          int semid;
423            int shmid;
424            int proj_id;
425            key_t key;
426            size_t size;
427            void *p_shm;
428            union semun arg;
429            int i;
430    
431          if (p_section_list_pool == NULL)          if (p_section_list_pool != NULL)
432          {          {
433                  p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));                  section_list_cleanup();
434                  if (p_section_list_pool == NULL)          }
435                  {  
436                          log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);          proj_id = (int)(time(NULL) % getpid());
437                          return NULL;          key = ftok(filename, proj_id);
438                  }          if (key == -1)
439            {
440                    log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
441                    return -3;
442            }
443    
444            // Allocate shared memory
445            size = sizeof(SECTION_LIST_POOL);
446            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
447            if (shmid == -1)
448            {
449                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
450                    return -3;
451            }
452            p_shm = shmat(shmid, NULL, 0);
453            if (p_shm == (void *)-1)
454            {
455                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
456                    return -3;
457            }
458    
459                  section_list_count = 0;          p_section_list_pool = p_shm;
460            p_section_list_pool->shmid = shmid;
461            p_section_list_pool->section_count = 0;
462    
463            // Allocate semaphore as section locks
464            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
465            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
466            if (semid == -1)
467            {
468                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
469                    return -3;
470          }          }
471    
472          if (p_trie_dict_section_list == NULL)          // Initialize sem value to 0
473            arg.val = 0;
474            for (i = 0; i < size; i++)
475          {          {
476                  p_trie_dict_section_list = trie_dict_create();                  if (semctl(semid, i, SETVAL, arg) == -1)
                 if (p_trie_dict_section_list == NULL)  
477                  {                  {
478                          log_error("trie_dict_create() OOM\n", BBS_max_section);                          log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
479                          return NULL;                          return -3;
480                  }                  }
481          }          }
482    
483          if (section_list_count >= BBS_max_section)          p_section_list_pool->semid = semid;
484    
485            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
486            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
487            {
488                    log_error("trie_dict_create() OOM\n", BBS_max_section);
489                    return -2;
490            }
491    
492            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
493            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
494            {
495                    log_error("trie_dict_create() OOM\n", BBS_max_section);
496                    return -2;
497            }
498    
499            return 0;
500    }
501    
502    void section_list_cleanup(void)
503    {
504            int shmid;
505    
506            if (p_section_list_pool == NULL)
507            {
508                    return;
509            }
510    
511            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
512            {
513                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
514                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
515            }
516    
517            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
518            {
519                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
520                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
521            }
522    
523            shmid = p_section_list_pool->shmid;
524    
525            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
526            {
527                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
528            }
529    
530            if (shmdt(p_section_list_pool) == -1)
531            {
532                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
533            }
534    
535            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
536          {          {
537                  log_error("section_list_count exceed limit %d\n", BBS_max_section);                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
538            }
539    
540            p_section_list_pool = NULL;
541    }
542    
543    int set_section_list_shm_readonly(void)
544    {
545    #ifndef __CYGWIN__
546            int shmid;
547            void *p_shm;
548    
549            if (p_section_list_pool == NULL)
550            {
551                    log_error("p_section_list_pool not initialized\n");
552                    return -1;
553            }
554    
555            shmid = p_section_list_pool->shmid;
556    
557            // Remap shared memory in read-only mode
558            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
559            if (p_shm == (void *)-1)
560            {
561                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
562                    return -3;
563            }
564    
565            p_section_list_pool = p_shm;
566    #endif
567    
568            return 0;
569    }
570    
571    int detach_section_list_shm(void)
572    {
573            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
574            {
575                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
576                    return -1;
577            }
578    
579            p_section_list_pool = NULL;
580    
581            return 0;
582    }
583    
584    inline static void sid_to_str(int32_t sid, char *p_sid_str)
585    {
586            uint32_t u_sid;
587            int i;
588    
589            u_sid = (uint32_t)sid;
590            for (i = 0; i < SID_STR_LEN - 1; i++)
591            {
592                    p_sid_str[i] = (char)(u_sid % 255 + 1);
593                    u_sid /= 255;
594            }
595            p_sid_str[i] = '\0';
596    }
597    
598    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
599    {
600            SECTION_LIST *p_section;
601            char sid_str[SID_STR_LEN];
602    
603            if (p_section_list_pool == NULL)
604            {
605                    log_error("session_list_pool not initialized\n");
606                  return NULL;                  return NULL;
607          }          }
608    
609          p_section = p_section_list_pool + section_list_count;          if (p_section_list_pool->section_count >= BBS_max_section)
610            {
611                    log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
612                    return NULL;
613            }
614    
615            sid_to_str(sid, sid_str);
616    
617          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
         p_section->sname[sizeof(p_section->sname - 1)] = '\0';  
618    
619          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          p_section->sid = sid;
620          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->ex_menu_tm = 0;
621    
622          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
623          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
624    
625          if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1)          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
626            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
627    
628            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
629            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
630    
631            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
632          {          {
633                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
634                    return NULL;
635            }
636    
637            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
638            {
639                    log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
640                  return NULL;                  return NULL;
641          }          }
642    
643          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
644    
645          section_list_count++;          p_section_list_pool->section_count++;
646    
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 376  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  void section_list_cleanup(void)  SECTION_LIST *section_list_find_by_name(const char *sname)
696  {  {
697          if (p_trie_dict_section_list != NULL)          int64_t index;
698            int ret;
699    
700            if (p_section_list_pool == NULL)
701          {          {
702                  trie_dict_destroy(p_trie_dict_section_list);                  log_error("section_list not initialized\n");
703                  p_trie_dict_section_list = NULL;                  return NULL;
704          }          }
705    
706          if (p_section_list_pool != NULL)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
707            if (ret < 0)
708            {
709                    log_error("trie_dict_get(section, %s) error\n", sname);
710                    return NULL;
711            }
712            else if (ret == 0)
713          {          {
714                  free(p_section_list_pool);                  return NULL;
                 p_section_list_pool = NULL;  
715          }          }
716    
717          section_list_count = 0;          return (p_section_list_pool->sections + index);
718  }  }
719    
720  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
721  {  {
722          int64_t index;          int64_t index;
723            int ret;
724            char sid_str[SID_STR_LEN];
725    
726          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
727          {          {
728                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
729                  return NULL;                  return NULL;
730          }          }
731    
732          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
733    
734            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
735            if (ret < 0)
736            {
737                    log_error("trie_dict_get(section, %d) error\n", sid);
738                    return NULL;
739            }
740            else if (ret == 0)
741          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
742                  return NULL;                  return NULL;
743          }          }
744    
745          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
746  }  }
747    
748  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)  int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
# Line 424  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 434  int section_list_append_article(SECTION_ Line 765  int section_list_append_article(SECTION_
765                  return -1;                  return -1;
766          }          }
767    
768            if (p_section->sid != p_article_src->sid)
769            {
770                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
771                    return -2;
772            }
773    
774            if (p_section->article_count >= BBS_article_limit_per_section)
775            {
776                    log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
777                    return -2;
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 445  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 460  int section_list_append_article(SECTION_ Line 803  int section_list_append_article(SECTION_
803          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
804          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
805          {          {
806                  log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);                  log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
807                  return -3;                  return -3;
808          }          }
809    
# Line 524  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->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++;
875                  p_section->last_page_visible_article_count = 0;                  p_section->last_page_visible_article_count = 0;
876          }          }
877          p_section->last_page_visible_article_count++;  
878            if (p_article->visible)
879            {
880                    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  }  }
# Line 543  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 553  int section_list_set_article_visible(SEC Line 908  int section_list_set_article_visible(SEC
908                  return -1; // Not found                  return -1; // Not found
909          }          }
910    
911            if (p_section->sid != p_article->sid)
912            {
913                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
914                    return -2;
915            }
916    
917          if (p_article->visible == visible)          if (p_article->visible == visible)
918          {          {
919                  return 0; // Already set                  return 0; // Already set
# Line 562  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 580  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 592  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 600  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  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset)  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)
1103  {  {
1104          ARTICLE *p_article;          ARTICLE *p_article;
1105          int left;          int left;
# Line 609  ARTICLE *section_list_find_article_with_ Line 1108  ARTICLE *section_list_find_article_with_
1108    
1109          *p_page = -1;          *p_page = -1;
1110          *p_offset = -1;          *p_offset = -1;
1111            *pp_next = NULL;
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 624  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 653  ARTICLE *section_list_find_article_with_ Line 1147  ARTICLE *section_list_find_article_with_
1147          p_article = p_section->p_page_first_article[*p_page];          p_article = p_section->p_page_first_article[*p_page];
1148    
1149          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
1150          right = (*p_page == p_section->page_count - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);          right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
1151    
1152          // left will be the offset of article found or offset to insert          // left will be the offset of article found or offset to insert
1153          left = 0;          left = 0;
1154    
1155          while (1)          while (aid > p_article->aid)
1156          {          {
1157                  if (aid == p_article->aid) // found                  p_article = p_article->p_next;
1158                  {                  left++;
1159                          break;  
1160                  }                  if (aid == p_article->aid)
                 else if (aid < p_article->aid) // not exist  
1161                  {                  {
1162                          p_article = NULL;                          *pp_next = p_article->p_next;
1163                          break;                          break;
1164                  }                  }
1165    
                 // aid > p_article->aid  
                 p_article = p_article->p_next;  
                 left++;  
   
1166                  // over last article in the page                  // over last article in the page
1167                  if (p_article == p_section->p_article_head || p_article->aid >= right)                  if (p_article == p_section->p_article_head || p_article->aid >= right)
1168                  {                  {
1169                          p_article = NULL;                          *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
1170                          break;                          *p_offset = left;
1171                            return NULL; // not found
1172                  }                  }
1173          }          }
1174    
1175            if (aid < p_article->aid)
1176            {
1177                    *pp_next = p_article;
1178                    p_article = NULL; // not found
1179            }
1180            else // aid == p_article->aid
1181            {
1182                    *pp_next = p_article->p_next;
1183            }
1184    
1185          *p_offset = left;          *p_offset = left;
1186    
1187          return p_article;          return p_article;
# Line 689  ARTICLE *section_list_find_article_with_ Line 1189  ARTICLE *section_list_find_article_with_
1189    
1190  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
1191  {  {
1192          // ARTICLE *p_article;          ARTICLE *p_article;
1193            ARTICLE *p_next;
1194            int32_t page;
1195            int32_t offset;
1196            int visible_article_count;
1197            int page_head_set;
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    
1205            if (p_section->article_count == 0) // empty
1206            {
1207                    p_section->page_count = 0;
1208                    p_section->last_page_visible_article_count = 0;
1209    
1210                    return 0;
1211            }
1212    
1213            if (start_aid > 0)
1214            {
1215                    p_article = article_block_find_by_aid(start_aid);
1216                    if (p_article == NULL)
1217                    {
1218                            return -1; // Not found
1219                    }
1220    
1221                    if (p_section->sid != p_article->sid)
1222                    {
1223                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1224                            return -2;
1225                    }
1226    
1227                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1228                    if (p_article == NULL)
1229                    {
1230                            if (page < 0)
1231                            {
1232                                    return -1;
1233                            }
1234                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
1235                                              start_aid, p_section->sid);
1236                            return -2;
1237                    }
1238    
1239                    if (offset > 0)
1240                    {
1241                            p_article = p_section->p_page_first_article[page];
1242                    }
1243            }
1244            else
1245            {
1246                    p_article = p_section->p_article_head;
1247                    page = 0;
1248                    offset = 0;
1249            }
1250    
1251            visible_article_count = 0;
1252            page_head_set = 0;
1253    
1254            do
1255            {
1256                    if (!page_head_set && visible_article_count == 0)
1257                    {
1258                            p_section->p_page_first_article[page] = p_article;
1259                            page_head_set = 1;
1260                    }
1261    
1262                    if (p_article->visible)
1263                    {
1264                            visible_article_count++;
1265                    }
1266    
1267                    p_article = p_article->p_next;
1268    
1269                    // skip remaining invisible articles
1270                    while (p_article->visible == 0 && p_article != p_section->p_article_head)
1271                    {
1272                            p_article = p_article->p_next;
1273                    }
1274    
1275                    if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
1276                    {
1277                            page++;
1278                            visible_article_count = 0;
1279                            page_head_set = 0;
1280    
1281                            if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
1282                            {
1283                                    log_error("Count of page exceed limit in section %d\n", p_section->sid);
1284                                    break;
1285                            }
1286                    }
1287            } while (p_article != p_section->p_article_head);
1288    
1289            p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1290            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  }  }
1296    
1297    int32_t article_block_last_aid(void)
1298    {
1299            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1300            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1301    
1302            return last_aid;
1303    }
1304    
1305    int article_block_article_count(void)
1306    {
1307            int ret;
1308    
1309            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1310            {
1311                    return -1;
1312            }
1313    
1314            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1315                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1316    
1317            return ret;
1318    }
1319    
1320    int article_count_of_topic(int32_t aid)
1321    {
1322            ARTICLE *p_article;
1323            int article_count;
1324    
1325            p_article = article_block_find_by_aid(aid);
1326            if (p_article == NULL)
1327            {
1328                    return 0; // Not found
1329            }
1330    
1331            article_count = 0;
1332    
1333            do
1334            {
1335                    if (p_article->tid != 0 && p_article->tid != aid)
1336                    {
1337                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1338                            break;
1339                    }
1340    
1341                    article_count++;
1342                    p_article = p_article->p_topic_next;
1343            } while (p_article->aid != aid);
1344    
1345            return article_count;
1346    }
1347    
1348    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1349    {
1350            ARTICLE *p_article;
1351            ARTICLE *p_next;
1352            int32_t page;
1353            int32_t offset;
1354            int32_t move_article_count;
1355            int32_t dest_article_count_old;
1356            int32_t last_unaffected_aid_src;
1357            int32_t first_inserted_aid_dest;
1358            int move_counter;
1359    
1360            if (p_section_src == NULL || p_section_dest == NULL)
1361            {
1362                    log_error("NULL pointer error\n");
1363                    return -1;
1364            }
1365    
1366            if (p_section_src->sid == p_section_dest->sid)
1367            {
1368                    log_error("section_list_move_topic() src and dest section are the same\n");
1369                    return -1;
1370            }
1371    
1372            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1373            {
1374                    log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1375                    return -2;
1376            }
1377    
1378            if (p_section_src->sid != p_article->sid)
1379            {
1380                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1381                                      p_section_src->sid, p_article->aid, p_article->sid);
1382                    return -2;
1383            }
1384    
1385            if (p_article->tid != 0)
1386            {
1387                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1388                    return -2;
1389            }
1390    
1391            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1392    
1393            move_article_count = article_count_of_topic(aid);
1394            if (move_article_count <= 0)
1395            {
1396                    log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1397                    return -2;
1398            }
1399    
1400            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1401            {
1402                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1403                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1404                    return -3;
1405            }
1406    
1407            dest_article_count_old = p_section_dest->article_count;
1408            move_counter = 0;
1409            first_inserted_aid_dest = p_article->aid;
1410    
1411            do
1412            {
1413                    if (p_section_src->sid != p_article->sid)
1414                    {
1415                            log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1416                                              p_section_src->sid, p_article->aid, p_article->sid);
1417                            p_article = p_article->p_topic_next;
1418                            continue;
1419                    }
1420    
1421                    // Remove from bi-directional article list of src section
1422                    if (p_section_src->p_article_head == p_article)
1423                    {
1424                            p_section_src->p_article_head = p_article->p_next;
1425                    }
1426                    if (p_section_src->p_article_tail == p_article)
1427                    {
1428                            p_section_src->p_article_tail = p_article->p_prior;
1429                    }
1430                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1431                    {
1432                            p_section_src->p_article_head = NULL;
1433                            p_section_src->p_article_tail = NULL;
1434                    }
1435    
1436                    p_article->p_prior->p_next = p_article->p_next;
1437                    p_article->p_next->p_prior = p_article->p_prior;
1438    
1439                    // Update sid of article
1440                    p_article->sid = p_section_dest->sid;
1441    
1442                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1443                    {
1444                            log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1445                            p_article = p_article->p_topic_next;
1446                            continue;
1447                    }
1448    
1449                    // Insert into bi-directional article list of dest section
1450                    if (p_next == NULL) // empty section
1451                    {
1452                            p_section_dest->p_article_head = p_article;
1453                            p_section_dest->p_article_tail = p_article;
1454                            p_article->p_prior = p_article;
1455                            p_article->p_next = p_article;
1456                    }
1457                    else
1458                    {
1459                            if (p_section_dest->p_article_head == p_next)
1460                            {
1461                                    if (p_article->aid < p_next->aid)
1462                                    {
1463                                            p_section_dest->p_article_head = p_article;
1464                                    }
1465                                    else // p_article->aid > p_next->aid
1466                                    {
1467                                            p_section_dest->p_article_tail = p_article;
1468                                    }
1469                            }
1470    
1471                            p_article->p_prior = p_next->p_prior;
1472                            p_article->p_next = p_next;
1473                            p_next->p_prior->p_next = p_article;
1474                            p_next->p_prior = p_article;
1475                    }
1476    
1477                    // Update article / topic counter of src / desc section
1478                    p_section_src->article_count--;
1479                    p_section_dest->article_count++;
1480                    if (p_article->tid == 0)
1481                    {
1482                            p_section_src->topic_count--;
1483                            p_section_dest->topic_count++;
1484                    }
1485    
1486                    // Update visible article / topic counter of src / desc section
1487                    if (p_article->visible)
1488                    {
1489                            p_section_src->visible_article_count--;
1490                            p_section_dest->visible_article_count++;
1491                            if (p_article->tid == 0)
1492                            {
1493                                    p_section_src->visible_topic_count--;
1494                                    p_section_dest->visible_topic_count++;
1495                            }
1496                    }
1497    
1498                    // Update page for empty dest section
1499                    if (p_section_dest->article_count == 1)
1500                    {
1501                            p_section_dest->p_page_first_article[0] = p_article;
1502                            p_section_dest->page_count = 1;
1503                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1504                    }
1505    
1506                    p_article = p_article->p_topic_next;
1507    
1508                    move_counter++;
1509                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1510                    {
1511                            // Re-calculate pages of desc section
1512                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1513                            {
1514                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1515                                                      p_section_dest->sid, first_inserted_aid_dest);
1516                            }
1517    
1518                            first_inserted_aid_dest = p_article->aid;
1519                    }
1520            } while (p_article->aid != aid);
1521    
1522            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1523            {
1524                    log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1525                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1526            }
1527    
1528            // Re-calculate pages of src section
1529            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1530            {
1531                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1532                                      p_section_src->sid, last_unaffected_aid_src, aid);
1533            }
1534    
1535            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1536            {
1537                    // Re-calculate pages of desc section
1538                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1539                    {
1540                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1541                                              p_section_dest->sid, first_inserted_aid_dest);
1542                    }
1543            }
1544    
1545            return move_article_count;
1546    }
1547    
1548    int get_section_index(SECTION_LIST *p_section)
1549    {
1550            int index;
1551    
1552            if (p_section_list_pool == NULL)
1553            {
1554                    log_error("get_section_index() error: uninitialized\n");
1555                    return -1;
1556            }
1557    
1558            if (p_section == NULL)
1559            {
1560                    index = BBS_max_section;
1561            }
1562            else
1563            {
1564                    index = (int)(p_section - p_section_list_pool->sections);
1565                    if (index < 0 || index >= BBS_max_section)
1566                    {
1567                            log_error("get_section_index(%d) error: index out of range\n", index);
1568                            return -2;
1569                    }
1570            }
1571    
1572            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)
1613    {
1614            int index;
1615            struct sembuf sops[4];
1616    #ifndef __CYGWIN__
1617            struct timespec timeout;
1618    #endif
1619            int ret;
1620    
1621            index = get_section_index(p_section);
1622            if (index < 0)
1623            {
1624                    return -2;
1625            }
1626    
1627            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1628            sops[0].sem_op = 0;                                                                // wait until unlocked
1629            sops[0].sem_flg = 0;
1630    
1631            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1632            sops[1].sem_op = 1;                                                        // lock
1633            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1634    
1635            // Read lock on any specific section will also acquire single read lock on "all section"
1636            // so that write lock on all section only need to acquire single write on on "all section"
1637            // rather than to acquire multiple write locks on all the available sections.
1638            if (index != BBS_max_section)
1639            {
1640                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1641                    sops[2].sem_op = 0;                                                // wait until unlocked
1642                    sops[2].sem_flg = 0;
1643    
1644                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1645                    sops[3].sem_op = 1;                                        // lock
1646                    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;
1653            timeout.tv_nsec = 0;
1654    
1655            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)
1658            {
1659                    log_error("semop(index = %d, lock read) error %d\n", index, errno);
1660            }
1661    
1662            return ret;
1663    }
1664    
1665    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1666    {
1667            int index;
1668            struct sembuf sops[3];
1669    #ifndef __CYGWIN__
1670            struct timespec timeout;
1671    #endif
1672            int ret;
1673    
1674            index = get_section_index(p_section);
1675            if (index < 0)
1676            {
1677                    return -2;
1678            }
1679    
1680            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1681            sops[0].sem_op = 0;                                                                // wait until unlocked
1682            sops[0].sem_flg = 0;
1683    
1684            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1685            sops[1].sem_op = 1;                                                                // lock
1686            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1687    
1688            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1689            sops[2].sem_op = 0;                                                        // wait until unlocked
1690            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;
1696            timeout.tv_nsec = 0;
1697    
1698            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1699    #endif
1700            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1701            {
1702                    log_error("semop(index = %d, lock write) error %d\n", index, errno);
1703            }
1704    
1705            return ret;
1706    }
1707    
1708    int section_list_rd_unlock(SECTION_LIST *p_section)
1709    {
1710            int index;
1711            struct sembuf sops[2];
1712            int ret;
1713    
1714            index = get_section_index(p_section);
1715            if (index < 0)
1716            {
1717                    return -2;
1718            }
1719    
1720            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1721            sops[0].sem_op = -1;                                               // unlock
1722            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1723    
1724            if (index != BBS_max_section)
1725            {
1726                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1727                    sops[1].sem_op = -1;                                     // unlock
1728                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1729            }
1730    
1731            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1732            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1733            {
1734                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1735            }
1736    
1737            return ret;
1738    }
1739    
1740    int section_list_rw_unlock(SECTION_LIST *p_section)
1741    {
1742            int index;
1743            struct sembuf sops[1];
1744            int ret;
1745    
1746            index = get_section_index(p_section);
1747            if (index < 0)
1748            {
1749                    return -2;
1750            }
1751    
1752            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1753            sops[0].sem_op = -1;                                                       // unlock
1754            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1755    
1756            ret = semop(p_section_list_pool->semid, sops, 1);
1757            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1758            {
1759                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1760            }
1761    
1762            return ret;
1763    }
1764    
1765    int section_list_rd_lock(SECTION_LIST *p_section)
1766    {
1767            int timer = 0;
1768            int sid = (p_section == NULL ? 0 : p_section->sid);
1769            int ret = -1;
1770    
1771            while (!SYS_server_exit)
1772            {
1773                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1774                    if (ret == 0) // success
1775                    {
1776                            break;
1777                    }
1778                    else if (errno == EAGAIN || errno == EINTR) // retry
1779                    {
1780                            timer++;
1781                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1782                            {
1783                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1784                            }
1785                    }
1786                    else // failed
1787                    {
1788                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1789                            break;
1790                    }
1791            }
1792    
1793            return ret;
1794    }
1795    
1796    int section_list_rw_lock(SECTION_LIST *p_section)
1797    {
1798            int timer = 0;
1799            int sid = (p_section == NULL ? 0 : p_section->sid);
1800            int ret = -1;
1801    
1802            while (!SYS_server_exit)
1803            {
1804                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1805                    if (ret == 0) // success
1806                    {
1807                            break;
1808                    }
1809                    else if (errno == EAGAIN || errno == EINTR) // retry
1810                    {
1811                            timer++;
1812                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1813                            {
1814                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1815                            }
1816                    }
1817                    else // failed
1818                    {
1819                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1820                            break;
1821                    }
1822            }
1823    
1824            return ret;
1825    }


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

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