misc: correctly proceed to thread-deletion in rb79-delete-post
[rb-79.git] / rb79-delete-post.c
blob0f9f5ea317bf5e3e9a31ce6452f466fabd0de155
1 /*
2 * Copyright (c) 2017-2018, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <sodium.h>
27 #include "macros.h"
28 #include "rb79.h"
30 #include "config.h"
32 const char *program_name = "rb79-delete-post";
34 /* Show usage message, do not exit */
35 void usage(void)
37 size_t len = strlen(program_name);
39 printf("Usage: %s -b board-name\n", program_name);
40 printf(" %*s -p post-id\n", (int) len, "");
43 /* Do the thing */
44 int main(int argc, char **argv)
46 int ret = EINVAL;
47 struct configuration conf = { 0 };
48 int opt = 0;
49 const char *b_arg = 0;
50 size_t board_idx = 0;
51 const char *p_arg = 0;
52 uintmax_t post_id = 0;
53 uint_fast8_t actually_is_op = 0;
55 setlocale(LC_ALL, "");
57 /* Parse options */
58 while ((opt = getopt(argc, argv, "b:p:")) != -1) {
59 switch (opt) {
60 case 'b':
62 if (b_arg) {
63 ERROR_MESSAGE("-b already specified");
64 goto done;
67 b_arg = optarg;
68 break;
69 case 'p':
71 if (p_arg) {
72 ERROR_MESSAGE("-p already specified");
73 goto done;
76 p_arg = optarg;
77 break;
78 default:
79 usage();
80 goto done;
84 if (!b_arg ||
85 !p_arg) {
86 usage();
87 goto done;
90 conf = (struct configuration) {
91 /* */
92 .static_www_folder = static_www_folder, /* */
93 .work_path = work_path, /* */
94 .temp_dir_template = temp_dir_template, /* */
95 .trip_salt = trip_salt, /* */
96 .trip_salt_len = strlen(trip_salt), /* */
97 .boards = boards, /* */
98 .boards_num = NUM_OF(boards), /* */
99 .max_form_data_size = max_form_data_size, /* */
100 .max_file_size = max_file_size, /* */
101 .max_text_len = max_text_len, /* */
102 .filetypes = filetypes, /* */
103 .filetypes_num = NUM_OF(filetypes), /* */
104 .file_description_prog = file_description_prog, /* */
105 .headers = headers, /* */
106 .headers_num = NUM_OF(headers), /* */
107 .challenges = challenges, /* */
108 .challenges_num = NUM_OF(challenges), /* */
109 .wordfilter_inputs = wordfilter_inputs, /* */
110 .wordfilter_inputs_num = NUM_OF(wordfilter_inputs), /* */
113 /* Interpret board */
114 board_idx = (size_t) -1;
116 for (size_t j = 0; j < conf.boards_num; ++j) {
117 if (!strcmp(conf.boards[j].name, b_arg)) {
118 board_idx = j;
122 if (board_idx == (size_t) -1) {
123 ERROR_MESSAGE("No board \"%s\" known", b_arg);
124 goto done;
127 /* Interpret post */
128 errno = 0;
129 post_id = strtoull(p_arg, 0, 0);
131 if (errno) {
132 ret = errno;
133 PERROR_MESSAGE("strtoull");
134 goto done;
137 /* Set up a minimal part of the system */
138 if (setup_locks(&conf) < 0) {
139 goto done;
142 if (setup_dbs(&conf) < 0) {
143 goto done;
146 if (setup_write_thread(&conf) < 0) {
147 goto done;
150 if (db_is_op(board_idx, post_id, &actually_is_op) < 0) {
151 goto done;
154 if (lock_acquire(board_idx) < 0) {
155 goto done;
158 if (actually_is_op) {
159 if (db_remove_thread_and_files(board_idx, post_id) < 0) {
160 goto done;
162 } else {
163 if (db_remove_post_and_files(board_idx, post_id) < 0) {
164 goto done;
168 lock_release(board_idx);
169 util_rebuild(&conf);
170 ret = 0;
171 done:
172 clean_locks();
173 clean_dbs();
174 clean_write_thread();
176 return ret;