]> git.tuebingen.mpg.de Git - paraslash.git/blob - upgrade_db.c
Merge topic branch t/sf_float into pu
[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         const char *confdir = get_confdir();
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("%s/afs_database-0.4", confdir);
86         if (OPT_GIVEN(DST_DATABASE_DIR, lpr))
87                 dst_db_dir = para_strdup(OPT_STRING_VAL(DST_DATABASE_DIR,
88                         lpr));
89         else
90                 dst_db_dir = make_message("%s/afs_database-0.7", confdir);
91         src_aft_dir = make_message("%s/audio_files", src_db_dir);
92         dst_aft_dir = make_message("%s/audio-files", src_db_dir);
93         PARA_NOTICE_LOG("source aft dir: %s\n", src_aft_dir);
94         PARA_NOTICE_LOG("destination aft dir: %s\n", dst_aft_dir);
95 }
96
97 static void check_sanity(void)
98 {
99         PARA_INFO_LOG("checking source and destination directories\n");
100         if (!is_dir(src_db_dir))
101                 die("source db directory does not exist");
102         if (path_exists(dst_db_dir))
103                 die("destination db already exists");
104         if (!is_dir(src_aft_dir))
105                 die("source audio file table does not exist");
106         if (path_exists(dst_aft_dir))
107                 die("destination audio file table already exists");
108 }
109
110 /** The columns of the audio file table (both old and new). */
111 enum audio_file_table_columns {
112         /** The hash on the content of the audio file. */
113         AFTCOL_HASH,
114         /** The full path in the filesystem. */
115         AFTCOL_PATH,
116         /** The audio file selector info. */
117         AFTCOL_AFSI,
118         /** The audio format handler info. */
119         AFTCOL_AFHI,
120         /** The chunk table info and the chunk table of the audio file. */
121         AFTCOL_CHUNKS,
122         /** The number of columns of this table. */
123         NUM_AFT_COLUMNS
124 };
125
126 #define AFSI_SIZE 32
127
128 static int src_aft_hash_compare(const struct osl_object *obj1,
129                 const struct osl_object *obj2)
130 {
131         return hash_compare((unsigned char *)obj1->data,
132                 (unsigned char *)obj2->data);
133 }
134
135 static struct osl_column_description src_aft_cols[] = {
136         [AFTCOL_HASH] = {
137                 .storage_type = OSL_MAPPED_STORAGE,
138                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
139                 .name = "hash",
140                 .compare_function = src_aft_hash_compare,
141                 .data_size = HASH_SIZE
142         },
143         [AFTCOL_PATH] = {
144                 .storage_type = OSL_MAPPED_STORAGE,
145                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
146                 .name = "path",
147                 .compare_function = string_compare,
148         },
149         [AFTCOL_AFSI] = {
150                 .storage_type = OSL_MAPPED_STORAGE,
151                 .storage_flags = OSL_FIXED_SIZE,
152                 .name = "afs_info",
153                 .data_size = AFSI_SIZE
154         },
155         [AFTCOL_AFHI] = {
156                 .storage_type = OSL_MAPPED_STORAGE,
157                 .name = "afh_info",
158         },
159         [AFTCOL_CHUNKS] = {
160                 .storage_type = OSL_DISK_STORAGE,
161                 .name = "chunks",
162         }
163 };
164
165 static struct osl_table_description src_aft_desc = {
166         .name = "audio_files",
167         .num_columns = NUM_AFT_COLUMNS,
168         .flags = OSL_LARGE_TABLE,
169         .column_descriptions = src_aft_cols
170 };
171
172 static struct osl_table *src_aft, *dst_aft;
173
174 static void open_src_aft(void)
175 {
176         int ret;
177
178         PARA_NOTICE_LOG("opening: %s\n", src_aft_dir);
179         src_aft_desc.dir = src_db_dir;
180         ret = osl(osl_open_table(&src_aft_desc, &src_aft));
181         if (ret < 0) {
182                 PARA_EMERG_LOG("can not open source audio file table: %s\n",
183                          para_strerror(-ret));
184                 exit(EXIT_FAILURE);
185         }
186         PARA_INFO_LOG("successfully opened source audio file table\n");
187 }
188
189 static int dst_aft_hash_compare(const struct osl_object *obj1,
190                 const struct osl_object *obj2)
191 {
192         return hash2_compare((unsigned char *)obj1->data,
193                 (unsigned char *)obj2->data);
194 }
195
196 /* identical to src_aft_cols except the comparator and the hash size. */
197 static struct osl_column_description dst_aft_cols[] = {
198         [AFTCOL_HASH] = {
199                 .storage_type = OSL_MAPPED_STORAGE,
200                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
201                 .name = "hash",
202                 .compare_function = dst_aft_hash_compare,
203                 .data_size = HASH2_SIZE
204         },
205         [AFTCOL_PATH] = {
206                 .storage_type = OSL_MAPPED_STORAGE,
207                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
208                 .name = "path",
209                 .compare_function = string_compare,
210         },
211         [AFTCOL_AFSI] = {
212                 .storage_type = OSL_MAPPED_STORAGE,
213                 .storage_flags = OSL_FIXED_SIZE,
214                 .name = "afs_info",
215                 .data_size = AFSI_SIZE
216         },
217         [AFTCOL_AFHI] = {
218                 .storage_type = OSL_MAPPED_STORAGE,
219                 .name = "afh_info",
220         },
221         [AFTCOL_CHUNKS] = {
222                 .storage_type = OSL_DISK_STORAGE,
223                 .name = "chunks",
224         }
225 };
226
227 static struct osl_table_description dst_aft_desc = {
228         .name = "audio-files",
229         .num_columns = NUM_AFT_COLUMNS,
230         .flags = OSL_LARGE_TABLE,
231         .column_descriptions = dst_aft_cols
232 };
233
234 static int create_and_open_dst_aft(void)
235 {
236         int ret;
237
238         PARA_NOTICE_LOG("creating %s\n", dst_aft_dir);
239         dst_aft_desc.dir = src_db_dir;
240         ret = osl(osl_create_table(&dst_aft_desc));
241         if (ret < 0) {
242                 PARA_EMERG_LOG("could not create destination audio file table\n");
243                 return ret;
244         }
245         ret = osl(osl_open_table(&dst_aft_desc, &dst_aft));
246         if (ret < 0) {
247                 PARA_EMERG_LOG("could not open destination audio file table: %s\n",
248                          para_strerror(-ret));
249                 exit(EXIT_FAILURE);
250         }
251         PARA_INFO_LOG("successfully opened destination audio file table\n");
252         return 0;
253 }
254
255 static int copy_aft_row(struct osl_row *row, void *data)
256 {
257         unsigned *n = data;
258         int i, ret;
259         unsigned char hash2[HASH2_SIZE] = "\0";
260         struct osl_object objs[NUM_AFT_COLUMNS] = {
261                 [AFTCOL_HASH] = {.data = hash2, .size = HASH2_SIZE}
262         };
263
264         ret = osl(osl_open_disk_object(src_aft, row, AFTCOL_CHUNKS,
265                 objs + AFTCOL_CHUNKS));
266         if (ret < 0) {
267                 PARA_ERROR_LOG("can not open disk object: %s\n",
268                         para_strerror(-ret));
269                 return ret;
270         }
271         for (i = 0; i < NUM_AFT_COLUMNS; i++) {
272                 if (i == AFTCOL_HASH) /* never assign to this index */
273                         continue;
274                 if (i == AFTCOL_CHUNKS) /* disk storage object handled above */
275                         continue;
276                 /* mapped storage */
277                 ret = osl(osl_get_object(src_aft, row, i, objs + i));
278                 if (ret < 0) {
279                         PARA_ERROR_LOG("get_object (col = %d): %s\n",
280                                 i, para_strerror(-ret));
281                         return ret;
282                 }
283                 if (i == AFTCOL_PATH)
284                         PARA_DEBUG_LOG("copying %s\n", (char *)objs[i].data);
285         }
286         (*n)++;
287         memcpy(hash2, n, sizeof(*n));
288         ret = osl(osl_add_row(dst_aft, objs));
289         if (ret < 0)
290                 PARA_ERROR_LOG("failed to add row: %s\n", para_strerror(-ret));
291         osl_close_disk_object(objs + AFTCOL_CHUNKS);
292         return ret;
293 }
294
295 static int convert_aft(void)
296 {
297         unsigned n;
298         int ret;
299
300         osl_get_num_rows(src_aft, &n);
301         PARA_NOTICE_LOG("converting hash of %u rows to sha256\n", n);
302         n = 0;
303         ret = osl(osl_rbtree_loop(src_aft, AFTCOL_HASH, &n, copy_aft_row));
304         if (ret < 0)
305                 PARA_ERROR_LOG("osl_rbtree_loop failed\n");
306         return ret;
307 }
308
309 static int remove_source_aft(void)
310 {
311         pid_t pid;
312         int fds[3] = {-1, -1, -1}; /* no redirection of stdin/stdout/stderr */
313         int ret, wstatus;
314         char *cmdline = make_message("rm -rf %s", src_aft_dir);
315
316         PARA_NOTICE_LOG("removing %s\n", src_aft_dir);
317         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
318         if (ret < 0) {
319                 PARA_ERROR_LOG("exec failure\n");
320                 goto out;
321         }
322         do {
323                 ret = waitpid(pid, &wstatus, 0);
324         } while (ret < 0 && errno == EINTR);
325         if (ret < 0)
326                 PARA_ERROR_LOG("waitpid failure\n");
327 out:
328         return ret;
329 }
330
331 static int rename_db(void)
332 {
333         PARA_NOTICE_LOG("renaming %s -> %s\n", src_db_dir, dst_db_dir);
334         if (rename(src_db_dir, dst_db_dir) < 0) {
335                 int ret = -ERRNO_TO_PARA_ERROR(errno);
336                 PARA_ERROR_LOG("rename failed\n");
337                 return ret;
338         }
339         return 1;
340 }
341
342 int main(int argc, char *argv[])
343 {
344         struct lls_parse_result *lpr; /* command line */
345         char *errctx;
346         int ret;
347
348         ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
349         if (ret < 0)
350                 goto out;
351         loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
352         version_handle_flag("recv", OPT_GIVEN(VERSION, lpr));
353         handle_help_flag(lpr);
354         set_paths(lpr);
355         check_sanity();
356         open_src_aft();
357         ret = create_and_open_dst_aft();
358         if (ret < 0)
359                 goto close_src_aft;
360         ret = convert_aft();
361         if (ret < 0)
362                 goto close_dst_aft;
363         ret = remove_source_aft();
364         if (ret < 0)
365                 goto close_dst_aft;
366         ret = rename_db();
367 close_dst_aft:
368         osl_close_table(dst_aft, OSL_MARK_CLEAN);
369 close_src_aft:
370         PARA_INFO_LOG("closing audio file tables\n");
371         osl_close_table(src_aft, OSL_MARK_CLEAN);
372 out:
373         if (ret < 0) {
374                 if (errctx)
375                         PARA_ERROR_LOG("%s\n", errctx);
376                 free(errctx);
377                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
378         } else {
379                 PARA_WARNING_LOG("success. Now start para_server and force-add"
380                         " all audio files.\n");
381         }
382         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
383 }