chiark / gitweb /
fixes and test for disorder-dump
[disorder] / server / dump.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005 Richard Kettlewell
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
21#include <config.h>
22#include "types.h"
23
24#include <getopt.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <stdio.h>
28#include <string.h>
29#include <pcre.h>
30#include <unistd.h>
31#include <db.h>
32
33#include "configuration.h"
34#include "syscalls.h"
35#include "log.h"
36#include "client.h"
37#include "sink.h"
38#include "mem.h"
39#include "defs.h"
40#include "printf.h"
41#include "kvp.h"
42#include "vector.h"
43#include "inputline.h"
44#include "trackdb.h"
45#include "trackdb-int.h"
46#include "charset.h"
47
48static const struct option options[] = {
49 { "help", no_argument, 0, 'h' },
50 { "version", no_argument, 0, 'V' },
51 { "config", required_argument, 0, 'c' },
52 { "dump", no_argument, 0, 'd' },
53 { "undump", no_argument, 0, 'u' },
54 { "debug", no_argument, 0, 'D' },
55 { "recover", no_argument, 0, 'r' },
56 { "recover-fatal", no_argument, 0, 'R' },
57 { "trackdb", no_argument, 0, 't' },
58 { "searchdb", no_argument, 0, 's' },
59 { "recompute-aliases", no_argument, 0, 'a' },
60 { "remove-pathless", no_argument, 0, 'P' },
61 { 0, 0, 0, 0 }
62};
63
64/* display usage message and terminate */
65static void help(void) {
66 xprintf("Usage:\n"
67 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
68 " disorder-dump [OPTIONS] --recompute-aliases\n"
69 "Options:\n"
70 " --help, -h Display usage message\n"
71 " --version, -V Display version number\n"
72 " --config PATH, -c PATH Set configuration file\n"
73 " --dump, -d Dump state to PATH\n"
74 " --undump, -u Restore state from PATH\n"
75 " --recover, -r Run database recovery\n"
76 " --recompute-aliases, -a Recompute aliases\n"
77 " --remove-pathless, -P Remove pathless tracks\n"
78 " --debug Debug mode\n");
79 xfclose(stdout);
80 exit(0);
81}
82
83/* display version number and terminate */
84static void version(void) {
85 xprintf("disorder-dump version %s\n", disorder_version_string);
86 xfclose(stdout);
87 exit(0);
88}
89
90/* dump prefs to FP, return nonzero on error */
91static void do_dump(FILE *fp, const char *tag,
92 int tracksdb, int searchdb) {
93 DBC *cursor = 0;
94 DB_TXN *tid;
95 struct sink *s = sink_stdio(tag, fp);
96 int err;
97 DBT k, d;
98
99 for(;;) {
100 tid = trackdb_begin_transaction();
101 if(fseek(fp, 0, SEEK_SET) < 0)
102 fatal(errno, "error calling fseek");
103 if(fflush(fp) < 0)
104 fatal(errno, "error calling fflush");
105 if(ftruncate(fileno(fp), 0) < 0)
106 fatal(errno, "error calling ftruncate");
107 if(fprintf(fp, "V%c\n", (tracksdb || searchdb) ? '1' : '0') < 0)
108 fatal(errno, "error writing to %s", tag);
f35e5800 109 /* dump the preferences */
460b9539 110 cursor = trackdb_opencursor(trackdb_prefsdb, tid);
111 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
112 DB_FIRST);
113 while(err == 0) {
114 if(fputc('P', fp) < 0
115 || urlencode(s, k.data, k.size)
116 || fputc('\n', fp) < 0
117 || urlencode(s, d.data, d.size)
118 || fputc('\n', fp) < 0)
119 fatal(errno, "error writing to %s", tag);
120 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
121 DB_NEXT);
122 }
123 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
124 cursor = 0;
125
f35e5800
RK
126 /* dump the global preferences */
127 cursor = trackdb_opencursor(trackdb_globaldb, tid);
128 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
129 DB_FIRST);
130 while(err == 0) {
131 if(fputc('G', fp) < 0
132 || urlencode(s, k.data, k.size)
133 || fputc('\n', fp) < 0
134 || urlencode(s, d.data, d.size)
135 || fputc('\n', fp) < 0)
136 fatal(errno, "error writing to %s", tag);
137 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
138 DB_NEXT);
139 }
140 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
141 cursor = 0;
142
460b9539 143 if(tracksdb) {
144 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
145 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
146 DB_FIRST);
147 while(err == 0) {
148 if(fputc('T', fp) < 0
149 || urlencode(s, k.data, k.size)
150 || fputc('\n', fp) < 0
151 || urlencode(s, d.data, d.size)
152 || fputc('\n', fp) < 0)
153 fatal(errno, "error writing to %s", tag);
154 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
155 DB_NEXT);
156 }
157 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
158 cursor = 0;
159 }
160
161 if(searchdb) {
162 cursor = trackdb_opencursor(trackdb_searchdb, tid);
163 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
164 DB_FIRST);
165 while(err == 0) {
166 if(fputc('S', fp) < 0
167 || urlencode(s, k.data, k.size)
168 || fputc('\n', fp) < 0
169 || urlencode(s, d.data, d.size)
170 || fputc('\n', fp) < 0)
171 fatal(errno, "error writing to %s", tag);
172 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
173 DB_NEXT);
174 }
175 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } cursor = 0;
176 }
177
178 if(fputs("E\n", fp) < 0) fatal(errno, "error writing to %s", tag);
179 if(err == DB_LOCK_DEADLOCK) {
180 error(0, "c->c_get: %s", db_strerror(err));
181 goto fail;
182 }
183 if(err && err != DB_NOTFOUND)
184 fatal(0, "cursor->c_get: %s", db_strerror(err));
185 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
186 break;
187fail:
188 trackdb_closecursor(cursor);
189 cursor = 0;
190 info("aborting transaction and retrying dump");
191 trackdb_abort_transaction(tid);
192 }
193 trackdb_commit_transaction(tid);
194 if(fflush(fp) < 0) fatal(errno, "error writing to %s", tag);
195 /* caller might not be paranoid so we are paranoid on their behalf */
196 if(fsync(fileno(fp)) < 0) fatal(errno, "error syncing %s", tag);
197}
198
199/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
200static int remove_aliases(DB_TXN *tid, int remove_pathless) {
201 DBC *cursor;
202 int err;
203 DBT k, d;
204 struct kvp *data;
205 int alias, pathless;
206
207 info("removing aliases");
208 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
209 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
210 DB_FIRST)) == DB_LOCK_DEADLOCK) {
211 error(0, "cursor->c_get: %s", db_strerror(err));
212 goto done;
213 }
214 while(err == 0) {
215 data = kvp_urldecode(d.data, d.size);
216 alias = !!kvp_get(data, "_alias_for");
217 pathless = !kvp_get(data, "_path");
218 if(pathless && !remove_pathless)
219 info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
220 if(alias || (remove_pathless && pathless)) {
221 switch(err = cursor->c_del(cursor, 0)) {
222 case 0: break;
223 case DB_LOCK_DEADLOCK:
224 error(0, "cursor->c_get: %s", db_strerror(err));
225 goto done;
226 default:
227 fatal(0, "cursor->c_del: %s", db_strerror(err));
228 }
229 }
230 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
231 }
232 if(err == DB_LOCK_DEADLOCK) {
233 error(0, "cursor operation: %s", db_strerror(err));
234 goto done;
235 }
236 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
237 err = 0;
238done:
239 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
240 return err;
241}
242
243/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
244static int truncdb(DB_TXN *tid, DB *db) {
245 int err;
246 u_int32_t count;
247
248 switch(err = db->truncate(db, tid, &count, 0)) {
249 case 0: break;
250 case DB_LOCK_DEADLOCK:
251 error(0, "db->truncate: %s", db_strerror(err));
252 break;
253 default:
254 fatal(0, "db->truncate: %s", db_strerror(err));
255 }
256 return err;
257}
258
259/* read a DBT from FP, return 0 on success or -1 on input error */
260static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
261 char *s;
262 struct dynstr d;
263
264 if(inputline(tag, fp, &s, '\n')) return -1;
265 dynstr_init(&d);
266 if(urldecode(sink_dynstr(&d), s, strlen(s)))
267 fatal(0, "invalid URL-encoded data in %s", tag);
268 dbt->data = d.vec;
269 dbt->size = d.nvec;
270 return 0;
271}
272
273/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
274static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
275 int err, c;
276 DBT k, d;
f35e5800
RK
277 const char *which_name;
278 DB *which_db;
460b9539 279
280 info("undumping");
281 if(fseek(fp, 0, SEEK_SET) < 0)
282 fatal(errno, "error calling fseek on %s", tag);
283 if((err = truncdb(tid, trackdb_prefsdb))) return err;
284 if((err = truncdb(tid, trackdb_searchdb))) return err;
285 c = getc(fp);
286 while(!ferror(fp) && !feof(fp)) {
287 switch(c) {
288 case 'V':
289 c = getc(fp);
290 if(c != '0')
291 fatal(0, "unknown version '%c'", c);
292 break;
293 case 'E':
294 return 0;
295 case 'P':
f35e5800
RK
296 case 'G':
297 if(c == 'P') {
298 which_db = trackdb_prefsdb;
299 which_name = "prefs.db";
300 } else {
301 which_db = trackdb_globaldb;
302 which_name = "global.db";
303 }
460b9539 304 if(undump_dbt(fp, tag, prepare_data(&k))
305 || undump_dbt(fp, tag, prepare_data(&d)))
306 break;
f35e5800 307 switch(err = trackdb_prefsdb->put(which_db, tid, &k, &d, 0)) {
460b9539 308 case 0:
309 break;
310 case DB_LOCK_DEADLOCK:
f35e5800 311 error(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 312 return err;
313 default:
f35e5800 314 fatal(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 315 }
316 break;
317 case 'T':
318 case 'S':
319 if(undump_dbt(fp, tag, prepare_data(&k))
320 || undump_dbt(fp, tag, prepare_data(&d)))
321 break;
322 /* We don't restore the tracks.db or search.db entries, instead
323 * we recompute them */
324 break;
325 case '\n':
326 break;
327 }
328 c = getc(fp);
329 }
330 if(ferror(fp))
331 fatal(errno, "error reading %s", tag);
332 else
333 fatal(0, "unexpected EOF reading %s", tag);
334 return 0;
335}
336
337/* recompute aliases and search database from prefs, return 0 or
338 * DB_LOCK_DEADLOCK */
339static int recompute_aliases(DB_TXN *tid) {
340 DBC *cursor;
341 DBT k, d;
342 int err;
343 struct kvp *data;
344 const char *path, *track;
345
346 info("recomputing aliases");
347 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
348 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
349 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
350 while(err == 0) {
351 data = kvp_urldecode(d.data, d.size);
352 track = xstrndup(k.data, k.size);
353 if(!kvp_get(data, "_alias_for")) {
354 if(!(path = kvp_get(data, "_path")))
355 error(0, "%s is not an alias but has no path", utf82mb(track));
356 else
357 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
358 goto done;
359 }
360 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
361 DB_NEXT);
362 }
363 switch(err) {
364 case 0:
365 break;
366 case DB_NOTFOUND:
367 err = 0;
368 break;
369 case DB_LOCK_DEADLOCK:
370 break;
371 default:
372 fatal(0, "cursor->c_get: %s", db_strerror(err));
373 }
374done:
375 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
376 return err;
377}
378
379/* restore prefs from FP */
380static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
381 DB_TXN *tid;
382
383 for(;;) {
384 tid = trackdb_begin_transaction();
385 if(remove_aliases(tid, remove_pathless)
386 || undump_from_fp(tid, fp, tag)
387 || recompute_aliases(tid)) goto fail;
388 break;
389fail:
390 info("aborting transaction and retrying undump");
391 trackdb_abort_transaction(tid);
392 }
393 info("committing undump");
394 trackdb_commit_transaction(tid);
395}
396
397/* just recompute alisaes */
398static void do_recompute(int remove_pathless) {
399 DB_TXN *tid;
400
401 for(;;) {
402 tid = trackdb_begin_transaction();
403 if(remove_aliases(tid, remove_pathless)
404 || recompute_aliases(tid)) goto fail;
405 break;
406fail:
407 info("aborting transaction and retrying recomputation");
408 trackdb_abort_transaction(tid);
409 }
410 info("committing recomputed aliases");
411 trackdb_commit_transaction(tid);
412}
413
414int main(int argc, char **argv) {
415 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
416 int tracksdb = 0, searchdb = 0, remove_pathless = 0;
417 const char *path;
418 char *tmp;
419 FILE *fp;
420
320598d4 421 mem_init();
460b9539 422 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
423 switch(n) {
424 case 'h': help();
425 case 'V': version();
426 case 'c': configfile = optarg; break;
427 case 'd': dump = 1; break;
428 case 'u': undump = 1; break;
429 case 'D': debugging = 1; break;
430 case 't': tracksdb = 1; break;
431 case 's': searchdb = 1; break;
432 case 'r': recover = TRACKDB_NORMAL_RECOVER;
433 case 'R': recover = TRACKDB_FATAL_RECOVER;
434 case 'a': recompute = 1; break;
435 case 'P': remove_pathless = 1; break;
436 default: fatal(0, "invalid option");
437 }
438 }
439 if(dump + undump + recompute != 1)
440 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
441 if((undump || recompute) && (tracksdb || searchdb))
442 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
443 if(recompute) {
444 if(optind != argc)
445 fatal(0, "--recompute-aliases does not take a filename");
446 path = 0;
447 } else {
448 if(optind >= argc)
449 fatal(0, "missing dump file name");
450 if(optind + 1 < argc)
451 fatal(0, "specify only a dump file name");
452 path = argv[optind];
453 }
c00fce3a 454 if(config_read(0)) fatal(0, "cannot read configuration");
460b9539 455 trackdb_init(recover);
d25c4615 456 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 457 if(dump) {
458 /* we write to a temporary file and rename into place */
459 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
460 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
461 do_dump(fp, tmp, tracksdb, searchdb);
462 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
463 if(rename(tmp, path) < 0)
464 fatal(errno, "error renaming %s to %s", tmp, path);
465 } else if(undump) {
466 /* the databases or logfiles might end up with wrong permissions
467 * if new ones are created */
468 if(getuid() == 0) info("you might need to chown database files");
469 if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path);
470 do_undump(fp, path, remove_pathless);
471 xfclose(fp);
472 } else if(recompute) {
473 do_recompute(remove_pathless);
474 }
475 trackdb_close();
476 trackdb_deinit();
477 return 0;
478}
479
480/*
481Local Variables:
482c-basic-offset:2
483comment-column:40
484End:
485*/