2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007 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
34 #include "configuration.h"
44 #include "inputline.h"
47 #include "trackdb-int.h"
50 static const struct option options
[] = {
51 { "help", no_argument
, 0, 'h' },
52 { "version", no_argument
, 0, 'V' },
53 { "config", required_argument
, 0, 'c' },
54 { "dump", no_argument
, 0, 'd' },
55 { "undump", no_argument
, 0, 'u' },
56 { "debug", no_argument
, 0, 'D' },
57 { "recover", no_argument
, 0, 'r' },
58 { "recover-fatal", no_argument
, 0, 'R' },
59 { "trackdb", no_argument
, 0, 't' },
60 { "searchdb", no_argument
, 0, 's' },
61 { "recompute-aliases", no_argument
, 0, 'a' },
62 { "remove-pathless", no_argument
, 0, 'P' },
66 /* display usage message and terminate */
67 static void help(void) {
69 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
70 " disorder-dump [OPTIONS] --recompute-aliases\n"
72 " --help, -h Display usage message\n"
73 " --version, -V Display version number\n"
74 " --config PATH, -c PATH Set configuration file\n"
75 " --dump, -d Dump state to PATH\n"
76 " --undump, -u Restore state from PATH\n"
77 " --recover, -r Run database recovery\n"
78 " --recompute-aliases, -a Recompute aliases\n"
79 " --remove-pathless, -P Remove pathless tracks\n"
80 " --debug Debug mode\n");
85 /* display version number and terminate */
86 static void version(void) {
87 xprintf("%s", disorder_version_string
);
92 /* dump prefs to FP, return nonzero on error */
93 static void do_dump(FILE *fp
, const char *tag
,
94 int tracksdb
, int searchdb
) {
97 struct sink
*s
= sink_stdio(tag
, fp
);
102 tid
= trackdb_begin_transaction();
103 if(fseek(fp
, 0, SEEK_SET
) < 0)
104 fatal(errno
, "error calling fseek");
106 fatal(errno
, "error calling fflush");
107 if(ftruncate(fileno(fp
), 0) < 0)
108 fatal(errno
, "error calling ftruncate");
109 if(fprintf(fp
, "V%c\n", (tracksdb
|| searchdb
) ?
'1' : '0') < 0)
110 fatal(errno
, "error writing to %s", tag
);
111 /* dump the preferences */
112 cursor
= trackdb_opencursor(trackdb_prefsdb
, tid
);
113 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
116 if(fputc('P', fp
) < 0
117 || urlencode(s
, k
.data
, k
.size
)
118 || fputc('\n', fp
) < 0
119 || urlencode(s
, d
.data
, d
.size
)
120 || fputc('\n', fp
) < 0)
121 fatal(errno
, "error writing to %s", tag
);
122 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
125 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
128 /* dump the global preferences */
129 cursor
= trackdb_opencursor(trackdb_globaldb
, tid
);
130 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
133 if(fputc('G', fp
) < 0
134 || urlencode(s
, k
.data
, k
.size
)
135 || fputc('\n', fp
) < 0
136 || urlencode(s
, d
.data
, d
.size
)
137 || fputc('\n', fp
) < 0)
138 fatal(errno
, "error writing to %s", tag
);
139 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
142 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
146 cursor
= trackdb_opencursor(trackdb_usersdb
, tid
);
147 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
150 if(fputc('U', fp
) < 0
151 || urlencode(s
, k
.data
, k
.size
)
152 || fputc('\n', fp
) < 0
153 || urlencode(s
, d
.data
, d
.size
)
154 || fputc('\n', fp
) < 0)
155 fatal(errno
, "error writing to %s", tag
);
156 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
159 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
163 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
164 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
167 if(fputc('T', fp
) < 0
168 || urlencode(s
, k
.data
, k
.size
)
169 || fputc('\n', fp
) < 0
170 || urlencode(s
, d
.data
, d
.size
)
171 || fputc('\n', fp
) < 0)
172 fatal(errno
, "error writing to %s", tag
);
173 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
176 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
181 cursor
= trackdb_opencursor(trackdb_searchdb
, tid
);
182 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
185 if(fputc('S', fp
) < 0
186 || urlencode(s
, k
.data
, k
.size
)
187 || fputc('\n', fp
) < 0
188 || urlencode(s
, d
.data
, d
.size
)
189 || fputc('\n', fp
) < 0)
190 fatal(errno
, "error writing to %s", tag
);
191 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
194 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; } cursor
= 0;
197 if(fputs("E\n", fp
) < 0) fatal(errno
, "error writing to %s", tag
);
198 if(err
== DB_LOCK_DEADLOCK
) {
199 error(0, "c->c_get: %s", db_strerror(err
));
202 if(err
&& err
!= DB_NOTFOUND
)
203 fatal(0, "cursor->c_get: %s", db_strerror(err
));
204 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
207 trackdb_closecursor(cursor
);
209 info("aborting transaction and retrying dump");
210 trackdb_abort_transaction(tid
);
212 trackdb_commit_transaction(tid
);
213 if(fflush(fp
) < 0) fatal(errno
, "error writing to %s", tag
);
214 /* caller might not be paranoid so we are paranoid on their behalf */
215 if(fsync(fileno(fp
)) < 0) fatal(errno
, "error syncing %s", tag
);
218 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
219 static int remove_aliases(DB_TXN
*tid
, int remove_pathless
) {
226 info("removing aliases");
227 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
228 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
229 DB_FIRST
)) == DB_LOCK_DEADLOCK
) {
230 error(0, "cursor->c_get: %s", db_strerror(err
));
234 data
= kvp_urldecode(d
.data
, d
.size
);
235 alias
= !!kvp_get(data
, "_alias_for");
236 pathless
= !kvp_get(data
, "_path");
237 if(pathless
&& !remove_pathless
)
238 info("no _path for %s", utf82mb(xstrndup(k
.data
, k
.size
)));
239 if(alias
|| (remove_pathless
&& pathless
)) {
240 switch(err
= cursor
->c_del(cursor
, 0)) {
242 case DB_LOCK_DEADLOCK
:
243 error(0, "cursor->c_get: %s", db_strerror(err
));
246 fatal(0, "cursor->c_del: %s", db_strerror(err
));
249 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
), DB_NEXT
);
251 if(err
== DB_LOCK_DEADLOCK
) {
252 error(0, "cursor operation: %s", db_strerror(err
));
255 if(err
!= DB_NOTFOUND
) fatal(0, "cursor->c_get: %s", db_strerror(err
));
258 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
262 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
263 static int truncdb(DB_TXN
*tid
, DB
*db
) {
267 switch(err
= db
->truncate(db
, tid
, &count
, 0)) {
269 case DB_LOCK_DEADLOCK
:
270 error(0, "db->truncate: %s", db_strerror(err
));
273 fatal(0, "db->truncate: %s", db_strerror(err
));
278 /* read a DBT from FP, return 0 on success or -1 on input error */
279 static int undump_dbt(FILE *fp
, const char *tag
, DBT
*dbt
) {
283 if(inputline(tag
, fp
, &s
, '\n')) return -1;
285 if(urldecode(sink_dynstr(&d
), s
, strlen(s
)))
286 fatal(0, "invalid URL-encoded data in %s", tag
);
292 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
293 static int undump_from_fp(DB_TXN
*tid
, FILE *fp
, const char *tag
) {
296 const char *which_name
;
300 if(fseek(fp
, 0, SEEK_SET
) < 0)
301 fatal(errno
, "error calling fseek on %s", tag
);
302 if((err
= truncdb(tid
, trackdb_prefsdb
))) return err
;
303 if((err
= truncdb(tid
, trackdb_globaldb
))) return err
;
304 if((err
= truncdb(tid
, trackdb_searchdb
))) return err
;
305 if((err
= truncdb(tid
, trackdb_tagsdb
))) return err
;
306 if((err
= truncdb(tid
, trackdb_usersdb
))) return err
;
308 while(!ferror(fp
) && !feof(fp
)) {
313 fatal(0, "unknown version '%c'", c
);
322 which_db
= trackdb_prefsdb
;
323 which_name
= "prefs.db";
326 which_db
= trackdb_globaldb
;
327 which_name
= "global.db";
330 which_db
= trackdb_usersdb
;
331 which_name
= "users.db";
336 if(undump_dbt(fp
, tag
, prepare_data(&k
))
337 || undump_dbt(fp
, tag
, prepare_data(&d
)))
339 switch(err
= trackdb_prefsdb
->put(which_db
, tid
, &k
, &d
, 0)) {
342 case DB_LOCK_DEADLOCK
:
343 error(0, "error updating %s: %s", which_name
, db_strerror(err
));
346 fatal(0, "error updating %s: %s", which_name
, db_strerror(err
));
351 if(undump_dbt(fp
, tag
, prepare_data(&k
))
352 || undump_dbt(fp
, tag
, prepare_data(&d
)))
354 /* We don't restore the tracks.db or search.db entries, instead
355 * we recompute them */
363 fatal(errno
, "error reading %s", tag
);
365 fatal(0, "unexpected EOF reading %s", tag
);
369 /* recompute aliases and search database from prefs, return 0 or
370 * DB_LOCK_DEADLOCK */
371 static int recompute_aliases(DB_TXN
*tid
) {
376 const char *path
, *track
;
378 info("recomputing aliases");
379 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
380 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
381 DB_FIRST
)) == DB_LOCK_DEADLOCK
) goto done
;
383 data
= kvp_urldecode(d
.data
, d
.size
);
384 track
= xstrndup(k
.data
, k
.size
);
385 if(!kvp_get(data
, "_alias_for")) {
386 if(!(path
= kvp_get(data
, "_path")))
387 error(0, "%s is not an alias but has no path", utf82mb(track
));
389 if((err
= trackdb_notice_tid(track
, path
, tid
)) == DB_LOCK_DEADLOCK
)
392 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
401 case DB_LOCK_DEADLOCK
:
404 fatal(0, "cursor->c_get: %s", db_strerror(err
));
407 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
411 /* restore prefs from FP */
412 static void do_undump(FILE *fp
, const char *tag
, int remove_pathless
) {
416 tid
= trackdb_begin_transaction();
417 if(remove_aliases(tid
, remove_pathless
)
418 || undump_from_fp(tid
, fp
, tag
)
419 || recompute_aliases(tid
)) goto fail
;
422 info("aborting transaction and retrying undump");
423 trackdb_abort_transaction(tid
);
425 info("committing undump");
426 trackdb_commit_transaction(tid
);
429 /* just recompute alisaes */
430 static void do_recompute(int remove_pathless
) {
434 tid
= trackdb_begin_transaction();
435 if(remove_aliases(tid
, remove_pathless
)
436 || recompute_aliases(tid
)) goto fail
;
439 info("aborting transaction and retrying recomputation");
440 trackdb_abort_transaction(tid
);
442 info("committing recomputed aliases");
443 trackdb_commit_transaction(tid
);
446 int main(int argc
, char **argv
) {
447 int n
, dump
= 0, undump
= 0, recover
= TRACKDB_NO_RECOVER
, recompute
= 0;
448 int tracksdb
= 0, searchdb
= 0, remove_pathless
= 0, fd
;
454 while((n
= getopt_long(argc
, argv
, "hVc:dDutsrRaP", options
, 0)) >= 0) {
458 case 'c': configfile
= optarg
; break;
459 case 'd': dump
= 1; break;
460 case 'u': undump
= 1; break;
461 case 'D': debugging
= 1; break;
462 case 't': tracksdb
= 1; break;
463 case 's': searchdb
= 1; break;
464 case 'r': recover
= TRACKDB_NORMAL_RECOVER
;
465 case 'R': recover
= TRACKDB_FATAL_RECOVER
;
466 case 'a': recompute
= 1; break;
467 case 'P': remove_pathless
= 1; break;
468 default: fatal(0, "invalid option");
471 if(dump
+ undump
+ recompute
!= 1)
472 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
473 if((undump
|| recompute
) && (tracksdb
|| searchdb
))
474 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
477 fatal(0, "--recompute-aliases does not take a filename");
481 fatal(0, "missing dump file name");
482 if(optind
+ 1 < argc
)
483 fatal(0, "specify only a dump file name");
486 if(config_read(0)) fatal(0, "cannot read configuration");
487 trackdb_init(recover
|TRACKDB_MAY_CREATE
);
488 trackdb_open(TRACKDB_NO_UPGRADE
);
490 /* We write to a temporary file and rename into place. We make
491 * sure the permissions are tight from the start. */
492 byte_xasprintf(&tmp
, "%s.%lx.tmp", path
, (unsigned long)getpid());
493 if((fd
= open(tmp
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) < 0)
494 fatal(errno
, "error opening %s", tmp
);
495 if(!(fp
= fdopen(fd
, "w")))
496 fatal(errno
, "fdopen on %s", tmp
);
497 do_dump(fp
, tmp
, tracksdb
, searchdb
);
498 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
499 if(rename(tmp
, path
) < 0)
500 fatal(errno
, "error renaming %s to %s", tmp
, path
);
502 /* the databases or logfiles might end up with wrong permissions
503 * if new ones are created */
504 if(getuid() == 0) info("you might need to chown database files");
505 if(!(fp
= fopen(path
, "r"))) fatal(errno
, "error opening %s", path
);
506 do_undump(fp
, path
, remove_pathless
);
508 } else if(recompute
) {
509 do_recompute(remove_pathless
);