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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file server/dump.c
19 * @brief Dump and restore database contents
21 #include "disorder-server.h"
23 static const struct option options
[] = {
24 { "help", no_argument
, 0, 'h' },
25 { "version", no_argument
, 0, 'V' },
26 { "config", required_argument
, 0, 'c' },
27 { "dump", no_argument
, 0, 'd' },
28 { "undump", no_argument
, 0, 'u' },
29 { "debug", no_argument
, 0, 'D' },
30 { "recover", no_argument
, 0, 'r' },
31 { "recover-fatal", no_argument
, 0, 'R' },
32 { "trackdb", no_argument
, 0, 't' },
33 { "searchdb", no_argument
, 0, 's' },
34 { "recompute-aliases", no_argument
, 0, 'a' },
35 { "remove-pathless", no_argument
, 0, 'P' },
39 /* display usage message and terminate */
40 static void help(void) {
42 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
43 " disorder-dump [OPTIONS] --recompute-aliases\n"
45 " --help, -h Display usage message\n"
46 " --version, -V Display version number\n"
47 " --config PATH, -c PATH Set configuration file\n"
48 " --dump, -d Dump state to PATH\n"
49 " --undump, -u Restore state from PATH\n"
50 " --recover, -r Run database recovery\n"
51 " --recompute-aliases, -a Recompute aliases\n"
52 " --remove-pathless, -P Remove pathless tracks\n"
53 " --debug Debug mode\n");
58 /* dump prefs to FP, return nonzero on error */
59 static void do_dump(FILE *fp
, const char *tag
,
60 int tracksdb
, int searchdb
) {
63 struct sink
*s
= sink_stdio(tag
, fp
);
68 tid
= trackdb_begin_transaction();
69 if(fseek(fp
, 0, SEEK_SET
) < 0)
70 fatal(errno
, "error calling fseek");
72 fatal(errno
, "error calling fflush");
73 if(ftruncate(fileno(fp
), 0) < 0)
74 fatal(errno
, "error calling ftruncate");
75 if(fprintf(fp
, "V%c\n", (tracksdb
|| searchdb
) ?
'1' : '0') < 0)
76 fatal(errno
, "error writing to %s", tag
);
77 /* dump the preferences */
78 cursor
= trackdb_opencursor(trackdb_prefsdb
, tid
);
79 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
83 || urlencode(s
, k
.data
, k
.size
)
84 || fputc('\n', fp
) < 0
85 || urlencode(s
, d
.data
, d
.size
)
86 || fputc('\n', fp
) < 0)
87 fatal(errno
, "error writing to %s", tag
);
88 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
91 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
94 /* dump the global preferences */
95 cursor
= trackdb_opencursor(trackdb_globaldb
, tid
);
96 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
100 || urlencode(s
, k
.data
, k
.size
)
101 || fputc('\n', fp
) < 0
102 || urlencode(s
, d
.data
, d
.size
)
103 || fputc('\n', fp
) < 0)
104 fatal(errno
, "error writing to %s", tag
);
105 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
108 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
112 cursor
= trackdb_opencursor(trackdb_usersdb
, tid
);
113 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
116 if(fputc('U', 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 schedule */
129 cursor
= trackdb_opencursor(trackdb_scheduledb
, tid
);
130 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
133 if(fputc('W', 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
; }
147 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
148 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
151 if(fputc('T', fp
) < 0
152 || urlencode(s
, k
.data
, k
.size
)
153 || fputc('\n', fp
) < 0
154 || urlencode(s
, d
.data
, d
.size
)
155 || fputc('\n', fp
) < 0)
156 fatal(errno
, "error writing to %s", tag
);
157 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
160 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
165 cursor
= trackdb_opencursor(trackdb_searchdb
, tid
);
166 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
169 if(fputc('S', fp
) < 0
170 || urlencode(s
, k
.data
, k
.size
)
171 || fputc('\n', fp
) < 0
172 || urlencode(s
, d
.data
, d
.size
)
173 || fputc('\n', fp
) < 0)
174 fatal(errno
, "error writing to %s", tag
);
175 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
178 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; } cursor
= 0;
181 if(fputs("E\n", fp
) < 0) fatal(errno
, "error writing to %s", tag
);
182 if(err
== DB_LOCK_DEADLOCK
) {
183 error(0, "c->c_get: %s", db_strerror(err
));
186 if(err
&& err
!= DB_NOTFOUND
)
187 fatal(0, "cursor->c_get: %s", db_strerror(err
));
188 if(trackdb_closecursor(cursor
)) { cursor
= 0; goto fail
; }
191 trackdb_closecursor(cursor
);
193 info("aborting transaction and retrying dump");
194 trackdb_abort_transaction(tid
);
196 trackdb_commit_transaction(tid
);
197 if(fflush(fp
) < 0) fatal(errno
, "error writing to %s", tag
);
198 /* caller might not be paranoid so we are paranoid on their behalf */
199 if(fsync(fileno(fp
)) < 0) fatal(errno
, "error syncing %s", tag
);
202 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
203 static int remove_aliases(DB_TXN
*tid
, int remove_pathless
) {
210 info("removing aliases");
211 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
212 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
213 DB_FIRST
)) == DB_LOCK_DEADLOCK
) {
214 error(0, "cursor->c_get: %s", db_strerror(err
));
218 data
= kvp_urldecode(d
.data
, d
.size
);
219 alias
= !!kvp_get(data
, "_alias_for");
220 pathless
= !kvp_get(data
, "_path");
221 if(pathless
&& !remove_pathless
)
222 info("no _path for %s", utf82mb(xstrndup(k
.data
, k
.size
)));
223 if(alias
|| (remove_pathless
&& pathless
)) {
224 switch(err
= cursor
->c_del(cursor
, 0)) {
226 case DB_LOCK_DEADLOCK
:
227 error(0, "cursor->c_get: %s", db_strerror(err
));
230 fatal(0, "cursor->c_del: %s", db_strerror(err
));
233 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
), DB_NEXT
);
235 if(err
== DB_LOCK_DEADLOCK
) {
236 error(0, "cursor operation: %s", db_strerror(err
));
239 if(err
!= DB_NOTFOUND
) fatal(0, "cursor->c_get: %s", db_strerror(err
));
242 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
246 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
247 static int truncdb(DB_TXN
*tid
, DB
*db
) {
251 switch(err
= db
->truncate(db
, tid
, &count
, 0)) {
253 case DB_LOCK_DEADLOCK
:
254 error(0, "db->truncate: %s", db_strerror(err
));
257 fatal(0, "db->truncate: %s", db_strerror(err
));
262 /* read a DBT from FP, return 0 on success or -1 on input error */
263 static int undump_dbt(FILE *fp
, const char *tag
, DBT
*dbt
) {
267 if(inputline(tag
, fp
, &s
, '\n')) return -1;
269 if(urldecode(sink_dynstr(&d
), s
, strlen(s
)))
270 fatal(0, "invalid URL-encoded data in %s", tag
);
276 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
277 static int undump_from_fp(DB_TXN
*tid
, FILE *fp
, const char *tag
) {
280 const char *which_name
;
284 if(fseek(fp
, 0, SEEK_SET
) < 0)
285 fatal(errno
, "error calling fseek on %s", tag
);
286 if((err
= truncdb(tid
, trackdb_prefsdb
))) return err
;
287 if((err
= truncdb(tid
, trackdb_globaldb
))) return err
;
288 if((err
= truncdb(tid
, trackdb_searchdb
))) return err
;
289 if((err
= truncdb(tid
, trackdb_tagsdb
))) return err
;
290 if((err
= truncdb(tid
, trackdb_usersdb
))) return err
;
291 if((err
= truncdb(tid
, trackdb_scheduledb
))) return err
;
293 while(!ferror(fp
) && !feof(fp
)) {
298 fatal(0, "unknown version '%c'", c
);
308 which_db
= trackdb_prefsdb
;
309 which_name
= "prefs.db";
312 which_db
= trackdb_globaldb
;
313 which_name
= "global.db";
316 which_db
= trackdb_usersdb
;
317 which_name
= "users.db";
319 case 'W': /* for 'when' */
320 which_db
= trackdb_scheduledb
;
321 which_name
= "scheduledb.db";
326 if(undump_dbt(fp
, tag
, prepare_data(&k
))
327 || undump_dbt(fp
, tag
, prepare_data(&d
)))
329 switch(err
= which_db
->put(which_db
, tid
, &k
, &d
, 0)) {
332 case DB_LOCK_DEADLOCK
:
333 error(0, "error updating %s: %s", which_name
, db_strerror(err
));
336 fatal(0, "error updating %s: %s", which_name
, db_strerror(err
));
341 if(undump_dbt(fp
, tag
, prepare_data(&k
))
342 || undump_dbt(fp
, tag
, prepare_data(&d
)))
344 /* We don't restore the tracks.db or search.db entries, instead
345 * we recompute them */
353 fatal(errno
, "error reading %s", tag
);
355 fatal(0, "unexpected EOF reading %s", tag
);
359 /* recompute aliases and search database from prefs, return 0 or
360 * DB_LOCK_DEADLOCK */
361 static int recompute_aliases(DB_TXN
*tid
) {
366 const char *path
, *track
;
368 info("recomputing aliases");
369 cursor
= trackdb_opencursor(trackdb_tracksdb
, tid
);
370 if((err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
371 DB_FIRST
)) == DB_LOCK_DEADLOCK
) goto done
;
373 data
= kvp_urldecode(d
.data
, d
.size
);
374 track
= xstrndup(k
.data
, k
.size
);
375 if(!kvp_get(data
, "_alias_for")) {
376 if(!(path
= kvp_get(data
, "_path")))
377 error(0, "%s is not an alias but has no path", utf82mb(track
));
379 if((err
= trackdb_notice_tid(track
, path
, tid
)) == DB_LOCK_DEADLOCK
)
382 err
= cursor
->c_get(cursor
, prepare_data(&k
), prepare_data(&d
),
391 case DB_LOCK_DEADLOCK
:
394 fatal(0, "cursor->c_get: %s", db_strerror(err
));
397 if(trackdb_closecursor(cursor
) && !err
) err
= DB_LOCK_DEADLOCK
;
401 /* restore prefs from FP */
402 static void do_undump(FILE *fp
, const char *tag
, int remove_pathless
) {
406 tid
= trackdb_begin_transaction();
407 if(remove_aliases(tid
, remove_pathless
)
408 || undump_from_fp(tid
, fp
, tag
)
409 || recompute_aliases(tid
)) goto fail
;
412 info("aborting transaction and retrying undump");
413 trackdb_abort_transaction(tid
);
415 info("committing undump");
416 trackdb_commit_transaction(tid
);
419 /* just recompute alisaes */
420 static void do_recompute(int remove_pathless
) {
424 tid
= trackdb_begin_transaction();
425 if(remove_aliases(tid
, remove_pathless
)
426 || recompute_aliases(tid
)) goto fail
;
429 info("aborting transaction and retrying recomputation");
430 trackdb_abort_transaction(tid
);
432 info("committing recomputed aliases");
433 trackdb_commit_transaction(tid
);
436 int main(int argc
, char **argv
) {
437 int n
, dump
= 0, undump
= 0, recover
= TRACKDB_NO_RECOVER
, recompute
= 0;
438 int tracksdb
= 0, searchdb
= 0, remove_pathless
= 0, fd
;
444 while((n
= getopt_long(argc
, argv
, "hVc:dDutsrRaP", options
, 0)) >= 0) {
447 case 'V': version("disorder-dump");
448 case 'c': configfile
= optarg
; break;
449 case 'd': dump
= 1; break;
450 case 'u': undump
= 1; break;
451 case 'D': debugging
= 1; break;
452 case 't': tracksdb
= 1; break;
453 case 's': searchdb
= 1; break;
454 case 'r': recover
= TRACKDB_NORMAL_RECOVER
;
455 case 'R': recover
= TRACKDB_FATAL_RECOVER
;
456 case 'a': recompute
= 1; break;
457 case 'P': remove_pathless
= 1; break;
458 default: fatal(0, "invalid option");
461 if(dump
+ undump
+ recompute
!= 1)
462 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
463 if((undump
|| recompute
) && (tracksdb
|| searchdb
))
464 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
467 fatal(0, "--recompute-aliases does not take a filename");
471 fatal(0, "missing dump file name");
472 if(optind
+ 1 < argc
)
473 fatal(0, "specify only a dump file name");
476 if(config_read(0, NULL
)) fatal(0, "cannot read configuration");
477 trackdb_init(recover
|TRACKDB_MAY_CREATE
);
478 trackdb_open(TRACKDB_NO_UPGRADE
);
480 /* We write to a temporary file and rename into place. We make
481 * sure the permissions are tight from the start. */
482 byte_xasprintf(&tmp
, "%s.%lx.tmp", path
, (unsigned long)getpid());
483 if((fd
= open(tmp
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0600)) < 0)
484 fatal(errno
, "error opening %s", tmp
);
485 if(!(fp
= fdopen(fd
, "w")))
486 fatal(errno
, "fdopen on %s", tmp
);
487 do_dump(fp
, tmp
, tracksdb
, searchdb
);
488 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
489 if(rename(tmp
, path
) < 0)
490 fatal(errno
, "error renaming %s to %s", tmp
, path
);
492 /* the databases or logfiles might end up with wrong permissions
493 * if new ones are created */
494 if(getuid() == 0) info("you might need to chown database files");
495 if(!(fp
= fopen(path
, "r"))) fatal(errno
, "error opening %s", path
);
496 do_undump(fp
, path
, remove_pathless
);
498 } else if(recompute
) {
499 do_recompute(remove_pathless
);