/[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.1 by sysadm, Mon May 26 03:42:45 2025 UTC Revision 1.3 by sysadm, Tue May 27 00:54:59 2025 UTC
# Line 21  Line 21 
21  #include <string.h>  #include <string.h>
22  #include <errno.h>  #include <errno.h>
23  #include <stdlib.h>  #include <stdlib.h>
24    #include <strings.h>
25    
26  int load_section_config_from_db(MYSQL *db)  int load_section_config_from_db(void)
27  {  {
28            MYSQL *db;
29          MYSQL_RES *rs, *rs2;          MYSQL_RES *rs, *rs2;
30          MYSQL_ROW row, row2;          MYSQL_ROW row, row2;
31          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
# Line 32  int load_section_config_from_db(MYSQL *d Line 34  int load_section_config_from_db(MYSQL *d
34          SECTION_LIST *p_section;          SECTION_LIST *p_section;
35          int ret;          int ret;
36    
37            db = db_open();
38            if (db == NULL)
39            {
40                    log_error("db_open() error: %s\n", mysql_error(db));
41                    return -2;
42            }
43    
44          snprintf(sql, sizeof(sql),          snprintf(sql, sizeof(sql),
45                           "SELECT section_config.SID, sname, title, section_config.CID, read_user_level, write_user_level, "                           "SELECT section_config.SID, sname, section_config.title, section_config.CID, "
46                           "section_config.enable * section_class.enable AS enable "                           "read_user_level, write_user_level, section_config.enable * section_class.enable AS enable "
47                           "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 "
48                           "ORDER BY section_config.SID");                           "ORDER BY section_config.SID");
49    
50          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
51          {          {
52                  log_error("Query section_list error: %s\n", mysql_error(db));                  log_error("Query section_list error: %s\n", mysql_error(db));
53                  return -1;                  return -3;
54          }          }
55          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
56          {          {
57                  log_error("Get section_list data failed\n");                  log_error("Get section_list data failed\n");
58                  return -1;                  return -3;
59          }          }
60    
61            ret = 0;
62          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
63          {          {
64                  sid = atoi(row[0]);                  sid = atoi(row[0]);
# Line 63  int load_section_config_from_db(MYSQL *d Line 74  int load_section_config_from_db(MYSQL *d
74                  if (mysql_query(db, sql) != 0)                  if (mysql_query(db, sql) != 0)
75                  {                  {
76                          log_error("Query section_master error: %s\n", mysql_error(db));                          log_error("Query section_master error: %s\n", mysql_error(db));
77                          return -2;                          ret = -3;
78                            break;
79                  }                  }
80                  if ((rs2 = mysql_store_result(db)) == NULL)                  if ((rs2 = mysql_store_result(db)) == NULL)
81                  {                  {
82                          log_error("Get section_master data failed\n");                          log_error("Get section_master data failed\n");
83                          return -2;                          ret = -3;
84                            break;
85                  }                  }
86                  if ((row2 = mysql_fetch_row(rs2)))                  if ((row2 = mysql_fetch_row(rs2)))
87                  {                  {
# Line 88  int load_section_config_from_db(MYSQL *d Line 101  int load_section_config_from_db(MYSQL *d
101                          p_section = section_list_create(sid, row[1], row[2], "");                          p_section = section_list_create(sid, row[1], row[2], "");
102                          if (p_section == NULL)                          if (p_section == NULL)
103                          {                          {
104                                  log_error("load_section_config_from_db() 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]);
105                                    ret = -4;
106                                  break;                                  break;
107                          }                          }
108    
# Line 130  int load_section_config_from_db(MYSQL *d Line 144  int load_section_config_from_db(MYSQL *d
144          }          }
145          mysql_free_result(rs);          mysql_free_result(rs);
146    
147          return 0;          mysql_close(db);
148    
149            return ret;
150    }
151    
152    int append_articles_from_db(int32_t start_aid, int global_lock)
153    {
154            MYSQL *db;
155            MYSQL_RES *rs;
156            MYSQL_ROW row;
157            char sql[SQL_BUFFER_LEN];
158            ARTICLE article;
159            SECTION_LIST *p_section = NULL;
160            int32_t last_sid = 0;
161            int ret = 0;
162            int i;
163    
164            db = db_open();
165            if (db == NULL)
166            {
167                    log_error("db_open() error: %s\n", mysql_error(db));
168                    return -2;
169            }
170    
171            snprintf(sql, sizeof(sql),
172                             "SELECT AID, TID, SID, CID, UID, visible, excerption, "
173                             "ontop, `lock`, username, nickname, title, UNIX_TIMESTAMP(sub_dt) AS sub_dt "
174                             "FROM bbs WHERE AID >= %d ORDER BY AID",
175                             start_aid);
176    
177            if (mysql_query(db, sql) != 0)
178            {
179                    log_error("Query article list error: %s\n", mysql_error(db));
180                    return -3;
181            }
182            if ((rs = mysql_use_result(db)) == NULL)
183            {
184                    log_error("Get article list data failed\n");
185                    return -3;
186            }
187    
188            // acquire global lock
189            if (global_lock)
190            {
191                    if ((ret = section_list_rw_lock(NULL)) < 0)
192                    {
193                            log_error("section_list_rw_lock(sid = 0) error\n");
194                            goto cleanup;
195                    }
196            }
197    
198            while ((row = mysql_fetch_row(rs)))
199            {
200                    bzero(&article, sizeof(ARTICLE));
201    
202                    // copy data of article
203                    i = 0;
204    
205                    article.aid = atoi(row[i++]);
206                    article.tid = atoi(row[i++]);
207                    article.sid = atoi(row[i++]);
208                    article.cid = atoi(row[i++]);
209                    article.uid = atoi(row[i++]);
210                    article.visible = (int8_t)atoi(row[i++]);
211                    article.excerption = (int8_t)atoi(row[i++]);
212                    article.ontop = (int8_t)atoi(row[i++]);
213                    article.lock = (int8_t)atoi(row[i++]);
214    
215                    strncpy(article.username, row[i++], sizeof(article.username) - 1);
216                    article.username[sizeof(article.username) - 1] = '\0';
217                    strncpy(article.nickname, row[i++], sizeof(article.nickname) - 1);
218                    article.nickname[sizeof(article.nickname) - 1] = '\0';
219                    strncpy(article.title, row[i++], sizeof(article.title) - 1);
220                    article.title[sizeof(article.title) - 1] = '\0';
221    
222                    article.sub_dt = atol(row[i++]);
223    
224                    // release lock of last section if different from current one
225                    if (!global_lock && article.sid != last_sid && last_sid != 0)
226                    {
227                            if ((ret = section_list_rw_unlock(p_section)) < 0)
228                            {
229                                    log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
230                                    break;
231                            }
232                    }
233    
234                    if ((p_section = section_list_find_by_sid(article.sid)) == NULL)
235                    {
236                            log_error("section_list_find_by_sid(%d) error: unknown section, try reloading section config\n", article.sid);
237                            ret = -4; // known section found
238                            break;
239                    }
240    
241                    // acquire lock of current section if different from last one
242                    if (!global_lock && article.sid != last_sid)
243                    {
244                            if ((ret = section_list_rw_lock(NULL)) < 0)
245                            {
246                                    log_error("section_list_rw_lock(sid = 0) error\n");
247                                    break;
248                            }
249                    }
250    
251                    // append article to section list
252                    last_sid = article.sid;
253    
254                    if (section_list_append_article(p_section, &article) < 0)
255                    {
256                            log_error("section_list_append_article(sid = %d, aid = %d) error\n",
257                                              p_section->sid, article.aid);
258                            ret = -3;
259                            break;
260                    }
261            }
262    
263            // release lock of last section
264            if (!global_lock && last_sid != 0)
265            {
266                    if ((ret = section_list_rw_unlock(p_section)) < 0)
267                    {
268                            log_error("section_list_rw_unlock(sid = %d) error\n", p_section->sid);
269                    }
270            }
271    
272            // release global lock
273            if (global_lock)
274            {
275                    if ((ret = section_list_rw_unlock(NULL)) < 0)
276                    {
277                            log_error("section_list_rw_unlock(sid = 0) error\n");
278                    }
279            }
280    
281    cleanup:
282            mysql_free_result(rs);
283    
284            mysql_close(db);
285    
286            return ret;
287  }  }


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

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