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


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

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