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

Contents of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Mon Jun 16 14:30:44 2025 UTC (9 months ago) by sysadm
Branch: MAIN
Changes since 1.21: +4 -4 lines
Content type: text/x-csrc
Change gcc std to c17
Update header macro accordingly

1 /***************************************************************************
2 section_list_loader.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 #define _POSIX_C_SOURCE 200809L
18
19 #include "section_list_loader.h"
20 #include "article_cache.h"
21 #include "bbs.h"
22 #include "log.h"
23 #include "database.h"
24 #include "menu.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <unistd.h>
33
34 int section_list_loader_pid;
35 int last_article_op_log_mid;
36
37 int load_section_config_from_db(void)
38 {
39 MYSQL *db;
40 MYSQL_RES *rs, *rs2;
41 MYSQL_ROW row, row2;
42 char sql[SQL_BUFFER_LEN];
43 int32_t sid;
44 char master_list[(BBS_username_max_len + 1) * 3 + 1];
45 SECTION_LIST *p_section;
46 int ret;
47
48 db = db_open();
49 if (db == NULL)
50 {
51 log_error("db_open() error: %s\n", mysql_error(db));
52 return -2;
53 }
54
55 snprintf(sql, sizeof(sql),
56 "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
57 "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "
58 "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "
59 "ORDER BY section_config.SID");
60
61 if (mysql_query(db, sql) != 0)
62 {
63 log_error("Query section_list error: %s\n", mysql_error(db));
64 return -3;
65 }
66 if ((rs = mysql_store_result(db)) == NULL)
67 {
68 log_error("Get section_list data failed\n");
69 return -3;
70 }
71
72 ret = 0;
73 while ((row = mysql_fetch_row(rs)))
74 {
75 sid = atoi(row[0]);
76
77 // Query section master
78 snprintf(sql, sizeof(sql),
79 "SELECT username FROM section_master "
80 "INNER JOIN user_list ON section_master.UID = user_list.UID "
81 "WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
82 "ORDER BY major DESC, begin_dt ASC LIMIT 3",
83 sid);
84
85 if (mysql_query(db, sql) != 0)
86 {
87 log_error("Query section_master error: %s\n", mysql_error(db));
88 ret = -3;
89 break;
90 }
91 if ((rs2 = mysql_store_result(db)) == NULL)
92 {
93 log_error("Get section_master data failed\n");
94 ret = -3;
95 break;
96 }
97
98 master_list[0] = '\0';
99 while ((row2 = mysql_fetch_row(rs2)))
100 {
101 strncat(master_list, row2[0], sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
102 strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
103 }
104 mysql_free_result(rs2);
105
106 p_section = section_list_find_by_sid(sid);
107
108 if (p_section == NULL)
109 {
110 p_section = section_list_create(sid, row[1], row[2], master_list);
111 if (p_section == NULL)
112 {
113 log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);
114 ret = -4;
115 break;
116 }
117
118 // acquire rw lock
119 ret = section_list_rw_lock(p_section);
120 if (ret < 0)
121 {
122 break;
123 }
124 }
125 else
126 {
127 // acquire rw lock
128 ret = section_list_rw_lock(p_section);
129 if (ret < 0)
130 {
131 break;
132 }
133
134 strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
135 p_section->sname[sizeof(p_section->sname) - 1] = '\0';
136 strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
137 p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
138 strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
139 p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
140 }
141
142 p_section->class_id = atoi(row[3]);
143 p_section->read_user_level = atoi(row[4]);
144 p_section->write_user_level = atoi(row[5]);
145 p_section->enable = (int8_t)atoi(row[6]);
146
147 // release rw lock
148 ret = section_list_rw_unlock(p_section);
149 if (ret < 0)
150 {
151 break;
152 }
153 }
154 mysql_free_result(rs);
155
156 mysql_close(db);
157
158 return ret;
159 }
160
161 int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
162 {
163 MYSQL *db;
164 MYSQL_RES *rs;
165 MYSQL_ROW row;
166 char sql[SQL_BUFFER_LEN];
167 ARTICLE article;
168 ARTICLE *p_topic;
169 SECTION_LIST *p_section = NULL;
170 int32_t last_sid = 0;
171 char sub_ip[IP_ADDR_LEN];
172 int article_count = 0;
173 int ret = 0;
174 int i;
175
176 db = db_open();
177 if (db == NULL)
178 {
179 log_error("db_open() error: %s\n", mysql_error(db));
180 return -2;
181 }
182
183 snprintf(sql, sizeof(sql),
184 "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
185 "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
186 "sub_ip, bbs_content.content "
187 "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
188 "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
189 start_aid, article_count_limit);
190
191 if (mysql_query(db, sql) != 0)
192 {
193 log_error("Query article list error: %s\n", mysql_error(db));
194 return -3;
195 }
196 if ((rs = mysql_use_result(db)) == NULL)
197 {
198 log_error("Get article list data failed\n");
199 return -3;
200 }
201
202 // acquire global lock
203 if (global_lock)
204 {
205 if ((ret = section_list_rw_lock(NULL)) < 0)
206 {
207 log_error("section_list_rw_lock(sid = 0) error\n");
208 goto cleanup;
209 }
210 }
211
212 while ((row = mysql_fetch_row(rs)))
213 {
214 memset(&article, 0, sizeof(ARTICLE));
215
216 // copy data of article
217 i = 0;
218
219 article.aid = atoi(row[i++]);
220 article.tid = atoi(row[i++]);
221 article.sid = atoi(row[i++]);
222 article.cid = atoi(row[i++]);
223 article.uid = atoi(row[i++]);
224 article.visible = (int8_t)atoi(row[i++]);
225 article.excerption = (int8_t)atoi(row[i++]);
226 article.ontop = (int8_t)atoi(row[i++]);
227 article.lock = (int8_t)atoi(row[i++]);
228 article.transship = (int8_t)atoi(row[i++]);
229
230 strncpy(article.username, row[i++], sizeof(article.username) - 1);
231 article.username[sizeof(article.username) - 1] = '\0';
232 strncpy(article.nickname, row[i++], sizeof(article.nickname) - 1);
233 article.nickname[sizeof(article.nickname) - 1] = '\0';
234 strncpy(article.title, row[i++], sizeof(article.title) - 1);
235 article.title[sizeof(article.title) - 1] = '\0';
236
237 article.sub_dt = atol(row[i++]);
238
239 strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
240 sub_ip[sizeof(sub_ip) - 1] = '\0';
241 ip_mask(sub_ip, 1, '*');
242
243 // release lock of last section if different from current one
244 if (!global_lock && article.sid != last_sid && last_sid != 0)
245 {
246 if ((ret = section_list_rw_unlock(p_section)) < 0)
247 {
248 log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
249 break;
250 }
251 }
252
253 if ((p_section = section_list_find_by_sid(article.sid)) == NULL)
254 {
255 log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid);
256 ret = ERR_UNKNOWN_SECTION; // Unknown section found
257 break;
258 }
259
260 if (article.visible != 0 && article.tid != 0)
261 {
262 // Check if topic article is visible
263 p_topic = article_block_find_by_aid(article.tid);
264 if (p_topic == NULL || p_topic->visible == 0)
265 {
266 // log_error("Set article (aid = %d) as invisible due to invisible or non-existing topic head\n", article.aid);
267 article.tid = 0;
268 article.visible = 0;
269 }
270 }
271
272 // acquire lock of current section if different from last one
273 if (!global_lock && article.sid != last_sid)
274 {
275 if ((ret = section_list_rw_lock(p_section)) < 0)
276 {
277 log_error("section_list_rw_lock(sid=0) error\n");
278 break;
279 }
280 }
281
282 // append article to section list
283 last_sid = article.sid;
284
285 if (section_list_append_article(p_section, &article) < 0)
286 {
287 log_error("section_list_append_article(sid=%d, aid=%d) error\n",
288 p_section->sid, article.aid);
289 ret = -3;
290 break;
291 }
292
293 article_count++;
294
295 if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
296 {
297 log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
298 ret = -4;
299 break;
300 }
301 }
302
303 // release lock of last section
304 if (!global_lock && last_sid != 0)
305 {
306 if ((ret = section_list_rw_unlock(p_section)) < 0)
307 {
308 log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
309 }
310 }
311
312 // release global lock
313 if (global_lock)
314 {
315 if ((ret = section_list_rw_unlock(NULL)) < 0)
316 {
317 log_error("section_list_rw_unlock(sid=0) error\n");
318 }
319 }
320
321 cleanup:
322 mysql_free_result(rs);
323
324 mysql_close(db);
325
326 return (ret < 0 ? ret : article_count);
327 }
328
329 int set_last_article_op_log_from_db(void)
330 {
331 MYSQL *db;
332 MYSQL_RES *rs;
333 MYSQL_ROW row;
334 char sql[SQL_BUFFER_LEN];
335
336 db = db_open();
337 if (db == NULL)
338 {
339 log_error("db_open() error: %s\n", mysql_error(db));
340 return -1;
341 }
342
343 snprintf(sql, sizeof(sql),
344 "SELECT MID FROM bbs_article_op ORDER BY MID DESC LIMIT 1");
345
346 if (mysql_query(db, sql) != 0)
347 {
348 log_error("Query article op error: %s\n", mysql_error(db));
349 return -2;
350 }
351 if ((rs = mysql_store_result(db)) == NULL)
352 {
353 log_error("Get article op data failed\n");
354 return -2;
355 }
356
357 if ((row = mysql_fetch_row(rs)))
358 {
359 last_article_op_log_mid = atoi(row[0]);
360 }
361
362 mysql_free_result(rs);
363
364 mysql_close(db);
365
366 return last_article_op_log_mid;
367 }
368
369 int apply_article_op_log_from_db(int op_count_limit)
370 {
371 MYSQL *db;
372 MYSQL_RES *rs, *rs2;
373 MYSQL_ROW row, row2;
374 char sql[SQL_BUFFER_LEN];
375 ARTICLE *p_article;
376 SECTION_LIST *p_section = NULL;
377 SECTION_LIST *p_section_dest;
378 char sub_ip[IP_ADDR_LEN];
379 int32_t last_sid = 0;
380 int32_t sid_dest;
381 int op_count = 0;
382 int ret = 0;
383
384 db = db_open();
385 if (db == NULL)
386 {
387 log_error("db_open() error: %s\n", mysql_error(db));
388 return -3;
389 }
390
391 snprintf(sql, sizeof(sql),
392 "SELECT MID, AID, type FROM bbs_article_op "
393 "WHERE MID > %d AND type NOT IN ('A') "
394 "ORDER BY MID LIMIT %d",
395 last_article_op_log_mid, op_count_limit);
396
397 if (mysql_query(db, sql) != 0)
398 {
399 log_error("Query article log error: %s\n", mysql_error(db));
400 return -3;
401 }
402 if ((rs = mysql_store_result(db)) == NULL)
403 {
404 log_error("Get article log data failed\n");
405 return -3;
406 }
407
408 while ((row = mysql_fetch_row(rs)))
409 {
410 p_article = article_block_find_by_aid(atoi(row[1]));
411 if (p_article == NULL) // related article has not been appended yet
412 {
413 ret = -2;
414 break;
415 }
416
417 // release lock of last section if different from current one
418 if (p_article->sid != last_sid && last_sid != 0)
419 {
420 if ((ret = section_list_rw_unlock(p_section)) < 0)
421 {
422 log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
423 break;
424 }
425 }
426
427 if ((p_section = section_list_find_by_sid(p_article->sid)) == NULL)
428 {
429 log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", p_article->sid);
430 ret = ERR_UNKNOWN_SECTION; // Unknown section found
431 break;
432 }
433
434 // acquire lock of current section if different from last one
435 if (p_article->sid != last_sid)
436 {
437 if ((ret = section_list_rw_lock(p_section)) < 0)
438 {
439 log_error("section_list_rw_lock(sid = 0) error\n");
440 break;
441 }
442 }
443
444 last_sid = p_article->sid;
445
446 switch (row[2][0])
447 {
448 case 'A': // Add article
449 log_error("Operation type=A should not be found\n");
450 break;
451 case 'D': // Delete article
452 case 'X': // Delete article by Admin
453 if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
454 {
455 log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1]));
456 }
457 if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
458 {
459 log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
460 }
461 break;
462 case 'S': // Restore article
463 if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
464 {
465 log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
466 }
467 if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
468 {
469 log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
470 }
471 break;
472 case 'L': // Lock article
473 p_article->lock = 1;
474 break;
475 case 'U': // Unlock article
476 p_article->lock = 0;
477 break;
478 case 'M': // Modify article
479 snprintf(sql, sizeof(sql),
480 "SELECT bbs.CID, sub_ip, bbs_content.content "
481 "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
482 "WHERE bbs.AID = %d",
483 p_article->aid);
484
485 if (mysql_query(db, sql) != 0)
486 {
487 log_error("Query article error: %s\n", mysql_error(db));
488 ret = -3;
489 break;
490 }
491 if ((rs2 = mysql_use_result(db)) == NULL)
492 {
493 log_error("Get article data failed\n");
494 ret = -3;
495 break;
496 }
497 if ((row2 = mysql_fetch_row(rs2)))
498 {
499 p_article->cid = atoi(row2[0]);
500
501 strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
502 sub_ip[sizeof(sub_ip) - 1] = '\0';
503 ip_mask(sub_ip, 1, '*');
504
505 if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
506 {
507 log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
508 ret = -4;
509 }
510 }
511 else
512 {
513 p_article->cid = 0;
514 ret = -4;
515 }
516 mysql_free_result(rs2);
517
518 p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
519 break;
520 case 'T': // Move article
521 snprintf(sql, sizeof(sql),
522 "SELECT SID FROM bbs WHERE AID = %d",
523 p_article->aid);
524
525 if (mysql_query(db, sql) != 0)
526 {
527 log_error("Query article error: %s\n", mysql_error(db));
528 ret = -3;
529 break;
530 }
531 if ((rs2 = mysql_store_result(db)) == NULL)
532 {
533 log_error("Get article data failed\n");
534 ret = -3;
535 break;
536 }
537 if ((row2 = mysql_fetch_row(rs2)))
538 {
539 sid_dest = atoi(row2[0]);
540 }
541 else
542 {
543 sid_dest = 0;
544 ret = -4;
545 }
546 mysql_free_result(rs2);
547
548 if (sid_dest > 0 && sid_dest != p_article->sid)
549 {
550 p_section_dest = section_list_find_by_sid(sid_dest);
551 if (p_section_dest == NULL)
552 {
553 ret = ERR_UNKNOWN_SECTION;
554 break;
555 }
556 // acquire lock of dest section
557 if ((ret = section_list_rw_lock(p_section_dest)) < 0)
558 {
559 log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest);
560 break;
561 }
562 // Move topic
563 if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)
564 {
565 log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n",
566 p_section->sid, p_section_dest->sid, p_article->aid, ret);
567 }
568 // release lock of dest section
569 if (section_list_rw_unlock(p_section_dest) < 0)
570 {
571 log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest);
572 ret = -1;
573 }
574 }
575 break;
576 case 'E': // Set article as excerption
577 p_article->excerption = 1;
578 break;
579 case 'O': // Unset article as excerption
580 p_article->excerption = 0;
581 break;
582 case 'F': // Set article on top
583 p_article->ontop = 1;
584 break;
585 case 'V': // Unset article on top
586 p_article->ontop = 0;
587 break;
588 case 'Z': // Set article as trnasship
589 p_article->transship = 1;
590 break;
591 default:
592 // log_error("Operation type=%s unknown, mid=%s\n", row[2], row[0]);
593 break;
594 }
595
596 if (ret < 0)
597 {
598 break;
599 }
600
601 // Update MID with last successfully proceeded article_op_log
602 last_article_op_log_mid = atoi(row[0]);
603
604 op_count++;
605 }
606
607 // release lock of last section
608 if (last_sid != 0)
609 {
610 if ((ret = section_list_rw_unlock(p_section)) < 0)
611 {
612 log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
613 }
614 }
615
616 mysql_free_result(rs);
617
618 mysql_close(db);
619
620 return (ret < 0 ? ret : op_count);
621 }
622
623 int section_list_loader_launch(void)
624 {
625 int pid;
626 int ret;
627 int32_t last_aid;
628 int article_count;
629 int load_count;
630 int last_mid;
631 int i;
632
633 if (section_list_loader_pid != 0)
634 {
635 log_error("section_list_loader already running, pid = %d\n", section_list_loader_pid);
636 return -2;
637 }
638
639 pid = fork();
640
641 if (pid > 0) // Parent process
642 {
643 SYS_child_process_count++;
644 section_list_loader_pid = pid;
645 log_common("Section list loader process (pid = %d) start\n", pid);
646 return 0;
647 }
648 else if (pid < 0) // Error
649 {
650 log_error("fork() error (%d)\n", errno);
651 return -1;
652 }
653
654 // Child process
655 SYS_child_process_count = 0;
656
657 // Detach menu in shared memory
658 detach_menu_shm(p_bbs_menu);
659 free(p_bbs_menu);
660 p_bbs_menu = NULL;
661
662 // Do section data loader periodically
663 while (!SYS_server_exit)
664 {
665 if (SYS_section_list_reload)
666 {
667 SYS_section_list_reload = 0;
668
669 // Load section config
670 if (load_section_config_from_db() < 0)
671 {
672 log_error("load_section_config_from_db() error\n");
673 }
674 else
675 {
676 log_error("Reload section config successfully\n");
677 }
678 }
679
680 // Load section articles
681 article_count = article_block_article_count();
682
683 do
684 {
685 last_aid = article_block_last_aid();
686
687 if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
688 {
689 log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
690
691 if (ret == ERR_UNKNOWN_SECTION)
692 {
693 SYS_section_list_reload = 1; // Force reload section_list
694 }
695 }
696 } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
697
698 load_count = article_block_article_count() - article_count;
699 if (load_count > 0)
700 {
701 log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
702 }
703
704 if (SYS_section_list_reload)
705 {
706 continue;
707 }
708
709 // Load article_op log
710 last_mid = last_article_op_log_mid;
711
712 do
713 {
714 if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
715 {
716 log_error("apply_article_op_log_from_db() error\n");
717
718 if (ret == ERR_UNKNOWN_SECTION)
719 {
720 SYS_section_list_reload = 1; // Force reload section_list
721 }
722 }
723 } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
724
725 if (last_article_op_log_mid > last_mid)
726 {
727 log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
728 }
729
730 if (SYS_section_list_reload)
731 {
732 continue;
733 }
734
735 for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)
736 {
737 sleep(1);
738 }
739 }
740
741 // Child process exit
742
743 // Detach data pools shm
744 detach_section_list_shm();
745 detach_article_block_shm();
746 detach_trie_dict_shm();
747
748 log_common("Section list loader process exit normally\n");
749 log_end();
750
751 section_list_loader_pid = 0;
752
753 _exit(0);
754
755 return 0;
756 }
757
758 int section_list_loader_reload(void)
759 {
760 if (section_list_loader_pid == 0)
761 {
762 log_error("section_list_loader not running\n");
763 return -2;
764 }
765
766 if (kill(section_list_loader_pid, SIGHUP) < 0)
767 {
768 log_error("Send SIGTERM signal failed (%d)\n", errno);
769 return -1;
770 }
771
772 return 0;
773 }
774
775 int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
776 {
777 ARTICLE *p_article;
778 ARTICLE *p_next_page_first_article;
779 int ret = 0;
780
781 if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
782 {
783 log_error("query_section_articles() NULL pointer error\n");
784 return -1;
785 }
786
787 // acquire lock of section
788 if ((ret = section_list_rd_lock(p_section)) < 0)
789 {
790 log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
791 return -2;
792 }
793
794 *p_page_count = p_section->page_count;
795
796 if (p_section->visible_article_count == 0)
797 {
798 *p_article_count = 0;
799 }
800 else if (page_id < 0 || page_id >= p_section->page_count)
801 {
802 log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
803 ret = -3;
804 }
805 else
806 {
807 ret = page_id;
808 p_article = p_section->p_page_first_article[page_id];
809 p_next_page_first_article =
810 (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
811 *p_article_count = 0;
812
813 do
814 {
815 if (p_article->visible)
816 {
817 p_articles[*p_article_count] = p_article;
818 (*p_article_count)++;
819 }
820 p_article = p_article->p_next;
821 } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
822
823 if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
824 {
825 log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
826 }
827 }
828
829 // release lock of section
830 if (section_list_rd_unlock(p_section) < 0)
831 {
832 log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
833 ret = -2;
834 }
835
836 return ret;
837 }
838
839 int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,
840 int *p_page_id, int *p_visible_offset, int *p_article_count)
841 {
842 const ARTICLE *p_article = NULL;
843 ARTICLE *p_tmp;
844 int32_t aid = 0;
845 int page_id;
846 int offset;
847 int ret = 0;
848 int i;
849
850 if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
851 {
852 log_error("locate_article_in_section() NULL pointer error\n");
853 return -1;
854 }
855
856 // acquire lock of section
857 if ((ret = section_list_rd_lock(p_section)) < 0)
858 {
859 log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
860 return -2;
861 }
862
863 if (direction == 0)
864 {
865 aid = p_article_cur->aid;
866 }
867 else if (direction == 1)
868 {
869 p_article = p_article_cur;
870 do
871 {
872 p_article = p_article->p_topic_next;
873 } while (p_article != p_article_cur && p_article->visible == 0);
874
875 aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0);
876 }
877 else if (direction == -1)
878 {
879 p_article = p_article_cur;
880 do
881 {
882 p_article = p_article->p_topic_prior;
883 } while (p_article != p_article_cur && p_article->visible == 0);
884
885 aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0);
886 }
887
888 p_article = NULL;
889
890 if (aid > 0)
891 {
892 p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
893 if (p_article != NULL)
894 {
895 *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
896
897 p_article = p_section->p_page_first_article[page_id];
898 for (i = 0; i < *p_article_count;)
899 {
900 if (p_article->visible)
901 {
902 if (p_article->aid == aid)
903 {
904 *p_page_id = page_id;
905 *p_visible_offset = i;
906 break;
907 }
908 i++;
909 if (i >= *p_article_count)
910 {
911 log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
912 p_article = NULL;
913 }
914 }
915 p_article = p_article->p_next;
916 }
917 }
918 }
919
920 // release lock of section
921 if (section_list_rd_unlock(p_section) < 0)
922 {
923 log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
924 ret = -2;
925 }
926
927 return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
928 }

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