/[LeafOK_CVS]/lbbs/src/section_list.c
ViewVC logotype

Diff of /lbbs/src/section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.24 by sysadm, Mon May 26 04:47:45 2025 UTC Revision 1.29 by sysadm, Wed May 28 07:30:23 2025 UTC
# Line 108  int article_block_init(const char *filen Line 108  int article_block_init(const char *filen
108                  return -1;                  return -1;
109          }          }
110    
111          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
112          {          {
113                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);                  log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL);
114                  return -2;                  return -2;
# Line 236  void article_block_cleanup(void) Line 236  void article_block_cleanup(void)
236          p_article_block_pool = NULL;          p_article_block_pool = NULL;
237  }  }
238    
239    int set_article_block_shm_readonly(void)
240    {
241            int shmid;
242            void *p_shm;
243            int i;
244    
245            if (p_article_block_pool == NULL)
246            {
247                    log_error("article_block_pool not initialized\n");
248                    return -1;
249            }
250    
251            for (i = 0; i < p_article_block_pool->shm_count; i++)
252            {
253                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
254    
255                    // Remap shared memory in read-only mode
256                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
257                    if (p_shm == (void *)-1)
258                    {
259                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
260                            return -2;
261                    }
262            }
263    
264            return 0;
265    }
266    
267    int detach_article_block_shm(void)
268    {
269            int shmid;
270    
271            if (p_article_block_pool == NULL)
272            {
273                    return -1;
274            }
275    
276            for (int i = 0; i < p_article_block_pool->shm_count; i++)
277            {
278                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
279                    {
280                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
281                            return -2;
282                    }
283            }
284    
285            shmid = p_article_block_pool->shmid;
286    
287            if (shmdt(p_article_block_pool) == -1)
288            {
289                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
290                    return -3;
291            }
292    
293            p_article_block_pool = NULL;
294    
295            return 0;
296    }
297    
298  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
299  {  {
300          ARTICLE_BLOCK *p_block = NULL;          ARTICLE_BLOCK *p_block = NULL;
# Line 340  ARTICLE *article_block_find_by_aid(int32 Line 399  ARTICLE *article_block_find_by_aid(int32
399                  }                  }
400          }          }
401    
402          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
403            {
404                    return NULL;
405            }
406    
407            return (p_block->articles + left); // found
408  }  }
409    
410  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 452  extern int section_list_init(const char Line 516  extern int section_list_init(const char
516          return 0;          return 0;
517  }  }
518    
519    void section_list_cleanup(void)
520    {
521            int shmid;
522    
523            if (p_section_list_pool == NULL)
524            {
525                    return;
526            }
527    
528            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
529            {
530                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
531                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
532            }
533    
534            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
535            {
536                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
537                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
538            }
539    
540            shmid = p_section_list_pool->shmid;
541    
542            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
543            {
544                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
545            }
546    
547            if (shmdt(p_section_list_pool) == -1)
548            {
549                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
550            }
551    
552            if (shmctl(shmid, IPC_RMID, NULL) == -1)
553            {
554                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
555            }
556    
557            p_section_list_pool = NULL;
558    }
559    
560    int set_section_list_shm_readonly(void)
561    {
562            int shmid;
563            void *p_shm;
564    
565            if (p_section_list_pool == NULL)
566            {
567                    log_error("p_section_list_pool not initialized\n");
568                    return -1;
569            }
570    
571            shmid = p_section_list_pool->shmid;
572    
573            // Remap shared memory in read-only mode
574            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
575            if (p_shm == (void *)-1)
576            {
577                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
578                    return -3;
579            }
580    
581            p_section_list_pool = p_shm;
582    
583            return 0;
584    }
585    
586    int detach_section_list_shm(void)
587    {
588            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
589            {
590                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
591                    return -1;
592            }
593    
594            p_section_list_pool = NULL;
595    
596            return 0;
597    }
598    
599  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
600  {  {
601          uint32_t u_sid;          uint32_t u_sid;
# Line 489  SECTION_LIST *section_list_create(int32_ Line 633  SECTION_LIST *section_list_create(int32_
633    
634          p_section->sid = sid;          p_section->sid = sid;
635    
636          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
637          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
638    
639          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
640          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
641    
642          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_list, master_name, sizeof(p_section->master_list) - 1);
643          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
644    
645          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1)
646          {          {
# Line 530  void section_list_reset_articles(SECTION Line 674  void section_list_reset_articles(SECTION
674          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
675  }  }
676    
 void section_list_cleanup(void)  
 {  
         int shmid;  
   
         if (p_section_list_pool == NULL)  
         {  
                 return;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_name != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);  
                 p_section_list_pool->p_trie_dict_section_by_name = NULL;  
         }  
   
         if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)  
         {  
                 trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);  
                 p_section_list_pool->p_trie_dict_section_by_sid = NULL;  
         }  
   
         if (p_section_list_pool != NULL)  
         {  
                 if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)  
                 {  
                         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);  
                 }  
   
                 shmid = p_section_list_pool->shmid;  
   
                 if (shmdt(p_section_list_pool) == -1)  
                 {  
                         log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);  
                 }  
   
                 if (shmctl(shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);  
                 }  
   
                 p_section_list_pool = NULL;  
         }  
 }  
   
677  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
678  {  {
679          int64_t index;          int64_t index;
# Line 1035  int section_list_calculate_page(SECTION_ Line 1135  int section_list_calculate_page(SECTION_
1135          return 0;          return 0;
1136  }  }
1137    
1138    int32_t article_block_last_aid(void)
1139    {
1140            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1141            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1142    
1143            return last_aid;
1144    }
1145    
1146    int article_block_article_count(void)
1147    {
1148            int ret;
1149    
1150            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1151            {
1152                    return -1;
1153            }
1154    
1155            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1156                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1157    
1158            return ret;
1159    }
1160    
1161  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1162  {  {
1163          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1075  int section_list_move_topic(SECTION_LIST Line 1198  int section_list_move_topic(SECTION_LIST
1198          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1199          int move_counter;          int move_counter;
1200    
1201          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1202          {          {
1203                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("section_list_move_topic() NULL pointer error\n");
1204                  return -1;                  return -1;
1205          }          }
1206    
1207            if (p_section_src->sid == p_section_dest->sid)
1208            {
1209                    log_error("section_list_move_topic() src and dest section are the same\n");
1210                    return -1;
1211            }
1212    
1213          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1214          {          {
1215                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1216                  return -2;                  return -2;
1217          }          }
1218    
# Line 1124  int section_list_move_topic(SECTION_LIST Line 1253  int section_list_move_topic(SECTION_LIST
1253          {          {
1254                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1255                  {                  {
1256                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1257                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1258                          return -2;                          p_article = p_article->p_topic_next;
1259                            continue;
1260                  }                  }
1261    
1262                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1152  int section_list_move_topic(SECTION_LIST Line 1282  int section_list_move_topic(SECTION_LIST
1282    
1283                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1284                  {                  {
1285                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1286                          return -4;                          p_article = p_article->p_topic_next;
1287                            continue;
1288                  }                  }
1289    
1290                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1231  int section_list_move_topic(SECTION_LIST Line 1362  int section_list_move_topic(SECTION_LIST
1362    
1363          if (p_section_dest->article_count - dest_article_count_old != move_article_count)          if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1364          {          {
1365                  log_error("section_list_move_topic() error: count of moved articles %d != %d\n",                  log_error("section_list_move_topic() warning: count of moved articles %d != %d\n",
1366                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1367          }          }
1368    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1