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


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

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