/[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.4 by sysadm, Tue May 27 03:25:02 2025 UTC Revision 1.63 by sysadm, Tue Nov 4 13:49:51 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                          section_list_loader.c  -  description  /*
3                                                           -------------------   * section_list_loader
4          Copyright            : (C) 2004-2025 by Leaflet   *   - load and query operations of section articles
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #include "article_cache.h"
10   *   This program is free software; you can redistribute it and/or modify  *  #include "article_view_log.h"
11   *   it under the terms of the GNU General Public License as published by  *  #include "bbs.h"
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
   
 #include "section_list_loader.h"  
 #include "log.h"  
12  #include "database.h"  #include "database.h"
13    #include "ip_mask.h"
14    #include "log.h"
15  #include "menu.h"  #include "menu.h"
16  #include <stdio.h>  #include "section_list_loader.h"
17  #include <string.h>  #include "user_list.h"
18    #include "user_priv.h"
19  #include <errno.h>  #include <errno.h>
20  #include <signal.h>  #include <signal.h>
21    #include <stdio.h>
22  #include <stdlib.h>  #include <stdlib.h>
23  #include <strings.h>  #include <string.h>
24  #include <unistd.h>  #include <unistd.h>
25    
26  #define SECTION_LIST_LOAD_INTERVAL 10 // second  int section_list_loader_pid;
27    int last_article_op_log_mid;
 static int section_list_loader_pid;  
28    
29  int load_section_config_from_db(void)  int load_section_config_from_db(int update_gen_ex)
30  {  {
31          MYSQL *db;          MYSQL *db = NULL;
32          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs = NULL, *rs2 = NULL;
33          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
34          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
35          int32_t sid;          int32_t sid;
36          char master_name[BBS_username_max_len + 1];          char master_list[(BBS_username_max_len + 1) * 3 + 1];
37          SECTION_LIST *p_section;          SECTION_LIST *p_section;
38          int ret;          char ex_menu_conf[FILE_PATH_LEN];
39            MENU_SET ex_menu_set_new;
40            int ret = 0;
41    
42          db = db_open();          db = db_open();
43          if (db == NULL)          if (db == NULL)
44          {          {
45                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
46                  return -2;                  ret = -1;
47                    goto cleanup;
48          }          }
49    
50          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
51                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
52                           "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "                           "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable, "
53                             "UNIX_TIMESTAMP(ex_menu_tm) AS ex_menu_tm "
54                           "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "                           "FROM section_config INNER JOIN section_class ON section_config.CID = section_class.CID "
55                           "ORDER BY section_config.SID");                           "ORDER BY section_config.SID");
56    
57          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
58          {          {
59                  log_error("Query section_list error: %s\n", mysql_error(db));                  log_error("Query section_list error: %s\n", mysql_error(db));
60                  return -3;                  ret = -2;
61                    goto cleanup;
62          }          }
63          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
64          {          {
65                  log_error("Get section_list data failed\n");                  log_error("Get section_list data failed\n");
66                  return -3;                  ret = -2;
67                    goto cleanup;
68          }          }
69    
70          ret = 0;          ret = 0;
# Line 75  int load_section_config_from_db(void) Line 77  int load_section_config_from_db(void)
77                                   "SELECT username FROM section_master "                                   "SELECT username FROM section_master "
78                                   "INNER JOIN user_list ON section_master.UID = user_list.UID "                                   "INNER JOIN user_list ON section_master.UID = user_list.UID "
79                                   "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) "
80                                   "ORDER BY major DESC LIMIT 1",                                   "ORDER BY major DESC, begin_dt ASC LIMIT 3",
81                                   sid);                                   sid);
82    
83                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
# Line 90  int load_section_config_from_db(void) Line 92  int load_section_config_from_db(void)
92                          ret = -3;                          ret = -3;
93                          break;                          break;
94                  }                  }
95                  if ((row2 = mysql_fetch_row(rs2)))  
96                  {                  master_list[0] = '\0';
97                          strncpy(master_name, row2[0], sizeof(master_name) - 1);                  while ((row2 = mysql_fetch_row(rs2)))
                         master_name[sizeof(master_name) - 1] = '\0';  
                 }  
                 else  
98                  {                  {
99                          master_name[0] = '\0';                          strncat(master_list, row2[0], sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
100                            strncat(master_list, " ", sizeof(master_list) - 1 - strnlen(master_list, sizeof(master_list)));
101                  }                  }
102                  mysql_free_result(rs2);                  mysql_free_result(rs2);
103                    rs2 = NULL;
104    
105                  p_section = section_list_find_by_sid(sid);                  p_section = section_list_find_by_sid(sid);
106    
107                  if (p_section == NULL)                  if (p_section == NULL)
108                  {                  {
109                          p_section = section_list_create(sid, row[1], row[2], "");                          p_section = section_list_create(sid, row[1], row[2], master_list);
110                          if (p_section == NULL)                          if (p_section == NULL)
111                          {                          {
112                                  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 129  int load_section_config_from_db(void) Line 130  int load_section_config_from_db(void)
130                                  break;                                  break;
131                          }                          }
132    
133                          strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);                          if (section_list_update(p_section, row[1], row[2], master_list) < 0)
134                          p_section->sname[sizeof(p_section->sname) - 1] = '\0';                          {
135                          strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);                                  log_error("section_list_update(sid=%d) error\n", p_section->sid);
136                          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';                                  ret = -4;
137                          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1);                                  break;
138                          p_section->master_name[sizeof(p_section->master_name) - 1] = '\0';                          }
139                  }                  }
140    
141                  p_section->class_id = atoi(row[3]);                  p_section->class_id = atoi(row[3]);
# Line 142  int load_section_config_from_db(void) Line 143  int load_section_config_from_db(void)
143                  p_section->write_user_level = atoi(row[5]);                  p_section->write_user_level = atoi(row[5]);
144                  p_section->enable = (int8_t)atoi(row[6]);                  p_section->enable = (int8_t)atoi(row[6]);
145    
146                    // Update gen_ex menu set
147                    if (update_gen_ex && p_section->enable && atol(row[7] == NULL ? "0" : row[7]) > p_section->ex_menu_tm)
148                    {
149                            snprintf(ex_menu_conf, sizeof(ex_menu_conf), "%s/%d", VAR_GEN_EX_MENU_DIR, p_section->sid);
150    
151                            ret = load_menu(&ex_menu_set_new, ex_menu_conf);
152                            if (ret < 0)
153                            {
154                                    unload_menu(&ex_menu_set_new);
155                                    log_error("load_menu(%s) error: %d\n", ex_menu_conf, ret);
156                            }
157                            else
158                            {
159                                    if (p_section->ex_menu_tm > 0)
160                                    {
161                                            unload_menu(&(p_section->ex_menu_set));
162                                    }
163    
164                                    ex_menu_set_new.allow_exit = 1; // Allow exit menu
165                                    memcpy(&(p_section->ex_menu_set), &ex_menu_set_new, sizeof(ex_menu_set_new));
166    
167                                    p_section->ex_menu_tm = atol(row[7]);
168    #ifdef _DEBUG
169                                    log_common("Loaded gen_ex_menu of section %d [%s]\n", p_section->sid, p_section->sname);
170    #endif
171                            }
172                    }
173    
174                  // release rw lock                  // release rw lock
175                  ret = section_list_rw_unlock(p_section);                  ret = section_list_rw_unlock(p_section);
176                  if (ret < 0)                  if (ret < 0)
# Line 149  int load_section_config_from_db(void) Line 178  int load_section_config_from_db(void)
178                          break;                          break;
179                  }                  }
180          }          }
         mysql_free_result(rs);  
181    
182    cleanup:
183            mysql_free_result(rs2);
184            mysql_free_result(rs);
185          mysql_close(db);          mysql_close(db);
186    
187          return ret;          return ret;
188  }  }
189    
190  int append_articles_from_db(int32_t start_aid, int global_lock)  int append_articles_from_db(int32_t start_aid, int global_lock, int article_count_limit)
191  {  {
192          MYSQL *db;          MYSQL *db = NULL;
193          MYSQL_RES *rs;          MYSQL_RES *rs = NULL;
194          MYSQL_ROW row;          MYSQL_ROW row;
195          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
196          ARTICLE article;          ARTICLE article;
197            ARTICLE *p_topic;
198          SECTION_LIST *p_section = NULL;          SECTION_LIST *p_section = NULL;
199          int32_t last_sid = 0;          int32_t last_sid = 0;
200            char sub_ip[IP_ADDR_LEN];
201            int article_count = 0;
202          int ret = 0;          int ret = 0;
203          int i;          int i;
204    
# Line 172  int append_articles_from_db(int32_t star Line 206  int append_articles_from_db(int32_t star
206          if (db == NULL)          if (db == NULL)
207          {          {
208                  log_error("db_open() error: %s\n", mysql_error(db));                  log_error("db_open() error: %s\n", mysql_error(db));
209                  return -2;                  ret = -1;
210                    goto cleanup;
211          }          }
212    
213          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
214                           "SELECT AID, TID, SID, CID, UID, visible, excerption, "                           "SELECT bbs.AID, TID, SID, bbs.CID, UID, visible, excerption, ontop, `lock`, "
215                           "ontop, `lock`, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt "                           "transship, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt, "
216                           "FROM bbs WHERE AID >= %d ORDER BY AID",                           "sub_ip, bbs_content.content "
217                           start_aid);                           "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
218                             "WHERE bbs.AID >= %d ORDER BY bbs.AID LIMIT %d",
219                             start_aid, article_count_limit);
220    
221          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
222          {          {
223                  log_error("Query article list error: %s\n", mysql_error(db));                  log_error("Query article list error: %s\n", mysql_error(db));
224                  return -3;                  ret = -2;
225                    goto cleanup;
226          }          }
227          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
228          {          {
229                  log_error("Get article list data failed\n");                  log_error("Get article list data failed\n");
230                  return -3;                  ret = -2;
231                    goto cleanup;
232          }          }
233    
234          // acquire global lock          // acquire global lock
235          if (global_lock)          if (global_lock && (ret = section_list_rw_lock(NULL)) < 0)
236          {          {
237                  if ((ret = section_list_rw_lock(NULL)) < 0)                  log_error("section_list_rw_lock(sid = 0) error\n");
238                  {                  goto cleanup;
                         log_error("section_list_rw_lock(sid = 0) error\n");  
                         goto cleanup;  
                 }  
239          }          }
240    
241          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
242          {          {
243                  bzero(&article, sizeof(ARTICLE));                  memset(&article, 0, sizeof(ARTICLE));
244    
245                  // copy data of article                  // copy data of article
246                  i = 0;                  i = 0;
# Line 218  int append_articles_from_db(int32_t star Line 254  int append_articles_from_db(int32_t star
254                  article.excerption = (int8_t)atoi(row[i++]);                  article.excerption = (int8_t)atoi(row[i++]);
255                  article.ontop = (int8_t)atoi(row[i++]);                  article.ontop = (int8_t)atoi(row[i++]);
256                  article.lock = (int8_t)atoi(row[i++]);                  article.lock = (int8_t)atoi(row[i++]);
257                    article.transship = (int8_t)atoi(row[i++]);
258    
259                  strncpy(article.username, row[i++], sizeof(article.username) - 1);                  strncpy(article.username, row[i++], sizeof(article.username) - 1);
260                  article.username[sizeof(article.username) - 1] = '\0';                  article.username[sizeof(article.username) - 1] = '\0';
# Line 228  int append_articles_from_db(int32_t star Line 265  int append_articles_from_db(int32_t star
265    
266                  article.sub_dt = atol(row[i++]);                  article.sub_dt = atol(row[i++]);
267    
268                    strncpy(sub_ip, row[i++], sizeof(sub_ip) - 1);
269                    sub_ip[sizeof(sub_ip) - 1] = '\0';
270                    ip_mask(sub_ip, 1, '*');
271    
272                  // release lock of last section if different from current one                  // release lock of last section if different from current one
273                  if (!global_lock && article.sid != last_sid && last_sid != 0)                  if (!global_lock && article.sid != last_sid && last_sid != 0)
274                  {                  {
275                          if ((ret = section_list_rw_unlock(p_section)) < 0)                          if ((ret = section_list_rw_unlock(p_section)) < 0)
276                          {                          {
277                                  log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                                  log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
278                                  break;                                  break;
279                          }                          }
280                  }                  }
# Line 245  int append_articles_from_db(int32_t star Line 286  int append_articles_from_db(int32_t star
286                          break;                          break;
287                  }                  }
288    
289                    if (article.visible != 0 && article.tid != 0)
290                    {
291                            // Check if topic article is visible
292                            p_topic = article_block_find_by_aid(article.tid);
293                            if (p_topic == NULL || p_topic->visible == 0)
294                            {
295                                    // log_error("Set article (aid = %d) as invisible due to invisible or non-existing topic head\n", article.aid);
296                                    article.tid = 0;
297                                    article.visible = 0;
298                            }
299                    }
300    
301                  // acquire lock of current section if different from last one                  // acquire lock of current section if different from last one
302                  if (!global_lock && article.sid != last_sid)                  if (!global_lock && article.sid != last_sid)
303                  {                  {
304                          if ((ret = section_list_rw_lock(p_section)) < 0)                          if ((ret = section_list_rw_lock(p_section)) < 0)
305                          {                          {
306                                  log_error("section_list_rw_lock(sid = 0) error\n");                                  log_error("section_list_rw_lock(sid=0) error\n");
307                                  break;                                  break;
308                          }                          }
309                  }                  }
# Line 260  int append_articles_from_db(int32_t star Line 313  int append_articles_from_db(int32_t star
313    
314                  if (section_list_append_article(p_section, &article) < 0)                  if (section_list_append_article(p_section, &article) < 0)
315                  {                  {
316                          log_error("section_list_append_article(sid = %d, aid = %d) error\n",                          log_error("section_list_append_article(sid=%d, aid=%d) error\n",
317                                            p_section->sid, article.aid);                                            p_section->sid, article.aid);
318                          ret = -3;                          ret = -3;
319                          break;                          break;
320                  }                  }
321    
322                    article_count++;
323    
324                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, &article, p_section, row[i++], sub_ip, 0) < 0)
325                    {
326                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", article.aid, article.cid);
327                            ret = -4;
328                            break;
329                    }
330          }          }
331    
332          // release lock of last section          // release lock of last section
# Line 272  int append_articles_from_db(int32_t star Line 334  int append_articles_from_db(int32_t star
334          {          {
335                  if ((ret = section_list_rw_unlock(p_section)) < 0)                  if ((ret = section_list_rw_unlock(p_section)) < 0)
336                  {                  {
337                          log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);                          log_error("section_list_rw_unlock(sid=%d) error\n", p_section->sid);
338                  }                  }
339          }          }
340    
341          // release global lock          // release global lock
342          if (global_lock)          if (global_lock && (ret = section_list_rw_unlock(NULL)) < 0)
343            {
344                    log_error("section_list_rw_unlock(sid=0) error\n");
345            }
346    
347    cleanup:
348            mysql_free_result(rs);
349            mysql_close(db);
350    
351            article_cache_cleanup();
352    
353            return (ret < 0 ? ret : article_count);
354    }
355    
356    int set_last_article_op_log_from_db(void)
357    {
358            MYSQL *db = NULL;
359            MYSQL_RES *rs = NULL;
360            MYSQL_ROW row;
361            char sql[SQL_BUFFER_LEN];
362            int ret = 0;
363    
364            db = db_open();
365            if (db == NULL)
366            {
367                    log_error("db_open() error: %s\n", mysql_error(db));
368                    ret = -1;
369                    goto cleanup;
370            }
371    
372            snprintf(sql, sizeof(sql),
373                             "SELECT MID FROM bbs_article_op ORDER BY MID DESC LIMIT 1");
374    
375            if (mysql_query(db, sql) != 0)
376            {
377                    log_error("Query article op error: %s\n", mysql_error(db));
378                    ret = -2;
379                    goto cleanup;
380            }
381            if ((rs = mysql_store_result(db)) == NULL)
382            {
383                    log_error("Get article op data failed\n");
384                    ret = -2;
385                    goto cleanup;
386            }
387    
388            if ((row = mysql_fetch_row(rs)))
389            {
390                    last_article_op_log_mid = atoi(row[0]);
391            }
392    
393    cleanup:
394            mysql_free_result(rs);
395            mysql_close(db);
396    
397            return (ret < 0 ? ret : last_article_op_log_mid);
398    }
399    
400    int apply_article_op_log_from_db(int op_count_limit)
401    {
402            MYSQL *db = NULL;
403            MYSQL_RES *rs = NULL, *rs2 = NULL;
404            MYSQL_ROW row, row2;
405            char sql[SQL_BUFFER_LEN];
406            ARTICLE *p_article;
407            SECTION_LIST *p_section = NULL;
408            SECTION_LIST *p_section_dest;
409            char sub_ip[IP_ADDR_LEN];
410            int32_t last_sid = 0;
411            int32_t sid_dest;
412            int op_count = 0;
413            int ret = 0;
414    
415            db = db_open();
416            if (db == NULL)
417            {
418                    log_error("db_open() error: %s\n", mysql_error(db));
419                    ret = -1;
420                    goto cleanup;
421            }
422    
423            snprintf(sql, sizeof(sql),
424                             "SELECT MID, AID, type FROM bbs_article_op "
425                             "WHERE MID > %d AND type NOT IN ('A') "
426                             "ORDER BY MID LIMIT %d",
427                             last_article_op_log_mid, op_count_limit);
428    
429            if (mysql_query(db, sql) != 0)
430            {
431                    log_error("Query article log error: %s\n", mysql_error(db));
432                    ret = -2;
433                    goto cleanup;
434            }
435            if ((rs = mysql_store_result(db)) == NULL)
436            {
437                    log_error("Get article log data failed\n");
438                    ret = -2;
439                    goto cleanup;
440            }
441    
442            while ((row = mysql_fetch_row(rs)))
443          {          {
444                  if ((ret = section_list_rw_unlock(NULL)) < 0)                  p_article = article_block_find_by_aid(atoi(row[1]));
445                    if (p_article == NULL) // related article has not been appended yet
446                    {
447                            ret = -2;
448                            break;
449                    }
450    
451                    // release lock of last section if different from current one
452                    if (p_article->sid != last_sid && last_sid != 0)
453                    {
454                            if ((ret = section_list_rw_unlock(p_section)) < 0)
455                            {
456                                    log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
457                                    break;
458                            }
459                    }
460    
461                    if ((p_section = section_list_find_by_sid(p_article->sid)) == NULL)
462                  {                  {
463                          log_error("section_list_rw_unlock(sid = 0) error\n");                          log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", p_article->sid);
464                            ret = ERR_UNKNOWN_SECTION; // Unknown section found
465                            break;
466                    }
467    
468                    // acquire lock of current section if different from last one
469                    if (p_article->sid != last_sid)
470                    {
471                            if ((ret = section_list_rw_lock(p_section)) < 0)
472                            {
473                                    log_error("section_list_rw_lock(sid = 0) error\n");
474                                    break;
475                            }
476                    }
477    
478                    last_sid = p_article->sid;
479    
480                    switch (row[2][0])
481                    {
482                    case 'A': // Add article
483                            log_error("Operation type=A should not be found\n");
484                            break;
485                    case 'D': // Delete article
486                    case 'X': // Delete article by Admin
487                            if (section_list_set_article_visible(p_section, atoi(row[1]), 0) < 0)
488                            {
489                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=0) error\n", p_section->sid, atoi(row[1]));
490                            }
491                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
492                            {
493                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
494                            }
495                            break;
496                    case 'S': // Restore article
497                            if (section_list_set_article_visible(p_section, atoi(row[1]), 1) < 0)
498                            {
499                                    log_error("section_list_set_article_visible(sid=%d, aid=%d, visible=1) error\n", p_section->sid, atoi(row[1]));
500                            }
501                            if (section_list_calculate_page(p_section, atoi(row[1])) < 0)
502                            {
503                                    log_error("section_list_calculate_page(aid=%d) error\n", atoi(row[1]));
504                            }
505                            break;
506                    case 'L': // Lock article
507                            p_article->lock = 1;
508                            break;
509                    case 'U': // Unlock article
510                            p_article->lock = 0;
511                            break;
512                    case 'M': // Modify article
513                            snprintf(sql, sizeof(sql),
514                                             "SELECT bbs.CID, sub_ip, bbs_content.content "
515                                             "FROM bbs INNER JOIN bbs_content ON bbs.CID = bbs_content.CID "
516                                             "WHERE bbs.AID = %d",
517                                             p_article->aid);
518    
519                            if (mysql_query(db, sql) != 0)
520                            {
521                                    log_error("Query article error: %s\n", mysql_error(db));
522                                    ret = -3;
523                                    break;
524                            }
525                            if ((rs2 = mysql_use_result(db)) == NULL)
526                            {
527                                    log_error("Get article data failed\n");
528                                    ret = -3;
529                                    break;
530                            }
531                            if ((row2 = mysql_fetch_row(rs2)))
532                            {
533                                    p_article->cid = atoi(row2[0]);
534    
535                                    strncpy(sub_ip, row2[1], sizeof(sub_ip) - 1);
536                                    sub_ip[sizeof(sub_ip) - 1] = '\0';
537                                    ip_mask(sub_ip, 1, '*');
538    
539                                    if (article_cache_generate(VAR_ARTICLE_CACHE_DIR, p_article, p_section, row2[2], sub_ip, 0) < 0)
540                                    {
541                                            log_error("article_cache_generate(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
542                                            ret = -4;
543                                    }
544                            }
545                            else
546                            {
547                                    p_article->cid = 0;
548                                    ret = -4;
549                            }
550                            mysql_free_result(rs2);
551                            rs2 = NULL;
552    
553                            p_article->excerption = 0; // Set excerption = 0 implicitly in case of rare condition
554                            break;
555                    case 'T': // Move article
556                            snprintf(sql, sizeof(sql),
557                                             "SELECT SID FROM bbs WHERE AID = %d",
558                                             p_article->aid);
559    
560                            if (mysql_query(db, sql) != 0)
561                            {
562                                    log_error("Query article error: %s\n", mysql_error(db));
563                                    ret = -3;
564                                    break;
565                            }
566                            if ((rs2 = mysql_store_result(db)) == NULL)
567                            {
568                                    log_error("Get article data failed\n");
569                                    ret = -3;
570                                    break;
571                            }
572                            if ((row2 = mysql_fetch_row(rs2)))
573                            {
574                                    sid_dest = atoi(row2[0]);
575                            }
576                            else
577                            {
578                                    sid_dest = 0;
579                                    ret = -4;
580                            }
581                            mysql_free_result(rs2);
582                            rs2 = NULL;
583    
584                            if (sid_dest > 0 && sid_dest != p_article->sid)
585                            {
586                                    p_section_dest = section_list_find_by_sid(sid_dest);
587                                    if (p_section_dest == NULL)
588                                    {
589                                            ret = ERR_UNKNOWN_SECTION;
590                                            break;
591                                    }
592                                    // acquire lock of dest section
593                                    if ((ret = section_list_rw_lock(p_section_dest)) < 0)
594                                    {
595                                            log_error("section_list_rw_lock(sid = %d) error\n", p_section_dest);
596                                            break;
597                                    }
598                                    // Move topic
599                                    if ((ret = section_list_move_topic(p_section, p_section_dest, p_article->aid)) < 0)
600                                    {
601                                            log_error("section_list_move_topic(src_sid=%d, dest_sid=%d, aid=%d) error (%d), retry in the next loop\n",
602                                                              p_section->sid, p_section_dest->sid, p_article->aid, ret);
603                                    }
604                                    // release lock of dest section
605                                    if (section_list_rw_unlock(p_section_dest) < 0)
606                                    {
607                                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section_dest);
608                                            ret = -1;
609                                    }
610                            }
611                            break;
612                    case 'E': // Set article as excerption
613                            p_article->excerption = 1;
614                            break;
615                    case 'O': // Unset article as excerption
616                            p_article->excerption = 0;
617                            break;
618                    case 'F': // Set article on top
619                    case 'V': // Unset article on top
620                            p_article->ontop = (row[2][0] == 'F' ? 1 : 0);
621                            if (section_list_update_article_ontop(p_section, p_article) < 0)
622                            {
623                                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
624                                                      p_section->sid, p_article->aid);
625                            }
626                            break;
627                    case 'Z': // Set article as trnasship
628                            p_article->transship = 1;
629                            break;
630                    default:
631                            // log_error("Operation type=%s unknown, mid=%s\n", row[2], row[0]);
632                            break;
633                    }
634    
635                    if (ret < 0)
636                    {
637                            break;
638                    }
639    
640                    // Update MID with last successfully proceeded article_op_log
641                    last_article_op_log_mid = atoi(row[0]);
642    
643                    op_count++;
644            }
645    
646            // release lock of last section
647            if (last_sid != 0)
648            {
649                    if ((ret = section_list_rw_unlock(p_section)) < 0)
650                    {
651                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
652                  }                  }
653          }          }
654    
655  cleanup:  cleanup:
656            mysql_free_result(rs2);
657          mysql_free_result(rs);          mysql_free_result(rs);
   
658          mysql_close(db);          mysql_close(db);
659    
660          return ret;          return (ret < 0 ? ret : op_count);
661    }
662    
663    static void section_list_ex_menu_set_cleanup(void)
664    {
665            int i;
666    
667            for (i = 0; i < p_section_list_pool->section_count; i++)
668            {
669                    if (p_section_list_pool->sections[i].ex_menu_tm > 0)
670                    {
671                            unload_menu(&(p_section_list_pool->sections[i].ex_menu_set));
672                    }
673            }
674  }  }
675    
676  int section_list_loader_launch(void)  int section_list_loader_launch(void)
677  {  {
678            struct sigaction act = {0};
679          int pid;          int pid;
680          int ret;          int ret;
681          int32_t last_aid;          int32_t last_aid;
682          int article_count;          int article_count;
683          int load_count;          int load_count;
684          int i;          int last_mid;
685            time_t tm_section_list_reload = 0;
686            time_t tm_user_list_reload = time(NULL);
687            time_t tm_user_online_list_reload = time(NULL);
688    
689          if (section_list_loader_pid != 0)          if (section_list_loader_pid != 0)
690          {          {
# Line 314  int section_list_loader_launch(void) Line 698  int section_list_loader_launch(void)
698          {          {
699                  SYS_child_process_count++;                  SYS_child_process_count++;
700                  section_list_loader_pid = pid;                  section_list_loader_pid = pid;
701                  log_std("Section list loader process (%d) start\n", pid);                  log_common("Section list loader process (pid = %d) start\n", pid);
702                  return 0;                  return 0;
703          }          }
704          else if (pid < 0) // Error          else if (pid < 0) // Error
# Line 327  int section_list_loader_launch(void) Line 711  int section_list_loader_launch(void)
711          SYS_child_process_count = 0;          SYS_child_process_count = 0;
712    
713          // Detach menu in shared memory          // Detach menu in shared memory
714          detach_menu_shm(p_bbs_menu);          detach_menu_shm(&bbs_menu);
715          free(p_bbs_menu);          detach_menu_shm(&top10_menu);
716          p_bbs_menu = NULL;  
717            // Set signal handler
718            act.sa_handler = SIG_IGN;
719            if (sigaction(SIGHUP, &act, NULL) == -1)
720            {
721                    log_error("set signal action of SIGHUP error: %d\n", errno);
722            }
723            act.sa_handler = SIG_DFL;
724            if (sigaction(SIGCHLD, &act, NULL) == -1)
725            {
726                    log_error("set signal action of SIGCHLD error: %d\n", errno);
727            }
728    
729          // Do section data loader periodically          // Do section data loader periodically
730          while (!SYS_server_exit)          while (!SYS_server_exit)
731          {          {
732                  if (SYS_section_list_reload)                  tm_section_list_reload = time(NULL);
733    
734                    if (SYS_conf_reload)
735                  {                  {
736                          SYS_section_list_reload = 0;                          SYS_conf_reload = 0;
737    
738                          // Load section config                          // Load section config
739                          if (load_section_config_from_db() < 0)                          if (load_section_config_from_db(0) < 0)
740                          {                          {
741                                  log_error("load_section_config_from_db() error\n");                                  log_error("load_section_config_from_db(0) error\n");
742                          }                          }
743                          else                          else
744                          {                          {
745                                  log_error("Reload section config successfully\n");                                  log_common("Reload section config successfully\n");
746                          }                          }
747                  }                  }
748    
749                  // Load section articles                  // Load section articles
                 last_aid = article_block_last_aid();  
750                  article_count = article_block_article_count();                  article_count = article_block_article_count();
751    
752                  if ((ret = append_articles_from_db(last_aid + 1, 0)) < 0)                  do
753                  {                  {
754                          log_error("append_articles_from_db(%d, 0) error\n", last_aid + 1);                          last_aid = article_block_last_aid();
755    
756                          if (ret == ERR_UNKNOWN_SECTION)                          if ((ret = append_articles_from_db(last_aid + 1, 0, LOAD_ARTICLE_COUNT_LIMIT)) < 0)
757                          {                          {
758                                  SYS_section_list_reload = 1; // Force reload section_list                                  log_error("append_articles_from_db(%d, 0, %d) error\n", last_aid + 1, LOAD_ARTICLE_COUNT_LIMIT);
759    
760                                    if (ret == ERR_UNKNOWN_SECTION)
761                                    {
762                                            SYS_conf_reload = 1; // Force reload section_list
763                                    }
764                          }                          }
765                    } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
766    
767                    load_count = article_block_article_count() - article_count;
768                    if (load_count > 0)
769                    {
770                            log_common("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());
771                  }                  }
772                  else  
773                    if (SYS_conf_reload)
774                  {                  {
775                          load_count = article_block_article_count() - article_count;                          continue;
776                    }
777    
778                    // Load article_op log
779                    last_mid = last_article_op_log_mid;
780    
781                          if (load_count > 0)                  do
782                    {
783                            if ((ret = apply_article_op_log_from_db(LOAD_ARTICLE_COUNT_LIMIT)) < 0)
784                          {                          {
785                                  log_std("Incrementally load %d articles, last_aid = %d\n", load_count, article_block_last_aid());                                  log_error("apply_article_op_log_from_db() error\n");
786    
787                                    if (ret == ERR_UNKNOWN_SECTION)
788                                    {
789                                            SYS_conf_reload = 1; // Force reload section_list
790                                    }
791                          }                          }
792                    } while (ret == LOAD_ARTICLE_COUNT_LIMIT);
793    
794                          for (i = 0; i < SECTION_LIST_LOAD_INTERVAL && !SYS_server_exit && !SYS_section_list_reload; i++)                  if (last_article_op_log_mid > last_mid)
795                    {
796                            log_common("Proceeded %d article logs, last_mid = %d\n", last_article_op_log_mid - last_mid, last_article_op_log_mid);
797                    }
798    
799                    // Reload user list
800                    if (time(NULL) - tm_user_list_reload >= BBS_user_list_load_interval)
801                    {
802                            if (user_list_pool_reload(0) < 0)
803                          {                          {
804                                  sleep(1);                                  log_error("user_list_pool_reload(all_user) error\n");
805                          }                          }
806    
807                            if (user_stat_update() < 0)
808                            {
809                                    log_error("user_stat_update() error\n");
810                            }
811    
812                            tm_user_list_reload = time(NULL);
813                    }
814    
815                    // Reload user online list
816                    if (time(NULL) - tm_user_online_list_reload >= BBS_user_online_list_load_interval)
817                    {
818                            if (user_list_pool_reload(1) < 0)
819                            {
820                                    log_error("user_list_pool_reload(online_user) error\n");
821                            }
822    
823                            tm_user_online_list_reload = time(NULL);
824                    }
825    
826                    // Wait for BBS_section_list_load_interval seconds
827                    while (!SYS_server_exit && time(NULL) - tm_section_list_reload < BBS_section_list_load_interval)
828                    {
829                            sleep(1);
830                  }                  }
831          }          }
832    
833          // Child process exit          // Child process exit
834    
835            article_cache_cleanup();
836    
837            // gen_ex_menu cleanup
838            section_list_ex_menu_set_cleanup();
839    
840          // Detach data pools shm          // Detach data pools shm
841          detach_section_list_shm();          detach_section_list_shm();
842          detach_article_block_shm();          detach_article_block_shm();
843          detach_trie_dict_shm();          detach_trie_dict_shm();
844            detach_user_list_pool_shm();
845    
846          log_std("Section list loader process exit normally\n");          log_common("Section list loader process exit normally\n");
847          log_end();          log_end();
848    
849          section_list_loader_pid = 0;          section_list_loader_pid = 0;
# Line 395  int section_list_loader_launch(void) Line 853  int section_list_loader_launch(void)
853          return 0;          return 0;
854  }  }
855    
856  int section_list_loader_reload(void)  int query_section_articles(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[],
857                                                       int *p_article_count, int *p_page_count, int *p_ontop_start_offset)
858  {  {
859          if (section_list_loader_pid == 0)          ARTICLE *p_article;
860            ARTICLE *p_next_page_first_article;
861            int ret = 0;
862            int i;
863    
864            if (p_section == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL || p_ontop_start_offset == NULL)
865            {
866                    log_error("NULL pointer error\n");
867                    return -1;
868            }
869    
870            // acquire lock of section
871            if ((ret = section_list_rd_lock(p_section)) < 0)
872          {          {
873                  log_error("section_list_loader not running\n");                  log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
874                  return -2;                  return -2;
875          }          }
876    
877          if (kill(section_list_loader_pid, SIGHUP) < 0)          *p_page_count = section_list_page_count_with_ontop(p_section);
878            *p_article_count = 0;
879            *p_ontop_start_offset = BBS_article_limit_per_page;
880    
881            if (p_section->visible_article_count == 0)
882            {
883                    // empty section
884            }
885            else if (page_id < 0 || page_id >= *p_page_count)
886            {
887    #ifdef _DEBUG
888                    log_error("Invalid page_id=%d, not in range [0, %d)\n", page_id, *p_page_count);
889    #endif
890                    ret = -3;
891            }
892            else
893            {
894                    ret = page_id;
895    
896                    if (page_id <= p_section->page_count - 1)
897                    {
898                            p_article = p_section->p_page_first_article[page_id];
899                            p_next_page_first_article =
900                                    (page_id == p_section->page_count - 1 ? p_section->p_article_head : p_section->p_page_first_article[page_id + 1]);
901    
902                            do
903                            {
904                                    if (p_article->visible)
905                                    {
906                                            p_articles[*p_article_count] = p_article;
907                                            (*p_article_count)++;
908                                    }
909                                    p_article = p_article->p_next;
910                            } while (p_article != p_next_page_first_article && (*p_article_count) <= BBS_article_limit_per_page);
911    
912                            if (*p_article_count != (page_id < p_section->page_count - 1 ? BBS_article_limit_per_page : p_section->last_page_visible_article_count))
913                            {
914                                    log_error("Inconsistent visible article count %d detected in section %d page %d\n", *p_article_count, p_section->sid, page_id);
915                            }
916                    }
917    
918                    *p_ontop_start_offset = *p_article_count;
919    
920                    // Append ontop articles
921                    if (page_id >= p_section->page_count - 1)
922                    {
923                            i = (page_id == p_section->page_count - 1
924                                             ? 0
925                                             : (page_id - p_section->page_count + 1) * BBS_article_limit_per_page - p_section->last_page_visible_article_count);
926                            while (*p_article_count < BBS_article_limit_per_page && i < p_section->ontop_article_count)
927                            {
928                                    p_articles[*p_article_count] = p_section->p_ontop_articles[i++];
929                                    (*p_article_count)++;
930                            }
931                    }
932            }
933    
934            // release lock of section
935            if (section_list_rd_unlock(p_section) < 0)
936            {
937                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
938                    ret = -2;
939            }
940    
941            return ret;
942    }
943    
944    int locate_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, int direction, int step,
945                                                              int *p_page_id, int *p_visible_offset, int *p_article_count)
946    {
947            const ARTICLE *p_article = NULL;
948            const ARTICLE *p_article_last = NULL;
949            ARTICLE *p_tmp;
950            int32_t aid = 0;
951            int page_id = 0;
952            int offset = 0;
953            int ret = 0;
954            int i;
955    
956            if (p_section == NULL || p_article_cur == NULL || p_page_id == NULL || p_visible_offset == NULL || p_article_count == NULL)
957          {          {
958                  log_error("Send SIGTERM signal failed (%d)\n", errno);                  log_error("NULL pointer error\n");
959                  return -1;                  return -1;
960          }          }
961    
962          return 0;          // acquire lock of section
963            if ((ret = section_list_rd_lock(p_section)) < 0)
964            {
965                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
966                    return -2;
967            }
968    
969            if (direction == 0)
970            {
971                    aid = p_article_cur->aid;
972            }
973            else if (direction == 1)
974            {
975                    for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_next;
976                             step > 0 && p_article->aid > p_article_cur->aid;
977                             p_article = p_article->p_topic_next)
978                    {
979                            if (p_article->visible)
980                            {
981                                    step--;
982                                    p_article_last = p_article;
983                            }
984                    }
985                    p_article = p_article_last;
986    
987                    aid = (p_article->aid > p_article_cur->aid && p_article->visible ? p_article->aid : 0);
988            }
989            else if (direction == -1)
990            {
991                    for (p_article_last = p_article_cur, p_article = p_article_cur->p_topic_prior;
992                             step > 0 && p_article->aid < p_article_cur->aid;
993                             p_article = p_article->p_topic_prior)
994                    {
995                            if (p_article->visible)
996                            {
997                                    step--;
998                                    p_article_last = p_article;
999                            }
1000                    }
1001                    p_article = p_article_last;
1002    
1003                    aid = (p_article->aid < p_article_cur->aid && p_article->visible ? p_article->aid : 0);
1004            }
1005    
1006            p_article = NULL;
1007    
1008            if (aid > 0)
1009            {
1010                    p_article = section_list_find_article_with_offset(p_section, aid, &page_id, &offset, &p_tmp);
1011                    if (p_article != NULL)
1012                    {
1013                            *p_article_count = (page_id >= p_section->page_count - 1 ? p_section->last_page_visible_article_count : BBS_article_limit_per_page);
1014    
1015                            p_article = p_section->p_page_first_article[page_id];
1016                            for (i = 0; i < *p_article_count && p_article != NULL;)
1017                            {
1018                                    if (p_article->visible)
1019                                    {
1020                                            if (p_article->aid == aid)
1021                                            {
1022                                                    *p_page_id = page_id;
1023                                                    *p_visible_offset = i;
1024                                                    break;
1025                                            }
1026                                            i++;
1027                                            if (i >= *p_article_count)
1028                                            {
1029                                                    log_error("Visible article (aid=%d) not found in page %d\n", aid, page_id);
1030                                                    p_article = NULL;
1031                                                    break;
1032                                            }
1033                                    }
1034    
1035                                    p_article = p_article->p_next;
1036                                    if (p_article == p_section->p_page_first_article[page_id])
1037                                    {
1038                                            log_error("Dead loop detected at page=%d, article_count=%d\n", page_id, *p_article_count);
1039                                            break;
1040                                    }
1041                            }
1042    
1043                            // Include ontop articles
1044                            *p_article_count = section_list_page_article_count_with_ontop(p_section, page_id);
1045                    }
1046            }
1047    
1048            // release lock of section
1049            if (section_list_rd_unlock(p_section) < 0)
1050            {
1051                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1052                    ret = -2;
1053            }
1054    
1055            return (ret < 0 ? ret : (p_article == NULL ? 0 : 1));
1056    }
1057    
1058    int last_article_in_section(SECTION_LIST *p_section, const ARTICLE **pp_article)
1059    {
1060            int ret = 0;
1061    
1062            const ARTICLE *p_article;
1063    
1064            if (p_section == NULL || pp_article == NULL)
1065            {
1066                    log_error("NULL pointer error\n");
1067                    return -1;
1068            }
1069    
1070            *pp_article = NULL;
1071    
1072            // acquire lock of section
1073            if ((ret = section_list_rd_lock(p_section)) < 0)
1074            {
1075                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1076                    return -2;
1077            }
1078    
1079            for (p_article = p_section->p_article_tail;
1080                     p_article && p_article != p_section->p_article_head && !p_article->visible;
1081                     p_article = p_article->p_prior)
1082                    ;
1083    
1084            if (p_article && p_article->visible)
1085            {
1086                    *pp_article = p_article;
1087                    ret = 1;
1088            }
1089    
1090            // release lock of section
1091            if (section_list_rd_unlock(p_section) < 0)
1092            {
1093                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1094                    ret = -2;
1095            }
1096    
1097            return ret;
1098    }
1099    
1100    int scan_unread_article_in_section(SECTION_LIST *p_section, const ARTICLE *p_article_cur, const ARTICLE **pp_article_unread)
1101    {
1102            ARTICLE *p_article;
1103            int ret = 0;
1104    
1105            if (p_section == NULL || p_article_cur == NULL || pp_article_unread == NULL)
1106            {
1107                    log_error("NULL pointer error\n");
1108                    return -1;
1109            }
1110    
1111            if (p_article_cur->sid != p_section->sid)
1112            {
1113                    log_error("Inconsistent SID\n");
1114                    return -1;
1115            }
1116    
1117            // acquire lock of section
1118            if ((ret = section_list_rd_lock(p_section)) < 0)
1119            {
1120                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1121                    return -2;
1122            }
1123    
1124            *pp_article_unread = NULL;
1125    
1126            for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next)
1127            {
1128                    if (p_article->visible && p_article->uid != BBS_priv.uid && !article_view_log_is_viewed(p_article->aid, &BBS_article_view_log))
1129                    {
1130                            *pp_article_unread = p_article;
1131                            break;
1132                    }
1133            }
1134    
1135            // release lock of section
1136            if (section_list_rd_unlock(p_section) < 0)
1137            {
1138                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1139                    return -2;
1140            }
1141    
1142            return (p_article != NULL && p_article != p_article_cur ? 1 : 0);
1143    }
1144    
1145    int scan_article_in_section_by_uid(SECTION_LIST *p_section, const ARTICLE *p_article_cur,
1146                                                                       int direction, int32_t uid, const ARTICLE **pp_article)
1147    {
1148            ARTICLE *p_article;
1149            int ret = 0;
1150    
1151            if (p_section == NULL || p_article_cur == NULL || pp_article == NULL)
1152            {
1153                    log_error("NULL pointer error\n");
1154                    return -1;
1155            }
1156    
1157            if (p_article_cur->sid != p_section->sid)
1158            {
1159                    log_error("Inconsistent SID\n");
1160                    return -1;
1161            }
1162    
1163            // acquire lock of section
1164            if ((ret = section_list_rd_lock(p_section)) < 0)
1165            {
1166                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1167                    return -2;
1168            }
1169    
1170            *pp_article = NULL;
1171    
1172            if (direction >= 0)
1173            {
1174                    for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next)
1175                    {
1176                            if (p_article->visible && p_article->uid == uid)
1177                            {
1178                                    *pp_article = p_article;
1179                                    break;
1180                            }
1181                    }
1182            }
1183            else // direction < 0
1184            {
1185                    for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior)
1186                    {
1187                            if (p_article->visible && p_article->uid == uid)
1188                            {
1189                                    *pp_article = p_article;
1190                                    break;
1191                            }
1192                    }
1193            }
1194    
1195            // release lock of section
1196            if (section_list_rd_unlock(p_section) < 0)
1197            {
1198                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1199                    return -2;
1200            }
1201    
1202            return (p_article != NULL && p_article != p_article_cur ? 1 : 0);
1203    }
1204    
1205    int scan_article_in_section_by_username(SECTION_LIST *p_section, const ARTICLE *p_article_cur,
1206                                                                                    int direction, const char *username, const ARTICLE **pp_article)
1207    {
1208            ARTICLE *p_article;
1209            int ret = 0;
1210    
1211            if (p_section == NULL || p_article_cur == NULL || username == NULL || pp_article == NULL)
1212            {
1213                    log_error("NULL pointer error\n");
1214                    return -1;
1215            }
1216    
1217            if (p_article_cur->sid != p_section->sid)
1218            {
1219                    log_error("Inconsistent SID\n");
1220                    return -1;
1221            }
1222    
1223            // acquire lock of section
1224            if ((ret = section_list_rd_lock(p_section)) < 0)
1225            {
1226                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1227                    return -2;
1228            }
1229    
1230            *pp_article = NULL;
1231    
1232            if (direction >= 0)
1233            {
1234                    for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next)
1235                    {
1236                            if (p_article->visible && strcasecmp(p_article->username, username) == 0)
1237                            {
1238                                    *pp_article = p_article;
1239                                    break;
1240                            }
1241                    }
1242            }
1243            else // direction < 0
1244            {
1245                    for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior)
1246                    {
1247                            if (p_article->visible && strcasecmp(p_article->username, username) == 0)
1248                            {
1249                                    *pp_article = p_article;
1250                                    break;
1251                            }
1252                    }
1253            }
1254    
1255            // release lock of section
1256            if (section_list_rd_unlock(p_section) < 0)
1257            {
1258                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1259                    return -2;
1260            }
1261    
1262            return (p_article != NULL && p_article != p_article_cur ? 1 : 0);
1263    }
1264    
1265    int scan_article_in_section_by_title(SECTION_LIST *p_section, const ARTICLE *p_article_cur,
1266                                                                             int direction, const char *title, const ARTICLE **pp_article)
1267    {
1268            ARTICLE *p_article;
1269            int ret = 0;
1270    
1271            if (p_section == NULL || p_article_cur == NULL || title == NULL || pp_article == NULL)
1272            {
1273                    log_error("NULL pointer error\n");
1274                    return -1;
1275            }
1276    
1277            if (p_article_cur->sid != p_section->sid)
1278            {
1279                    log_error("Inconsistent SID\n");
1280                    return -1;
1281            }
1282    
1283            // acquire lock of section
1284            if ((ret = section_list_rd_lock(p_section)) < 0)
1285            {
1286                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1287                    return -2;
1288            }
1289    
1290            *pp_article = NULL;
1291    
1292            if (direction >= 0)
1293            {
1294                    for (p_article = p_article_cur->p_next; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_next)
1295                    {
1296                            if (p_article->visible && strcasestr(p_article->title, title) != NULL)
1297                            {
1298                                    *pp_article = p_article;
1299                                    break;
1300                            }
1301                    }
1302            }
1303            else // direction < 0
1304            {
1305                    for (p_article = p_article_cur->p_prior; p_article != NULL && p_article != p_article_cur; p_article = p_article->p_prior)
1306                    {
1307                            if (p_article->visible && strcasestr(p_article->title, title) != NULL)
1308                            {
1309                                    *pp_article = p_article;
1310                                    break;
1311                            }
1312                    }
1313            }
1314    
1315            // release lock of section
1316            if (section_list_rd_unlock(p_section) < 0)
1317            {
1318                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1319                    return -2;
1320            }
1321    
1322            return (p_article != NULL && p_article != p_article_cur ? 1 : 0);
1323    }
1324    
1325    int get_section_ex_menu_set(SECTION_LIST *p_section, MENU_SET *p_ex_menu_set)
1326    {
1327            int ret = 0;
1328    
1329            if (p_section == NULL || p_ex_menu_set == NULL)
1330            {
1331                    log_error("NULL pointer error\n");
1332                    return -1;
1333            }
1334    
1335            // acquire lock of section
1336            if ((ret = section_list_rd_lock(p_section)) < 0)
1337            {
1338                    log_error("section_list_rd_lock(sid = %d) error\n", p_section->sid);
1339                    return -2;
1340            }
1341    
1342            memcpy(p_ex_menu_set, &(p_section->ex_menu_set), sizeof(p_section->ex_menu_set));
1343    
1344            // release lock of section
1345            if (section_list_rd_unlock(p_section) < 0)
1346            {
1347                    log_error("section_list_rd_unlock(sid = %d) error\n", p_section->sid);
1348                    ret = -2;
1349            }
1350    
1351            return ret;
1352  }  }


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

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