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

Diff of /lbbs/src/user_list.c

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

Revision 1.1 by sysadm, Tue Oct 21 06:24:51 2025 UTC Revision 1.4 by sysadm, Tue Oct 21 12:35:03 2025 UTC
# Line 24  Line 24 
24  #include <time.h>  #include <time.h>
25  #include <sys/ipc.h>  #include <sys/ipc.h>
26  #include <sys/mman.h>  #include <sys/mman.h>
27    #include <sys/param.h>
28  #include <sys/sem.h>  #include <sys/sem.h>
29  #include <sys/shm.h>  #include <sys/shm.h>
30    
# Line 105  int user_list_load(MYSQL *db, USER_LIST Line 106  int user_list_load(MYSQL *db, USER_LIST
106          p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));          p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9]));
107    
108          i++;          i++;
109            if (i >= BBS_max_user_count)
110            {
111                log_error("Too many users, exceed limit %d\n", BBS_max_user_count);
112                break;
113            }
114      }      }
115      mysql_free_result(rs);      mysql_free_result(rs);
116      rs = NULL;      rs = NULL;
# Line 268  int user_list_pool_reload(void) Line 274  int user_list_pool_reload(void)
274  {  {
275      MYSQL *db = NULL;      MYSQL *db = NULL;
276      USER_LIST *p_tmp;      USER_LIST *p_tmp;
     int ret = 0;  
277    
278      if (p_user_list_pool == NULL)      if (p_user_list_pool == NULL)
279      {      {
# Line 283  int user_list_pool_reload(void) Line 288  int user_list_pool_reload(void)
288          return -1;          return -1;
289      }      }
290    
291      if (user_list_rw_lock() < 0)      if (user_list_load(db, p_user_list_pool->p_new) < 0)
292      {      {
293          log_error("user_list_rw_lock() error\n");          log_error("user_list_rw_lock() error\n");
294          return -2;          return -2;
295      }      }
296    
297      if (user_list_load(db, p_user_list_pool->p_new) < 0)      mysql_close(db);
298    
299        if (user_list_rw_lock() < 0)
300      {      {
301          log_error("user_list_rw_lock() error\n");          log_error("user_list_rw_lock() error\n");
302          ret = -3;          return -3;
         goto cleanup;  
303      }      }
304    
305      // Swap p_current and p_new      // Swap p_current and p_new
# Line 301  int user_list_pool_reload(void) Line 307  int user_list_pool_reload(void)
307      p_user_list_pool->p_current = p_user_list_pool->p_new;      p_user_list_pool->p_current = p_user_list_pool->p_new;
308      p_user_list_pool->p_new = p_tmp;      p_user_list_pool->p_new = p_tmp;
309    
 cleanup:  
310      if (user_list_rw_unlock() < 0)      if (user_list_rw_unlock() < 0)
311      {      {
312          log_error("user_list_rw_unlock() error\n");          log_error("user_list_rw_unlock() error\n");
313          ret = -2;          return -3;
314      }      }
315    
316      mysql_close(db);      return 0;
   
     return ret;  
317  }  }
318    
319  int user_list_try_rd_lock(int wait_sec)  int user_list_try_rd_lock(int wait_sec)
# Line 463  int user_list_rw_lock(void) Line 466  int user_list_rw_lock(void)
466      }      }
467    
468      return ret;      return ret;
469    }
470    
471    int query_user_list(int page_id, USER_INFO *p_users, int *p_user_count, int *p_page_count)
472    {
473        int ret = 0;
474    
475        if (p_users == NULL || p_user_count == NULL || p_page_count == NULL)
476        {
477            log_error("NULL pointer error\n");
478            return -1;
479        }
480    
481        // acquire lock of user list
482        if (user_list_rd_lock() < 0)
483        {
484            log_error("user_list_rd_lock() error\n");
485            return -2;
486        }
487    
488        if (p_user_list_pool->p_current->user_count == 0)
489        {
490            // empty list
491            ret = 0;
492            goto cleanup;
493        }
494    
495        *p_page_count = p_user_list_pool->p_current->user_count / BBS_user_limit_per_page +
496                        (p_user_list_pool->p_current->user_count % BBS_user_limit_per_page == 0 ? 0 : 1);
497    
498        if (page_id < 0 || page_id >= *p_page_count)
499        {
500            log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);
501            ret = -3;
502            goto cleanup;
503        }
504    
505        *p_user_count = MIN(BBS_user_limit_per_page,
506                            p_user_list_pool->p_current->user_count -
507                                page_id * BBS_user_limit_per_page);
508    
509        memcpy(p_users,
510               p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,
511               sizeof(USER_INFO) * (size_t)(*p_user_count));
512    
513    cleanup:
514        // release lock of user list
515        if (user_list_rd_unlock() < 0)
516        {
517            log_error("user_list_rd_unlock() error\n");
518            ret = -1;
519        }
520    
521        return ret;
522    }
523    
524    int query_user_info(int32_t uid, USER_INFO *p_user)
525    {
526        int left;
527        int right;
528        int mid;
529        int ret = 0;
530    
531        if (p_user == NULL)
532        {
533            log_error("NULL pointer error\n");
534            return -1;
535        }
536    
537        // acquire lock of user list
538        if (user_list_rd_lock() < 0)
539        {
540            log_error("user_list_rd_lock() error\n");
541            return -2;
542        }
543    
544        left = 0;
545        right = p_user_list_pool->p_current->user_count - 1;
546    
547        while (left < right)
548        {
549            mid = (left + right) / 2;
550            if (uid < p_user_list_pool->p_current->users[mid].uid)
551            {
552                right = mid;
553            }
554            else if (uid > p_user_list_pool->p_current->users[mid].uid)
555            {
556                left = mid + 1;
557            }
558            else // if (uid == p_user_list_pool->p_current->users[mid].uid)
559            {
560                left = mid;
561                break;
562            }
563        }
564    
565        if (uid == p_user_list_pool->p_current->users[left].uid) // Found
566        {
567            *p_user = p_user_list_pool->p_current->users[left];
568            ret = 1;
569        }
570    
571        // release lock of user list
572        if (user_list_rd_unlock() < 0)
573        {
574            log_error("user_list_rd_unlock() error\n");
575            ret = -1;
576        }
577    
578        return ret;
579  }  }


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

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