chiark / gitweb /
Log discarded junk events.
[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
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>
a745dd43 32#include <fcntl.h>
460b9539 33
34#include "configuration.h"
35#include "syscalls.h"
36#include "log.h"
37#include "client.h"
38#include "sink.h"
39#include "mem.h"
40#include "defs.h"
41#include "printf.h"
42#include "kvp.h"
43#include "vector.h"
44#include "inputline.h"
5df73aeb 45#include "rights.h"
460b9539 46#include "trackdb.h"
47#include "trackdb-int.h"
48#include "charset.h"
3fbdc96d 49#include "version.h"
460b9539 50
51static 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' },
64 { 0, 0, 0, 0 }
65};
66
67/* display usage message and terminate */
68static void help(void) {
69 xprintf("Usage:\n"
70 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
71 " disorder-dump [OPTIONS] --recompute-aliases\n"
72 "Options:\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");
82 xfclose(stdout);
83 exit(0);
84}
85
460b9539 86/* dump prefs to FP, return nonzero on error */
87static void do_dump(FILE *fp, const char *tag,
88 int tracksdb, int searchdb) {
89 DBC *cursor = 0;
90 DB_TXN *tid;
91 struct sink *s = sink_stdio(tag, fp);
92 int err;
93 DBT k, d;
94
95 for(;;) {
96 tid = trackdb_begin_transaction();
97 if(fseek(fp, 0, SEEK_SET) < 0)
98 fatal(errno, "error calling fseek");
99 if(fflush(fp) < 0)
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);
f35e5800 105 /* dump the preferences */
460b9539 106 cursor = trackdb_opencursor(trackdb_prefsdb, tid);
107 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
108 DB_FIRST);
109 while(err == 0) {
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),
117 DB_NEXT);
118 }
119 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
120 cursor = 0;
121
f35e5800
RK
122 /* dump the global preferences */
123 cursor = trackdb_opencursor(trackdb_globaldb, tid);
124 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
125 DB_FIRST);
126 while(err == 0) {
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),
134 DB_NEXT);
135 }
136 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
137 cursor = 0;
138
a745dd43
RK
139 /* dump the users */
140 cursor = trackdb_opencursor(trackdb_usersdb, tid);
141 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
142 DB_FIRST);
143 while(err == 0) {
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),
151 DB_NEXT);
152 }
153 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
154 cursor = 0;
0e464e60
RK
155
156 /* dump the schedule */
157 cursor = trackdb_opencursor(trackdb_scheduledb, tid);
158 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
159 DB_FIRST);
160 while(err == 0) {
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),
168 DB_NEXT);
169 }
170 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
171 cursor = 0;
172
a745dd43 173
460b9539 174 if(tracksdb) {
175 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
176 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
177 DB_FIRST);
178 while(err == 0) {
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),
186 DB_NEXT);
187 }
188 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
189 cursor = 0;
190 }
191
192 if(searchdb) {
193 cursor = trackdb_opencursor(trackdb_searchdb, tid);
194 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
195 DB_FIRST);
196 while(err == 0) {
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),
204 DB_NEXT);
205 }
206 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; } cursor = 0;
207 }
208
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));
212 goto fail;
213 }
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; }
217 break;
218fail:
219 trackdb_closecursor(cursor);
220 cursor = 0;
221 info("aborting transaction and retrying dump");
222 trackdb_abort_transaction(tid);
223 }
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);
228}
229
230/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
231static int remove_aliases(DB_TXN *tid, int remove_pathless) {
232 DBC *cursor;
233 int err;
234 DBT k, d;
235 struct kvp *data;
236 int alias, pathless;
237
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));
243 goto done;
244 }
245 while(err == 0) {
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)) {
253 case 0: break;
254 case DB_LOCK_DEADLOCK:
255 error(0, "cursor->c_get: %s", db_strerror(err));
256 goto done;
257 default:
258 fatal(0, "cursor->c_del: %s", db_strerror(err));
259 }
260 }
261 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
262 }
263 if(err == DB_LOCK_DEADLOCK) {
264 error(0, "cursor operation: %s", db_strerror(err));
265 goto done;
266 }
267 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
268 err = 0;
269done:
270 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
271 return err;
272}
273
274/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
275static int truncdb(DB_TXN *tid, DB *db) {
276 int err;
277 u_int32_t count;
278
279 switch(err = db->truncate(db, tid, &count, 0)) {
280 case 0: break;
281 case DB_LOCK_DEADLOCK:
282 error(0, "db->truncate: %s", db_strerror(err));
283 break;
284 default:
285 fatal(0, "db->truncate: %s", db_strerror(err));
286 }
287 return err;
288}
289
290/* read a DBT from FP, return 0 on success or -1 on input error */
291static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
292 char *s;
293 struct dynstr d;
294
295 if(inputline(tag, fp, &s, '\n')) return -1;
296 dynstr_init(&d);
297 if(urldecode(sink_dynstr(&d), s, strlen(s)))
298 fatal(0, "invalid URL-encoded data in %s", tag);
299 dbt->data = d.vec;
300 dbt->size = d.nvec;
301 return 0;
302}
303
304/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
305static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
306 int err, c;
307 DBT k, d;
f35e5800
RK
308 const char *which_name;
309 DB *which_db;
460b9539 310
311 info("undumping");
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;
fb1bc1f5 315 if((err = truncdb(tid, trackdb_globaldb))) return err;
460b9539 316 if((err = truncdb(tid, trackdb_searchdb))) return err;
fb1bc1f5 317 if((err = truncdb(tid, trackdb_tagsdb))) return err;
a745dd43 318 if((err = truncdb(tid, trackdb_usersdb))) return err;
0e464e60 319 if((err = truncdb(tid, trackdb_scheduledb))) return err;
460b9539 320 c = getc(fp);
321 while(!ferror(fp) && !feof(fp)) {
322 switch(c) {
323 case 'V':
324 c = getc(fp);
325 if(c != '0')
326 fatal(0, "unknown version '%c'", c);
327 break;
328 case 'E':
329 return 0;
330 case 'P':
f35e5800 331 case 'G':
a745dd43 332 case 'U':
0e464e60 333 case 'W':
a745dd43
RK
334 switch(c) {
335 case 'P':
f35e5800
RK
336 which_db = trackdb_prefsdb;
337 which_name = "prefs.db";
a745dd43
RK
338 break;
339 case 'G':
f35e5800
RK
340 which_db = trackdb_globaldb;
341 which_name = "global.db";
a745dd43
RK
342 break;
343 case 'U':
344 which_db = trackdb_usersdb;
345 which_name = "users.db";
346 break;
0e464e60
RK
347 case 'W': /* for 'when' */
348 which_db = trackdb_scheduledb;
349 which_name = "scheduledb.db";
350 break;
a745dd43
RK
351 default:
352 abort();
f35e5800 353 }
460b9539 354 if(undump_dbt(fp, tag, prepare_data(&k))
355 || undump_dbt(fp, tag, prepare_data(&d)))
356 break;
0e464e60 357 switch(err = which_db->put(which_db, tid, &k, &d, 0)) {
460b9539 358 case 0:
359 break;
360 case DB_LOCK_DEADLOCK:
f35e5800 361 error(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 362 return err;
363 default:
f35e5800 364 fatal(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 365 }
366 break;
367 case 'T':
368 case 'S':
369 if(undump_dbt(fp, tag, prepare_data(&k))
370 || undump_dbt(fp, tag, prepare_data(&d)))
371 break;
372 /* We don't restore the tracks.db or search.db entries, instead
373 * we recompute them */
374 break;
375 case '\n':
376 break;
377 }
378 c = getc(fp);
379 }
380 if(ferror(fp))
381 fatal(errno, "error reading %s", tag);
382 else
383 fatal(0, "unexpected EOF reading %s", tag);
384 return 0;
385}
386
387/* recompute aliases and search database from prefs, return 0 or
388 * DB_LOCK_DEADLOCK */
389static int recompute_aliases(DB_TXN *tid) {
390 DBC *cursor;
391 DBT k, d;
392 int err;
393 struct kvp *data;
394 const char *path, *track;
395
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;
400 while(err == 0) {
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));
406 else
407 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
408 goto done;
409 }
410 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
411 DB_NEXT);
412 }
413 switch(err) {
414 case 0:
415 break;
416 case DB_NOTFOUND:
417 err = 0;
418 break;
419 case DB_LOCK_DEADLOCK:
420 break;
421 default:
422 fatal(0, "cursor->c_get: %s", db_strerror(err));
423 }
424done:
425 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
426 return err;
427}
428
429/* restore prefs from FP */
430static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
431 DB_TXN *tid;
432
433 for(;;) {
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;
438 break;
439fail:
440 info("aborting transaction and retrying undump");
441 trackdb_abort_transaction(tid);
442 }
443 info("committing undump");
444 trackdb_commit_transaction(tid);
445}
446
447/* just recompute alisaes */
448static void do_recompute(int remove_pathless) {
449 DB_TXN *tid;
450
451 for(;;) {
452 tid = trackdb_begin_transaction();
453 if(remove_aliases(tid, remove_pathless)
454 || recompute_aliases(tid)) goto fail;
455 break;
456fail:
457 info("aborting transaction and retrying recomputation");
458 trackdb_abort_transaction(tid);
459 }
460 info("committing recomputed aliases");
461 trackdb_commit_transaction(tid);
462}
463
464int main(int argc, char **argv) {
465 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
a745dd43 466 int tracksdb = 0, searchdb = 0, remove_pathless = 0, fd;
460b9539 467 const char *path;
468 char *tmp;
469 FILE *fp;
470
320598d4 471 mem_init();
460b9539 472 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
473 switch(n) {
474 case 'h': help();
3fbdc96d 475 case 'V': version("disorder-dump");
460b9539 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");
487 }
488 }
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");
493 if(recompute) {
494 if(optind != argc)
495 fatal(0, "--recompute-aliases does not take a filename");
496 path = 0;
497 } else {
498 if(optind >= argc)
499 fatal(0, "missing dump file name");
500 if(optind + 1 < argc)
501 fatal(0, "specify only a dump file name");
502 path = argv[optind];
503 }
c00fce3a 504 if(config_read(0)) fatal(0, "cannot read configuration");
a745dd43 505 trackdb_init(recover|TRACKDB_MAY_CREATE);
d25c4615 506 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 507 if(dump) {
a745dd43
RK
508 /* We write to a temporary file and rename into place. We make
509 * sure the permissions are tight from the start. */
460b9539 510 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
a745dd43
RK
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);
460b9539 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);
519 } else if(undump) {
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);
525 xfclose(fp);
526 } else if(recompute) {
527 do_recompute(remove_pathless);
528 }
529 trackdb_close();
530 trackdb_deinit();
531 return 0;
532}
533
534/*
535Local Variables:
536c-basic-offset:2
537comment-column:40
538End:
539*/