| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#include "section_list_loader.h" |
|
| 17 |
#include "article_cache.h" |
#include "article_cache.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
|
#include "log.h" |
|
| 19 |
#include "database.h" |
#include "database.h" |
| 20 |
|
#include "log.h" |
| 21 |
#include "menu.h" |
#include "menu.h" |
| 22 |
#include <stdio.h> |
#include "section_list_loader.h" |
|
#include <string.h> |
|
| 23 |
#include <errno.h> |
#include <errno.h> |
| 24 |
#include <signal.h> |
#include <signal.h> |
| 25 |
|
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <strings.h> |
|
|
#include <unistd.h> |
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
| 27 |
#include <string.h> |
#include <string.h> |
| 28 |
|
#include <unistd.h> |
| 29 |
|
|
| 30 |
int section_list_loader_pid; |
int section_list_loader_pid; |
| 31 |
int last_article_op_log_mid; |
int last_article_op_log_mid; |
| 32 |
|
|
| 33 |
int load_section_config_from_db(void) |
int load_section_config_from_db(void) |
| 34 |
{ |
{ |
| 35 |
MYSQL *db; |
MYSQL *db = NULL; |
| 36 |
MYSQL_RES *rs, *rs2; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 37 |
MYSQL_ROW row, row2; |
MYSQL_ROW row, row2; |
| 38 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 39 |
int32_t sid; |
int32_t sid; |
| 40 |
char master_list[(BBS_username_max_len + 1) * 3 + 1]; |
char master_list[(BBS_username_max_len + 1) * 3 + 1]; |
| 41 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 42 |
int ret; |
int ret = 0; |
| 43 |
|
|
| 44 |
db = db_open(); |
db = db_open(); |
| 45 |
if (db == NULL) |
if (db == NULL) |
| 46 |
{ |
{ |
| 47 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 48 |
return -2; |
ret = -1; |
| 49 |
|
goto cleanup; |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 58 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 59 |
{ |
{ |
| 60 |
log_error("Query section_list error: %s\n", mysql_error(db)); |
log_error("Query section_list error: %s\n", mysql_error(db)); |
| 61 |
return -3; |
ret = -2; |
| 62 |
|
goto cleanup; |
| 63 |
} |
} |
| 64 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 65 |
{ |
{ |
| 66 |
log_error("Get section_list data failed\n"); |
log_error("Get section_list data failed\n"); |
| 67 |
return -3; |
ret = -2; |
| 68 |
|
goto cleanup; |
| 69 |
} |
} |
| 70 |
|
|
| 71 |
ret = 0; |
ret = 0; |
| 101 |
strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list))); |
strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list))); |
| 102 |
} |
} |
| 103 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 104 |
|
rs2 = NULL; |
| 105 |
|
|
| 106 |
p_section = section_list_find_by_sid(sid); |
p_section = section_list_find_by_sid(sid); |
| 107 |
|
|
| 131 |
break; |
break; |
| 132 |
} |
} |
| 133 |
|
|
| 134 |
strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1); |
strncpy(p_section->sname, row[0], sizeof(p_section->sname) - 1); |
| 135 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 136 |
strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1); |
strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1); |
| 137 |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 151 |
break; |
break; |
| 152 |
} |
} |
| 153 |
} |
} |
|
mysql_free_result(rs); |
|
| 154 |
|
|
| 155 |
|
cleanup: |
| 156 |
|
mysql_free_result(rs2); |
| 157 |
|
mysql_free_result(rs); |
| 158 |
mysql_close(db); |
mysql_close(db); |
| 159 |
|
|
| 160 |
return ret; |
return ret; |
| 162 |
|
|
| 163 |
int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit) |
int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit) |
| 164 |
{ |
{ |
| 165 |
MYSQL *db; |
MYSQL *db = NULL; |
| 166 |
MYSQL_RES *rs; |
MYSQL_RES *rs = NULL; |
| 167 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 168 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 169 |
ARTICLE article; |
ARTICLE article; |
| 179 |
if (db == NULL) |
if (db == NULL) |
| 180 |
{ |
{ |
| 181 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 182 |
return -2; |
ret = -1; |
| 183 |
|
goto cleanup; |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 194 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 195 |
{ |
{ |
| 196 |
log_error("Query article list error: %s\n", mysql_error(db)); |
log_error("Query article list error: %s\n", mysql_error(db)); |
| 197 |
return -3; |
ret = -2; |
| 198 |
|
goto cleanup; |
| 199 |
} |
} |
| 200 |
if ((rs = mysql_use_result(db)) == NULL) |
if ((rs = mysql_use_result(db)) == NULL) |
| 201 |
{ |
{ |
| 202 |
log_error("Get article list data failed\n"); |
log_error("Get article list data failed\n"); |
| 203 |
return -3; |
ret = -2; |
| 204 |
|
goto cleanup; |
| 205 |
} |
} |
| 206 |
|
|
| 207 |
// acquire global lock |
// acquire global lock |
| 208 |
if (global_lock) |
if (global_lock && (ret = section_list_rw_lock(NULL)) < 0) |
| 209 |
{ |
{ |
| 210 |
if ((ret = section_list_rw_lock(NULL)) < 0) |
log_error("section_list_rw_lock(sid = 0) error\n"); |
| 211 |
{ |
goto cleanup; |
|
log_error("section_list_rw_lock(sid = 0) error\n"); |
|
|
goto cleanup; |
|
|
} |
|
| 212 |
} |
} |
| 213 |
|
|
| 214 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 215 |
{ |
{ |
| 216 |
bzero(&article, sizeof(ARTICLE)); |
memset(&article, 0, sizeof(ARTICLE)); |
| 217 |
|
|
| 218 |
// copy data of article |
// copy data of article |
| 219 |
i = 0; |
i = 0; |
| 312 |
} |
} |
| 313 |
|
|
| 314 |
// release global lock |
// release global lock |
| 315 |
if (global_lock) |
if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0) |
| 316 |
{ |
{ |
| 317 |
if ((ret = section_list_rw_unlock(NULL)) < 0) |
log_error("section_list_rw_unlock(sid=0) error\n"); |
|
{ |
|
|
log_error("section_list_rw_unlock(sid=0) error\n"); |
|
|
} |
|
| 318 |
} |
} |
| 319 |
|
|
| 320 |
cleanup: |
cleanup: |
| 321 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 322 |
mysql_close(db); |
mysql_close(db); |
| 323 |
|
|
| 324 |
|
article_cache_cleanup(); |
| 325 |
|
|
| 326 |
return (ret < 0 ? ret : article_count); |
return (ret < 0 ? ret : article_count); |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
int set_last_article_op_log_from_db(void) |
int set_last_article_op_log_from_db(void) |
| 330 |
{ |
{ |
| 331 |
MYSQL *db; |
MYSQL *db = NULL; |
| 332 |
MYSQL_RES *rs; |
MYSQL_RES *rs = NULL; |
| 333 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 334 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 335 |
|
int ret = 0; |
| 336 |
|
|
| 337 |
db = db_open(); |
db = db_open(); |
| 338 |
if (db == NULL) |
if (db == NULL) |
| 339 |
{ |
{ |
| 340 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 341 |
return -1; |
ret = -1; |
| 342 |
|
goto cleanup; |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 348 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 349 |
{ |
{ |
| 350 |
log_error("Query article op error: %s\n", mysql_error(db)); |
log_error("Query article op error: %s\n", mysql_error(db)); |
| 351 |
return -2; |
ret = -2; |
| 352 |
|
goto cleanup; |
| 353 |
} |
} |
| 354 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 355 |
{ |
{ |
| 356 |
log_error("Get article op data failed\n"); |
log_error("Get article op data failed\n"); |
| 357 |
return -2; |
ret = -2; |
| 358 |
|
goto cleanup; |
| 359 |
} |
} |
| 360 |
|
|
| 361 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 363 |
last_article_op_log_mid = atoi(row[0]); |
last_article_op_log_mid = atoi(row[0]); |
| 364 |
} |
} |
| 365 |
|
|
| 366 |
|
cleanup: |
| 367 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 368 |
mysql_close(db); |
mysql_close(db); |
| 369 |
|
|
| 370 |
return last_article_op_log_mid; |
return (ret < 0 ? ret : last_article_op_log_mid); |
| 371 |
} |
} |
| 372 |
|
|
| 373 |
int apply_article_op_log_from_db(int op_count_limit) |
int apply_article_op_log_from_db(int op_count_limit) |
| 374 |
{ |
{ |
| 375 |
MYSQL *db; |
MYSQL *db = NULL; |
| 376 |
MYSQL_RES *rs, *rs2; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 377 |
MYSQL_ROW row, row2; |
MYSQL_ROW row, row2; |
| 378 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 379 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 389 |
if (db == NULL) |
if (db == NULL) |
| 390 |
{ |
{ |
| 391 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 392 |
return -3; |
ret = -1; |
| 393 |
|
goto cleanup; |
| 394 |
} |
} |
| 395 |
|
|
| 396 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 402 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 403 |
{ |
{ |
| 404 |
log_error("Query article log error: %s\n", mysql_error(db)); |
log_error("Query article log error: %s\n", mysql_error(db)); |
| 405 |
return -3; |
ret = -2; |
| 406 |
|
goto cleanup; |
| 407 |
} |
} |
| 408 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 409 |
{ |
{ |
| 410 |
log_error("Get article log data failed\n"); |
log_error("Get article log data failed\n"); |
| 411 |
return -3; |
ret = -2; |
| 412 |
|
goto cleanup; |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 521 |
ret = -4; |
ret = -4; |
| 522 |
} |
} |
| 523 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 524 |
|
rs2 = NULL; |
| 525 |
|
|
| 526 |
p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition |
p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition |
| 527 |
break; |
break; |
| 552 |
ret = -4; |
ret = -4; |
| 553 |
} |
} |
| 554 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 555 |
|
rs2 = NULL; |
| 556 |
|
|
| 557 |
if (sid_dest > 0 && sid_dest != p_article->sid) |
if (sid_dest > 0 && sid_dest != p_article->sid) |
| 558 |
{ |
{ |
| 622 |
} |
} |
| 623 |
} |
} |
| 624 |
|
|
| 625 |
|
cleanup: |
| 626 |
|
mysql_free_result(rs2); |
| 627 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 628 |
mysql_close(db); |
mysql_close(db); |
| 629 |
|
|
| 630 |
return (ret < 0 ? ret : op_count); |
return (ret < 0 ? ret : op_count); |
| 683 |
} |
} |
| 684 |
else |
else |
| 685 |
{ |
{ |
| 686 |
log_error("Reload section config successfully\n"); |
log_common("Reload section config successfully\n"); |
| 687 |
} |
} |
| 688 |
} |
} |
| 689 |
|
|
| 750 |
|
|
| 751 |
// Child process exit |
// Child process exit |
| 752 |
|
|
| 753 |
|
article_cache_cleanup(); |
| 754 |
|
|
| 755 |
// Detach data pools shm |
// Detach data pools shm |
| 756 |
detach_section_list_shm(); |
detach_section_list_shm(); |
| 757 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 848 |
return ret; |
return ret; |
| 849 |
} |
} |
| 850 |
|
|
| 851 |
int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, |
int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step, |
| 852 |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
int *p_page_id, int *p_visible_offset, int *p_article_count) |
| 853 |
{ |
{ |
| 854 |
const ARTICLE *p_article = NULL; |
const ARTICLE *p_article = NULL; |
| 855 |
ARTICLE *p_tmp; |
ARTICLE *p_tmp; |
| 856 |
int32_t aid = 0; |
int32_t aid = 0; |
| 857 |
int page_id; |
int page_id = 0; |
| 858 |
int offset; |
int offset = 0; |
| 859 |
int ret = 0; |
int ret = 0; |
| 860 |
int i; |
int i; |
| 861 |
|
|
| 878 |
} |
} |
| 879 |
else if (direction == 1) |
else if (direction == 1) |
| 880 |
{ |
{ |
| 881 |
p_article = p_article_cur; |
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) |
|
do |
|
| 882 |
{ |
{ |
| 883 |
p_article = p_article->p_topic_next; |
if (p_article->visible) |
| 884 |
} while (p_article != p_article_cur && p_article->visible == 0); |
{ |
| 885 |
|
step--; |
| 886 |
|
} |
| 887 |
|
} |
| 888 |
|
|
| 889 |
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); |
| 890 |
} |
} |
| 891 |
else if (direction == -1) |
else if (direction == -1) |
| 892 |
{ |
{ |
| 893 |
p_article = p_article_cur; |
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) |
|
do |
|
| 894 |
{ |
{ |
| 895 |
p_article = p_article->p_topic_prior; |
if (p_article->visible) |
| 896 |
} while (p_article != p_article_cur && p_article->visible == 0); |
{ |
| 897 |
|
step--; |
| 898 |
|
} |
| 899 |
|
} |
| 900 |
|
|
| 901 |
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); |
| 902 |
} |
} |
| 903 |
|
|
| 904 |
p_article = NULL; |
p_article = NULL; |
| 911 |
*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); |
| 912 |
|
|
| 913 |
p_article = p_section->p_page_first_article[page_id]; |
p_article = p_section->p_page_first_article[page_id]; |
| 914 |
for (i = 0; i < *p_article_count;) |
for (i = 0; i < *p_article_count && p_article != NULL;) |
| 915 |
{ |
{ |
| 916 |
if (p_article->visible) |
if (p_article->visible) |
| 917 |
{ |
{ |
| 926 |
{ |
{ |
| 927 |
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); |
| 928 |
p_article = NULL; |
p_article = NULL; |
| 929 |
|
break; |
| 930 |
} |
} |
| 931 |
} |
} |
| 932 |
p_article = p_article->p_next; |
p_article = p_article->p_next; |