7b4b3a49a90eb02b7c8e241d5d275d747a0bb9e1
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 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
; }
156 /* dump the schedule */
157 cursor
= trackdb_opencursor(trackdb_scheduledb
, tid
);
158 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
161 if(fputc('W', 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_tracksdb
, tid
);
176 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
179 if(fputc('T', 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
; }
193 cursor
= trackdb_opencursor(trackdb_searchdb
, tid
);
194 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
197 if(fputc('S', fp
) < 0
198 || urlencode(s
, k
.data
, k
.size
)
199 || fputc('\n', fp
) < 0
200 || urlencode(s
, d
.data
, d
.size
)
201 || fputc('\n', fp
) < 0)
202 fatal(errno
, "error writing to %s", tag
);
203 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
206 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; } cursor
= 0;
209 if(fputs("E\n", fp
) < 0) fatal(errno
, "error writing to %s", tag
);
210 if(err
== DB_LOCK_DEADLOCK
) {
211 error(0, "c->c_get: %s", db_strerror(err
));
214 if(err
&& err
!= DB_NOTFOUND
)
215 fatal(0, "cursor->c_get: %s", db_strerror(err
));
216 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
219 trackdb_closecursor(cursor
);
221 info("aborting transaction and retrying dump");
222 trackdb_abort_transaction(tid
);
224 trackdb_commit_transaction(tid
);
225 if(fflush(fp
) < 0) fatal(errno
, "error writing to %s", tag
);
226 /* caller might not be paranoid so we are paranoid on their behalf */
227 if(fsync(fileno(fp
)) < 0) fatal(errno
, "error syncing %s", tag
);
230 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
231 static int remove_aliases(DB_TXN
*tid
, int remove_pathless
) {
238 info("removing aliases");
239 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
240 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
241 DB_FIRST
)) == DB_LOCK_DEADLOCK
) {
242 error(0, "cursor->c_get: %s", db_strerror(err
));
246 data
= kvp_urldecode(d
.data
, d
.size
);
247 alias
= !!kvp_get(data
, "_alias_for");
248 pathless
= !kvp_get(data
, "_path");
249 if(pathless
&& !remove_pathless
)
250 info("no _path for %s", utf82mb(xstrndup(k
.data
, k
.size
)));
251 if(alias
|| (remove_pathless
&& pathless
)) {
252 switch(err
= cursor
->c_del(cursor
, 0)) {
254 case DB_LOCK_DEADLOCK
:
255 error(0, "cursor->c_get: %s", db_strerror(err
));
258 fatal(0, "cursor->c_del: %s", db_strerror(err
));
261 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
), DB_NEXT
);
263 if(err
== DB_LOCK_DEADLOCK
) {
264 error(0, "cursor operation: %s", db_strerror(err
));
267 if(err
!= DB_NOTFOUND
) fatal(0, "cursor->c_get: %s", db_strerror(err
));
270 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
274 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
275 static int truncdb(DB_TXN
*tid
, DB
*db
) {
279 switch(err
= db
->truncate(db
, tid
, &count
, 0)) {
281 case DB_LOCK_DEADLOCK
:
282 error(0, "db->truncate: %s", db_strerror(err
));
285 fatal(0, "db->truncate: %s", db_strerror(err
));
290 /* read a DBT from FP, return 0 on success or -1 on input error */
291 static int undump_dbt(FILE *fp
, const char *tag
, DBT
*dbt
) {
295 if(inputline(tag
, fp
, &s
, '\n')) return -1;
297 if(urldecode(sink_dynstr(&d
), s
, strlen(s
)))
298 fatal(0, "invalid URL-encoded data in %s", tag
);
304 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
305 static int undump_from_fp(DB_TXN
*tid
, FILE *fp
, const char *tag
) {
308 const char *which_name
;
312 if(fseek(fp
, 0, SEEK_SET
) < 0)
313 fatal(errno
, "error calling fseek on %s", tag
);
314 if((err
= truncdb(tid
, trackdb_prefsdb
))) return err
;
315 if((err
= truncdb(tid
, trackdb_globaldb
))) return err
;
316 if((err
= truncdb(tid
, trackdb_searchdb
))) return err
;
317 if((err
= truncdb(tid
, trackdb_tagsdb
))) return err
;
318 if((err
= truncdb(tid
, trackdb_usersdb
))) return err
;
319 if((err
= truncdb(tid
, trackdb_scheduledb
))) return err
;
321 while(!ferror(fp
) && !feof(fp
)) {
326 fatal(0, "unknown version '%c'", c
);
336 which_db
= trackdb_prefsdb
;
337 which_name
= "prefs.db";
340 which_db
= trackdb_globaldb
;
341 which_name
= "global.db";
344 which_db
= trackdb_usersdb
;
345 which_name
= "users.db";
347 case 'W': /* for 'when' */
348 which_db
= trackdb_scheduledb
;
349 which_name
= "scheduledb.db";
354 if(undump_dbt(fp
, tag
, prepare_data(&k
))
355 || undump_dbt(fp
, tag
, prepare_data(&d
)))
357 switch(err
= which_db
->put(which_db
, tid
, &k
, &d
, 0)) {
360 case DB_LOCK_DEADLOCK
:
361 error(0, "error updating %s: %s", which_name
, db_strerror(err
));
364 fatal(0, "error updating %s: %s", which_name
, db_strerror(err
));
369 if(undump_dbt(fp
, tag
, prepare_data(&k
))
370 || undump_dbt(fp
, tag
, prepare_data(&d
)))
372 /* We don't restore the tracks.db or search.db entries, instead
373 * we recompute them */
381 fatal(errno
, "error reading %s", tag
);
383 fatal(0, "unexpected EOF reading %s", tag
);
387 /* recompute aliases and search database from prefs, return 0 or
388 * DB_LOCK_DEADLOCK */
389 static int recompute_aliases(DB_TXN
*tid
) {
394 const char *path
, *track
;
396 info("recomputing aliases");
397 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
398 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
399 DB_FIRST
)) == DB_LOCK_DEADLOCK
) goto done
;
401 data
= kvp_urldecode(d
.data
, d
.size
);
402 track
= xstrndup(k
.data
, k
.size
);
403 if(!kvp_get(data
, "_alias_for")) {
404 if(!(path
= kvp_get(data
, "_path")))
405 error(0, "%s is not an alias but has no path", utf82mb(track
));
407 if((err
= trackdb_notice_tid(track
, path
, tid
)) == DB_LOCK_DEADLOCK
)
410 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
419 case DB_LOCK_DEADLOCK
:
422 fatal(0, "cursor->c_get: %s", db_strerror(err
));
425 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
429 /* restore prefs from FP */
430 static void do_undump(FILE *fp
, const char *tag
, int remove_pathless
) {
434 tid
= trackdb_begin_transaction();
435 if(remove_aliases(tid
, remove_pathless
)
436 || undump_from_fp(tid
, fp
, tag
)
437 || recompute_aliases(tid
)) goto fail
;
440 info("aborting transaction and retrying undump");
441 trackdb_abort_transaction(tid
);
443 info("committing undump");
444 trackdb_commit_transaction(tid
);
447 /* just recompute alisaes */
448 static void do_recompute(int remove_pathless
) {
452 tid
= trackdb_begin_transaction();
453 if(remove_aliases(tid
, remove_pathless
)
454 || recompute_aliases(tid
)) goto fail
;
457 info("aborting transaction and retrying recomputation");
458 trackdb_abort_transaction(tid
);
460 info("committing recomputed aliases");
461 trackdb_commit_transaction(tid
);
464 int main(int argc
, char **argv
) {
465 int n
, dump
= 0, undump
= 0, recover
= TRACKDB_NO_RECOVER
, recompute
= 0;
466 int tracksdb
= 0, searchdb
= 0, remove_pathless
= 0, fd
;
472 while((n
= getopt_long(argc
, argv
, "hVc:dDutsrRaP", options
, 0)) >= 0) {
475 case 'V': version("disorder-dump");
476 case 'c': configfile
= optarg
; break;
477 case 'd': dump
= 1; break;
478 case 'u': undump
= 1; break;
479 case 'D': debugging
= 1; break;
480 case 't': tracksdb
= 1; break;
481 case 's': searchdb
= 1; break;
482 case 'r': recover
= TRACKDB_NORMAL_RECOVER
;
483 case 'R': recover
= TRACKDB_FATAL_RECOVER
;
484 case 'a': recompute
= 1; break;
485 case 'P': remove_pathless
= 1; break;
486 default: fatal(0, "invalid option");
489 if(dump
+ undump
+ recompute
!= 1)
490 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
491 if((undump
|| recompute
) && (tracksdb
|| searchdb
))
492 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
495 fatal(0, "--recompute-aliases does not take a filename");
499 fatal(0, "missing dump file name");
500 if(optind
+ 1 < argc
)
501 fatal(0, "specify only a dump file name");
504 if(config_read(0)) fatal(0, "cannot read configuration");
505 trackdb_init(recover
|TRACKDB_MAY_CREATE
);
506 trackdb_open(TRACKDB_NO_UPGRADE
);
508 /* We write to a temporary file and rename into place. We make
509 * sure the permissions are tight from the start. */
510 byte_xasprintf(&tmp
, "%s.%lx.tmp", path
, (unsigned long)getpid());
511 if((fd
= open(tmp
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) < 0)
512 fatal(errno
, "error opening %s", tmp
);
513 if(!(fp
= fdopen(fd
, "w")))
514 fatal(errno
, "fdopen on %s", tmp
);
515 do_dump(fp
, tmp
, tracksdb
, searchdb
);
516 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
517 if(rename(tmp
, path
) < 0)
518 fatal(errno
, "error renaming %s to %s", tmp
, path
);
520 /* the databases or logfiles might end up with wrong permissions
521 * if new ones are created */
522 if(getuid() == 0) info("you might need to chown database files");
523 if(!(fp
= fopen(path
, "r"))) fatal(errno
, "error opening %s", path
);
524 do_undump(fp
, path
, remove_pathless
);
526 } else if(recompute
) {
527 do_recompute(remove_pathless
);