/[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.13 by sysadm, Fri May 23 14:04:05 2025 UTC Revision 1.58 by sysadm, Mon Nov 17 11:56:11 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(__MSYS__) || defined(__MINGW32__)
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    enum _section_list_constant_t
40    {
41            SECTION_TRY_LOCK_WAIT_TIME = 1, // second
42            SECTION_TRY_LOCK_TIMES = 10,
43    
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  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic          CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections
49    
50            SID_STR_LEN = 5, // 32-bit + NULL
51    };
52    
53  struct article_block_t  struct article_block_t
54  {  {
55          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
56          int32_t article_count;          int article_count;
57          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
58  };  };
59  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
60    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
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 int section_list_pool_shmid;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static SECTION_LIST *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 83  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 105  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 114  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 158  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            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
219            {
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            int shmid;
229            void *p_shm;
230            int i;
231    
232            if (p_article_block_pool == NULL)
233            {
234                    log_error("article_block_pool not initialized\n");
235                    return -1;
236            }
237    
238            for (i = 0; i < p_article_block_pool->shm_count; i++)
239            {
240                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
241    
242                    // Remap shared memory in read-only mode
243                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
244                    if (p_shm == (void *)-1)
245                    {
246                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
247                            return -2;
248                    }
249            }
250    
251            return 0;
252    }
253    
254    int detach_article_block_shm(void)
255    {
256            int shmid;
257    
258            if (p_article_block_pool == NULL)
259            {
260                    return -1;
261            }
262    
263            for (int i = 0; i < p_article_block_pool->shm_count; i++)
264            {
265                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
266                    {
267                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
268                            return -2;
269                  }                  }
270            }
271    
272            shmid = p_article_block_pool->shmid;
273    
274                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
275                  p_article_block_pool = NULL;          {
276                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
277                    return -3;
278          }          }
279    
280            p_article_block_pool = NULL;
281    
282            return 0;
283  }  }
284    
285  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 238  ARTICLE *article_block_find_by_aid(int32 Line 342  ARTICLE *article_block_find_by_aid(int32
342          }          }
343    
344          left = 0;          left = 0;
345          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
346    
347          // 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] ]
348          while (left < right - 1)          while (left < right)
349          {          {
350                  // 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
351                  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;  
                 }  
352    
353                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
354                  {                  {
355                          right = mid;                          right = mid - 1;
356                  }                  }
357                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
358                  {                  {
359                          left = mid;                          left = mid;
360                  }                  }
# Line 282  ARTICLE *article_block_find_by_aid(int32 Line 380  ARTICLE *article_block_find_by_aid(int32
380                  }                  }
381          }          }
382    
383          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
384            {
385                    return NULL;
386            }
387    
388            return (p_block->articles + left); // found
389  }  }
390    
391  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 295  ARTICLE *article_block_find_by_index(int Line 398  ARTICLE *article_block_find_by_index(int
398                  return NULL;                  return NULL;
399          }          }
400    
401          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)
402          {          {
403                  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);
404                  return NULL;                  return NULL;
405          }          }
406    
407          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];
408    
409          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
410          {          {
411                  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);
412                  return NULL;                  return NULL;
413          }          }
414    
415          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
416  }  }
417    
418  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
419  {  {
420            int semid;
421          int shmid;          int shmid;
422          int proj_id;          int proj_id;
423          key_t key;          key_t key;
424          size_t size;          size_t size;
425          void *p_shm;          void *p_shm;
426            union semun arg;
427            int i;
428    
429          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool != NULL)
         {  
                 section_list_pool_cleanup();  
         }  
   
         p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));  
         if (p_section_list_pool == NULL)  
