2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include "configuration.h"
43 #include "inputline.h"
45 #include "trackdb-int.h"
48 static const struct option options[] = {
49 { "help", no_argument, 0, 'h' },
50 { "version", no_argument, 0, 'V' },
51 { "config", required_argument, 0, 'c' },
52 { "dump", no_argument, 0, 'd' },
53 { "undump", no_argument, 0, 'u' },
54 { "debug", no_argument, 0, 'D' },
55 { "recover", no_argument, 0, 'r' },
56 { "recover-fatal", no_argument, 0, 'R' },
57 { "trackdb", no_argument, 0, 't' },
58 { "searchdb", no_argument, 0, 's' },
59 { "recompute-aliases", no_argument, 0, 'a' },
60 { "remove-pathless", no_argument, 0, 'P' },
64 /* display usage message and terminate */
65 static void help(void) {
67 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
68 " disorder-dump [OPTIONS] --recompute-aliases\n"
70 " --help, -h Display usage message\n"
71 " --version, -V Display version number\n"
72 " --config PATH, -c PATH Set configuration file\n"
73 " --dump, -d Dump state to PATH\n"
74 " --undump, -u Restore state from PATH\n"
75 " --recover, -r Run database recovery\n"
76 " --recompute-aliases, -a Recompute aliases\n"
77 " --remove-pathless, -P Remove pathless tracks\n"
78 " --debug Debug mode\n");
83 /* display version number and terminate */
84 static void version(void) {
85 xprintf("disorder-dump version %s\n", disorder_version_string);
90 /* dump prefs to FP, return nonzero on error */
91 static void do_dump(FILE *fp, const char *tag,
92 int tracksdb, int searchdb) {
95 struct sink *s = sink_stdio(tag, fp);
100 tid = trackdb_begin_transaction();
101 if(fseek(fp, 0, SEEK_SET) < 0)
102 fatal(errno, "error calling fseek");
104 fatal(errno, "error calling fflush");
105 if(ftruncate(fileno(fp), 0) < 0)
106 fatal(errno, "error calling ftruncate");
107 if(fprintf(fp, "V%c\n", (tracksdb || searchdb) ? '1' : '0') < 0)
108 fatal(errno, "error writing to %s", tag);
109 cursor = trackdb_opencursor(trackdb_prefsdb, tid);
110 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
113 if(fputc('P', fp) < 0
114 || urlencode(s, k.data, k.size)
115 || fputc('\n', fp) < 0
116 || urlencode(s, d.data, d.size)
117 || fputc('\n', fp) < 0)
118 fatal(errno, "error writing to %s", tag);
119 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
122 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
126 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
127 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
130 if(fputc('T', fp) < 0
131 || urlencode(s, k.data, k.size)
132 || fputc('\n', fp) < 0
133 || urlencode(s, d.data, d.size)
134 || fputc('\n', fp) < 0)
135 fatal(errno, "error writing to %s", tag);
136 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
139 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
144 cursor = trackdb_opencursor(trackdb_searchdb, tid);
145 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
148 if(fputc('S', fp) < 0
149 || urlencode(s, k.data, k.size)
150 || fputc('\n', fp) < 0
151 || urlencode(s, d.data, d.size)
152 || fputc('\n', fp) < 0)
153 fatal(errno, "error writing to %s", tag);
154 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
157 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } cursor = 0;
160 if(fputs("E\n", fp) < 0) fatal(errno, "error writing to %s", tag);
161 if(err == DB_LOCK_DEADLOCK) {
162 error(0, "c->c_get: %s", db_strerror(err));
165 if(err && err != DB_NOTFOUND)
166 fatal(0, "cursor->c_get: %s", db_strerror(err));
167 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
170 trackdb_closecursor(cursor);
172 info("aborting transaction and retrying dump");
173 trackdb_abort_transaction(tid);
175 trackdb_commit_transaction(tid);
176 if(fflush(fp) < 0) fatal(errno, "error writing to %s", tag);
177 /* caller might not be paranoid so we are paranoid on their behalf */
178 if(fsync(fileno(fp)) < 0) fatal(errno, "error syncing %s", tag);
181 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
182 static int remove_aliases(DB_TXN *tid, int remove_pathless) {
189 info("removing aliases");
190 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
191 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
192 DB_FIRST)) == DB_LOCK_DEADLOCK) {
193 error(0, "cursor->c_get: %s", db_strerror(err));
197 data = kvp_urldecode(d.data, d.size);
198 alias = !!kvp_get(data, "_alias_for");
199 pathless = !kvp_get(data, "_path");
200 if(pathless && !remove_pathless)
201 info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
202 if(alias || (remove_pathless && pathless)) {
203 switch(err = cursor->c_del(cursor, 0)) {
205 case DB_LOCK_DEADLOCK:
206 error(0, "cursor->c_get: %s", db_strerror(err));
209 fatal(0, "cursor->c_del: %s", db_strerror(err));
212 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
214 if(err == DB_LOCK_DEADLOCK) {
215 error(0, "cursor operation: %s", db_strerror(err));
218 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
221 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
225 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
226 static int truncdb(DB_TXN *tid, DB *db) {
230 switch(err = db->truncate(db, tid, &count, 0)) {
232 case DB_LOCK_DEADLOCK:
233 error(0, "db->truncate: %s", db_strerror(err));
236 fatal(0, "db->truncate: %s", db_strerror(err));
241 /* read a DBT from FP, return 0 on success or -1 on input error */
242 static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
246 if(inputline(tag, fp, &s, '\n')) return -1;
248 if(urldecode(sink_dynstr(&d), s, strlen(s)))
249 fatal(0, "invalid URL-encoded data in %s", tag);
255 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
256 static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
261 if(fseek(fp, 0, SEEK_SET) < 0)
262 fatal(errno, "error calling fseek on %s", tag);
263 if((err = truncdb(tid, trackdb_prefsdb))) return err;
264 if((err = truncdb(tid, trackdb_searchdb))) return err;
266 while(!ferror(fp) && !feof(fp)) {
271 fatal(0, "unknown version '%c'", c);
276 if(undump_dbt(fp, tag, prepare_data(&k))
277 || undump_dbt(fp, tag, prepare_data(&d)))
279 switch(err = trackdb_prefsdb->put(trackdb_prefsdb, tid, &k, &d, 0)) {
282 case DB_LOCK_DEADLOCK:
283 error(0, "error updating prefs.db: %s", db_strerror(err));
286 fatal(0, "error updating prefs.db: %s", db_strerror(err));
291 if(undump_dbt(fp, tag, prepare_data(&k))
292 || undump_dbt(fp, tag, prepare_data(&d)))
294 /* We don't restore the tracks.db or search.db entries, instead
295 * we recompute them */
303 fatal(errno, "error reading %s", tag);
305 fatal(0, "unexpected EOF reading %s", tag);
309 /* recompute aliases and search database from prefs, return 0 or
310 * DB_LOCK_DEADLOCK */
311 static int recompute_aliases(DB_TXN *tid) {
316 const char *path, *track;
318 info("recomputing aliases");
319 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
320 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
321 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
323 data = kvp_urldecode(d.data, d.size);
324 track = xstrndup(k.data, k.size);
325 if(!kvp_get(data, "_alias_for")) {
326 if(!(path = kvp_get(data, "_path")))
327 error(0, "%s is not an alias but has no path", utf82mb(track));
329 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
332 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
341 case DB_LOCK_DEADLOCK:
344 fatal(0, "cursor->c_get: %s", db_strerror(err));
347 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
351 /* restore prefs from FP */
352 static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
356 tid = trackdb_begin_transaction();
357 if(remove_aliases(tid, remove_pathless)
358 || undump_from_fp(tid, fp, tag)
359 || recompute_aliases(tid)) goto fail;
362 info("aborting transaction and retrying undump");
363 trackdb_abort_transaction(tid);
365 info("committing undump");
366 trackdb_commit_transaction(tid);
369 /* just recompute alisaes */
370 static void do_recompute(int remove_pathless) {
374 tid = trackdb_begin_transaction();
375 if(remove_aliases(tid, remove_pathless)
376 || recompute_aliases(tid)) goto fail;
379 info("aborting transaction and retrying recomputation");
380 trackdb_abort_transaction(tid);
382 info("committing recomputed aliases");
383 trackdb_commit_transaction(tid);
386 int main(int argc, char **argv) {
387 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
388 int tracksdb = 0, searchdb = 0, remove_pathless = 0;
394 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
398 case 'c': configfile = optarg; break;
399 case 'd': dump = 1; break;
400 case 'u': undump = 1; break;
401 case 'D': debugging = 1; break;
402 case 't': tracksdb = 1; break;
403 case 's': searchdb = 1; break;
404 case 'r': recover = TRACKDB_NORMAL_RECOVER;
405 case 'R': recover = TRACKDB_FATAL_RECOVER;
406 case 'a': recompute = 1; break;
407 case 'P': remove_pathless = 1; break;
408 default: fatal(0, "invalid option");
411 if(dump + undump + recompute != 1)
412 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
413 if((undump || recompute) && (tracksdb || searchdb))
414 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
417 fatal(0, "--recompute-aliases does not take a filename");
421 fatal(0, "missing dump file name");
422 if(optind + 1 < argc)
423 fatal(0, "specify only a dump file name");
426 if(config_read(0)) fatal(0, "cannot read configuration");
427 trackdb_init(recover);
430 /* we write to a temporary file and rename into place */
431 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
432 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
433 do_dump(fp, tmp, tracksdb, searchdb);
434 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
435 if(rename(tmp, path) < 0)
436 fatal(errno, "error renaming %s to %s", tmp, path);
438 /* the databases or logfiles might end up with wrong permissions
439 * if new ones are created */
440 if(getuid() == 0) info("you might need to chown database files");
441 if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path);
442 do_undump(fp, path, remove_pathless);
444 } else if(recompute) {
445 do_recompute(remove_pathless);