1 /* Copyright (C) 2020 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file upgrade_db.c Prepare the paraslash database for paraslash-0.7. */
9 #include "upgrade_db.lsg.h"
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)))
25 INIT_STDERR_LOGGING(loglevel);
27 /** Array of error strings. */
30 static void handle_help_flag(struct lls_parse_result *lpr)
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);
45 static struct stat *path_exists(const char *path)
47 static struct stat sb;
49 if (stat(path, &sb) < 0)
54 static bool is_dir(const char *path)
56 struct stat *sb = path_exists(path);
59 return (sb->st_mode & S_IFMT) == S_IFDIR;
62 __noreturn static void die(const char *msg)
64 PARA_EMERG_LOG("%s\n", msg);
68 static int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
70 const char *str1 = obj1->data;
71 const char *str2 = obj2->data;
72 return strncmp(str1, str2, PARA_MIN(obj1->size, obj2->size));
75 static char *src_db_dir, *dst_db_dir, *src_aft_dir, *dst_aft_dir;
77 static void set_paths(const struct lls_parse_result *lpr)
79 char *home = para_homedir();
81 if (OPT_GIVEN(SRC_DATABASE_DIR, lpr))
82 src_db_dir = para_strdup(OPT_STRING_VAL(SRC_DATABASE_DIR,
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,
91 dst_db_dir = make_message(
92 "%s/.paraslash/afs_database-0.7", 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);
100 static void check_sanity(void)
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");
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. */
117 /** The full path in the filesystem. */
119 /** The audio file selector info. */
121 /** The audio format handler info. */
123 /** The chunk table info and the chunk table of the audio file. */
125 /** The number of columns of this table. */
131 static int src_aft_hash_compare(const struct osl_object *obj1,
132 const struct osl_object *obj2)
134 return hash_compare((unsigned char *)obj1->data,
135 (unsigned char *)obj2->data);
138 static struct osl_column_description src_aft_cols[] = {
140 .storage_type = OSL_MAPPED_STORAGE,
141 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
143 .compare_function = src_aft_hash_compare,
144 .data_size = HASH_SIZE
147 .storage_type = OSL_MAPPED_STORAGE,
148 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
150 .compare_function = string_compare,
153 .storage_type = OSL_MAPPED_STORAGE,
154 .storage_flags = OSL_FIXED_SIZE,
156 .data_size = AFSI_SIZE
159 .storage_type = OSL_MAPPED_STORAGE,
163 .storage_type = OSL_DISK_STORAGE,
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
175 static struct osl_table *src_aft, *dst_aft;
177 static void open_src_aft(void)
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));
185 PARA_EMERG_LOG("can not open source audio file table: %s\n",
186 para_strerror(-ret));
189 PARA_INFO_LOG("successfully opened source audio file table\n");
192 static int dst_aft_hash_compare(const struct osl_object *obj1,
193 const struct osl_object *obj2)
195 return hash2_compare((unsigned char *)obj1->data,
196 (unsigned char *)obj2->data);
199 /* identical to src_aft_cols except the comparator and the hash size. */
200 static struct osl_column_description dst_aft_cols[] = {
202 .storage_type = OSL_MAPPED_STORAGE,
203 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
205 .compare_function = dst_aft_hash_compare,
206 .data_size = HASH2_SIZE
209 .storage_type = OSL_MAPPED_STORAGE,
210 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
212 .compare_function = string_compare,
215 .storage_type = OSL_MAPPED_STORAGE,
216 .storage_flags = OSL_FIXED_SIZE,
218 .data_size = AFSI_SIZE
221 .storage_type = OSL_MAPPED_STORAGE,
225 .storage_type = OSL_DISK_STORAGE,
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
237 static int create_and_open_dst_aft(void)
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));
245 PARA_EMERG_LOG("could not create destination audio file table\n");
248 ret = osl(osl_open_table(&dst_aft_desc, &dst_aft));
250 PARA_EMERG_LOG("could not open destination audio file table: %s\n",
251 para_strerror(-ret));
254 PARA_INFO_LOG("successfully opened destination audio file table\n");
258 static int copy_aft_row(struct osl_row *row, void *data)
262 unsigned char hash2[HASH2_SIZE] = "\0";
263 struct osl_object objs[NUM_AFT_COLUMNS] = {
264 [AFTCOL_HASH] = {.data = hash2, .size = HASH2_SIZE}
267 ret = osl(osl_open_disk_object(src_aft, row, AFTCOL_CHUNKS,
268 objs + AFTCOL_CHUNKS));
270 PARA_ERROR_LOG("can not open disk object: %s\n",
271 para_strerror(-ret));
274 for (i = 0; i < NUM_AFT_COLUMNS; i++) {
275 if (i == AFTCOL_HASH) /* never assign to this index */
277 if (i == AFTCOL_CHUNKS) /* disk storage object handled above */
280 ret = osl(osl_get_object(src_aft, row, i, objs + i));
282 PARA_ERROR_LOG("get_object (col = %d): %s\n",
283 i, para_strerror(-ret));
286 if (i == AFTCOL_PATH)
287 PARA_DEBUG_LOG("copying %s\n", (char *)objs[i].data);
290 memcpy(hash2, n, sizeof(*n));
291 ret = osl(osl_add_row(dst_aft, objs));
293 PARA_ERROR_LOG("failed to add row: %s\n", para_strerror(-ret));
294 osl_close_disk_object(objs + AFTCOL_CHUNKS);
298 static int convert_aft(void)
303 osl_get_num_rows(src_aft, &n);
304 PARA_NOTICE_LOG("converting hash of %u rows to sha256\n", n);
306 ret = osl(osl_rbtree_loop(src_aft, AFTCOL_HASH, &n, copy_aft_row));
308 PARA_ERROR_LOG("osl_rbtree_loop failed\n");
312 static int remove_source_aft(void)
315 int fds[3] = {-1, -1, -1}; /* no redirection of stdin/stdout/stderr */
317 char *cmdline = make_message("rm -rf %s", src_aft_dir);
319 PARA_NOTICE_LOG("removing %s\n", src_aft_dir);
320 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
322 PARA_ERROR_LOG("exec failure\n");
326 ret = waitpid(pid, &wstatus, 0);
327 } while (ret < 0 && errno == EINTR);
329 PARA_ERROR_LOG("waitpid failure\n");
334 static int rename_db(void)
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");
345 int main(int argc, char *argv[])
347 struct lls_parse_result *lpr; /* command line */
351 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
354 loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
355 version_handle_flag("recv", OPT_GIVEN(VERSION, lpr));
356 handle_help_flag(lpr);
360 ret = create_and_open_dst_aft();
366 ret = remove_source_aft();
371 osl_close_table(dst_aft, OSL_MARK_CLEAN);
373 PARA_INFO_LOG("closing audio file tables\n");
374 osl_close_table(src_aft, OSL_MARK_CLEAN);
378 PARA_ERROR_LOG("%s\n", errctx);
380 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
382 PARA_WARNING_LOG("success. Now start para_server and force-add"
383 " all audio files.\n");
385 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;