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

Diff of /lbbs/src/section_list_loader.c

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

Revision 1.2 by sysadm, Mon May 26 04:47:45 2025 UTC Revision 1.19 by sysadm, Sat Jun 14 09:20:13 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "section_list_loader.h"  #include "section_list_loader.h"
18    #include "article_cache.h"
19    #include "bbs.h"
20  #include "log.h"  #include "log.h"
21  #include "database.h"  #include "database.h"
22    #include "menu.h"
23  #include <stdio.h>  #include <stdio.h>
24  #include <string.h>  #include <string.h>
25  #include <errno.h>  #include <errno.h>
26    #include <signal.h>
27  #include <stdlib.h>  #include <stdlib.h>
28    #include <strings.h>
29    #include <unistd.h>
30    
31    #define _POSIX_C_SOURCE 200809L
32    #include <string.h>
33    
34    int section_list_loader_pid;
35    int last_article_op_log_mid;
36    
37  int load_section_config_from_db(void)  int load_section_config_from_db(void)
38  {  {
# Line 29  int load_section_config_from_db(void) Line 41  int load_section_config_from_db(void)
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    
# Line 67  int load_section_config_from_db(void) Line 79  int load_section_config_from_db(void)
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)
# Line 82  int load_section_config_from_db(void) Line 94  int load_section_config_from_db(void)
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    
# Line 97  int load_section_config_from_db(void) Line 107  int load_section_config_from_db(void)
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]);
# Line 125  int load_section_config_from_db(void) Line 135  int load_section_config_from_db(void)
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]);
# Line 147  int load_section_config_from_db(void) Line 157  int load_section_config_from_db(void)
157    
158          return ret;          return ret;
159  }  }
160    
161    int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
162    {
163            MYSQL *db;
164            MYSQL_RES *rs;
165            MYSQL_ROW row;
166            char sql[SQL_BUFFER_LEN];
167            ARTICLE article;
168            ARTICLE *p_topic;
169            SECTION_LIST *p_section = NULL;
170            int32_t last_sid = 0;
171            char sub_ip[IP_ADDR_LEN];
172            int article_count = 0;
173            int ret = 0;
174            int i;
175    
176            db = db_open();
177            if (db == NULL)
178            {
179                    log_error("db_open() error: %s\n", mysql_error(db));
180                    return -2;
181            }
182    
183            snprintf(sql, sizeof(sql),
184                             "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
185                             "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
186                             "sub_ip, bbs_content.content "
187                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
188                             "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
189                             start_aid, article_count_limit);
190    
191            if (mysql_query(db, sql) != 0)
192            {
193                    log_error("Query article list error: %s\n", mysql_error(db));
194                    return -3;
195            }
196            if ((rs = mysql_use_result(db)) == NULL)
197            {
198                    log_error("Get article list data failed\n");
199                    return -3;
200            }
201    
202            // acquire global lock
203            if (global_lock)
204            {
205                    if ((ret = section_list_rw_lock(NULL)) < 0)
206                    {
207                            log_error("section_list_rw_lock(sid = 0) error\n");
208                            goto cleanup;
209                    }
210            }
211    
212            while ((row = mysql_fetch_row(rs)))
213            {
214                    bzero(&article, sizeof(ARTICLE));
215    
216                    // copy data of article
217                    i = 0;
218    
219                    article.aid = atoi(row[i++]);
220                    article.tid = atoi(row[i++]);
221                    article.sid = atoi(row[i++]);
222                    article.cid = atoi(row[i++]);
223                    article.uid = atoi(row[i++]);
224                    article.visible = (int8_t)atoi(row[i++]);
225                    article.excerption = (int8_t)atoi(row[i++]);
226                    article.ontop = (int8_t)atoi(row[i++]);
227                    article.lock = (int8_t)atoi(row[i++]);
228                    article.transship = (int8_t)atoi(row[i++]);
229    
230                    strncpy(article.username, row[i++], sizeof(article.username) - 1);
231                    article.username[sizeof(article.username) - 1] = '\0';
232                    strncpy(article.nickname, row[i++], sizeof(article.nickname) - 1);
233                    article.nickname[sizeof(article.nickname) - 1] = '\0';
234                    strncpy(article.title, row[i++], sizeof(article.title) - 1);
235                    article.title[sizeof(article.title) - 1] = '\0';
236    
237                    article.sub_dt = atol(row[i++]);
238    
239                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
240                    sub_ip[sizeof(sub_ip) - 1] = '\0';
241                    ip_mask(sub_ip, 1, '*');
242    
243                    // release lock of last section if different from current one
244                    if (!global_lock && article.sid != last_sid && last_sid != 0)
245                    {
246                            if ((ret = section_list_rw_unlock(p_section)) < 0)
247                            {
248                                    log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
249                                    break;
250                            }
251                    }
252    
253                    if ((p_section = section_list_find_by_sid(article.sid)) == NULL)
254                    {
255                            log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid);
256                            ret = ERR_UNKNOWN_SECTION; // Unknown section found
257                            break;
258                    }
259    
260                    if (article.visible != 0 && article.tid != 0)
261                    {
262                            // Check if topic article is visible
263                            p_topic = article_block_find_by_aid(article.tid);
264                            if (p_topic == NULL || p_topic->visible == 0)
265                            {
266                                    // log_error("Set article (aid = %d) as invisible due to invisible or non-existing topic head\n", article.aid);
267                                    article.tid = 0;
268                                    article.visible = 0;
269                            }
270                    }
271    
272                    // acquire lock of current section if different from last one
273                    if (!global_lock && article.sid != last_sid)
274                    {
275                            if ((ret = section_list_rw_lock(p_section)) < 0)
276                            {
277                                    log_error("section_list_rw_lock(sid=0) error\n");
278                                    break;
279                            }
280                    }
281    
282                    // append article to section list
283                    last_sid = article.sid;
284    
285                    if (section_list_append_article(p_section, &article) < 0)
286                    {
287                            log_error("section_list_append_article(sid=%d, aid=%d) error\n",
288                                              p_section->sid, article.aid);
289                            ret = -3;
290                            break;
291                    }
292    
293                    article_count++;
294    
295                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
296                    {
297                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
298                            ret = -4;
299                            break;
300                    }
301            }
302    
303            // release lock of last section
304            if (!global_lock && last_sid != 0)
305            {
306                    if ((ret = section_list_rw_unlock(p_section)) < 0)
307                    {
308                            log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
309                    }
310            }
311    
312            // release global lock
313            if (global_lock)
314            {
315                    if ((ret = section_list_rw_unlock(NULL)) < 0)
316                    {
317                            log_error("section_list_rw_unlock(sid=0) error\n");
318                    }
319            }
320    
321    cleanup:
322            mysql_free_result(rs);
323    
324            mysql_close(db);
325    
326            return (ret < 0 ? ret : article_count);
327    }
328    
329    int set_last_article_op_log_from_db(void)
330    {
331            MYSQL *db;
332            MYSQL_RES *rs;
333            MYSQL_ROW row;
334            char sql[SQL_BUFFER_LEN];
335    
336            db = db_open();
337            if (db == NULL)
338            {
339                    log_error("db_open() error: %s\n", mysql_error(db));
340                    return -1;
341            }
342    
343            snprintf(sql, sizeof(sql),
344                             "SELECT MID FROM bbs_article_op ORDER BY MID DESC LIMIT 1");
345    
346            if (mysql_query(db, sql) != 0)
347            {
348                    log_error("Query article op error: %s\n", mysql_error(db));
349                    return -2;
350            }
351            if ((rs = mysql_store_result(db)) == NULL)
352            {
353                    log_error("Get article op data failed\n");
354                    return -2;
355            }
356    
357            if ((row = mysql_fetch_row(rs)))
358            {
359                    last_article_op_log_mid = atoi(row[0]);
360            }
361    
362            mysql_free_result(rs);
363    
364            mysql_close(db);
365    
366            return last_article_op_log_mid;
367    }
368    
369    int apply_article_op_log_from_db(int op_count_limit)
370    {
371            MYSQL *db;
372            MYSQL_RES *rs, *rs2;
373            MYSQL_ROW row, row2;
374            char sql[SQL_BUFFER_LEN];
375            ARTICLE *p_article;
376            SECTION_LIST *p_section = NULL;
377            SECTION_LIST *p_section_dest;
378            char sub_ip[IP_ADDR_LEN];
379            int32_t last_sid = 0;
380            int32_t sid_dest;
381            int op_count = 0;
382            int ret = 0;
383    
384            db = db_open();
385            if (db == NULL)
386            {
387                    log_error("db_open() error: %s\n", mysql_error(db));
388                    return -3;
389            }
390    
391            snprintf(sql, sizeof(sql),
392                             "SELECT MID, AID, type FROM bbs_article_op "
393                             "WHERE MID > %d AND type NOT IN ('A') "
394                             "ORDER BY MID LIMIT %d",
395                             last_article_op_log_mid, op_count_limit);
396    
397            if (mysql_query(db, sql) != 0)
398            {
399                    log_error("Query article log error: %s\n", mysql_error(db));
400                    return -3;
401            }
402            if ((rs = mysql_store_result(db)) == NULL)
403            {
404                    log_error("Get article log data failed\n");
405                    return -3;
406            }
407    
408            while ((row = mysql_fetch_row(rs)))
409            {
410                    p_article = article_block_find_by_aid(atoi(row[1]));
411                    if (p_article == NULL) // related article has not been appended yet
412                    {
413                            ret = -2;
414                            break;
415                    }
416    
417                    // release lock of last section if different from current one
418                    if (p_article->sid != last_sid && last_sid != 0)
419                    {
420                            if ((ret = section_list_rw_unlock(p_section)) < 0)
421                            {
422                                    log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
423                                    break;
424                            }
425                    }
426    
427                    if ((p_section = section_list_find_by_sid(p_article->sid)) == NULL)
428                    {
429                            log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", p_article->sid);
430                            ret = ERR_UNKNOWN_SECTION; // Unknown section found
431                            break;
432                    }
433    
434                    // acquire lock of current section if different from last one
435                    if (p_article->sid != last_sid)
436                    {
437                            if ((ret = section_list_rw_lock(p_section)) < 0)
438                            {
439                                    log_error("section_list_rw_lock(sid = 0) error\n");
440                                    break;
441                            }
442                    }
443    
444                    last_sid = p_article->sid;
445    
446                    switch (row[2][0])
447                    {
448                    case 'A': // Add article
449                            log_error("Operation type=A should not be found\n");
450                            break;
451                    case 'D': // Delete article
452                    case 'X': // Delete article by Admin
453                            p_article->visible = 0;
454                            if (p_article->tid == 0)
455                            {
456                                    // Set articles in the topic to be invisible
457                                    do
458                                    {
459                                            p_article = p_article->p_topic_next;
460                                            p_article->visible = 0;
461                                    } while (p_article->tid != 0);
462                            }
463                            break;
464                    case 'S': // Restore article
465                            p_article->visible = 1;
466                            break;
467                    case 'L': // Lock article
468                            p_article->lock = 1;
469                            break;
470                    case 'U': // Unlock article
471                            p_article->lock = 0;
472                            break;
473                    case 'M': // Modify article
474                            snprintf(sql, sizeof(sql),
475                                             "SELECT bbs.CID, sub_ip, bbs_content.content "
476                                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
477                                             "WHERE bbs.AID = %d",
478                                             p_article->aid);
479    
480                            if (mysql_query(db, sql) != 0)
481                            {
482                                    log_error("Query article error: %s\n", mysql_error(db));
483                                    ret = -3;
484                                    break;
485                            }
486                            if ((rs2 = mysql_use_result(db)) == NULL)
487                            {
488                                    log_error("Get article data failed\n");
489                                    ret = -3;
490                                    break;
491                            }
492                            if ((row2 = mysql_fetch_row(rs2)))
493                            {
494                                    p_article->cid = atoi(row2[0]);
495    
496                                    strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
497                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
498                                    ip_mask(sub_ip, 1, '*');
499    
500                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
501                                    {
502                                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
503                                            ret = -4;
504                                    }
505                            }
506                            else
507                            {
508                                    p_article->cid = 0;
509                                    ret = -4;
510                            }
511                            mysql_free_result(rs2);
512                            break;
513                    case 'T': // Move article
514                            snprintf(sql, sizeof(sql),
515                                             "SELECT SID FROM bbs WHERE AID = %d",
516                                             p_article->aid);
517    
518                            if (mysql_query(db, sql) != 0)
519                            {
520                                    log_error("Query article error: %s\n", mysql_error(db));
521                                    ret = -3;
522                                    break;
523                            }
524                            if ((rs2 = mysql_store_result(db)) == NULL)
525                            {
526                                    log_error("Get article data failed\n");
527                                    ret = -3;
528                                    break;
529                            }
530                            if ((row2 = mysql_fetch_row(rs2)))
531                            {
532                                    sid_dest = atoi(row2[0]);
533                            }
534                            else
535                            {
536                                    sid_dest = 0;
537                                    ret = -4;
538                            }
539                            mysql_free_result(rs2);
540    
541                            if (sid_dest > 0 && sid_dest != p_article->sid)
542                            {
543                                    p_section_dest = section_list_find_by_sid(sid_dest);
544                                    if (p_section_dest == NULL)
545                                    {
546                                            ret = ERR_UNKNOWN_SECTION;
547                                            break;
548                                    }
549                                    // acquire lock of dest section
550                                    if ((ret = section_list_rw_lock(p_section_dest)) < 0)
551                                    {
552                                            log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest);
553                                            break;
554                                    }
555                                    // Move topic
556                                    if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)
557                                    {
558                                            log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n",
559                                                              p_section->sid, p_section_dest->sid, p_article->aid, ret);
560                                    }
561                                    // release lock of dest section
562                                    if (section_list_rw_unlock(p_section_dest) < 0)
563                                    {
564                                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest);
565                                            ret = -1;
566                                    }
567                            }
568                            break;
569                    case 'E': // Set article as excerption
570                            p_article->excerption = 1;
571                            break;
572                    case 'O': // Unset article as excerption
573                            p_article->excerption = 0;
574                            break;
575                    case 'F': // Set article on top
576                            p_article->ontop = 1;
577                            break;
578                    case 'V': // Unset article on top
579                            p_article->ontop = 0;
580                            break;
581                    case 'Z': // Set article as trnasship
582                            p_article->transship = 1;
583                            break;
584                    default:
585                            // log_error("Operation type=%s unknown, mid=%s\n", row[2], row[0]);
586                            break;
587                    }
588    
589                    if (ret < 0)
590                    {
591                            break;
592                    }
593    
594                    // Update MID with last successfully proceeded article_op_log
595                    last_article_op_log_mid = atoi(row[0]);
596    
597                    op_count++;
598            }
599    
600            // release lock of last section
601            if (last_sid != 0)
602            {
603                    if ((ret = section_list_rw_unlock(p_section)) < 0)
604                    {
605                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
606                    }
607            }
608    
609            mysql_free_result(rs);
610    
611            mysql_close(db);
612    
613            return (ret < 0 ? ret : op_count);
614    }
615    
616    int section_list_loader_launch(void)
617    {
618            int pid;
619            int ret;
620            int32_t last_aid;
621            int article_count;
622            int load_count;
623            int last_mid;
624            int i;
625    
626            if (section_list_loader_pid != 0)
627            {
628                    log_error("section_list_loader already running, pid = %d\n", section_list_loader_pid);
629                    return -2;
630            }
631    
632            pid = fork();
633    
634            if (pid > 0) // Parent process
635            {
636                    SYS_child_process_count++;
637                    section_list_loader_pid = pid;
638                    log_common("Section list loader process (pid = %d) start\n", pid);
639                    return 0;
640            }
641            else if (pid < 0) // Error
642            {
643                    log_error("fork() error (%d)\n", errno);
644                    return -1;
645            }
646    
647            // Child process
648            SYS_child_process_count = 0;
649    
650            // Detach menu in shared memory
651            detach_menu_shm(p_bbs_menu);
652            free(p_bbs_menu);
653            p_bbs_menu = NULL;
654    
655            // Do section data loader periodically
656            while (!SYS_server_exit)
657            {
658                    if (SYS_section_list_reload)
659                    {
660                            SYS_section_list_reload = 0;
661    
662                            // Load section config
663                            if (load_section_config_from_db() < 0)
664                            {
665                                    log_error("load_section_config_from_db() error\n");
666                            }
667                            else
668                            {
669                                    log_error("Reload section config successfully\n");
670                            }
671                    }
672    
673                    // Load section articles
674                    article_count = article_block_article_count();
675    
676                    do
677                    {
678                            last_aid = article_block_last_aid();
679    
680                            if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
681                            {
682                                    log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
683    
684                                    if (ret == ERR_UNKNOWN_SECTION)
685                                    {
686                                            SYS_section_list_reload = 1; // Force reload section_list
687                                    }
688                            }
689                    } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
690    
691                    load_count = article_block_article_count() - article_count;
692                    if (load_count > 0)
693                    {
694                            log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
695                    }
696    
697                    if (SYS_section_list_reload)
698                    {
699                            continue;
700                    }
701    
702                    // Load article_op log
703                    last_mid = last_article_op_log_mid;
704    
705                    do
706                    {
707                            if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
708                            {
709                                    log_error("apply_article_op_log_from_db() error\n");
710    
711                                    if (ret == ERR_UNKNOWN_SECTION)
712                                    {
713                                            SYS_section_list_reload = 1; // Force reload section_list
714                                    }
715                            }
716                    } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
717    
718                    if (last_article_op_log_mid > last_mid)
719                    {
720                            log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
721                    }
722    
723                    if (SYS_section_list_reload)
724                    {
725                            continue;
726                    }
727    
728                    for (i = 0; i < BBS_section_list_load_interval && !SYS_server_exit && !SYS_section_list_reload; i++)
729                    {
730                            sleep(1);
731                    }
732            }
733    
734            // Child process exit
735    
736            // Detach data pools shm
737            detach_section_list_shm();
738            detach_article_block_shm();
739            detach_trie_dict_shm();
740    
741            log_common("Section list loader process exit normally\n");
742            log_end();
743    
744            section_list_loader_pid = 0;
745    
746            _exit(0);
747    
748            return 0;
749    }
750    
751    int section_list_loader_reload(void)
752    {
753            if (section_list_loader_pid == 0)
754            {
755                    log_error("section_list_loader not running\n");
756                    return -2;
757            }
758    
759            if (kill(section_list_loader_pid, SIGHUP) < 0)
760            {
761                    log_error("Send SIGTERM signal failed (%d)\n", errno);
762                    return -1;
763            }
764    
765            return 0;
766    }
767    
768    int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int *p_article_count, int *p_page_count)
769    {
770            ARTICLE *p_article;
771            ARTICLE *p_next_page_first_article;
772            int ret = 0;
773    
774            if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
775            {
776                    log_error("query_section_articles() NULL pointer error\n");
777                    return -1;
778            }
779    
780            // acquire lock of section
781            if ((ret = section_list_rd_lock(p_section)) < 0)
782            {
783                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
784                    return -2;
785            }
786    
787            *p_page_count = p_section->page_count;
788    
789            if (p_section->visible_article_count == 0)
790            {
791                    *p_article_count = 0;
792            }
793            else if (page_id < 0 || page_id >= p_section->page_count)
794            {
795                    log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, p_section->page_count);
796                    ret = -3;
797            }
798            else
799            {
800                    ret = page_id;
801                    p_article = p_section->p_page_first_article[page_id];
802                    p_next_page_first_article =
803                            (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
804                    *p_article_count = 0;
805    
806                    do
807                    {
808                            if (p_article->visible)
809                            {
810                                    p_articles[*p_article_count] = p_article;
811                                    (*p_article_count)++;
812                            }
813                            p_article = p_article->p_next;
814                    } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
815    
816                    if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
817                    {
818                            log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
819                    }
820            }
821    
822            // release lock of section
823            if (section_list_rd_unlock(p_section) < 0)
824            {
825                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
826                    ret = -2;
827            }
828    
829            return ret;
830    }
831    
832    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction,
833                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
834    {
835            const ARTICLE *p_article = NULL;
836            ARTICLE *p_tmp;
837            int32_t aid = 0;
838            int page_id;
839            int offset;
840            int ret = 0;
841            int i;
842    
843            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
844            {
845                    log_error("locate_article_in_section() NULL pointer error\n");
846                    return -1;
847            }
848    
849            // acquire lock of section
850            if ((ret = section_list_rd_lock(p_section)) < 0)
851            {
852                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
853                    return -2;
854            }
855    
856            if (direction == 0)
857            {
858                    aid = p_article_cur->aid;
859            }
860            else if (direction == 1)
861            {
862                    p_article = p_article_cur;
863                    do
864                    {
865                            p_article = p_article->p_topic_next;
866                    } while (p_article != p_article_cur && p_article->visible == 0);
867    
868                    aid = (p_article->aid > p_article_cur->aid ? p_article->aid : 0);
869            }
870            else if (direction == -1)
871            {
872                    p_article = p_article_cur;
873                    do
874                    {
875                            p_article = p_article->p_topic_prior;
876                    } while (p_article != p_article_cur && p_article->visible == 0);
877    
878                    aid = (p_article->aid < p_article_cur->aid ? p_article->aid : 0);
879            }
880    
881            p_article = NULL;
882    
883            if (aid > 0)
884            {
885                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
886                    if (p_article != NULL)
887                    {
888                            *p_article_count = (page_id == p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
889    
890                            p_article = p_section->p_page_first_article[page_id];
891                            for (i = 0; i < *p_article_count;)
892                            {
893                                    if (p_article->visible)
894                                    {
895                                            if (p_article->aid == aid)
896                                            {
897                                                    *p_page_id = page_id;
898                                                    *p_visible_offset = i;
899                                                    break;
900                                            }
901                                            i++;
902                                            if (i >= *p_article_count)
903                                            {
904                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
905                                                    p_article = NULL;
906                                            }
907                                    }
908                                    p_article = p_article->p_next;
909                            }
910                    }
911            }
912    
913            // release lock of section
914            if (section_list_rd_unlock(p_section) < 0)
915            {
916                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
917                    ret = -2;
918            }
919    
920            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
921    }


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

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