430          {          {
431                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
432          }          }
433    
434          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 340  extern int section_list_pool_init(const Line 439  extern int section_list_pool_init(const
439                  return -3;                  return -3;
440          }          }
441    
442          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          // Allocate shared memory
443            size = sizeof(SECTION_LIST_POOL);
444          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
445          if (shmid == -1)          if (shmid == -1)
446          {          {
447                  log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno);                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
448                  return -3;                  return -3;
449          }          }
450          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 354  extern int section_list_pool_init(const Line 454  extern int section_list_pool_init(const
454                  return -3;                  return -3;
455          }          }
456    
         section_list_pool_shmid = shmid;  
457          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
458          section_list_count = 0;          p_section_list_pool->shmid = shmid;
459            p_section_list_pool->section_count = 0;
460    
461          p_trie_dict_section_list = trie_dict_create();          // Allocate semaphore as section locks
462          if (p_trie_dict_section_list == NULL)          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
463            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
464            if (semid == -1)
465            {
466                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
467                    return -3;
468            }
469    
470            // Initialize sem value to 0
471            arg.val = 0;
472            for (i = 0; i < size; i++)
473            {
474                    if (semctl(semid, i, SETVAL, arg) == -1)
475                    {
476                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
477                            return -3;
478                    }
479            }
480    
481            p_section_list_pool->semid = semid;
482    
483            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
484            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
485          {          {
486                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
487                  return -2;                  return -2;
488          }          }
489    
490            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
491            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
492            {
493                    log_error("trie_dict_create() OOM\n", BBS_max_section);
494                    return -2;
495            }
496    
497            return 0;
498    }
499    
500    void section_list_cleanup(void)
501    {
502            int shmid;
503    
504            if (p_section_list_pool == NULL)
505            {
506                    return;
507            }
508    
509            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
510            {
511                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
512                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
513            }
514    
515            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
516            {
517                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
518                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
519            }
520    
521            shmid = p_section_list_pool->shmid;
522    
523            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
524            {
525                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
526            }
527    
528            if (shmdt(p_section_list_pool) == -1)
529            {
530                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
531            }
532    
533            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
534            {
535                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
536            }
537    
538            p_section_list_pool = NULL;
539    }
540    
541    int set_section_list_shm_readonly(void)
542    {
543            int shmid;
544            void *p_shm;
545    
546            if (p_section_list_pool == NULL)
547            {
548                    log_error("p_section_list_pool not initialized\n");
549                    return -1;
550            }
551    
552            shmid = p_section_list_pool->shmid;
553    
554            // Remap shared memory in read-only mode
555            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
556            if (p_shm == (void *)-1)
557            {
558                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
559                    return -3;
560            }
561    
562            p_section_list_pool = p_shm;
563    
564          return 0;          return 0;
565  }  }
566    
567  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  int detach_section_list_shm(void)
568    {
569            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
570            {
571                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
572                    return -1;
573            }
574    
575            p_section_list_pool = NULL;
576    
577            return 0;
578    }
579    
580    inline static void sid_to_str(int32_t sid, char *p_sid_str)
581    {
582            uint32_t u_sid;
583            int i;
584    
585            u_sid = (uint32_t)sid;
586            for (i = 0; i < SID_STR_LEN - 1; i++)
587            {
588                    p_sid_str[i] = (char)(u_sid % 255 + 1);
589                    u_sid /= 255;
590            }
591            p_sid_str[i] = '\0';
592    }
593    
594    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
595  {  {
596          SECTION_LIST *p_section;          SECTION_LIST *p_section;
597            char sid_str[SID_STR_LEN];
598    
599          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
600          {          {
601                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
602                  return NULL;                  return NULL;
603          }          }
604    
605          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
606          {          {
607                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);                  log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
608                  return NULL;                  return NULL;
609          }          }
610    
611          p_section = p_section_list_pool + section_list_count;          sid_to_str(sid, sid_str);
612    
613            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
614    
615          p_section->sid = sid;          p_section->sid = sid;
616            p_section->ex_menu_tm = 0;
617    
618            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
619            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
620    
621          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
622          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
623    
624          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
625          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
626    
627          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
628          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          {
629                    log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
630                    return NULL;
631            }
632    
633          if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
634          {          {
635                  log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count);                  log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
636                  return NULL;                  return NULL;
637          }          }
638    
639          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
640    
641          section_list_count++;          p_section_list_pool->section_count++;
642    
643          return p_section;          return p_section;
644  }  }
645    
646    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
647    {
648            int64_t index;
649    
650            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
651            {
652                    log_error("NULL pointer error\n");
653                    return -1;
654            }
655    
656            index = get_section_index(p_section);
657    
658            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
659            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
660    
661            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
662            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
663    
664            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
665            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
666    
667            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
668            {
669                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
670                    return -2;
671            }
672    
673            return 0;
674    }
675    
676  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
677  {  {
678          p_section->article_count = 0;          p_section->article_count = 0;
# Line 421  void section_list_reset_articles(SECTION Line 684  void section_list_reset_articles(SECTION
684    
685          p_section->page_count = 0;          p_section->page_count = 0;
686          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
687    
688            p_section->ontop_article_count = 0;
689  }  }
690    
691  void section_list_pool_cleanup(void)  SECTION_LIST *section_list_find_by_name(const char *sname)
692  {  {
693          if (p_trie_dict_section_list != NULL)          int64_t index;
694            int ret;
695    
696            if (p_section_list_pool == NULL)
697          {          {
698                  trie_dict_destroy(p_trie_dict_section_list);                  log_error("section_list not initialized\n");
699                  p_trie_dict_section_list = NULL;                  return NULL;
700          }          }
701    
702          if (p_section_list_pool != NULL)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
703            if (ret < 0)
704          {          {
705                  if (shmdt(p_section_list_pool) == -1)                  log_error("trie_dict_get(section, %s) error\n", sname);
706                  {                  return NULL;
707                          log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);          }
708                  }          else if (ret == 0)
709                  p_section_list_pool = NULL;          {
710                    return NULL;
                 if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
711          }          }
712    
713          section_list_count = 0;          return (p_section_list_pool->sections + index);
714  }  }
715    
716  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
717  {  {
718          int64_t index;          int64_t index;
719            int ret;
720            char sid_str[SID_STR_LEN];
721    
722          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
723          {          {
724                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
725                  return NULL;                  return NULL;
726          }          }
727    
728          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
729    
730            ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
731            if (ret < 0)
732            {
733                    log_error("trie_dict_get(section, %d) error\n", sid);
734                    return NULL;
735            }
736            else if (ret == 0)
737          {          {
                 log_error("trie_dict_get(section_data, %s) error\n", sname);  
738                  return NULL;                  return NULL;
739          }          }
740    
741          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
742  }  }
743    
744  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 477  int section_list_append_article(SECTION_ Line 751  int section_list_append_article(SECTION_
751    
752          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
753          {          {
754                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
755                  return -1;                  return -1;
756          }          }
757    
# Line 487  int section_list_append_article(SECTION_ Line 761  int section_list_append_article(SECTION_
761                  return -1;                  return -1;
762          }          }
763    
764            if (p_section->sid != p_article_src->sid)
765            {
766                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
767                    return -2;
768            }
769    
770          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
771          {          {
772                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);                  log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
# Line 494  int section_list_append_article(SECTION_ Line 774  int section_list_append_article(SECTION_
774          }          }
775    
776          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
777                  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)
778          {          {
779                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
780                  {                  {
# Line 504  int section_list_append_article(SECTION_ Line 784  int section_list_append_article(SECTION_
784    
785                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
786                  {                  {
787                          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;
788                  }                  }
789    
790                  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 519  int section_list_append_article(SECTION_ Line 799  int section_list_append_article(SECTION_
799          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
800          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
801          {          {
802                  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);
803                  return -3;                  return -3;
804          }          }
805    
# Line 583  int section_list_append_article(SECTION_ Line 863  int section_list_append_article(SECTION_
863          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
864    
865          // Update page          // Update page
866          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) ||
867                  p_section->article_count == 1)                  p_section->page_count == 0)
868          {          {
869                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
870                  p_section->page_count++;                  p_section->page_count++;
# Line 596  int section_list_append_article(SECTION_ Line 876  int section_list_append_article(SECTION_
876                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
877          }          }
878    
879            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
880            {
881                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
882                                      p_section->sid, p_article->aid);
883                    return -5;
884            }
885    
886          return 0;          return 0;
887  }  }
888    
# Line 607  int section_list_set_article_visible(SEC Line 894  int section_list_set_article_visible(SEC
894    
895          if (p_section == NULL)          if (p_section == NULL)
896          {          {
897                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
898                  return -2;                  return -1;
899          }          }
900    
901          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 617  int section_list_set_article_visible(SEC Line 904  int section_list_set_article_visible(SEC
904                  return -1; // Not found                  return -1; // Not found
905          }          }
906    
907            if (p_section->sid != p_article->sid)
908            {
909                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
910                    return -2;
911            }
912    
913          if (p_article->visible == visible)          if (p_article->visible == visible)
914          {          {
915                  return 0; // Already set                  return 0; // Already set
# Line 626  int section_list_set_article_visible(SEC Line 919  int section_list_set_article_visible(SEC
919          {          {
920                  p_section->visible_article_count--;                  p_section->visible_article_count--;
921    
922                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
923                    {
924                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
925                    }
926    
927                  if (p_article->tid == 0)                  if (p_article->tid == 0)
928                  {                  {
929                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 644  int section_list_set_article_visible(SEC Line 942  int section_list_set_article_visible(SEC
942                                          p_reply->visible = 0;                                          p_reply->visible = 0;
943                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
944                                          affected_count++;                                          affected_count++;
945    
946                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
947                                            {
948                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
949                                            }
950                                  }                                  }
951                          }                          }
952                  }                  }
# Line 656  int section_list_set_article_visible(SEC Line 959  int section_list_set_article_visible(SEC
959                  {                  {
960                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
961                  }                  }
962    
963                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
964                    {
965                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
966                    }
967          }          }
968    
969          p_article->visible = visible;          p_article->visible = visible;
# Line 664  int section_list_set_article_visible(SEC Line 972  int section_list_set_article_visible(SEC
972          return affected_count;          return affected_count;
973  }  }
974    
975    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
976    {
977            int i;
978    
979            if (p_section == NULL || p_article == NULL)
980            {
981                    log_error("NULL pointer error\n");
982                    return -1;
983            }
984    
985            if (p_section->sid != p_article->sid)
986            {
987                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
988                    return -2;
989            }
990    
991            if (p_article->ontop)
992            {
993                    for (i = 0; i < p_section->ontop_article_count; i++)
994                    {
995                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
996                            {
997                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
998                                    return 0;
999                            }
1000                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1001                            {
1002                                    break;
1003                            }
1004                    }
1005    
1006                    // Remove the oldest one if the array of ontop articles is full
1007                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1008                    {
1009                            if (i == 0) // p_article is the oldest one
1010                            {
1011                                    return 0;
1012                            }
1013                            memmove((void *)(p_section->p_ontop_articles),
1014                                            (void *)(p_section->p_ontop_articles + 1),
1015                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1016                            p_section->ontop_article_count--;
1017                            i--;
1018                    }
1019                    else
1020                    {
1021                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1022                                            (void *)(p_section->p_ontop_articles + i),
1023                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1024                    }
1025    
1026                    p_section->p_ontop_articles[i] = p_article;
1027                    p_section->ontop_article_count++;
1028    
1029                    // TODO: debug
1030            }
1031            else // ontop == 0
1032            {
1033                    for (i = 0; i < p_section->ontop_article_count; i++)
1034                    {
1035                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1036                            {
1037                                    break;
1038                            }
1039                    }
1040                    if (i == p_section->ontop_article_count) // not found
1041                    {
1042                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1043                            return 0;
1044                    }
1045    
1046                    memmove((void *)(p_section->p_ontop_articles + i),
1047                                    (void *)(p_section->p_ontop_articles + i + 1),
1048                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1049                    p_section->ontop_article_count--;
1050            }
1051    
1052            return 0;
1053    }
1054    
1055    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1056    {
1057            int page_count;
1058    
1059            if (p_section == NULL)
1060            {
1061                    log_error("NULL pointer error\n");
1062                    return -1;
1063            }
1064    
1065            page_count = p_section->page_count - 1 +
1066                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) /
1067                                             BBS_article_limit_per_page;
1068    
1069            if (page_count < 0)
1070            {
1071                    page_count = 0;
1072            }
1073    
1074            return page_count;
1075    }
1076    
1077    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1078    {
1079            if (p_section == NULL)
1080            {
1081                    log_error("NULL pointer error\n");
1082                    return -1;
1083            }
1084    
1085            if (page_id < p_section->page_count - 1)
1086            {
1087                    return BBS_article_limit_per_page;
1088            }
1089            else // if (page_id >= p_section->page_count - 1)
1090            {
1091                    return MIN(MAX(0,
1092                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1093                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1094                                       BBS_article_limit_per_page);
1095            }
1096    }
1097    
1098  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
1099  {  {
1100          ARTICLE *p_article;          ARTICLE *p_article;
# Line 677  ARTICLE *section_list_find_article_with_ Line 1108  ARTICLE *section_list_find_article_with_
1108    
1109          if (p_section == NULL)          if (p_section == NULL)
1110          {          {
1111                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1112                  return NULL;                  return NULL;
1113          }          }
1114    
# Line 689  ARTICLE *section_list_find_article_with_ Line 1120  ARTICLE *section_list_find_article_with_
1120          }          }
1121    
1122          left = 0;          left = 0;
1123          right = p_section->page_count;          right = p_section->page_count - 1;
1124    
1125          // 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] ]
1126          while (left < right - 1)          while (left < right)
1127          {          {
1128                  // 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
1129                  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;  
                 }  
1130    
1131                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1132                  {                  {
1133                          right = mid;                          right = mid - 1;
1134                  }                  }
1135                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1136                  {                  {
1137                          left = mid;                          left = mid;
1138                  }                  }
# Line 769  int section_list_calculate_page(SECTION_ Line 1194  int section_list_calculate_page(SECTION_
1194    
1195          if (p_section == NULL)          if (p_section == NULL)
1196          {          {
1197                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1198                  return -1;                  return -1;
1199          }          }
1200    
# Line 783  int section_list_calculate_page(SECTION_ Line 1208  int section_list_calculate_page(SECTION_
1208    
1209          if (start_aid > 0)          if (start_aid > 0)
1210          {          {
1211                    p_article = article_block_find_by_aid(start_aid);
1212                    if (p_article == NULL)
1213                    {
1214                            return -1; // Not found
1215                    }
1216    
1217                    if (p_section->sid != p_article->sid)
1218                    {
1219                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1220                            return -2;
1221                    }
1222    
1223                  p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);                  p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1224                  if (p_article == NULL)                  if (p_article == NULL)
1225                  {                  {
# Line 846  int section_list_calculate_page(SECTION_ Line 1283  int section_list_calculate_page(SECTION_
1283          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1284    
1285          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1286          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1287                                                                                                              ? visible_article_count
1288                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1289    
1290          return 0;          return 0;
1291  }  }
1292    
1293  int section_list_count_of_topic_articles(int32_t aid)  int32_t article_block_last_aid(void)
1294    {
1295            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1296            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1297    
1298            return last_aid;
1299    }
1300    
1301    int article_block_article_count(void)
1302    {
1303            int ret;
1304    
1305            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1306            {
1307                    return -1;
1308            }
1309    
1310            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1311                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1312    
1313            return ret;
1314    }
1315    
1316    int article_count_of_topic(int32_t aid)
1317  {  {
1318          ARTICLE *p_article;          ARTICLE *p_article;
1319          int article_count;          int article_count;
# Line 866  int section_list_count_of_topic_articles Line 1328  int section_list_count_of_topic_articles
1328    
1329          do          do
1330          {          {
1331                    if (p_article->tid != 0 && p_article->tid != aid)
1332                    {
1333                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1334                            break;
1335                    }
1336    
1337                  article_count++;                  article_count++;
1338                  p_article = p_article->p_topic_next;                  p_article = p_article->p_topic_next;
1339          } while (p_article->aid != aid);          } while (p_article->aid != aid);
# Line 885  int section_list_move_topic(SECTION_LIST Line 1353  int section_list_move_topic(SECTION_LIST
1353          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1354          int move_counter;          int move_counter;
1355    
1356          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1357            {
1358                    log_error("NULL pointer error\n");
1359                    return -1;
1360            }
1361    
1362            if (p_section_src->sid == p_section_dest->sid)
1363          {          {
1364                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() src and dest section are the same\n");
1365                  return -1;                  return -1;
1366          }          }
1367    
1368          if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1369            {
1370                    log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1371                    return -2;
1372            }
1373    
1374            if (p_section_src->sid != p_article->sid)
1375          {          {
1376                  log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid);                  log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1377                                      p_section_src->sid, p_article->aid, p_article->sid);
1378                  return -2;                  return -2;
1379          }          }
1380    
# Line 905  int section_list_move_topic(SECTION_LIST Line 1386  int section_list_move_topic(SECTION_LIST
1386    
1387          last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);          last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1388    
1389          move_article_count = section_list_count_of_topic_articles(aid);          move_article_count = article_count_of_topic(aid);
1390          if (move_article_count <= 0)          if (move_article_count <= 0)
1391          {          {
1392                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1393                  return -2;                  return -2;
1394          }          }
1395    
# Line 925  int section_list_move_topic(SECTION_LIST Line 1406  int section_list_move_topic(SECTION_LIST
1406    
1407          do          do
1408          {          {
1409                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (p_section_src->sid != p_article->sid)
1410                  {                  {
1411                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1412                          return -4;                                            p_section_src->sid, p_article->aid, p_article->sid);
1413                            p_article = p_article->p_topic_next;
1414                            continue;
1415                  }                  }
1416    
1417                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 949  int section_list_move_topic(SECTION_LIST Line 1432  int section_list_move_topic(SECTION_LIST
1432                  p_article->p_prior->p_next = p_article->p_next;                  p_article->p_prior->p_next = p_article->p_next;
1433                  p_article->p_next->p_prior = p_article->p_prior;                  p_article->p_next->p_prior = p_article->p_prior;
1434    
1435                    // Update sid of article
1436                    p_article->sid = p_section_dest->sid;
1437    
1438                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1439                    {
1440                            log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1441                            p_article = p_article->p_topic_next;
1442                            continue;
1443                    }
1444    
1445                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
1446                  if (p_next == NULL) // empty section                  if (p_next == NULL) // empty section
1447                  {                  {
# Line 1014  int section_list_move_topic(SECTION_LIST Line 1507  int section_list_move_topic(SECTION_LIST
1507                          // Re-calculate pages of desc section                          // Re-calculate pages of desc section
1508                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                          if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1509                          {                          {
1510                                  log_error("section_list_calculate_page(section = %d, aid = %d) error\n",                                  log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1511                                                    p_section_dest->sid, first_inserted_aid_dest);                                                    p_section_dest->sid, first_inserted_aid_dest);
1512                          }                          }
1513    
# Line 1024  int section_list_move_topic(SECTION_LIST Line 1517  int section_list_move_topic(SECTION_LIST
1517    
1518          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1519          {          {
1520                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1521                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1522          }          }
1523    
1524          // Re-calculate pages of src section          // Re-calculate pages of src section
1525          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)          if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1526          {          {
1527                  log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n",                  log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1528                                    p_section_src->sid, last_unaffected_aid_src, aid);                                    p_section_src->sid, last_unaffected_aid_src, aid);
1529          }          }
1530    
# Line 1040  int section_list_move_topic(SECTION_LIST Line 1533  int section_list_move_topic(SECTION_LIST
1533                  // Re-calculate pages of desc section                  // Re-calculate pages of desc section
1534                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)                  if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1535                  {                  {
1536                          log_error("section_list_calculate_page(section = %d, aid = %d) error\n",                          log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1537                                            p_section_dest->sid, first_inserted_aid_dest);                                            p_section_dest->sid, first_inserted_aid_dest);
1538                  }                  }
1539          }          }
1540    
1541          return move_article_count;          return move_article_count;
1542  }  }
1543    
1544    int get_section_index(SECTION_LIST *p_section)
1545    {
1546            int index;
1547    
1548            if (p_section_list_pool == NULL)
1549            {
1550                    log_error("get_section_index() error: uninitialized\n");
1551                    return -1;
1552            }
1553    
1554            if (p_section == NULL)
1555            {
1556                    index = BBS_max_section;
1557            }
1558            else
1559            {
1560                    index = (int)(p_section - p_section_list_pool->sections);
1561                    if (index < 0 || index >= BBS_max_section)
1562                    {
1563                            log_error("get_section_index(%d) error: index out of range\n", index);
1564                            return -2;
1565                    }
1566            }
1567    
1568            return index;
1569    }
1570    
1571    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1572    {
1573            if (p_section == NULL)
1574            {
1575                    log_error("NULL pointer error\n");
1576                    return -1;
1577            }
1578    
1579            if (section_list_rd_lock(p_section) < 0)
1580            {
1581                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1582                    return -2;
1583            }
1584    
1585            if (sname != NULL)
1586            {
1587                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1588            }
1589            if (stitle != NULL)
1590            {
1591                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1592            }
1593            if (master_list != NULL)
1594            {
1595                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1596            }
1597    
1598            // release lock of section
1599            if (section_list_rd_unlock(p_section) < 0)
1600            {
1601                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1602                    return -2;
1603            }
1604    
1605            return 0;
1606    }
1607    
1608    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1609    {
1610            int index;
1611            struct sembuf sops[4];
1612    #if !defined(__MSYS__) && !defined(__MINGW32__)
1613            struct timespec timeout;
1614    #endif
1615            int ret;
1616    
1617            index = get_section_index(p_section);
1618            if (index < 0)
1619            {
1620                    return -2;
1621            }
1622    
1623            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1624            sops[0].sem_op = 0;                                                                // wait until unlocked
1625            sops[0].sem_flg = 0;
1626    
1627            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1628            sops[1].sem_op = 1;                                                        // lock
1629            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1630    
1631            // Read lock on any specific section will also acquire single read lock on "all section"
1632            // so that write lock on all section only need to acquire single write on on "all section"
1633            // rather than to acquire multiple write locks on all the available sections.
1634            if (index != BBS_max_section)
1635            {
1636                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1637                    sops[2].sem_op = 0;                                                // wait until unlocked
1638                    sops[2].sem_flg = 0;
1639    
1640                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1641                    sops[3].sem_op = 1;                                        // lock
1642                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1643            }
1644    
1645    #if defined(__MSYS__) || defined(__MINGW32__)
1646            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4));
1647    #else
1648            timeout.tv_sec = wait_sec;
1649            timeout.tv_nsec = 0;
1650    
1651            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1652    #endif
1653            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1654            {
1655                    log_error("semop(index = %d, lock read) error %d\n", index, errno);
1656            }
1657    
1658            return ret;
1659    }
1660    
1661    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1662    {
1663            int index;
1664            struct sembuf sops[3];
1665    #if !defined(__MSYS__) && !defined(__MINGW32__)
1666            struct timespec timeout;
1667    #endif
1668            int ret;
1669    
1670            index = get_section_index(p_section);
1671            if (index < 0)
1672            {
1673                    return -2;
1674            }
1675    
1676            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1677            sops[0].sem_op = 0;                                                                // wait until unlocked
1678            sops[0].sem_flg = 0;
1679    
1680            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1681            sops[1].sem_op = 1;                                                                // lock
1682            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1683    
1684            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1685            sops[2].sem_op = 0;                                                        // wait until unlocked
1686            sops[2].sem_flg = 0;
1687    
1688    #if defined(__MSYS__) || defined(__MINGW32__)
1689            ret = semop(p_section_list_pool->semid, sops, 3);
1690    #else
1691            timeout.tv_sec = wait_sec;
1692            timeout.tv_nsec = 0;
1693    
1694            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1695    #endif
1696            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1697            {
1698                    log_error("semop(index = %d, lock write) error %d\n", index, errno);
1699            }
1700    
1701            return ret;
1702    }
1703    
1704    int section_list_rd_unlock(SECTION_LIST *p_section)
1705    {
1706            int index;
1707            struct sembuf sops[2];
1708            int ret;
1709    
1710            index = get_section_index(p_section);
1711            if (index < 0)
1712            {
1713                    return -2;
1714            }
1715    
1716            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1717            sops[0].sem_op = -1;                                               // unlock
1718            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1719    
1720            if (index != BBS_max_section)
1721            {
1722                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1723                    sops[1].sem_op = -1;                                     // unlock
1724                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1725            }
1726    
1727            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1728            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1729            {
1730                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1731            }
1732    
1733            return ret;
1734    }
1735    
1736    int section_list_rw_unlock(SECTION_LIST *p_section)
1737    {
1738            int index;
1739            struct sembuf sops[1];
1740            int ret;
1741    
1742            index = get_section_index(p_section);
1743            if (index < 0)
1744            {
1745                    return -2;
1746            }
1747    
1748            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1749            sops[0].sem_op = -1;                                                       // unlock
1750            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1751    
1752            ret = semop(p_section_list_pool->semid, sops, 1);
1753            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1754            {
1755                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1756            }
1757    
1758            return ret;
1759    }
1760    
1761    int section_list_rd_lock(SECTION_LIST *p_section)
1762    {
1763            int timer = 0;
1764            int sid = (p_section == NULL ? 0 : p_section->sid);
1765            int ret = -1;
1766    
1767            while (!SYS_server_exit)
1768            {
1769                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1770                    if (ret == 0) // success
1771                    {
1772                            break;
1773                    }
1774                    else if (errno == EAGAIN || errno == EINTR) // retry
1775                    {
1776                            timer++;
1777                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1778                            {
1779                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1780                            }
1781                    }
1782                    else // failed
1783                    {
1784                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1785                            break;
1786                    }
1787            }
1788    
1789            return ret;
1790    }
1791    
1792    int section_list_rw_lock(SECTION_LIST *p_section)
1793    {
1794            int timer = 0;
1795            int sid = (p_section == NULL ? 0 : p_section->sid);
1796            int ret = -1;
1797    
1798            while (!SYS_server_exit)
1799            {
1800                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1801                    if (ret == 0) // success
1802                    {
1803                            break;
1804                    }
1805                    else if (errno == EAGAIN || errno == EINTR) // retry
1806                    {
1807                            timer++;
1808                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1809                            {
1810                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1811                            }
1812                    }
1813                    else // failed
1814                    {
1815                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1816                            break;
1817                    }
1818            }
1819    
1820            return ret;
1821    }


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

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