chiark / gitweb /
Remove some obsolete code
[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;
155
460b9539 156 if(tracksdb) {
157 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
158 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
159 DB_FIRST);
160 while(err == 0) {
161 if(fputc('T', 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 }
173
174 if(searchdb) {
175 cursor = trackdb_opencursor(trackdb_searchdb, tid);
176 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
177 DB_FIRST);
178 while(err == 0) {
179 if(fputc('S', 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; } cursor = 0;
189 }
190
191 if(fputs("E\n", fp) < 0) fatal(errno, "error writing to %s", tag);
192 if(err == DB_LOCK_DEADLOCK) {
193 error(0, "c->c_get: %s", db_strerror(err));
194 goto fail;
195 }
196 if(err && err != DB_NOTFOUND)
197 fatal(0, "cursor->c_get: %s", db_strerror(err));
198 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
199 break;
200fail:
201 trackdb_closecursor(cursor);
202 cursor = 0;
203 info("aborting transaction and retrying dump");
204 trackdb_abort_transaction(tid);
205 }
206 trackdb_commit_transaction(tid);
207 if(fflush(fp) < 0) fatal(errno, "error writing to %s", tag);
208 /* caller might not be paranoid so we are paranoid on their behalf */
209 if(fsync(fileno(fp)) < 0) fatal(errno, "error syncing %s", tag);
210}
211
212/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
213static int remove_aliases(DB_TXN *tid, int remove_pathless) {
214 DBC *cursor;
215 int err;
216 DBT k, d;
217 struct kvp *data;
218 int alias, pathless;
219
220 info("removing aliases");
221 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
222 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
223 DB_FIRST)) == DB_LOCK_DEADLOCK) {
224 error(0, "cursor->c_get: %s", db_strerror(err));
225 goto done;
226 }
227 while(err == 0) {
228 data = kvp_urldecode(d.data, d.size);
229 alias = !!kvp_get(data, "_alias_for");
230 pathless = !kvp_get(data, "_path");
231 if(pathless && !remove_pathless)
232 info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
233 if(alias || (remove_pathless && pathless)) {
234 switch(err = cursor->c_del(cursor, 0)) {
235 case 0: break;
236 case DB_LOCK_DEADLOCK:
237 error(0, "cursor->c_get: %s", db_strerror(err));
238 goto done;
239 default:
240 fatal(0, "cursor->c_del: %s", db_strerror(err));
241 }
242 }
243 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
244 }
245 if(err == DB_LOCK_DEADLOCK) {
246 error(0, "cursor operation: %s", db_strerror(err));
247 goto done;
248 }
249 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
250 err = 0;
251done:
252 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
253 return err;
254}
255
256/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
257static int truncdb(DB_TXN *tid, DB *db) {
258 int err;
259 u_int32_t count;
260
261 switch(err = db->truncate(db, tid, &count, 0)) {
262 case 0: break;
263 case DB_LOCK_DEADLOCK:
264 error(0, "db->truncate: %s", db_strerror(err));
265 break;
266 default:
267 fatal(0, "db->truncate: %s", db_strerror(err));
268 }
269 return err;
270}
271
272/* read a DBT from FP, return 0 on success or -1 on input error */
273static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
274 char *s;
275 struct dynstr d;
276
277 if(inputline(tag, fp, &s, '\n')) return -1;
278 dynstr_init(&d);
279 if(urldecode(sink_dynstr(&d), s, strlen(s)))
280 fatal(0, "invalid URL-encoded data in %s", tag);
281 dbt->data = d.vec;
282 dbt->size = d.nvec;
283 return 0;
284}
285
286/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
287static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
288 int err, c;
289 DBT k, d;
f35e5800
RK
290 const char *which_name;
291 DB *which_db;
460b9539 292
293 info("undumping");
294 if(fseek(fp, 0, SEEK_SET) < 0)
295 fatal(errno, "error calling fseek on %s", tag);
296 if((err = truncdb(tid, trackdb_prefsdb))) return err;
fb1bc1f5 297 if((err = truncdb(tid, trackdb_globaldb))) return err;
460b9539 298 if((err = truncdb(tid, trackdb_searchdb))) return err;
fb1bc1f5 299 if((err = truncdb(tid, trackdb_tagsdb))) return err;
a745dd43 300 if((err = truncdb(tid, trackdb_usersdb))) return err;
460b9539 301 c = getc(fp);
302 while(!ferror(fp) && !feof(fp)) {
303 switch(c) {
304 case 'V':
305 c = getc(fp);
306 if(c != '0')
307 fatal(0, "unknown version '%c'", c);
308 break;
309 case 'E':
310 return 0;
311 case 'P':
f35e5800 312 case 'G':
a745dd43
RK
313 case 'U':
314 switch(c) {
315 case 'P':
f35e5800
RK
316 which_db = trackdb_prefsdb;
317 which_name = "prefs.db";
a745dd43
RK
318 break;
319 case 'G':
f35e5800
RK
320 which_db = trackdb_globaldb;
321 which_name = "global.db";
a745dd43
RK
322 break;
323 case 'U':
324 which_db = trackdb_usersdb;
325 which_name = "users.db";
326 break;
327 default:
328 abort();
f35e5800 329 }
460b9539 330 if(undump_dbt(fp, tag, prepare_data(&k))
331 || undump_dbt(fp, tag, prepare_data(&d)))
332 break;
f35e5800 333 switch(err = trackdb_prefsdb->put(which_db, tid, &k, &d, 0)) {
460b9539 334 case 0:
335 break;
336 case DB_LOCK_DEADLOCK:
f35e5800 337 error(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 338 return err;
339 default:
f35e5800 340 fatal(0, "error updating %s: %s", which_name, db_strerror(err));
460b9539 341 }
342 break;
343 case 'T':
344 case 'S':
345 if(undump_dbt(fp, tag, prepare_data(&k))
346 || undump_dbt(fp, tag, prepare_data(&d)))
347 break;
348 /* We don't restore the tracks.db or search.db entries, instead
349 * we recompute them */
350 break;
351 case '\n':
352 break;
353 }
354 c = getc(fp);
355 }
356 if(ferror(fp))
357 fatal(errno, "error reading %s", tag);
358 else
359 fatal(0, "unexpected EOF reading %s", tag);
360 return 0;
361}
362
363/* recompute aliases and search database from prefs, return 0 or
364 * DB_LOCK_DEADLOCK */
365static int recompute_aliases(DB_TXN *tid) {
366 DBC *cursor;
367 DBT k, d;
368 int err;
369 struct kvp *data;
370 const char *path, *track;
371
372 info("recomputing aliases");
373 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
374 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
375 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
376 while(err == 0) {
377 data = kvp_urldecode(d.data, d.size);
378 track = xstrndup(k.data, k.size);
379 if(!kvp_get(data, "_alias_for")) {
380 if(!(path = kvp_get(data, "_path")))
381 error(0, "%s is not an alias but has no path", utf82mb(track));
382 else
383 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
384 goto done;
385 }
386 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
387 DB_NEXT);
388 }
389 switch(err) {
390 case 0:
391 break;
392 case DB_NOTFOUND:
393 err = 0;
394 break;
395 case DB_LOCK_DEADLOCK:
396 break;
397 default:
398 fatal(0, "cursor->c_get: %s", db_strerror(err));
399 }
400done:
401 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
402 return err;
403}
404
405/* restore prefs from FP */
406static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
407 DB_TXN *tid;
408
409 for(;;) {
410 tid = trackdb_begin_transaction();
411 if(remove_aliases(tid, remove_pathless)
412 || undump_from_fp(tid, fp, tag)
413 || recompute_aliases(tid)) goto fail;
414 break;
415fail:
416 info("aborting transaction and retrying undump");
417 trackdb_abort_transaction(tid);
418 }
419 info("committing undump");
420 trackdb_commit_transaction(tid);
421}
422
423/* just recompute alisaes */
424static void do_recompute(int remove_pathless) {
425 DB_TXN *tid;
426
427 for(;;) {
428 tid = trackdb_begin_transaction();
429 if(remove_aliases(tid, remove_pathless)
430 || recompute_aliases(tid)) goto fail;
431 break;
432fail:
433 info("aborting transaction and retrying recomputation");
434 trackdb_abort_transaction(tid);
435 }
436 info("committing recomputed aliases");
437 trackdb_commit_transaction(tid);
438}
439
440int main(int argc, char **argv) {
441 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
a745dd43 442 int tracksdb = 0, searchdb = 0, remove_pathless = 0, fd;
460b9539 443 const char *path;
444 char *tmp;
445 FILE *fp;
446
320598d4 447 mem_init();
460b9539 448 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
449 switch(n) {
450 case 'h': help();
3fbdc96d 451 case 'V': version("disorder-dump");
460b9539 452 case 'c': configfile = optarg; break;
453 case 'd': dump = 1; break;
454 case 'u': undump = 1; break;
455 case 'D': debugging = 1; break;
456 case 't': tracksdb = 1; break;
457 case 's': searchdb = 1; break;
458 case 'r': recover = TRACKDB_NORMAL_RECOVER;
459 case 'R': recover = TRACKDB_FATAL_RECOVER;
460 case 'a': recompute = 1; break;
461 case 'P': remove_pathless = 1; break;
462 default: fatal(0, "invalid option");
463 }
464 }
465 if(dump + undump + recompute != 1)
466 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
467 if((undump || recompute) && (tracksdb || searchdb))
468 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
469 if(recompute) {
470 if(optind != argc)
471 fatal(0, "--recompute-aliases does not take a filename");
472 path = 0;
473 } else {
474 if(optind >= argc)
475 fatal(0, "missing dump file name");
476 if(optind + 1 < argc)
477 fatal(0, "specify only a dump file name");
478 path = argv[optind];
479 }
c00fce3a 480 if(config_read(0)) fatal(0, "cannot read configuration");
a745dd43 481 trackdb_init(recover|TRACKDB_MAY_CREATE);
d25c4615 482 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 483 if(dump) {
a745dd43
RK
484 /* We write to a temporary file and rename into place. We make
485 * sure the permissions are tight from the start. */
460b9539 486 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
a745dd43
RK
487 if((fd = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0)
488 fatal(errno, "error opening %s", tmp);
489 if(!(fp = fdopen(fd, "w")))
490 fatal(errno, "fdopen on %s", tmp);
460b9539 491 do_dump(fp, tmp, tracksdb, searchdb);
492 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
493 if(rename(tmp, path) < 0)
494 fatal(errno, "error renaming %s to %s", tmp, path);
495 } else if(undump) {
496 /* the databases or logfiles might end up with wrong permissions
497 * if new ones are created */
498 if(getuid() == 0) info("you might need to chown database files");
499 if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path);
500 do_undump(fp, path, remove_pathless);
501 xfclose(fp);
502 } else if(recompute) {
503 do_recompute(remove_pathless);
504 }
505 trackdb_close();
506 trackdb_deinit();
507 return 0;
508}
509
510/*
511Local Variables:
512c-basic-offset:2
513comment-column:40
514End:
515*/