chiark / gitweb /
tests for cache.c
[disorder] / server / dump.c
... / ...
CommitLineData
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);
109 cursor = trackdb_opencursor(trackdb_prefsdb, tid);
110 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
111 DB_FIRST);
112 while(err == 0) {
113 if(fputc('P', fp) < 0
114 || urlencode(s, k.data, k.size)
115 || fputc('\n', fp) < 0
116 || urlencode(s, d.data, d.size)
117 || fputc('\n', fp) < 0)
118 fatal(errno, "error writing to %s", tag);
119 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
120 DB_NEXT);
121 }
122 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
123 cursor = 0;
124
125 if(tracksdb) {
126 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
127 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
128 DB_FIRST);
129 while(err == 0) {
130 if(fputc('T', fp) < 0
131 || urlencode(s, k.data, k.size)
132 || fputc('\n', fp) < 0
133 || urlencode(s, d.data, d.size)
134 || fputc('\n', fp) < 0)
135 fatal(errno, "error writing to %s", tag);
136 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
137 DB_NEXT);
138 }
139 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
140 cursor = 0;
141 }
142
143 if(searchdb) {
144 cursor = trackdb_opencursor(trackdb_searchdb, tid);
145 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
146 DB_FIRST);
147 while(err == 0) {
148 if(fputc('S', 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; } cursor = 0;
158 }
159
160 if(fputs("E\n", fp) < 0) fatal(errno, "error writing to %s", tag);
161 if(err == DB_LOCK_DEADLOCK) {
162 error(0, "c->c_get: %s", db_strerror(err));
163 goto fail;
164 }
165 if(err && err != DB_NOTFOUND)
166 fatal(0, "cursor->c_get: %s", db_strerror(err));
167 if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
168 break;
169fail:
170 trackdb_closecursor(cursor);
171 cursor = 0;
172 info("aborting transaction and retrying dump");
173 trackdb_abort_transaction(tid);
174 }
175 trackdb_commit_transaction(tid);
176 if(fflush(fp) < 0) fatal(errno, "error writing to %s", tag);
177 /* caller might not be paranoid so we are paranoid on their behalf */
178 if(fsync(fileno(fp)) < 0) fatal(errno, "error syncing %s", tag);
179}
180
181/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
182static int remove_aliases(DB_TXN *tid, int remove_pathless) {
183 DBC *cursor;
184 int err;
185 DBT k, d;
186 struct kvp *data;
187 int alias, pathless;
188
189 info("removing aliases");
190 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
191 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
192 DB_FIRST)) == DB_LOCK_DEADLOCK) {
193 error(0, "cursor->c_get: %s", db_strerror(err));
194 goto done;
195 }
196 while(err == 0) {
197 data = kvp_urldecode(d.data, d.size);
198 alias = !!kvp_get(data, "_alias_for");
199 pathless = !kvp_get(data, "_path");
200 if(pathless && !remove_pathless)
201 info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
202 if(alias || (remove_pathless && pathless)) {
203 switch(err = cursor->c_del(cursor, 0)) {
204 case 0: break;
205 case DB_LOCK_DEADLOCK:
206 error(0, "cursor->c_get: %s", db_strerror(err));
207 goto done;
208 default:
209 fatal(0, "cursor->c_del: %s", db_strerror(err));
210 }
211 }
212 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
213 }
214 if(err == DB_LOCK_DEADLOCK) {
215 error(0, "cursor operation: %s", db_strerror(err));
216 goto done;
217 }
218 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
219 err = 0;
220done:
221 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
222 return err;
223}
224
225/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
226static int truncdb(DB_TXN *tid, DB *db) {
227 int err;
228 u_int32_t count;
229
230 switch(err = db->truncate(db, tid, &count, 0)) {
231 case 0: break;
232 case DB_LOCK_DEADLOCK:
233 error(0, "db->truncate: %s", db_strerror(err));
234 break;
235 default:
236 fatal(0, "db->truncate: %s", db_strerror(err));
237 }
238 return err;
239}
240
241/* read a DBT from FP, return 0 on success or -1 on input error */
242static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
243 char *s;
244 struct dynstr d;
245
246 if(inputline(tag, fp, &s, '\n')) return -1;
247 dynstr_init(&d);
248 if(urldecode(sink_dynstr(&d), s, strlen(s)))
249 fatal(0, "invalid URL-encoded data in %s", tag);
250 dbt->data = d.vec;
251 dbt->size = d.nvec;
252 return 0;
253}
254
255/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
256static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
257 int err, c;
258 DBT k, d;
259
260 info("undumping");
261 if(fseek(fp, 0, SEEK_SET) < 0)
262 fatal(errno, "error calling fseek on %s", tag);
263 if((err = truncdb(tid, trackdb_prefsdb))) return err;
264 if((err = truncdb(tid, trackdb_searchdb))) return err;
265 c = getc(fp);
266 while(!ferror(fp) && !feof(fp)) {
267 switch(c) {
268 case 'V':
269 c = getc(fp);
270 if(c != '0')
271 fatal(0, "unknown version '%c'", c);
272 break;
273 case 'E':
274 return 0;
275 case 'P':
276 if(undump_dbt(fp, tag, prepare_data(&k))
277 || undump_dbt(fp, tag, prepare_data(&d)))
278 break;
279 switch(err = trackdb_prefsdb->put(trackdb_prefsdb, tid, &k, &d, 0)) {
280 case 0:
281 break;
282 case DB_LOCK_DEADLOCK:
283 error(0, "error updating prefs.db: %s", db_strerror(err));
284 return err;
285 default:
286 fatal(0, "error updating prefs.db: %s", db_strerror(err));
287 }
288 break;
289 case 'T':
290 case 'S':
291 if(undump_dbt(fp, tag, prepare_data(&k))
292 || undump_dbt(fp, tag, prepare_data(&d)))
293 break;
294 /* We don't restore the tracks.db or search.db entries, instead
295 * we recompute them */
296 break;
297 case '\n':
298 break;
299 }
300 c = getc(fp);
301 }
302 if(ferror(fp))
303 fatal(errno, "error reading %s", tag);
304 else
305 fatal(0, "unexpected EOF reading %s", tag);
306 return 0;
307}
308
309/* recompute aliases and search database from prefs, return 0 or
310 * DB_LOCK_DEADLOCK */
311static int recompute_aliases(DB_TXN *tid) {
312 DBC *cursor;
313 DBT k, d;
314 int err;
315 struct kvp *data;
316 const char *path, *track;
317
318 info("recomputing aliases");
319 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
320 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
321 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
322 while(err == 0) {
323 data = kvp_urldecode(d.data, d.size);
324 track = xstrndup(k.data, k.size);
325 if(!kvp_get(data, "_alias_for")) {
326 if(!(path = kvp_get(data, "_path")))
327 error(0, "%s is not an alias but has no path", utf82mb(track));
328 else
329 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
330 goto done;
331 }
332 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
333 DB_NEXT);
334 }
335 switch(err) {
336 case 0:
337 break;
338 case DB_NOTFOUND:
339 err = 0;
340 break;
341 case DB_LOCK_DEADLOCK:
342 break;
343 default:
344 fatal(0, "cursor->c_get: %s", db_strerror(err));
345 }
346done:
347 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
348 return err;
349}
350
351/* restore prefs from FP */
352static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
353 DB_TXN *tid;
354
355 for(;;) {
356 tid = trackdb_begin_transaction();
357 if(remove_aliases(tid, remove_pathless)
358 || undump_from_fp(tid, fp, tag)
359 || recompute_aliases(tid)) goto fail;
360 break;
361fail:
362 info("aborting transaction and retrying undump");
363 trackdb_abort_transaction(tid);
364 }
365 info("committing undump");
366 trackdb_commit_transaction(tid);
367}
368
369/* just recompute alisaes */
370static void do_recompute(int remove_pathless) {
371 DB_TXN *tid;
372
373 for(;;) {
374 tid = trackdb_begin_transaction();
375 if(remove_aliases(tid, remove_pathless)
376 || recompute_aliases(tid)) goto fail;
377 break;
378fail:
379 info("aborting transaction and retrying recomputation");
380 trackdb_abort_transaction(tid);
381 }
382 info("committing recomputed aliases");
383 trackdb_commit_transaction(tid);
384}
385
386int main(int argc, char **argv) {
387 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
388 int tracksdb = 0, searchdb = 0, remove_pathless = 0;
389 const char *path;
390 char *tmp;
391 FILE *fp;
392
393 mem_init();
394 while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) {
395 switch(n) {
396 case 'h': help();
397 case 'V': version();
398 case 'c': configfile = optarg; break;
399 case 'd': dump = 1; break;
400 case 'u': undump = 1; break;
401 case 'D': debugging = 1; break;
402 case 't': tracksdb = 1; break;
403 case 's': searchdb = 1; break;
404 case 'r': recover = TRACKDB_NORMAL_RECOVER;
405 case 'R': recover = TRACKDB_FATAL_RECOVER;
406 case 'a': recompute = 1; break;
407 case 'P': remove_pathless = 1; break;
408 default: fatal(0, "invalid option");
409 }
410 }
411 if(dump + undump + recompute != 1)
412 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
413 if((undump || recompute) && (tracksdb || searchdb))
414 fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases");
415 if(recompute) {
416 if(optind != argc)
417 fatal(0, "--recompute-aliases does not take a filename");
418 path = 0;
419 } else {
420 if(optind >= argc)
421 fatal(0, "missing dump file name");
422 if(optind + 1 < argc)
423 fatal(0, "specify only a dump file name");
424 path = argv[optind];
425 }
426 if(config_read(0)) fatal(0, "cannot read configuration");
427 trackdb_init(recover);
428 trackdb_open(TRACKDB_NO_UPGRADE);
429 if(dump) {
430 /* we write to a temporary file and rename into place */
431 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
432 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
433 do_dump(fp, tmp, tracksdb, searchdb);
434 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
435 if(rename(tmp, path) < 0)
436 fatal(errno, "error renaming %s to %s", tmp, path);
437 } else if(undump) {
438 /* the databases or logfiles might end up with wrong permissions
439 * if new ones are created */
440 if(getuid() == 0) info("you might need to chown database files");
441 if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path);
442 do_undump(fp, path, remove_pathless);
443 xfclose(fp);
444 } else if(recompute) {
445 do_recompute(remove_pathless);
446 }
447 trackdb_close();
448 trackdb_deinit();
449 return 0;
450}
451
452/*
453Local Variables:
454c-basic-offset:2
455comment-column:40
456End:
457*/