/[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.32 - (show annotations)
Mon Jun 23 08:38:01 2025 UTC (8 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.31: +42 -19 lines
Content type: text/x-csrc
Display articles with ontop state at the end of section list

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

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