/[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.28 - (show annotations)
Fri Jun 20 09:36:58 2025 UTC (8 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.27: +1 -1 lines
Content type: text/x-csrc
Fix bug

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

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