| 17 |
#include "log.h" |
#include "log.h" |
| 18 |
#include "section_list.h" |
#include "section_list.h" |
| 19 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 20 |
|
#include "user_list.h" |
| 21 |
#include <errno.h> |
#include <errno.h> |
| 22 |
#include <signal.h> |
#include <signal.h> |
| 23 |
#include <stdio.h> |
#include <stdio.h> |
| 76 |
|
|
| 77 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 78 |
|
|
| 79 |
struct section_list_pool_t |
SECTION_LIST_POOL *p_section_list_pool = NULL; |
|
{ |
|
|
int shmid; |
|
|
SECTION_LIST sections[BBS_max_section]; |
|
|
int section_count; |
|
|
int semid; |
|
|
TRIE_NODE *p_trie_dict_section_by_name; |
|
|
TRIE_NODE *p_trie_dict_section_by_sid; |
|
|
}; |
|
|
typedef struct section_list_pool_t SECTION_LIST_POOL; |
|
|
|
|
|
static SECTION_LIST_POOL *p_section_list_pool = NULL; |
|
| 80 |
|
|
| 81 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 82 |
{ |
{ |
| 343 |
} |
} |
| 344 |
|
|
| 345 |
left = 0; |
left = 0; |
| 346 |
right = p_article_block_pool->block_count; |
right = p_article_block_pool->block_count - 1; |
| 347 |
|
|
| 348 |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ] |
// aid in the range [ head aid of blocks[left], tail aid of blocks[right] ] |
| 349 |
while (left < right - 1) |
while (left < right) |
| 350 |
{ |
{ |
| 351 |
// get block offset no less than mid value of left and right block offsets |
// get block offset no less than mid value of left and right block offsets |
| 352 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_article_block_pool->block_count) |
|
|
{ |
|
|
log_error("block(mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 353 |
|
|
| 354 |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
| 355 |
{ |
{ |
| 356 |
right = mid; |
right = mid - 1; |
| 357 |
} |
} |
| 358 |
else |
else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid) |
| 359 |
{ |
{ |
| 360 |
left = mid; |
left = mid; |
| 361 |
} |
} |
| 614 |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
| 615 |
|
|
| 616 |
p_section->sid = sid; |
p_section->sid = sid; |
| 617 |
|
p_section->ex_menu_tm = 0; |
| 618 |
|
|
| 619 |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 620 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 644 |
return p_section; |
return p_section; |
| 645 |
} |
} |
| 646 |
|
|
| 647 |
|
int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list) |
| 648 |
|
{ |
| 649 |
|
int64_t index; |
| 650 |
|
|
| 651 |
|
if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL) |
| 652 |
|
{ |
| 653 |
|
log_error("NULL pointer error\n"); |
| 654 |
|
return -1; |
| 655 |
|
} |
| 656 |
|
|
| 657 |
|
index = get_section_index(p_section); |
| 658 |
|
|
| 659 |
|
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 660 |
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 661 |
|
|
| 662 |
|
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 663 |
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 664 |
|
|
| 665 |
|
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 666 |
|
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 667 |
|
|
| 668 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0) |
| 669 |
|
{ |
| 670 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, index); |
| 671 |
|
return -2; |
| 672 |
|
} |
| 673 |
|
|
| 674 |
|
return 0; |
| 675 |
|
} |
| 676 |
|
|
| 677 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 678 |
{ |
{ |
| 679 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 752 |
|
|
| 753 |
if (p_section == NULL || p_article_src == NULL) |
if (p_section == NULL || p_article_src == NULL) |
| 754 |
{ |
{ |
| 755 |
log_error("section_list_append_article() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 756 |
return -1; |
return -1; |
| 757 |
} |
} |
| 758 |
|
|
| 864 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 865 |
|
|
| 866 |
// Update page |
// Update page |
| 867 |
if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) || |
if ((p_article->visible && p_section->last_page_visible_article_count == BBS_article_limit_per_page) || |
| 868 |
p_section->article_count == 1) |
p_section->page_count == 0) |
| 869 |
{ |
{ |
| 870 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 871 |
p_section->page_count++; |
p_section->page_count++; |
| 920 |
{ |
{ |
| 921 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 922 |
|
|
| 923 |
|
if (user_article_cnt_inc(p_article->uid, -1) < 0) |
| 924 |
|
{ |
| 925 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid); |
| 926 |
|
} |
| 927 |
|
|
| 928 |
if (p_article->tid == 0) |
if (p_article->tid == 0) |
| 929 |
{ |
{ |
| 930 |
p_section->visible_topic_count--; |
p_section->visible_topic_count--; |
| 943 |
p_reply->visible = 0; |
p_reply->visible = 0; |
| 944 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 945 |
affected_count++; |
affected_count++; |
| 946 |
|
|
| 947 |
|
if (user_article_cnt_inc(p_reply->uid, -1) < 0) |
| 948 |
|
{ |
| 949 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid); |
| 950 |
|
} |
| 951 |
} |
} |
| 952 |
} |
} |
| 953 |
} |
} |
| 960 |
{ |
{ |
| 961 |
p_section->visible_topic_count++; |
p_section->visible_topic_count++; |
| 962 |
} |
} |
| 963 |
|
|
| 964 |
|
if (user_article_cnt_inc(p_article->uid, 1) < 0) |
| 965 |
|
{ |
| 966 |
|
log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid); |
| 967 |
|
} |
| 968 |
} |
} |
| 969 |
|
|
| 970 |
p_article->visible = visible; |
p_article->visible = visible; |
| 1065 |
|
|
| 1066 |
page_count = p_section->page_count - 1 + |
page_count = p_section->page_count - 1 + |
| 1067 |
(p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page + |
(p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page + |
| 1068 |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1); |
((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0); |
| 1069 |
|
|
| 1070 |
|
if (page_count < 0) |
| 1071 |
|
{ |
| 1072 |
|
page_count = 0; |
| 1073 |
|
} |
| 1074 |
|
|
| 1075 |
return page_count; |
return page_count; |
| 1076 |
} |
} |
| 1089 |
} |
} |
| 1090 |
else // if (page_id >= p_section->page_count - 1) |
else // if (page_id >= p_section->page_count - 1) |
| 1091 |
{ |
{ |
| 1092 |
return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count - |
return MIN(MAX(0, |
| 1093 |
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))); |
(p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1094 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))), |
| 1095 |
|
BBS_article_limit_per_page); |
| 1096 |
} |
} |
| 1097 |
} |
} |
| 1098 |
|
|
| 1109 |
|
|
| 1110 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1111 |
{ |
{ |
| 1112 |
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1113 |
return NULL; |
return NULL; |
| 1114 |
} |
} |
| 1115 |
|
|
| 1121 |
} |
} |
| 1122 |
|
|
| 1123 |
left = 0; |
left = 0; |
| 1124 |
right = p_section->page_count; |
right = p_section->page_count - 1; |
| 1125 |
|
|
| 1126 |
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
// aid in the range [ head aid of pages[left], tail aid of pages[right] ] |
| 1127 |
while (left < right - 1) |
while (left < right) |
| 1128 |
{ |
{ |
| 1129 |
// get page id no less than mid value of left page id and right page id |
// get page id no less than mid value of left page id and right page id |
| 1130 |
mid = (left + right) / 2 + (right - left) % 2; |
mid = (left + right) / 2 + (left + right) % 2; |
|
|
|
|
if (mid >= p_section->page_count) |
|
|
{ |
|
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
|
|
return NULL; |
|
|
} |
|
| 1131 |
|
|
| 1132 |
if (aid < p_section->p_page_first_article[mid]->aid) |
if (aid < p_section->p_page_first_article[mid]->aid) |
| 1133 |
{ |
{ |
| 1134 |
right = mid; |
right = mid - 1; |
| 1135 |
} |
} |
| 1136 |
else |
else // if (aid < p_section->p_page_first_article[mid]->aid) |
| 1137 |
{ |
{ |
| 1138 |
left = mid; |
left = mid; |
| 1139 |
} |
} |
| 1195 |
|
|
| 1196 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1197 |
{ |
{ |
| 1198 |
log_error("section_list_calculate_page() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1199 |
return -1; |
return -1; |
| 1200 |
} |
} |
| 1201 |
|
|
| 1284 |
} while (p_article != p_section->p_article_head); |
} while (p_article != p_section->p_article_head); |
| 1285 |
|
|
| 1286 |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 1287 |
p_section->last_page_visible_article_count = visible_article_count; |
p_section->last_page_visible_article_count = (visible_article_count > 0 |
| 1288 |
|
? visible_article_count |
| 1289 |
|
: (page > 0 ? BBS_article_limit_per_page : 0)); |
| 1290 |
|
|
| 1291 |
return 0; |
return 0; |
| 1292 |
} |
} |
| 1356 |
|
|
| 1357 |
if (p_section_src == NULL || p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1358 |
{ |
{ |
| 1359 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1360 |
return -1; |
return -1; |
| 1361 |
} |
} |
| 1362 |
|
|
| 1569 |
return index; |
return index; |
| 1570 |
} |
} |
| 1571 |
|
|
| 1572 |
|
int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list) |
| 1573 |
|
{ |
| 1574 |
|
if (p_section == NULL) |
| 1575 |
|
{ |
| 1576 |
|
log_error("NULL pointer error\n"); |
| 1577 |
|
return -1; |
| 1578 |
|
} |
| 1579 |
|
|
| 1580 |
|
if (section_list_rd_lock(p_section) < 0) |
| 1581 |
|
{ |
| 1582 |
|
log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid); |
| 1583 |
|
return -2; |
| 1584 |
|
} |
| 1585 |
|
|
| 1586 |
|
if (sname != NULL) |
| 1587 |
|
{ |
| 1588 |
|
memcpy(sname, p_section->sname, sizeof(p_section->sname)); |
| 1589 |
|
} |
| 1590 |
|
if (stitle != NULL) |
| 1591 |
|
{ |
| 1592 |
|
memcpy(stitle, p_section->stitle, sizeof(p_section->stitle)); |
| 1593 |
|
} |
| 1594 |
|
if (master_list != NULL) |
| 1595 |
|
{ |
| 1596 |
|
memcpy(master_list, p_section->master_list, sizeof(p_section->master_list)); |
| 1597 |
|
} |
| 1598 |
|
|
| 1599 |
|
// release lock of section |
| 1600 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1601 |
|
{ |
| 1602 |
|
log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid); |
| 1603 |
|
return -2; |
| 1604 |
|
} |
| 1605 |
|
|
| 1606 |
|
return 0; |
| 1607 |
|
} |
| 1608 |
|
|
| 1609 |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1610 |
{ |
{ |
| 1611 |
int index; |
int index; |
| 1765 |
timer++; |
timer++; |
| 1766 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1767 |
{ |
{ |
| 1768 |
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid); |
| 1769 |
} |
} |
| 1770 |
} |
} |
| 1771 |
else // failed |
else // failed |
| 1772 |
{ |
{ |
| 1773 |
log_error("section_list_rd_lock() failed on section %d\n", sid); |
log_error("section_list_try_rd_lock() failed on section %d\n", sid); |
| 1774 |
break; |
break; |
| 1775 |
} |
} |
| 1776 |
} |
} |
| 1796 |
timer++; |
timer++; |
| 1797 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1798 |
{ |
{ |
| 1799 |
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid); |
| 1800 |
} |
} |
| 1801 |
} |
} |
| 1802 |
else // failed |
else // failed |
| 1803 |
{ |
{ |
| 1804 |
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
log_error("section_list_try_rw_lock() failed on section %d\n", sid); |
| 1805 |
break; |
break; |
| 1806 |
} |
} |
| 1807 |
} |
} |