chiark / gitweb /
doxygen fixes
[disorder] / server / trackdb.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
eb525fcd 3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
460b9539 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 <string.h>
25#include <stdio.h>
26#include <db.h>
27#include <sys/socket.h>
28#include <pcre.h>
29#include <assert.h>
30#include <unistd.h>
31#include <errno.h>
32#include <stddef.h>
33#include <sys/time.h>
34#include <sys/resource.h>
35#include <time.h>
2a10b70b 36#include <arpa/inet.h>
460b9539 37
38#include "event.h"
39#include "mem.h"
40#include "kvp.h"
41#include "log.h"
42#include "vector.h"
43#include "trackdb.h"
44#include "configuration.h"
45#include "syscalls.h"
46#include "wstat.h"
47#include "words.h"
48#include "printf.h"
49#include "filepart.h"
50#include "trackname.h"
51#include "trackdb-int.h"
52#include "logfd.h"
53#include "cache.h"
54#include "eventlog.h"
55#include "hash.h"
56
57#define RESCAN "disorder-rescan"
58#define DEADLOCK "disorder-deadlock"
59
60static const char *getpart(const char *track,
61 const char *context,
62 const char *part,
63 const struct kvp *p,
64 int *used_db);
65static int trackdb_alltags_tid(DB_TXN *tid, char ***taglistp);
66static int trackdb_get_global_tid(const char *name,
67 DB_TXN *tid,
68 const char **rp);
2a10b70b
RK
69static char **trackdb_new_tid(int *ntracksp,
70 int maxtracks,
71 DB_TXN *tid);
1e64e9fb 72static int trackdb_expire_noticed_tid(time_t earliest, DB_TXN *tid);
460b9539 73
74const struct cache_type cache_files_type = { 86400 };
75unsigned long cache_files_hits, cache_files_misses;
76
77/* setup and teardown ********************************************************/
78
79static const char *home; /* home had better not change */
80DB_ENV *trackdb_env; /* db environment */
81DB *trackdb_tracksdb; /* the db itself */
82DB *trackdb_prefsdb; /* preferences */
83DB *trackdb_searchdb; /* the search database */
84DB *trackdb_tagsdb; /* the tags database */
85DB *trackdb_globaldb; /* global preferences */
2a10b70b 86DB *trackdb_noticeddb; /* when track noticed */
460b9539 87static pid_t db_deadlock_pid = -1; /* deadlock manager PID */
88static pid_t rescan_pid = -1; /* rescanner PID */
89static int initialized, opened; /* state */
90
91/* tracks matched by required_tags */
92static char **reqtracks;
93static size_t nreqtracks;
94
95/* comparison function for keys */
96static int compare(DB attribute((unused)) *db_,
97 const DBT *a, const DBT *b) {
98 return compare_path_raw(a->data, a->size, b->data, b->size);
99}
100
101/* open environment */
102void trackdb_init(int recover) {
103 int err;
104 static int recover_type[] = { 0, DB_RECOVER, DB_RECOVER_FATAL };
105
106 /* sanity checks */
107 assert(initialized == 0);
108 ++initialized;
109 if(home) {
110 if(strcmp(home, config->home))
111 fatal(0, "cannot change db home without server restart");
112 home = config->home;
113 }
114
115 /* create environment */
116 if((err = db_env_create(&trackdb_env, 0))) fatal(0, "db_env_create: %s",
117 db_strerror(err));
118 if((err = trackdb_env->set_alloc(trackdb_env,
119 xmalloc_noptr, xrealloc_noptr, xfree)))
120 fatal(0, "trackdb_env->set_alloc: %s", db_strerror(err));
121 if((err = trackdb_env->set_lk_max_locks(trackdb_env, 10000)))
122 fatal(0, "trackdb_env->set_lk_max_locks: %s", db_strerror(err));
123 if((err = trackdb_env->set_lk_max_objects(trackdb_env, 10000)))
124 fatal(0, "trackdb_env->set_lk_max_objects: %s", db_strerror(err));
125 if((err = trackdb_env->open(trackdb_env, config->home,
126 DB_INIT_LOG
127 |DB_INIT_LOCK
128 |DB_INIT_MPOOL
129 |DB_INIT_TXN
130 |DB_CREATE
131 |recover_type[recover],
132 0666)))
dc450d30 133 fatal(0, "trackdb_env->open %s: %s", config->home, db_strerror(err));
460b9539 134 trackdb_env->set_errpfx(trackdb_env, "DB");
135 trackdb_env->set_errfile(trackdb_env, stderr);
136 trackdb_env->set_verbose(trackdb_env, DB_VERB_DEADLOCK, 1);
137 trackdb_env->set_verbose(trackdb_env, DB_VERB_RECOVERY, 1);
138 trackdb_env->set_verbose(trackdb_env, DB_VERB_REPLICATION, 1);
139 D(("initialized database environment"));
140}
141
142/* called when deadlock manager terminates */
143static int reap_db_deadlock(ev_source attribute((unused)) *ev,
144 pid_t attribute((unused)) pid,
145 int status,
146 const struct rusage attribute((unused)) *rusage,
147 void attribute((unused)) *u) {
148 db_deadlock_pid = -1;
149 if(initialized)
150 fatal(0, "deadlock manager unexpectedly terminated: %s",
151 wstat(status));
152 else
153 D(("deadlock manager terminated: %s", wstat(status)));
154 return 0;
155}
156
d6dde5a3
RK
157static pid_t subprogram(ev_source *ev, const char *prog,
158 int outputfd) {
460b9539 159 pid_t pid;
160 int lfd;
161
162 /* If we're in the background then trap subprocess stdout/stderr */
163 if(!isatty(2))
164 lfd = logfd(ev, prog);
165 else
166 lfd = -1;
167 if(!(pid = xfork())) {
168 exitfn = _exit;
169 ev_signal_atfork(ev);
170 signal(SIGPIPE, SIG_DFL);
171 if(lfd != -1) {
172 xdup2(lfd, 1);
173 xdup2(lfd, 2);
d6dde5a3
RK
174 xclose(lfd);
175 }
176 if(outputfd != -1) {
177 xdup2(outputfd, 1);
178 xclose(outputfd);
460b9539 179 }
180 /* If we were negatively niced, undo it. We don't bother checking for
181 * error, it's not that important. */
182 setpriority(PRIO_PROCESS, 0, 0);
183 execlp(prog, prog, "--config", configfile,
184 debugging ? "--debug" : "--no-debug",
185 (char *)0);
186 fatal(errno, "error invoking %s", prog);
187 }
188 if(lfd != -1) xclose(lfd);
189 return pid;
190}
191
192/* start deadlock manager */
193void trackdb_master(ev_source *ev) {
194 assert(db_deadlock_pid == -1);
d6dde5a3 195 db_deadlock_pid = subprogram(ev, DEADLOCK, -1);
460b9539 196 ev_child(ev, db_deadlock_pid, 0, reap_db_deadlock, 0);
197 D(("started deadlock manager"));
198}
199
200/* close environment */
201void trackdb_deinit(void) {
202 int err;
203
204 /* sanity checks */
205 assert(initialized == 1);
206 --initialized;
207
208 /* close the environment */
209 if((err = trackdb_env->close(trackdb_env, 0)))
210 fatal(0, "trackdb_env->close: %s", db_strerror(err));
211
212 if(rescan_pid != -1 && kill(rescan_pid, SIGTERM) < 0)
213 fatal(errno, "error killing rescanner");
214
215 /* terminate the deadlock manager */
216 if(db_deadlock_pid != -1 && kill(db_deadlock_pid, SIGTERM) < 0)
217 fatal(errno, "error killing deadlock manager");
218 db_deadlock_pid = -1;
219
220 D(("deinitialized database environment"));
221}
222
223/* open a specific database */
224static DB *open_db(const char *path,
225 u_int32_t dbflags,
226 DBTYPE dbtype,
227 u_int32_t openflags,
228 int mode) {
229 int err;
230 DB *db;
231
232 D(("open %s", path));
233 path = config_get_file(path);
234 if((err = db_create(&db, trackdb_env, 0)))
235 fatal(0, "db_create %s: %s", path, db_strerror(err));
236 if(dbflags)
237 if((err = db->set_flags(db, dbflags)))
238 fatal(0, "db->set_flags %s: %s", path, db_strerror(err));
239 if(dbtype == DB_BTREE)
240 if((err = db->set_bt_compare(db, compare)))
241 fatal(0, "db->set_bt_compare %s: %s", path, db_strerror(err));
242 if((err = db->open(db, 0, path, 0, dbtype,
243 openflags | DB_AUTO_COMMIT, mode)))
244 fatal(0, "db->open %s: %s", path, db_strerror(err));
245 return db;
246}
247
248/* open track databases */
249void trackdb_open(void) {
250 /* sanity checks */
251 assert(opened == 0);
252 ++opened;
253 /* open the databases */
254 trackdb_tracksdb = open_db("tracks.db",
255 DB_RECNUM, DB_BTREE, DB_CREATE, 0666);
256 trackdb_searchdb = open_db("search.db",
257 DB_DUP|DB_DUPSORT, DB_HASH, DB_CREATE, 0666);
258 trackdb_tagsdb = open_db("tags.db",
259 DB_DUP|DB_DUPSORT, DB_HASH, DB_CREATE, 0666);
260 trackdb_prefsdb = open_db("prefs.db", 0, DB_HASH, DB_CREATE, 0666);
261 trackdb_globaldb = open_db("global.db", 0, DB_HASH, DB_CREATE, 0666);
2a10b70b
RK
262 trackdb_noticeddb = open_db("noticed.db",
263 DB_DUPSORT, DB_BTREE, DB_CREATE, 0666);
460b9539 264 D(("opened databases"));
265}
266
267/* close track databases */
268void trackdb_close(void) {
269 int err;
270
271 /* sanity checks */
272 assert(opened == 1);
273 --opened;
274 if((err = trackdb_tracksdb->close(trackdb_tracksdb, 0)))
275 fatal(0, "error closing tracks.db: %s", db_strerror(err));
276 if((err = trackdb_searchdb->close(trackdb_searchdb, 0)))
277 fatal(0, "error closing search.db: %s", db_strerror(err));
278 if((err = trackdb_tagsdb->close(trackdb_tagsdb, 0)))
279 fatal(0, "error closing tags.db: %s", db_strerror(err));
280 if((err = trackdb_prefsdb->close(trackdb_prefsdb, 0)))
281 fatal(0, "error closing prefs.db: %s", db_strerror(err));
282 if((err = trackdb_globaldb->close(trackdb_globaldb, 0)))
283 fatal(0, "error closing global.db: %s", db_strerror(err));
2a10b70b
RK
284 if((err = trackdb_noticeddb->close(trackdb_noticeddb, 0)))
285 fatal(0, "error closing noticed.db: %s", db_strerror(err));
460b9539 286 trackdb_tracksdb = trackdb_searchdb = trackdb_prefsdb = 0;
287 trackdb_tagsdb = trackdb_globaldb = 0;
288 D(("closed databases"));
289}
290
291/* generic db routines *******************************************************/
292
293/* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
294 * DB_LOCK_DEADLOCK. */
295int trackdb_getdata(DB *db,
296 const char *track,
297 struct kvp **kp,
298 DB_TXN *tid) {
299 int err;
300 DBT key, data;
301
302 switch(err = db->get(db, tid, make_key(&key, track),
303 prepare_data(&data), 0)) {
304 case 0:
305 *kp = kvp_urldecode(data.data, data.size);
306 return 0;
307 case DB_NOTFOUND:
308 *kp = 0;
309 return err;
310 case DB_LOCK_DEADLOCK:
311 error(0, "error querying database: %s", db_strerror(err));
312 return err;
313 default:
314 fatal(0, "error querying database: %s", db_strerror(err));
315 }
316}
317
318/* encode and store a database entry. Returns 0, DB_KEYEXIST or
319 * DB_LOCK_DEADLOCK. */
320int trackdb_putdata(DB *db,
321 const char *track,
322 const struct kvp *k,
323 DB_TXN *tid,
324 u_int32_t flags) {
325 int err;
326 DBT key, data;
327
328 switch(err = db->put(db, tid, make_key(&key, track),
329 encode_data(&data, k), flags)) {
330 case 0:
331 case DB_KEYEXIST:
332 return err;
333 case DB_LOCK_DEADLOCK:
334 error(0, "error updating database: %s", db_strerror(err));
335 return err;
336 default:
337 fatal(0, "error updating database: %s", db_strerror(err));
338 }
339}
340
341/* delete a database entry */
342int trackdb_delkey(DB *db,
343 const char *track,
344 DB_TXN *tid) {
345 int err;
346
347 DBT key;
348 switch(err = db->del(db, tid, make_key(&key, track), 0)) {
349 case 0:
350 case DB_NOTFOUND:
351 return 0;
352 case DB_LOCK_DEADLOCK:
353 error(0, "error updating database: %s", db_strerror(err));
354 return err;
355 default:
356 fatal(0, "error updating database: %s", db_strerror(err));
357 }
358}
359
360/* open a database cursor */
361DBC *trackdb_opencursor(DB *db, DB_TXN *tid) {
362 int err;
363 DBC *c;
364
365 switch(err = db->cursor(db, tid, &c, 0)) {
366 case 0: break;
367 default: fatal(0, "error creating cursor: %s", db_strerror(err));
368 }
369 return c;
370}
371
372/* close a database cursor; returns 0 or DB_LOCK_DEADLOCK */
373int trackdb_closecursor(DBC *c) {
374 int err;
375
376 if(!c) return 0;
377 switch(err = c->c_close(c)) {
378 case 0:
379 return err;
380 case DB_LOCK_DEADLOCK:
381 error(0, "error closing cursor: %s", db_strerror(err));
382 return err;
383 default:
384 fatal(0, "error closing cursor: %s", db_strerror(err));
385 }
386}
387
388/* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
389int trackdb_delkeydata(DB *db,
390 const char *word,
391 const char *track,
392 DB_TXN *tid) {
393 int err;
394 DBC *c;
395 DBT key, data;
396
397 c = trackdb_opencursor(db, tid);
398 switch(err = c->c_get(c, make_key(&key, word),
399 make_key(&data, track), DB_GET_BOTH)) {
400 case 0:
401 switch(err = c->c_del(c, 0)) {
402 case 0:
403 break;
404 case DB_KEYEMPTY:
405 err = 0;
406 break;
407 case DB_LOCK_DEADLOCK:
408 error(0, "error updating database: %s", db_strerror(err));
409 break;
410 default:
411 fatal(0, "c->c_del: %s", db_strerror(err));
412 }
413 break;
414 case DB_NOTFOUND:
415 break;
416 case DB_LOCK_DEADLOCK:
417 error(0, "error updating database: %s", db_strerror(err));
418 break;
419 default:
420 fatal(0, "c->c_get: %s", db_strerror(err));
421 }
422 if(trackdb_closecursor(c)) err = DB_LOCK_DEADLOCK;
423 return err;
424}
425
426/* start a transaction */
427DB_TXN *trackdb_begin_transaction(void) {
428 DB_TXN *tid;
429 int err;
430
431 if((err = trackdb_env->txn_begin(trackdb_env, 0, &tid, 0)))
432 fatal(0, "trackdb_env->txn_begin: %s", db_strerror(err));
433 return tid;
434}
435
436/* abort transaction */
437void trackdb_abort_transaction(DB_TXN *tid) {
438 int err;
439
440 if(tid)
441 if((err = tid->abort(tid)))
442 fatal(0, "tid->abort: %s", db_strerror(err));
443}
444
445/* commit transaction */
446void trackdb_commit_transaction(DB_TXN *tid) {
447 int err;
448
449 if((err = tid->commit(tid, 0)))
450 fatal(0, "tid->commit: %s", db_strerror(err));
451}
452
453/* search/tags shared code ***************************************************/
454
455/* comparison function used by dedupe() */
456static int wordcmp(const void *a, const void *b) {
457 return strcmp(*(const char **)a, *(const char **)b);
458}
459
460/* sort and de-dupe VEC */
461static char **dedupe(char **vec, int nvec) {
462 int m, n;
463
464 qsort(vec, nvec, sizeof (char *), wordcmp);
465 m = n = 0;
466 if(nvec) {
467 vec[m++] = vec[0];
468 for(n = 1; n < nvec; ++n)
469 if(strcmp(vec[n], vec[m - 1]))
470 vec[m++] = vec[n];
471 }
472 vec[m] = 0;
473 return vec;
474}
475
476/* update a key/track database. Returns 0 or DB_DEADLOCK. */
477static int register_word(DB *db, const char *what,
478 const char *track, const char *word,
479 DB_TXN *tid) {
480 int err;
481 DBT key, data;
482
483 switch(err = db->put(db, tid, make_key(&key, word),
484 make_key(&data, track), DB_NODUPDATA)) {
485 case 0:
486 case DB_KEYEXIST:
487 return 0;
488 case DB_LOCK_DEADLOCK:
489 error(0, "error updating %s.db: %s", what, db_strerror(err));
490 return err;
491 default:
492 fatal(0, "error updating %s.db: %s", what, db_strerror(err));
493 }
494}
495
496/* search primitives *********************************************************/
497
498/* return true iff NAME is a trackname_display_ pref */
499static int is_display_pref(const char *name) {
500 static const char prefix[] = "trackname_display_";
501 return !strncmp(name, prefix, (sizeof prefix) - 1);
502}
503
504/* compute the words of a track name */
505static char **track_to_words(const char *track,
506 const struct kvp *p) {
507 struct vector v;
508 char **w;
509 int nw;
a8747834 510 const char *rootless = track_rootless(track);
460b9539 511
a8747834 512 if(!rootless)
513 rootless = track; /* bodge */
460b9539 514 vector_init(&v);
a8747834 515 if((w = words(casefold(strip_extension(rootless)), &nw)))
460b9539 516 vector_append_many(&v, w, nw);
517
518 for(; p; p = p->next)
519 if(is_display_pref(p->name))
520 if((w = words(casefold(p->value), &nw)))
521 vector_append_many(&v, w, nw);
522 vector_terminate(&v);
523 return dedupe(v.vec, v.nvec);
524}
525
526/* return nonzero iff WORD is a stopword */
527static int stopword(const char *word) {
528 int n;
529
530 for(n = 0; n < config->stopword.n
531 && strcmp(word, config->stopword.s[n]); ++n)
532 ;
533 return n < config->stopword.n;
534}
535
536/* record that WORD appears in TRACK. Returns 0 or DB_LOCK_DEADLOCK. */
537static int register_search_word(const char *track, const char *word,
538 DB_TXN *tid) {
539 if(stopword(word)) return 0;
540 return register_word(trackdb_searchdb, "search", track, word, tid);
541}
542
543/* Tags **********************************************************************/
544
545/* Return nonzero if C is a valid tag character */
546static int tagchar(int c) {
547 switch(c) {
548 case ',':
549 return 0;
550 default:
551 return c >= ' ';
552 }
553}
554
555/* Parse and de-dupe a tag list. If S=0 then assumes "". */
556static char **parsetags(const char *s) {
557 const char *t;
558 struct vector v;
559
560 vector_init(&v);
561 if(s) {
562 /* skip initial separators */
563 while(*s && (!tagchar(*s) || *s == ' '))
564 ++s;
565 while(*s) {
566 /* find the extent of the tag */
567 t = s;
568 while(*s && tagchar(*s))
569 ++s;
570 /* strip trailing spaces */
571 while(s > t && s[-1] == ' ')
572 --s;
573 vector_append(&v, xstrndup(t, s - t));
574 /* skip intermediate and trailing separators */
575 while(*s && (!tagchar(*s) || *s == ' '))
576 ++s;
577 }
578 }
579 vector_terminate(&v);
580 return dedupe(v.vec, v.nvec);
581}
582
583/* Record that TRACK has TAG. Returns 0 or DB_LOCK_DEADLOCK. */
584static int register_tag(const char *track, const char *tag, DB_TXN *tid) {
585 return register_word(trackdb_tagsdb, "tags", track, tag, tid);
586}
587
588/* aliases *******************************************************************/
589
590/* compute the alias and store at aliasp. Returns 0 or DB_LOCK_DEADLOCK. If
591 * there is no alias sets *aliasp to 0. */
592static int compute_alias(char **aliasp,
593 const char *track,
594 const struct kvp *p,
595 DB_TXN *tid) {
596 struct dynstr d;
597 const char *s = config->alias, *t, *expansion, *part;
598 int c, used_db = 0, slash_prefix, err;
599 struct kvp *at;
137e36ed 600 const char *const root = find_track_root(track);
460b9539 601
137e36ed 602 if(!root) {
603 /* Bodge for tracks with no root */
604 *aliasp = 0;
605 return 0;
606 }
460b9539 607 dynstr_init(&d);
137e36ed 608 dynstr_append_string(&d, root);
460b9539 609 while((c = (unsigned char)*s++)) {
610 if(c != '{') {
611 dynstr_append(&d, c);
612 continue;
613 }
614 if((slash_prefix = (*s == '/')))
615 s++;
616 t = strchr(s, '}');
617 assert(t != 0); /* validated at startup */
618 part = xstrndup(s, t - s);
619 expansion = getpart(track, "display", part, p, &used_db);
620 if(*expansion) {
621 if(slash_prefix) dynstr_append(&d, '/');
622 dynstr_append_string(&d, expansion);
623 }
624 s = t + 1; /* skip {part} */
625 }
626 /* only admit to the alias if we used the db... */
627 if(!used_db) {
628 *aliasp = 0;
629 return 0;
630 }
631 dynstr_terminate(&d);
632 /* ...and the answer differs from the original... */
633 if(!strcmp(track, d.vec)) {
634 *aliasp = 0;
635 return 0;
636 }
637 /* ...and there isn't already a different track with that name (including as
638 * an alias) */
639 switch(err = trackdb_getdata(trackdb_tracksdb, d.vec, &at, tid)) {
640 case 0:
460b9539 641 if((s = kvp_get(at, "_alias_for"))
642 && !strcmp(s, track)) {
643 case DB_NOTFOUND:
460b9539 644 *aliasp = d.vec;
645 } else {
460b9539 646 *aliasp = 0;
647 }
648 return 0;
649 default:
650 return err;
651 }
652}
653
654/* get track and prefs data (if tp/pp not null pointers). Returns 0 on
655 * success, DB_NOTFOUND if the track does not exist or DB_LOCK_DEADLOCK.
656 * Always sets the return values, even if only to null pointers. */
657static int gettrackdata(const char *track,
658 struct kvp **tp,
659 struct kvp **pp,
660 const char **actualp,
661 unsigned flags,
662#define GTD_NOALIAS 0x0001
663 DB_TXN *tid) {
664 int err;
665 const char *actual = track;
666 struct kvp *t = 0, *p = 0;
667
668 if((err = trackdb_getdata(trackdb_tracksdb, track, &t, tid))) goto done;
669 if((actual = kvp_get(t, "_alias_for"))) {
670 if(flags & GTD_NOALIAS) {
671 error(0, "alias passed to gettrackdata where real path required");
672 abort();
673 }
674 if((err = trackdb_getdata(trackdb_tracksdb, actual, &t, tid))) goto done;
675 } else
676 actual = track;
677 assert(actual != 0);
678 if(pp) {
679 if((err = trackdb_getdata(trackdb_prefsdb, actual, &p, tid)) == DB_LOCK_DEADLOCK)
680 goto done;
681 }
682 err = 0;
683done:
684 if(actualp) *actualp = actual;
685 if(tp) *tp = t;
686 if(pp) *pp = p;
687 return err;
688}
689
690/* trackdb_notice() **********************************************************/
691
6c81eef9 692/** @brief notice a possibly new track
2a10b70b
RK
693 * @return @c DB_NOTFOUND if new, 0 if already known
694 */
460b9539 695int trackdb_notice(const char *track,
696 const char *path) {
697 int err;
698 DB_TXN *tid;
699
700 for(;;) {
701 tid = trackdb_begin_transaction();
702 err = trackdb_notice_tid(track, path, tid);
703 if((err == DB_LOCK_DEADLOCK)) goto fail;
704 break;
705 fail:
706 trackdb_abort_transaction(tid);
707 }
708 trackdb_commit_transaction(tid);
709 return err;
710}
711
6c81eef9 712/** @brief notice a possibly new track
2a10b70b
RK
713 * @return @c DB_NOTFOUND if new, 0 if already known, @c DB_LOCK_DEADLOCK also
714 */
460b9539 715int trackdb_notice_tid(const char *track,
716 const char *path,
717 DB_TXN *tid) {
718 int err, n;
719 struct kvp *t, *a, *p;
720 int t_changed, ret;
721 char *alias, **w;
2a10b70b 722
460b9539 723 /* notice whether the tracks.db entry changes */
724 t_changed = 0;
725 /* get any existing tracks entry */
726 if((err = gettrackdata(track, &t, &p, 0, 0, tid)) == DB_LOCK_DEADLOCK)
727 return err;
2a10b70b 728 ret = err; /* 0 or DB_NOTFOUND */
460b9539 729 /* this is a real track */
730 t_changed += kvp_set(&t, "_alias_for", 0);
731 t_changed += kvp_set(&t, "_path", path);
732 /* if we have an alias record it in the database */
733 if((err = compute_alias(&alias, track, p, tid))) return err;
734 if(alias) {
735 /* won't overwrite someone else's alias as compute_alias() checks */
736 D(("%s: alias %s", track, alias));
737 a = 0;
738 kvp_set(&a, "_alias_for", track);
739 if((err = trackdb_putdata(trackdb_tracksdb, alias, a, tid, 0))) return err;
740 }
741 /* update search.db */
742 w = track_to_words(track, p);
743 for(n = 0; w[n]; ++n)
744 if((err = register_search_word(track, w[n], tid)))
745 return err;
746 /* update tags.db */
747 w = parsetags(kvp_get(p, "tags"));
748 for(n = 0; w[n]; ++n)
749 if((err = register_tag(track, w[n], tid)))
750 return err;
751 reqtracks = 0;
752 /* only store the tracks.db entry if it has changed */
753 if(t_changed && (err = trackdb_putdata(trackdb_tracksdb, track, t, tid, 0)))
754 return err;
2a10b70b
RK
755 if(ret == DB_NOTFOUND) {
756 uint32_t timestamp[2];
757 time_t now;
758 DBT key, data;
759
760 time(&now);
761 timestamp[0] = htonl((uint64_t)now >> 32);
762 timestamp[1] = htonl((uint32_t)now);
763 memset(&key, 0, sizeof key);
764 key.data = timestamp;
765 key.size = sizeof timestamp;
766 switch(err = trackdb_noticeddb->put(trackdb_noticeddb, tid, &key,
767 make_key(&data, track), 0)) {
768 case 0: break;
769 case DB_LOCK_DEADLOCK: return err;
770 default: fatal(0, "error updating noticed.db: %s", db_strerror(err));
771 }
772 }
460b9539 773 return ret;
774}
775
776/* trackdb_obsolete() ********************************************************/
777
778/* obsolete a track */
779int trackdb_obsolete(const char *track, DB_TXN *tid) {
780 int err, n;
781 struct kvp *p;
782 char *alias, **w;
783
784 if((err = gettrackdata(track, 0, &p, 0,
785 GTD_NOALIAS, tid)) == DB_LOCK_DEADLOCK)
786 return err;
787 else if(err == DB_NOTFOUND) return 0;
788 /* compute the alias, if any, and delete it */
789 if(compute_alias(&alias, track, p, tid)) return err;
790 if(alias) {
791 /* if the alias points to some other track then compute_alias won't
792 * return it */
793 if(trackdb_delkey(trackdb_tracksdb, alias, tid))
794 return err;
795 }
796 /* update search.db */
797 w = track_to_words(track, p);
798 for(n = 0; w[n]; ++n)
799 if(trackdb_delkeydata(trackdb_searchdb,
800 w[n], track, tid) == DB_LOCK_DEADLOCK)
801 return err;
802 /* update tags.db */
803 w = parsetags(kvp_get(p, "tags"));
804 for(n = 0; w[n]; ++n)
805 if(trackdb_delkeydata(trackdb_tagsdb,
806 w[n], track, tid) == DB_LOCK_DEADLOCK)
807 return err;
808 reqtracks = 0;
809 /* update tracks.db */
810 if(trackdb_delkey(trackdb_tracksdb, track, tid) == DB_LOCK_DEADLOCK)
811 return err;
812 /* We don't delete the prefs, so they survive temporary outages of the
813 * (possibly virtual) track filesystem */
814 return 0;
815}
816
817/* trackdb_stats() ***********************************************************/
818
819#define H(name) { #name, offsetof(DB_HASH_STAT, name) }
820#define B(name) { #name, offsetof(DB_BTREE_STAT, name) }
821
822static const struct statinfo {
823 const char *name;
824 size_t offset;
825} statinfo_hash[] = {
826 H(hash_magic),
827 H(hash_version),
828 H(hash_nkeys),
829 H(hash_ndata),
830 H(hash_pagesize),
831 H(hash_ffactor),
832 H(hash_buckets),
833 H(hash_free),
834 H(hash_bfree),
835 H(hash_bigpages),
836 H(hash_big_bfree),
837 H(hash_overflows),
838 H(hash_ovfl_free),
839 H(hash_dup),
840 H(hash_dup_free),
841}, statinfo_btree[] = {
842 B(bt_magic),
843 B(bt_version),
844 B(bt_nkeys),
845 B(bt_ndata),
846 B(bt_pagesize),
847 B(bt_minkey),
848 B(bt_re_len),
849 B(bt_re_pad),
850 B(bt_levels),
851 B(bt_int_pg),
852 B(bt_leaf_pg),
853 B(bt_dup_pg),
854 B(bt_over_pg),
855 B(bt_free),
856 B(bt_int_pgfree),
857 B(bt_leaf_pgfree),
858 B(bt_dup_pgfree),
859 B(bt_over_pgfree),
860};
861
862/* look up stats for DB */
863static int get_stats(struct vector *v,
864 DB *database,
865 const struct statinfo *si,
866 size_t nsi,
867 DB_TXN *tid) {
868 void *sp;
869 size_t n;
870 char *str;
871 int err;
872
873 if(database) {
874 switch(err = database->stat(database, tid, &sp, 0)) {
875 case 0:
876 break;
877 case DB_LOCK_DEADLOCK:
878 error(0, "error querying database: %s", db_strerror(err));
879 return err;
880 default:
881 fatal(0, "error querying database: %s", db_strerror(err));
882 }
883 for(n = 0; n < nsi; ++n) {
884 byte_xasprintf(&str, "%s=%"PRIuMAX, si[n].name,
885 (uintmax_t)*(u_int32_t *)((char *)sp + si[n].offset));
886 vector_append(v, str);
887 }
888 }
889 return 0;
890}
891
892struct search_entry {
893 char *word;
894 int n;
895};
896
897/* find the top COUNT words in the search database */
898static int search_league(struct vector *v, int count, DB_TXN *tid) {
899 struct search_entry *se;
900 DBT k, d;
901 DBC *cursor;
902 int err, n = 0, nse = 0, i;
903 char *word = 0;
904 size_t wl = 0;
905 char *str;
906
907 cursor = trackdb_opencursor(trackdb_searchdb, tid);
908 se = xmalloc(count * sizeof *se);
909 while(!(err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
910 DB_NEXT))) {
911 if(word && wl == k.size && !strncmp(word, k.data, wl))
912 ++n;
913 else {
914#define FINALIZE() do { \
915 if(word && (nse < count || n > se[nse - 1].n)) { \
916 if(nse == count) \
917 i = nse - 1; \
918 else \
919 i = nse++; \
920 while(i > 0 && n > se[i - 1].n) \
921 --i; \
922 memmove(&se[i + 1], &se[i], (nse - i) * sizeof *se); \
923 se[i].word = word; \
924 se[i].n = n; \
925 } \
926} while(0)
927 FINALIZE();
928 word = xstrndup(k.data, wl = k.size);
929 n = 1;
930 }
931 }
932 switch(err) {
933 case DB_NOTFOUND:
934 err = 0;
935 break;
936 case DB_LOCK_DEADLOCK:
937 error(0, "error querying search database: %s", db_strerror(err));
938 break;
939 default:
940 fatal(0, "error querying search database: %s", db_strerror(err));
941 }
942 if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK;
943 if(err) return err;
944 FINALIZE();
945 byte_xasprintf(&str, "Top %d search words:", nse);
946 vector_append(v, str);
947 for(i = 0; i < nse; ++i) {
948 byte_xasprintf(&str, "%4d: %5d %s", i + 1, se[i].n, se[i].word);
949 vector_append(v, str);
950 }
951 return 0;
952}
953
954#define SI(what) statinfo_##what, \
955 sizeof statinfo_##what / sizeof (struct statinfo)
956
957/* return a list of database stats */
958char **trackdb_stats(int *nstatsp) {
959 DB_TXN *tid;
960 struct vector v;
460b9539 961
962 vector_init(&v);
963 for(;;) {
964 tid = trackdb_begin_transaction();
965 v.nvec = 0;
966 vector_append(&v, (char *)"Tracks database stats:");
967 if(get_stats(&v, trackdb_tracksdb, SI(btree), tid)) goto fail;
968 vector_append(&v, (char *)"");
969 vector_append(&v, (char *)"Search database stats:");
970 if(get_stats(&v, trackdb_searchdb, SI(hash), tid)) goto fail;
971 vector_append(&v, (char *)"");
972 vector_append(&v, (char *)"Prefs database stats:");
973 if(get_stats(&v, trackdb_prefsdb, SI(hash), tid)) goto fail;
974 vector_append(&v, (char *)"");
975 if(search_league(&v, 10, tid)) goto fail;
460b9539 976 vector_terminate(&v);
977 break;
978fail:
979 trackdb_abort_transaction(tid);
980 }
981 trackdb_commit_transaction(tid);
982 if(nstatsp) *nstatsp = v.nvec;
983 return v.vec;
984}
985
d6dde5a3
RK
986struct stats_details {
987 void (*done)(char *data, void *u);
988 void *u;
989 int exited; /* subprocess exited */
990 int closed; /* pipe close */
991 int wstat; /* wait status from subprocess */
992 struct dynstr data[1]; /* data read from pipe */
993};
994
995static void stats_complete(struct stats_details *d) {
996 char *s;
997
998 if(!(d->exited && d->closed))
999 return;
1000 byte_xasprintf(&s, "\n"
1001 "Server stats:\n"
1002 "track lookup cache hits: %lu\n"
1003 "track lookup cache misses: %lu\n",
1004 cache_files_hits,
1005 cache_files_misses);
1006 dynstr_append_string(d->data, s);
1007 dynstr_terminate(d->data);
1008 d->done(d->data->vec, d->u);
1009}
1010
1011static int stats_finished(ev_source attribute((unused)) *ev,
1012 pid_t attribute((unused)) pid,
1013 int status,
1014 const struct rusage attribute((unused)) *rusage,
1015 void *u) {
1016 struct stats_details *const d = u;
1017
1018 d->exited = 1;
1019 if(status)
1020 error(0, "disorder-stats %s", wstat(status));
1021 stats_complete(d);
1022 return 0;
1023}
1024
1025static int stats_read(ev_source attribute((unused)) *ev,
1026 ev_reader *reader,
d6dde5a3
RK
1027 void *ptr,
1028 size_t bytes,
1029 int eof,
1030 void *u) {
1031 struct stats_details *const d = u;
1032
1033 dynstr_append_bytes(d->data, ptr, bytes);
1034 ev_reader_consume(reader, bytes);
1035 if(eof)
1036 d->closed = 1;
1037 stats_complete(d);
1038 return 0;
1039}
1040
1041static int stats_error(ev_source attribute((unused)) *ev,
d6dde5a3
RK
1042 int errno_value,
1043 void *u) {
1044 struct stats_details *const d = u;
1045
1046 error(errno_value, "error reading from pipe to disorder-stats");
1047 d->closed = 1;
1048 stats_complete(d);
1049 return 0;
1050}
1051
1052void trackdb_stats_subprocess(ev_source *ev,
1053 void (*done)(char *data, void *u),
1054 void *u) {
1055 int p[2];
1056 pid_t pid;
1057 struct stats_details *d = xmalloc(sizeof *d);
1058
1059 dynstr_init(d->data);
1060 d->done = done;
1061 d->u = u;
1062 xpipe(p);
1063 pid = subprogram(ev, "disorder-stats", p[1]);
1064 xclose(p[1]);
1065 ev_child(ev, pid, 0, stats_finished, d);
1066 ev_reader_new(ev, p[0], stats_read, stats_error, d, "disorder-stats reader");
1067}
1068
460b9539 1069/* set a pref (remove if value=0) */
1070int trackdb_set(const char *track,
1071 const char *name,
1072 const char *value) {
1073 struct kvp *t, *p, *a;
1074 DB_TXN *tid;
1075 int err, cmp;
1076 char *oldalias, *newalias, **oldtags = 0, **newtags;
1077
f26f062a 1078 if(value) {
1079 /* TODO: if value matches default then set value=0 */
1080 }
1081
460b9539 1082 for(;;) {
1083 tid = trackdb_begin_transaction();
1084 if((err = gettrackdata(track, &t, &p, 0,
1085 0, tid)) == DB_LOCK_DEADLOCK)
1086 goto fail;
1087 if(err == DB_NOTFOUND) break;
1088 if(name[0] == '_') {
1089 if(kvp_set(&t, name, value))
1090 if(trackdb_putdata(trackdb_tracksdb, track, t, tid, 0))
1091 goto fail;
1092 } else {
1093 /* get the old alias name */
1094 if(compute_alias(&oldalias, track, p, tid)) goto fail;
1095 /* get the old tags */
1096 if(!strcmp(name, "tags"))
1097 oldtags = parsetags(kvp_get(p, "tags"));
1098 /* set the value */
1099 if(kvp_set(&p, name, value))
1100 if(trackdb_putdata(trackdb_prefsdb, track, p, tid, 0))
1101 goto fail;
1102 /* compute the new alias name */
1103 if((err = compute_alias(&newalias, track, p, tid))) goto fail;
1104 /* check whether alias has changed */
1105 if(!(oldalias == newalias
1106 || (oldalias && newalias && !strcmp(oldalias, newalias)))) {
1107 /* adjust alias records to fit change */
1108 if(oldalias
1109 && trackdb_delkey(trackdb_tracksdb, oldalias, tid)) goto fail;
1110 if(newalias) {
1111 a = 0;
1112 kvp_set(&a, "_alias_for", track);
1113 if(trackdb_putdata(trackdb_tracksdb, newalias, a, tid, 0)) goto fail;
1114 }
1115 }
1116 /* check whether tags have changed */
1117 if(!strcmp(name, "tags")) {
1118 newtags = parsetags(value);
1119 while(*oldtags || *newtags) {
1120 if(*oldtags && *newtags) {
1121 cmp = strcmp(*oldtags, *newtags);
1122 if(!cmp) {
1123 /* keeping this tag */
1124 ++oldtags;
1125 ++newtags;
1126 } else if(cmp < 0)
1127 /* old tag fits into a gap in the new list, so delete old */
1128 goto delete_old;
1129 else
1130 /* new tag fits into a gap in the old list, so insert new */
1131 goto insert_new;
1132 } else if(*oldtags) {
1133 /* we've run out of new tags, so remaining old ones are to be
1134 * deleted */
1135 delete_old:
1136 if(trackdb_delkeydata(trackdb_tagsdb,
1137 *oldtags, track, tid) == DB_LOCK_DEADLOCK)
1138 goto fail;
1139 ++oldtags;
1140 } else {
1141 /* we've run out of old tags, so remainig new ones are to be
1142 * inserted */
1143 insert_new:
1144 if(register_tag(track, *newtags, tid)) goto fail;
1145 ++newtags;
1146 }
1147 }
1148 reqtracks = 0;
1149 }
1150 }
1151 err = 0;
1152 break;
1153fail:
1154 trackdb_abort_transaction(tid);
1155 }
1156 trackdb_commit_transaction(tid);
1157 return err == 0 ? 0 : -1;
1158}
1159
1160/* get a pref */
1161const char *trackdb_get(const char *track,
1162 const char *name) {
1163 return kvp_get(trackdb_get_all(track), name);
1164}
1165
1166/* get all prefs as a 0-terminated array */
1167struct kvp *trackdb_get_all(const char *track) {
1168 struct kvp *t, *p, **pp;
1169 DB_TXN *tid;
1170
1171 for(;;) {
1172 tid = trackdb_begin_transaction();
1173 if(gettrackdata(track, &t, &p, 0, 0, tid) == DB_LOCK_DEADLOCK)
1174 goto fail;
1175 break;
1176fail:
1177 trackdb_abort_transaction(tid);
1178 }
1179 trackdb_commit_transaction(tid);
1180 for(pp = &p; *pp; pp = &(*pp)->next)
1181 ;
1182 *pp = t;
1183 return p;
1184}
1185
1186/* resolve alias */
1187const char *trackdb_resolve(const char *track) {
1188 DB_TXN *tid;
1189 const char *actual;
1190
1191 for(;;) {
1192 tid = trackdb_begin_transaction();
1193 if(gettrackdata(track, 0, 0, &actual, 0, tid) == DB_LOCK_DEADLOCK)
1194 goto fail;
1195 break;
1196fail:
1197 trackdb_abort_transaction(tid);
1198 }
1199 trackdb_commit_transaction(tid);
1200 return actual;
1201}
1202
1203int trackdb_isalias(const char *track) {
1204 const char *actual = trackdb_resolve(track);
1205
1206 return strcmp(actual, track);
1207}
1208
1209/* test whether a track exists (perhaps an alias) */
1210int trackdb_exists(const char *track) {
1211 DB_TXN *tid;
1212 int err;
1213
1214 for(;;) {
1215 tid = trackdb_begin_transaction();
1216 /* unusually, here we want the return value */
1217 if((err = gettrackdata(track, 0, 0, 0, 0, tid)) == DB_LOCK_DEADLOCK)
1218 goto fail;
1219 break;
1220fail:
1221 trackdb_abort_transaction(tid);
1222 }
1223 trackdb_commit_transaction(tid);
1224 return (err == 0);
1225}
1226
1227/* return the list of tags */
1228char **trackdb_alltags(void) {
1229 DB_TXN *tid;
1230 int err;
1231 char **taglist;
1232
1233 for(;;) {
1234 tid = trackdb_begin_transaction();
1235 err = trackdb_alltags_tid(tid, &taglist);
1236 if(!err) break;
1237 trackdb_abort_transaction(tid);
1238 }
1239 trackdb_commit_transaction(tid);
1240 return taglist;
1241}
1242
1243static int trackdb_alltags_tid(DB_TXN *tid, char ***taglistp) {
1244 struct vector v;
1245 DBC *c;
1246 DBT k, d;
1247 int err;
1248
1249 vector_init(&v);
1250 c = trackdb_opencursor(trackdb_tagsdb, tid);
1251 memset(&k, 0, sizeof k);
1252 while(!(err = c->c_get(c, &k, prepare_data(&d), DB_NEXT_NODUP)))
1253 vector_append(&v, xstrndup(k.data, k.size));
1254 switch(err) {
1255 case DB_NOTFOUND:
1256 break;
1257 case DB_LOCK_DEADLOCK:
1258 return err;
1259 default:
1260 fatal(0, "c->c_get: %s", db_strerror(err));
1261 }
1262 if((err = trackdb_closecursor(c))) return err;
1263 vector_terminate(&v);
1264 *taglistp = v.vec;
1265 return 0;
1266}
1267
1268/* return 1 iff sorted tag lists A and B have at least one member in common */
1269static int tag_intersection(char **a, char **b) {
1270 int cmp;
1271
1272 /* Same sort of logic as trackdb_set() above */
1273 while(*a && *b) {
1274 if(!(cmp = strcmp(*a, *b))) return 1;
1275 else if(cmp < 0) ++a;
1276 else ++b;
1277 }
1278 return 0;
1279}
1280
1281/* Check whether a track is suitable for random play. Returns 0 if it is,
6aba3f6c
RK
1282 * DB_NOTFOUND if it is not or DB_LOCK_DEADLOCK if the database gave us
1283 * that. */
460b9539 1284static int check_suitable(const char *track,
1285 DB_TXN *tid,
1286 char **required_tags,
1287 char **prohibited_tags) {
1288 char **track_tags;
1289 time_t last, now;
1290 struct kvp *p, *t;
1291 const char *pick_at_random, *played_time;
1292
6aba3f6c
RK
1293 /* don't pick tracks that aren't in any surviving collection (for instance
1294 * you've edited the config but the rescan hasn't done its job yet) */
1295 if(!find_track_root(track)) {
1296 info("found track not in any collection: %s", track);
1297 return DB_NOTFOUND;
1298 }
460b9539 1299 /* don't pick aliases - only pick the canonical form */
1300 if(gettrackdata(track, &t, &p, 0, 0, tid) == DB_LOCK_DEADLOCK)
1301 return DB_LOCK_DEADLOCK;
1302 if(kvp_get(t, "_alias_for"))
1303 return DB_NOTFOUND;
1304 /* check that random play is not suppressed for this track */
1305 if((pick_at_random = kvp_get(p, "pick_at_random"))
1306 && !strcmp(pick_at_random, "0"))
1307 return DB_NOTFOUND;
1308 /* don't pick a track that's been played in the last 8 hours */
1309 if((played_time = kvp_get(p, "played_time"))) {
1310 last = atoll(played_time);
1311 now = time(0);
1312 if(now < last + 8 * 3600) /* TODO configurable */
1313 return DB_NOTFOUND;
1314 }
1315 track_tags = parsetags(kvp_get(p, "tags"));
1316 /* check that no prohibited tag is present for this track */
1317 if(prohibited_tags && tag_intersection(track_tags, prohibited_tags))
1318 return DB_NOTFOUND;
1319 /* check that at least one required tags is present for this track */
1320 if(*required_tags && !tag_intersection(track_tags, required_tags))
1321 return DB_NOTFOUND;
1322 return 0;
1323}
1324
1325/* attempt to pick a random non-alias track */
1326const char *trackdb_random(int tries) {
1327 DBT key, data;
1328 DB_BTREE_STAT *sp;
1329 int err, n;
1330 DB_TXN *tid;
1331 const char *track, *candidate;
1332 db_recno_t r;
1333 const char *tags;
1334 char **required_tags, **prohibited_tags, **tp;
1335 hash *h;
1336 DBC *c = 0;
1337
1338 for(;;) {
1339 tid = trackdb_begin_transaction();
1340 if((err = trackdb_get_global_tid("required-tags", tid, &tags)))
1341 goto fail;
1342 required_tags = parsetags(tags);
1343 if((err = trackdb_get_global_tid("prohibited-tags", tid, &tags)))
1344 goto fail;
1345 prohibited_tags = parsetags(tags);
1346 track = 0;
1347 if(*required_tags) {
1348 /* Bung all the suitable tracks into a hash and convert to a list of keys
1349 * (to eliminate duplicates). We cache this list since it is possible
1350 * that it will be very large. */
1351 if(!reqtracks) {
1352 h = hash_new(0);
1353 for(tp = required_tags; *tp; ++tp) {
1354 c = trackdb_opencursor(trackdb_tagsdb, tid);
1355 memset(&key, 0, sizeof key);
1356 key.data = *tp;
1357 key.size = strlen(*tp);
1358 n = 0;
1359 err = c->c_get(c, &key, prepare_data(&data), DB_SET);
1360 while(err == 0) {
1361 hash_add(h, xstrndup(data.data, data.size), 0,
1362 HASH_INSERT_OR_REPLACE);
1363 ++n;
1364 err = c->c_get(c, &key, prepare_data(&data), DB_NEXT_DUP);
1365 }
1366 switch(err) {
1367 case 0:
1368 case DB_NOTFOUND:
1369 break;
1370 case DB_LOCK_DEADLOCK:
1371 goto fail;
1372 default:
1373 fatal(0, "error querying tags.db: %s", db_strerror(err));
1374 }
1375 trackdb_closecursor(c);
1376 c = 0;
1377 if(!n)
1378 error(0, "required tag %s does not match any tracks", *tp);
1379 }
1380 nreqtracks = hash_count(h);
1381 reqtracks = hash_keys(h);
1382 }
1383 while(nreqtracks && !track && tries-- > 0) {
1384 r = (rand() * (double)nreqtracks / (RAND_MAX + 1.0));
1385 candidate = reqtracks[r];
1386 switch(check_suitable(candidate, tid,
1387 required_tags, prohibited_tags)) {
1388 case 0:
1389 track = candidate;
1390 break;
1391 case DB_NOTFOUND:
1392 break;
1393 case DB_LOCK_DEADLOCK:
1394 goto fail;
1395 }
1396 }
1397 } else {
1398 /* No required tags. We pick random record numbers in the database
1399 * instead. */
58a4416c 1400 switch(err = trackdb_tracksdb->stat(trackdb_tracksdb, tid, &sp, 0)) {
460b9539 1401 case 0:
1402 break;
1403 case DB_LOCK_DEADLOCK:
1404 error(0, "error querying tracks.db: %s", db_strerror(err));
1405 goto fail;
1406 default:
1407 fatal(0, "error querying tracks.db: %s", db_strerror(err));
1408 }
1409 if(!sp->bt_nkeys)
1410 error(0, "cannot pick tracks at random from an empty database");
1411 while(sp->bt_nkeys && !track && tries-- > 0) {
1412 /* record numbers count from 1 upwards */
1413 r = 1 + (rand() * (double)sp->bt_nkeys / (RAND_MAX + 1.0));
1414 memset(&key, sizeof key, 0);
1415 key.flags = DB_DBT_MALLOC;
1416 key.size = sizeof r;
1417 key.data = &r;
1418 switch(err = trackdb_tracksdb->get(trackdb_tracksdb, tid, &key, prepare_data(&data),
1419 DB_SET_RECNO)) {
1420 case 0:
1421 break;
1422 case DB_LOCK_DEADLOCK:
1423 error(0, "error querying tracks.db: %s", db_strerror(err));
1424 goto fail;
1425 default:
1426 fatal(0, "error querying tracks.db: %s", db_strerror(err));
1427 }
1428 candidate = xstrndup(key.data, key.size);
1429 switch(check_suitable(candidate, tid,
1430 required_tags, prohibited_tags)) {
1431 case 0:
1432 track = candidate;
1433 break;
1434 case DB_NOTFOUND:
1435 break;
1436 case DB_LOCK_DEADLOCK:
1437 goto fail;
1438 }
1439 }
1440 }
1441 break;
1442fail:
1443 trackdb_closecursor(c);
1444 c = 0;
1445 trackdb_abort_transaction(tid);
1446 }
1447 trackdb_commit_transaction(tid);
1448 if(!track)
1449 error(0, "could not pick a random track");
1450 return track;
1451}
1452
1453/* get a track name given the prefs. Set *used_db to 1 if we got the answer
1454 * from the prefs. */
1455static const char *getpart(const char *track,
1456 const char *context,
1457 const char *part,
1458 const struct kvp *p,
1459 int *used_db) {
1460 const char *result;
1461 char *pref;
1462
1463 byte_xasprintf(&pref, "trackname_%s_%s", context, part);
1464 if((result = kvp_get(p, pref)))
1465 *used_db = 1;
1466 else
1467 result = trackname_part(track, context, part);
1468 assert(result != 0);
1469 return result;
1470}
1471
1472/* get a track name part, like trackname_part(), but taking the database into
1473 * account. */
1474const char *trackdb_getpart(const char *track,
1475 const char *context,
1476 const char *part) {
1477 struct kvp *p;
1478 DB_TXN *tid;
1479 char *pref;
1480 const char *actual;
1481 int used_db, err;
1482
1483 /* construct the full pref */
1484 byte_xasprintf(&pref, "trackname_%s_%s", context, part);
1485 for(;;) {
1486 tid = trackdb_begin_transaction();
1487 if((err = gettrackdata(track, 0, &p, &actual, 0, tid)) == DB_LOCK_DEADLOCK)
1488 goto fail;
1489 break;
1490fail:
1491 trackdb_abort_transaction(tid);
1492 }
1493 trackdb_commit_transaction(tid);
1494 return getpart(actual, context, part, p, &used_db);
1495}
1496
1497/* get the raw path name for @track@ (might be an alias) */
1498const char *trackdb_rawpath(const char *track) {
1499 DB_TXN *tid;
1500 struct kvp *t;
1501 const char *path;
1502
1503 for(;;) {
1504 tid = trackdb_begin_transaction();
1505 if(gettrackdata(track, &t, 0, 0, 0, tid) == DB_LOCK_DEADLOCK)
1506 goto fail;
1507 break;
1508fail:
1509 trackdb_abort_transaction(tid);
1510 }
1511 trackdb_commit_transaction(tid);
1512 if(!(path = kvp_get(t, "_path"))) path = track;
1513 return path;
1514}
1515
1516/* trackdb_list **************************************************************/
1517
1518/* this is incredibly ugly, sorry, perhaps it will be rewritten to be actually
1519 * readable at some point */
1520
1521/* return true if the basename of TRACK[0..TL-1], as defined by DL, matches RE.
1522 * If RE is a null pointer then it matches everything. */
1523static int track_matches(size_t dl, const char *track, size_t tl,
1524 const pcre *re) {
1525 int ovec[3], rc;
1526
1527 if(!re)
1528 return 1;
1529 track += dl + 1;
1530 tl -= (dl + 1);
1531 switch(rc = pcre_exec(re, 0, track, tl, 0, 0, ovec, 3)) {
1532 case PCRE_ERROR_NOMATCH: return 0;
1533 default:
1534 if(rc < 0) {
1535 error(0, "pcre_exec returned %d, subject '%s'", rc, track);
1536 return 0;
1537 }
1538 return 1;
1539 }
1540}
1541
1542static int do_list(struct vector *v, const char *dir,
1543 enum trackdb_listable what, const pcre *re, DB_TXN *tid) {
1544 DBC *cursor;
1545 DBT k, d;
1546 size_t dl;
1547 char *ptr;
1548 int err;
1549 size_t l, last_dir_len = 0;
1550 char *last_dir = 0, *track, *alias;
1551 struct kvp *p;
1552
1553 dl = strlen(dir);
1554 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
1555 make_key(&k, dir);
1556 prepare_data(&d);
1557 /* find the first key >= dir */
1558 err = cursor->c_get(cursor, &k, &d, DB_SET_RANGE);
1559 /* keep going while we're dealing with <dir/anything> */
1560 while(err == 0
1561 && k.size > dl
1562 && ((char *)k.data)[dl] == '/'
1563 && !memcmp(k.data, dir, dl)) {
1564 ptr = memchr((char *)k.data + dl + 1, '/', k.size - (dl + 1));
1565 if(ptr) {
1566 /* we have <dir/component/anything>, so <dir/component> is a directory */
1567 l = ptr - (char *)k.data;
1568 if(what & trackdb_directories)
1569 if(!(last_dir
1570 && l == last_dir_len
1571 && !memcmp(last_dir, k.data, l))) {
1572 last_dir = xstrndup(k.data, last_dir_len = l);
1573 if(track_matches(dl, k.data, l, re))
1574 vector_append(v, last_dir);
1575 }
1576 } else {
1577 /* found a plain file */
1578 if((what & trackdb_files)) {
1579 track = xstrndup(k.data, k.size);
1580 if((err = trackdb_getdata(trackdb_prefsdb,
1581 track, &p, tid)) == DB_LOCK_DEADLOCK)
1582 goto deadlocked;
1583 /* if this file has an alias in the same directory then we skip it */
1584 if((err = compute_alias(&alias, track, p, tid)))
1585 goto deadlocked;
1586 if(!(alias && !strcmp(d_dirname(alias), d_dirname(track))))
1587 if(track_matches(dl, k.data, k.size, re))
1588 vector_append(v, track);
1589 }
1590 }
1591 err = cursor->c_get(cursor, &k, &d, DB_NEXT);
1592 }
1593 switch(err) {
1594 case 0:
1595 break;
1596 case DB_NOTFOUND:
1597 err = 0;
1598 break;
1599 case DB_LOCK_DEADLOCK:
1600 error(0, "error querying database: %s", db_strerror(err));
1601 break;
1602 default:
1603 fatal(0, "error querying database: %s", db_strerror(err));
1604 }
1605deadlocked:
1606 if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK;
1607 return err;
1608}
1609
1610/* return the directories or files below @dir@ */
1611char **trackdb_list(const char *dir, int *np, enum trackdb_listable what,
1612 const pcre *re) {
1613 DB_TXN *tid;
1614 int n;
1615 struct vector v;
1616
1617 vector_init(&v);
1618 for(;;) {
1619 tid = trackdb_begin_transaction();
1620 v.nvec = 0;
1621 if(dir) {
1622 if(do_list(&v, dir, what, re, tid))
1623 goto fail;
1624 } else {
1625 for(n = 0; n < config->collection.n; ++n)
1626 if(do_list(&v, config->collection.s[n].root, what, re, tid))
1627 goto fail;
1628 }
1629 break;
1630fail:
1631 trackdb_abort_transaction(tid);
1632 }
1633 trackdb_commit_transaction(tid);
1634 vector_terminate(&v);
1635 if(np)
1636 *np = v.nvec;
1637 return v.vec;
1638}
1639
1640/* If S is tag:something, return something. Else return 0. */
1641static const char *checktag(const char *s) {
1642 if(!strncmp(s, "tag:", 4))
1643 return s + 4;
1644 else
1645 return 0;
1646}
1647
1648/* return a list of tracks containing all of the words given. If you
1649 * ask for only stopwords you get no tracks. */
1650char **trackdb_search(char **wordlist, int nwordlist, int *ntracks) {
1651 const char **w, *best = 0, *tag;
1652 char **twords, **tags;
1653 int i, j, n, err, what;
1654 DBC *cursor = 0;
1655 DBT k, d;
1656 struct vector u, v;
1657 DB_TXN *tid;
1658 struct kvp *p;
1659 int ntags = 0;
1660 DB *db;
1661 const char *dbname;
1662
1663 *ntracks = 0; /* for early returns */
1664 /* casefold all the words */
1665 w = xmalloc(nwordlist * sizeof (char *));
1666 for(n = 0; n < nwordlist; ++n) {
1667 w[n] = casefold(wordlist[n]);
1668 if(checktag(w[n])) ++ntags; /* count up tags */
1669 }
1670 /* find the longest non-stopword */
1671 for(n = 0; n < nwordlist; ++n)
1672 if(!stopword(w[n]) && !checktag(w[n]))
1673 if(!best || strlen(w[n]) > strlen(best))
1674 best = w[n];
1675 /* TODO: we should at least in principal be able to identify the word or tag
1676 * with the least matches in log time, and choose that as our primary search
1677 * term. */
1678 if(ntags && !best) {
1679 /* Only tags are listed. We limit to the first and narrow down with the
1680 * rest. */
1681 best = checktag(w[0]);
1682 db = trackdb_tagsdb;
1683 dbname = "tags";
1684 } else if(best) {
1685 /* We can limit to some word. */
1686 db = trackdb_searchdb;
1687 dbname = "search";
1688 } else {
1689 /* Only stopwords */
1690 return 0;
1691 }
1692 vector_init(&u);
1693 vector_init(&v);
1694 for(;;) {
1695 tid = trackdb_begin_transaction();
1696 /* find all the tracks that have that word */
1697 make_key(&k, best);
1698 prepare_data(&d);
1699 what = DB_SET;
1700 v.nvec = 0;
1701 cursor = trackdb_opencursor(db, tid);
1702 while(!(err = cursor->c_get(cursor, &k, &d, what))) {
1703 vector_append(&v, xstrndup(d.data, d.size));
1704 what = DB_NEXT_DUP;
1705 }
1706 switch(err) {
1707 case DB_NOTFOUND:
1708 err = 0;
1709 break;
1710 case DB_LOCK_DEADLOCK:
1711 error(0, "error querying %s database: %s", dbname, db_strerror(err));
1712 break;
1713 default:
1714 fatal(0, "error querying %s database: %s", dbname, db_strerror(err));
1715 }
1716 if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK;
1717 cursor = 0;
1718 /* do a naive search over that (hopefuly fairly small) list of tracks */
1719 u.nvec = 0;
1720 for(n = 0; n < v.nvec; ++n) {
1721 if((err = gettrackdata(v.vec[n], 0, &p, 0, 0, tid) == DB_LOCK_DEADLOCK))
1722 goto fail;
1723 else if(err) {
1724 error(0, "track %s unexpected error: %s", v.vec[n], db_strerror(err));
1725 continue;
1726 }
1727 twords = track_to_words(v.vec[n], p);
1728 tags = parsetags(kvp_get(p, "tags"));
1729 for(i = 0; i < nwordlist; ++i) {
1730 if((tag = checktag(w[i]))) {
1731 /* Track must have this tag */
1732 for(j = 0; tags[j]; ++j)
1733 if(!strcmp(tag, tags[j])) break; /* tag found */
1734 if(!tags[j]) break; /* tag not found */
1735 } else {
1736 /* Track must contain this word */
1737 for(j = 0; twords[j]; ++j)
1738 if(!strcmp(w[i], twords[j])) break; /* word found */
1739 if(!twords[j]) break; /* word not found */
1740 }
1741 }
1742 if(i >= nwordlist) /* all words found */
1743 vector_append(&u, v.vec[n]);
1744 }
1745 break;
1746 fail:
1747 trackdb_closecursor(cursor);
1748 cursor = 0;
1749 trackdb_abort_transaction(tid);
1750 info("retrying search");
1751 }
1752 trackdb_commit_transaction(tid);
1753 vector_terminate(&u);
1754 if(ntracks)
1755 *ntracks = u.nvec;
1756 return u.vec;
1757}
1758
1759/* trackdb_scan **************************************************************/
1760
1761int trackdb_scan(const char *root,
1762 int (*callback)(const char *track,
1763 struct kvp *data,
1764 void *u,
1765 DB_TXN *tid),
1766 void *u,
1767 DB_TXN *tid) {
1768 DBC *cursor;
1769 DBT k, d;
d1694464 1770 const size_t root_len = root ? strlen(root) : 0;
1771 int err, cberr;
460b9539 1772 struct kvp *data;
d1694464 1773 const char *track;
460b9539 1774
1775 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
d1694464 1776 if(root)
1777 err = cursor->c_get(cursor, make_key(&k, root), prepare_data(&d),
1778 DB_SET_RANGE);
1779 else {
1780 memset(&k, 0, sizeof k);
1781 err = cursor->c_get(cursor, &k, prepare_data(&d),
1782 DB_FIRST);
1783 }
460b9539 1784 while(!err) {
d1694464 1785 if(!root
1786 || (k.size > root_len
1787 && !strncmp(k.data, root, root_len)
1788 && ((char *)k.data)[root_len] == '/')) {
460b9539 1789 data = kvp_urldecode(d.data, d.size);
d1694464 1790 if(kvp_get(data, "_path")) {
1791 track = xstrndup(k.data, k.size);
1792 /* Advance to the next track before the callback so that the callback
1793 * may safely delete the track */
1794 err = cursor->c_get(cursor, &k, &d, DB_NEXT);
1795 if((cberr = callback(track, data, u, tid))) {
1796 err = cberr;
460b9539 1797 break;
d1694464 1798 }
1799 } else
1800 err = cursor->c_get(cursor, &k, &d, DB_NEXT);
460b9539 1801 } else
1802 break;
1803 }
1804 trackdb_closecursor(cursor);
1805 switch(err) {
1806 case EINTR:
1807 return err;
1808 case 0:
1809 case DB_NOTFOUND:
1810 return 0;
1811 case DB_LOCK_DEADLOCK:
1812 error(0, "c->c_get: %s", db_strerror(err));
1813 return err;
1814 default:
1815 fatal(0, "c->c_get: %s", db_strerror(err));
1816 }
1817}
1818
1819/* trackdb_rescan ************************************************************/
1820
1821/* called when the rescanner terminates */
1822static int reap_rescan(ev_source attribute((unused)) *ev,
1823 pid_t pid,
1824 int status,
1825 const struct rusage attribute((unused)) *rusage,
1826 void attribute((unused)) *u) {
1827 if(pid == rescan_pid) rescan_pid = -1;
1828 if(status)
1829 error(0, "disorderd-rescan: %s", wstat(status));
1830 else
1831 D(("disorderd-rescan terminate: %s", wstat(status)));
1832 /* Our cache of file lookups is out of date now */
1833 cache_clean(&cache_files_type);
e025abff 1834 eventlog("rescanned", (char *)0);
460b9539 1835 return 0;
1836}
1837
1838void trackdb_rescan(ev_source *ev) {
1839 if(rescan_pid != -1) {
1840 error(0, "rescan already underway");
1841 return;
1842 }
d6dde5a3 1843 rescan_pid = subprogram(ev, RESCAN, -1);
460b9539 1844 ev_child(ev, rescan_pid, 0, reap_rescan, 0);
1845 D(("started rescanner"));
1846
1847}
1848
1849int trackdb_rescan_cancel(void) {
1850 if(rescan_pid == -1) return 0;
1851 if(kill(rescan_pid, SIGTERM) < 0)
1852 fatal(errno, "error killing rescanner");
1853 rescan_pid = -1;
1854 return 1;
1855}
1856
1857/* global prefs **************************************************************/
1858
1859void trackdb_set_global(const char *name,
1860 const char *value,
1861 const char *who) {
1862 DB_TXN *tid;
1863 DBT k, d;
1864 int err;
1865 int state;
1866
1867 memset(&k, 0, sizeof k);
1868 memset(&d, 0, sizeof d);
1869 k.data = (void *)name;
1870 k.size = strlen(name);
1871 if(value) {
1872 d.data = (void *)value;
1873 d.size = strlen(value);
1874 }
1875 for(;;) {
1876 tid = trackdb_begin_transaction();
1877 if(value)
1878 err = trackdb_globaldb->put(trackdb_globaldb, tid, &k, &d, 0);
1879 else
1880 err = trackdb_globaldb->del(trackdb_globaldb, tid, &k, 0);
1881 if(!err || err == DB_NOTFOUND) break;
1882 if(err != DB_LOCK_DEADLOCK)
1883 fatal(0, "error updating database: %s", db_strerror(err));
1884 trackdb_abort_transaction(tid);
1885 }
1886 trackdb_commit_transaction(tid);
1887 /* log important state changes */
1888 if(!strcmp(name, "playing")) {
1889 state = !value || !strcmp(value, "yes");
1890 info("playing %s by %s",
1891 state ? "enabled" : "disabled",
1892 who ? who : "-");
1893 eventlog("state", state ? "enable_play" : "disable_play", (char *)0);
1894 }
1895 if(!strcmp(name, "random-play")) {
1896 state = !value || !strcmp(value, "yes");
1897 info("random play %s by %s",
1898 state ? "enabled" : "disabled",
1899 who ? who : "-");
1900 eventlog("state", state ? "enable_random" : "disable_random", (char *)0);
1901 }
1902 if(!strcmp(name, "required-tags"))
1903 reqtracks = 0;
1904}
1905
1906const char *trackdb_get_global(const char *name) {
1907 DB_TXN *tid;
1908 int err;
1909 const char *r;
1910
1911 for(;;) {
1912 tid = trackdb_begin_transaction();
1913 if(!(err = trackdb_get_global_tid(name, tid, &r)))
1914 break;
1915 trackdb_abort_transaction(tid);
1916 }
1917 trackdb_commit_transaction(tid);
1918 return r;
1919}
1920
1921static int trackdb_get_global_tid(const char *name,
1922 DB_TXN *tid,
1923 const char **rp) {
1924 DBT k, d;
1925 int err;
1926
1927 memset(&k, 0, sizeof k);
1928 k.data = (void *)name;
1929 k.size = strlen(name);
1930 switch(err = trackdb_globaldb->get(trackdb_globaldb, tid, &k,
1931 prepare_data(&d), 0)) {
1932 case 0:
1933 *rp = xstrndup(d.data, d.size);
1934 return 0;
1935 case DB_NOTFOUND:
1936 *rp = 0;
1937 return 0;
1938 case DB_LOCK_DEADLOCK:
1939 return err;
1940 default:
1941 fatal(0, "error updating database: %s", db_strerror(err));
1942 }
1943}
1944
2a10b70b
RK
1945/** @brief Retrieve the most recently added tracks
1946 * @param ntracksp Where to put count, or 0
1947 * @param maxtracks Maximum number of tracks to retrieve
1948 * @return null-terminated array of track names
1949 *
1950 * The most recently added track is first in the array.
1951 */
1952char **trackdb_new(int *ntracksp,
1953 int maxtracks) {
1954 DB_TXN *tid;
1955 char **tracks;
1956
1957 for(;;) {
1958 tid = trackdb_begin_transaction();
1959 tracks = trackdb_new_tid(ntracksp, maxtracks, tid);
1960 if(tracks)
1961 break;
1962 trackdb_abort_transaction(tid);
1963 }
1964 trackdb_commit_transaction(tid);
1965 return tracks;
1966}
1967
1968/** @brief Retrieve the most recently added tracks
1969 * @param ntracksp Where to put count, or 0
1970 * @param maxtracks Maximum number of tracks to retrieve, or 0 for all
1971 * @param tid Transaction ID
1972 * @return null-terminated array of track names, or NULL on deadlock
1973 *
1974 * The most recently added track is first in the array.
1975 */
1976static char **trackdb_new_tid(int *ntracksp,
1977 int maxtracks,
1978 DB_TXN *tid) {
1979 DBC *c;
1980 DBT k, d;
1981 int err = 0;
1982 struct vector tracks[1];
1983
1984 vector_init(tracks);
1985 c = trackdb_opencursor(trackdb_noticeddb, tid);
1986 while((maxtracks <= 0 || tracks->nvec < maxtracks)
1987 && !(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_PREV)))
1988 vector_append(tracks, xstrndup(d.data, d.size));
1989 switch(err) {
1990 case 0: /* hit maxtracks */
1991 case DB_NOTFOUND: /* ran out of tracks */
1992 break;
1993 case DB_LOCK_DEADLOCK:
1994 trackdb_closecursor(c);
1995 return 0;
1996 default:
1997 fatal(0, "error reading noticed.db: %s", db_strerror(err));
1998 }
1999 if((err = trackdb_closecursor(c)))
2000 return 0; /* deadlock */
2001 vector_terminate(tracks);
2002 if(ntracksp)
2003 *ntracksp = tracks->nvec;
2004 return tracks->vec;
2005}
2006
1e64e9fb
RK
2007/** @brief Expire noticed.db
2008 * @param earliest Earliest timestamp to keep
2009 */
2010void trackdb_expire_noticed(time_t earliest) {
2011 DB_TXN *tid;
2012
2013 for(;;) {
2014 tid = trackdb_begin_transaction();
2015 if(!trackdb_expire_noticed_tid(earliest, tid))
2016 break;
2017 trackdb_abort_transaction(tid);
2018 }
2019 trackdb_commit_transaction(tid);
2020}
2021
2022/** @brief Expire noticed.db
2023 * @param earliest Earliest timestamp to keep
2024 * @param tid Transaction ID
2025 * @return 0 or DB_LOCK_DEADLOCK
2026 */
2027static int trackdb_expire_noticed_tid(time_t earliest, DB_TXN *tid) {
2028 DBC *c;
2029 DBT k, d;
2030 int err = 0, ret;
2031 time_t when;
2032 uint32_t *kk;
2033 int count = 0;
2034
2035 c = trackdb_opencursor(trackdb_noticeddb, tid);
2036 while(!(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_NEXT))) {
2037 kk = k.data;
2038 when = (time_t)(((uint64_t)ntohl(kk[0]) << 32) + ntohl(kk[1]));
2039 if(when >= earliest)
2040 break;
2041 if((err = c->c_del(c, 0))) {
2042 if(err != DB_LOCK_DEADLOCK)
2043 fatal(0, "error deleting expired noticed.db entry: %s",
2044 db_strerror(err));
2045 break;
2046 }
2047 ++count;
2048 }
2049 if(err == DB_NOTFOUND)
2050 err = 0;
2051 if(err && err != DB_LOCK_DEADLOCK)
2052 fatal(0, "error expiring noticed.db: %s", db_strerror(err));
2053 ret = err;
2054 if((err = trackdb_closecursor(c))) {
2055 if(err != DB_LOCK_DEADLOCK)
2056 fatal(0, "error closing cursor: %s", db_strerror(err));
2057 ret = err;
2058 }
2059 if(!ret && count)
2060 info("expired %d tracks from noticed.db", count);
2061 return ret;
2062}
2063
460b9539 2064/* tidying up ****************************************************************/
2065
2066void trackdb_gc(void) {
2067 int err;
2068 char **logfiles;
2069
2070 if((err = trackdb_env->txn_checkpoint(trackdb_env,
2071 config->checkpoint_kbyte,
2072 config->checkpoint_min,
2073 0)))
2074 fatal(0, "trackdb_env->txn_checkpoint: %s", db_strerror(err));
2075 if((err = trackdb_env->log_archive(trackdb_env, &logfiles, DB_ARCH_REMOVE)))
2076 fatal(0, "trackdb_env->log_archive: %s", db_strerror(err));
2077 /* This makes catastrophic recovery impossible. However, the user can still
2078 * preserve the important data by using disorder-dump to snapshot their
2079 * prefs, and later to restore it. This is likely to have much small
2080 * long-term storage requirements than record the db logfiles. */
2081}
2082
2083/*
2084Local Variables:
2085c-basic-offset:2
2086comment-column:40
2087fill-column:79
2088indent-tabs-mode:nil
2089End:
2090*/