From a810a4c9436f03e0c4d84d03e6f408fa5caa6d36 Mon Sep 17 00:00:00 2001 From: De Rais Date: Sat, 16 May 2020 01:32:06 -0400 Subject: [PATCH] misc: correctly proceed to thread-deletion in rb79-delete-post The sqlite3 boilerplate accidentally forced the code into a redundant post deletion attempt, which caused a quick exit instead of a thread deletion. Also reformat. --- API | 2 +- LICENSE | 2 +- db-sqlite3.c | 123 +++++++++++++++++++++++++++++++++++------------------------ 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/API b/API index 9d0e031..5074459 100644 --- a/API +++ b/API @@ -43,4 +43,4 @@ the local IP: - curl -X POST 127.0.0.1/action -F action=rebuild -That will regenerated all static files. +That will regenerate all static files. diff --git a/LICENSE b/LICENSE index 4c40f8b..918b746 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017-2018, De Rais +Copyright (c) 2017-2020, De Rais Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the diff --git a/db-sqlite3.c b/db-sqlite3.c index 1201eae..812f009 100644 --- a/db-sqlite3.c +++ b/db-sqlite3.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018, De Rais + * Copyright (c) 2017-2020, De Rais * * Permission to use, copy, modify, and/or distribute this software for * any purpose with or without fee is hereby granted, provided that the @@ -373,7 +373,8 @@ static sqlite3 *global_db = 0; const struct configuration *conf; /* Clean the internals of a prepared_post */ -static void clean_prepared_post(struct prepared_post *p) +static void +clean_prepared_post(struct prepared_post *p) { free(p->name); free(p->subject); @@ -399,7 +400,8 @@ static void clean_prepared_post(struct prepared_post *p) * * - Any other function in this file may be safely called. */ -int setup_dbs(const struct configuration *in_conf) +int +setup_dbs(const struct configuration *in_conf) { int ret = -1; int sret = 0; @@ -599,9 +601,9 @@ done: * - Otherwise, *found is 1, and *out is a string like "/a/res/1235" * of length *out_len, which can be used in a element. */ -int db_construct_post_link(const char *board, size_t board_len, const - char *post, size_t post_len, int *found, char **out, - size_t *out_len) +int +db_construct_post_link(const char *board, size_t board_len, const char *post, + size_t post_len, int *found, char **out, size_t *out_len) { int ret = -1; int sret = 0; @@ -716,9 +718,9 @@ done: * (the thread page, the stored files for replies, etc.) have * been deleted. */ -int db_cull_and_report_threads(size_t board_idx, uintmax_t **out_thread_ids, - size_t *out_thread_ids_num, - size_t *out_num_pages) +int +db_cull_and_report_threads(size_t board_idx, uintmax_t **out_thread_ids, + size_t *out_thread_ids_num, size_t *out_num_pages) { uintmax_t *to_delete = 0; size_t to_delete_num = 0; @@ -854,9 +856,9 @@ done: * like "2020-01-01T12:34:56" and *out_ban_reason is something * like "having wrong opinions"). They are not 0. */ -static int check_ban_h(sqlite3_stmt *s, sqlite3 * db, const char *ip, time_t - now, int *out_is_banned, char **out_ban_until, - char **out_ban_reason) +static int +check_ban_h(sqlite3_stmt *s, sqlite3 * db, const char *ip, time_t now, + int *out_is_banned, char **out_ban_until, char **out_ban_reason) { int ret = -1; int sret = 0; @@ -922,9 +924,9 @@ done: * like "2020-01-01T12:34:56" and *out_ban_reason is something * like "having wrong opinions"). They are not 0. */ -int db_check_bans(const char *ip, size_t board_idx, time_t now, - int *out_is_banned, char **out_ban_until, - char **out_ban_reason) +int +db_check_bans(const char *ip, size_t board_idx, time_t now, int *out_is_banned, + char **out_ban_until, char **out_ban_reason) { int ret = -1; @@ -976,8 +978,9 @@ done: * - If *out_is_cooled != 0, then *out_cooldown_length is a string * like "20 seconds", corresponding to the cooldown row. */ -int db_check_cooldowns(const char *ip, size_t board_idx, time_t now, - int *out_is_cooled, char **out_cooldown_length) +int +db_check_cooldowns(const char *ip, size_t board_idx, time_t now, + int *out_is_cooled, char **out_cooldown_length) { int ret = -1; int sret = 0; @@ -1039,8 +1042,9 @@ done: * * - thread_dne, thread_closed, thread_full are not 0. */ -static int check_thread(uintmax_t id, size_t board_idx, int *thread_dne, - int *thread_closed, int *thread_full) +static int +check_thread(uintmax_t id, size_t board_idx, int *thread_dne, + int *thread_closed, int *thread_full) { int ret = -1; int sret = 0; @@ -1094,8 +1098,9 @@ done: * * - The memory of *out_subject should be freed by the caller. */ -int db_extract_subject(size_t board_idx, uintmax_t thread, char **out_subject, - size_t *out_subject_len) +int +db_extract_subject(size_t board_idx, uintmax_t thread, char **out_subject, + size_t *out_subject_len) { int ret = -1; int sret = 0; @@ -1144,10 +1149,10 @@ done: * of an appropriate database has been created, depending on the * input parameters in an obvious way. */ -int db_insert_ban(uint_fast8_t global_ban, size_t board_idx, const - char *first_ip, const char *last_ip, const char *message, - time_t ban_start, - time_t ban_expiry) +int +db_insert_ban(uint_fast8_t global_ban, size_t board_idx, const char *first_ip, + const char *last_ip, const char *message, time_t ban_start, time_t + ban_expiry) { int ret = -1; int sret = 0; @@ -1222,10 +1227,11 @@ done: * * - Furthermore, *post_id is the number of the inserted post. */ -int db_insert_post(const char *ip, size_t in_thread, int cooldown, struct - post_cmd *pc, int *thread_dne, int *thread_closed, - int *thread_full, - uintmax_t *post_id) +int +db_insert_post(const char *ip, size_t in_thread, int cooldown, struct + post_cmd *pc, int *thread_dne, int *thread_closed, + int *thread_full, + uintmax_t *post_id) { int ret = -1; int sret = 0; @@ -1343,7 +1349,8 @@ done: * - *out_is_op is either 1 (if the row with id = post_id has * in_thread NULL), or 0 (otherwise). */ -int db_is_op(size_t board_idx, uintmax_t post_id, uint_fast8_t *out_is_op) +int +db_is_op(size_t board_idx, uintmax_t post_id, uint_fast8_t *out_is_op) { int ret = -1; int sret = 0; @@ -1397,10 +1404,11 @@ done: * - If change_close, then the thread_closed will be adjusted to * close_status. */ -int db_moderate_post(size_t board_idx, uintmax_t post_id, const - char *moderator_comment, uint_fast8_t change_sticky, - uint_fast8_t sticky_status, - uint_fast8_t change_close, uint_fast8_t close_status) +int +db_moderate_post(size_t board_idx, uintmax_t post_id, const + char *moderator_comment, uint_fast8_t change_sticky, + uint_fast8_t sticky_status, + uint_fast8_t change_close, uint_fast8_t close_status) { int ret = -1; int sret = 0; @@ -1527,7 +1535,8 @@ done: * - The row for which id is thread_id has been removed * from the database. */ -int db_remove_post_and_files(size_t board_idx, uintmax_t post_id) +int +db_remove_post_and_files(size_t board_idx, uintmax_t post_id) { int ret = -1; int sret = 0; @@ -1603,7 +1612,8 @@ done: * - Any row for which in_thread is thread_id has been removed * from the database. */ -int db_remove_thread_and_files(size_t board_idx, uintmax_t thread_id) +int +db_remove_thread_and_files(size_t board_idx, uintmax_t thread_id) { int ret = -1; int sret = 0; @@ -1614,6 +1624,7 @@ int db_remove_thread_and_files(size_t board_idx, uintmax_t thread_id) size_t system_full_path_len = 0; char *system_thumb_path = 0; size_t system_thumb_path_len = 0; + char first_try = 1; TRY_BIND_I(s, db, "@thread", thread_id); again: @@ -1621,9 +1632,14 @@ again: switch (sret) { case SQLITE_DONE: - LOG("Board /%s/, post %ju does not exist", - conf->boards[board_idx].name, thread_id); - goto done; + + if (first_try) { + LOG("Board /%s/, post %ju does not exist", + conf->boards[board_idx].name, thread_id); + goto done; + } + + goto nowthread; case SQLITE_ROW: EXFILTRATE_TEXT(s, 8, system_full_path, system_full_path_len); EXFILTRATE_TEXT(s, 9, system_thumb_path, system_thumb_path_len); @@ -1635,12 +1651,15 @@ again: free(system_thumb_path); system_full_path = 0; system_thumb_path = 0; + first_try = 0; goto again; default: ERROR_MESSAGE("sqlite3_step(): %s", sqlite3_errmsg(db)); goto done; } +nowthread: + if (wt_remove_thread_page(board_idx, thread_id) < 0) { goto done; } @@ -1684,11 +1703,11 @@ done: * - `select file_info from comments where id is @post_id', on the * correct board, would return info. */ -int db_update_file_info(size_t board_idx, uintmax_t post_id, const char *info, - size_t info_len, const char *system_full_path, size_t - system_full_path_len, - const char *system_thumb_path, size_t - system_thumb_path_len) +int +db_update_file_info(size_t board_idx, uintmax_t post_id, const char *info, + size_t info_len, const char *system_full_path, size_t + system_full_path_len, + const char *system_thumb_path, size_t system_thumb_path_len) { int ret = -1; int sret = 0; @@ -1747,8 +1766,9 @@ done: * this means wt_write_post() has been called on the rows that * correspond to thread. */ -int db_writeback_posts_in_thread(size_t board_idx, uintmax_t thread, FILE *f, - post_writeback pw_function) +int +db_writeback_posts_in_thread(size_t board_idx, uintmax_t thread, FILE *f, + post_writeback pw_function) { int ret = -1; int sret = 0; @@ -1825,7 +1845,8 @@ done: * written out to f. In practice, this means wt_write_recent_post() * has been called on the rows that correspond to thread. */ -int db_writeback_recent_posts(FILE *f, post_writeback pw_function) +int +db_writeback_recent_posts(FILE *f, post_writeback pw_function) { int ret = -1; int sret = 0; @@ -1955,8 +1976,9 @@ done: * on the rows that correspond to the OP and the last few posts * of a thread. */ -int db_writeback_thread_summaries(size_t board_idx, uintmax_t *thread_ids, - size_t thread_ids_num, FILE *f) +int +db_writeback_thread_summaries(size_t board_idx, uintmax_t *thread_ids, size_t + thread_ids_num, FILE *f) { int ret = -1; int sret = 0; @@ -2044,7 +2066,8 @@ done: * * - setup_dbs() can be safely called again. */ -int clean_dbs(void) +int +clean_dbs(void) { /* Close board connections */ for (size_t j = 0; j < num_connected_db; ++j) { -- 2.11.4.GIT