chiark / gitweb /
Concentrate knowledge about the `pcre' API in one place.
[disorder] / server / dump.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
8a886602 3 * Copyright (C) 2004, 2005, 2007-2011 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file server/dump.c
19 * @brief Dump and restore database contents
20 */
05b75f8d 21#include "disorder-server.h"
460b9539 22
e5af9e8d
RK
23enum {
24 NO_SETUID = UCHAR_MAX + 1,
25};
26
460b9539 27static const struct option options[] = {
28 { "help", no_argument, 0, 'h' },
29 { "version", no_argument, 0, 'V' },
30 { "config", required_argument, 0, 'c' },
31 { "dump", no_argument, 0, 'd' },
32 { "undump", no_argument, 0, 'u' },
33 { "debug", no_argument, 0, 'D' },
34 { "recover", no_argument, 0, 'r' },
35 { "recover-fatal", no_argument, 0, 'R' },
460b9539 36 { "recompute-aliases", no_argument, 0, 'a' },
37 { "remove-pathless", no_argument, 0, 'P' },
e5af9e8d 38 { "no-setuid", no_argument, 0, NO_SETUID },
460b9539 39 { 0, 0, 0, 0 }
40};
41
42/* display usage message and terminate */
43static void help(void) {
44 xprintf("Usage:\n"
45 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
46 " disorder-dump [OPTIONS] --recompute-aliases\n"
47 "Options:\n"
48 " --help, -h Display usage message\n"
49 " --version, -V Display version number\n"
50 " --config PATH, -c PATH Set configuration file\n"
51 " --dump, -d Dump state to PATH\n"
52 " --undump, -u Restore state from PATH\n"
53 " --recover, -r Run database recovery\n"
54 " --recompute-aliases, -a Recompute aliases\n"
55 " --remove-pathless, -P Remove pathless tracks\n"
e5af9e8d 56 " --no-setuid Do not become jukebox user\n"
460b9539 57 " --debug Debug mode\n");
58 xfclose(stdout);
59 exit(0);
60}
61
b3074bd1
RK
62/** @brief Dump one record
63 * @param s Output stream
64 * @param tag Tag for error messages
65 * @param letter Prefix leter for dumped record
66 * @param dbname Database name
67 * @param db Database handle
68 * @param tid Transaction handle
69 * @return 0 or @c DB_LOCK_DEADLOCK
70 */
71static int dump_one(struct sink *s,
72 const char *tag,
73 int letter,
74 const char *dbname,
75 DB *db,
76 DB_TXN *tid) {
77 int err;
78 DBC *cursor;
79 DBT k, d;
80
81 /* dump the preferences */
82 cursor = trackdb_opencursor(db, tid);
83 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
84 DB_FIRST);
85 while(err == 0) {
86 if(sink_writec(s, letter) < 0
87 || urlencode(s, k.data, k.size)
88 || sink_writec(s, '\n') < 0
89 || urlencode(s, d.data, d.size)
90 || sink_writec(s, '\n') < 0)
2e9ba080 91 disorder_fatal(errno, "error writing to %s", tag);
b3074bd1
RK
92 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
93 DB_NEXT);
94 }
95 switch(err) {
96 case DB_LOCK_DEADLOCK:
97 trackdb_closecursor(cursor);
98 return err;
99 case DB_NOTFOUND:
100 return trackdb_closecursor(cursor);
101 case 0:
102 assert(!"cannot happen");
103 default:
2e9ba080 104 disorder_fatal(0, "error reading %s: %s", dbname, db_strerror(err));
b3074bd1
RK
105 }
106}
107
108static struct {
109 int letter;
110 const char *dbname;
111 DB **db;
112} dbtable[] = {
113 { 'P', "prefs.db", &trackdb_prefsdb },
114 { 'G', "global.db", &trackdb_globaldb },
115 { 'U', "users.db", &trackdb_usersdb },
116 { 'W', "schedule.db", &trackdb_scheduledb },
117 { 'L', "playlists.db", &trackdb_playlistsdb },
118 /* avoid 'T' and 'S' for now */
119};
120#define NDBTABLE (sizeof dbtable / sizeof *dbtable)
121
460b9539 122/* dump prefs to FP, return nonzero on error */
b3074bd1 123static void do_dump(FILE *fp, const char *tag) {
460b9539 124 DB_TXN *tid;
125 struct sink *s = sink_stdio(tag, fp);
460b9539 126
127 for(;;) {
128 tid = trackdb_begin_transaction();
129 if(fseek(fp, 0, SEEK_SET) < 0)
2e9ba080 130 disorder_fatal(errno, "error calling fseek");
460b9539 131 if(fflush(fp) < 0)
2e9ba080 132 disorder_fatal(errno, "error calling fflush");
460b9539 133 if(ftruncate(fileno(fp), 0) < 0)
2e9ba080 134 disorder_fatal(errno, "error calling ftruncate");
b3074bd1 135 if(fprintf(fp, "V0") < 0)
2e9ba080 136 disorder_fatal(errno, "error writing to %s", tag);
b3074bd1
RK
137 for(size_t n = 0; n < NDBTABLE; ++n)
138 if(dump_one(s, tag,
139 dbtable[n].letter, dbtable[n].dbname, *dbtable[n].db,
140 tid))
141 goto fail;
f35e5800 142
b3074bd1 143 if(fputs("E\n", fp) < 0)
2e9ba080 144 disorder_fatal(errno, "error writing to %s", tag);
460b9539 145 break;
146fail:
2e9ba080 147 disorder_info("aborting transaction and retrying dump");
460b9539 148 trackdb_abort_transaction(tid);
149 }
150 trackdb_commit_transaction(tid);
2e9ba080 151 if(fflush(fp) < 0) disorder_fatal(errno, "error writing to %s", tag);
460b9539 152 /* caller might not be paranoid so we are paranoid on their behalf */
2e9ba080 153 if(fsync(fileno(fp)) < 0) disorder_fatal(errno, "error syncing %s", tag);
460b9539 154}
155
156/* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
157static int remove_aliases(DB_TXN *tid, int remove_pathless) {
158 DBC *cursor;
159 int err;
160 DBT k, d;
161 struct kvp *data;
162 int alias, pathless;
163
2e9ba080 164 disorder_info("removing aliases");
460b9539 165 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
166 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
167 DB_FIRST)) == DB_LOCK_DEADLOCK) {
2e9ba080 168 disorder_error(0, "cursor->c_get: %s", db_strerror(err));
460b9539 169 goto done;
170 }
171 while(err == 0) {
172 data = kvp_urldecode(d.data, d.size);
173 alias = !!kvp_get(data, "_alias_for");
174 pathless = !kvp_get(data, "_path");
175 if(pathless && !remove_pathless)
2e9ba080 176 disorder_info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
460b9539 177 if(alias || (remove_pathless && pathless)) {
178 switch(err = cursor->c_del(cursor, 0)) {
179 case 0: break;
180 case DB_LOCK_DEADLOCK:
2e9ba080 181 disorder_error(0, "cursor->c_get: %s", db_strerror(err));
460b9539 182 goto done;
183 default:
2e9ba080 184 disorder_fatal(0, "cursor->c_del: %s", db_strerror(err));
460b9539 185 }
186 }
187 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
188 }
189 if(err == DB_LOCK_DEADLOCK) {
2e9ba080 190 disorder_error(0, "cursor operation: %s", db_strerror(err));
460b9539 191 goto done;
192 }
2e9ba080
RK
193 if(err != DB_NOTFOUND)
194 disorder_fatal(0, "cursor->c_get: %s", db_strerror(err));
460b9539 195 err = 0;
196done:
197 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
198 return err;
199}
200
201/* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
202static int truncdb(DB_TXN *tid, DB *db) {
203 int err;
204 u_int32_t count;
205
206 switch(err = db->truncate(db, tid, &count, 0)) {
207 case 0: break;
208 case DB_LOCK_DEADLOCK:
2e9ba080 209 disorder_error(0, "db->truncate: %s", db_strerror(err));
460b9539 210 break;
211 default:
2e9ba080 212 disorder_fatal(0, "db->truncate: %s", db_strerror(err));
460b9539 213 }
214 return err;
215}
216
217/* read a DBT from FP, return 0 on success or -1 on input error */
218static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
219 char *s;
220 struct dynstr d;
221
222 if(inputline(tag, fp, &s, '\n')) return -1;
223 dynstr_init(&d);
224 if(urldecode(sink_dynstr(&d), s, strlen(s)))
2e9ba080 225 disorder_fatal(0, "invalid URL-encoded data in %s", tag);
460b9539 226 dbt->data = d.vec;
227 dbt->size = d.nvec;
228 return 0;
229}
230
231/* undump from FP, return 0 or DB_LOCK_DEADLOCK */
232static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
233 int err, c;
460b9539 234
2e9ba080 235 disorder_info("undumping");
460b9539 236 if(fseek(fp, 0, SEEK_SET) < 0)
2e9ba080 237 disorder_fatal(errno, "error calling fseek on %s", tag);
460b9539 238 if((err = truncdb(tid, trackdb_prefsdb))) return err;
fb1bc1f5 239 if((err = truncdb(tid, trackdb_globaldb))) return err;
460b9539 240 if((err = truncdb(tid, trackdb_searchdb))) return err;
fb1bc1f5 241 if((err = truncdb(tid, trackdb_tagsdb))) return err;
a745dd43 242 if((err = truncdb(tid, trackdb_usersdb))) return err;
0e464e60 243 if((err = truncdb(tid, trackdb_scheduledb))) return err;
460b9539 244 c = getc(fp);
245 while(!ferror(fp) && !feof(fp)) {
b3074bd1
RK
246 for(size_t n = 0; n < NDBTABLE; ++n) {
247 if(dbtable[n].letter == c) {
248 DB *db = *dbtable[n].db;
249 const char *dbname = dbtable[n].dbname;
250 DBT k, d;
251
252 if(undump_dbt(fp, tag, prepare_data(&k))
253 || undump_dbt(fp, tag, prepare_data(&d)))
254 break;
255 switch(err = db->put(db, tid, &k, &d, 0)) {
256 case 0:
257 break;
258 case DB_LOCK_DEADLOCK:
2e9ba080 259 disorder_error(0, "error updating %s: %s", dbname, db_strerror(err));
b3074bd1
RK
260 return err;
261 default:
2e9ba080 262 disorder_fatal(0, "error updating %s: %s", dbname, db_strerror(err));
b3074bd1
RK
263 }
264 goto next;
265 }
266 }
267
460b9539 268 switch(c) {
269 case 'V':
270 c = getc(fp);
271 if(c != '0')
2e9ba080 272 disorder_fatal(0, "unknown version '%c'", c);
460b9539 273 break;
274 case 'E':
275 return 0;
460b9539 276 case '\n':
277 break;
b3074bd1
RK
278 default:
279 if(c >= 32 && c <= 126)
2e9ba080 280 disorder_fatal(0, "unexpected character '%c'", c);
b3074bd1 281 else
2e9ba080 282 disorder_fatal(0, "unexpected character 0x%02X", c);
460b9539 283 }
b3074bd1 284 next:
460b9539 285 c = getc(fp);
286 }
287 if(ferror(fp))
2e9ba080 288 disorder_fatal(errno, "error reading %s", tag);
460b9539 289 else
2e9ba080 290 disorder_fatal(0, "unexpected EOF reading %s", tag);
460b9539 291 return 0;
292}
293
294/* recompute aliases and search database from prefs, return 0 or
295 * DB_LOCK_DEADLOCK */
296static int recompute_aliases(DB_TXN *tid) {
297 DBC *cursor;
298 DBT k, d;
299 int err;
300 struct kvp *data;
301 const char *path, *track;
302
2e9ba080 303 disorder_info("recomputing aliases");
460b9539 304 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
305 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
306 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
307 while(err == 0) {
308 data = kvp_urldecode(d.data, d.size);
309 track = xstrndup(k.data, k.size);
310 if(!kvp_get(data, "_alias_for")) {
311 if(!(path = kvp_get(data, "_path")))
2e9ba080 312 disorder_error(0, "%s is not an alias but has no path", utf82mb(track));
460b9539 313 else
314 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
315 goto done;
316 }
317 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
318 DB_NEXT);
319 }
320 switch(err) {
321 case 0:
322 break;
323 case DB_NOTFOUND:
324 err = 0;
325 break;
326 case DB_LOCK_DEADLOCK:
327 break;
328 default:
2e9ba080 329 disorder_fatal(0, "cursor->c_get: %s", db_strerror(err));
460b9539 330 }
331done:
332 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
333 return err;
334}
335
336/* restore prefs from FP */
337static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
338 DB_TXN *tid;
339
340 for(;;) {
341 tid = trackdb_begin_transaction();
342 if(remove_aliases(tid, remove_pathless)
343 || undump_from_fp(tid, fp, tag)
344 || recompute_aliases(tid)) goto fail;
345 break;
346fail:
2e9ba080 347 disorder_info("aborting transaction and retrying undump");
460b9539 348 trackdb_abort_transaction(tid);
349 }
2e9ba080 350 disorder_info("committing undump");
460b9539 351 trackdb_commit_transaction(tid);
352}
353
354/* just recompute alisaes */
355static void do_recompute(int remove_pathless) {
356 DB_TXN *tid;
357
358 for(;;) {
359 tid = trackdb_begin_transaction();
360 if(remove_aliases(tid, remove_pathless)
361 || recompute_aliases(tid)) goto fail;
362 break;
363fail:
2e9ba080 364 disorder_info("aborting transaction and retrying recomputation");
460b9539 365 trackdb_abort_transaction(tid);
366 }
2e9ba080 367 disorder_info("committing recomputed aliases");
460b9539 368 trackdb_commit_transaction(tid);
369}
370
371int main(int argc, char **argv) {
372 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
b3074bd1 373 int remove_pathless = 0, fd;
e5af9e8d 374 int changeuid = !getuid();
460b9539 375 const char *path;
376 char *tmp;
377 FILE *fp;
378
320598d4 379 mem_init();
e5af9e8d 380 while((n = getopt_long(argc, argv, "hVc:dDurRaPR", options, 0)) >= 0) {
460b9539 381 switch(n) {
382 case 'h': help();
3fbdc96d 383 case 'V': version("disorder-dump");
460b9539 384 case 'c': configfile = optarg; break;
385 case 'd': dump = 1; break;
386 case 'u': undump = 1; break;
387 case 'D': debugging = 1; break;
375d9478
RK
388 case 'r': recover = TRACKDB_NORMAL_RECOVER; break;
389 case 'R': recover = TRACKDB_FATAL_RECOVER; break;
460b9539 390 case 'a': recompute = 1; break;
391 case 'P': remove_pathless = 1; break;
e5af9e8d 392 case NO_SETUID: changeuid = 0; break;
2e9ba080 393 default: disorder_fatal(0, "invalid option");
460b9539 394 }
395 }
396 if(dump + undump + recompute != 1)
2e9ba080 397 disorder_fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
460b9539 398 if(recompute) {
399 if(optind != argc)
2e9ba080 400 disorder_fatal(0, "--recompute-aliases does not take a filename");
460b9539 401 path = 0;
402 } else {
403 if(optind >= argc)
2e9ba080 404 disorder_fatal(0, "missing dump file name");
460b9539 405 if(optind + 1 < argc)
2e9ba080 406 disorder_fatal(0, "specify only a dump file name");
460b9539 407 path = argv[optind];
408 }
2e9ba080
RK
409 if(config_read(0, NULL))
410 disorder_fatal(0, "cannot read configuration");
460b9539 411 if(dump) {
a745dd43
RK
412 /* We write to a temporary file and rename into place. We make
413 * sure the permissions are tight from the start. */
460b9539 414 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
a745dd43 415 if((fd = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0)
2e9ba080 416 disorder_fatal(errno, "error opening %s", tmp);
a745dd43 417 if(!(fp = fdopen(fd, "w")))
2e9ba080 418 disorder_fatal(errno, "fdopen on %s", tmp);
df048ebd
RK
419 trackdb_init(recover|TRACKDB_MAY_CREATE);
420 trackdb_open(TRACKDB_NO_UPGRADE);
b3074bd1 421 do_dump(fp, tmp);
2e9ba080 422 if(fclose(fp) < 0) disorder_fatal(errno, "error closing %s", tmp);
460b9539 423 if(rename(tmp, path) < 0)
2e9ba080 424 disorder_fatal(errno, "error renaming %s to %s", tmp, path);
460b9539 425 } else if(undump) {
e5af9e8d
RK
426 /* Open the dump file before changing UID */
427 if(!(fp = fopen(path, "r")))
428 disorder_fatal(errno, "error opening %s", path);
429 if(changeuid)
430 become_mortal();
460b9539 431 /* the databases or logfiles might end up with wrong permissions
432 * if new ones are created */
2e9ba080
RK
433 if(getuid() == 0)
434 disorder_info("you might need to chown database files");
df048ebd
RK
435 trackdb_init(recover|TRACKDB_MAY_CREATE);
436 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 437 do_undump(fp, path, remove_pathless);
438 xfclose(fp);
439 } else if(recompute) {
440 do_recompute(remove_pathless);
441 }
442 trackdb_close();
8cd7d4bc 443 trackdb_deinit(NULL);
460b9539 444 return 0;
445}
446
447/*
448Local Variables:
449c-basic-offset:2
450comment-column:40
451End:
452*/