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"
51 static const struct option options
[] = {
52 { "help", no_argument
, 0, 'h' },
53 { "version", no_argument
, 0, 'V' },
54 { "config", required_argument
, 0, 'c' },
55 { "dump", no_argument
, 0, 'd' },
56 { "undump", no_argument
, 0, 'u' },
57 { "debug", no_argument
, 0, 'D' },
58 { "recover", no_argument
, 0, 'r' },
59 { "recover-fatal", no_argument
, 0, 'R' },
60 { "trackdb", no_argument
, 0, 't' },
61 { "searchdb", no_argument
, 0, 's' },
62 { "recompute-aliases", no_argument
, 0, 'a' },
63 { "remove-pathless", no_argument
, 0, 'P' },
67 /* display usage message and terminate */
68 static void help(void) {
70 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
71 " disorder-dump [OPTIONS] --recompute-aliases\n"
73 " --help, -h Display usage message\n"
74 " --version, -V Display version number\n"
75 " --config PATH, -c PATH Set configuration file\n"
76 " --dump, -d Dump state to PATH\n"
77 " --undump, -u Restore state from PATH\n"
78 " --recover, -r Run database recovery\n"
79 " --recompute-aliases, -a Recompute aliases\n"
80 " --remove-pathless, -P Remove pathless tracks\n"
81 " --debug Debug mode\n");
86 /* dump prefs to FP, return nonzero on error */
87 static void do_dump(FILE *fp
, const char *tag
,
88 int tracksdb
, int searchdb
) {
91 struct sink
*s
= sink_stdio(tag
, fp
);
96 tid
= trackdb_begin_transaction();
97 if(fseek(fp
, 0, SEEK_SET
) < 0)
98 fatal(errno
, "error calling fseek");
100 fatal(errno
, "error calling fflush");
101 if(ftruncate(fileno(fp
), 0) < 0)
102 fatal(errno
, "error calling ftruncate");
103 if(fprintf(fp
, "V%c\n", (tracksdb
|| searchdb
) ?
'1' : '0') < 0)
104 fatal(errno
, "error writing to %s", tag
);
105 /* dump the preferences */
106 cursor
= trackdb_opencursor(trackdb_prefsdb
, tid
);
107 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
110 if(fputc('P', fp
) < 0
111 || urlencode(s
, k
.data
, k
.size
)
112 || fputc('\n', fp
) < 0
113 || urlencode(s
, d
.data
, d
.size
)
114 || fputc('\n', fp
) < 0)
115 fatal(errno
, "error writing to %s", tag
);
116 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
119 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
122 /* dump the global preferences */
123 cursor
= trackdb_opencursor(trackdb_globaldb
, tid
);
124 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
127 if(fputc('G', fp
) < 0
128 || urlencode(s
, k
.data
, k
.size
)
129 || fputc('\n', fp
) < 0
130 || urlencode(s
, d
.data
, d
.size
)
131 || fputc('\n', fp
) < 0)
132 fatal(errno
, "error writing to %s", tag
);
133 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
136 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
140 cursor
= trackdb_opencursor(trackdb_usersdb
, tid
);
141 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
144 if(fputc('U', fp
) < 0
145 || urlencode(s
, k
.data
, k
.size
)
146 || fputc('\n', fp
) < 0
147 || urlencode(s
, d
.data
, d
.size
)
148 || fputc('\n', fp
) < 0)
149 fatal(errno
, "error writing to %s", tag
);
150 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
153 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
157 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
158 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
161 if(fputc('T', fp
) < 0
162 || urlencode(s
, k
.data
, k
.size
)
163 || fputc('\n', fp
) < 0
164 || urlencode(s
, d
.data
, d
.size
)
165 || fputc('\n', fp
) < 0)
166 fatal(errno
, "error writing to %s", tag
);
167 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
170 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
175 cursor
= trackdb_opencursor(trackdb_searchdb
, tid
);
176 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
179 if(fputc('S', fp
) < 0
180 || urlencode(s
, k
.data
, k
.size
)
181 || fputc('\n', fp
) < 0
182 || urlencode(s
, d
.data
, d
.size
)
183 || fputc('\n', fp
) < 0)
184 fatal(errno
, "error writing to %s", tag
);
185 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
188 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; } cursor
= 0;
191 if(fputs("E\n", fp
) < 0) fatal(errno
, "error writing to %s", tag
);
192 if(err
== DB_LOCK_DEADLOCK
) {
193 error(0, "c->c_get: %s", db_strerror(err
));
196 if(err
&& err
!= DB_NOTFOUND
)
197 fatal(0, "cursor->c_get: %s", db_strerror(err
));
198 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
201 trackdb_closecursor(cursor
);
203 info("aborting transaction and retrying dump");
204 trackdb_abort_transaction(tid
);
206 trackdb_commit_transaction(tid
);
207 if(fflush(fp
) < 0) fatal(errno
, "error writing to %s", tag
);
208 /* caller might not be paranoid so we are paranoid on their behalf */
209 if(fsync(fileno(fp
)) < 0) fatal(errno
, "error syncing %s", tag
);
212 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
213 static int remove_aliases(DB_TXN
*tid
, int remove_pathless
) {
220 info("removing aliases");
221 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
222 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
223 DB_FIRST
)) == DB_LOCK_DEADLOCK
) {
224 error(0, "cursor->c_get: %s", db_strerror(err
));
228 data
= kvp_urldecode(d
.data
, d
.size
);
229 alias
= !!kvp_get(data
, "_alias_for");
230 pathless
= !kvp_get(data
, "_path");
231 if(pathless
&& !remove_pathless
)
232 info("no _path for %s", utf82mb(xstrndup(k
.data
, k
.size
)));
233 if(alias
|| (remove_pathless
&& pathless
)) {
234 switch(err
= cursor
->c_del(cursor
, 0)) {
236 case DB_LOCK_DEADLOCK
:
237 error(0, "cursor->c_get: %s", db_strerror(err
));
240 fatal(0, "cursor->c_del: %s", db_strerror(err
));
243 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
), DB_NEXT
);
245 if(err
== DB_LOCK_DEADLOCK
) {
246 error(0, "cursor operation: %s", db_strerror(err
));
249 if(err
!= DB_NOTFOUND
) fatal(0, "cursor->c_get: %s", db_strerror(err
));
252 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
256 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
257 static int truncdb(DB_TXN
*tid
, DB
*db
) {
261 switch(err
= db
->truncate(db
, tid
, &count
, 0)) {
263 case DB_LOCK_DEADLOCK
:
264 error(0, "db->truncate: %s", db_strerror(err
));
267 fatal(0, "db->truncate: %s", db_strerror(err
));
272 /* read a DBT from FP, return 0 on success or -1 on input error */
273 static int undump_dbt(FILE *fp
, const char *tag
, DBT
*dbt
) {
277 if(inputline(tag
, fp
, &s
, '\n')) return -1;
279 if(urldecode(sink_dynstr(&d
), s
, strlen(s
)))
280 fatal(0, "invalid URL-encoded data in %s", tag
);
286 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
287 static int undump_from_fp(DB_TXN
*tid
, FILE *fp
, const char *tag
) {
290 const char *which_name
;
294 if(fseek(fp
, 0, SEEK_SET
) < 0)
295 fatal(errno
, "error calling fseek on %s", tag
);
296 if((err
= truncdb(tid
, trackdb_prefsdb
))) return err
;
297 if((err
= truncdb(tid
, trackdb_globaldb
))) return err
;
298 if((err
= truncdb(tid
, trackdb_searchdb
))) return err
;
299 if((err
= truncdb(tid
, trackdb_tagsdb
))) return err
;
300 if((err
= truncdb(tid
, trackdb_usersdb
))) return err
;
302 while(!ferror(fp
) && !feof(fp
)) {
307 fatal(0, "unknown version '%c'", c
);
316 which_db
= trackdb_prefsdb
;
317 which_name
= "prefs.db";
320 which_db
= trackdb_globaldb
;
321 which_name
= "global.db";
324 which_db
= trackdb_usersdb
;
325 which_name
= "users.db";
330 if(undump_dbt(fp
, tag
, prepare_data(&k
))
331 || undump_dbt(fp
, tag
, prepare_data(&d
)))
333 switch(err
= trackdb_prefsdb
->put(which_db
, tid
, &k
, &d
, 0)) {
336 case DB_LOCK_DEADLOCK
:
337 error(0, "error updating %s: %s", which_name
, db_strerror(err
));
340 fatal(0, "error updating %s: %s", which_name
, db_strerror(err
));
345 if(undump_dbt(fp
, tag
, prepare_data(&k
))
346 || undump_dbt(fp
, tag
, prepare_data(&d
)))
348 /* We don't restore the tracks.db or search.db entries, instead
349 * we recompute them */
357 fatal(errno
, "error reading %s", tag
);
359 fatal(0, "unexpected EOF reading %s", tag
);
363 /* recompute aliases and search database from prefs, return 0 or
364 * DB_LOCK_DEADLOCK */
365 static int recompute_aliases(DB_TXN
*tid
) {
370 const char *path
, *track
;
372 info("recomputing aliases");
373 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
374 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
375 DB_FIRST
)) == DB_LOCK_DEADLOCK
) goto done
;
377 data
= kvp_urldecode(d
.data
, d
.size
);
378 track
= xstrndup(k
.data
, k
.size
);
379 if(!kvp_get(data
, "_alias_for")) {
380 if(!(path
= kvp_get(data
, "_path")))
381 error(0, "%s is not an alias but has no path", utf82mb(track
));
383 if((err
= trackdb_notice_tid(track
, path
, tid
)) == DB_LOCK_DEADLOCK
)
386 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
395 case DB_LOCK_DEADLOCK
:
398 fatal(0, "cursor->c_get: %s", db_strerror(err
));
401 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
405 /* restore prefs from FP */
406 static void do_undump(FILE *fp
, const char *tag
, int remove_pathless
) {
410 tid
= trackdb_begin_transaction();
411 if(remove_aliases(tid
, remove_pathless
)
412 || undump_from_fp(tid
, fp
, tag
)
413 || recompute_aliases(tid
)) goto fail
;
416 info("aborting transaction and retrying undump");
417 trackdb_abort_transaction(tid
);
419 info("committing undump");
420 trackdb_commit_transaction(tid
);
423 /* just recompute alisaes */
424 static void do_recompute(int remove_pathless
) {
428 tid
= trackdb_begin_transaction();
429 if(remove_aliases(tid
, remove_pathless
)
430 || recompute_aliases(tid
)) goto fail
;
433 info("aborting transaction and retrying recomputation");
434 trackdb_abort_transaction(tid
);
436 info("committing recomputed aliases");
437 trackdb_commit_transaction(tid
);
440 int main(int argc
, char **argv
) {
441 int n
, dump
= 0, undump
= 0, recover
= TRACKDB_NO_RECOVER
, recompute
= 0;
442 int tracksdb
= 0, searchdb
= 0, remove_pathless
= 0, fd
;
448 while((n
= getopt_long(argc
, argv
, "hVc:dDutsrRaP", options
, 0)) >= 0) {
451 case 'V': version("disorder-dump");
452 case 'c': configfile
= optarg
; break;
453 case 'd': dump
= 1; break;
454 case 'u': undump
= 1; break;
455 case 'D': debugging
= 1; break;
456 case 't': tracksdb
= 1; break;
457 case 's': searchdb
= 1; break;
458 case 'r': recover
= TRACKDB_NORMAL_RECOVER
;
459 case 'R': recover
= TRACKDB_FATAL_RECOVER
;
460 case 'a': recompute
= 1; break;
461 case 'P': remove_pathless
= 1; break;
462 default: fatal(0, "invalid option");
465 if(dump
+ undump
+ recompute
!= 1)
466 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
467 if((undump
|| recompute
) && (tracksdb
|| searchdb
))
468 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
471 fatal(0, "--recompute-aliases does not take a filename");
475 fatal(0, "missing dump file name");
476 if(optind
+ 1 < argc
)
477 fatal(0, "specify only a dump file name");
480 if(config_read(0)) fatal(0, "cannot read configuration");
481 trackdb_init(recover
|TRACKDB_MAY_CREATE
);
482 trackdb_open(TRACKDB_NO_UPGRADE
);
484 /* We write to a temporary file and rename into place. We make
485 * sure the permissions are tight from the start. */
486 byte_xasprintf(&tmp
, "%s.%lx.tmp", path
, (unsigned long)getpid());
487 if((fd
= open(tmp
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) < 0)
488 fatal(errno
, "error opening %s", tmp
);
489 if(!(fp
= fdopen(fd
, "w")))
490 fatal(errno
, "fdopen on %s", tmp
);
491 do_dump(fp
, tmp
, tracksdb
, searchdb
);
492 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
493 if(rename(tmp
, path
) < 0)
494 fatal(errno
, "error renaming %s to %s", tmp
, path
);
496 /* the databases or logfiles might end up with wrong permissions
497 * if new ones are created */
498 if(getuid() == 0) info("you might need to chown database files");
499 if(!(fp
= fopen(path
, "r"))) fatal(errno
, "error opening %s", path
);
500 do_undump(fp
, path
, remove_pathless
);
502 } else if(recompute
) {
503 do_recompute(remove_pathless
);