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