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
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.
23 #include <sys/types.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) {
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
) {
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
)
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
);
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
)
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
,
84 if (!(path
= malloc(len
+ 1))) {
85 PERROR_MESSAGE("malloc");
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",
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
,
109 if (!(path
= malloc(len
+ 1))) {
110 PERROR_MESSAGE("malloc");
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",
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",
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
,
146 if (!(path
= malloc(len
+ 1))) {
147 PERROR_MESSAGE("malloc");
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
);
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
);
183 * Returns < 0 in case something isn't set up right
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
)
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",
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",
220 for (j
= 0; j
< conf
->boards_num
; ++j
) {
221 const struct board
*b
= &conf
->boards
[j
];
224 ERROR_MESSAGE("Board /%s/'s num_pages must be positive",
229 if (!b
->threads_per_page
) {
230 ERROR_MESSAGE("Board /%s/'s threads_per_page must be "
231 "positive", b
->name
);
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
);
242 if (try_write_work_dir(conf
->work_path
) < 0) {
246 if (setup_multipart() < 0) {
250 if (setup_sanitize_comment(conf
) < 0) {
254 if (setup_sanitize_file(conf
) < 0) {
258 if (setup_tripcodes(conf
) < 0) {
262 if (setup_locks(conf
) < 0) {
266 if (setup_write_thread(conf
) < 0) {
270 if (try_get_all_lock_files(conf
->boards_num
) < 0) {
274 /* Since we have the lock files now, we need not fear races */
275 if (setup_dbs(conf
) < 0) {
279 if (try_write_www_dir(conf
->boards
, conf
->boards_num
,
280 conf
->static_www_folder
) < 0) {
287 if (try_drop_all_lock_files(conf
->boards_num
) < 0) {