9363a344a0d5ead7de6c78d3dd1a0f00bf2c37db
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 /* dump the preferences */
110 cursor
= trackdb_opencursor(trackdb_prefsdb
, tid
);
111 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
114 if(fputc('P', fp
) < 0
115 || urlencode(s
, k
.data
, k
.size
)
116 || fputc('\n', fp
) < 0
117 || urlencode(s
, d
.data
, d
.size
)
118 || fputc('\n', fp
) < 0)
119 fatal(errno
, "error writing to %s", tag
);
120 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
123 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
126 /* dump the global preferences */
127 cursor
= trackdb_opencursor(trackdb_globaldb
, tid
);
128 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
131 if(fputc('G', fp
) < 0
132 || urlencode(s
, k
.data
, k
.size
)
133 || fputc('\n', fp
) < 0
134 || urlencode(s
, d
.data
, d
.size
)
135 || fputc('\n', fp
) < 0)
136 fatal(errno
, "error writing to %s", tag
);
137 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
140 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
144 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
145 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
148 if(fputc('T', 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
; }
162 cursor
= trackdb_opencursor(trackdb_searchdb
, tid
);
163 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
166 if(fputc('S', fp
) < 0
167 || urlencode(s
, k
.data
, k
.size
)
168 || fputc('\n', fp
) < 0
169 || urlencode(s
, d
.data
, d
.size
)
170 || fputc('\n', fp
) < 0)
171 fatal(errno
, "error writing to %s", tag
);
172 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
175 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; } cursor
= 0;
178 if(fputs("E\n", fp
) < 0) fatal(errno
, "error writing to %s", tag
);
179 if(err
== DB_LOCK_DEADLOCK
) {
180 error(0, "c->c_get: %s", db_strerror(err
));
183 if(err
&& err
!= DB_NOTFOUND
)
184 fatal(0, "cursor->c_get: %s", db_strerror(err
));
185 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
188 trackdb_closecursor(cursor
);
190 info("aborting transaction and retrying dump");
191 trackdb_abort_transaction(tid
);
193 trackdb_commit_transaction(tid
);
194 if(fflush(fp
) < 0) fatal(errno
, "error writing to %s", tag
);
195 /* caller might not be paranoid so we are paranoid on their behalf */
196 if(fsync(fileno(fp
)) < 0) fatal(errno
, "error syncing %s", tag
);
199 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
200 static int remove_aliases(DB_TXN
*tid
, int remove_pathless
) {
207 info("removing aliases");
208 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
209 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
210 DB_FIRST
)) == DB_LOCK_DEADLOCK
) {
211 error(0, "cursor->c_get: %s", db_strerror(err
));
215 data
= kvp_urldecode(d
.data
, d
.size
);
216 alias
= !!kvp_get(data
, "_alias_for");
217 pathless
= !kvp_get(data
, "_path");
218 if(pathless
&& !remove_pathless
)
219 info("no _path for %s", utf82mb(xstrndup(k
.data
, k
.size
)));
220 if(alias
|| (remove_pathless
&& pathless
)) {
221 switch(err
= cursor
->c_del(cursor
, 0)) {
223 case DB_LOCK_DEADLOCK
:
224 error(0, "cursor->c_get: %s", db_strerror(err
));
227 fatal(0, "cursor->c_del: %s", db_strerror(err
));
230 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
), DB_NEXT
);
232 if(err
== DB_LOCK_DEADLOCK
) {
233 error(0, "cursor operation: %s", db_strerror(err
));
236 if(err
!= DB_NOTFOUND
) fatal(0, "cursor->c_get: %s", db_strerror(err
));
239 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
243 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
244 static int truncdb(DB_TXN
*tid
, DB
*db
) {
248 switch(err
= db
->truncate(db
, tid
, &count
, 0)) {
250 case DB_LOCK_DEADLOCK
:
251 error(0, "db->truncate: %s", db_strerror(err
));
254 fatal(0, "db->truncate: %s", db_strerror(err
));
259 /* read a DBT from FP, return 0 on success or -1 on input error */
260 static int undump_dbt(FILE *fp
, const char *tag
, DBT
*dbt
) {
264 if(inputline(tag
, fp
, &s
, '\n')) return -1;
266 if(urldecode(sink_dynstr(&d
), s
, strlen(s
)))
267 fatal(0, "invalid URL-encoded data in %s", tag
);
273 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
274 static int undump_from_fp(DB_TXN
*tid
, FILE *fp
, const char *tag
) {
277 const char *which_name
;
281 if(fseek(fp
, 0, SEEK_SET
) < 0)
282 fatal(errno
, "error calling fseek on %s", tag
);
283 if((err
= truncdb(tid
, trackdb_prefsdb
))) return err
;
284 if((err
= truncdb(tid
, trackdb_searchdb
))) return err
;
286 while(!ferror(fp
) && !feof(fp
)) {
291 fatal(0, "unknown version '%c'", c
);
298 which_db
= trackdb_prefsdb
;
299 which_name
= "prefs.db";
301 which_db
= trackdb_globaldb
;
302 which_name
= "global.db";
304 if(undump_dbt(fp
, tag
, prepare_data(&k
))
305 || undump_dbt(fp
, tag
, prepare_data(&d
)))
307 switch(err
= trackdb_prefsdb
->put(which_db
, tid
, &k
, &d
, 0)) {
310 case DB_LOCK_DEADLOCK
:
311 error(0, "error updating %s: %s", which_name
, db_strerror(err
));
314 fatal(0, "error updating %s: %s", which_name
, db_strerror(err
));
319 if(undump_dbt(fp
, tag
, prepare_data(&k
))
320 || undump_dbt(fp
, tag
, prepare_data(&d
)))
322 /* We don't restore the tracks.db or search.db entries, instead
323 * we recompute them */
331 fatal(errno
, "error reading %s", tag
);
333 fatal(0, "unexpected EOF reading %s", tag
);
337 /* recompute aliases and search database from prefs, return 0 or
338 * DB_LOCK_DEADLOCK */
339 static int recompute_aliases(DB_TXN
*tid
) {
344 const char *path
, *track
;
346 info("recomputing aliases");
347 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
348 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
349 DB_FIRST
)) == DB_LOCK_DEADLOCK
) goto done
;
351 data
= kvp_urldecode(d
.data
, d
.size
);
352 track
= xstrndup(k
.data
, k
.size
);
353 if(!kvp_get(data
, "_alias_for")) {
354 if(!(path
= kvp_get(data
, "_path")))
355 error(0, "%s is not an alias but has no path", utf82mb(track
));
357 if((err
= trackdb_notice_tid(track
, path
, tid
)) == DB_LOCK_DEADLOCK
)
360 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
369 case DB_LOCK_DEADLOCK
:
372 fatal(0, "cursor->c_get: %s", db_strerror(err
));
375 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
379 /* restore prefs from FP */
380 static void do_undump(FILE *fp
, const char *tag
, int remove_pathless
) {
384 tid
= trackdb_begin_transaction();
385 if(remove_aliases(tid
, remove_pathless
)
386 || undump_from_fp(tid
, fp
, tag
)
387 || recompute_aliases(tid
)) goto fail
;
390 info("aborting transaction and retrying undump");
391 trackdb_abort_transaction(tid
);
393 info("committing undump");
394 trackdb_commit_transaction(tid
);
397 /* just recompute alisaes */
398 static void do_recompute(int remove_pathless
) {
402 tid
= trackdb_begin_transaction();
403 if(remove_aliases(tid
, remove_pathless
)
404 || recompute_aliases(tid
)) goto fail
;
407 info("aborting transaction and retrying recomputation");
408 trackdb_abort_transaction(tid
);
410 info("committing recomputed aliases");
411 trackdb_commit_transaction(tid
);
414 int main(int argc
, char **argv
) {
415 int n
, dump
= 0, undump
= 0, recover
= TRACKDB_NO_RECOVER
, recompute
= 0;
416 int tracksdb
= 0, searchdb
= 0, remove_pathless
= 0;
422 while((n
= getopt_long(argc
, argv
, "hVc:dDutsrRaP", options
, 0)) >= 0) {
426 case 'c': configfile
= optarg
; break;
427 case 'd': dump
= 1; break;
428 case 'u': undump
= 1; break;
429 case 'D': debugging
= 1; break;
430 case 't': tracksdb
= 1; break;
431 case 's': searchdb
= 1; break;
432 case 'r': recover
= TRACKDB_NORMAL_RECOVER
;
433 case 'R': recover
= TRACKDB_FATAL_RECOVER
;
434 case 'a': recompute
= 1; break;
435 case 'P': remove_pathless
= 1; break;
436 default: fatal(0, "invalid option");
439 if(dump
+ undump
+ recompute
!= 1)
440 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
441 if((undump
|| recompute
) && (tracksdb
|| searchdb
))
442 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
445 fatal(0, "--recompute-aliases does not take a filename");
449 fatal(0, "missing dump file name");
450 if(optind
+ 1 < argc
)
451 fatal(0, "specify only a dump file name");
454 if(config_read(0)) fatal(0, "cannot read configuration");
455 trackdb_init(recover
);
456 trackdb_open(TRACKDB_NO_UPGRADE
);
458 /* we write to a temporary file and rename into place */
459 byte_xasprintf(&tmp
, "%s.%lx.tmp", path
, (unsigned long)getpid());
460 if(!(fp
= fopen(tmp
, "w"))) fatal(errno
, "error opening %s", tmp
);
461 do_dump(fp
, tmp
, tracksdb
, searchdb
);
462 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
463 if(rename(tmp
, path
) < 0)
464 fatal(errno
, "error renaming %s to %s", tmp
, path
);
466 /* the databases or logfiles might end up with wrong permissions
467 * if new ones are created */
468 if(getuid() == 0) info("you might need to chown database files");
469 if(!(fp
= fopen(path
, "r"))) fatal(errno
, "error opening %s", path
);
470 do_undump(fp
, path
, remove_pathless
);
472 } else if(recompute
) {
473 do_recompute(remove_pathless
);