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


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

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