Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004, 2005, 2007, 2008 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 | |
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' }, | |
460b9539 | 32 | { "recompute-aliases", no_argument, 0, 'a' }, |
33 | { "remove-pathless", no_argument, 0, 'P' }, | |
34 | { 0, 0, 0, 0 } | |
35 | }; | |
36 | ||
37 | /* display usage message and terminate */ | |
38 | static void help(void) { | |
39 | xprintf("Usage:\n" | |
40 | " disorder-dump [OPTIONS] --dump|--undump PATH\n" | |
41 | " disorder-dump [OPTIONS] --recompute-aliases\n" | |
42 | "Options:\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"); | |
52 | xfclose(stdout); | |
53 | exit(0); | |
54 | } | |
55 | ||
b3074bd1 RK |
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 | |
64 | */ | |
65 | static int dump_one(struct sink *s, | |
66 | const char *tag, | |
67 | int letter, | |
68 | const char *dbname, | |
69 | DB *db, | |
70 | DB_TXN *tid) { | |
71 | int err; | |
72 | DBC *cursor; | |
73 | DBT k, d; | |
74 | ||
75 | /* dump the preferences */ | |
76 | cursor = trackdb_opencursor(db, tid); | |
77 | err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), | |
78 | DB_FIRST); | |
79 | while(err == 0) { | |
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), | |
87 | DB_NEXT); | |
88 | } | |
89 | switch(err) { | |
90 | case DB_LOCK_DEADLOCK: | |
91 | trackdb_closecursor(cursor); | |
92 | return err; | |
93 | case DB_NOTFOUND: | |
94 | return trackdb_closecursor(cursor); | |
95 | case 0: | |
96 | assert(!"cannot happen"); | |
97 | default: | |
98 | fatal(0, "error reading %s: %s", dbname, db_strerror(err)); | |
99 | } | |
100 | } | |
101 | ||
102 | static struct { | |
103 | int letter; | |
104 | const char *dbname; | |
105 | DB **db; | |
106 | } dbtable[] = { | |
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 */ | |
113 | }; | |
114 | #define NDBTABLE (sizeof dbtable / sizeof *dbtable) | |
115 | ||
460b9539 | 116 | /* dump prefs to FP, return nonzero on error */ |
b3074bd1 | 117 | static void do_dump(FILE *fp, const char *tag) { |
460b9539 | 118 | DB_TXN *tid; |
119 | struct sink *s = sink_stdio(tag, fp); | |
460b9539 | 120 | |
121 | for(;;) { | |
122 | tid = trackdb_begin_transaction(); | |
123 | if(fseek(fp, 0, SEEK_SET) < 0) | |
124 | fatal(errno, "error calling fseek"); | |
125 | if(fflush(fp) < 0) | |
126 | fatal(errno, "error calling fflush"); | |
127 | if(ftruncate(fileno(fp), 0) < 0) | |
128 | fatal(errno, "error calling ftruncate"); | |
b3074bd1 | 129 | if(fprintf(fp, "V0") < 0) |
460b9539 | 130 | fatal(errno, "error writing to %s", tag); |
b3074bd1 RK |
131 | for(size_t n = 0; n < NDBTABLE; ++n) |
132 | if(dump_one(s, tag, | |
133 | dbtable[n].letter, dbtable[n].dbname, *dbtable[n].db, | |
134 | tid)) | |
135 | goto fail; | |
f35e5800 | 136 | |
b3074bd1 RK |
137 | if(fputs("E\n", fp) < 0) |
138 | fatal(errno, "error writing to %s", tag); | |
460b9539 | 139 | break; |
140 | fail: | |
460b9539 | 141 | info("aborting transaction and retrying dump"); |
142 | trackdb_abort_transaction(tid); | |
143 | } | |
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); | |
148 | } | |
149 | ||
150 | /* delete all aliases prefs, return 0 or DB_LOCK_DEADLOCK */ | |
151 | static int remove_aliases(DB_TXN *tid, int remove_pathless) { | |
152 | DBC *cursor; | |
153 | int err; | |
154 | DBT k, d; | |
155 | struct kvp *data; | |
156 | int alias, pathless; | |
157 | ||
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)); | |
163 | goto done; | |
164 | } | |
165 | while(err == 0) { | |
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)) { | |
173 | case 0: break; | |
174 | case DB_LOCK_DEADLOCK: | |
175 | error(0, "cursor->c_get: %s", db_strerror(err)); | |
176 | goto done; | |
177 | default: | |
178 | fatal(0, "cursor->c_del: %s", db_strerror(err)); | |
179 | } | |
180 | } | |
181 | err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT); | |
182 | } | |
183 | if(err == DB_LOCK_DEADLOCK) { | |
184 | error(0, "cursor operation: %s", db_strerror(err)); | |
185 | goto done; | |
186 | } | |
187 | if(err != DB_NOTFOUND) fatal(0, "cursor->c_get: %s", db_strerror(err)); | |
188 | err = 0; | |
189 | done: | |
190 | if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK; | |
191 | return err; | |
192 | } | |
193 | ||
194 | /* truncate (i.e. empty) a database, return 0 or DB_LOCK_DEADLOCK */ | |
195 | static int truncdb(DB_TXN *tid, DB *db) { | |
196 | int err; | |
197 | u_int32_t count; | |
198 | ||
199 | switch(err = db->truncate(db, tid, &count, 0)) { | |
200 | case 0: break; | |
201 | case DB_LOCK_DEADLOCK: | |
202 | error(0, "db->truncate: %s", db_strerror(err)); | |
203 | break; | |
204 | default: | |
205 | fatal(0, "db->truncate: %s", db_strerror(err)); | |
206 | } | |
207 | return err; | |
208 | } | |
209 | ||
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) { | |
212 | char *s; | |
213 | struct dynstr d; | |
214 | ||
215 | if(inputline(tag, fp, &s, '\n')) return -1; | |
216 | dynstr_init(&d); | |
217 | if(urldecode(sink_dynstr(&d), s, strlen(s))) | |
218 | fatal(0, "invalid URL-encoded data in %s", tag); | |
219 | dbt->data = d.vec; | |
220 | dbt->size = d.nvec; | |
221 | return 0; | |
222 | } | |
223 | ||
224 | /* undump from FP, return 0 or DB_LOCK_DEADLOCK */ | |
225 | static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) { | |
226 | int err, c; | |
460b9539 | 227 | |
228 | info("undumping"); | |
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; | |
fb1bc1f5 | 232 | if((err = truncdb(tid, trackdb_globaldb))) return err; |
460b9539 | 233 | if((err = truncdb(tid, trackdb_searchdb))) return err; |
fb1bc1f5 | 234 | if((err = truncdb(tid, trackdb_tagsdb))) return err; |
a745dd43 | 235 | if((err = truncdb(tid, trackdb_usersdb))) return err; |
0e464e60 | 236 | if((err = truncdb(tid, trackdb_scheduledb))) return err; |
460b9539 | 237 | c = getc(fp); |
238 | while(!ferror(fp) && !feof(fp)) { | |
b3074bd1 RK |
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; | |
243 | DBT k, d; | |
244 | ||
245 | if(undump_dbt(fp, tag, prepare_data(&k)) | |
246 | || undump_dbt(fp, tag, prepare_data(&d))) | |
247 | break; | |
248 | switch(err = db->put(db, tid, &k, &d, 0)) { | |
249 | case 0: | |
250 | break; | |
251 | case DB_LOCK_DEADLOCK: | |
252 | error(0, "error updating %s: %s", dbname, db_strerror(err)); | |
253 | return err; | |
254 | default: | |
255 | fatal(0, "error updating %s: %s", dbname, db_strerror(err)); | |
256 | } | |
257 | goto next; | |
258 | } | |
259 | } | |
260 | ||
460b9539 | 261 | switch(c) { |
262 | case 'V': | |
263 | c = getc(fp); | |
264 | if(c != '0') | |
265 | fatal(0, "unknown version '%c'", c); | |
266 | break; | |
267 | case 'E': | |
268 | return 0; | |
460b9539 | 269 | case '\n': |
270 | break; | |
b3074bd1 RK |
271 | default: |
272 | if(c >= 32 && c <= 126) | |
273 | fatal(0, "unexpected character '%c'", c); | |
274 | else | |
275 | fatal(0, "unexpected character 0x%02X", c); | |
460b9539 | 276 | } |
b3074bd1 | 277 | next: |
460b9539 | 278 | c = getc(fp); |
279 | } | |
280 | if(ferror(fp)) | |
281 | fatal(errno, "error reading %s", tag); | |
282 | else | |
283 | fatal(0, "unexpected EOF reading %s", tag); | |
284 | return 0; | |
285 | } | |
286 | ||
287 | /* recompute aliases and search database from prefs, return 0 or | |
288 | * DB_LOCK_DEADLOCK */ | |
289 | static int recompute_aliases(DB_TXN *tid) { | |
290 | DBC *cursor; | |
291 | DBT k, d; | |
292 | int err; | |
293 | struct kvp *data; | |
294 | const char *path, *track; | |
295 | ||
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; | |
300 | while(err == 0) { | |
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)); | |
306 | else | |
307 | if((err = trackdb_notice_tid(track, path, tid)) == DB_LOCK_DEADLOCK) | |
308 | goto done; | |
309 | } | |
310 | err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), | |
311 | DB_NEXT); | |
312 | } | |
313 | switch(err) { | |
314 | case 0: | |
315 | break; | |
316 | case DB_NOTFOUND: | |
317 | err = 0; | |
318 | break; | |
319 | case DB_LOCK_DEADLOCK: | |
320 | break; | |
321 | default: | |
322 | fatal(0, "cursor->c_get: %s", db_strerror(err)); | |
323 | } | |
324 | done: | |
325 | if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK; | |
326 | return err; | |
327 | } | |
328 | ||
329 | /* restore prefs from FP */ | |
330 | static void do_undump(FILE *fp, const char *tag, int remove_pathless) { | |
331 | DB_TXN *tid; | |
332 | ||
333 | for(;;) { | |
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; | |
338 | break; | |
339 | fail: | |
340 | info("aborting transaction and retrying undump"); | |
341 | trackdb_abort_transaction(tid); | |
342 | } | |
343 | info("committing undump"); | |
344 | trackdb_commit_transaction(tid); | |
345 | } | |
346 | ||
347 | /* just recompute alisaes */ | |
348 | static void do_recompute(int remove_pathless) { | |
349 | DB_TXN *tid; | |
350 | ||
351 | for(;;) { | |
352 | tid = trackdb_begin_transaction(); | |
353 | if(remove_aliases(tid, remove_pathless) | |
354 | || recompute_aliases(tid)) goto fail; | |
355 | break; | |
356 | fail: | |
357 | info("aborting transaction and retrying recomputation"); | |
358 | trackdb_abort_transaction(tid); | |
359 | } | |
360 | info("committing recomputed aliases"); | |
361 | trackdb_commit_transaction(tid); | |
362 | } | |
363 | ||
364 | int main(int argc, char **argv) { | |
365 | int n, dump = 0, undump = 0, recover = TRACKDB_NO_RECOVER, recompute = 0; | |
b3074bd1 | 366 | int remove_pathless = 0, fd; |
460b9539 | 367 | const char *path; |
368 | char *tmp; | |
369 | FILE *fp; | |
370 | ||
320598d4 | 371 | mem_init(); |
b3074bd1 | 372 | while((n = getopt_long(argc, argv, "hVc:dDurRaP", options, 0)) >= 0) { |
460b9539 | 373 | switch(n) { |
374 | case 'h': help(); | |
3fbdc96d | 375 | case 'V': version("disorder-dump"); |
460b9539 | 376 | case 'c': configfile = optarg; break; |
377 | case 'd': dump = 1; break; | |
378 | case 'u': undump = 1; break; | |
379 | case 'D': debugging = 1; break; | |
460b9539 | 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"); | |
385 | } | |
386 | } | |
387 | if(dump + undump + recompute != 1) | |
388 | fatal(0, "choose exactly one of --dump, --undump or --recompute-aliases"); | |
460b9539 | 389 | if(recompute) { |
390 | if(optind != argc) | |
391 | fatal(0, "--recompute-aliases does not take a filename"); | |
392 | path = 0; | |
393 | } else { | |
394 | if(optind >= argc) | |
395 | fatal(0, "missing dump file name"); | |
396 | if(optind + 1 < argc) | |
397 | fatal(0, "specify only a dump file name"); | |
398 | path = argv[optind]; | |
399 | } | |
02ba7921 | 400 | if(config_read(0, NULL)) fatal(0, "cannot read configuration"); |
a745dd43 | 401 | trackdb_init(recover|TRACKDB_MAY_CREATE); |
d25c4615 | 402 | trackdb_open(TRACKDB_NO_UPGRADE); |
460b9539 | 403 | if(dump) { |
a745dd43 RK |
404 | /* We write to a temporary file and rename into place. We make |
405 | * sure the permissions are tight from the start. */ | |
460b9539 | 406 | byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid()); |
a745dd43 RK |
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); | |
b3074bd1 | 411 | do_dump(fp, tmp); |
460b9539 | 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); | |
415 | } else if(undump) { | |
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); | |
421 | xfclose(fp); | |
422 | } else if(recompute) { | |
423 | do_recompute(remove_pathless); | |
424 | } | |
425 | trackdb_close(); | |
426 | trackdb_deinit(); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | /* | |
431 | Local Variables: | |
432 | c-basic-offset:2 | |
433 | comment-column:40 | |
434 | End: | |
435 | */ |