| 1 |
sysadm |
1.1 |
/***************************************************************************
|
| 2 |
|
|
section_list_loader.c - description
|
| 3 |
|
|
-------------------
|
| 4 |
|
|
Copyright : (C) 2004-2025 by Leaflet
|
| 5 |
|
|
Email : leaflet@leafok.com
|
| 6 |
|
|
***************************************************************************/
|
| 7 |
|
|
|
| 8 |
|
|
/***************************************************************************
|
| 9 |
|
|
* *
|
| 10 |
|
|
* This program is free software; you can redistribute it and/or modify *
|
| 11 |
|
|
* it under the terms of the GNU General Public License as published by *
|
| 12 |
|
|
* the Free Software Foundation; either version 3 of the License, or *
|
| 13 |
|
|
* (at your option) any later version. *
|
| 14 |
|
|
* *
|
| 15 |
|
|
***************************************************************************/
|
| 16 |
|
|
|
| 17 |
|
|
#include "section_list_loader.h"
|
| 18 |
|
|
#include "log.h"
|
| 19 |
|
|
#include "database.h"
|
| 20 |
|
|
#include <stdio.h>
|
| 21 |
|
|
#include <string.h>
|
| 22 |
|
|
#include <errno.h>
|
| 23 |
|
|
#include <stdlib.h>
|
| 24 |
sysadm |
1.3 |
#include <strings.h>
|
| 25 |
sysadm |
1.1 |
|
| 26 |
sysadm |
1.2 |
int load_section_config_from_db(void)
|
| 27 |
sysadm |
1.1 |
{
|
| 28 |
sysadm |
1.2 |
MYSQL *db;
|
| 29 |
sysadm |
1.1 |
MYSQL_RES *rs, *rs2;
|
| 30 |
|
|
MYSQL_ROW row, row2;
|
| 31 |
|
|
char sql[SQL_BUFFER_LEN];
|
| 32 |
|
|
int32_t sid;
|
| 33 |
|
|
char master_name[BBS_username_max_len + 1];
|
| 34 |
|
|
SECTION_LIST *p_section;
|
| 35 |
|
|
int ret;
|
| 36 |
|
|
|
| 37 |
sysadm |
1.2 |
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 |
sysadm |
1.1 |
snprintf(sql, sizeof(sql),
|
| 45 |
sysadm |
1.2 |
"SELECT section_config.SID, sname, section_config.title, section_config.CID, "
|
| 46 |
|
|
"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 "
|
| 48 |
sysadm |
1.1 |
"ORDER BY section_config.SID");
|
| 49 |
|
|
|
| 50 |
|
|
if (mysql_query(db, sql) != 0)
|
| 51 |
|
|
{
|
| 52 |
|
|
log_error("Query section_list error: %s\n", mysql_error(db));
|
| 53 |
sysadm |
1.2 |
return -3;
|
| 54 |
sysadm |
1.1 |
}
|
| 55 |
|
|
if ((rs = mysql_store_result(db)) == NULL)
|
| 56 |
|
|
{
|
| 57 |
|
|
log_error("Get section_list data failed\n");
|
| 58 |
sysadm |
1.2 |
return -3;
|
| 59 |
sysadm |
1.1 |
}
|
| 60 |
sysadm |
1.2 |
|
| 61 |
|
|
ret = 0;
|
| 62 |
sysadm |
1.1 |
while ((row = mysql_fetch_row(rs)))
|
| 63 |
|
|
{
|
| 64 |
|
|
sid = atoi(row[0]);
|
| 65 |
|
|
|
| 66 |
|
|
// Query section master
|
| 67 |
|
|
snprintf(sql, sizeof(sql),
|
| 68 |
|
|
"SELECT username FROM section_master "
|
| 69 |
|
|
"INNER JOIN user_list ON section_master.UID = user_list.UID "
|
| 70 |
|
|
"WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
|
| 71 |
|
|
"ORDER BY major DESC LIMIT 1",
|
| 72 |
|
|
sid);
|
| 73 |
|
|
|
| 74 |
|
|
if (mysql_query(db, sql) != 0)
|
| 75 |
|
|
{
|
| 76 |
|
|
log_error("Query section_master error: %s\n", mysql_error(db));
|
| 77 |
sysadm |
1.2 |
ret = -3;
|
| 78 |
|
|
break;
|
| 79 |
sysadm |
1.1 |
}
|
| 80 |
|
|
if ((rs2 = mysql_store_result(db)) == NULL)
|
| 81 |
|
|
{
|
| 82 |
|
|
log_error("Get section_master data failed\n");
|
| 83 |
sysadm |
1.2 |
ret = -3;
|
| 84 |
|
|
break;
|
| 85 |
sysadm |
1.1 |
}
|
| 86 |
|
|
if ((row2 = mysql_fetch_row(rs2)))
|
| 87 |
|
|
{
|
| 88 |
|
|
strncpy(master_name, row2[0], sizeof(master_name) - 1);
|
| 89 |
|
|
master_name[sizeof(master_name) - 1] = '\0';
|
| 90 |
|
|
}
|
| 91 |
|
|
else
|
| 92 |
|
|
{
|
| 93 |
|
|
master_name[0] = '\0';
|
| 94 |
|
|
}
|
| 95 |
|
|
mysql_free_result(rs2);
|
| 96 |
|
|
|
| 97 |
|
|
p_section = section_list_find_by_sid(sid);
|
| 98 |
|
|
|
| 99 |
|
|
if (p_section == NULL)
|
| 100 |
|
|
{
|
| 101 |
|
|
p_section = section_list_create(sid, row[1], row[2], "");
|
| 102 |
|
|
if (p_section == NULL)
|
| 103 |
|
|
{
|
| 104 |
sysadm |
1.2 |
log_error("section_list_create() error: load new section sid = %d sname = %s\n", sid, row[1]);
|
| 105 |
|
|
ret = -4;
|
| 106 |
sysadm |
1.1 |
break;
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
// acquire rw lock
|
| 110 |
|
|
ret = section_list_rw_lock(p_section);
|
| 111 |
|
|
if (ret < 0)
|
| 112 |
|
|
{
|
| 113 |
|
|
break;
|
| 114 |
|
|
}
|
| 115 |
|
|
}
|
| 116 |
|
|
else
|
| 117 |
|
|
{
|
| 118 |
|
|
// acquire rw lock
|
| 119 |
|
|
ret = section_list_rw_lock(p_section);
|
| 120 |
|
|
if (ret < 0)
|
| 121 |
|
|
{
|
| 122 |
|
|
break;
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
|
| 126 |
|
|
p_section->sname[sizeof(p_section->sname) - 1] = '\0';
|
| 127 |
|
|
strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
|
| 128 |
|
|
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
|
| 129 |
|
|
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1);
|
| 130 |
|
|
p_section->master_name[sizeof(p_section->master_name) - 1] = '\0';
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
p_section->class_id = atoi(row[3]);
|
| 134 |
|
|
p_section->read_user_level = atoi(row[4]);
|
| 135 |
|
|
p_section->write_user_level = atoi(row[5]);
|
| 136 |
|
|
p_section->enable = (int8_t)atoi(row[6]);
|
| 137 |
|
|
|
| 138 |
|
|
// release rw lock
|
| 139 |
|
|
ret = section_list_rw_unlock(p_section);
|
| 140 |
|
|
if (ret < 0)
|
| 141 |
|
|
{
|
| 142 |
|
|
break;
|
| 143 |
|
|
}
|
| 144 |
|
|
}
|
| 145 |
|
|
mysql_free_result(rs);
|
| 146 |
|
|
|
| 147 |
sysadm |
1.2 |
mysql_close(db);
|
| 148 |
|
|
|
| 149 |
|
|
return ret;
|
| 150 |
sysadm |
1.1 |
}
|
| 151 |
sysadm |
1.3 |
|
| 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 |
|
|
}
|