chiark / gitweb /
Assume initial digits in a track name are a sort key even without the
[disorder] / server / dump.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
05b75f8d 21#include "disorder-server.h"
460b9539 22
23static 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' },
36 { 0, 0, 0, 0 }
37};
38
39/* display usage message and terminate */
40static void help(void) {
41 xprintf("Usage:\n"
42 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
43 " disorder-dump [OPTIONS] --recompute-aliases\n"
44 "Options:\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");
54 xfclose(stdout);
55 exit(0);
56}
57
460b9539 58/* dump prefs to FP, return nonzero on error */
59static void do_dump(FILE *fp, const char *tag,
60 int tracksdb, int searchdb) {
61 DBC *cursor = 0;
62 DB_TXN *tid;
63 struct sink *s = sink_stdio(tag, fp);
64 int err;
65 DBT k, d;
66
67 for(;;) {
68 tid = trackdb_begin_transaction();
69 if(fseek(fp, 0, SEEK_SET) < 0)
70 fatal(errno, "error calling fseek");
71 if(fflush(fp) < 0)
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);
f35e5800 77 /* dump the preferences */
460b9539 78 cursor = trackdb_opencursor(trackdb_prefsdb, tid);
79 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
80 DB_FIRST);
81 while(err == 0) {
82 if(fputc('P', fp) < 0
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),
89 DB_NEXT);
90 }
91 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
92 cursor = 0;
93
f35e5800
RK
94 /* dump the global preferences */
95 cursor = trackdb_opencursor(trackdb_globaldb, tid);
96 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
97 DB_FIRST);
98 while(err == 0) {
99 if(fputc('G', fp) < 0
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),
106 DB_NEXT);
107 }
108 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
109 cursor = 0;
110
a745dd43
RK
111 /* dump the users */
112 cursor = trackdb_opencursor(trackdb_usersdb, tid);
113 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
114 DB_FIRST);
115 while(err == 0) {
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),
123 DB_NEXT);
124 }
125 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
126 cursor = 0;
0e464e60
RK
127
128 /* dump the schedule */
129 cursor = trackdb_opencursor(trackdb_scheduledb, tid);
130 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
131 DB_FIRST);
132 while(err == 0) {
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),
140 DB_NEXT);
141 }
142 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
143 cursor = 0;
144
a745dd43 145
460b9539 146 if(tracksdb) {
147 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
148 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
149 DB_FIRST);
150 while(err == 0) {
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),
158 DB_NEXT);
159 }
160 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
161 cursor = 0;
162 }
163
164 if(searchdb) {
165 cursor = trackdb_opencursor(trackdb_searchdb, tid);
166 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
167 DB_FIRST);
168 while(err == 0) {
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),
176 DB_NEXT);
177 }
178 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } cursor = 0;
179 }
180
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));
184 goto fail;
185 }
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; }
189 break;
190fail:
191 trackdb_closecursor(cursor);
192 cursor = 0;
193 info("aborting transaction and retrying dump");
194 trackdb_abort_transaction(tid);
195 }
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);
200}
201
202/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
203static int remove_aliases(DB_TXN *tid, int remove_pathless) {
204 DBC *cursor;
205 int err;
206 DBT k, d;
207 struct kvp *data;
208 int alias, pathless;
209
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));
215 goto done;
216 }
217 while(err == 0) {
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)) {
225 case 0: break;
226 case DB_LOCK_DEADLOCK:
227 error(0, "cursor->c_get: %s", db_strerror(err));
228 goto done;
229 default:
230 fatal(0, "cursor->c_del: %s", db_strerror(err));
231 }
232 }
233 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
234 }
235 if(err == DB_LOCK_DEADLOCK) {
236 error(0, "cursor operation: %s", db_strerror(err));
237 goto done;
238 }
239 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
240 err = 0;
241done:
242 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
243 return err;
244}
245
246/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
247static int truncdb(DB_TXN *tid, DB *db) {
248 int err;
249 u_int32_t count;
250
251 switch(err = db->truncate(db, tid, &count, 0)) {
252 case 0: break;
253 case DB_LOCK_DEADLOCK:
254 error(0, "db->truncate: %s", db_strerror(err));
255 break;
256 default:
257 fatal(0, "db->truncate: %s", db_strerror(err));
258 }
259 return err;
260}
261
262/* read a DBT from FP, return 0 on success or -1 on input error */
263static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
264 char *s;
265 struct dynstr d;
266
267 if(inputline(tag, fp, &s, '\n')) return -1;
268 dynstr_init(&d);
269 if(urldecode(sink_dynstr(&d), s, strlen(s)))
270 fatal(0, "invalid URL-encoded data in %s", tag);
271 dbt->data = d.vec;
272 dbt->size = d.nvec;
273 return 0;
274}
275
276/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
277static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
278 int err, c;
279 DBT k, d;
f35e5800
RK
280 const char *which_name;
281 DB *which_db;
460b9539 282
283 info("undumping");
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;
fb1bc1f5 287 if((err = truncdb(tid, trackdb_globaldb))) return err;
460b9539 288 if((err = truncdb(tid, trackdb_searchdb))) return err;
fb1bc1f5 289 if((err = truncdb(tid, trackdb_tagsdb))) return err;
a745dd43 290 if((err = truncdb(tid, trackdb_usersdb))) return err;
0e464e60 291 if((err = truncdb(tid, trackdb_scheduledb))) return err;
460b9539 292 c = getc(fp);
293 while(!ferror(fp) && !feof(fp)) {
294 switch(c) {
295 case 'V':
296 c = getc(fp);
297 if(c != '0')
298 fatal(0, "unknown version '%c'", c);
299 break;
300 case 'E':
301 return 0;
302 case 'P':
f35e5800 303 case 'G':
a745dd43 304 case 'U':
0e464e60 305 case 'W':
a745dd43
RK
306 switch(c) {
307 case 'P':
f35e5800
RK
308 which_db = trackdb_prefsdb;
309 which_name = "prefs.db";
a745dd43
RK
310 break;
311 case 'G':
f35e5800
RK
312 which_db = trackdb_globaldb;
313 which_name = "global.db";
a745dd43
RK
314 break;
315 case 'U':
316 which_db = trackdb_usersdb;
317 which_name = "users.db";
318 break;
0e464e60
RK
319 case 'W': /* for 'when' */
320 which_db = trackdb_scheduledb;
321 which_name = "scheduledb.db";
322 break;
a745dd43
RK
323 default:
324 abort();
f35e5800 325 }
460b9539 326 if(undump_dbt(fp, tag, prepare_data(&k))
327 || undump_dbt(fp, tag, prepare_data(&d)))
328 break;
0e464e60 329 switch(err = which_db->put(which_db, tid, &k, &d, 0)) {
460b9539 330 case 0:
331 break;
332 case DB_LOCK_DEADLOCK:
f35e5800 333 error(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 334 return err;
335 default:
f35e5800 336 fatal(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 337 }
338 break;
339 case 'T':
340 case 'S':
341 if(undump_dbt(fp, tag, prepare_data(&k))
342 || undump_dbt(fp, tag, prepare_data(&d)))
343 break;
344 /* We don't restore the tracks.db or search.db entries, instead
345 * we recompute them */
346 break;
347 case '\n':
348 break;
349 }
350 c = getc(fp);
351 }
352 if(ferror(fp))
353 fatal(errno, "error reading %s", tag);
354 else
355 fatal(0, "unexpected EOF reading %s", tag);
356 return 0;
357}
358
359/* recompute aliases and search database from prefs, return 0 or
360 * DB_LOCK_DEADLOCK */
361static int recompute_aliases(DB_TXN *tid) {
362 DBC *cursor;
363 DBT k, d;
364 int err;
365 struct kvp *data;
366 const char *path, *track;
367
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;
372 while(err == 0) {
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));
378 else
379 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
380 goto done;
381 }
382 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
383 DB_NEXT);
384 }
385 switch(err) {
386 case 0:
387 break;
388 case DB_NOTFOUND:
389 err = 0;
390 break;
391 case DB_LOCK_DEADLOCK:
392 break;
393 default:
394 fatal(0, "cursor->c_get: %s", db_strerror(err));
395 }
396done:
397 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
398 return err;
399}
400
401/* restore prefs from FP */
402static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
403 DB_TXN *tid;
404
405 for(;;) {
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;
410 break;
411fail:
412 info("aborting transaction and retrying undump");
413 trackdb_abort_transaction(tid);
414 }
415 info("committing undump");
416 trackdb_commit_transaction(tid);
417}
418
419/* just recompute alisaes */
420static void do_recompute(int remove_pathless) {
421 DB_TXN *tid;
422
423 for(;;) {
424 tid = trackdb_begin_transaction();
425 if(remove_aliases(tid, remove_pathless)
426 || recompute_aliases(tid)) goto fail;
427 break;
428fail:
429 info("aborting transaction and retrying recomputation");
430 trackdb_abort_transaction(tid);
431 }
432 info("committing recomputed aliases");
433 trackdb_commit_transaction(tid);
434}
435
436int main(int argc, char **argv) {
437 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
a745dd43 438 int tracksdb = 0, searchdb = 0, remove_pathless = 0, fd;
460b9539 439 const char *path;
440 char *tmp;
441 FILE *fp;
442
320598d4 443 mem_init();
460b9539 444 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
445 switch(n) {
446 case 'h': help();
3fbdc96d 447 case 'V': version("disorder-dump");
460b9539 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");
459 }
460 }
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");
465 if(recompute) {
466 if(optind != argc)
467 fatal(0, "--recompute-aliases does not take a filename");
468 path = 0;
469 } else {
470 if(optind >= argc)
471 fatal(0, "missing dump file name");
472 if(optind + 1 < argc)
473 fatal(0, "specify only a dump file name");
474 path = argv[optind];
475 }
c00fce3a 476 if(config_read(0)) fatal(0, "cannot read configuration");
a745dd43 477 trackdb_init(recover|TRACKDB_MAY_CREATE);
d25c4615 478 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 479 if(dump) {
a745dd43
RK
480 /* We write to a temporary file and rename into place. We make
481 * sure the permissions are tight from the start. */
460b9539 482 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
a745dd43
RK
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);
460b9539 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);
491 } else if(undump) {
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);
497 xfclose(fp);
498 } else if(recompute) {
499 do_recompute(remove_pathless);
500 }
501 trackdb_close();
502 trackdb_deinit();
503 return 0;
504}
505
506/*
507Local Variables:
508c-basic-offset:2
509comment-column:40
510End:
511*/