| 26 |
#include <strings.h> |
#include <strings.h> |
| 27 |
#include <unistd.h> |
#include <unistd.h> |
| 28 |
|
|
| 29 |
|
#define _POSIX_C_SOURCE 200809L |
| 30 |
|
#include <string.h> |
| 31 |
|
|
| 32 |
#define SECTION_LIST_LOAD_INTERVAL 10 // second |
#define SECTION_LIST_LOAD_INTERVAL 10 // second |
| 33 |
|
|
| 34 |
int section_list_loader_pid; |
int section_list_loader_pid; |
| 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_name[BBS_username_max_len + 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; |
| 47 |
|
|
| 79 |
"SELECT username FROM section_master " |
"SELECT username FROM section_master " |
| 80 |
"INNER JOIN user_list ON section_master.UID = user_list.UID " |
"INNER JOIN user_list ON section_master.UID = user_list.UID " |
| 81 |
"WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) " |
"WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) " |
| 82 |
"ORDER BY major DESC LIMIT 1", |
"ORDER BY major DESC, begin_dt ASC LIMIT 3", |
| 83 |
sid); |
sid); |
| 84 |
|
|
| 85 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 94 |
ret = -3; |
ret = -3; |
| 95 |
break; |
break; |
| 96 |
} |
} |
| 97 |
if ((row2 = mysql_fetch_row(rs2))) |
|
| 98 |
{ |
master_list[0] = '\0'; |
| 99 |
strncpy(master_name, row2[0], sizeof(master_name) - 1); |
while ((row2 = mysql_fetch_row(rs2))) |
|
master_name[sizeof(master_name) - 1] = '\0'; |
|
|
} |
|
|
else |
|
| 100 |
{ |
{ |
| 101 |
master_name[0] = '\0'; |
strncat(master_list, row2[0], sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list))); |
| 102 |
|
strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list))); |
| 103 |
} |
} |
| 104 |
mysql_free_result(rs2); |
mysql_free_result(rs2); |
| 105 |
|
|
| 107 |
|
|
| 108 |
if (p_section == NULL) |
if (p_section == NULL) |
| 109 |
{ |
{ |
| 110 |
p_section = section_list_create(sid, row[1], row[2], ""); |
p_section = section_list_create(sid, row[1], row[2], master_list); |
| 111 |
if (p_section == NULL) |
if (p_section == NULL) |
| 112 |
{ |
{ |
| 113 |
log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]); |
log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[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'; |
| 138 |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1); |
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 139 |
p_section->master_name[sizeof(p_section->master_name) - 1] = '\0'; |
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 140 |
} |
} |
| 141 |
|
|
| 142 |
p_section->class_id = atoi(row[3]); |
p_section->class_id = atoi(row[3]); |
| 489 |
ret = ERR_UNKNOWN_SECTION; |
ret = ERR_UNKNOWN_SECTION; |
| 490 |
break; |
break; |
| 491 |
} |
} |
| 492 |
|
// acquire lock of dest section |
| 493 |
|
if ((ret = section_list_rw_lock(p_section_dest)) < 0) |
| 494 |
|
{ |
| 495 |
|
log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest); |
| 496 |
|
break; |
| 497 |
|
} |
| 498 |
// Move topic |
// Move topic |
| 499 |
if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0) |
if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0) |
| 500 |
{ |
{ |
| 501 |
break; |
log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n", |
| 502 |
|
p_section->sid, p_section_dest->sid, p_article->aid, ret); |
| 503 |
|
} |
| 504 |
|
// release lock of dest section |
| 505 |
|
if (section_list_rw_unlock(p_section_dest) < 0) |
| 506 |
|
{ |
| 507 |
|
log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest); |
| 508 |
|
ret = -1; |
| 509 |
} |
} |
| 510 |
} |
} |
| 511 |
break; |
break; |
| 699 |
|
|
| 700 |
return 0; |
return 0; |
| 701 |
} |
} |
| 702 |
|
|
| 703 |
|
int query_section_articles(SECTION_LIST *p_section, int32_t page_id, ARTICLE *p_articles[], int32_t *p_article_count) |
| 704 |
|
{ |
| 705 |
|
ARTICLE *p_article; |
| 706 |
|
ARTICLE *p_next_page_first_article; |
| 707 |
|
int ret = 0; |
| 708 |
|
|
| 709 |
|
if (p_section == NULL || p_articles == NULL || p_article_count == NULL) |
| 710 |
|
{ |
| 711 |
|
log_error("query_section_articles() NULL pointer error\n"); |
| 712 |
|
return -1; |
| 713 |
|
} |
| 714 |
|
|
| 715 |
|
// acquire lock of section |
| 716 |
|
if ((ret = section_list_rd_lock(p_section)) < 0) |
| 717 |
|
{ |
| 718 |
|
log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid); |
| 719 |
|
return -2; |
| 720 |
|
} |
| 721 |
|
|
| 722 |
|
if (page_id < 0 || page_id >= p_section->page_count) |
| 723 |
|
{ |
| 724 |
|
log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count); |
| 725 |
|
ret = -3; |
| 726 |
|
} |
| 727 |
|
else |
| 728 |
|
{ |
| 729 |
|
ret = page_id; |
| 730 |
|
p_article = p_section->p_page_first_article[page_id]; |
| 731 |
|
p_next_page_first_article = |
| 732 |
|
(page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]); |
| 733 |
|
*p_article_count = 0; |
| 734 |
|
|
| 735 |
|
do |
| 736 |
|
{ |
| 737 |
|
if (p_article->visible) |
| 738 |
|
{ |
| 739 |
|
p_articles[*p_article_count] = p_article; |
| 740 |
|
(*p_article_count)++; |
| 741 |
|
} |
| 742 |
|
p_article = p_article->p_next; |
| 743 |
|
} while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page); |
| 744 |
|
|
| 745 |
|
if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count)) |
| 746 |
|
{ |
| 747 |
|
log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id); |
| 748 |
|
} |
| 749 |
|
} |
| 750 |
|
|
| 751 |
|
// release lock of section |
| 752 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 753 |
|
{ |
| 754 |
|
log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid); |
| 755 |
|
ret = -2; |
| 756 |
|
} |
| 757 |
|
|
| 758 |
|
return ret; |
| 759 |
|
} |