| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
section_list.c - description |
/* |
| 3 |
------------------- |
* section_list |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - data models and basic operations of section and article |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
|
#define _GNU_SOURCE |
|
|
|
|
|
#include "section_list.h" |
|
| 13 |
#include "log.h" |
#include "log.h" |
| 14 |
|
#include "section_list.h" |
| 15 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 16 |
|
#include "user_list.h" |
| 17 |
|
#include <errno.h> |
| 18 |
|
#include <signal.h> |
| 19 |
#include <stdio.h> |
#include <stdio.h> |
| 20 |
|
#include <stdlib.h> |
| 21 |
#include <string.h> |
#include <string.h> |
|
#include <signal.h> |
|
| 22 |
#include <unistd.h> |
#include <unistd.h> |
| 23 |
#include <stdlib.h> |
#include <sys/ipc.h> |
|
#include <errno.h> |
|
| 24 |
#include <sys/param.h> |
#include <sys/param.h> |
| 25 |
#include <sys/sem.h> |
#include <sys/sem.h> |
| 26 |
#include <sys/shm.h> |
#include <sys/shm.h> |
|
#include <sys/ipc.h> |
|
| 27 |
|
|
| 28 |
#ifdef _SEM_SEMUN_UNDEFINED |
#ifdef _SEM_SEMUN_UNDEFINED |
| 29 |
union semun |
union semun |
| 36 |
}; |
}; |
| 37 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 38 |
|
|
| 39 |
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
enum _section_list_constant_t |
| 40 |
#define SECTION_TRY_LOCK_TIMES 10 |
{ |
| 41 |
|
SECTION_TRY_LOCK_WAIT_TIME = 1, // second |
| 42 |
|
SECTION_TRY_LOCK_TIMES = 10, |
| 43 |
|
|
| 44 |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
ARTICLE_BLOCK_PER_SHM = 1000, // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 45 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
ARTICLE_BLOCK_SHM_COUNT_LIMIT = 80, // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 46 |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
ARTICLE_BLOCK_PER_POOL = (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT), |
| 47 |
|
|
| 48 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
CALCULATE_PAGE_THRESHOLD = 100, // Adjust to tune performance of moving topic between sections |
| 49 |
|
|
| 50 |
#define SID_STR_LEN 5 // 32-bit + NULL |
SID_STR_LEN = 5, // 32-bit + NULL |
| 51 |
|
}; |
| 52 |
|
|
| 53 |
struct article_block_t |
struct article_block_t |
| 54 |
{ |
{ |
| 55 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[BBS_article_count_per_block]; |
| 56 |
int article_count; |
int article_count; |
| 57 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 58 |
}; |
}; |
| 75 |
|
|
| 76 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 77 |
|
|
| 78 |
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; |
|
| 79 |
|
|
| 80 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 81 |
{ |
{ |
| 215 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 219 |
{ |
{ |
| 220 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 221 |
} |
} |
| 342 |
} |
} |
| 343 |
|
|
| 344 |
left = 0; |
left = 0; |
| 345 |
right = p_article_block_pool->block_count; |
right = p_article_block_pool->block_count - 1; |
| 346 |
|
|
| 347 |
// 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] ] |
| 348 |
while (left < right - 1) |
while (left < right) |
| 349 |
{ |
{ |
| 350 |
// 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 |
| 351 |
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; |
|
|
} |
|
| 352 |
|
|
| 353 |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
if (aid < p_article_block_pool->p_block[mid]->articles[0].aid) |
| 354 |
{ |
{ |
| 355 |
right = mid; |
right = mid - 1; |
| 356 |
} |
} |
| 357 |
else |
else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid) |
| 358 |
{ |
{ |
| 359 |
left = mid; |
left = mid; |
| 360 |
} |
} |
| 398 |
return NULL; |
return NULL; |
| 399 |
} |
} |
| 400 |
|
|
| 401 |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count) |
| 402 |
{ |
{ |
| 403 |
log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
| 404 |
return NULL; |
return NULL; |
| 405 |
} |
} |
| 406 |
|
|
| 407 |
p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK]; |
p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block]; |
| 408 |
|
|
| 409 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % BBS_article_count_per_block >= p_block->article_count) |
| 410 |
{ |
{ |
| 411 |
log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
| 412 |
return NULL; |
return NULL; |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % BBS_article_count_per_block)); |
| 416 |
} |
} |
| 417 |
|
|
| 418 |
extern int section_list_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 530 |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 531 |
} |
} |
| 532 |
|
|
| 533 |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) |
| 534 |
{ |
{ |
| 535 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 536 |
} |
} |
| 613 |
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; |
| 614 |
|
|
| 615 |
p_section->sid = sid; |
p_section->sid = sid; |
| 616 |
|
p_section->ex_menu_tm = 0; |
| 617 |
|
|
| 618 |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 619 |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 643 |
return p_section; |
return p_section; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
|
int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list) |
| 647 |
|
{ |
| 648 |
|
int64_t index; |
| 649 |
|
|
| 650 |
|
if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL) |
| 651 |
|
{ |
| 652 |
|
log_error("NULL pointer error\n"); |
| 653 |
|
return -1; |
| 654 |
|
} |
| 655 |
|
|
| 656 |
|
index = get_section_index(p_section); |
| 657 |
|
|
| 658 |
|
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 659 |
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 660 |
|
|
| 661 |
|
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 662 |
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 663 |
|
|
| 664 |
|
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 665 |
|
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 666 |
|
|
| 667 |
|
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0) |
| 668 |
|
{ |
| 669 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, index); |
| 670 |
|
return -2; |
| 671 |
|
} |
| 672 |
|
|
| 673 |
|
return 0; |
| 674 |
|
} |
| 675 |
|
|
| 676 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 677 |
{ |
{ |
| 678 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 684 |
|
|
| 685 |
p_section->page_count = 0; |
p_section->page_count = 0; |
| 686 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 687 |
|
|
| 688 |
|
p_section->ontop_article_count = 0; |
| 689 |
} |
} |
| 690 |
|
|
| 691 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 751 |
|
|
| 752 |
if (p_section == NULL || p_article_src == NULL) |
if (p_section == NULL || p_article_src == NULL) |
| 753 |
{ |
{ |
| 754 |
log_error("section_list_append_article() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 755 |
return -1; |
return -1; |
| 756 |
} |
} |
| 757 |
|
|
| 774 |
} |
} |
| 775 |
|
|
| 776 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 777 |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK) |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block) |
| 778 |
{ |
{ |
| 779 |
if ((p_block = pop_free_article_block()) == NULL) |
if ((p_block = pop_free_article_block()) == NULL) |
| 780 |
{ |
{ |
| 784 |
|
|
| 785 |
if (p_article_block_pool->block_count > 0) |
if (p_article_block_pool->block_count > 0) |
| 786 |
{ |
{ |
| 787 |
last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid; |
last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid; |
| 788 |
} |
} |
| 789 |
|
|
| 790 |
p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block; |
p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block; |
| 863 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 864 |
|
|
| 865 |
// Update page |
// Update page |
| 866 |
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) || |
| 867 |
p_section->article_count == 1) |
p_section->page_count == 0) |
| 868 |
{ |
{ |
| 869 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 870 |
p_section->page_count++; |
p_section->page_count++; |
| 876 |
p_section->last_page_visible_article_count++; |
p_section->last_page_visible_article_count++; |
| 877 |
} |
} |
| 878 |
|
|
| 879 |
|
if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0) |
| 880 |
|
{ |
| 881 |
|
log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n", |
| 882 |
|
p_section->sid, p_article->aid); |
| 883 |
|
return -5; |
| 884 |
|
} |
| 885 |
|
|
| 886 |
return 0; |
return 0; |
| 887 |
} |
} |
| 888 |
|
|
| 894 |
|
|
| 895 |
if (p_section == NULL) |
if (p_section == NULL) |
| 896 |
{ |
{ |
| 897 |
log_error("section_list_set_article_visible() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 898 |
return -2; |
return -1; |
| 899 |
} |
} |
| 900 |
|
|
| 901 |
p_article = article_block_find_by_aid(aid); |
p_article = article_block_find_by_aid(aid); |
| 906 |
|
|
| 907 |
if (p_section->sid != p_article->sid) |
if (p_section->sid != p_article->sid) |
| 908 |
{ |
{ |
| 909 |
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 910 |
return -2; |
return -2; |
| 911 |
} |
} |
| 912 |
|
|
| 919 |
{ |
{ |
| 920 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 921 |
|
|
| 922 |
|
if (user_article_cnt_inc(p_article->uid, -1) < 0) |
| 923 |
|
{ |
| 924 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid); |
| 925 |
|
} |
| 926 |
|
|
| 927 |
if (p_article->tid == 0) |
if (p_article->tid == 0) |
| 928 |
{ |
{ |
| 929 |
p_section->visible_topic_count--; |
p_section->visible_topic_count--; |
| 942 |
p_reply->visible = 0; |
p_reply->visible = 0; |
| 943 |
p_section->visible_article_count--; |
p_section->visible_article_count--; |
| 944 |
affected_count++; |
affected_count++; |
| 945 |
|
|
| 946 |
|
if (user_article_cnt_inc(p_reply->uid, -1) < 0) |
| 947 |
|
{ |
| 948 |
|
log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid); |
| 949 |
|
} |
| 950 |
} |
} |
| 951 |
} |
} |
| 952 |
} |
} |
| 959 |
{ |
{ |
| 960 |
p_section->visible_topic_count++; |
p_section->visible_topic_count++; |
| 961 |
} |
} |
| 962 |
|
|
| 963 |
|
if (user_article_cnt_inc(p_article->uid, 1) < 0) |
| 964 |
|
{ |
| 965 |
|
log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid); |
| 966 |
|
} |
| 967 |
} |
} |
| 968 |
|
|
| 969 |
p_article->visible = visible; |
p_article->visible = visible; |
| 972 |
return affected_count; |
return affected_count; |
| 973 |
} |
} |
| 974 |
|
|
| 975 |
|
int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article) |
| 976 |
|
{ |
| 977 |
|
int i; |
| 978 |
|
|
| 979 |
|
if (p_section == NULL || p_article == NULL) |
| 980 |
|
{ |
| 981 |
|
log_error("NULL pointer error\n"); |
| 982 |
|
return -1; |
| 983 |
|
} |
| 984 |
|
|
| 985 |
|
if (p_section->sid != p_article->sid) |
| 986 |
|
{ |
| 987 |
|
log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 988 |
|
return -2; |
| 989 |
|
} |
| 990 |
|
|
| 991 |
|
if (p_article->ontop) |
| 992 |
|
{ |
| 993 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 994 |
|
{ |
| 995 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 996 |
|
{ |
| 997 |
|
log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid); |
| 998 |
|
return 0; |
| 999 |
|
} |
| 1000 |
|
else if (p_section->p_ontop_articles[i]->aid > p_article->aid) |
| 1001 |
|
{ |
| 1002 |
|
break; |
| 1003 |
|
} |
| 1004 |
|
} |
| 1005 |
|
|
| 1006 |
|
// Remove the oldest one if the array of ontop articles is full |
| 1007 |
|
if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section) |
| 1008 |
|
{ |
| 1009 |
|
if (i == 0) // p_article is the oldest one |
| 1010 |
|
{ |
| 1011 |
|
return 0; |
| 1012 |
|
} |
| 1013 |
|
memmove((void *)(p_section->p_ontop_articles), |
| 1014 |
|
(void *)(p_section->p_ontop_articles + 1), |
| 1015 |
|
sizeof(ARTICLE *) * (size_t)(i - 1)); |
| 1016 |
|
p_section->ontop_article_count--; |
| 1017 |
|
i--; |
| 1018 |
|
} |
| 1019 |
|
else |
| 1020 |
|
{ |
| 1021 |
|
memmove((void *)(p_section->p_ontop_articles + i + 1), |
| 1022 |
|
(void *)(p_section->p_ontop_articles + i), |
| 1023 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i)); |
| 1024 |
|
} |
| 1025 |
|
|
| 1026 |
|
p_section->p_ontop_articles[i] = p_article; |
| 1027 |
|
p_section->ontop_article_count++; |
| 1028 |
|
|
| 1029 |
|
// TODO: debug |
| 1030 |
|
} |
| 1031 |
|
else // ontop == 0 |
| 1032 |
|
{ |
| 1033 |
|
for (i = 0; i < p_section->ontop_article_count; i++) |
| 1034 |
|
{ |
| 1035 |
|
if (p_section->p_ontop_articles[i]->aid == p_article->aid) |
| 1036 |
|
{ |
| 1037 |
|
break; |
| 1038 |
|
} |
| 1039 |
|
} |
| 1040 |
|
if (i == p_section->ontop_article_count) // not found |
| 1041 |
|
{ |
| 1042 |
|
log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid); |
| 1043 |
|
return 0; |
| 1044 |
|
} |
| 1045 |
|
|
| 1046 |
|
memmove((void *)(p_section->p_ontop_articles + i), |
| 1047 |
|
(void *)(p_section->p_ontop_articles + i + 1), |
| 1048 |
|
sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1)); |
| 1049 |
|
p_section->ontop_article_count--; |
| 1050 |
|
} |
| 1051 |
|
|
| 1052 |
|
return 0; |
| 1053 |
|
} |
| 1054 |
|
|
| 1055 |
|
int section_list_page_count_with_ontop(SECTION_LIST *p_section) |
| 1056 |
|
{ |
| 1057 |
|
int page_count; |
| 1058 |
|
|
| 1059 |
|
if (p_section == NULL) |
| 1060 |
|
{ |
| 1061 |
|
log_error("NULL pointer error\n"); |
| 1062 |
|
return -1; |
| 1063 |
|
} |
| 1064 |
|
|
| 1065 |
|
page_count = p_section->page_count - 1 + |
| 1066 |
|
(p_section->last_page_visible_article_count + p_section->ontop_article_count + BBS_article_limit_per_page - 1) / |
| 1067 |
|
BBS_article_limit_per_page; |
| 1068 |
|
|
| 1069 |
|
if (page_count < 0) |
| 1070 |
|
{ |
| 1071 |
|
page_count = 0; |
| 1072 |
|
} |
| 1073 |
|
|
| 1074 |
|
return page_count; |
| 1075 |
|
} |
| 1076 |
|
|
| 1077 |
|
int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id) |
| 1078 |
|
{ |
| 1079 |
|
if (p_section == NULL) |
| 1080 |
|
{ |
| 1081 |
|
log_error("NULL pointer error\n"); |
| 1082 |
|
return -1; |
| 1083 |
|
} |
| 1084 |
|
|
| 1085 |
|
if (page_id < p_section->page_count - 1) |
| 1086 |
|
{ |
| 1087 |
|
return BBS_article_limit_per_page; |
| 1088 |
|
} |
| 1089 |
|
else // if (page_id >= p_section->page_count - 1) |
| 1090 |
|
{ |
| 1091 |
|
return MIN(MAX(0, |
| 1092 |
|
(p_section->last_page_visible_article_count + p_section->ontop_article_count - |
| 1093 |
|
BBS_article_limit_per_page * (page_id - p_section->page_count + 1))), |
| 1094 |
|
BBS_article_limit_per_page); |
| 1095 |
|
} |
| 1096 |
|
} |
| 1097 |
|
|
| 1098 |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next) |
| 1099 |
{ |
{ |
| 1100 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1108 |
|
|
| 1109 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1110 |
{ |
{ |
| 1111 |
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1112 |
return NULL; |
return NULL; |
| 1113 |
} |
} |
| 1114 |
|
|
| 1120 |
} |
} |
| 1121 |
|
|
| 1122 |
left = 0; |
left = 0; |
| 1123 |
right = p_section->page_count; |
right = p_section->page_count - 1; |
| 1124 |
|
|
| 1125 |
// 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] ] |
| 1126 |
while (left < right - 1) |
while (left < right) |
| 1127 |
{ |
{ |
| 1128 |
// 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 |
| 1129 |
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; |
|
|
} |
|
| 1130 |
|
|
| 1131 |
if (aid < p_section->p_page_first_article[mid]->aid) |
if (aid < p_section->p_page_first_article[mid]->aid) |
| 1132 |
{ |
{ |
| 1133 |
right = mid; |
right = mid - 1; |
| 1134 |
} |
} |
| 1135 |
else |
else // if (aid < p_section->p_page_first_article[mid]->aid) |
| 1136 |
{ |
{ |
| 1137 |
left = mid; |
left = mid; |
| 1138 |
} |
} |
| 1194 |
|
|
| 1195 |
if (p_section == NULL) |
if (p_section == NULL) |
| 1196 |
{ |
{ |
| 1197 |
log_error("section_list_calculate_page() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1198 |
return -1; |
return -1; |
| 1199 |
} |
} |
| 1200 |
|
|
| 1283 |
} while (p_article != p_section->p_article_head); |
} while (p_article != p_section->p_article_head); |
| 1284 |
|
|
| 1285 |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 1286 |
p_section->last_page_visible_article_count = visible_article_count; |
p_section->last_page_visible_article_count = (visible_article_count > 0 |
| 1287 |
|
? visible_article_count |
| 1288 |
|
: (page > 0 ? BBS_article_limit_per_page : 0)); |
| 1289 |
|
|
| 1290 |
return 0; |
return 0; |
| 1291 |
} |
} |
| 1307 |
return -1; |
return -1; |
| 1308 |
} |
} |
| 1309 |
|
|
| 1310 |
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block + |
| 1311 |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
| 1312 |
|
|
| 1313 |
return ret; |
return ret; |
| 1355 |
|
|
| 1356 |
if (p_section_src == NULL || p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1357 |
{ |
{ |
| 1358 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("NULL pointer error\n"); |
| 1359 |
return -1; |
return -1; |
| 1360 |
} |
} |
| 1361 |
|
|
| 1568 |
return index; |
return index; |
| 1569 |
} |
} |
| 1570 |
|
|
| 1571 |
|
int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list) |
| 1572 |
|
{ |
| 1573 |
|
if (p_section == NULL) |
| 1574 |
|
{ |
| 1575 |
|
log_error("NULL pointer error\n"); |
| 1576 |
|
return -1; |
| 1577 |
|
} |
| 1578 |
|
|
| 1579 |
|
if (section_list_rd_lock(p_section) < 0) |
| 1580 |
|
{ |
| 1581 |
|
log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid); |
| 1582 |
|
return -2; |
| 1583 |
|
} |
| 1584 |
|
|
| 1585 |
|
if (sname != NULL) |
| 1586 |
|
{ |
| 1587 |
|
memcpy(sname, p_section->sname, sizeof(p_section->sname)); |
| 1588 |
|
} |
| 1589 |
|
if (stitle != NULL) |
| 1590 |
|
{ |
| 1591 |
|
memcpy(stitle, p_section->stitle, sizeof(p_section->stitle)); |
| 1592 |
|
} |
| 1593 |
|
if (master_list != NULL) |
| 1594 |
|
{ |
| 1595 |
|
memcpy(master_list, p_section->master_list, sizeof(p_section->master_list)); |
| 1596 |
|
} |
| 1597 |
|
|
| 1598 |
|
// release lock of section |
| 1599 |
|
if (section_list_rd_unlock(p_section) < 0) |
| 1600 |
|
{ |
| 1601 |
|
log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid); |
| 1602 |
|
return -2; |
| 1603 |
|
} |
| 1604 |
|
|
| 1605 |
|
return 0; |
| 1606 |
|
} |
| 1607 |
|
|
| 1608 |
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) |
| 1609 |
{ |
{ |
| 1610 |
int index; |
int index; |
| 1764 |
timer++; |
timer++; |
| 1765 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1766 |
{ |
{ |
| 1767 |
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); |
| 1768 |
} |
} |
| 1769 |
} |
} |
| 1770 |
else // failed |
else // failed |
| 1771 |
{ |
{ |
| 1772 |
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); |
| 1773 |
break; |
break; |
| 1774 |
} |
} |
| 1775 |
} |
} |
| 1795 |
timer++; |
timer++; |
| 1796 |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1797 |
{ |
{ |
| 1798 |
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); |
| 1799 |
} |
} |
| 1800 |
} |
} |
| 1801 |
else // failed |
else // failed |
| 1802 |
{ |
{ |
| 1803 |
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); |
| 1804 |
break; |
break; |
| 1805 |
} |
} |
| 1806 |
} |
} |