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