/[LeafOK_CVS]/lbbs/src/section_list_loader.c
ViewVC logotype

Diff of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Mon May 26 04:47:45 2025 UTC Revision 1.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(void)  int load_section_config_from_db(void)
27  {  {
# Line 144  int load_section_config_from_db(void) Line 145  int load_section_config_from_db(void)
145          mysql_free_result(rs);          mysql_free_result(rs);
146    
147          mysql_close(db);          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;          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