2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
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.
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.
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
21 #include "disorder-server.h"
23 static const struct option options[] = {
24 { "help", no_argument, 0, 'h' },
25 { "version", no_argument, 0, 'V' },
26 { "config", required_argument, 0, 'c' },
27 { "dump", no_argument, 0, 'd' },
28 { "undump", no_argument, 0, 'u' },
29 { "debug", no_argument, 0, 'D' },
30 { "recover", no_argument, 0, 'r' },
31 { "recover-fatal", no_argument, 0, 'R' },
32 { "recompute-aliases", no_argument, 0, 'a' },
33 { "remove-pathless", no_argument, 0, 'P' },
37 /* display usage message and terminate */
38 static void help(void) {
40 " disorder-dump [OPTIONS] --dump|--undump PATH\n"
41 " disorder-dump [OPTIONS] --recompute-aliases\n"
43 " --help, -h Display usage message\n"
44 " --version, -V Display version number\n"
45 " --config PATH, -c PATH Set configuration file\n"
46 " --dump, -d Dump state to PATH\n"
47 " --undump, -u Restore state from PATH\n"
48 " --recover, -r Run database recovery\n"
49 " --recompute-aliases, -a Recompute aliases\n"
50 " --remove-pathless, -P Remove pathless tracks\n"
51 " --debug Debug mode\n");
56 /** @brief Dump one record
57 * @param s Output stream
58 * @param tag Tag for error messages
59 * @param letter Prefix leter for dumped record
60 * @param dbname Database name
61 * @param db Database handle
62 * @param tid Transaction handle
63 * @return 0 or @c DB_LOCK_DEADLOCK
65 static int dump_one(struct sink *s,
75 /* dump the preferences */
76 cursor = trackdb_opencursor(db, tid);
77 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
80 if(sink_writec(s, letter) < 0
81 || urlencode(s, k.data, k.size)
82 || sink_writec(s, '\n') < 0
83 || urlencode(s, d.data, d.size)
84 || sink_writec(s, '\n') < 0)
85 fatal(errno, "error writing to %s", tag);
86 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
90 case DB_LOCK_DEADLOCK:
91 trackdb_closecursor(cursor);
94 return trackdb_closecursor(cursor);
96 assert(!"cannot happen");
98 fatal(0, "error reading %s: %s", dbname, db_strerror(err));
107 { 'P', "prefs.db", &trackdb_prefsdb },
108 { 'G', "global.db", &trackdb_globaldb },
109 { 'U', "users.db", &trackdb_usersdb },
110 { 'W', "schedule.db", &trackdb_scheduledb },
111 { 'L', "playlists.db", &trackdb_playlistsdb },
112 /* avoid 'T' and 'S' for now */
114 #define NDBTABLE (sizeof dbtable / sizeof *dbtable)
116 /* dump prefs to FP, return nonzero on error */
117 static void do_dump(FILE *fp, const char *tag) {
119 struct sink *s = sink_stdio(tag, fp);
122 tid = trackdb_begin_transaction();
123 if(fseek(fp, 0, SEEK_SET) < 0)
124 fatal(errno, "error calling fseek");
126 fatal(errno, "error calling fflush");
127 if(ftruncate(fileno(fp), 0) < 0)
128 fatal(errno, "error calling ftruncate");
129 if(fprintf(fp, "V0") < 0)
130 fatal(errno, "error writing to %s", tag);
131 for(size_t n = 0; n < NDBTABLE; ++n)
133 dbtable[n].letter, dbtable[n].dbname, *dbtable[n].db,
137 if(fputs("E\n", fp) < 0)
138 fatal(errno, "error writing to %s", tag);
141 info("aborting transaction and retrying dump");
142 trackdb_abort_transaction(tid);
144 trackdb_commit_transaction(tid);
145 if(fflush(fp) < 0) fatal(errno, "error writing to %s", tag);
146 /* caller might not be paranoid so we are paranoid on their behalf */
147 if(fsync(fileno(fp)) < 0) fatal(errno, "error syncing %s", tag);
150 /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */
151 static int remove_aliases(DB_TXN *tid, int remove_pathless) {
158 info("removing aliases");
159 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
160 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
161 DB_FIRST)) == DB_LOCK_DEADLOCK) {
162 error(0, "cursor->c_get: %s", db_strerror(err));
166 data = kvp_urldecode(d.data, d.size);
167 alias = !!kvp_get(data, "_alias_for");
168 pathless = !kvp_get(data, "_path");
169 if(pathless && !remove_pathless)
170 info("no _path for %s", utf82mb(xstrndup(k.data, k.size)));
171 if(alias || (remove_pathless && pathless)) {
172 switch(err = cursor->c_del(cursor, 0)) {
174 case DB_LOCK_DEADLOCK:
175 error(0, "cursor->c_get: %s", db_strerror(err));
178 fatal(0, "cursor->c_del: %s", db_strerror(err));
181 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
183 if(err == DB_LOCK_DEADLOCK) {
184 error(0, "cursor operation: %s", db_strerror(err));
187 if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err));
190 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
194 /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */
195 static int truncdb(DB_TXN *tid, DB *db) {
199 switch(err = db->truncate(db, tid, &count, 0)) {
201 case DB_LOCK_DEADLOCK:
202 error(0, "db->truncate: %s", db_strerror(err));
205 fatal(0, "db->truncate: %s", db_strerror(err));
210 /* read a DBT from FP, return 0 on success or -1 on input error */
211 static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
215 if(inputline(tag, fp, &s, '\n')) return -1;
217 if(urldecode(sink_dynstr(&d), s, strlen(s)))
218 fatal(0, "invalid URL-encoded data in %s", tag);
224 /* undump from FP, return 0 or DB_LOCK_DEADLOCK */
225 static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
229 if(fseek(fp, 0, SEEK_SET) < 0)
230 fatal(errno, "error calling fseek on %s", tag);
231 if((err = truncdb(tid, trackdb_prefsdb))) return err;
232 if((err = truncdb(tid, trackdb_globaldb))) return err;
233 if((err = truncdb(tid, trackdb_searchdb))) return err;
234 if((err = truncdb(tid, trackdb_tagsdb))) return err;
235 if((err = truncdb(tid, trackdb_usersdb))) return err;
236 if((err = truncdb(tid, trackdb_scheduledb))) return err;
238 while(!ferror(fp) && !feof(fp)) {
239 for(size_t n = 0; n < NDBTABLE; ++n) {
240 if(dbtable[n].letter == c) {
241 DB *db = *dbtable[n].db;
242 const char *dbname = dbtable[n].dbname;
245 if(undump_dbt(fp, tag, prepare_data(&k))
246 || undump_dbt(fp, tag, prepare_data(&d)))
248 switch(err = db->put(db, tid, &k, &d, 0)) {
251 case DB_LOCK_DEADLOCK:
252 error(0, "error updating %s: %s", dbname, db_strerror(err));
255 fatal(0, "error updating %s: %s", dbname, db_strerror(err));
265 fatal(0, "unknown version '%c'", c);
272 if(c >= 32 && c <= 126)
273 fatal(0, "unexpected character '%c'", c);
275 fatal(0, "unexpected character 0x%02X", c);
281 fatal(errno, "error reading %s", tag);
283 fatal(0, "unexpected EOF reading %s", tag);
287 /* recompute aliases and search database from prefs, return 0 or
288 * DB_LOCK_DEADLOCK */
289 static int recompute_aliases(DB_TXN *tid) {
294 const char *path, *track;
296 info("recomputing aliases");
297 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
298 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
299 DB_FIRST)) == DB_LOCK_DEADLOCK) goto done;
301 data = kvp_urldecode(d.data, d.size);
302 track = xstrndup(k.data, k.size);
303 if(!kvp_get(data, "_alias_for")) {
304 if(!(path = kvp_get(data, "_path")))
305 error(0, "%s is not an alias but has no path", utf82mb(track));
307 if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK)
310 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
319 case DB_LOCK_DEADLOCK:
322 fatal(0, "cursor->c_get: %s", db_strerror(err));
325 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
329 /* restore prefs from FP */
330 static void do_undump(FILE *fp, const char *tag, int remove_pathless) {
334 tid = trackdb_begin_transaction();
335 if(remove_aliases(tid, remove_pathless)
336 || undump_from_fp(tid, fp, tag)
337 || recompute_aliases(tid)) goto fail;
340 info("aborting transaction and retrying undump");
341 trackdb_abort_transaction(tid);
343 info("committing undump");
344 trackdb_commit_transaction(tid);
347 /* just recompute alisaes */
348 static void do_recompute(int remove_pathless) {
352 tid = trackdb_begin_transaction();
353 if(remove_aliases(tid, remove_pathless)
354 || recompute_aliases(tid)) goto fail;
357 info("aborting transaction and retrying recomputation");
358 trackdb_abort_transaction(tid);
360 info("committing recomputed aliases");
361 trackdb_commit_transaction(tid);
364 int main(int argc, char **argv) {
365 int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0;
366 int remove_pathless = 0, fd;
372 while((n = getopt_long(argc, argv, "hVc:dDurRaP", options, 0)) >= 0) {
375 case 'V': version("disorder-dump");
376 case 'c': configfile = optarg; break;
377 case 'd': dump = 1; break;
378 case 'u': undump = 1; break;
379 case 'D': debugging = 1; break;
380 case 'r': recover = TRACKDB_NORMAL_RECOVER;
381 case 'R': recover = TRACKDB_FATAL_RECOVER;
382 case 'a': recompute = 1; break;
383 case 'P': remove_pathless = 1; break;
384 default: fatal(0, "invalid option");
387 if(dump + undump + recompute != 1)
388 fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases");
391 fatal(0, "--recompute-aliases does not take a filename");
395 fatal(0, "missing dump file name");
396 if(optind + 1 < argc)
397 fatal(0, "specify only a dump file name");
400 if(config_read(0)) fatal(0, "cannot read configuration");
401 trackdb_init(recover|TRACKDB_MAY_CREATE);
402 trackdb_open(TRACKDB_NO_UPGRADE);
404 /* We write to a temporary file and rename into place. We make
405 * sure the permissions are tight from the start. */
406 byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid());
407 if((fd = open(tmp, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0)
408 fatal(errno, "error opening %s", tmp);
409 if(!(fp = fdopen(fd, "w")))
410 fatal(errno, "fdopen on %s", tmp);
412 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
413 if(rename(tmp, path) < 0)
414 fatal(errno, "error renaming %s to %s", tmp, path);
416 /* the databases or logfiles might end up with wrong permissions
417 * if new ones are created */
418 if(getuid() == 0) info("you might need to chown database files");
419 if(!(fp = fopen(path, "r"))) fatal(errno, "error opening %s", path);
420 do_undump(fp, path, remove_pathless);
422 } else if(recompute) {
423 do_recompute(remove_pathless);