| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
|
|
|
|
#include "section_list_loader.h" |
|
| 17 |
#include "article_cache.h" |
#include "article_cache.h" |
| 18 |
|
#include "article_view_log.h" |
| 19 |
#include "bbs.h" |
#include "bbs.h" |
|
#include "log.h" |
|
| 20 |
#include "database.h" |
#include "database.h" |
| 21 |
|
#include "ip_mask.h" |
| 22 |
|
#include "log.h" |
| 23 |
#include "menu.h" |
#include "menu.h" |
| 24 |
#include <stdio.h> |
#include "section_list_loader.h" |
| 25 |
#include <string.h> |
#include "user_list.h" |
| 26 |
|
#include "user_priv.h" |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <signal.h> |
#include <signal.h> |
| 29 |
|
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
#include <stdlib.h> |
| 31 |
#include <string.h> |
#include <string.h> |
|
#include <strings.h> |
|
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
|
|
| 34 |
int section_list_loader_pid; |
int section_list_loader_pid; |
| 35 |
int last_article_op_log_mid; |
int last_article_op_log_mid; |
| 36 |
|
|
| 37 |
int load_section_config_from_db(void) |
int load_section_config_from_db(int update_gen_ex) |
| 38 |
{ |
{ |
| 39 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 40 |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 43 |
int32_t sid; |
int32_t sid; |
| 44 |
char master_list[(BBS_username_max_len + 1) * 3 + 1]; |
char master_list[(BBS_username_max_len + 1) * 3 + 1]; |
| 45 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 46 |
|
char ex_menu_conf[FILE_PATH_LEN]; |
| 47 |
|
MENU_SET ex_menu_set_new; |
| 48 |
int ret = 0; |
int ret = 0; |
| 49 |
|
|
| 50 |
db = db_open(); |
db = db_open(); |
| 57 |
|
|
| 58 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 59 |
"SELECT section_config.SID, sname, section_config.title, section_config.CID, " |
"SELECT section_config.SID, sname, section_config.title, section_config.CID, " |
| 60 |
"read_user_level, write_user_level, section_config.enable * section_class.enable AS enable " |
"read_user_level, write_user_level, section_config.enable * section_class.enable AS enable, " |
| 61 |
|
"UNIX_TIMESTAMP(ex_menu_tm) AS ex_menu_tm " |
| 62 |
"FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID " |
"FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID " |
| 63 |
"ORDER BY section_config.SID"); |
"ORDER BY section_config.SID"); |
| 64 |
|
|
| 138 |
break; |
break; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1); |
if (section_list_update(p_section, row[1], row[2], master_list) < 0) |
| 142 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
{ |
| 143 |
strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1); |
log_error("section_list_update(sid=%d) error\n", p_section->sid); |
| 144 |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
ret = -4; |
| 145 |
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
break; |
| 146 |
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
} |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
p_section->class_id = atoi(row[3]); |
p_section->class_id = atoi(row[3]); |
| 151 |
p_section->write_user_level = atoi(row[5]); |
p_section->write_user_level = atoi(row[5]); |
| 152 |
p_section->enable = (int8_t)atoi(row[6]); |
p_section->enable = (int8_t)atoi(row[6]); |
| 153 |
|
|
| 154 |
|
// Update gen_ex menu set |
| 155 |
|
if (update_gen_ex && p_section->enable && atol(row[7] == NULL ? "0" : row[7]) > p_section->ex_menu_tm) |
| 156 |
|
{ |
| 157 |
|
snprintf(ex_menu_conf, sizeof(ex_menu_conf), "%s/%d", VAR_GEN_EX_MENU_DIR, p_section->sid); |
| 158 |
|
|
| 159 |
|
ret = load_menu(&ex_menu_set_new, ex_menu_conf); |
| 160 |
|
if (ret < 0) |
| 161 |
|
{ |
| 162 |
|
unload_menu(&ex_menu_set_new); |
| 163 |
|
log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret); |
| 164 |
|
} |
| 165 |
|
else |
| 166 |
|
{ |
| 167 |
|
if (p_section->ex_menu_tm > 0) |
| 168 |
|
{ |
| 169 |
|
unload_menu(&(p_section->ex_menu_set)); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
ex_menu_set_new.allow_exit = 1; // Allow exit menu |
| 173 |
|
memcpy(&(p_section->ex_menu_set), &ex_menu_set_new, sizeof(ex_menu_set_new)); |
| 174 |
|
|
| 175 |
|
p_section->ex_menu_tm = atol(row[7]); |
| 176 |
|
#ifdef _DEBUG |
| 177 |
|
log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname); |
| 178 |
|
#endif |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
// release rw lock |
// release rw lock |
| 183 |
ret = section_list_rw_unlock(p_section); |
ret = section_list_rw_unlock(p_section); |
| 184 |
if (ret < 0) |
if (ret < 0) |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
cleanup: |
cleanup: |
| 191 |
|
mysql_free_result(rs2); |
| 192 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 193 |
mysql_close(db); |
mysql_close(db); |
| 194 |
|
|
| 356 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 357 |
mysql_close(db); |
mysql_close(db); |
| 358 |
|
|
| 359 |
|
article_cache_cleanup(); |
| 360 |
|
|
| 361 |
return (ret < 0 ? ret : article_count); |
return (ret < 0 ? ret : article_count); |
| 362 |
} |
} |
| 363 |
|
|
| 624 |
p_article->excerption = 0; |
p_article->excerption = 0; |
| 625 |
break; |
break; |
| 626 |
case 'F': // Set article on top |
case 'F': // Set article on top |
|
p_article->ontop = 1; |
|
|
break; |
|
| 627 |
case 'V': // Unset article on top |
case 'V': // Unset article on top |
| 628 |
p_article->ontop = 0; |
p_article->ontop = (row[2][0] == 'F' ? 1 : 0); |
| 629 |
|
if (section_list_update_article_ontop(p_section, p_article) < 0) |
| 630 |
|
{ |
| 631 |
|
log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n", |
| 632 |
|
p_section->sid, p_article->aid); |
| 633 |
|
} |
| 634 |
break; |
break; |
| 635 |
case 'Z': // Set article as trnasship |
case 'Z': // Set article as trnasship |
| 636 |
p_article->transship = 1; |
p_article->transship = 1; |
| 668 |
return (ret < 0 ? ret : op_count); |
return (ret < 0 ? ret : op_count); |
| 669 |
} |
} |
| 670 |
|
|
| 671 |
|
static void section_list_ex_menu_set_cleanup(void) |
| 672 |
|
{ |
| 673 |
|
int i; |
| 674 |
|
|
| 675 |
|
for (i = 0; i < p_section_list_pool->section_count; i++) |
| 676 |
|
{ |
| 677 |
|
if (p_section_list_pool->sections[i].ex_menu_tm > 0) |
| 678 |
|
{ |
| 679 |
|
unload_menu(&(p_section_list_pool->sections[i].ex_menu_set)); |
| 680 |
|
} |
| 681 |
|
} |
| 682 |
|
} |
| 683 |
|
|
| 684 |
int section_list_loader_launch(void) |
int section_list_loader_launch(void) |
| 685 |
{ |
{ |
| 686 |
|
struct sigaction act = {0}; |
| 687 |
int pid; |
int pid; |
| 688 |
int ret; |
int ret; |
| 689 |
int32_t last_aid; |
int32_t last_aid; |
| 690 |
int article_count; |
int article_count; |
| 691 |
int load_count; |
int load_count; |
| 692 |
int last_mid; |
int last_mid; |
| 693 |
int i; |
time_t tm_section_list_reload = 0; |
| 694 |
|
time_t tm_user_list_reload = time(NULL); |
| 695 |
|
time_t tm_user_online_list_reload = time(NULL); |
| 696 |
|
|
| 697 |
if (section_list_loader_pid != 0) |
if (section_list_loader_pid != 0) |
| 698 |
{ |
{ |
| 719 |
SYS_child_process_count = 0; |
SYS_child_process_count = 0; |
| 720 |
|
|
| 721 |
// Detach menu in shared memory |
// Detach menu in shared memory |
| 722 |
detach_menu_shm(p_bbs_menu); |
detach_menu_shm(&bbs_menu); |
| 723 |
free(p_bbs_menu); |
detach_menu_shm(&top10_menu); |
| 724 |
p_bbs_menu = NULL; |
|
| 725 |
|
// Set signal handler |
| 726 |
|
act.sa_handler = SIG_IGN; |
| 727 |
|
if (sigaction(SIGHUP, &act, NULL) == -1) |
| 728 |
|
{ |
| 729 |
|
log_error("set signal action of SIGHUP error: %d\n", errno); |
| 730 |
|
} |
| 731 |
|
act.sa_handler = SIG_DFL; |
| 732 |
|
if (sigaction(SIGCHLD, &act, NULL) == -1) |
| 733 |
|
{ |
| 734 |
|
log_error("set signal action of SIGCHLD error: %d\n", errno); |
| 735 |
|
} |
| 736 |
|
|
| 737 |
// Do section data loader periodically |
// Do section data loader periodically |
| 738 |
while (!SYS_server_exit) |
while (!SYS_server_exit) |
| 739 |
{ |
{ |
| 740 |
if (SYS_section_list_reload) |
tm_section_list_reload = time(NULL); |
| 741 |
|
|
| 742 |
|
if (SYS_conf_reload) |
| 743 |
{ |
{ |
| 744 |
SYS_section_list_reload = 0; |
SYS_conf_reload = 0; |
| 745 |
|
|
| 746 |
// Load section config |
// Load section config |
| 747 |
if (load_section_config_from_db() < 0) |
if (load_section_config_from_db(0) < 0) |
| 748 |
{ |
{ |
| 749 |
log_error("load_section_config_from_db() error\n"); |
log_error("load_section_config_from_db(0) error\n"); |
| 750 |
} |
} |
| 751 |
else |
else |
| 752 |
{ |
{ |
| 753 |
log_error("Reload section config successfully\n"); |
log_common("Reload section config successfully\n"); |
| 754 |
} |
} |
| 755 |
} |
} |
| 756 |
|
|
| 767 |
|
|
| 768 |
if (ret == ERR_UNKNOWN_SECTION) |
if (ret == ERR_UNKNOWN_SECTION) |
| 769 |
{ |
{ |
| 770 |
SYS_section_list_reload = 1; // Force reload section_list |
SYS_conf_reload = 1; // Force reload section_list |
| 771 |
} |
} |
| 772 |
} |
} |
| 773 |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
| 778 |
log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid()); |
log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid()); |
| 779 |
} |
} |
| 780 |
|
|
| 781 |
if (SYS_section_list_reload) |
if (SYS_conf_reload) |
| 782 |
{ |
{ |
| 783 |
continue; |
continue; |
| 784 |
} |
} |
| 794 |
|
|
| 795 |
if (ret == ERR_UNKNOWN_SECTION) |
if (ret == ERR_UNKNOWN_SECTION) |
| 796 |
{ |
{ |
| 797 |
SYS_section_list_reload = 1; // Force reload section_list |
SYS_conf_reload = 1; // Force reload section_list |
| 798 |
} |
} |
| 799 |
} |
} |
| 800 |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
} while (ret == LOAD_ARTICLE_COUNT_LIMIT); |
| 804 |
log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid); |
log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid); |
| 805 |
} |
} |
| 806 |
|
|
| 807 |
if (SYS_section_list_reload) |
// Reload user list |
| 808 |
|
if (time(NULL) - tm_user_list_reload >= BBS_user_list_load_interval) |
| 809 |
{ |
{ |
| 810 |
continue; |
if (user_list_pool_reload(0) < 0) |
| 811 |
|
{ |
| 812 |
|
log_error("user_list_pool_reload(all_user) error\n"); |
| 813 |
|
} |
| 814 |
|
|
| 815 |
|
if (user_stat_update() < 0) |
| 816 |
|
{ |
| 817 |
|
log_error("user_stat_update() error\n"); |
| 818 |
|
} |
| 819 |
|
|
| 820 |
|
tm_user_list_reload = time(NULL); |
| 821 |
|
} |
| 822 |
|
|
| 823 |
|
// Reload user online list |
| 824 |
|
if (time(NULL) - tm_user_online_list_reload >= BBS_user_online_list_load_interval) |
| 825 |
|
{ |
| 826 |
|
if (user_list_pool_reload(1) < 0) |
| 827 |
|
{ |
| 828 |
|
log_error("user_list_pool_reload(online_user) error\n"); |
| 829 |
|
} |
| 830 |
|
|
| 831 |
|
tm_user_online_list_reload = time(NULL); |
| 832 |
} |
} |
| 833 |
|
|
| 834 |
for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++) |
// Wait for BBS_section_list_load_interval seconds |
| 835 |
|
while (!SYS_server_exit && time(NULL) - tm_section_list_reload < BBS_section_list_load_interval) |
| 836 |
{ |
{ |
| 837 |
sleep(1); |
sleep(1); |
| 838 |
} |
} |
| 840 |
|
|
| 841 |
// Child process exit |
// Child process exit |
| 842 |
|
|
| 843 |
|
article_cache_cleanup(); |
| 844 |
|
|
| 845 |
|
// gen_ex_menu cleanup |
| 846 |
|
section_list_ex_menu_set_cleanup(); |
| 847 |
|
|
| 848 |
// Detach data pools shm |
// Detach data pools shm |
| 849 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 850 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 851 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |
| 852 |
|
detach_user_list_pool_shm(); |
| 853 |
|
|
| 854 |
log_common("Section list loader process exit normally\n"); |
log_common("Section list loader process exit normally\n"); |
| 855 |
log_end(); |
log_end(); |
| 861 |
return 0; |
return 0; |
| 862 |
} |
} |
| 863 |
|
|
| 864 |
int section_list_loader_reload(void) |
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], |
| 865 |
{ |
int *p_article_count, int *p_page_count, int *p_ontop_start_offset) |
|
if (section_list_loader_pid == 0) |
|
|
{ |
|
|
log_error("section_list_loader not running\n"); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
if (kill(section_list_loader_pid, SIGHUP) < 0) |
|
|
{ |
|
|
log_error("Send SIGTERM signal failed (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count) |
|
| 866 |
{ |
{ |
| 867 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 868 |
ARTICLE *p_next_page_first_article; |
ARTICLE *p_next_page_first_article; |
| 869 |
int ret = 0; |
int ret = 0; |
| 870 |
|
int i; |
| 871 |
|
|
| 872 |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL) |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL || p_ontop_start_offset == NULL) |
| 873 |
{ |
{ |
| 874 |
log_error("query_section_articles() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 875 |
return -1; |
return -1; |
| 876 |
} |
} |
| 877 |
|
|
| 882 |
return -2; |
return -2; |
| 883 |
} |
} |
| 884 |
|
|
| 885 |
*p_page_count = p_section->page_count; |
*p_page_count = section_list_page_count_with_ontop(p_section); |
| 886 |
|
*p_article_count = 0; |
| 887 |
|
*p_ontop_start_offset = BBS_article_limit_per_page; |
| 888 |
|
|
| 889 |
if (p_section->visible_article_count == 0) |
if (p_section->visible_article_count == 0) |
| 890 |
{ |
{ |
| 891 |
*p_article_count = 0; |
// empty section |
| 892 |
} |
} |
| 893 |
else if (page_id < 0 || page_id >= p_section->page_count) |
else if (page_id < 0 || page_id >= *p_page_count) |
| 894 |
{ |
{ |
| 895 |
log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count); |
#ifdef _DEBUG |
| 896 |
|
log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, *p_page_count); |
| 897 |
|
#endif |
| 898 |
ret = -3; |
ret = -3; |
| 899 |
} |
} |
| 900 |
else |
else |
| 901 |
{ |
{ |
| 902 |
ret = page_id; |
ret = page_id; |
|
p_article = p_section->p_page_first_article[page_id]; |
|
|
p_next_page_first_article = |
|
|
(page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]); |
|
|
*p_article_count = 0; |
|
| 903 |
|
|
| 904 |
do |
if (page_id <= p_section->page_count - 1) |
| 905 |
{ |
{ |
| 906 |
if (p_article->visible) |
p_article = p_section->p_page_first_article[page_id]; |
| 907 |
|
p_next_page_first_article = |
| 908 |
|
(page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]); |
| 909 |
|
|
| 910 |
|
do |
| 911 |
{ |
{ |
| 912 |
p_articles[*p_article_count] = p_article; |
if (p_article->visible) |
| 913 |
(*p_article_count)++; |
{ |
| 914 |
|
p_articles[*p_article_count] = p_article; |
| 915 |
|
(*p_article_count)++; |
| 916 |
|
} |
| 917 |
|
p_article = p_article->p_next; |
| 918 |
|
} while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page); |
| 919 |
|
|
| 920 |
|
if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count)) |
| 921 |
|
{ |
| 922 |
|
log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id); |
| 923 |
} |
} |
| 924 |
p_article = p_article->p_next; |
} |
|
} while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page); |
|
| 925 |
|
|
| 926 |
if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count)) |
*p_ontop_start_offset = *p_article_count; |
| 927 |
|
|
| 928 |
|
// Append ontop articles |
| 929 |
|
if (page_id >= p_section->page_count - 1) |
| 930 |
{ |
{ |
| 931 |
log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id); |
i = (page_id == p_section->page_count - 1 |
| 932 |
|
? 0 |
| 933 |
|
: (page_id - p_section->page_count + 1) * BBS_article_limit_per_page - p_section->last_page_visible_article_count); |
| 934 |
|
while (*p_article_count < BBS_article_limit_per_page && i < p_section->ontop_article_count) |
| 935 |
|
{ |
| 936 |
|
p_articles[*p_article_count] = p_section->p_ontop_articles[i++]; |
| 937 |
|
(*p_article_count)++; |
| 938 |
|
} |
| 939 |
} |
} |
| 940 |
} |
} |
| 941 |
|
|
| 953 |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
| 954 |
{ |
{ |
| 955 |
const ARTICLE *p_article = NULL; |
const ARTICLE *p_article = NULL; |
| 956 |
|
const ARTICLE *p_article_last = NULL; |
| 957 |
ARTICLE *p_tmp; |
ARTICLE *p_tmp; |
| 958 |
int32_t aid = 0; |
int32_t aid = 0; |
| 959 |
int page_id = 0; |
int page_id = 0; |
| 963 |
|
|
| 964 |
if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL) |
if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL) |
| 965 |
{ |
{ |
| 966 |
log_error("locate_article_in_section() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 967 |
return -1; |
return -1; |
| 968 |
} |
} |
| 969 |
|
|
| 980 |
} |
} |
| 981 |
else if (direction == 1) |
else if (direction == 1) |
| 982 |
{ |
{ |
| 983 |
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) |
for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_next; |
| 984 |
|
step > 0 && p_article->aid > p_article_cur->aid; |
| 985 |
|
p_article = p_article->p_topic_next) |
| 986 |
{ |
{ |
| 987 |
if (p_article->visible) |
if (p_article->visible) |
| 988 |
{ |
{ |
| 989 |
step--; |
step--; |
| 990 |
|
p_article_last = p_article; |
| 991 |
} |
} |
| 992 |
} |
} |
| 993 |
|
p_article = p_article_last; |
| 994 |
|
|
| 995 |
aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0); |
aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 996 |
} |
} |
| 997 |
else if (direction == -1) |
else if (direction == -1) |
| 998 |
{ |
{ |
| 999 |
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) |
for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_prior; |
| 1000 |
|
step > 0 && p_article->aid < p_article_cur->aid; |
| 1001 |
|
p_article = p_article->p_topic_prior) |
| 1002 |
{ |
{ |
| 1003 |
if (p_article->visible) |
if (p_article->visible) |
| 1004 |
{ |
{ |
| 1005 |
step--; |
step--; |
| 1006 |
|
p_article_last = p_article; |
| 1007 |
} |
} |
| 1008 |
} |
} |
| 1009 |
|
p_article = p_article_last; |
| 1010 |
|
|
| 1011 |
aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0); |
aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 1012 |
} |
} |
| 1013 |
|
|
| 1014 |
p_article = NULL; |
p_article = NULL; |
| 1018 |
p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp); |
p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp); |
| 1019 |
if (p_article != NULL) |
if (p_article != NULL) |
| 1020 |
{ |
{ |
| 1021 |
*p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page); |
*p_article_count = (page_id >= p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page); |
| 1022 |
|
|
| 1023 |
p_article = p_section->p_page_first_article[page_id]; |
p_article = p_section->p_page_first_article[page_id]; |
| 1024 |
for (i = 0; i < *p_article_count;) |
for (i = 0; i < *p_article_count && p_article != NULL;) |
| 1025 |
{ |
{ |
| 1026 |
if (p_article->visible) |
if (p_article->visible) |
| 1027 |
{ |
{ |
| 1036 |
{ |
{ |
| 1037 |
log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id); |
log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id); |
| 1038 |
p_article = NULL; |
p_article = NULL; |
| 1039 |
|
break; |
| 1040 |
} |
} |
| 1041 |
} |
} |
| 1042 |
|
|
| 1043 |
p_article = p_article->p_next; |
p_article = p_article->p_next; |
| 1044 |
|
if (p_article == p_section->p_page_first_article[page_id]) |
| 1045 |
|
{ |
| 1046 |
|
log_error("Dead loop detected at page=%d, article_count=%d\n", page_id, *p_article_count); |
| 1047 |
|
break; |
| 1048 |
|
} |
| 1049 |
} |
} |
| 1050 |
|
|
| 1051 |
|
// Include ontop articles |
| 1052 |
|
*p_article_count = section_list_page_article_count_with_ontop(p_section, page_id); |
| 1053 |
} |
} |
| 1054 |
} |
} |
| 1055 |
|
|
| 1062 |
|
|
| 1063 |
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
| 1064 |
} |
} |
| 1065 |
|
|
| 1066 |
|
int scan_unread_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, const ARTICLE **pp_article_unread) |
| 1067 |
|
{ |
| 1068 |
|
ARTICLE *p_article; |
| 1069 |
|
int ret = 0; |
| 1070 |
|
|
| 1071 |
|
if (p_section == NULL || p_article_cur == NULL || pp_article_unread == NULL) |
| 1072 |
|
{ |
| 1073 |
|
log_error("NULL pointer error\n"); |
| 1074 |
|
return -1; |
| 1075 |
|
} |
| 1076 |
|
|
| 1077 |
|
if (p_article_cur->sid != p_section->sid) |
| 1078 |
|
{ |
| 1079 |
|
log_error("Inconsistent SID\n"); |
| 1080 |
|
return -1; |
| 1081 |
|
} |
| 1082 |
|
|
| 1083 |
|
// acquire lock of section |
| 1084 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1085 |
|
{ |
| 1086 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1087 |
|
return -2; |
| 1088 |
|
} |
| 1089 |
|
|
| 1090 |
|
*pp_article_unread = NULL; |
| 1091 |
|
|
| 1092 |
|
for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next) |
| 1093 |
|
{ |
| 1094 |
|
if (p_article->visible && p_article->uid != BBS_priv.uid && !article_view_log_is_viewed(p_article->aid, &BBS_article_view_log)) |
| 1095 |
|
{ |
| 1096 |
|
*pp_article_unread = p_article; |
| 1097 |
|
break; |
| 1098 |
|
} |
| 1099 |
|
} |
| 1100 |
|
|
| 1101 |
|
// release lock of section |
| 1102 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1103 |
|
{ |
| 1104 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1105 |
|
return -2; |
| 1106 |
|
} |
| 1107 |
|
|
| 1108 |
|
return (p_article != NULL && p_article != p_article_cur ? 1 : 0); |
| 1109 |
|
} |
| 1110 |
|
|
| 1111 |
|
int scan_article_in_section_by_uid(SECTION_LIST *p_section, const ARTICLE *p_article_cur, |
| 1112 |
|
int direction, int32_t uid, const ARTICLE **pp_article) |
| 1113 |
|
{ |
| 1114 |
|
ARTICLE *p_article; |
| 1115 |
|
int ret = 0; |
| 1116 |
|
|
| 1117 |
|
if (p_section == NULL || p_article_cur == NULL || pp_article == NULL) |
| 1118 |
|
{ |
| 1119 |
|
log_error("NULL pointer error\n"); |
| 1120 |
|
return -1; |
| 1121 |
|
} |
| 1122 |
|
|
| 1123 |
|
if (p_article_cur->sid != p_section->sid) |
| 1124 |
|
{ |
| 1125 |
|
log_error("Inconsistent SID\n"); |
| 1126 |
|
return -1; |
| 1127 |
|
} |
| 1128 |
|
|
| 1129 |
|
// acquire lock of section |
| 1130 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1131 |
|
{ |
| 1132 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1133 |
|
return -2; |
| 1134 |
|
} |
| 1135 |
|
|
| 1136 |
|
*pp_article = NULL; |
| 1137 |
|
|
| 1138 |
|
if (direction >= 0) |
| 1139 |
|
{ |
| 1140 |
|
for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next) |
| 1141 |
|
{ |
| 1142 |
|
if (p_article->visible && p_article->uid == uid) |
| 1143 |
|
{ |
| 1144 |
|
*pp_article = p_article; |
| 1145 |
|
break; |
| 1146 |
|
} |
| 1147 |
|
} |
| 1148 |
|
} |
| 1149 |
|
else // direction < 0 |
| 1150 |
|
{ |
| 1151 |
|
for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior) |
| 1152 |
|
{ |
| 1153 |
|
if (p_article->visible && p_article->uid == uid) |
| 1154 |
|
{ |
| 1155 |
|
*pp_article = p_article; |
| 1156 |
|
break; |
| 1157 |
|
} |
| 1158 |
|
} |
| 1159 |
|
} |
| 1160 |
|
|
| 1161 |
|
// release lock of section |
| 1162 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1163 |
|
{ |
| 1164 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1165 |
|
return -2; |
| 1166 |
|
} |
| 1167 |
|
|
| 1168 |
|
return (p_article != NULL && p_article != p_article_cur ? 1 : 0); |
| 1169 |
|
} |
| 1170 |
|
|
| 1171 |
|
int scan_article_in_section_by_username(SECTION_LIST *p_section, const ARTICLE *p_article_cur, |
| 1172 |
|
int direction, const char *username, const ARTICLE **pp_article) |
| 1173 |
|
{ |
| 1174 |
|
ARTICLE *p_article; |
| 1175 |
|
int ret = 0; |
| 1176 |
|
|
| 1177 |
|
if (p_section == NULL || p_article_cur == NULL || username == NULL || pp_article == NULL) |
| 1178 |
|
{ |
| 1179 |
|
log_error("NULL pointer error\n"); |
| 1180 |
|
return -1; |
| 1181 |
|
} |
| 1182 |
|
|
| 1183 |
|
if (p_article_cur->sid != p_section->sid) |
| 1184 |
|
{ |
| 1185 |
|
log_error("Inconsistent SID\n"); |
| 1186 |
|
return -1; |
| 1187 |
|
} |
| 1188 |
|
|
| 1189 |
|
// acquire lock of section |
| 1190 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1191 |
|
{ |
| 1192 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1193 |
|
return -2; |
| 1194 |
|
} |
| 1195 |
|
|
| 1196 |
|
*pp_article = NULL; |
| 1197 |
|
|
| 1198 |
|
if (direction >= 0) |
| 1199 |
|
{ |
| 1200 |
|
for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next) |
| 1201 |
|
{ |
| 1202 |
|
if (p_article->visible && strcasecmp(p_article->username, username) == 0) |
| 1203 |
|
{ |
| 1204 |
|
*pp_article = p_article; |
| 1205 |
|
break; |
| 1206 |
|
} |
| 1207 |
|
} |
| 1208 |
|
} |
| 1209 |
|
else // direction < 0 |
| 1210 |
|
{ |
| 1211 |
|
for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior) |
| 1212 |
|
{ |
| 1213 |
|
if (p_article->visible && strcasecmp(p_article->username, username) == 0) |
| 1214 |
|
{ |
| 1215 |
|
*pp_article = p_article; |
| 1216 |
|
break; |
| 1217 |
|
} |
| 1218 |
|
} |
| 1219 |
|
} |
| 1220 |
|
|
| 1221 |
|
// release lock of section |
| 1222 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1223 |
|
{ |
| 1224 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1225 |
|
return -2; |
| 1226 |
|
} |
| 1227 |
|
|
| 1228 |
|
return (p_article != NULL && p_article != p_article_cur ? 1 : 0); |
| 1229 |
|
} |
| 1230 |
|
|
| 1231 |
|
int scan_article_in_section_by_title(SECTION_LIST *p_section, const ARTICLE *p_article_cur, |
| 1232 |
|
int direction, const char *title, const ARTICLE **pp_article) |
| 1233 |
|
{ |
| 1234 |
|
ARTICLE *p_article; |
| 1235 |
|
int ret = 0; |
| 1236 |
|
|
| 1237 |
|
if (p_section == NULL || p_article_cur == NULL || title == NULL || pp_article == NULL) |
| 1238 |
|
{ |
| 1239 |
|
log_error("NULL pointer error\n"); |
| 1240 |
|
return -1; |
| 1241 |
|
} |
| 1242 |
|
|
| 1243 |
|
if (p_article_cur->sid != p_section->sid) |
| 1244 |
|
{ |
| 1245 |
|
log_error("Inconsistent SID\n"); |
| 1246 |
|
return -1; |
| 1247 |
|
} |
| 1248 |
|
|
| 1249 |
|
// acquire lock of section |
| 1250 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1251 |
|
{ |
| 1252 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1253 |
|
return -2; |
| 1254 |
|
} |
| 1255 |
|
|
| 1256 |
|
*pp_article = NULL; |
| 1257 |
|
|
| 1258 |
|
if (direction >= 0) |
| 1259 |
|
{ |
| 1260 |
|
for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next) |
| 1261 |
|
{ |
| 1262 |
|
if (p_article->visible && strcasestr(p_article->title, title) != NULL) |
| 1263 |
|
{ |
| 1264 |
|
*pp_article = p_article; |
| 1265 |
|
break; |
| 1266 |
|
} |
| 1267 |
|
} |
| 1268 |
|
} |
| 1269 |
|
else // direction < 0 |
| 1270 |
|
{ |
| 1271 |
|
for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior) |
| 1272 |
|
{ |
| 1273 |
|
if (p_article->visible && strcasestr(p_article->title, title) != NULL) |
| 1274 |
|
{ |
| 1275 |
|
*pp_article = p_article; |
| 1276 |
|
break; |
| 1277 |
|
} |
| 1278 |
|
} |
| 1279 |
|
} |
| 1280 |
|
|
| 1281 |
|
// release lock of section |
| 1282 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1283 |
|
{ |
| 1284 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1285 |
|
return -2; |
| 1286 |
|
} |
| 1287 |
|
|
| 1288 |
|
return (p_article != NULL && p_article != p_article_cur ? 1 : 0); |
| 1289 |
|
} |
| 1290 |
|
|
| 1291 |
|
int get_section_ex_menu_set(SECTION_LIST *p_section, MENU_SET *p_ex_menu_set) |
| 1292 |
|
{ |
| 1293 |
|
int ret = 0; |
| 1294 |
|
|
| 1295 |
|
if (p_section == NULL || p_ex_menu_set == NULL) |
| 1296 |
|
{ |
| 1297 |
|
log_error("NULL pointer error\n"); |
| 1298 |
|
return -1; |
| 1299 |
|
} |
| 1300 |
|
|
| 1301 |
|
// acquire lock of section |
| 1302 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 1303 |
|
{ |
| 1304 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 1305 |
|
return -2; |
| 1306 |
|
} |
| 1307 |
|
|
| 1308 |
|
memcpy(p_ex_menu_set, &(p_section->ex_menu_set), sizeof(p_section->ex_menu_set)); |
| 1309 |
|
|
| 1310 |
|
// release lock of section |
| 1311 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1312 |
|
{ |
| 1313 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 1314 |
|
ret = -2; |
| 1315 |
|
} |
| 1316 |
|
|
| 1317 |
|
return ret; |
| 1318 |
|
} |