Commit | Line | Data |
---|---|---|
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 | ||
48 | static 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 */ | |
65 | static 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 */ | |
84 | static 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 */ | |
91 | static 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; | |
187 | fail: | |
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 */ | |
200 | static 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; | |
238 | done: | |
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 */ | |
244 | static 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 */ | |
260 | static 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 */ | |
274 | static 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; | |
fb1bc1f5 | 284 | if((err = truncdb(tid, trackdb_globaldb))) return err; |
460b9539 | 285 | if((err = truncdb(tid, trackdb_searchdb))) return err; |
fb1bc1f5 | 286 | if((err = truncdb(tid, trackdb_tagsdb))) return err; |
460b9539 | 287 | c = getc(fp); |
288 | while(!ferror(fp) && !feof(fp)) { | |
289 | switch(c) { | |
290 | case 'V': | |
291 | c = getc(fp); | |
292 | if(c != '0') | |
293 | fatal(0, "unknown version '%c'", c); | |
294 | break; | |
295 | case 'E': | |
296 | return 0; | |
297 | case 'P': | |
f35e5800 RK |
298 | case 'G': |
299 | if(c == 'P') { | |
300 | which_db = trackdb_prefsdb; | |
301 | which_name = "prefs.db"; | |
302 | } else { | |
303 | which_db = trackdb_globaldb; | |
304 | which_name = "global.db"; | |
305 | } | |
460b9539 | 306 | if(undump_dbt(fp, tag, prepare_data(&k)) |
307 | || undump_dbt(fp, tag, prepare_data(&d))) | |
308 | break; | |
f35e5800 | 309 | switch(err = trackdb_prefsdb->put(which_db, tid, &k, &d, 0)) { |
460b9539 | 310 | case 0: |
311 | break; | |
312 | case DB_LOCK_DEADLOCK: | |
f35e5800 | 313 | error(0, "error updating %s: %s", which_name, db_strerror(err)); |
460b9539 | 314 | return err; |
315 | default: | |
f35e5800 | 316 | fatal(0, "error updating %s: %s", which_name, db_strerror(err)); |
460b9539 | 317 | } |
318 | break; | |
319 | case 'T': | |
320 | case 'S': | |
321 | if(undump_dbt(fp, tag, prepare_data(&k)) | |
322 | || undump_dbt(fp, tag, prepare_data(&d))) | |
323 | break; | |
324 | /* We don't restore the tracks.db or search.db entries, instead | |
325 | * we recompute them */ | |
326 | break; | |
327 | case '\n': | |
328 | break; | |
329 | } | |
330 | c = getc(fp); | |
331 | } | |
332 | if(ferror(fp)) | |
333 | fatal(errno, "error reading %s", tag); | |
334 | else | |
335 | fatal(0, "unexpected EOF reading %s", tag); | |
336 | return 0; | |
337 | } | |
338 | ||
339 | /* recompute aliases and search database from prefs, return 0 or | |
340 | * DB_LOCK_DEADLOCK */ | |
341 | static int recompute_aliases(DB_TXN *tid) { | |
342 | DBC *cursor; | |
343 | DBT k, d; | |
344 | int err; | |
345 | struct kvp *data; | |
346 | const char *path, *track; | |
347 | ||
348 | info("recomputing aliases"); | |
349 | cursor = trackdb_opencursor(trackdb_tracksdb, tid); | |
350 | if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), | |
351 | DB_FIRST)) == DB_LOCK_DEADLOCK) goto done; | |
352 | while(err == 0) { | |
353 | data = kvp_urldecode(d.data, d.size); | |
354 | track = xstrndup(k.data, k.size); | |
355 | if(!kvp_get(data, "_alias_for")) { | |
356 | if(!(path = kvp_get(data, "_path"))) | |
357 | error(0, "%s is not an alias but has no path", utf82mb(track)); | |
358 | else | |
359 | if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK) | |
360 | goto done; | |
361 | } | |
362 | err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), | |
363 | DB_NEXT); | |
364 | } | |
365 | switch(err) { | |
366 | case 0: | |
367 | break; | |
368 | case DB_NOTFOUND: | |
369 | err = 0; | |
370 | break; | |
371 | case DB_LOCK_DEADLOCK: | |
372 | break; | |
373 | default: | |
374 | fatal(0, "cursor->c_get: %s", db_strerror(err)); | |
375 | } | |
376 | done: | |
377 | if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK; | |
378 | return err; | |
379 | } | |
380 | ||
381 | /* restore prefs from FP */ | |
382 | static void do_undump(FILE *fp, const char *tag, int remove_pathless) { | |
383 | DB_TXN *tid; | |
384 | ||
385 | for(;;) { | |
386 | tid = trackdb_begin_transaction(); | |
387 | if(remove_aliases(tid, remove_pathless) | |
388 | || undump_from_fp(tid, fp, tag) | |
389 | || recompute_aliases(tid)) goto fail; | |
390 | break; | |
391 | fail: | |
392 | info("aborting transaction and retrying undump"); | |
393 | trackdb_abort_transaction(tid); | |
394 | } | |
395 | info("committing undump"); | |
396 | trackdb_commit_transaction(tid); | |
397 | } | |
398 | ||
399 | /* just recompute alisaes */ | |
400 | static void do_recompute(int remove_pathless) { | |
401 | DB_TXN *tid; | |
402 | ||
403 | for(;;) { | |
404 | tid = trackdb_begin_transaction(); | |
405 | if(remove_aliases(tid, remove_pathless) | |
406 | || recompute_aliases(tid)) goto fail; | |
407 | break; | |
408 | fail: | |
409 | info("aborting transaction and retrying recomputation"); | |
410 | trackdb_abort_transaction(tid); | |
411 | } | |
412 | info("committing recomputed aliases"); | |
413 | trackdb_commit_transaction(tid); | |
414 | } | |
415 | ||
416 | int main(int argc, char **argv) { | |
417 | int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0; | |
418 | int tracksdb = 0, searchdb = 0, remove_pathless = 0; | |
419 | const char *path; | |
420 | char *tmp; | |
421 | FILE *fp; | |
422 | ||
320598d4 | 423 | mem_init(); |
460b9539 | 424 | while((n = getopt_long(argc, argv, "hVc:dDutsrRaP", options, 0)) >= 0) { |
425 | switch(n) { | |
426 | case 'h': help(); | |
427 | case 'V': version(); | |
428 | case 'c': configfile = optarg; break; | |
429 | case 'd': dump = 1; break; | |
430 | case 'u': undump = 1; break; | |
431 | case 'D': debugging = 1; break; | |
432 | case 't': tracksdb = 1; break; | |
433 | case 's': searchdb = 1; break; | |
434 | case 'r': recover = TRACKDB_NORMAL_RECOVER; | |
435 | case 'R': recover = TRACKDB_FATAL_RECOVER; | |
436 | case 'a': recompute = 1; break; | |
437 | case 'P': remove_pathless = 1; break; | |
438 | default: fatal(0, "invalid option"); | |
439 | } | |
440 | } | |
441 | if(dump + undump + recompute != 1) | |
442 | fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases"); | |
443 | if((undump || recompute) && (tracksdb || searchdb)) | |
444 | fatal(0, "--trackdb and --searchdb with --undump or --recompute-aliases"); | |
445 | if(recompute) { | |
446 | if(optind != argc) | |
447 | fatal(0, "--recompute-aliases does not take a filename"); | |
448 | path = 0; | |
449 | } else { | |
450 | if(optind >= argc) | |
451 | fatal(0, "missing dump file name"); | |
452 | if(optind + 1 < argc) | |
453 | fatal(0, "specify only a dump file name"); | |
454 | path = argv[optind]; | |
455 | } | |
c00fce3a | 456 | if(config_read(0)) fatal(0, "cannot read configuration"); |
460b9539 | 457 | trackdb_init(recover); |
d25c4615 | 458 | trackdb_open(TRACKDB_NO_UPGRADE); |
460b9539 | 459 | if(dump) { |
460 | /* we write to a temporary file and rename into place */ | |
461 | byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid()); | |
462 | if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp); | |
463 | do_dump(fp, tmp, tracksdb, searchdb); | |
464 | if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp); | |
465 | if(rename(tmp, path) < 0) | |
466 | fatal(errno, "error renaming %s to %s", tmp, path); | |
467 | } else if(undump) { | |
468 | /* the databases or logfiles might end up with wrong permissions | |
469 | * if new ones are created */ | |
470 | if(getuid() == 0) info("you might need to chown database files"); | |
471 | if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path); | |
472 | do_undump(fp, path, remove_pathless); | |
473 | xfclose(fp); | |
474 | } else if(recompute) { | |
475 | do_recompute(remove_pathless); | |
476 | } | |
477 | trackdb_close(); | |
478 | trackdb_deinit(); | |
479 | return 0; | |
480 | } | |
481 | ||
482 | /* | |
483 | Local Variables: | |
484 | c-basic-offset:2 | |
485 | comment-column:40 | |
486 | End: | |
487 | */ |