server: remove useless casts
[rb-79.git] / preconditions.c
blobb9ef18b5f24d2775b119e892b9539cd3ddee01d0
1 /*
2 * Copyright (c) 2017, 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 <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <time.h>
25 #include <unistd.h>
27 #include "macros.h"
28 #include "rb79.h"
30 /* Try and acquire all lock files, in order */
31 static int try_get_all_lock_files(size_t num_boards)
33 for (size_t j = 0; j < num_boards; ++j) {
34 if (lock_acquire(j) < 0) {
35 return -1;
39 return 0;
42 /* Try and release all lock files, in order */
43 static int try_drop_all_lock_files(size_t num_boards)
45 for (size_t j = 0; j < num_boards; ++j) {
46 lock_release(j);
49 return 0;
52 /* Make sure we can write to the directory where all the www files will be */
53 static int try_write_work_dir(const char *work_dir)
55 int ret = -1;
57 /* First, the directories should be there */
58 if (mkdir(work_dir, 0755) < 0) {
59 if (errno != EEXIST) {
60 PERROR_MESSAGE("mkdir");
61 ERROR_MESSAGE("Cannot create directory %s", work_dir);
62 goto done;
66 ret = 0;
67 done:
69 return ret;
72 /* Make sure we can write to the directory where all the www files will be */
73 static int try_write_www_dir(const struct board *boards, size_t boards_num,
74 const char *static_www_folder)
76 int ret = -1;
77 char *path = 0;
79 /* First, the directories should be there */
80 for (size_t j = 0; j < boards_num; ++j) {
81 size_t len = snprintf(0, 0, "%s/%s", static_www_folder,
82 boards[j].name);
84 if (!(path = malloc(len + 1))) {
85 PERROR_MESSAGE("malloc");
86 goto done;
89 sprintf(path, "%s/%s", static_www_folder, boards[j].name);
91 if (mkdir(path, 0755) < 0) {
92 if (errno != EEXIST) {
93 PERROR_MESSAGE("mkdir");
94 ERROR_MESSAGE("Cannot create directory %s",
95 path);
96 goto done;
100 free(path);
101 path = 0;
104 /* Now, the src/ and res/ dirs should be there */
105 for (size_t j = 0; j < boards_num; ++j) {
106 size_t len = snprintf(0, 0, "%s/%s/src", static_www_folder,
107 boards[j].name);
109 if (!(path = malloc(len + 1))) {
110 PERROR_MESSAGE("malloc");
111 goto done;
114 sprintf(path, "%s/%s/src", static_www_folder, boards[j].name);
116 if (mkdir(path, 0755) < 0) {
117 if (errno != EEXIST) {
118 PERROR_MESSAGE("mkdir");
119 ERROR_MESSAGE("Cannot create directory %s",
120 path);
121 goto done;
125 sprintf(path, "%s/%s/res", static_www_folder, boards[j].name);
127 if (mkdir(path, 0755) < 0) {
128 if (errno != EEXIST) {
129 PERROR_MESSAGE("mkdir");
130 ERROR_MESSAGE("Cannot create directory %s",
131 path);
132 goto done;
136 free(path);
137 path = 0;
140 /* Now, we need to be able to write there */
141 for (size_t j = 0; j < boards_num; ++j) {
142 size_t len = snprintf(0, 0, "%s/%s/src/a", static_www_folder,
143 boards[j].name);
144 FILE *f = 0;
146 if (!(path = malloc(len + 1))) {
147 PERROR_MESSAGE("malloc");
148 goto done;
151 sprintf(path, "%s/%s/src/a", static_www_folder, boards[j].name);
153 if (!(f = fopen(path, "a"))) {
154 PERROR_MESSAGE("fopen");
155 ERROR_MESSAGE("Cannot create dummy file %s", path);
156 goto done;
159 fclose(f);
160 unlink(path);
161 sprintf(path, "%s/%s/res/a", static_www_folder, boards[j].name);
163 if (!(f = fopen(path, "a"))) {
164 PERROR_MESSAGE("fopen");
165 ERROR_MESSAGE("Cannot create dummy file %s", path);
166 goto done;
169 fclose(f);
170 unlink(path);
171 free(path);
172 path = 0;
175 ret = 0;
176 done:
177 free(path);
179 return ret;
183 * Returns < 0 in case something isn't set up right
185 * Preconditions:
187 * - conf is correctly set up.
189 * Postconditions (success):
191 * - The ZZZ_setup() functions in other files have all been called,
192 * and all returned successfully.
194 int preconditions_check(const struct configuration *conf)
196 int ret = -1;
197 size_t j = 0;
198 uintmax_t max_size = (size_t) -1;
200 for (j = 0; j < conf->filetypes_num; ++j) {
201 const struct filetype *f = &conf->filetypes[j];
203 if (f->thumb_creation_command &&
204 f->static_thumbnail) {
205 ERROR_MESSAGE("Filetype for %s cannot have both "
206 "thumb_creation_command and static_thumbnail",
207 f->mime_type);
208 goto done;
211 if (!f->thumb_creation_command &&
212 !f->static_thumbnail) {
213 ERROR_MESSAGE("Filetype for %s must have one of "
214 "thumb_creation_command or static_thumbnail",
215 f->mime_type);
216 goto done;
220 for (j = 0; j < conf->boards_num; ++j) {
221 const struct board *b = &conf->boards[j];
223 if (!b->num_pages) {
224 ERROR_MESSAGE("Board /%s/'s num_pages must be positive",
225 b->name);
226 goto done;
229 if (!b->threads_per_page) {
230 ERROR_MESSAGE("Board /%s/'s threads_per_page must be "
231 "positive", b->name);
232 goto done;
235 if (b->num_pages * b->threads_per_page > max_size) {
236 ERROR_MESSAGE("Board /%s/'s threads cannot be "
237 "fit in memory", b->name);
238 goto done;
242 if (try_write_work_dir(conf->work_path) < 0) {
243 goto done;
246 if (setup_multipart() < 0) {
247 goto done;
250 if (setup_sanitize_comment(conf) < 0) {
251 goto done;
254 if (setup_sanitize_file(conf) < 0) {
255 goto done;
258 if (setup_tripcodes(conf) < 0) {
259 goto done;
262 if (setup_locks(conf) < 0) {
263 goto done;
266 if (setup_write_thread(conf) < 0) {
267 goto done;
270 if (try_get_all_lock_files(conf->boards_num) < 0) {
271 goto done;
274 /* Since we have the lock files now, we need not fear races */
275 if (setup_dbs(conf) < 0) {
276 goto done;
279 if (try_write_www_dir(conf->boards, conf->boards_num,
280 conf->static_www_folder) < 0) {
281 goto done;
284 ret = 0;
285 done:
287 if (try_drop_all_lock_files(conf->boards_num) < 0) {
288 ret = -1;
291 return ret;