| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#define _POSIX_C_SOURCE 200809L |
| 18 |
|
|
| 19 |
#include "section_list_loader.h" |
#include "section_list_loader.h" |
| 20 |
|
#include "article_cache.h" |
| 21 |
|
#include "bbs.h" |
| 22 |
#include "log.h" |
#include "log.h" |
| 23 |
#include "database.h" |
#include "database.h" |
| 24 |
#include "menu.h" |
#include "menu.h" |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <signal.h> |
#include <signal.h> |
| 29 |
#include <stdlib.h> |
#include <stdlib.h> |
| 30 |
|
#include <string.h> |
| 31 |
#include <strings.h> |
#include <strings.h> |
| 32 |
#include <unistd.h> |
#include <unistd.h> |
| 33 |
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
|
#include <string.h> |
|
|
|
|
|
#define SECTION_LIST_LOAD_INTERVAL 10 // second |
|
|
|
|
| 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(void) |
| 38 |
{ |
{ |
| 39 |
MYSQL *db; |
MYSQL *db = NULL; |
| 40 |
MYSQL_RES *rs, *rs2; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 41 |
MYSQL_ROW row, row2; |
MYSQL_ROW row, row2; |
| 42 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 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 |
int ret; |
int ret = 0; |
| 47 |
|
|
| 48 |
db = db_open(); |
db = db_open(); |
| 49 |
if (db == NULL) |
if (db == NULL) |
| 50 |
{ |
{ |
| 51 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 52 |
return -2; |
ret = -1; |
| 53 |
|
goto cleanup; |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 62 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 63 |
{ |
{ |
| 64 |
log_error("Query section_list error: %s\n", mysql_error(db)); |
log_error("Query section_list error: %s\n", mysql_error(db)); |
| 65 |
return -3; |
ret = -2; |
| 66 |
|
goto cleanup; |
| 67 |
} |
} |
| 68 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 69 |
{ |
{ |
| 70 |
log_error("Get section_list data failed\n"); |
log_error("Get section_list data failed\n"); |
| 71 |
return -3; |
ret = -2; |
| 72 |
|
goto cleanup; |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
ret = 0; |
ret = 0; |
| 105 |
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))); |
| 106 |
} |
} |
| 107 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 108 |
|
rs2 = NULL; |
| 109 |
|
|
| 110 |
p_section = section_list_find_by_sid(sid); |
p_section = section_list_find_by_sid(sid); |
| 111 |
|
|
| 155 |
break; |
break; |
| 156 |
} |
} |
| 157 |
} |
} |
|
mysql_free_result(rs); |
|
| 158 |
|
|
| 159 |
|
cleanup: |
| 160 |
|
mysql_free_result(rs); |
| 161 |
mysql_close(db); |
mysql_close(db); |
| 162 |
|
|
| 163 |
return ret; |
return ret; |
| 165 |
|
|
| 166 |
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) |
| 167 |
{ |
{ |
| 168 |
MYSQL *db; |
MYSQL *db = NULL; |
| 169 |
MYSQL_RES *rs; |
MYSQL_RES *rs = NULL; |
| 170 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 171 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 172 |
ARTICLE article; |
ARTICLE article; |
| 173 |
ARTICLE *p_topic; |
ARTICLE *p_topic; |
| 174 |
SECTION_LIST *p_section = NULL; |
SECTION_LIST *p_section = NULL; |
| 175 |
int32_t last_sid = 0; |
int32_t last_sid = 0; |
| 176 |
|
char sub_ip[IP_ADDR_LEN]; |
| 177 |
int article_count = 0; |
int article_count = 0; |
| 178 |
int ret = 0; |
int ret = 0; |
| 179 |
int i; |
int i; |
| 182 |
if (db == NULL) |
if (db == NULL) |
| 183 |
{ |
{ |
| 184 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 185 |
return -2; |
ret = -1; |
| 186 |
|
goto cleanup; |
| 187 |
} |
} |
| 188 |
|
|
| 189 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 190 |
"SELECT AID, TID, SID, CID, UID, visible, excerption, ontop, `lock`, " |
"SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, " |
| 191 |
"transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt " |
"transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, " |
| 192 |
"FROM bbs WHERE AID >= %d ORDER BY AID LIMIT %d", |
"sub_ip, bbs_content.content " |
| 193 |
|
"FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID " |
| 194 |
|
"WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d", |
| 195 |
start_aid, article_count_limit); |
start_aid, article_count_limit); |
| 196 |
|
|
| 197 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 198 |
{ |
{ |
| 199 |
log_error("Query article list error: %s\n", mysql_error(db)); |
log_error("Query article list error: %s\n", mysql_error(db)); |
| 200 |
return -3; |
ret = -2; |
| 201 |
|
goto cleanup; |
| 202 |
} |
} |
| 203 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_use_result(db)) == NULL) |
| 204 |
{ |
{ |
| 205 |
log_error("Get article list data failed\n"); |
log_error("Get article list data failed\n"); |
| 206 |
return -3; |
ret = -2; |
| 207 |
|
goto cleanup; |
| 208 |
} |
} |
| 209 |
|
|
| 210 |
// acquire global lock |
// acquire global lock |
| 211 |
if (global_lock) |
if (global_lock && (ret = section_list_rw_lock(NULL)) < 0) |
| 212 |
{ |
{ |
| 213 |
if ((ret = section_list_rw_lock(NULL)) < 0) |
log_error("section_list_rw_lock(sid = 0) error\n"); |
| 214 |
{ |
goto cleanup; |
|
log_error("section_list_rw_lock(sid = 0) error\n"); |
|
|
goto cleanup; |
|
|
} |
|
| 215 |
} |
} |
| 216 |
|
|
| 217 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 218 |
{ |
{ |
| 219 |
bzero(&article, sizeof(ARTICLE)); |
memset(&article, 0, sizeof(ARTICLE)); |
| 220 |
|
|
| 221 |
// copy data of article |
// copy data of article |
| 222 |
i = 0; |
i = 0; |
| 241 |
|
|
| 242 |
article.sub_dt = atol(row[i++]); |
article.sub_dt = atol(row[i++]); |
| 243 |
|
|
| 244 |
|
strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1); |
| 245 |
|
sub_ip[sizeof(sub_ip) - 1] = '\0'; |
| 246 |
|
ip_mask(sub_ip, 1, '*'); |
| 247 |
|
|
| 248 |
// release lock of last section if different from current one |
// release lock of last section if different from current one |
| 249 |
if (!global_lock && article.sid != last_sid && last_sid != 0) |
if (!global_lock && article.sid != last_sid && last_sid != 0) |
| 250 |
{ |
{ |
| 251 |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
| 252 |
{ |
{ |
| 253 |
log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid); |
log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid); |
| 254 |
break; |
break; |
| 255 |
} |
} |
| 256 |
} |
} |
| 279 |
{ |
{ |
| 280 |
if ((ret = section_list_rw_lock(p_section)) < 0) |
if ((ret = section_list_rw_lock(p_section)) < 0) |
| 281 |
{ |
{ |
| 282 |
log_error("section_list_rw_lock(sid = 0) error\n"); |
log_error("section_list_rw_lock(sid=0) error\n"); |
| 283 |
break; |
break; |
| 284 |
} |
} |
| 285 |
} |
} |
| 289 |
|
|
| 290 |
if (section_list_append_article(p_section, &article) < 0) |
if (section_list_append_article(p_section, &article) < 0) |
| 291 |
{ |
{ |
| 292 |
log_error("section_list_append_article(sid = %d, aid = %d) error\n", |
log_error("section_list_append_article(sid=%d, aid=%d) error\n", |
| 293 |
p_section->sid, article.aid); |
p_section->sid, article.aid); |
| 294 |
ret = -3; |
ret = -3; |
| 295 |
break; |
break; |
| 297 |
|
|
| 298 |
article_count++; |
article_count++; |
| 299 |
|
|
| 300 |
// TODO: generate content cache |
if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0) |
| 301 |
|
{ |
| 302 |
|
log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid); |
| 303 |
|
ret = -4; |
| 304 |
|
break; |
| 305 |
|
} |
| 306 |
} |
} |
| 307 |
|
|
| 308 |
// release lock of last section |
// release lock of last section |
| 310 |
{ |
{ |
| 311 |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
if ((ret = section_list_rw_unlock(p_section)) < 0) |
| 312 |
{ |
{ |
| 313 |
log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid); |
log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid); |
| 314 |
} |
} |
| 315 |
} |
} |
| 316 |
|
|
| 317 |
// release global lock |
// release global lock |
| 318 |
if (global_lock) |
if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0) |
| 319 |
{ |
{ |
| 320 |
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"); |
|
|
} |
|
| 321 |
} |
} |
| 322 |
|
|
| 323 |
cleanup: |
cleanup: |
| 324 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 325 |
mysql_close(db); |
mysql_close(db); |
| 326 |
|
|
| 327 |
return (ret < 0 ? ret : article_count); |
return (ret < 0 ? ret : article_count); |
| 329 |
|
|
| 330 |
int set_last_article_op_log_from_db(void) |
int set_last_article_op_log_from_db(void) |
| 331 |
{ |
{ |
| 332 |
MYSQL *db; |
MYSQL *db = NULL; |
| 333 |
MYSQL_RES *rs; |
MYSQL_RES *rs = NULL; |
| 334 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 335 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 336 |
|
int ret = 0; |
| 337 |
|
|
| 338 |
db = db_open(); |
db = db_open(); |
| 339 |
if (db == NULL) |
if (db == NULL) |
| 340 |
{ |
{ |
| 341 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 342 |
return -1; |
ret = -1; |
| 343 |
|
goto cleanup; |
| 344 |
} |
} |
| 345 |
|
|
| 346 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 349 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 350 |
{ |
{ |
| 351 |
log_error("Query article op error: %s\n", mysql_error(db)); |
log_error("Query article op error: %s\n", mysql_error(db)); |
| 352 |
return -2; |
ret = -2; |
| 353 |
|
goto cleanup; |
| 354 |
} |
} |
| 355 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 356 |
{ |
{ |
| 357 |
log_error("Get article op data failed\n"); |
log_error("Get article op data failed\n"); |
| 358 |
return -2; |
ret = -2; |
| 359 |
|
goto cleanup; |
| 360 |
} |
} |
| 361 |
|
|
| 362 |
if ((row = mysql_fetch_row(rs))) |
if ((row = mysql_fetch_row(rs))) |
| 364 |
last_article_op_log_mid = atoi(row[0]); |
last_article_op_log_mid = atoi(row[0]); |
| 365 |
} |
} |
| 366 |
|
|
| 367 |
|
cleanup: |
| 368 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 369 |
mysql_close(db); |
mysql_close(db); |
| 370 |
|
|
| 371 |
return last_article_op_log_mid; |
return (ret < 0 ? ret : last_article_op_log_mid); |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
int apply_article_op_log_from_db(int op_count_limit) |
int apply_article_op_log_from_db(int op_count_limit) |
| 375 |
{ |
{ |
| 376 |
MYSQL *db; |
MYSQL *db = NULL; |
| 377 |
MYSQL_RES *rs, *rs2; |
MYSQL_RES *rs = NULL, *rs2 = NULL; |
| 378 |
MYSQL_ROW row, row2; |
MYSQL_ROW row, row2; |
| 379 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 380 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 381 |
SECTION_LIST *p_section = NULL; |
SECTION_LIST *p_section = NULL; |
| 382 |
SECTION_LIST *p_section_dest; |
SECTION_LIST *p_section_dest; |
| 383 |
|
char sub_ip[IP_ADDR_LEN]; |
| 384 |
int32_t last_sid = 0; |
int32_t last_sid = 0; |
| 385 |
int32_t sid_dest; |
int32_t sid_dest; |
| 386 |
int op_count = 0; |
int op_count = 0; |
| 390 |
if (db == NULL) |
if (db == NULL) |
| 391 |
{ |
{ |
| 392 |
log_error("db_open() error: %s\n", mysql_error(db)); |
log_error("db_open() error: %s\n", mysql_error(db)); |
| 393 |
return -3; |
ret = -1; |
| 394 |
|
goto cleanup; |
| 395 |
} |
} |
| 396 |
|
|
| 397 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 403 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 404 |
{ |
{ |
| 405 |
log_error("Query article log error: %s\n", mysql_error(db)); |
log_error("Query article log error: %s\n", mysql_error(db)); |
| 406 |
return -3; |
ret = -2; |
| 407 |
|
goto cleanup; |
| 408 |
} |
} |
| 409 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 410 |
{ |
{ |
| 411 |
log_error("Get article log data failed\n"); |
log_error("Get article log data failed\n"); |
| 412 |
return -3; |
ret = -2; |
| 413 |
|
goto cleanup; |
| 414 |
} |
} |
| 415 |
|
|
| 416 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 458 |
break; |
break; |
| 459 |
case 'D': // Delete article |
case 'D': // Delete article |
| 460 |
case 'X': // Delete article by Admin |
case 'X': // Delete article by Admin |
| 461 |
p_article->visible = 0; |
if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0) |
|
if (p_article->tid == 0) |
|
| 462 |
{ |
{ |
| 463 |
// Set articles in the topic to be invisible |
log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1])); |
| 464 |
do |
} |
| 465 |
{ |
if (section_list_calculate_page(p_section, atoi(row[1])) < 0) |
| 466 |
p_article = p_article->p_topic_next; |
{ |
| 467 |
p_article->visible = 0; |
log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1])); |
|
} while (p_article->tid != 0); |
|
| 468 |
} |
} |
| 469 |
break; |
break; |
| 470 |
case 'S': // Restore article |
case 'S': // Restore article |
| 471 |
p_article->visible = 1; |
if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0) |
| 472 |
|
{ |
| 473 |
|
log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1])); |
| 474 |
|
} |
| 475 |
|
if (section_list_calculate_page(p_section, atoi(row[1])) < 0) |
| 476 |
|
{ |
| 477 |
|
log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1])); |
| 478 |
|
} |
| 479 |
break; |
break; |
| 480 |
case 'L': // Lock article |
case 'L': // Lock article |
| 481 |
p_article->lock = 1; |
p_article->lock = 1; |
| 485 |
break; |
break; |
| 486 |
case 'M': // Modify article |
case 'M': // Modify article |
| 487 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 488 |
"SELECT CID FROM bbs WHERE AID = %d", |
"SELECT bbs.CID, sub_ip, bbs_content.content " |
| 489 |
|
"FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID " |
| 490 |
|
"WHERE bbs.AID = %d", |
| 491 |
p_article->aid); |
p_article->aid); |
| 492 |
|
|
| 493 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 496 |
ret = -3; |
ret = -3; |
| 497 |
break; |
break; |
| 498 |
} |
} |
| 499 |
if ((rs2 = mysql_store_result(db)) == NULL) |
if ((rs2 = mysql_use_result(db)) == NULL) |
| 500 |
{ |
{ |
| 501 |
log_error("Get article data failed\n"); |
log_error("Get article data failed\n"); |
| 502 |
ret = -3; |
ret = -3; |
| 505 |
if ((row2 = mysql_fetch_row(rs2))) |
if ((row2 = mysql_fetch_row(rs2))) |
| 506 |
{ |
{ |
| 507 |
p_article->cid = atoi(row2[0]); |
p_article->cid = atoi(row2[0]); |
| 508 |
|
|
| 509 |
|
strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1); |
| 510 |
|
sub_ip[sizeof(sub_ip) - 1] = '\0'; |
| 511 |
|
ip_mask(sub_ip, 1, '*'); |
| 512 |
|
|
| 513 |
|
if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0) |
| 514 |
|
{ |
| 515 |
|
log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid); |
| 516 |
|
ret = -4; |
| 517 |
|
} |
| 518 |
} |
} |
| 519 |
else |
else |
| 520 |
{ |
{ |
| 522 |
ret = -4; |
ret = -4; |
| 523 |
} |
} |
| 524 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 525 |
|
rs2 = NULL; |
| 526 |
|
|
| 527 |
if (p_article->cid > 0) |
p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition |
|
{ |
|
|
// TODO: generate content cache |
|
|
} |
|
|
|
|
| 528 |
break; |
break; |
| 529 |
case 'T': // Move article |
case 'T': // Move article |
| 530 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 553 |
ret = -4; |
ret = -4; |
| 554 |
} |
} |
| 555 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 556 |
|
rs2 = NULL; |
| 557 |
|
|
| 558 |
if (sid_dest > 0 && sid_dest != p_article->sid) |
if (sid_dest > 0 && sid_dest != p_article->sid) |
| 559 |
{ |
{ |
| 623 |
} |
} |
| 624 |
} |
} |
| 625 |
|
|
| 626 |
|
cleanup: |
| 627 |
|
mysql_free_result(rs2); |
| 628 |
mysql_free_result(rs); |
mysql_free_result(rs); |
|
|
|
| 629 |
mysql_close(db); |
mysql_close(db); |
| 630 |
|
|
| 631 |
return (ret < 0 ? ret : op_count); |
return (ret < 0 ? ret : op_count); |
| 653 |
{ |
{ |
| 654 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 655 |
section_list_loader_pid = pid; |
section_list_loader_pid = pid; |
| 656 |
log_std("Section list loader process (%d) start\n", pid); |
log_common("Section list loader process (pid = %d) start\n", pid); |
| 657 |
return 0; |
return 0; |
| 658 |
} |
} |
| 659 |
else if (pid < 0) // Error |
else if (pid < 0) // Error |
| 709 |
load_count = article_block_article_count() - article_count; |
load_count = article_block_article_count() - article_count; |
| 710 |
if (load_count > 0) |
if (load_count > 0) |
| 711 |
{ |
{ |
| 712 |
log_std("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()); |
| 713 |
} |
} |
| 714 |
|
|
| 715 |
if (SYS_section_list_reload) |
if (SYS_section_list_reload) |
| 735 |
|
|
| 736 |
if (last_article_op_log_mid > last_mid) |
if (last_article_op_log_mid > last_mid) |
| 737 |
{ |
{ |
| 738 |
log_std("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); |
| 739 |
} |
} |
| 740 |
|
|
| 741 |
if (SYS_section_list_reload) |
if (SYS_section_list_reload) |
| 743 |
continue; |
continue; |
| 744 |
} |
} |
| 745 |
|
|
| 746 |
for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++) |
for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++) |
| 747 |
{ |
{ |
| 748 |
sleep(1); |
sleep(1); |
| 749 |
} |
} |
| 756 |
detach_article_block_shm(); |
detach_article_block_shm(); |
| 757 |
detach_trie_dict_shm(); |
detach_trie_dict_shm(); |
| 758 |
|
|
| 759 |
log_std("Section list loader process exit normally\n"); |
log_common("Section list loader process exit normally\n"); |
| 760 |
log_end(); |
log_end(); |
| 761 |
|
|
| 762 |
section_list_loader_pid = 0; |
section_list_loader_pid = 0; |
| 783 |
return 0; |
return 0; |
| 784 |
} |
} |
| 785 |
|
|
| 786 |
int query_section_articles(SECTION_LIST *p_section, int32_t page_id, ARTICLE *p_articles[], int32_t *p_article_count) |
int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count) |
| 787 |
{ |
{ |
| 788 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 789 |
ARTICLE *p_next_page_first_article; |
ARTICLE *p_next_page_first_article; |
| 790 |
int ret = 0; |
int ret = 0; |
| 791 |
|
|
| 792 |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL) |
if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL) |
| 793 |
{ |
{ |
| 794 |
log_error("query_section_articles() NULL pointer error\n"); |
log_error("query_section_articles() NULL pointer error\n"); |
| 795 |
return -1; |
return -1; |
| 802 |
return -2; |
return -2; |
| 803 |
} |
} |
| 804 |
|
|
| 805 |
|
*p_page_count = p_section->page_count; |
| 806 |
|
|
| 807 |
if (p_section->visible_article_count == 0) |
if (p_section->visible_article_count == 0) |
| 808 |
{ |
{ |
| 809 |
*p_article_count = 0; |
*p_article_count = 0; |
| 846 |
|
|
| 847 |
return ret; |
return ret; |
| 848 |
} |
} |
| 849 |
|
|
| 850 |
|
int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step, |
| 851 |
|
int *p_page_id, int *p_visible_offset, int *p_article_count) |
| 852 |
|
{ |
| 853 |
|
const ARTICLE *p_article = NULL; |
| 854 |
|
ARTICLE *p_tmp; |
| 855 |
|
int32_t aid = 0; |
| 856 |
|
int page_id = 0; |
| 857 |
|
int offset = 0; |
| 858 |
|
int ret = 0; |
| 859 |
|
int i; |
| 860 |
|
|
| 861 |
|
if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL) |
| 862 |
|
{ |
| 863 |
|
log_error("locate_article_in_section() NULL pointer error\n"); |
| 864 |
|
return -1; |
| 865 |
|
} |
| 866 |
|
|
| 867 |
|
// acquire lock of section |
| 868 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 869 |
|
{ |
| 870 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 871 |
|
return -2; |
| 872 |
|
} |
| 873 |
|
|
| 874 |
|
if (direction == 0) |
| 875 |
|
{ |
| 876 |
|
aid = p_article_cur->aid; |
| 877 |
|
} |
| 878 |
|
else if (direction == 1) |
| 879 |
|
{ |
| 880 |
|
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) |
| 881 |
|
{ |
| 882 |
|
if (p_article->visible) |
| 883 |
|
{ |
| 884 |
|
step--; |
| 885 |
|
} |
| 886 |
|
} |
| 887 |
|
|
| 888 |
|
aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 889 |
|
} |
| 890 |
|
else if (direction == -1) |
| 891 |
|
{ |
| 892 |
|
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) |
| 893 |
|
{ |
| 894 |
|
if (p_article->visible) |
| 895 |
|
{ |
| 896 |
|
step--; |
| 897 |
|
} |
| 898 |
|
} |
| 899 |
|
|
| 900 |
|
aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0); |
| 901 |
|
} |
| 902 |
|
|
| 903 |
|
p_article = NULL; |
| 904 |
|
|
| 905 |
|
if (aid > 0) |
| 906 |
|
{ |
| 907 |
|
p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp); |
| 908 |
|
if (p_article != NULL) |
| 909 |
|
{ |
| 910 |
|
*p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page); |
| 911 |
|
|
| 912 |
|
p_article = p_section->p_page_first_article[page_id]; |
| 913 |
|
for (i = 0; i < *p_article_count && p_article != NULL;) |
| 914 |
|
{ |
| 915 |
|
if (p_article->visible) |
| 916 |
|
{ |
| 917 |
|
if (p_article->aid == aid) |
| 918 |
|
{ |
| 919 |
|
*p_page_id = page_id; |
| 920 |
|
*p_visible_offset = i; |
| 921 |
|
break; |
| 922 |
|
} |
| 923 |
|
i++; |
| 924 |
|
if (i >= *p_article_count) |
| 925 |
|
{ |
| 926 |
|
log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id); |
| 927 |
|
p_article = NULL; |
| 928 |
|
break; |
| 929 |
|
} |
| 930 |
|
} |
| 931 |
|
p_article = p_article->p_next; |
| 932 |
|
} |
| 933 |
|
} |
| 934 |
|
} |
| 935 |
|
|
| 936 |
|
// release lock of section |
| 937 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 938 |
|
{ |
| 939 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 940 |
|
ret = -2; |
| 941 |
|
} |
| 942 |
|
|
| 943 |
|
return (ret < 0 ? ret : (p_article == NULL ? 0 : 1)); |
| 944 |
|
} |