audiod: Demote severity level of command errors.
[paraslash.git] / upgrade_db.c
1 /* Copyright (C) 2020 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file upgrade_db.c Prepare the paraslash database for paraslash-0.7. */
4
5 #include <osl.h>
6 #include <lopsub.h>
7 #include <regex.h>
8
9 #include "upgrade_db.lsg.h"
10 #include "para.h"
11 #include "error.h"
12 #include "string.h"
13 #include "fd.h"
14 #include "crypt.h"
15 #include "version.h"
16
17 #define CMD_PTR (lls_cmd(0, upgrade_db_suite))
18 #define OPT_RESULT(_name, _lpr) \
19         (lls_opt_result(LSG_UPGRADE_DB_PARA_UPGRADE_DB_OPT_ ## _name, lpr))
20 #define OPT_GIVEN(_name, _lpr) (lls_opt_given(OPT_RESULT(_name, _lpr)))
21 #define OPT_UINT32_VAL(_name, _lpr) (lls_uint32_val(0, OPT_RESULT(_name, _lpr)))
22 #define OPT_STRING_VAL(_name, _lpr) (lls_string_val(0, OPT_RESULT(_name, _lpr)))
23
24 static int loglevel;
25 INIT_STDERR_LOGGING(loglevel);
26
27 /** Array of error strings. */
28 DEFINE_PARA_ERRLIST;
29
30 static void handle_help_flag(struct lls_parse_result *lpr)
31 {
32         char *help;
33
34         if (OPT_GIVEN(DETAILED_HELP, lpr))
35                 help = lls_long_help(CMD_PTR);
36         else if (OPT_GIVEN(HELP, lpr))
37                 help = lls_short_help(CMD_PTR);
38         else
39                 return;
40         printf("%s\n", help);
41         free(help);
42         exit(EXIT_SUCCESS);
43 }
44
45 static struct stat *path_exists(const char *path)
46 {
47         static struct stat sb;
48
49         if (stat(path, &sb) < 0)
50                 return NULL;
51         return &sb;
52 }
53
54 static bool is_dir(const char *path)
55 {
56         struct stat *sb = path_exists(path);
57         if (!sb)
58                 return false;
59         return (sb->st_mode & S_IFMT) == S_IFDIR;
60 }
61
62 __noreturn static void die(const char *msg)
63 {
64         PARA_EMERG_LOG("%s\n", msg);
65         exit(EXIT_FAILURE);
66 }
67
68 static int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
69 {
70         const char *str1 = obj1->data;
71         const char *str2 = obj2->data;
72         return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
73 }
74
75 static char *src_db_dir, *dst_db_dir, *src_aft_dir, *dst_aft_dir;
76
77 static void set_paths(const struct lls_parse_result *lpr)
78 {
79         char *home = para_homedir();
80
81         if (OPT_GIVEN(SRC_DATABASE_DIR, lpr))
82                 src_db_dir = para_strdup(OPT_STRING_VAL(SRC_DATABASE_DIR,
83                         lpr));
84         else
85                 src_db_dir = make_message(
86                         "%s/.paraslash/afs_database-0.4", home);
87         if (OPT_GIVEN(DST_DATABASE_DIR, lpr))
88                 dst_db_dir = para_strdup(OPT_STRING_VAL(DST_DATABASE_DIR,
89                         lpr));
90         else
91                 dst_db_dir = make_message(
92                         "%s/.paraslash/afs_database-0.7", home);
93         free(home);
94         src_aft_dir = make_message("%s/audio_files", src_db_dir);
95         dst_aft_dir = make_message("%s/audio-files", src_db_dir);
96         PARA_NOTICE_LOG("source aft dir: %s\n", src_aft_dir);
97         PARA_NOTICE_LOG("destination aft dir: %s\n", dst_aft_dir);
98 }
99
100 static void check_sanity(void)
101 {
102         PARA_INFO_LOG("checking source and destination directories\n");
103         if (!is_dir(src_db_dir))
104                 die("source db directory does not exist");
105         if (path_exists(dst_db_dir))
106                 die("destination db already exists");
107         if (!is_dir(src_aft_dir))
108                 die("source audio file table does not exist");
109         if (path_exists(dst_aft_dir))
110                 die("destination audio file table already exists");
111 }
112
113 /** The columns of the audio file table (both old and new). */
114 enum audio_file_table_columns {
115         /** The hash on the content of the audio file. */
116         AFTCOL_HASH,
117         /** The full path in the filesystem. */
118         AFTCOL_PATH,
119         /** The audio file selector info. */
120         AFTCOL_AFSI,
121         /** The audio format handler info. */
122         AFTCOL_AFHI,
123         /** The chunk table info and the chunk table of the audio file. */
124         AFTCOL_CHUNKS,
125         /** The number of columns of this table. */
126         NUM_AFT_COLUMNS
127 };
128
129 #define AFSI_SIZE 32
130
131 static int src_aft_hash_compare(const struct osl_object *obj1,
132                 const struct osl_object *obj2)
133 {
134         return hash_compare((unsigned char *)obj1->data,
135                 (unsigned char *)obj2->data);
136 }
137
138 static struct osl_column_description src_aft_cols[] = {
139         [AFTCOL_HASH] = {
140                 .storage_type = OSL_MAPPED_STORAGE,
141                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
142                 .name = "hash",
143                 .compare_function = src_aft_hash_compare,
144                 .data_size = HASH_SIZE
145         },
146         [AFTCOL_PATH] = {
147                 .storage_type = OSL_MAPPED_STORAGE,
148                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
149                 .name = "path",
150                 .compare_function = string_compare,
151         },
152         [AFTCOL_AFSI] = {
153                 .storage_type = OSL_MAPPED_STORAGE,
154                 .storage_flags = OSL_FIXED_SIZE,
155                 .name = "afs_info",
156                 .data_size = AFSI_SIZE
157         },
158         [AFTCOL_AFHI] = {
159                 .storage_type = OSL_MAPPED_STORAGE,
160                 .name = "afh_info",
161         },
162         [AFTCOL_CHUNKS] = {
163                 .storage_type = OSL_DISK_STORAGE,
164                 .name = "chunks",
165         }
166 };
167
168 static struct osl_table_description src_aft_desc = {
169         .name = "audio_files",
170         .num_columns = NUM_AFT_COLUMNS,
171         .flags = OSL_LARGE_TABLE,
172         .column_descriptions = src_aft_cols
173 };
174
175 static struct osl_table *src_aft, *dst_aft;
176
177 static void open_src_aft(void)
178 {
179         int ret;
180
181         PARA_NOTICE_LOG("opening: %s\n", src_aft_dir);
182         src_aft_desc.dir = src_db_dir;
183         ret = osl(osl_open_table(&src_aft_desc, &src_aft));
184         if (ret < 0) {
185                 PARA_EMERG_LOG("can not open source audio file table: %s\n",
186                          para_strerror(-ret));
187                 exit(EXIT_FAILURE);
188         }
189         PARA_INFO_LOG("successfully opened source audio file table\n");
190 }
191
192 static int dst_aft_hash_compare(const struct osl_object *obj1,
193                 const struct osl_object *obj2)
194 {
195         return hash2_compare((unsigned char *)obj1->data,
196                 (unsigned char *)obj2->data);
197 }
198
199 /* identical to src_aft_cols except the comparator and the hash size. */
200 static struct osl_column_description dst_aft_cols[] = {
201         [AFTCOL_HASH] = {
202                 .storage_type = OSL_MAPPED_STORAGE,
203                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
204                 .name = "hash",
205                 .compare_function = dst_aft_hash_compare,
206                 .data_size = HASH2_SIZE
207         },
208         [AFTCOL_PATH] = {
209                 .storage_type = OSL_MAPPED_STORAGE,
210                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
211                 .name = "path",
212                 .compare_function = string_compare,
213         },
214         [AFTCOL_AFSI] = {
215                 .storage_type = OSL_MAPPED_STORAGE,
216                 .storage_flags = OSL_FIXED_SIZE,
217                 .name = "afs_info",
218                 .data_size = AFSI_SIZE
219         },
220         [AFTCOL_AFHI] = {
221                 .storage_type = OSL_MAPPED_STORAGE,
222                 .name = "afh_info",
223         },
224         [AFTCOL_CHUNKS] = {
225                 .storage_type = OSL_DISK_STORAGE,
226                 .name = "chunks",
227         }
228 };
229
230 static struct osl_table_description dst_aft_desc = {
231         .name = "audio-files",
232         .num_columns = NUM_AFT_COLUMNS,
233         .flags = OSL_LARGE_TABLE,
234         .column_descriptions = dst_aft_cols
235 };
236
237 static int create_and_open_dst_aft(void)
238 {
239         int ret;
240
241         PARA_NOTICE_LOG("creating %s\n", dst_aft_dir);
242         dst_aft_desc.dir = src_db_dir;
243         ret = osl(osl_create_table(&dst_aft_desc));
244         if (ret < 0) {
245                 PARA_EMERG_LOG("could not create destination audio file table\n");
246                 return ret;
247         }
248         ret = osl(osl_open_table(&dst_aft_desc, &dst_aft));
249         if (ret < 0) {
250                 PARA_EMERG_LOG("could not open destination audio file table: %s\n",
251                          para_strerror(-ret));
252                 exit(EXIT_FAILURE);
253         }
254         PARA_INFO_LOG("successfully opened destination audio file table\n");
255         return 0;
256 }
257
258 static int copy_aft_row(struct osl_row *row, void *data)
259 {
260         unsigned *n = data;
261         int i, ret;
262         unsigned char hash2[HASH2_SIZE] = "\0";
263         struct osl_object objs[NUM_AFT_COLUMNS] = {
264                 [AFTCOL_HASH] = {.data = hash2, .size = HASH2_SIZE}
265         };
266
267         ret = osl(osl_open_disk_object(src_aft, row, AFTCOL_CHUNKS,
268                 objs + AFTCOL_CHUNKS));
269         if (ret < 0) {
270                 PARA_ERROR_LOG("can not open disk object: %s\n",
271                         para_strerror(-ret));
272                 return ret;
273         }
274         for (i = 0; i < NUM_AFT_COLUMNS; i++) {
275                 if (i == AFTCOL_HASH) /* never assign to this index */
276                         continue;
277                 if (i == AFTCOL_CHUNKS) /* disk storage object handled above */
278                         continue;
279                 /* mapped storage */
280                 ret = osl(osl_get_object(src_aft, row, i, objs + i));
281                 if (ret < 0) {
282                         PARA_ERROR_LOG("get_object (col = %d): %s\n",
283                                 i, para_strerror(-ret));
284                         return ret;
285                 }
286                 if (i == AFTCOL_PATH)
287                         PARA_DEBUG_LOG("copying %s\n", (char *)objs[i].data);
288         }
289         (*n)++;
290         memcpy(hash2, n, sizeof(*n));
291         ret = osl(osl_add_row(dst_aft, objs));
292         if (ret < 0)
293                 PARA_ERROR_LOG("failed to add row: %s\n", para_strerror(-ret));
294         osl_close_disk_object(objs + AFTCOL_CHUNKS);
295         return ret;
296 }
297
298 static int convert_aft(void)
299 {
300         unsigned n;
301         int ret;
302
303         osl_get_num_rows(src_aft, &n);
304         PARA_NOTICE_LOG("converting hash of %u rows to sha256\n", n);
305         n = 0;
306         ret = osl(osl_rbtree_loop(src_aft, AFTCOL_HASH, &n, copy_aft_row));
307         if (ret < 0)
308                 PARA_ERROR_LOG("osl_rbtree_loop failed\n");
309         return ret;
310 }
311
312 static int remove_source_aft(void)
313 {
314         pid_t pid;
315         int fds[3] = {-1, -1, -1}; /* no redirection of stdin/stdout/stderr */
316         int ret, wstatus;
317         char *cmdline = make_message("rm -rf %s", src_aft_dir);
318
319         PARA_NOTICE_LOG("removing %s\n", src_aft_dir);
320         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
321         if (ret < 0) {
322                 PARA_ERROR_LOG("exec failure\n");
323                 goto out;
324         }
325         do {
326                 ret = waitpid(pid, &wstatus, 0);
327         } while (ret < 0 && errno == EINTR);
328         if (ret < 0)
329                 PARA_ERROR_LOG("waitpid failure\n");
330 out:
331         return ret;
332 }
333
334 static int rename_db(void)
335 {
336         PARA_NOTICE_LOG("renaming %s -> %s\n", src_db_dir, dst_db_dir);
337         if (rename(src_db_dir, dst_db_dir) < 0) {
338                 int ret = -ERRNO_TO_PARA_ERROR(errno);
339                 PARA_ERROR_LOG("rename failed\n");
340                 return ret;
341         }
342         return 1;
343 }
344
345 int main(int argc, char *argv[])
346 {
347         struct lls_parse_result *lpr; /* command line */
348         char *errctx;
349         int ret;
350
351         ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
352         if (ret < 0)
353                 goto out;
354         loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
355         version_handle_flag("recv", OPT_GIVEN(VERSION, lpr));
356         handle_help_flag(lpr);
357         set_paths(lpr);
358         check_sanity();
359         open_src_aft();
360         ret = create_and_open_dst_aft();
361         if (ret < 0)
362                 goto close_src_aft;
363         ret = convert_aft();
364         if (ret < 0)
365                 goto close_dst_aft;
366         ret = remove_source_aft();
367         if (ret < 0)
368                 goto close_dst_aft;
369         ret = rename_db();
370 close_dst_aft:
371         osl_close_table(dst_aft, OSL_MARK_CLEAN);
372 close_src_aft:
373         PARA_INFO_LOG("closing audio file tables\n");
374         osl_close_table(src_aft, OSL_MARK_CLEAN);
375 out:
376         if (ret < 0) {
377                 if (errctx)
378                         PARA_ERROR_LOG("%s\n", errctx);
379                 free(errctx);
380                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
381         } else {
382                 PARA_WARNING_LOG("success. Now start para_server and force-add"
383                         " all audio files.\n");
384         }
385         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
386 }