/[LeafOK_CVS]/lbbs/src/section_list.c
ViewVC logotype

Annotation of /lbbs/src/section_list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (hide annotations)
Tue Oct 14 00:57:06 2025 UTC (5 months ago) by sysadm
Branch: MAIN
Changes since 1.40: +2 -12 lines
Content type: text/x-csrc
Revert change to section_list

1 sysadm 1.1 /***************************************************************************
2     section_list.c - description
3     -------------------
4     Copyright : (C) 2004-2025 by Leaflet
5     Email : leaflet@leafok.com
6     ***************************************************************************/
7    
8     /***************************************************************************
9     * *
10     * This program is free software; you can redistribute it and/or modify *
11     * it under the terms of the GNU General Public License as published by *
12     * the Free Software Foundation; either version 3 of the License, or *
13     * (at your option) any later version. *
14     * *
15     ***************************************************************************/
16    
17 sysadm 1.34 #include "log.h"
18 sysadm 1.1 #include "section_list.h"
19     #include "trie_dict.h"
20 sysadm 1.34 #include <errno.h>
21     #include <signal.h>
22 sysadm 1.1 #include <stdio.h>
23 sysadm 1.34 #include <stdlib.h>
24 sysadm 1.1 #include <string.h>
25     #include <unistd.h>
26 sysadm 1.34 #include <sys/ipc.h>
27 sysadm 1.7 #include <sys/param.h>
28 sysadm 1.16 #include <sys/sem.h>
29 sysadm 1.1 #include <sys/shm.h>
30    
31 sysadm 1.17 #ifdef _SEM_SEMUN_UNDEFINED
32     union semun
33     {
34     int val; /* Value for SETVAL */
35     struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
36     unsigned short *array; /* Array for GETALL, SETALL */
37     struct seminfo *__buf; /* Buffer for IPC_INFO
38     (Linux-specific) */
39     };
40     #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42 sysadm 1.23 #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
43     #define SECTION_TRY_LOCK_TIMES 10
44    
45 sysadm 1.35 #define ARTICLE_BLOCK_PER_SHM 1000 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
46 sysadm 1.33 #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
47 sysadm 1.7 #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
48    
49 sysadm 1.31 #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections
50 sysadm 1.12
51 sysadm 1.14 #define SID_STR_LEN 5 // 32-bit + NULL
52    
53 sysadm 1.7 struct article_block_t
54     {
55     ARTICLE articles[ARTICLE_PER_BLOCK];
56 sysadm 1.22 int article_count;
57 sysadm 1.7 struct article_block_t *p_next_block;
58     };
59     typedef struct article_block_t ARTICLE_BLOCK;
60 sysadm 1.1
61 sysadm 1.22 struct article_block_pool_t
62 sysadm 1.1 {
63     int shmid;
64 sysadm 1.22 struct
65     {
66     int shmid;
67     void *p_shm;
68     } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
69 sysadm 1.7 int shm_count;
70     ARTICLE_BLOCK *p_block_free_list;
71     ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
72 sysadm 1.22 int block_count;
73 sysadm 1.7 };
74     typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
75    
76     static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77 sysadm 1.1
78 sysadm 1.38 SECTION_LIST_POOL *p_section_list_pool = NULL;
79 sysadm 1.7
80     int article_block_init(const char *filename, int block_count)
81 sysadm 1.1 {
82     int shmid;
83     int proj_id;
84     key_t key;
85     size_t size;
86     void *p_shm;
87     int i;
88 sysadm 1.7 int block_count_in_shm;
89     ARTICLE_BLOCK *p_block_in_shm;
90     ARTICLE_BLOCK **pp_block_next;
91    
92     if (p_article_block_pool != NULL)
93 sysadm 1.1 {
94 sysadm 1.7 log_error("article_block_pool already initialized\n");
95 sysadm 1.1 return -1;
96     }
97    
98 sysadm 1.27 if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
99 sysadm 1.1 {
100 sysadm 1.7 log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
101 sysadm 1.1 return -2;
102     }
103    
104 sysadm 1.18 // Allocate shared memory
105     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("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
110     return -3;
111     }
112    
113     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 sysadm 1.1 {
123 sysadm 1.18 log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
124     return -3;
125 sysadm 1.1 }
126    
127 sysadm 1.18 p_article_block_pool = p_shm;
128 sysadm 1.22 p_article_block_pool->shmid = shmid;
129 sysadm 1.18
130 sysadm 1.7 p_article_block_pool->shm_count = 0;
131     pp_block_next = &(p_article_block_pool->p_block_free_list);
132 sysadm 1.1
133 sysadm 1.7 while (block_count > 0)
134 sysadm 1.1 {
135 sysadm 1.7 block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
136     block_count -= block_count_in_shm;
137 sysadm 1.1
138 sysadm 1.18 proj_id = p_article_block_pool->shm_count;
139 sysadm 1.1 key = ftok(filename, proj_id);
140     if (key == -1)
141     {
142     log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
143 sysadm 1.7 article_block_cleanup();
144 sysadm 1.1 return -3;
145     }
146    
147 sysadm 1.19 size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
148 sysadm 1.1 shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
149     if (shmid == -1)
150     {
151 sysadm 1.7 log_error("shmget(shm_index = %d, size = %d) error (%d)\n", p_article_block_pool->shm_count, size, errno);
152     article_block_cleanup();
153 sysadm 1.1 return -3;
154     }
155     p_shm = shmat(shmid, NULL, 0);
156     if (p_shm == (void *)-1)
157     {
158     log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
159 sysadm 1.7 article_block_cleanup();
160 sysadm 1.1 return -3;
161     }
162    
163 sysadm 1.7 (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->shmid = shmid;
164     (p_article_block_pool->shm_pool + p_article_block_pool->shm_count)->p_shm = p_shm;
165     p_article_block_pool->shm_count++;
166 sysadm 1.1
167 sysadm 1.7 p_block_in_shm = p_shm;
168     *pp_block_next = p_block_in_shm;
169 sysadm 1.1
170 sysadm 1.7 for (i = 0; i < block_count_in_shm; i++)
171 sysadm 1.1 {
172 sysadm 1.7 if (i < block_count_in_shm - 1)
173 sysadm 1.1 {
174 sysadm 1.7 (p_block_in_shm + i)->p_next_block = (p_block_in_shm + i + 1);
175 sysadm 1.1 }
176     else
177     {
178 sysadm 1.7 (p_block_in_shm + i)->p_next_block = NULL;
179     pp_block_next = &((p_block_in_shm + i)->p_next_block);
180 sysadm 1.1 }
181     }
182     }
183    
184 sysadm 1.7 p_article_block_pool->block_count = 0;
185    
186 sysadm 1.1 return 0;
187     }
188    
189 sysadm 1.7 void article_block_cleanup(void)
190     {
191 sysadm 1.22 int shmid;
192    
193 sysadm 1.18 if (p_article_block_pool == NULL)
194     {
195     return;
196     }
197    
198     for (int i = 0; i < p_article_block_pool->shm_count; i++)
199 sysadm 1.7 {
200 sysadm 1.18 if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
201 sysadm 1.7 {
202 sysadm 1.18 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
203     }
204 sysadm 1.7
205 sysadm 1.18 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);
208 sysadm 1.7 }
209 sysadm 1.18 }
210    
211 sysadm 1.22 shmid = p_article_block_pool->shmid;
212    
213 sysadm 1.18 if (shmdt(p_article_block_pool) == -1)
214     {
215 sysadm 1.22 log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216 sysadm 1.18 }
217 sysadm 1.7
218 sysadm 1.22 if (shmctl(shmid, IPC_RMID, NULL) == -1)
219 sysadm 1.18 {
220 sysadm 1.22 log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221 sysadm 1.7 }
222 sysadm 1.18
223     p_article_block_pool = NULL;
224 sysadm 1.7 }
225    
226 sysadm 1.26 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     if (shmdt(p_article_block_pool) == -1)
275     {
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 sysadm 1.7 inline static ARTICLE_BLOCK *pop_free_article_block(void)
286     {
287     ARTICLE_BLOCK *p_block = NULL;
288    
289     if (p_article_block_pool->p_block_free_list != NULL)
290     {
291     p_block = p_article_block_pool->p_block_free_list;
292     p_article_block_pool->p_block_free_list = p_block->p_next_block;
293     p_block->p_next_block = NULL;
294     p_block->article_count = 0;
295     }
296    
297     return p_block;
298     }
299    
300     inline static void push_free_article_block(ARTICLE_BLOCK *p_block)
301 sysadm 1.1 {
302 sysadm 1.7 p_block->p_next_block = p_article_block_pool->p_block_free_list;
303     p_article_block_pool->p_block_free_list = p_block;
304     }
305    
306     int article_block_reset(void)
307     {
308     ARTICLE_BLOCK *p_block;
309    
310     if (p_article_block_pool == NULL)
311     {
312     log_error("article_block_pool not initialized\n");
313     return -1;
314     }
315 sysadm 1.1
316 sysadm 1.7 while (p_article_block_pool->block_count > 0)
317 sysadm 1.1 {
318 sysadm 1.7 p_article_block_pool->block_count--;
319     p_block = p_article_block_pool->p_block[p_article_block_pool->block_count];
320     push_free_article_block(p_block);
321 sysadm 1.1 }
322    
323 sysadm 1.7 return 0;
324     }
325    
326     ARTICLE *article_block_find_by_aid(int32_t aid)
327     {
328     ARTICLE_BLOCK *p_block;
329     int left;
330     int right;
331     int mid;
332    
333     if (p_article_block_pool == NULL)
334 sysadm 1.1 {
335 sysadm 1.7 log_error("article_block_pool not initialized\n");
336     return NULL;
337 sysadm 1.1 }
338    
339 sysadm 1.7 if (p_article_block_pool->block_count == 0) // empty
340 sysadm 1.1 {
341 sysadm 1.7 return NULL;
342 sysadm 1.1 }
343    
344 sysadm 1.7 left = 0;
345     right = p_article_block_pool->block_count;
346    
347     // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]
348     while (left < right - 1)
349 sysadm 1.1 {
350 sysadm 1.7 // get block offset no less than mid value of left and right block offsets
351     mid = (left + right) / 2 + (right - left) % 2;
352    
353     if (mid >= p_article_block_pool->block_count)
354 sysadm 1.1 {
355 sysadm 1.7 log_error("block(mid = %d) is out of boundary\n", mid);
356     return NULL;
357     }
358 sysadm 1.1
359 sysadm 1.7 if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
360     {
361     right = mid;
362     }
363     else
364     {
365     left = mid;
366 sysadm 1.1 }
367 sysadm 1.7 }
368    
369     p_block = p_article_block_pool->p_block[left];
370    
371     left = 0;
372     right = p_block->article_count - 1;
373    
374     // aid in the range [ aid of articles[left], aid of articles[right] ]
375     while (left < right)
376     {
377     mid = (left + right) / 2;
378 sysadm 1.1
379 sysadm 1.7 if (aid <= p_block->articles[mid].aid)
380     {
381     right = mid;
382     }
383     else
384     {
385     left = mid + 1;
386     }
387 sysadm 1.1 }
388 sysadm 1.7
389 sysadm 1.28 if (aid != p_block->articles[left].aid) // not found
390     {
391     return NULL;
392     }
393    
394     return (p_block->articles + left); // found
395 sysadm 1.1 }
396    
397 sysadm 1.7 ARTICLE *article_block_find_by_index(int index)
398 sysadm 1.1 {
399 sysadm 1.7 ARTICLE_BLOCK *p_block;
400    
401     if (p_article_block_pool == NULL)
402     {
403     log_error("article_block_pool not initialized\n");
404     return NULL;
405     }
406    
407     if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)
408     {
409 sysadm 1.14 log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count);
410 sysadm 1.7 return NULL;
411     }
412    
413     p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];
414 sysadm 1.1
415 sysadm 1.7 if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
416 sysadm 1.1 {
417 sysadm 1.14 log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count);
418 sysadm 1.7 return NULL;
419 sysadm 1.1 }
420    
421 sysadm 1.7 return (p_block->articles + (index % ARTICLE_PER_BLOCK));
422 sysadm 1.1 }
423    
424 sysadm 1.19 extern int section_list_init(const char *filename)
425 sysadm 1.1 {
426 sysadm 1.16 int semid;
427 sysadm 1.13 int shmid;
428     int proj_id;
429     key_t key;
430     size_t size;
431     void *p_shm;
432 sysadm 1.17 union semun arg;
433     int i;
434 sysadm 1.13
435 sysadm 1.22 if (p_section_list_pool != NULL)
436 sysadm 1.13 {
437 sysadm 1.19 section_list_cleanup();
438 sysadm 1.13 }
439 sysadm 1.7
440 sysadm 1.13 proj_id = (int)(time(NULL) % getpid());
441     key = ftok(filename, proj_id);
442     if (key == -1)
443     {
444     log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
445     return -3;
446     }
447 sysadm 1.1
448 sysadm 1.22 // Allocate shared memory
449     size = sizeof(SECTION_LIST_POOL);
450     shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
451     if (shmid == -1)
452     {
453     log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
454     return -3;
455     }
456     p_shm = shmat(shmid, NULL, 0);
457     if (p_shm == (void *)-1)
458     {
459     log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
460     return -3;
461     }
462    
463     p_section_list_pool = p_shm;
464     p_section_list_pool->shmid = shmid;
465     p_section_list_pool->section_count = 0;
466    
467     // Allocate semaphore as section locks
468 sysadm 1.16 size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
469     semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
470     if (semid == -1)
471     {
472 sysadm 1.17 log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
473 sysadm 1.16 return -3;
474     }
475    
476 sysadm 1.17 // Initialize sem value to 0
477     arg.val = 0;
478     for (i = 0; i < size; i++)
479     {
480     if (semctl(semid, i, SETVAL, arg) == -1)
481     {
482     log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
483     return -3;
484     }
485     }
486    
487 sysadm 1.22 p_section_list_pool->semid = semid;
488 sysadm 1.16
489 sysadm 1.22 p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
490     if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
491 sysadm 1.14 {
492     log_error("trie_dict_create() OOM\n", BBS_max_section);
493     return -2;
494     }
495    
496 sysadm 1.22 p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
497     if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
498 sysadm 1.1 {
499 sysadm 1.13 log_error("trie_dict_create() OOM\n", BBS_max_section);
500     return -2;
501     }
502    
503     return 0;
504     }
505    
506 sysadm 1.26 void section_list_cleanup(void)
507     {
508     int shmid;
509    
510     if (p_section_list_pool == NULL)
511     {
512     return;
513     }
514    
515     if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
516     {
517     trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
518     p_section_list_pool->p_trie_dict_section_by_name = NULL;
519     }
520    
521     if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
522     {
523     trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
524     p_section_list_pool->p_trie_dict_section_by_sid = NULL;
525     }
526    
527     shmid = p_section_list_pool->shmid;
528    
529     if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
530     {
531     log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
532     }
533    
534     if (shmdt(p_section_list_pool) == -1)
535     {
536     log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
537     }
538    
539     if (shmctl(shmid, IPC_RMID, NULL) == -1)
540     {
541     log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
542     }
543    
544     p_section_list_pool = NULL;
545     }
546    
547     int set_section_list_shm_readonly(void)
548     {
549     int shmid;
550     void *p_shm;
551    
552     if (p_section_list_pool == NULL)
553     {
554     log_error("p_section_list_pool not initialized\n");
555     return -1;
556     }
557    
558     shmid = p_section_list_pool->shmid;
559    
560     // Remap shared memory in read-only mode
561     p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
562     if (p_shm == (void *)-1)
563     {
564     log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
565     return -3;
566     }
567    
568     p_section_list_pool = p_shm;
569    
570     return 0;
571     }
572    
573     int detach_section_list_shm(void)
574     {
575     if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
576     {
577     log_error("shmdt(section_list_pool) error (%d)\n", errno);
578     return -1;
579     }
580    
581     p_section_list_pool = NULL;
582    
583     return 0;
584     }
585    
586 sysadm 1.14 inline static void sid_to_str(int32_t sid, char *p_sid_str)
587     {
588     uint32_t u_sid;
589     int i;
590    
591     u_sid = (uint32_t)sid;
592     for (i = 0; i < SID_STR_LEN - 1; i++)
593     {
594     p_sid_str[i] = (char)(u_sid % 255 + 1);
595     u_sid /= 255;
596     }
597     p_sid_str[i] = '\0';
598     }
599    
600 sysadm 1.30 SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
601 sysadm 1.13 {
602     SECTION_LIST *p_section;
603 sysadm 1.14 char sid_str[SID_STR_LEN];
604 sysadm 1.13
605 sysadm 1.22 if (p_section_list_pool == NULL)
606 sysadm 1.13 {
607     log_error("session_list_pool not initialized\n");
608     return NULL;
609 sysadm 1.1 }
610    
611 sysadm 1.22 if (p_section_list_pool->section_count >= BBS_max_section)
612 sysadm 1.1 {
613 sysadm 1.22 log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
614 sysadm 1.1 return NULL;
615     }
616    
617 sysadm 1.14 sid_to_str(sid, sid_str);
618    
619 sysadm 1.22 p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
620 sysadm 1.1
621 sysadm 1.11 p_section->sid = sid;
622 sysadm 1.36 p_section->ex_menu_tm = 0;
623 sysadm 1.11
624 sysadm 1.29 strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
625     p_section->sname[sizeof(p_section->sname) - 1] = '\0';
626 sysadm 1.1
627 sysadm 1.29 strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
628     p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
629 sysadm 1.1
630 sysadm 1.30 strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
631 sysadm 1.29 p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
632 sysadm 1.1
633 sysadm 1.22 if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
634 sysadm 1.14 {
635 sysadm 1.22 log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count);
636 sysadm 1.14 return NULL;
637     }
638    
639 sysadm 1.22 if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
640 sysadm 1.1 {
641 sysadm 1.22 log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
642 sysadm 1.1 return NULL;
643     }
644    
645 sysadm 1.7 section_list_reset_articles(p_section);
646    
647 sysadm 1.22 p_section_list_pool->section_count++;
648 sysadm 1.1
649     return p_section;
650     }
651    
652 sysadm 1.7 void section_list_reset_articles(SECTION_LIST *p_section)
653 sysadm 1.1 {
654 sysadm 1.7 p_section->article_count = 0;
655 sysadm 1.8 p_section->topic_count = 0;
656     p_section->visible_article_count = 0;
657     p_section->visible_topic_count = 0;
658 sysadm 1.7 p_section->p_article_head = NULL;
659     p_section->p_article_tail = NULL;
660 sysadm 1.1
661 sysadm 1.7 p_section->page_count = 0;
662 sysadm 1.8 p_section->last_page_visible_article_count = 0;
663 sysadm 1.35
664     p_section->ontop_article_count = 0;
665 sysadm 1.7 }
666 sysadm 1.1
667 sysadm 1.41 SECTION_LIST *section_list_find_by_name(const char *sname)
668 sysadm 1.1 {
669     int64_t index;
670 sysadm 1.24 int ret;
671 sysadm 1.1
672 sysadm 1.22 if (p_section_list_pool == NULL)
673 sysadm 1.14 {
674     log_error("section_list not initialized\n");
675     return NULL;
676     }
677    
678 sysadm 1.24 ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
679     if (ret < 0)
680 sysadm 1.14 {
681     log_error("trie_dict_get(section, %s) error\n", sname);
682     return NULL;
683     }
684 sysadm 1.24 else if (ret == 0)
685     {
686     return NULL;
687     }
688 sysadm 1.14
689 sysadm 1.22 return (p_section_list_pool->sections + index);
690 sysadm 1.14 }
691    
692 sysadm 1.41 SECTION_LIST *section_list_find_by_sid(int32_t sid)
693 sysadm 1.14 {
694     int64_t index;
695 sysadm 1.24 int ret;
696 sysadm 1.14 char sid_str[SID_STR_LEN];
697    
698 sysadm 1.22 if (p_section_list_pool == NULL)
699 sysadm 1.1 {
700 sysadm 1.7 log_error("section_list not initialized\n");
701 sysadm 1.1 return NULL;
702     }
703    
704 sysadm 1.14 sid_to_str(sid, sid_str);
705    
706 sysadm 1.24 ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
707     if (ret < 0)
708 sysadm 1.1 {
709 sysadm 1.14 log_error("trie_dict_get(section, %d) error\n", sid);
710 sysadm 1.1 return NULL;
711     }
712 sysadm 1.24 else if (ret == 0)
713     {
714     return NULL;
715     }
716 sysadm 1.1
717 sysadm 1.22 return (p_section_list_pool->sections + index);
718 sysadm 1.1 }
719    
720 sysadm 1.7 int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src)
721 sysadm 1.1 {
722     ARTICLE_BLOCK *p_block;
723     int32_t last_aid = 0;
724 sysadm 1.6 ARTICLE *p_article;
725 sysadm 1.1 ARTICLE *p_topic_head;
726     ARTICLE *p_topic_tail;
727    
728 sysadm 1.6 if (p_section == NULL || p_article_src == NULL)
729 sysadm 1.1 {
730 sysadm 1.7 log_error("section_list_append_article() NULL pointer error\n");
731 sysadm 1.1 return -1;
732     }
733    
734 sysadm 1.7 if (p_article_block_pool == NULL)
735 sysadm 1.1 {
736 sysadm 1.7 log_error("article_block_pool not initialized\n");
737 sysadm 1.1 return -1;
738     }
739    
740 sysadm 1.14 if (p_section->sid != p_article_src->sid)
741     {
742     log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
743     return -2;
744     }
745    
746 sysadm 1.9 if (p_section->article_count >= BBS_article_limit_per_section)
747     {
748     log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
749     return -2;
750     }
751    
752 sysadm 1.7 if (p_article_block_pool->block_count == 0 ||
753     p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)
754 sysadm 1.1 {
755     if ((p_block = pop_free_article_block()) == NULL)
756     {
757     log_error("pop_free_article_block() error\n");
758     return -2;
759     }
760    
761 sysadm 1.7 if (p_article_block_pool->block_count > 0)
762 sysadm 1.1 {
763 sysadm 1.7 last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;
764 sysadm 1.1 }
765 sysadm 1.7
766     p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
767     p_article_block_pool->block_count++;
768 sysadm 1.1 }
769     else
770     {
771 sysadm 1.7 p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
772     last_aid = p_block->articles[p_block->article_count - 1].aid;
773 sysadm 1.1 }
774    
775     // AID of articles should be strictly ascending
776 sysadm 1.6 if (p_article_src->aid <= last_aid)
777 sysadm 1.1 {
778 sysadm 1.14 log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid);
779 sysadm 1.1 return -3;
780     }
781    
782 sysadm 1.7 p_article = (p_block->articles + p_block->article_count);
783     p_block->article_count++;
784     p_section->article_count++;
785    
786     // Copy article data
787     *p_article = *p_article_src;
788 sysadm 1.1
789 sysadm 1.8 if (p_article->visible)
790     {
791     p_section->visible_article_count++;
792     }
793    
794 sysadm 1.7 // Link appended article as tail node of topic bi-directional list
795     if (p_article->tid != 0)
796 sysadm 1.1 {
797 sysadm 1.7 p_topic_head = article_block_find_by_aid(p_article->tid);
798 sysadm 1.1 if (p_topic_head == NULL)
799     {
800 sysadm 1.7 log_error("search head of topic (aid=%d) error\n", p_article->tid);
801 sysadm 1.1 return -4;
802     }
803    
804 sysadm 1.6 p_topic_tail = p_topic_head->p_topic_prior;
805 sysadm 1.1 if (p_topic_tail == NULL)
806     {
807 sysadm 1.7 log_error("tail of topic (aid=%d) is NULL\n", p_article->tid);
808 sysadm 1.1 return -4;
809     }
810     }
811     else
812     {
813 sysadm 1.8 p_section->topic_count++;
814    
815     if (p_article->visible)
816     {
817     p_section->visible_topic_count++;
818     }
819    
820 sysadm 1.7 p_topic_head = p_article;
821     p_topic_tail = p_article;
822 sysadm 1.1 }
823    
824 sysadm 1.7 p_article->p_topic_prior = p_topic_tail;
825     p_article->p_topic_next = p_topic_head;
826     p_topic_head->p_topic_prior = p_article;
827     p_topic_tail->p_topic_next = p_article;
828 sysadm 1.1
829 sysadm 1.6 // Link appended article as tail node of article bi-directional list
830     if (p_section->p_article_head == NULL)
831     {
832     p_section->p_article_head = p_article;
833     p_section->p_article_tail = p_article;
834     }
835     p_article->p_prior = p_section->p_article_tail;
836     p_article->p_next = p_section->p_article_head;
837     p_section->p_article_head->p_prior = p_article;
838     p_section->p_article_tail->p_next = p_article;
839     p_section->p_article_tail = p_article;
840    
841 sysadm 1.7 // Update page
842 sysadm 1.11 if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||
843     p_section->article_count == 1)
844 sysadm 1.1 {
845 sysadm 1.7 p_section->p_page_first_article[p_section->page_count] = p_article;
846     p_section->page_count++;
847 sysadm 1.8 p_section->last_page_visible_article_count = 0;
848 sysadm 1.1 }
849 sysadm 1.9
850     if (p_article->visible)
851     {
852     p_section->last_page_visible_article_count++;
853     }
854 sysadm 1.1
855 sysadm 1.35 if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
856     {
857     log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
858     p_section->sid, p_article->aid);
859     return -5;
860     }
861    
862 sysadm 1.7 return 0;
863 sysadm 1.2 }
864    
865 sysadm 1.7 int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible)
866 sysadm 1.1 {
867     ARTICLE *p_article;
868 sysadm 1.8 ARTICLE *p_reply;
869     int affected_count = 0;
870 sysadm 1.1
871 sysadm 1.2 if (p_section == NULL)
872     {
873 sysadm 1.35 log_error("NULL pointer error\n");
874     return -1;
875 sysadm 1.2 }
876    
877 sysadm 1.7 p_article = article_block_find_by_aid(aid);
878 sysadm 1.1 if (p_article == NULL)
879     {
880     return -1; // Not found
881     }
882    
883 sysadm 1.14 if (p_section->sid != p_article->sid)
884     {
885 sysadm 1.35 log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
886 sysadm 1.14 return -2;
887     }
888    
889 sysadm 1.7 if (p_article->visible == visible)
890 sysadm 1.1 {
891 sysadm 1.7 return 0; // Already set
892 sysadm 1.1 }
893    
894 sysadm 1.8 if (visible == 0) // 1 -> 0
895     {
896     p_section->visible_article_count--;
897    
898     if (p_article->tid == 0)
899     {
900     p_section->visible_topic_count--;
901    
902     // Set related visible replies to invisible
903     for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next)
904     {
905     if (p_reply->tid != aid)
906     {
907     log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);
908     continue;
909     }
910    
911     if (p_reply->visible == 1)
912     {
913     p_reply->visible = 0;
914     p_section->visible_article_count--;
915     affected_count++;
916     }
917     }
918     }
919     }
920     else // 0 -> 1
921     {
922     p_section->visible_article_count++;
923    
924     if (p_article->tid == 0)
925     {
926     p_section->visible_topic_count++;
927     }
928     }
929    
930 sysadm 1.7 p_article->visible = visible;
931 sysadm 1.8 affected_count++;
932    
933     return affected_count;
934     }
935    
936 sysadm 1.35 int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
937     {
938     int i;
939    
940     if (p_section == NULL || p_article == NULL)
941     {
942     log_error("NULL pointer error\n");
943     return -1;
944     }
945    
946     if (p_section->sid != p_article->sid)
947     {
948     log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
949     return -2;
950     }
951    
952     if (p_article->ontop)
953     {
954     for (i = 0; i < p_section->ontop_article_count; i++)
955     {
956     if (p_section->p_ontop_articles[i]->aid == p_article->aid)
957     {
958     log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
959     return 0;
960     }
961     else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
962     {
963     break;
964     }
965     }
966    
967     // Remove the oldest one if the array of ontop articles is full
968     if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
969     {
970     if (i == 0) // p_article is the oldest one
971     {
972     return 0;
973     }
974     memmove((void *)(p_section->p_ontop_articles),
975     (void *)(p_section->p_ontop_articles + 1),
976     sizeof(ARTICLE *) * (size_t)(i - 1));
977     p_section->ontop_article_count--;
978     i--;
979     }
980     else
981     {
982     memmove((void *)(p_section->p_ontop_articles + i + 1),
983     (void *)(p_section->p_ontop_articles + i),
984     sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
985     }
986    
987     p_section->p_ontop_articles[i] = p_article;
988     p_section->ontop_article_count++;
989    
990     // TODO: debug
991     }
992     else // ontop == 0
993     {
994     for (i = 0; i < p_section->ontop_article_count; i++)
995     {
996     if (p_section->p_ontop_articles[i]->aid == p_article->aid)
997     {
998     break;
999     }
1000     }
1001     if (i == p_section->ontop_article_count) // not found
1002     {
1003     log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1004     return 0;
1005     }
1006    
1007     memmove((void *)(p_section->p_ontop_articles + i),
1008     (void *)(p_section->p_ontop_articles + i + 1),
1009     sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1010     p_section->ontop_article_count--;
1011     }
1012    
1013     return 0;
1014     }
1015    
1016     int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1017     {
1018     int page_count;
1019    
1020     if (p_section == NULL)
1021     {
1022     log_error("NULL pointer error\n");
1023     return -1;
1024     }
1025    
1026 sysadm 1.39 page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1027 sysadm 1.35 (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1028     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1029    
1030     return page_count;
1031     }
1032    
1033     int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1034     {
1035     if (p_section == NULL)
1036     {
1037     log_error("NULL pointer error\n");
1038     return -1;
1039     }
1040    
1041     if (page_id < p_section->page_count - 1)
1042     {
1043     return BBS_article_limit_per_page;
1044     }
1045     else // if (page_id >= p_section->page_count - 1)
1046     {
1047     return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1048     BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1049     }
1050     }
1051    
1052 sysadm 1.11 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)
1053 sysadm 1.8 {
1054     ARTICLE *p_article;
1055     int left;
1056     int right;
1057     int mid;
1058    
1059     *p_page = -1;
1060     *p_offset = -1;
1061 sysadm 1.11 *pp_next = NULL;
1062 sysadm 1.8
1063     if (p_section == NULL)
1064     {
1065     log_error("section_list_find_article_with_offset() NULL pointer error\n");
1066     return NULL;
1067     }
1068 sysadm 1.7
1069 sysadm 1.8 if (p_section->article_count == 0) // empty
1070     {
1071     *p_page = 0;
1072     *p_offset = 0;
1073     return NULL;
1074     }
1075    
1076     left = 0;
1077     right = p_section->page_count;
1078    
1079     // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]
1080     while (left < right - 1)
1081     {
1082     // get page id no less than mid value of left page id and right page id
1083     mid = (left + right) / 2 + (right - left) % 2;
1084 sysadm 1.1
1085 sysadm 1.8 if (mid >= p_section->page_count)
1086     {
1087     log_error("page id (mid = %d) is out of boundary\n", mid);
1088     return NULL;
1089     }
1090    
1091     if (aid < p_section->p_page_first_article[mid]->aid)
1092     {
1093     right = mid;
1094     }
1095     else
1096     {
1097     left = mid;
1098     }
1099     }
1100    
1101     *p_page = left;
1102    
1103     p_article = p_section->p_page_first_article[*p_page];
1104    
1105     // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
1106 sysadm 1.11 right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
1107 sysadm 1.8
1108     // left will be the offset of article found or offset to insert
1109     left = 0;
1110    
1111 sysadm 1.10 while (aid > p_article->aid)
1112 sysadm 1.8 {
1113     p_article = p_article->p_next;
1114     left++;
1115    
1116 sysadm 1.11 if (aid == p_article->aid)
1117     {
1118     *pp_next = p_article->p_next;
1119     break;
1120     }
1121    
1122 sysadm 1.8 // over last article in the page
1123     if (p_article == p_section->p_article_head || p_article->aid >= right)
1124     {
1125 sysadm 1.11 *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
1126     *p_offset = left;
1127     return NULL; // not found
1128 sysadm 1.8 }
1129     }
1130    
1131 sysadm 1.11 if (aid < p_article->aid)
1132     {
1133     *pp_next = p_article;
1134     p_article = NULL; // not found
1135     }
1136     else // aid == p_article->aid
1137 sysadm 1.10 {
1138 sysadm 1.11 *pp_next = p_article->p_next;
1139 sysadm 1.10 }
1140    
1141 sysadm 1.8 *p_offset = left;
1142    
1143     return p_article;
1144     }
1145    
1146     int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
1147     {
1148 sysadm 1.9 ARTICLE *p_article;
1149 sysadm 1.11 ARTICLE *p_next;
1150 sysadm 1.9 int32_t page;
1151     int32_t offset;
1152     int visible_article_count;
1153     int page_head_set;
1154 sysadm 1.8
1155     if (p_section == NULL)
1156     {
1157     log_error("section_list_calculate_page() NULL pointer error\n");
1158     return -1;
1159     }
1160    
1161 sysadm 1.11 if (p_section->article_count == 0) // empty
1162     {
1163     p_section->page_count = 0;
1164     p_section->last_page_visible_article_count = 0;
1165    
1166     return 0;
1167     }
1168    
1169     if (start_aid > 0)
1170 sysadm 1.9 {
1171 sysadm 1.14 p_article = article_block_find_by_aid(start_aid);
1172     if (p_article == NULL)
1173     {
1174     return -1; // Not found
1175     }
1176    
1177     if (p_section->sid != p_article->sid)
1178     {
1179     log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
1180     return -2;
1181     }
1182    
1183 sysadm 1.11 p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
1184     if (p_article == NULL)
1185 sysadm 1.9 {
1186 sysadm 1.11 if (page < 0)
1187     {
1188     return -1;
1189     }
1190     log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
1191     start_aid, p_section->sid);
1192     return -2;
1193 sysadm 1.9 }
1194    
1195 sysadm 1.11 if (offset > 0)
1196     {
1197     p_article = p_section->p_page_first_article[page];
1198     }
1199 sysadm 1.9 }
1200 sysadm 1.11 else
1201 sysadm 1.9 {
1202 sysadm 1.11 p_article = p_section->p_article_head;
1203     page = 0;
1204     offset = 0;
1205 sysadm 1.9 }
1206    
1207     visible_article_count = 0;
1208     page_head_set = 0;
1209    
1210     do
1211     {
1212     if (!page_head_set && visible_article_count == 0)
1213     {
1214     p_section->p_page_first_article[page] = p_article;
1215     page_head_set = 1;
1216     }
1217    
1218     if (p_article->visible)
1219     {
1220     visible_article_count++;
1221     }
1222    
1223     p_article = p_article->p_next;
1224    
1225     // skip remaining invisible articles
1226     while (p_article->visible == 0 && p_article != p_section->p_article_head)
1227     {
1228     p_article = p_article->p_next;
1229     }
1230    
1231     if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
1232     {
1233     page++;
1234     visible_article_count = 0;
1235     page_head_set = 0;
1236    
1237     if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
1238     {
1239     log_error("Count of page exceed limit in section %d\n", p_section->sid);
1240     break;
1241     }
1242     }
1243     } while (p_article != p_section->p_article_head);
1244    
1245     p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1246     p_section->last_page_visible_article_count = visible_article_count;
1247    
1248 sysadm 1.8 return 0;
1249 sysadm 1.1 }
1250 sysadm 1.11
1251 sysadm 1.25 int32_t article_block_last_aid(void)
1252     {
1253     ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1254     int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1255    
1256     return last_aid;
1257     }
1258    
1259 sysadm 1.27 int article_block_article_count(void)
1260     {
1261     int ret;
1262    
1263     if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1264     {
1265     return -1;
1266     }
1267    
1268     ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1269     p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1270    
1271     return ret;
1272     }
1273    
1274 sysadm 1.14 int article_count_of_topic(int32_t aid)
1275 sysadm 1.11 {
1276     ARTICLE *p_article;
1277     int article_count;
1278    
1279     p_article = article_block_find_by_aid(aid);
1280     if (p_article == NULL)
1281     {
1282     return 0; // Not found
1283     }
1284    
1285     article_count = 0;
1286    
1287     do
1288     {
1289 sysadm 1.15 if (p_article->tid != 0 && p_article->tid != aid)
1290     {
1291     log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1292     break;
1293     }
1294    
1295 sysadm 1.11 article_count++;
1296     p_article = p_article->p_topic_next;
1297     } while (p_article->aid != aid);
1298    
1299     return article_count;
1300     }
1301    
1302     int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1303     {
1304     ARTICLE *p_article;
1305     ARTICLE *p_next;
1306     int32_t page;
1307     int32_t offset;
1308     int32_t move_article_count;
1309     int32_t dest_article_count_old;
1310     int32_t last_unaffected_aid_src;
1311 sysadm 1.12 int32_t first_inserted_aid_dest;
1312     int move_counter;
1313 sysadm 1.11
1314 sysadm 1.28 if (p_section_src == NULL || p_section_dest == NULL)
1315 sysadm 1.11 {
1316     log_error("section_list_move_topic() NULL pointer error\n");
1317     return -1;
1318     }
1319    
1320 sysadm 1.28 if (p_section_src->sid == p_section_dest->sid)
1321     {
1322     log_error("section_list_move_topic() src and dest section are the same\n");
1323     return -1;
1324     }
1325    
1326 sysadm 1.14 if ((p_article = article_block_find_by_aid(aid)) == NULL)
1327 sysadm 1.11 {
1328 sysadm 1.28 log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1329 sysadm 1.14 return -2;
1330     }
1331    
1332     if (p_section_src->sid != p_article->sid)
1333     {
1334     log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1335     p_section_src->sid, p_article->aid, p_article->sid);
1336 sysadm 1.11 return -2;
1337     }
1338    
1339     if (p_article->tid != 0)
1340     {
1341     log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1342     return -2;
1343     }
1344    
1345     last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1346    
1347 sysadm 1.14 move_article_count = article_count_of_topic(aid);
1348 sysadm 1.11 if (move_article_count <= 0)
1349     {
1350 sysadm 1.22 log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1351 sysadm 1.11 return -2;
1352     }
1353    
1354     if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1355     {
1356     log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1357     p_section_dest->article_count + move_article_count, p_section_dest->sid);
1358     return -3;
1359     }
1360    
1361     dest_article_count_old = p_section_dest->article_count;
1362 sysadm 1.12 move_counter = 0;
1363     first_inserted_aid_dest = p_article->aid;
1364 sysadm 1.11
1365     do
1366     {
1367 sysadm 1.14 if (p_section_src->sid != p_article->sid)
1368 sysadm 1.11 {
1369 sysadm 1.28 log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1370 sysadm 1.14 p_section_src->sid, p_article->aid, p_article->sid);
1371 sysadm 1.28 p_article = p_article->p_topic_next;
1372     continue;
1373 sysadm 1.11 }
1374    
1375     // Remove from bi-directional article list of src section
1376     if (p_section_src->p_article_head == p_article)
1377     {
1378     p_section_src->p_article_head = p_article->p_next;
1379     }
1380     if (p_section_src->p_article_tail == p_article)
1381     {
1382     p_section_src->p_article_tail = p_article->p_prior;
1383     }
1384     if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1385     {
1386     p_section_src->p_article_head = NULL;
1387     p_section_src->p_article_tail = NULL;
1388     }
1389    
1390     p_article->p_prior->p_next = p_article->p_next;
1391     p_article->p_next->p_prior = p_article->p_prior;
1392    
1393 sysadm 1.14 // Update sid of article
1394     p_article->sid = p_section_dest->sid;
1395    
1396     if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1397     {
1398 sysadm 1.28 log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1399     p_article = p_article->p_topic_next;
1400     continue;
1401 sysadm 1.14 }
1402    
1403 sysadm 1.11 // Insert into bi-directional article list of dest section
1404     if (p_next == NULL) // empty section
1405     {
1406     p_section_dest->p_article_head = p_article;
1407     p_section_dest->p_article_tail = p_article;
1408     p_article->p_prior = p_article;
1409     p_article->p_next = p_article;
1410     }
1411     else
1412     {
1413     if (p_section_dest->p_article_head == p_next)
1414     {
1415     if (p_article->aid < p_next->aid)
1416     {
1417     p_section_dest->p_article_head = p_article;
1418     }
1419     else // p_article->aid > p_next->aid
1420     {
1421     p_section_dest->p_article_tail = p_article;
1422     }
1423     }
1424    
1425     p_article->p_prior = p_next->p_prior;
1426     p_article->p_next = p_next;
1427     p_next->p_prior->p_next = p_article;
1428     p_next->p_prior = p_article;
1429     }
1430    
1431     // Update article / topic counter of src / desc section
1432     p_section_src->article_count--;
1433     p_section_dest->article_count++;
1434     if (p_article->tid == 0)
1435     {
1436     p_section_src->topic_count--;
1437     p_section_dest->topic_count++;
1438     }
1439    
1440     // Update visible article / topic counter of src / desc section
1441     if (p_article->visible)
1442     {
1443     p_section_src->visible_article_count--;
1444     p_section_dest->visible_article_count++;
1445     if (p_article->tid == 0)
1446     {
1447     p_section_src->visible_topic_count--;
1448     p_section_dest->visible_topic_count++;
1449     }
1450     }
1451    
1452     // Update page for empty dest section
1453     if (p_section_dest->article_count == 1)
1454     {
1455     p_section_dest->p_page_first_article[0] = p_article;
1456     p_section_dest->page_count = 1;
1457     p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1458     }
1459    
1460     p_article = p_article->p_topic_next;
1461 sysadm 1.12
1462     move_counter++;
1463     if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1464     {
1465     // Re-calculate pages of desc section
1466     if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1467     {
1468 sysadm 1.14 log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1469 sysadm 1.12 p_section_dest->sid, first_inserted_aid_dest);
1470     }
1471    
1472     first_inserted_aid_dest = p_article->aid;
1473     }
1474 sysadm 1.11 } while (p_article->aid != aid);
1475    
1476     if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1477     {
1478 sysadm 1.28 log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1479 sysadm 1.11 p_section_dest->article_count - dest_article_count_old, move_article_count);
1480     }
1481    
1482 sysadm 1.12 // Re-calculate pages of src section
1483 sysadm 1.11 if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1484     {
1485 sysadm 1.14 log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1486 sysadm 1.12 p_section_src->sid, last_unaffected_aid_src, aid);
1487 sysadm 1.11 }
1488 sysadm 1.12
1489     if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1490 sysadm 1.11 {
1491 sysadm 1.12 // Re-calculate pages of desc section
1492     if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1493     {
1494 sysadm 1.14 log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1495 sysadm 1.12 p_section_dest->sid, first_inserted_aid_dest);
1496     }
1497 sysadm 1.11 }
1498    
1499     return move_article_count;
1500     }
1501 sysadm 1.16
1502     int get_section_index(SECTION_LIST *p_section)
1503     {
1504     int index;
1505    
1506     if (p_section_list_pool == NULL)
1507     {
1508     log_error("get_section_index() error: uninitialized\n");
1509     return -1;
1510     }
1511    
1512     if (p_section == NULL)
1513     {
1514     index = BBS_max_section;
1515     }
1516     else
1517     {
1518 sysadm 1.22 index = (int)(p_section - p_section_list_pool->sections);
1519 sysadm 1.16 if (index < 0 || index >= BBS_max_section)
1520     {
1521     log_error("get_section_index(%d) error: index out of range\n", index);
1522     return -2;
1523     }
1524     }
1525    
1526     return index;
1527     }
1528    
1529     int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1530     {
1531     int index;
1532 sysadm 1.17 struct sembuf sops[4];
1533 sysadm 1.16 struct timespec timeout;
1534     int ret;
1535    
1536     index = get_section_index(p_section);
1537     if (index < 0)
1538     {
1539     return -2;
1540     }
1541    
1542 sysadm 1.17 sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1543     sops[0].sem_op = 0; // wait until unlocked
1544 sysadm 1.16 sops[0].sem_flg = 0;
1545    
1546 sysadm 1.17 sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1547     sops[1].sem_op = 1; // lock
1548     sops[1].sem_flg = SEM_UNDO; // undo on terminate
1549    
1550     // Read lock on any specific section will also acquire single read lock on "all section"
1551     // so that write lock on all section only need to acquire single write on on "all section"
1552     // rather than to acquire multiple write locks on all the available sections.
1553 sysadm 1.21 if (index != BBS_max_section)
1554 sysadm 1.17 {
1555     sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1556     sops[2].sem_op = 0; // wait until unlocked
1557     sops[2].sem_flg = 0;
1558    
1559     sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1560     sops[3].sem_op = 1; // lock
1561     sops[3].sem_flg = SEM_UNDO; // undo on terminate
1562     }
1563 sysadm 1.16
1564     timeout.tv_sec = wait_sec;
1565     timeout.tv_nsec = 0;
1566    
1567 sysadm 1.22 ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1568 sysadm 1.17 if (ret == -1 && errno != EAGAIN && errno != EINTR)
1569     {
1570     log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1571     }
1572 sysadm 1.16
1573     return ret;
1574     }
1575    
1576     int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1577     {
1578     int index;
1579     struct sembuf sops[3];
1580     struct timespec timeout;
1581     int ret;
1582    
1583     index = get_section_index(p_section);
1584     if (index < 0)
1585     {
1586     return -2;
1587     }
1588    
1589 sysadm 1.17 sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1590     sops[0].sem_op = 0; // wait until unlocked
1591 sysadm 1.16 sops[0].sem_flg = 0;
1592    
1593 sysadm 1.17 sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1594     sops[1].sem_op = 1; // lock
1595     sops[1].sem_flg = SEM_UNDO; // undo on terminate
1596    
1597     sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1598     sops[2].sem_op = 0; // wait until unlocked
1599     sops[2].sem_flg = 0;
1600 sysadm 1.16
1601     timeout.tv_sec = wait_sec;
1602     timeout.tv_nsec = 0;
1603    
1604 sysadm 1.22 ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1605 sysadm 1.17 if (ret == -1 && errno != EAGAIN && errno != EINTR)
1606     {
1607     log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1608     }
1609 sysadm 1.16
1610     return ret;
1611     }
1612    
1613     int section_list_rd_unlock(SECTION_LIST *p_section)
1614     {
1615     int index;
1616 sysadm 1.17 struct sembuf sops[2];
1617 sysadm 1.16 int ret;
1618    
1619     index = get_section_index(p_section);
1620     if (index < 0)
1621     {
1622     return -2;
1623     }
1624    
1625 sysadm 1.17 sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1626     sops[0].sem_op = -1; // unlock
1627     sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1628 sysadm 1.16
1629 sysadm 1.21 if (index != BBS_max_section)
1630 sysadm 1.17 {
1631     sops[1].sem_num = BBS_max_section * 2; // r_sem of all section
1632     sops[1].sem_op = -1; // unlock
1633     sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1634     }
1635    
1636 sysadm 1.22 ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1637 sysadm 1.17 if (ret == -1 && errno != EAGAIN && errno != EINTR)
1638     {
1639     log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1640     }
1641 sysadm 1.16
1642     return ret;
1643     }
1644    
1645     int section_list_rw_unlock(SECTION_LIST *p_section)
1646     {
1647     int index;
1648     struct sembuf sops[1];
1649     int ret;
1650    
1651     index = get_section_index(p_section);
1652     if (index < 0)
1653     {
1654     return -2;
1655     }
1656    
1657 sysadm 1.17 sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1658     sops[0].sem_op = -1; // unlock
1659     sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1660 sysadm 1.16
1661 sysadm 1.22 ret = semop(p_section_list_pool->semid, sops, 1);
1662 sysadm 1.17 if (ret == -1 && errno != EAGAIN && errno != EINTR)
1663     {
1664     log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1665     }
1666 sysadm 1.16
1667     return ret;
1668     }
1669 sysadm 1.23
1670     int section_list_rd_lock(SECTION_LIST *p_section)
1671     {
1672     int timer = 0;
1673     int sid = (p_section == NULL ? 0 : p_section->sid);
1674     int ret = -1;
1675    
1676     while (!SYS_server_exit)
1677     {
1678     ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1679     if (ret == 0) // success
1680     {
1681     break;
1682     }
1683     else if (errno == EAGAIN || errno == EINTR) // retry
1684     {
1685     timer++;
1686     if (timer % SECTION_TRY_LOCK_TIMES == 0)
1687     {
1688 sysadm 1.37 log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer);
1689 sysadm 1.23 }
1690     }
1691     else // failed
1692     {
1693 sysadm 1.37 log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1694 sysadm 1.23 break;
1695     }
1696     }
1697    
1698     return ret;
1699     }
1700    
1701     int section_list_rw_lock(SECTION_LIST *p_section)
1702     {
1703     int timer = 0;
1704     int sid = (p_section == NULL ? 0 : p_section->sid);
1705     int ret = -1;
1706    
1707     while (!SYS_server_exit)
1708     {
1709     ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1710     if (ret == 0) // success
1711     {
1712     break;
1713     }
1714     else if (errno == EAGAIN || errno == EINTR) // retry
1715     {
1716     timer++;
1717     if (timer % SECTION_TRY_LOCK_TIMES == 0)
1718     {
1719 sysadm 1.37 log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer);
1720 sysadm 1.23 }
1721     }
1722     else // failed
1723     {
1724 sysadm 1.37 log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1725 sysadm 1.23 break;
1726     }
1727     }
1728    
1729     return ret;
1730     }

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