Commit | Line | Data |
---|---|---|
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 | */ | |
158d0961 | 20 | /** @file lib/trackdb.c |
f0feb22e RK |
21 | * @brief Track database |
22 | * | |
23 | * This file is getting in desparate need of splitting up... | |
24 | */ | |
460b9539 | 25 | |
26 | #include <config.h> | |
27 | #include "types.h" | |
28 | ||
29 | #include <string.h> | |
30 | #include <stdio.h> | |
31 | #include <db.h> | |
32 | #include <sys/socket.h> | |
33 | #include <pcre.h> | |
34 | #include <assert.h> | |
35 | #include <unistd.h> | |
36 | #include <errno.h> | |
37 | #include <stddef.h> | |
38 | #include <sys/time.h> | |
39 | #include <sys/resource.h> | |
40 | #include <time.h> | |
2a10b70b | 41 | #include <arpa/inet.h> |
ad2f8275 | 42 | #include <sys/wait.h> |
a745dd43 RK |
43 | #include <dirent.h> |
44 | #include <sys/stat.h> | |
f0feb22e | 45 | #include <gcrypt.h> |
460b9539 | 46 | |
47 | #include "event.h" | |
48 | #include "mem.h" | |
49 | #include "kvp.h" | |
50 | #include "log.h" | |
51 | #include "vector.h" | |
5df73aeb | 52 | #include "rights.h" |
460b9539 | 53 | #include "trackdb.h" |
54 | #include "configuration.h" | |
55 | #include "syscalls.h" | |
56 | #include "wstat.h" | |
460b9539 | 57 | #include "printf.h" |
58 | #include "filepart.h" | |
59 | #include "trackname.h" | |
60 | #include "trackdb-int.h" | |
61 | #include "logfd.h" | |
62 | #include "cache.h" | |
63 | #include "eventlog.h" | |
64 | #include "hash.h" | |
c85b7022 RK |
65 | #include "unicode.h" |
66 | #include "unidata.h" | |
fce810c2 | 67 | #include "base64.h" |
460b9539 | 68 | |
69 | #define RESCAN "disorder-rescan" | |
70 | #define DEADLOCK "disorder-deadlock" | |
71 | ||
72 | static const char *getpart(const char *track, | |
73 | const char *context, | |
74 | const char *part, | |
75 | const struct kvp *p, | |
76 | int *used_db); | |
2a10b70b RK |
77 | static char **trackdb_new_tid(int *ntracksp, |
78 | int maxtracks, | |
79 | DB_TXN *tid); | |
1e64e9fb | 80 | static int trackdb_expire_noticed_tid(time_t earliest, DB_TXN *tid); |
a3777123 | 81 | static char *normalize_tag(const char *s, size_t ns); |
460b9539 | 82 | |
83 | const struct cache_type cache_files_type = { 86400 }; | |
84 | unsigned long cache_files_hits, cache_files_misses; | |
85 | ||
f982b120 RK |
86 | /** @brief Set by trackdb_open() */ |
87 | int trackdb_existing_database; | |
88 | ||
460b9539 | 89 | /* setup and teardown ********************************************************/ |
90 | ||
91 | static const char *home; /* home had better not change */ | |
92 | DB_ENV *trackdb_env; /* db environment */ | |
f9635e06 RK |
93 | |
94 | /** @brief The tracks database | |
95 | * - Keys are UTF-8(NFC(unicode(path name))) | |
96 | * - Values are encoded key-value pairs | |
97 | * - Data is reconstructable data about tracks that currently exist | |
98 | */ | |
99 | DB *trackdb_tracksdb; | |
100 | ||
101 | /** @brief The preferences database | |
102 | * | |
103 | * - Keys are UTF-8(NFC(unicode(path name))) | |
104 | * - Values are encoded key-value pairs | |
105 | * - Data is user data about tracks (that might not exist any more) | |
106 | * and cannot be reconstructed | |
107 | */ | |
108 | DB *trackdb_prefsdb; | |
109 | ||
110 | /** @brief The search database | |
111 | * | |
112 | * - Keys are UTF-8(NFKC(casefold(search term))) | |
113 | * - Values are UTF-8(NFC(unicode(path name))) | |
114 | * - There can be more than one value per key | |
115 | * - Presence of key,value means that path matches the search terms | |
158d0961 | 116 | * - Only tracks fond in @ref trackdb_tracksdb are represented here |
f9635e06 RK |
117 | * - This database can be reconstructed, it contains no user data |
118 | */ | |
119 | DB *trackdb_searchdb; | |
120 | ||
121 | /** @brief The tags database | |
122 | * | |
123 | * - Keys are UTF-8(NFKC(casefold(tag))) | |
124 | * - Values are UTF-8(NFC(unicode(path name))) | |
125 | * - There can be more than one value per key | |
126 | * - Presence of key,value means that path matches the tag | |
127 | * - This is always in sync with the tags preference | |
128 | * - This database can be reconstructed, it contains no user data | |
129 | */ | |
460b9539 | 130 | DB *trackdb_tagsdb; /* the tags database */ |
f9635e06 RK |
131 | |
132 | /** @brief The global preferences database | |
133 | * - Keys are UTF-8(NFC(preference)) | |
134 | * - Values are global preference values | |
135 | * - Data is user data and cannot be reconstructed | |
136 | */ | |
460b9539 | 137 | DB *trackdb_globaldb; /* global preferences */ |
f9635e06 RK |
138 | |
139 | /** @brief The noticed database | |
140 | * - Keys are 64-bit big-endian timestamps | |
141 | * - Values are UTF-8(NFC(unicode(path name))) | |
142 | * - There can be more than one value per key | |
143 | * - Presence of key,value means that path was added at the given time | |
144 | * - Data cannot be reconstructed (but isn't THAT important) | |
145 | */ | |
2a10b70b | 146 | DB *trackdb_noticeddb; /* when track noticed */ |
a745dd43 RK |
147 | |
148 | /** @brief The user database | |
149 | * - Keys are usernames | |
150 | * - Values are encoded key-value pairs | |
151 | * - Data is user data and cannot be reconstructed | |
152 | */ | |
153 | DB *trackdb_usersdb; | |
154 | ||
460b9539 | 155 | static pid_t db_deadlock_pid = -1; /* deadlock manager PID */ |
156 | static pid_t rescan_pid = -1; /* rescanner PID */ | |
157 | static int initialized, opened; /* state */ | |
158 | ||
159 | /* tracks matched by required_tags */ | |
160 | static char **reqtracks; | |
161 | static size_t nreqtracks; | |
162 | ||
163 | /* comparison function for keys */ | |
164 | static int compare(DB attribute((unused)) *db_, | |
165 | const DBT *a, const DBT *b) { | |
166 | return compare_path_raw(a->data, a->size, b->data, b->size); | |
167 | } | |
168 | ||
d25c4615 RK |
169 | /** @brief Open database environment |
170 | * @param flags Flags word | |
171 | * | |
172 | * Flags should be one of: | |
173 | * - @ref TRACKDB_NO_RECOVER | |
174 | * - @ref TRACKDB_NORMAL_RECOVER | |
175 | * - @ref TRACKDB_FATAL_RECOVER | |
a745dd43 | 176 | * - @ref TRACKDB_MAY_CREATE |
d25c4615 RK |
177 | */ |
178 | void trackdb_init(int flags) { | |
460b9539 | 179 | int err; |
d25c4615 | 180 | const int recover = flags & TRACKDB_RECOVER_MASK; |
460b9539 | 181 | static int recover_type[] = { 0, DB_RECOVER, DB_RECOVER_FATAL }; |
182 | ||
183 | /* sanity checks */ | |
184 | assert(initialized == 0); | |
185 | ++initialized; | |
186 | if(home) { | |
187 | if(strcmp(home, config->home)) | |
188 | fatal(0, "cannot change db home without server restart"); | |
189 | home = config->home; | |
190 | } | |
191 | ||
a745dd43 RK |
192 | if(flags & TRACKDB_MAY_CREATE) { |
193 | DIR *dp; | |
194 | struct dirent *de; | |
195 | struct stat st; | |
196 | char *p; | |
197 | ||
a745dd43 RK |
198 | /* Remove world/group permissions on any regular files already in the |
199 | * database directory. Actually we don't care about all of them but it's | |
200 | * easier to just do the lot. This can be revisited if it's a serious | |
201 | * practical inconvenience for anyone. | |
202 | * | |
203 | * The socket, not being a regular file, is excepted. | |
204 | */ | |
205 | if(!(dp = opendir(config->home))) | |
206 | fatal(errno, "error reading %s", config->home); | |
207 | while((de = readdir(dp))) { | |
208 | byte_xasprintf(&p, "%s/%s", config->home, de->d_name); | |
209 | if(lstat(p, &st) == 0 | |
210 | && S_ISREG(st.st_mode) | |
211 | && (st.st_mode & 077)) { | |
f0feb22e | 212 | if(chmod(p, st.st_mode & 07700) < 0) |
a745dd43 RK |
213 | fatal(errno, "cannot chmod %s", p); |
214 | } | |
215 | xfree(p); | |
216 | } | |
217 | closedir(dp); | |
218 | } | |
219 | ||
460b9539 | 220 | /* create environment */ |
221 | if((err = db_env_create(&trackdb_env, 0))) fatal(0, "db_env_create: %s", | |
222 | db_strerror(err)); | |
223 | if((err = trackdb_env->set_alloc(trackdb_env, | |
224 | xmalloc_noptr, xrealloc_noptr, xfree))) | |
225 | fatal(0, "trackdb_env->set_alloc: %s", db_strerror(err)); | |
226 | if((err = trackdb_env->set_lk_max_locks(trackdb_env, 10000))) | |
227 | fatal(0, "trackdb_env->set_lk_max_locks: %s", db_strerror(err)); | |
228 | if((err = trackdb_env->set_lk_max_objects(trackdb_env, 10000))) | |
229 | fatal(0, "trackdb_env->set_lk_max_objects: %s", db_strerror(err)); | |
230 | if((err = trackdb_env->open(trackdb_env, config->home, | |
231 | DB_INIT_LOG | |
232 | |DB_INIT_LOCK | |
233 | |DB_INIT_MPOOL | |
234 | |DB_INIT_TXN | |
235 | |DB_CREATE | |
236 | |recover_type[recover], | |
a745dd43 | 237 | 0600))) |
dc450d30 | 238 | fatal(0, "trackdb_env->open %s: %s", config->home, db_strerror(err)); |
460b9539 | 239 | trackdb_env->set_errpfx(trackdb_env, "DB"); |
240 | trackdb_env->set_errfile(trackdb_env, stderr); | |
241 | trackdb_env->set_verbose(trackdb_env, DB_VERB_DEADLOCK, 1); | |
242 | trackdb_env->set_verbose(trackdb_env, DB_VERB_RECOVERY, 1); | |
243 | trackdb_env->set_verbose(trackdb_env, DB_VERB_REPLICATION, 1); | |
244 | D(("initialized database environment")); | |
245 | } | |
246 | ||
247 | /* called when deadlock manager terminates */ | |
248 | static int reap_db_deadlock(ev_source attribute((unused)) *ev, | |
249 | pid_t attribute((unused)) pid, | |
250 | int status, | |
251 | const struct rusage attribute((unused)) *rusage, | |
252 | void attribute((unused)) *u) { | |
253 | db_deadlock_pid = -1; | |
254 | if(initialized) | |
255 | fatal(0, "deadlock manager unexpectedly terminated: %s", | |
256 | wstat(status)); | |
257 | else | |
258 | D(("deadlock manager terminated: %s", wstat(status))); | |
259 | return 0; | |
260 | } | |
261 | ||
d6dde5a3 RK |
262 | static pid_t subprogram(ev_source *ev, const char *prog, |
263 | int outputfd) { | |
460b9539 | 264 | pid_t pid; |
460b9539 | 265 | |
266 | /* If we're in the background then trap subprocess stdout/stderr */ | |
460b9539 | 267 | if(!(pid = xfork())) { |
268 | exitfn = _exit; | |
f2218e3c RK |
269 | if(ev) |
270 | ev_signal_atfork(ev); | |
460b9539 | 271 | signal(SIGPIPE, SIG_DFL); |
d6dde5a3 RK |
272 | if(outputfd != -1) { |
273 | xdup2(outputfd, 1); | |
274 | xclose(outputfd); | |
460b9539 | 275 | } |
f0feb22e RK |
276 | /* ensure we don't leak privilege anywhere */ |
277 | if(setuid(geteuid()) < 0) | |
278 | fatal(errno, "error calling setuid"); | |
279 | /* If we were negatively niced, undo it. We don't bother checking for | |
280 | * error, it's not that important. */ | |
460b9539 | 281 | setpriority(PRIO_PROCESS, 0, 0); |
282 | execlp(prog, prog, "--config", configfile, | |
283 | debugging ? "--debug" : "--no-debug", | |
0ca6d097 | 284 | log_default == &log_syslog ? "--syslog" : "--no-syslog", |
460b9539 | 285 | (char *)0); |
286 | fatal(errno, "error invoking %s", prog); | |
287 | } | |
460b9539 | 288 | return pid; |
289 | } | |
290 | ||
291 | /* start deadlock manager */ | |
292 | void trackdb_master(ev_source *ev) { | |
293 | assert(db_deadlock_pid == -1); | |
d6dde5a3 | 294 | db_deadlock_pid = subprogram(ev, DEADLOCK, -1); |
460b9539 | 295 | ev_child(ev, db_deadlock_pid, 0, reap_db_deadlock, 0); |
296 | D(("started deadlock manager")); | |
297 | } | |
298 | ||
299 | /* close environment */ | |
300 | void trackdb_deinit(void) { | |
301 | int err; | |
302 | ||
303 | /* sanity checks */ | |
304 | assert(initialized == 1); | |
305 | --initialized; | |
306 | ||
307 | /* close the environment */ | |
308 | if((err = trackdb_env->close(trackdb_env, 0))) | |
309 | fatal(0, "trackdb_env->close: %s", db_strerror(err)); | |
310 | ||
311 | if(rescan_pid != -1 && kill(rescan_pid, SIGTERM) < 0) | |
312 | fatal(errno, "error killing rescanner"); | |
313 | ||
314 | /* terminate the deadlock manager */ | |
315 | if(db_deadlock_pid != -1 && kill(db_deadlock_pid, SIGTERM) < 0) | |
316 | fatal(errno, "error killing deadlock manager"); | |
317 | db_deadlock_pid = -1; | |
318 | ||
319 | D(("deinitialized database environment")); | |
320 | } | |
321 | ||
322 | /* open a specific database */ | |
323 | static DB *open_db(const char *path, | |
324 | u_int32_t dbflags, | |
325 | DBTYPE dbtype, | |
326 | u_int32_t openflags, | |
327 | int mode) { | |
328 | int err; | |
329 | DB *db; | |
330 | ||
331 | D(("open %s", path)); | |
332 | path = config_get_file(path); | |
333 | if((err = db_create(&db, trackdb_env, 0))) | |
334 | fatal(0, "db_create %s: %s", path, db_strerror(err)); | |
335 | if(dbflags) | |
336 | if((err = db->set_flags(db, dbflags))) | |
337 | fatal(0, "db->set_flags %s: %s", path, db_strerror(err)); | |
338 | if(dbtype == DB_BTREE) | |
339 | if((err = db->set_bt_compare(db, compare))) | |
340 | fatal(0, "db->set_bt_compare %s: %s", path, db_strerror(err)); | |
341 | if((err = db->open(db, 0, path, 0, dbtype, | |
f9635e06 RK |
342 | openflags | DB_AUTO_COMMIT, mode))) { |
343 | if((openflags & DB_CREATE) || errno != ENOENT) | |
344 | fatal(0, "db->open %s: %s", path, db_strerror(err)); | |
345 | db->close(db, 0); | |
346 | db = 0; | |
347 | } | |
460b9539 | 348 | return db; |
349 | } | |
350 | ||
3dc3d7db | 351 | /** @brief Open track databases |
158d0961 | 352 | * @param flags Flags flags word |
d25c4615 | 353 | * |
f0feb22e | 354 | * @p flags should have one of: |
d25c4615 RK |
355 | * - @p TRACKDB_NO_UPGRADE, if no upgrade should be attempted |
356 | * - @p TRACKDB_CAN_UPGRADE, if an upgrade may be attempted | |
357 | * - @p TRACKDB_OPEN_FOR_UPGRADE, if this is disorder-dbupgrade | |
f0feb22e RK |
358 | * Also it may have: |
359 | * - @p TRACKDB_READ_ONLY, read only access | |
3dc3d7db | 360 | */ |
d25c4615 | 361 | void trackdb_open(int flags) { |
f982b120 | 362 | int err; |
f2218e3c | 363 | pid_t pid; |
f0feb22e | 364 | uint32_t dbflags = flags & TRACKDB_READ_ONLY ? DB_RDONLY : DB_CREATE; |
f9635e06 | 365 | |
460b9539 | 366 | /* sanity checks */ |
367 | assert(opened == 0); | |
368 | ++opened; | |
f9635e06 | 369 | /* check the database version first */ |
f0feb22e | 370 | trackdb_globaldb = open_db("global.db", 0, DB_HASH, DB_RDONLY, 0666); |
f9635e06 RK |
371 | if(trackdb_globaldb) { |
372 | /* This is an existing database */ | |
8818b7fc RK |
373 | const char *s; |
374 | long oldversion; | |
f9635e06 | 375 | |
8818b7fc | 376 | s = trackdb_get_global("_dbversion"); |
d25c4615 RK |
377 | /* Close the database again, we'll open it property below */ |
378 | if((err = trackdb_globaldb->close(trackdb_globaldb, 0))) | |
379 | fatal(0, "error closing global.db: %s", db_strerror(err)); | |
380 | trackdb_globaldb = 0; | |
381 | /* Convert version string to an integer */ | |
8818b7fc | 382 | oldversion = s ? atol(s) : 1; |
3dc3d7db | 383 | if(oldversion > config->dbversion) { |
d25c4615 | 384 | /* Database is from the future; we never allow this. */ |
3dc3d7db RK |
385 | fatal(0, "this version of DisOrder is too old for database version %ld", |
386 | oldversion); | |
387 | } | |
d25c4615 RK |
388 | if(oldversion < config->dbversion) { |
389 | /* Database version is out of date */ | |
390 | switch(flags & TRACKDB_UPGRADE_MASK) { | |
391 | case TRACKDB_NO_UPGRADE: | |
392 | /* This database needs upgrading but this is not permitted */ | |
393 | fatal(0, "database needs upgrading from %ld to %ld", | |
394 | oldversion, config->dbversion); | |
395 | case TRACKDB_CAN_UPGRADE: | |
396 | /* This database needs upgrading */ | |
f2218e3c RK |
397 | info("invoking disorder-dbupgrade to upgrade from %ld to %ld", |
398 | oldversion, config->dbversion); | |
399 | pid = subprogram(0, "disorder-dbupgrade", -1); | |
400 | while(waitpid(pid, &err, 0) == -1 && errno == EINTR) | |
401 | ; | |
402 | if(err) | |
403 | fatal(0, "disorder-dbupgrade %s", wstat(err)); | |
404 | info("disorder-dbupgrade succeeded"); | |
405 | break; | |
d25c4615 RK |
406 | case TRACKDB_OPEN_FOR_UPGRADE: |
407 | break; | |
408 | default: | |
409 | abort(); | |
410 | } | |
f9635e06 | 411 | } |
d25c4615 | 412 | if(oldversion == config->dbversion && (flags & TRACKDB_OPEN_FOR_UPGRADE)) { |
3dc3d7db RK |
413 | /* This doesn't make any sense */ |
414 | fatal(0, "database is already at current version"); | |
415 | } | |
f982b120 | 416 | trackdb_existing_database = 1; |
f9635e06 | 417 | } else { |
d25c4615 | 418 | if(flags & TRACKDB_OPEN_FOR_UPGRADE) { |
3dc3d7db RK |
419 | /* Cannot upgrade a new database */ |
420 | fatal(0, "cannot upgrade a database that does not exist"); | |
421 | } | |
f9635e06 | 422 | /* This is a brand new database */ |
f982b120 | 423 | trackdb_existing_database = 0; |
f9635e06 | 424 | } |
460b9539 | 425 | /* open the databases */ |
426 | trackdb_tracksdb = open_db("tracks.db", | |
f0feb22e | 427 | DB_RECNUM, DB_BTREE, dbflags, 0666); |
460b9539 | 428 | trackdb_searchdb = open_db("search.db", |
f0feb22e | 429 | DB_DUP|DB_DUPSORT, DB_HASH, dbflags, 0666); |
460b9539 | 430 | trackdb_tagsdb = open_db("tags.db", |
f0feb22e RK |
431 | DB_DUP|DB_DUPSORT, DB_HASH, dbflags, 0666); |
432 | trackdb_prefsdb = open_db("prefs.db", 0, DB_HASH, dbflags, 0666); | |
433 | trackdb_globaldb = open_db("global.db", 0, DB_HASH, dbflags, 0666); | |
2a10b70b | 434 | trackdb_noticeddb = open_db("noticed.db", |
f0feb22e | 435 | DB_DUPSORT, DB_BTREE, dbflags, 0666); |
a745dd43 | 436 | trackdb_usersdb = open_db("users.db", |
f0feb22e | 437 | 0, DB_HASH, dbflags, 0600); |
f982b120 | 438 | if(!trackdb_existing_database) { |
3dc3d7db | 439 | /* Stash the database version */ |
8818b7fc RK |
440 | char buf[32]; |
441 | ||
d25c4615 | 442 | assert(!(flags & TRACKDB_OPEN_FOR_UPGRADE)); |
8818b7fc RK |
443 | snprintf(buf, sizeof buf, "%ld", config->dbversion); |
444 | trackdb_set_global("_dbversion", buf, 0); | |
445 | } | |
460b9539 | 446 | D(("opened databases")); |
447 | } | |
448 | ||
449 | /* close track databases */ | |
450 | void trackdb_close(void) { | |
451 | int err; | |
f0feb22e | 452 | |
460b9539 | 453 | /* sanity checks */ |
454 | assert(opened == 1); | |
455 | --opened; | |
456 | if((err = trackdb_tracksdb->close(trackdb_tracksdb, 0))) | |
457 | fatal(0, "error closing tracks.db: %s", db_strerror(err)); | |
458 | if((err = trackdb_searchdb->close(trackdb_searchdb, 0))) | |
459 | fatal(0, "error closing search.db: %s", db_strerror(err)); | |
460 | if((err = trackdb_tagsdb->close(trackdb_tagsdb, 0))) | |
461 | fatal(0, "error closing tags.db: %s", db_strerror(err)); | |
462 | if((err = trackdb_prefsdb->close(trackdb_prefsdb, 0))) | |
463 | fatal(0, "error closing prefs.db: %s", db_strerror(err)); | |
464 | if((err = trackdb_globaldb->close(trackdb_globaldb, 0))) | |
465 | fatal(0, "error closing global.db: %s", db_strerror(err)); | |
2a10b70b RK |
466 | if((err = trackdb_noticeddb->close(trackdb_noticeddb, 0))) |
467 | fatal(0, "error closing noticed.db: %s", db_strerror(err)); | |
a745dd43 RK |
468 | if((err = trackdb_usersdb->close(trackdb_usersdb, 0))) |
469 | fatal(0, "error closing users.db: %s", db_strerror(err)); | |
460b9539 | 470 | trackdb_tracksdb = trackdb_searchdb = trackdb_prefsdb = 0; |
471 | trackdb_tagsdb = trackdb_globaldb = 0; | |
472 | D(("closed databases")); | |
473 | } | |
474 | ||
475 | /* generic db routines *******************************************************/ | |
476 | ||
477 | /* fetch and decode a database entry. Returns 0, DB_NOTFOUND or | |
478 | * DB_LOCK_DEADLOCK. */ | |
479 | int trackdb_getdata(DB *db, | |
480 | const char *track, | |
481 | struct kvp **kp, | |
482 | DB_TXN *tid) { | |
483 | int err; | |
484 | DBT key, data; | |
485 | ||
486 | switch(err = db->get(db, tid, make_key(&key, track), | |
487 | prepare_data(&data), 0)) { | |
488 | case 0: | |
489 | *kp = kvp_urldecode(data.data, data.size); | |
490 | return 0; | |
491 | case DB_NOTFOUND: | |
492 | *kp = 0; | |
493 | return err; | |
494 | case DB_LOCK_DEADLOCK: | |
495 | error(0, "error querying database: %s", db_strerror(err)); | |
496 | return err; | |
497 | default: | |
498 | fatal(0, "error querying database: %s", db_strerror(err)); | |
499 | } | |
500 | } | |
501 | ||
502 | /* encode and store a database entry. Returns 0, DB_KEYEXIST or | |
503 | * DB_LOCK_DEADLOCK. */ | |
504 | int trackdb_putdata(DB *db, | |
505 | const char *track, | |
506 | const struct kvp *k, | |
507 | DB_TXN *tid, | |
508 | u_int32_t flags) { | |
509 | int err; | |
510 | DBT key, data; | |
511 | ||
512 | switch(err = db->put(db, tid, make_key(&key, track), | |
513 | encode_data(&data, k), flags)) { | |
514 | case 0: | |
515 | case DB_KEYEXIST: | |
516 | return err; | |
517 | case DB_LOCK_DEADLOCK: | |
518 | error(0, "error updating database: %s", db_strerror(err)); | |
519 | return err; | |
520 | default: | |
521 | fatal(0, "error updating database: %s", db_strerror(err)); | |
522 | } | |
523 | } | |
524 | ||
f0feb22e RK |
525 | /** @brief Delete a database entry |
526 | * @param db Database | |
527 | * @param track Key to delete | |
528 | * @param tid Transaction ID | |
529 | * @return 0, DB_NOTFOUND or DB_LOCK_DEADLOCK | |
530 | */ | |
460b9539 | 531 | int trackdb_delkey(DB *db, |
532 | const char *track, | |
533 | DB_TXN *tid) { | |
534 | int err; | |
535 | ||
536 | DBT key; | |
537 | switch(err = db->del(db, tid, make_key(&key, track), 0)) { | |
538 | case 0: | |
539 | case DB_NOTFOUND: | |
540 | return 0; | |
541 | case DB_LOCK_DEADLOCK: | |
542 | error(0, "error updating database: %s", db_strerror(err)); | |
543 | return err; | |
544 | default: | |
545 | fatal(0, "error updating database: %s", db_strerror(err)); | |
546 | } | |
547 | } | |
548 | ||
549 | /* open a database cursor */ | |
550 | DBC *trackdb_opencursor(DB *db, DB_TXN *tid) { | |
551 | int err; | |
552 | DBC *c; | |
553 | ||
554 | switch(err = db->cursor(db, tid, &c, 0)) { | |
555 | case 0: break; | |
556 | default: fatal(0, "error creating cursor: %s", db_strerror(err)); | |
557 | } | |
558 | return c; | |
559 | } | |
560 | ||
561 | /* close a database cursor; returns 0 or DB_LOCK_DEADLOCK */ | |
562 | int trackdb_closecursor(DBC *c) { | |
563 | int err; | |
564 | ||
565 | if(!c) return 0; | |
566 | switch(err = c->c_close(c)) { | |
567 | case 0: | |
568 | return err; | |
569 | case DB_LOCK_DEADLOCK: | |
570 | error(0, "error closing cursor: %s", db_strerror(err)); | |
571 | return err; | |
572 | default: | |
573 | fatal(0, "error closing cursor: %s", db_strerror(err)); | |
574 | } | |
575 | } | |
576 | ||
577 | /* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */ | |
578 | int trackdb_delkeydata(DB *db, | |
579 | const char *word, | |
580 | const char *track, | |
581 | DB_TXN *tid) { | |
582 | int err; | |
583 | DBC *c; | |
584 | DBT key, data; | |
585 | ||
586 | c = trackdb_opencursor(db, tid); | |
587 | switch(err = c->c_get(c, make_key(&key, word), | |
588 | make_key(&data, track), DB_GET_BOTH)) { | |
589 | case 0: | |
590 | switch(err = c->c_del(c, 0)) { | |
591 | case 0: | |
592 | break; | |
593 | case DB_KEYEMPTY: | |
594 | err = 0; | |
595 | break; | |
596 | case DB_LOCK_DEADLOCK: | |
597 | error(0, "error updating database: %s", db_strerror(err)); | |
598 | break; | |
599 | default: | |
600 | fatal(0, "c->c_del: %s", db_strerror(err)); | |
601 | } | |
602 | break; | |
603 | case DB_NOTFOUND: | |
604 | break; | |
605 | case DB_LOCK_DEADLOCK: | |
606 | error(0, "error updating database: %s", db_strerror(err)); | |
607 | break; | |
608 | default: | |
609 | fatal(0, "c->c_get: %s", db_strerror(err)); | |
610 | } | |
611 | if(trackdb_closecursor(c)) err = DB_LOCK_DEADLOCK; | |
612 | return err; | |
613 | } | |
614 | ||
615 | /* start a transaction */ | |
616 | DB_TXN *trackdb_begin_transaction(void) { | |
617 | DB_TXN *tid; | |
618 | int err; | |
619 | ||
620 | if((err = trackdb_env->txn_begin(trackdb_env, 0, &tid, 0))) | |
621 | fatal(0, "trackdb_env->txn_begin: %s", db_strerror(err)); | |
622 | return tid; | |
623 | } | |
624 | ||
625 | /* abort transaction */ | |
626 | void trackdb_abort_transaction(DB_TXN *tid) { | |
627 | int err; | |
628 | ||
629 | if(tid) | |
630 | if((err = tid->abort(tid))) | |
631 | fatal(0, "tid->abort: %s", db_strerror(err)); | |
632 | } | |
633 | ||
634 | /* commit transaction */ | |
635 | void trackdb_commit_transaction(DB_TXN *tid) { | |
636 | int err; | |
637 | ||
638 | if((err = tid->commit(tid, 0))) | |
639 | fatal(0, "tid->commit: %s", db_strerror(err)); | |
640 | } | |
641 | ||
642 | /* search/tags shared code ***************************************************/ | |
643 | ||
644 | /* comparison function used by dedupe() */ | |
645 | static int wordcmp(const void *a, const void *b) { | |
646 | return strcmp(*(const char **)a, *(const char **)b); | |
647 | } | |
648 | ||
649 | /* sort and de-dupe VEC */ | |
650 | static char **dedupe(char **vec, int nvec) { | |
651 | int m, n; | |
652 | ||
653 | qsort(vec, nvec, sizeof (char *), wordcmp); | |
654 | m = n = 0; | |
655 | if(nvec) { | |
656 | vec[m++] = vec[0]; | |
657 | for(n = 1; n < nvec; ++n) | |
658 | if(strcmp(vec[n], vec[m - 1])) | |
659 | vec[m++] = vec[n]; | |
660 | } | |
661 | vec[m] = 0; | |
662 | return vec; | |
663 | } | |
664 | ||
665 | /* update a key/track database. Returns 0 or DB_DEADLOCK. */ | |
666 | static int register_word(DB *db, const char *what, | |
667 | const char *track, const char *word, | |
668 | DB_TXN *tid) { | |
669 | int err; | |
670 | DBT key, data; | |
671 | ||
672 | switch(err = db->put(db, tid, make_key(&key, word), | |
673 | make_key(&data, track), DB_NODUPDATA)) { | |
674 | case 0: | |
675 | case DB_KEYEXIST: | |
676 | return 0; | |
677 | case DB_LOCK_DEADLOCK: | |
678 | error(0, "error updating %s.db: %s", what, db_strerror(err)); | |
679 | return err; | |
680 | default: | |
681 | fatal(0, "error updating %s.db: %s", what, db_strerror(err)); | |
682 | } | |
683 | } | |
684 | ||
685 | /* search primitives *********************************************************/ | |
686 | ||
687 | /* return true iff NAME is a trackname_display_ pref */ | |
688 | static int is_display_pref(const char *name) { | |
689 | static const char prefix[] = "trackname_display_"; | |
690 | return !strncmp(name, prefix, (sizeof prefix) - 1); | |
691 | } | |
692 | ||
c85b7022 RK |
693 | /** @brief Word_Break property tailor that treats underscores as spaces */ |
694 | static int tailor_underscore_Word_Break_Other(uint32_t c) { | |
695 | switch(c) { | |
696 | default: | |
697 | return -1; | |
698 | case 0x005F: /* LOW LINE (SPACING UNDERSCORE) */ | |
699 | return unicode_Word_Break_Other; | |
700 | } | |
701 | } | |
702 | ||
3c82b504 RK |
703 | /** @brief Remove all combining characters in-place |
704 | * @param s Pointer to start of string | |
705 | * @param ns Length of string | |
706 | * @return New, possiblby reduced, length | |
707 | */ | |
708 | static size_t remove_combining_chars(uint32_t *s, size_t ns) { | |
709 | uint32_t *start = s, *t = s, *end = s + ns; | |
710 | ||
711 | while(s < end) { | |
712 | const uint32_t c = *s++; | |
713 | if(!utf32_combining_class(c)) | |
714 | *t++ = c; | |
715 | } | |
716 | return t - start; | |
717 | } | |
718 | ||
c85b7022 RK |
719 | /** @brief Normalize and split a string using a given tailoring */ |
720 | static void word_split(struct vector *v, | |
721 | const char *s, | |
722 | unicode_property_tailor *pt) { | |
723 | size_t nw, nt32, i; | |
724 | uint32_t *t32, **w32; | |
725 | ||
726 | /* Convert to UTF-32 */ | |
727 | if(!(t32 = utf8_to_utf32(s, strlen(s), &nt32))) | |
728 | return; | |
729 | /* Erase case distinctions */ | |
730 | if(!(t32 = utf32_casefold_compat(t32, nt32, &nt32))) | |
731 | return; | |
3c82b504 RK |
732 | /* Drop combining characters */ |
733 | nt32 = remove_combining_chars(t32, nt32); | |
c85b7022 RK |
734 | /* Split into words, treating _ as a space */ |
735 | w32 = utf32_word_split(t32, nt32, &nw, pt); | |
736 | /* Convert words back to UTF-8 and append to result */ | |
737 | for(i = 0; i < nw; ++i) | |
738 | vector_append(v, utf32_to_utf8(w32[i], utf32_len(w32[i]), 0)); | |
739 | } | |
740 | ||
a3777123 RK |
741 | /** @brief Normalize a tag |
742 | * @param s Tag | |
743 | * @param ns Length of tag | |
744 | * @return Normalized string or NULL on error | |
745 | * | |
746 | * The return value will be: | |
747 | * - case-folded | |
748 | * - have no leading or trailing space | |
749 | * - have no combining characters | |
750 | * - all spacing between words will be a single U+0020 SPACE | |
751 | */ | |
752 | static char *normalize_tag(const char *s, size_t ns) { | |
753 | uint32_t *s32, **w32; | |
754 | size_t ns32, nw32, i; | |
755 | struct dynstr d[1]; | |
756 | ||
757 | if(!(s32 = utf8_to_utf32(s, ns, &ns32))) | |
758 | return 0; | |
759 | if(!(s32 = utf32_casefold_compat(s32, ns32, &ns32))) /* ->NFKD */ | |
760 | return 0; | |
761 | ns32 = remove_combining_chars(s32, ns32); | |
762 | /* Split into words, no Word_Break tailoring */ | |
763 | w32 = utf32_word_split(s32, ns32, &nw32, 0); | |
764 | /* Compose back into a string */ | |
765 | dynstr_init(d); | |
766 | for(i = 0; i < nw32; ++i) { | |
767 | if(i) | |
768 | dynstr_append(d, ' '); | |
769 | dynstr_append_string(d, utf32_to_utf8(w32[i], utf32_len(w32[i]), 0)); | |
770 | } | |
771 | dynstr_terminate(d); | |
772 | return d->vec; | |
773 | } | |
774 | ||
460b9539 | 775 | /* compute the words of a track name */ |
776 | static char **track_to_words(const char *track, | |
777 | const struct kvp *p) { | |
778 | struct vector v; | |
a8747834 | 779 | const char *rootless = track_rootless(track); |
460b9539 | 780 | |
a8747834 | 781 | if(!rootless) |
782 | rootless = track; /* bodge */ | |
460b9539 | 783 | vector_init(&v); |
c85b7022 RK |
784 | rootless = strip_extension(rootless); |
785 | word_split(&v, strip_extension(rootless), tailor_underscore_Word_Break_Other); | |
460b9539 | 786 | for(; p; p = p->next) |
787 | if(is_display_pref(p->name)) | |
c85b7022 | 788 | word_split(&v, p->value, 0); |
460b9539 | 789 | vector_terminate(&v); |
790 | return dedupe(v.vec, v.nvec); | |
791 | } | |
792 | ||
793 | /* return nonzero iff WORD is a stopword */ | |
794 | static int stopword(const char *word) { | |
795 | int n; | |
796 | ||
797 | for(n = 0; n < config->stopword.n | |
798 | && strcmp(word, config->stopword.s[n]); ++n) | |
799 | ; | |
800 | return n < config->stopword.n; | |
801 | } | |
802 | ||
803 | /* record that WORD appears in TRACK. Returns 0 or DB_LOCK_DEADLOCK. */ | |
804 | static int register_search_word(const char *track, const char *word, | |
805 | DB_TXN *tid) { | |
806 | if(stopword(word)) return 0; | |
807 | return register_word(trackdb_searchdb, "search", track, word, tid); | |
808 | } | |
809 | ||
810 | /* Tags **********************************************************************/ | |
811 | ||
812 | /* Return nonzero if C is a valid tag character */ | |
813 | static int tagchar(int c) { | |
814 | switch(c) { | |
815 | case ',': | |
816 | return 0; | |
817 | default: | |
818 | return c >= ' '; | |
819 | } | |
820 | } | |
821 | ||
822 | /* Parse and de-dupe a tag list. If S=0 then assumes "". */ | |
823 | static char **parsetags(const char *s) { | |
824 | const char *t; | |
825 | struct vector v; | |
826 | ||
827 | vector_init(&v); | |
828 | if(s) { | |
829 | /* skip initial separators */ | |
830 | while(*s && (!tagchar(*s) || *s == ' ')) | |
831 | ++s; | |
832 | while(*s) { | |
833 | /* find the extent of the tag */ | |
834 | t = s; | |
835 | while(*s && tagchar(*s)) | |
836 | ++s; | |
837 | /* strip trailing spaces */ | |
838 | while(s > t && s[-1] == ' ') | |
839 | --s; | |
a3777123 RK |
840 | /* add tag to list */ |
841 | vector_append(&v, normalize_tag(t, (size_t)(s - t))); | |
460b9539 | 842 | /* skip intermediate and trailing separators */ |
843 | while(*s && (!tagchar(*s) || *s == ' ')) | |
844 | ++s; | |
845 | } | |
846 | } | |
847 | vector_terminate(&v); | |
848 | return dedupe(v.vec, v.nvec); | |
849 | } | |
850 | ||
851 | /* Record that TRACK has TAG. Returns 0 or DB_LOCK_DEADLOCK. */ | |
852 | static int register_tag(const char *track, const char *tag, DB_TXN *tid) { | |
853 | return register_word(trackdb_tagsdb, "tags", track, tag, tid); | |
854 | } | |
855 | ||
856 | /* aliases *******************************************************************/ | |
857 | ||
858 | /* compute the alias and store at aliasp. Returns 0 or DB_LOCK_DEADLOCK. If | |
859 | * there is no alias sets *aliasp to 0. */ | |
860 | static int compute_alias(char **aliasp, | |
861 | const char *track, | |
862 | const struct kvp *p, | |
863 | DB_TXN *tid) { | |
864 | struct dynstr d; | |
865 | const char *s = config->alias, *t, *expansion, *part; | |
866 | int c, used_db = 0, slash_prefix, err; | |
867 | struct kvp *at; | |
137e36ed | 868 | const char *const root = find_track_root(track); |
460b9539 | 869 | |
137e36ed | 870 | if(!root) { |
871 | /* Bodge for tracks with no root */ | |
872 | *aliasp = 0; | |
873 | return 0; | |
874 | } | |
460b9539 | 875 | dynstr_init(&d); |
137e36ed | 876 | dynstr_append_string(&d, root); |
460b9539 | 877 | while((c = (unsigned char)*s++)) { |
878 | if(c != '{') { | |
879 | dynstr_append(&d, c); | |
880 | continue; | |
881 | } | |
882 | if((slash_prefix = (*s == '/'))) | |
883 | s++; | |
884 | t = strchr(s, '}'); | |
885 | assert(t != 0); /* validated at startup */ | |
886 | part = xstrndup(s, t - s); | |
887 | expansion = getpart(track, "display", part, p, &used_db); | |
888 | if(*expansion) { | |
889 | if(slash_prefix) dynstr_append(&d, '/'); | |
890 | dynstr_append_string(&d, expansion); | |
891 | } | |
892 | s = t + 1; /* skip {part} */ | |
893 | } | |
894 | /* only admit to the alias if we used the db... */ | |
895 | if(!used_db) { | |
896 | *aliasp = 0; | |
897 | return 0; | |
898 | } | |
899 | dynstr_terminate(&d); | |
900 | /* ...and the answer differs from the original... */ | |
901 | if(!strcmp(track, d.vec)) { | |
902 | *aliasp = 0; | |
903 | return 0; | |
904 | } | |
905 | /* ...and there isn't already a different track with that name (including as | |
906 | * an alias) */ | |
907 | switch(err = trackdb_getdata(trackdb_tracksdb, d.vec, &at, tid)) { | |
908 | case 0: | |
460b9539 | 909 | if((s = kvp_get(at, "_alias_for")) |
910 | && !strcmp(s, track)) { | |
911 | case DB_NOTFOUND: | |
460b9539 | 912 | *aliasp = d.vec; |
913 | } else { | |
460b9539 | 914 | *aliasp = 0; |
915 | } | |
916 | return 0; | |
917 | default: | |
918 | return err; | |
919 | } | |
920 | } | |
921 | ||
922 | /* get track and prefs data (if tp/pp not null pointers). Returns 0 on | |
923 | * success, DB_NOTFOUND if the track does not exist or DB_LOCK_DEADLOCK. | |
924 | * Always sets the return values, even if only to null pointers. */ | |
925 | static int gettrackdata(const char *track, | |
926 | struct kvp **tp, | |
927 | struct kvp **pp, | |
928 | const char **actualp, | |
929 | unsigned flags, | |
930 | #define GTD_NOALIAS 0x0001 | |
931 | DB_TXN *tid) { | |
932 | int err; | |
933 | const char *actual = track; | |
934 | struct kvp *t = 0, *p = 0; | |
f0feb22e | 935 | |
460b9539 | 936 | if((err = trackdb_getdata(trackdb_tracksdb, track, &t, tid))) goto done; |
937 | if((actual = kvp_get(t, "_alias_for"))) { | |
938 | if(flags & GTD_NOALIAS) { | |
939 | error(0, "alias passed to gettrackdata where real path required"); | |
940 | abort(); | |
941 | } | |
942 | if((err = trackdb_getdata(trackdb_tracksdb, actual, &t, tid))) goto done; | |
943 | } else | |
944 | actual = track; | |
945 | assert(actual != 0); | |
946 | if(pp) { | |
947 | if((err = trackdb_getdata(trackdb_prefsdb, actual, &p, tid)) == DB_LOCK_DEADLOCK) | |
948 | goto done; | |
949 | } | |
950 | err = 0; | |
951 | done: | |
952 | if(actualp) *actualp = actual; | |
953 | if(tp) *tp = t; | |
954 | if(pp) *pp = p; | |
955 | return err; | |
956 | } | |
957 | ||
958 | /* trackdb_notice() **********************************************************/ | |
959 | ||
6c81eef9 | 960 | /** @brief notice a possibly new track |
2a10b70b RK |
961 | * @return @c DB_NOTFOUND if new, 0 if already known |
962 | */ | |
460b9539 | 963 | int trackdb_notice(const char *track, |
964 | const char *path) { | |
965 | int err; | |
966 | DB_TXN *tid; | |
f0feb22e | 967 | |
460b9539 | 968 | for(;;) { |
969 | tid = trackdb_begin_transaction(); | |
970 | err = trackdb_notice_tid(track, path, tid); | |
971 | if((err == DB_LOCK_DEADLOCK)) goto fail; | |
972 | break; | |
973 | fail: | |
974 | trackdb_abort_transaction(tid); | |
975 | } | |
976 | trackdb_commit_transaction(tid); | |
977 | return err; | |
978 | } | |
979 | ||
6c81eef9 | 980 | /** @brief notice a possibly new track |
f9635e06 RK |
981 | * @param track NFC UTF-8 track name |
982 | * @param path Raw path name | |
983 | * @param tid Transaction ID | |
2a10b70b RK |
984 | * @return @c DB_NOTFOUND if new, 0 if already known, @c DB_LOCK_DEADLOCK also |
985 | */ | |
460b9539 | 986 | int trackdb_notice_tid(const char *track, |
987 | const char *path, | |
988 | DB_TXN *tid) { | |
989 | int err, n; | |
990 | struct kvp *t, *a, *p; | |
991 | int t_changed, ret; | |
992 | char *alias, **w; | |
2a10b70b | 993 | |
460b9539 | 994 | /* notice whether the tracks.db entry changes */ |
995 | t_changed = 0; | |
996 | /* get any existing tracks entry */ | |
997 | if((err = gettrackdata(track, &t, &p, 0, 0, tid)) == DB_LOCK_DEADLOCK) | |
998 | return err; | |
2a10b70b | 999 | ret = err; /* 0 or DB_NOTFOUND */ |
460b9539 | 1000 | /* this is a real track */ |
1001 | t_changed += kvp_set(&t, "_alias_for", 0); | |
1002 | t_changed += kvp_set(&t, "_path", path); | |
1003 | /* if we have an alias record it in the database */ | |
1004 | if((err = compute_alias(&alias, track, p, tid))) return err; | |
1005 | if(alias) { | |
1006 | /* won't overwrite someone else's alias as compute_alias() checks */ | |
1007 | D(("%s: alias %s", track, alias)); | |
1008 | a = 0; | |
1009 | kvp_set(&a, "_alias_for", track); | |
1010 | if((err = trackdb_putdata(trackdb_tracksdb, alias, a, tid, 0))) return err; | |
1011 | } | |
1012 | /* update search.db */ | |
1013 | w = track_to_words(track, p); | |
1014 | for(n = 0; w[n]; ++n) | |
1015 | if((err = register_search_word(track, w[n], tid))) | |
1016 | return err; | |
1017 | /* update tags.db */ | |
1018 | w = parsetags(kvp_get(p, "tags")); | |
1019 | for(n = 0; w[n]; ++n) | |
1020 | if((err = register_tag(track, w[n], tid))) | |
1021 | return err; | |
1022 | reqtracks = 0; | |
1023 | /* only store the tracks.db entry if it has changed */ | |
1024 | if(t_changed && (err = trackdb_putdata(trackdb_tracksdb, track, t, tid, 0))) | |
1025 | return err; | |
2a10b70b RK |
1026 | if(ret == DB_NOTFOUND) { |
1027 | uint32_t timestamp[2]; | |
1028 | time_t now; | |
1029 | DBT key, data; | |
1030 | ||
1031 | time(&now); | |
1032 | timestamp[0] = htonl((uint64_t)now >> 32); | |
1033 | timestamp[1] = htonl((uint32_t)now); | |
1034 | memset(&key, 0, sizeof key); | |
1035 | key.data = timestamp; | |
1036 | key.size = sizeof timestamp; | |
1037 | switch(err = trackdb_noticeddb->put(trackdb_noticeddb, tid, &key, | |
1038 | make_key(&data, track), 0)) { | |
1039 | case 0: break; | |
1040 | case DB_LOCK_DEADLOCK: return err; | |
1041 | default: fatal(0, "error updating noticed.db: %s", db_strerror(err)); | |
1042 | } | |
1043 | } | |
460b9539 | 1044 | return ret; |
1045 | } | |
1046 | ||
1047 | /* trackdb_obsolete() ********************************************************/ | |
1048 | ||
1049 | /* obsolete a track */ | |
1050 | int trackdb_obsolete(const char *track, DB_TXN *tid) { | |
1051 | int err, n; | |
1052 | struct kvp *p; | |
1053 | char *alias, **w; | |
1054 | ||
1055 | if((err = gettrackdata(track, 0, &p, 0, | |
1056 | GTD_NOALIAS, tid)) == DB_LOCK_DEADLOCK) | |
1057 | return err; | |
1058 | else if(err == DB_NOTFOUND) return 0; | |
1059 | /* compute the alias, if any, and delete it */ | |
f0feb22e | 1060 | if((err = compute_alias(&alias, track, p, tid))) return err; |
460b9539 | 1061 | if(alias) { |
1062 | /* if the alias points to some other track then compute_alias won't | |
1063 | * return it */ | |
f0feb22e RK |
1064 | if((err = trackdb_delkey(trackdb_tracksdb, alias, tid)) |
1065 | && err != DB_NOTFOUND) | |
460b9539 | 1066 | return err; |
1067 | } | |
1068 | /* update search.db */ | |
1069 | w = track_to_words(track, p); | |
1070 | for(n = 0; w[n]; ++n) | |
1071 | if(trackdb_delkeydata(trackdb_searchdb, | |
1072 | w[n], track, tid) == DB_LOCK_DEADLOCK) | |
1073 | return err; | |
1074 | /* update tags.db */ | |
1075 | w = parsetags(kvp_get(p, "tags")); | |
1076 | for(n = 0; w[n]; ++n) | |
1077 | if(trackdb_delkeydata(trackdb_tagsdb, | |
1078 | w[n], track, tid) == DB_LOCK_DEADLOCK) | |
1079 | return err; | |
1080 | reqtracks = 0; | |
1081 | /* update tracks.db */ | |
1082 | if(trackdb_delkey(trackdb_tracksdb, track, tid) == DB_LOCK_DEADLOCK) | |
1083 | return err; | |
1084 | /* We don't delete the prefs, so they survive temporary outages of the | |
1085 | * (possibly virtual) track filesystem */ | |
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | /* trackdb_stats() ***********************************************************/ | |
1090 | ||
1091 | #define H(name) { #name, offsetof(DB_HASH_STAT, name) } | |
1092 | #define B(name) { #name, offsetof(DB_BTREE_STAT, name) } | |
1093 | ||
1094 | static const struct statinfo { | |
1095 | const char *name; | |
1096 | size_t offset; | |
1097 | } statinfo_hash[] = { | |
1098 | H(hash_magic), | |
1099 | H(hash_version), | |
1100 | H(hash_nkeys), | |
1101 | H(hash_ndata), | |
1102 | H(hash_pagesize), | |
1103 | H(hash_ffactor), | |
1104 | H(hash_buckets), | |
1105 | H(hash_free), | |
1106 | H(hash_bfree), | |
1107 | H(hash_bigpages), | |
1108 | H(hash_big_bfree), | |
1109 | H(hash_overflows), | |
1110 | H(hash_ovfl_free), | |
1111 | H(hash_dup), | |
1112 | H(hash_dup_free), | |
1113 | }, statinfo_btree[] = { | |
1114 | B(bt_magic), | |
1115 | B(bt_version), | |
1116 | B(bt_nkeys), | |
1117 | B(bt_ndata), | |
1118 | B(bt_pagesize), | |
1119 | B(bt_minkey), | |
1120 | B(bt_re_len), | |
1121 | B(bt_re_pad), | |
1122 | B(bt_levels), | |
1123 | B(bt_int_pg), | |
1124 | B(bt_leaf_pg), | |
1125 | B(bt_dup_pg), | |
1126 | B(bt_over_pg), | |
1127 | B(bt_free), | |
1128 | B(bt_int_pgfree), | |
1129 | B(bt_leaf_pgfree), | |
1130 | B(bt_dup_pgfree), | |
1131 | B(bt_over_pgfree), | |
1132 | }; | |
1133 | ||
1134 | /* look up stats for DB */ | |
1135 | static int get_stats(struct vector *v, | |
1136 | DB *database, | |
1137 | const struct statinfo *si, | |
1138 | size_t nsi, | |
1139 | DB_TXN *tid) { | |
1140 | void *sp; | |
1141 | size_t n; | |
1142 | char *str; | |
1143 | int err; | |
1144 | ||
1145 | if(database) { | |
1146 | switch(err = database->stat(database, tid, &sp, 0)) { | |
1147 | case 0: | |
1148 | break; | |
1149 | case DB_LOCK_DEADLOCK: | |
1150 | error(0, "error querying database: %s", db_strerror(err)); | |
1151 | return err; | |
1152 | default: | |
1153 | fatal(0, "error querying database: %s", db_strerror(err)); | |
1154 | } | |
1155 | for(n = 0; n < nsi; ++n) { | |
1156 | byte_xasprintf(&str, "%s=%"PRIuMAX, si[n].name, | |
1157 | (uintmax_t)*(u_int32_t *)((char *)sp + si[n].offset)); | |
1158 | vector_append(v, str); | |
1159 | } | |
1160 | } | |
1161 | return 0; | |
1162 | } | |
1163 | ||
ff76b21b | 1164 | /** @brief One entry in the search league */ |
460b9539 | 1165 | struct search_entry { |
1166 | char *word; | |
1167 | int n; | |
1168 | }; | |
1169 | ||
ff76b21b RK |
1170 | /** @brief Add a word to the search league |
1171 | * @param se Pointer to search league | |
1172 | * @param count Maximum size for search league | |
1173 | * @param nse Current size of search league | |
1174 | * @param word New word, or NULL | |
1175 | * @param n How often @p word appears | |
1176 | * @return New size of search league | |
1177 | */ | |
1178 | static int register_search_entry(struct search_entry *se, | |
1179 | int count, | |
1180 | int nse, | |
1181 | char *word, | |
1182 | int n) { | |
1183 | int i; | |
1184 | ||
1185 | if(word && (nse < count || n > se[nse - 1].n)) { | |
1186 | /* Find the starting point */ | |
1187 | if(nse == count) | |
1188 | i = nse - 1; | |
1189 | else | |
1190 | i = nse++; | |
1191 | /* Find the insertion point */ | |
1192 | while(i > 0 && n > se[i - 1].n) | |
1193 | --i; | |
1194 | memmove(&se[i + 1], &se[i], (nse - i - 1) * sizeof *se); | |
1195 | se[i].word = word; | |
1196 | se[i].n = n; | |
1197 | } | |
1198 | return nse; | |
1199 | } | |
1200 | ||
460b9539 | 1201 | /* find the top COUNT words in the search database */ |
1202 | static int search_league(struct vector *v, int count, DB_TXN *tid) { | |
1203 | struct search_entry *se; | |
1204 | DBT k, d; | |
1205 | DBC *cursor; | |
1206 | int err, n = 0, nse = 0, i; | |
1207 | char *word = 0; | |
1208 | size_t wl = 0; | |
1209 | char *str; | |
1210 | ||
1211 | cursor = trackdb_opencursor(trackdb_searchdb, tid); | |
1212 | se = xmalloc(count * sizeof *se); | |
ff76b21b RK |
1213 | /* Walk across the whole database counting up the number of times each |
1214 | * word appears. */ | |
460b9539 | 1215 | while(!(err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), |
1216 | DB_NEXT))) { | |
1217 | if(word && wl == k.size && !strncmp(word, k.data, wl)) | |
ff76b21b | 1218 | ++n; /* same word again */ |
460b9539 | 1219 | else { |
ff76b21b | 1220 | nse = register_search_entry(se, count, nse, word, n); |
460b9539 | 1221 | word = xstrndup(k.data, wl = k.size); |
1222 | n = 1; | |
1223 | } | |
1224 | } | |
1225 | switch(err) { | |
1226 | case DB_NOTFOUND: | |
1227 | err = 0; | |
1228 | break; | |
1229 | case DB_LOCK_DEADLOCK: | |
1230 | error(0, "error querying search database: %s", db_strerror(err)); | |
1231 | break; | |
1232 | default: | |
1233 | fatal(0, "error querying search database: %s", db_strerror(err)); | |
1234 | } | |
1235 | if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK; | |
1236 | if(err) return err; | |
ff76b21b | 1237 | nse = register_search_entry(se, count, nse, word, n); |
460b9539 | 1238 | byte_xasprintf(&str, "Top %d search words:", nse); |
1239 | vector_append(v, str); | |
1240 | for(i = 0; i < nse; ++i) { | |
1241 | byte_xasprintf(&str, "%4d: %5d %s", i + 1, se[i].n, se[i].word); | |
1242 | vector_append(v, str); | |
1243 | } | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | #define SI(what) statinfo_##what, \ | |
1248 | sizeof statinfo_##what / sizeof (struct statinfo) | |
1249 | ||
1250 | /* return a list of database stats */ | |
1251 | char **trackdb_stats(int *nstatsp) { | |
1252 | DB_TXN *tid; | |
1253 | struct vector v; | |
f0feb22e | 1254 | |
460b9539 | 1255 | vector_init(&v); |
1256 | for(;;) { | |
1257 | tid = trackdb_begin_transaction(); | |
1258 | v.nvec = 0; | |
1259 | vector_append(&v, (char *)"Tracks database stats:"); | |
1260 | if(get_stats(&v, trackdb_tracksdb, SI(btree), tid)) goto fail; | |
1261 | vector_append(&v, (char *)""); | |
1262 | vector_append(&v, (char *)"Search database stats:"); | |
1263 | if(get_stats(&v, trackdb_searchdb, SI(hash), tid)) goto fail; | |
1264 | vector_append(&v, (char *)""); | |
1265 | vector_append(&v, (char *)"Prefs database stats:"); | |
1266 | if(get_stats(&v, trackdb_prefsdb, SI(hash), tid)) goto fail; | |
1267 | vector_append(&v, (char *)""); | |
1268 | if(search_league(&v, 10, tid)) goto fail; | |
460b9539 | 1269 | vector_terminate(&v); |
1270 | break; | |
1271 | fail: | |
1272 | trackdb_abort_transaction(tid); | |
1273 | } | |
1274 | trackdb_commit_transaction(tid); | |
1275 | if(nstatsp) *nstatsp = v.nvec; | |
1276 | return v.vec; | |
1277 | } | |
1278 | ||
d6dde5a3 RK |
1279 | struct stats_details { |
1280 | void (*done)(char *data, void *u); | |
1281 | void *u; | |
1282 | int exited; /* subprocess exited */ | |
1283 | int closed; /* pipe close */ | |
1284 | int wstat; /* wait status from subprocess */ | |
1285 | struct dynstr data[1]; /* data read from pipe */ | |
1286 | }; | |
1287 | ||
1288 | static void stats_complete(struct stats_details *d) { | |
1289 | char *s; | |
ff76b21b | 1290 | |
d6dde5a3 RK |
1291 | if(!(d->exited && d->closed)) |
1292 | return; | |
1293 | byte_xasprintf(&s, "\n" | |
1294 | "Server stats:\n" | |
1295 | "track lookup cache hits: %lu\n" | |
1296 | "track lookup cache misses: %lu\n", | |
1297 | cache_files_hits, | |
1298 | cache_files_misses); | |
1299 | dynstr_append_string(d->data, s); | |
1300 | dynstr_terminate(d->data); | |
1301 | d->done(d->data->vec, d->u); | |
1302 | } | |
1303 | ||
1304 | static int stats_finished(ev_source attribute((unused)) *ev, | |
1305 | pid_t attribute((unused)) pid, | |
1306 | int status, | |
1307 | const struct rusage attribute((unused)) *rusage, | |
1308 | void *u) { | |
1309 | struct stats_details *const d = u; | |
1310 | ||
1311 | d->exited = 1; | |
1312 | if(status) | |
1313 | error(0, "disorder-stats %s", wstat(status)); | |
1314 | stats_complete(d); | |
1315 | return 0; | |
1316 | } | |
1317 | ||
1318 | static int stats_read(ev_source attribute((unused)) *ev, | |
1319 | ev_reader *reader, | |
d6dde5a3 RK |
1320 | void *ptr, |
1321 | size_t bytes, | |
1322 | int eof, | |
1323 | void *u) { | |
1324 | struct stats_details *const d = u; | |
1325 | ||
1326 | dynstr_append_bytes(d->data, ptr, bytes); | |
1327 | ev_reader_consume(reader, bytes); | |
1328 | if(eof) | |
1329 | d->closed = 1; | |
1330 | stats_complete(d); | |
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | static int stats_error(ev_source attribute((unused)) *ev, | |
d6dde5a3 RK |
1335 | int errno_value, |
1336 | void *u) { | |
1337 | struct stats_details *const d = u; | |
1338 | ||
1339 | error(errno_value, "error reading from pipe to disorder-stats"); | |
1340 | d->closed = 1; | |
1341 | stats_complete(d); | |
1342 | return 0; | |
1343 | } | |
1344 | ||
1345 | void trackdb_stats_subprocess(ev_source *ev, | |
1346 | void (*done)(char *data, void *u), | |
1347 | void *u) { | |
1348 | int p[2]; | |
1349 | pid_t pid; | |
1350 | struct stats_details *d = xmalloc(sizeof *d); | |
1351 | ||
1352 | dynstr_init(d->data); | |
1353 | d->done = done; | |
1354 | d->u = u; | |
1355 | xpipe(p); | |
1356 | pid = subprogram(ev, "disorder-stats", p[1]); | |
1357 | xclose(p[1]); | |
1358 | ev_child(ev, pid, 0, stats_finished, d); | |
1359 | ev_reader_new(ev, p[0], stats_read, stats_error, d, "disorder-stats reader"); | |
1360 | } | |
1361 | ||
460b9539 | 1362 | /* set a pref (remove if value=0) */ |
1363 | int trackdb_set(const char *track, | |
1364 | const char *name, | |
1365 | const char *value) { | |
1366 | struct kvp *t, *p, *a; | |
1367 | DB_TXN *tid; | |
1368 | int err, cmp; | |
1369 | char *oldalias, *newalias, **oldtags = 0, **newtags; | |
1370 | ||
f26f062a | 1371 | if(value) { |
1372 | /* TODO: if value matches default then set value=0 */ | |
1373 | } | |
f0feb22e | 1374 | |
460b9539 | 1375 | for(;;) { |
1376 | tid = trackdb_begin_transaction(); | |
1377 | if((err = gettrackdata(track, &t, &p, 0, | |
1378 | 0, tid)) == DB_LOCK_DEADLOCK) | |
1379 | goto fail; | |
1380 | if(err == DB_NOTFOUND) break; | |
1381 | if(name[0] == '_') { | |
1382 | if(kvp_set(&t, name, value)) | |
1383 | if(trackdb_putdata(trackdb_tracksdb, track, t, tid, 0)) | |
1384 | goto fail; | |
1385 | } else { | |
1386 | /* get the old alias name */ | |
1387 | if(compute_alias(&oldalias, track, p, tid)) goto fail; | |
1388 | /* get the old tags */ | |
1389 | if(!strcmp(name, "tags")) | |
1390 | oldtags = parsetags(kvp_get(p, "tags")); | |
1391 | /* set the value */ | |
1392 | if(kvp_set(&p, name, value)) | |
1393 | if(trackdb_putdata(trackdb_prefsdb, track, p, tid, 0)) | |
1394 | goto fail; | |
1395 | /* compute the new alias name */ | |
1396 | if((err = compute_alias(&newalias, track, p, tid))) goto fail; | |
1397 | /* check whether alias has changed */ | |
1398 | if(!(oldalias == newalias | |
1399 | || (oldalias && newalias && !strcmp(oldalias, newalias)))) { | |
1400 | /* adjust alias records to fit change */ | |
1401 | if(oldalias | |
f0feb22e RK |
1402 | && trackdb_delkey(trackdb_tracksdb, oldalias, tid) == DB_LOCK_DEADLOCK) |
1403 | goto fail; | |
460b9539 | 1404 | if(newalias) { |
1405 | a = 0; | |
1406 | kvp_set(&a, "_alias_for", track); | |
1407 | if(trackdb_putdata(trackdb_tracksdb, newalias, a, tid, 0)) goto fail; | |
1408 | } | |
1409 | } | |
1410 | /* check whether tags have changed */ | |
1411 | if(!strcmp(name, "tags")) { | |
1412 | newtags = parsetags(value); | |
1413 | while(*oldtags || *newtags) { | |
1414 | if(*oldtags && *newtags) { | |
1415 | cmp = strcmp(*oldtags, *newtags); | |
1416 | if(!cmp) { | |
1417 | /* keeping this tag */ | |
1418 | ++oldtags; | |
1419 | ++newtags; | |
1420 | } else if(cmp < 0) | |
1421 | /* old tag fits into a gap in the new list, so delete old */ | |
1422 | goto delete_old; | |
1423 | else | |
1424 | /* new tag fits into a gap in the old list, so insert new */ | |
1425 | goto insert_new; | |
1426 | } else if(*oldtags) { | |
1427 | /* we've run out of new tags, so remaining old ones are to be | |
1428 | * deleted */ | |
1429 | delete_old: | |
1430 | if(trackdb_delkeydata(trackdb_tagsdb, | |
1431 | *oldtags, track, tid) == DB_LOCK_DEADLOCK) | |
1432 | goto fail; | |
1433 | ++oldtags; | |
1434 | } else { | |
1435 | /* we've run out of old tags, so remainig new ones are to be | |
1436 | * inserted */ | |
1437 | insert_new: | |
1438 | if(register_tag(track, *newtags, tid)) goto fail; | |
1439 | ++newtags; | |
1440 | } | |
1441 | } | |
1442 | reqtracks = 0; | |
1443 | } | |
1444 | } | |
1445 | err = 0; | |
1446 | break; | |
1447 | fail: | |
1448 | trackdb_abort_transaction(tid); | |
1449 | } | |
1450 | trackdb_commit_transaction(tid); | |
1451 | return err == 0 ? 0 : -1; | |
1452 | } | |
1453 | ||
1454 | /* get a pref */ | |
1455 | const char *trackdb_get(const char *track, | |
1456 | const char *name) { | |
1457 | return kvp_get(trackdb_get_all(track), name); | |
1458 | } | |
1459 | ||
1460 | /* get all prefs as a 0-terminated array */ | |
1461 | struct kvp *trackdb_get_all(const char *track) { | |
1462 | struct kvp *t, *p, **pp; | |
1463 | DB_TXN *tid; | |
1464 | ||
1465 | for(;;) { | |
1466 | tid = trackdb_begin_transaction(); | |
1467 | if(gettrackdata(track, &t, &p, 0, 0, tid) == DB_LOCK_DEADLOCK) | |
1468 | goto fail; | |
1469 | break; | |
1470 | fail: | |
1471 | trackdb_abort_transaction(tid); | |
1472 | } | |
1473 | trackdb_commit_transaction(tid); | |
1474 | for(pp = &p; *pp; pp = &(*pp)->next) | |
1475 | ; | |
1476 | *pp = t; | |
1477 | return p; | |
1478 | } | |
1479 | ||
1480 | /* resolve alias */ | |
1481 | const char *trackdb_resolve(const char *track) { | |
1482 | DB_TXN *tid; | |
1483 | const char *actual; | |
f0feb22e | 1484 | |
460b9539 | 1485 | for(;;) { |
1486 | tid = trackdb_begin_transaction(); | |
1487 | if(gettrackdata(track, 0, 0, &actual, 0, tid) == DB_LOCK_DEADLOCK) | |
1488 | goto fail; | |
1489 | break; | |
1490 | fail: | |
1491 | trackdb_abort_transaction(tid); | |
1492 | } | |
1493 | trackdb_commit_transaction(tid); | |
1494 | return actual; | |
1495 | } | |
1496 | ||
1497 | int trackdb_isalias(const char *track) { | |
1498 | const char *actual = trackdb_resolve(track); | |
1499 | ||
1500 | return strcmp(actual, track); | |
1501 | } | |
1502 | ||
1503 | /* test whether a track exists (perhaps an alias) */ | |
1504 | int trackdb_exists(const char *track) { | |
1505 | DB_TXN *tid; | |
1506 | int err; | |
1507 | ||
1508 | for(;;) { | |
1509 | tid = trackdb_begin_transaction(); | |
1510 | /* unusually, here we want the return value */ | |
1511 | if((err = gettrackdata(track, 0, 0, 0, 0, tid)) == DB_LOCK_DEADLOCK) | |
1512 | goto fail; | |
1513 | break; | |
1514 | fail: | |
1515 | trackdb_abort_transaction(tid); | |
1516 | } | |
1517 | trackdb_commit_transaction(tid); | |
1518 | return (err == 0); | |
1519 | } | |
1520 | ||
1521 | /* return the list of tags */ | |
1522 | char **trackdb_alltags(void) { | |
c3be4f19 RK |
1523 | int e; |
1524 | struct vector v[1]; | |
460b9539 | 1525 | |
5b3f0631 | 1526 | vector_init(v); |
c3be4f19 RK |
1527 | WITH_TRANSACTION(trackdb_listkeys(trackdb_tagsdb, v, tid)); |
1528 | return v->vec; | |
460b9539 | 1529 | } |
1530 | ||
c3be4f19 RK |
1531 | /** @brief List all the keys in @p db |
1532 | * @param db Database | |
1533 | * @param v Vector to store keys in | |
1534 | * @param tid Transaction ID | |
1535 | * @return 0 or DB_LOCK_DEADLOCK | |
1536 | */ | |
1537 | int trackdb_listkeys(DB *db, struct vector *v, DB_TXN *tid) { | |
1538 | int e; | |
460b9539 | 1539 | DBT k, d; |
c3be4f19 | 1540 | DBC *const c = trackdb_opencursor(db, tid); |
460b9539 | 1541 | |
c3be4f19 | 1542 | v->nvec = 0; |
460b9539 | 1543 | memset(&k, 0, sizeof k); |
c3be4f19 RK |
1544 | while(!(e = c->c_get(c, &k, prepare_data(&d), DB_NEXT_NODUP))) |
1545 | vector_append(v, xstrndup(k.data, k.size)); | |
1546 | switch(e) { | |
460b9539 | 1547 | case DB_NOTFOUND: |
1548 | break; | |
1549 | case DB_LOCK_DEADLOCK: | |
c3be4f19 | 1550 | return e; |
460b9539 | 1551 | default: |
c3be4f19 | 1552 | fatal(0, "c->c_get: %s", db_strerror(e)); |
460b9539 | 1553 | } |
c3be4f19 RK |
1554 | if((e = trackdb_closecursor(c))) |
1555 | return e; | |
1556 | vector_terminate(v); | |
460b9539 | 1557 | return 0; |
1558 | } | |
1559 | ||
1560 | /* return 1 iff sorted tag lists A and B have at least one member in common */ | |
1561 | static int tag_intersection(char **a, char **b) { | |
1562 | int cmp; | |
1563 | ||
1564 | /* Same sort of logic as trackdb_set() above */ | |
1565 | while(*a && *b) { | |
1566 | if(!(cmp = strcmp(*a, *b))) return 1; | |
1567 | else if(cmp < 0) ++a; | |
1568 | else ++b; | |
1569 | } | |
1570 | return 0; | |
1571 | } | |
1572 | ||
1573 | /* Check whether a track is suitable for random play. Returns 0 if it is, | |
6aba3f6c RK |
1574 | * DB_NOTFOUND if it is not or DB_LOCK_DEADLOCK if the database gave us |
1575 | * that. */ | |
460b9539 | 1576 | static int check_suitable(const char *track, |
1577 | DB_TXN *tid, | |
1578 | char **required_tags, | |
1579 | char **prohibited_tags) { | |
1580 | char **track_tags; | |
1581 | time_t last, now; | |
1582 | struct kvp *p, *t; | |
1583 | const char *pick_at_random, *played_time; | |
1584 | ||
6aba3f6c RK |
1585 | /* don't pick tracks that aren't in any surviving collection (for instance |
1586 | * you've edited the config but the rescan hasn't done its job yet) */ | |
1587 | if(!find_track_root(track)) { | |
1588 | info("found track not in any collection: %s", track); | |
1589 | return DB_NOTFOUND; | |
1590 | } | |
460b9539 | 1591 | /* don't pick aliases - only pick the canonical form */ |
1592 | if(gettrackdata(track, &t, &p, 0, 0, tid) == DB_LOCK_DEADLOCK) | |
1593 | return DB_LOCK_DEADLOCK; | |
1594 | if(kvp_get(t, "_alias_for")) | |
1595 | return DB_NOTFOUND; | |
1596 | /* check that random play is not suppressed for this track */ | |
1597 | if((pick_at_random = kvp_get(p, "pick_at_random")) | |
1598 | && !strcmp(pick_at_random, "0")) | |
1599 | return DB_NOTFOUND; | |
1600 | /* don't pick a track that's been played in the last 8 hours */ | |
1601 | if((played_time = kvp_get(p, "played_time"))) { | |
1602 | last = atoll(played_time); | |
1603 | now = time(0); | |
1604 | if(now < last + 8 * 3600) /* TODO configurable */ | |
1605 | return DB_NOTFOUND; | |
1606 | } | |
1607 | track_tags = parsetags(kvp_get(p, "tags")); | |
1608 | /* check that no prohibited tag is present for this track */ | |
1609 | if(prohibited_tags && tag_intersection(track_tags, prohibited_tags)) | |
1610 | return DB_NOTFOUND; | |
1611 | /* check that at least one required tags is present for this track */ | |
1612 | if(*required_tags && !tag_intersection(track_tags, required_tags)) | |
1613 | return DB_NOTFOUND; | |
1614 | return 0; | |
1615 | } | |
1616 | ||
1617 | /* attempt to pick a random non-alias track */ | |
1618 | const char *trackdb_random(int tries) { | |
1619 | DBT key, data; | |
1620 | DB_BTREE_STAT *sp; | |
1621 | int err, n; | |
1622 | DB_TXN *tid; | |
1623 | const char *track, *candidate; | |
1624 | db_recno_t r; | |
1625 | const char *tags; | |
1626 | char **required_tags, **prohibited_tags, **tp; | |
1627 | hash *h; | |
1628 | DBC *c = 0; | |
1629 | ||
1630 | for(;;) { | |
1631 | tid = trackdb_begin_transaction(); | |
1632 | if((err = trackdb_get_global_tid("required-tags", tid, &tags))) | |
1633 | goto fail; | |
1634 | required_tags = parsetags(tags); | |
1635 | if((err = trackdb_get_global_tid("prohibited-tags", tid, &tags))) | |
1636 | goto fail; | |
1637 | prohibited_tags = parsetags(tags); | |
1638 | track = 0; | |
1639 | if(*required_tags) { | |
1640 | /* Bung all the suitable tracks into a hash and convert to a list of keys | |
1641 | * (to eliminate duplicates). We cache this list since it is possible | |
1642 | * that it will be very large. */ | |
1643 | if(!reqtracks) { | |
1644 | h = hash_new(0); | |
1645 | for(tp = required_tags; *tp; ++tp) { | |
1646 | c = trackdb_opencursor(trackdb_tagsdb, tid); | |
1647 | memset(&key, 0, sizeof key); | |
1648 | key.data = *tp; | |
1649 | key.size = strlen(*tp); | |
1650 | n = 0; | |
1651 | err = c->c_get(c, &key, prepare_data(&data), DB_SET); | |
1652 | while(err == 0) { | |
1653 | hash_add(h, xstrndup(data.data, data.size), 0, | |
1654 | HASH_INSERT_OR_REPLACE); | |
1655 | ++n; | |
1656 | err = c->c_get(c, &key, prepare_data(&data), DB_NEXT_DUP); | |
1657 | } | |
1658 | switch(err) { | |
1659 | case 0: | |
1660 | case DB_NOTFOUND: | |
1661 | break; | |
1662 | case DB_LOCK_DEADLOCK: | |
1663 | goto fail; | |
1664 | default: | |
1665 | fatal(0, "error querying tags.db: %s", db_strerror(err)); | |
1666 | } | |
1667 | trackdb_closecursor(c); | |
1668 | c = 0; | |
1669 | if(!n) | |
1670 | error(0, "required tag %s does not match any tracks", *tp); | |
1671 | } | |
1672 | nreqtracks = hash_count(h); | |
1673 | reqtracks = hash_keys(h); | |
1674 | } | |
1675 | while(nreqtracks && !track && tries-- > 0) { | |
1676 | r = (rand() * (double)nreqtracks / (RAND_MAX + 1.0)); | |
1677 | candidate = reqtracks[r]; | |
1678 | switch(check_suitable(candidate, tid, | |
1679 | required_tags, prohibited_tags)) { | |
1680 | case 0: | |
1681 | track = candidate; | |
1682 | break; | |
1683 | case DB_NOTFOUND: | |
1684 | break; | |
1685 | case DB_LOCK_DEADLOCK: | |
1686 | goto fail; | |
1687 | } | |
1688 | } | |
1689 | } else { | |
1690 | /* No required tags. We pick random record numbers in the database | |
1691 | * instead. */ | |
58a4416c | 1692 | switch(err = trackdb_tracksdb->stat(trackdb_tracksdb, tid, &sp, 0)) { |
460b9539 | 1693 | case 0: |
1694 | break; | |
1695 | case DB_LOCK_DEADLOCK: | |
1696 | error(0, "error querying tracks.db: %s", db_strerror(err)); | |
1697 | goto fail; | |
1698 | default: | |
1699 | fatal(0, "error querying tracks.db: %s", db_strerror(err)); | |
1700 | } | |
1701 | if(!sp->bt_nkeys) | |
1702 | error(0, "cannot pick tracks at random from an empty database"); | |
1703 | while(sp->bt_nkeys && !track && tries-- > 0) { | |
1704 | /* record numbers count from 1 upwards */ | |
1705 | r = 1 + (rand() * (double)sp->bt_nkeys / (RAND_MAX + 1.0)); | |
1706 | memset(&key, sizeof key, 0); | |
1707 | key.flags = DB_DBT_MALLOC; | |
1708 | key.size = sizeof r; | |
1709 | key.data = &r; | |
1710 | switch(err = trackdb_tracksdb->get(trackdb_tracksdb, tid, &key, prepare_data(&data), | |
1711 | DB_SET_RECNO)) { | |
1712 | case 0: | |
1713 | break; | |
1714 | case DB_LOCK_DEADLOCK: | |
1715 | error(0, "error querying tracks.db: %s", db_strerror(err)); | |
1716 | goto fail; | |
1717 | default: | |
1718 | fatal(0, "error querying tracks.db: %s", db_strerror(err)); | |
1719 | } | |
1720 | candidate = xstrndup(key.data, key.size); | |
1721 | switch(check_suitable(candidate, tid, | |
1722 | required_tags, prohibited_tags)) { | |
1723 | case 0: | |
1724 | track = candidate; | |
1725 | break; | |
1726 | case DB_NOTFOUND: | |
1727 | break; | |
1728 | case DB_LOCK_DEADLOCK: | |
1729 | goto fail; | |
1730 | } | |
1731 | } | |
1732 | } | |
1733 | break; | |
1734 | fail: | |
1735 | trackdb_closecursor(c); | |
1736 | c = 0; | |
1737 | trackdb_abort_transaction(tid); | |
1738 | } | |
1739 | trackdb_commit_transaction(tid); | |
1740 | if(!track) | |
1741 | error(0, "could not pick a random track"); | |
1742 | return track; | |
1743 | } | |
1744 | ||
1745 | /* get a track name given the prefs. Set *used_db to 1 if we got the answer | |
1746 | * from the prefs. */ | |
1747 | static const char *getpart(const char *track, | |
1748 | const char *context, | |
1749 | const char *part, | |
1750 | const struct kvp *p, | |
1751 | int *used_db) { | |
1752 | const char *result; | |
1753 | char *pref; | |
1754 | ||
1755 | byte_xasprintf(&pref, "trackname_%s_%s", context, part); | |
1756 | if((result = kvp_get(p, pref))) | |
1757 | *used_db = 1; | |
1758 | else | |
1759 | result = trackname_part(track, context, part); | |
1760 | assert(result != 0); | |
1761 | return result; | |
1762 | } | |
1763 | ||
1764 | /* get a track name part, like trackname_part(), but taking the database into | |
1765 | * account. */ | |
1766 | const char *trackdb_getpart(const char *track, | |
1767 | const char *context, | |
1768 | const char *part) { | |
1769 | struct kvp *p; | |
1770 | DB_TXN *tid; | |
1771 | char *pref; | |
1772 | const char *actual; | |
1773 | int used_db, err; | |
1774 | ||
1775 | /* construct the full pref */ | |
1776 | byte_xasprintf(&pref, "trackname_%s_%s", context, part); | |
1777 | for(;;) { | |
1778 | tid = trackdb_begin_transaction(); | |
1779 | if((err = gettrackdata(track, 0, &p, &actual, 0, tid)) == DB_LOCK_DEADLOCK) | |
1780 | goto fail; | |
1781 | break; | |
1782 | fail: | |
1783 | trackdb_abort_transaction(tid); | |
1784 | } | |
1785 | trackdb_commit_transaction(tid); | |
1786 | return getpart(actual, context, part, p, &used_db); | |
1787 | } | |
1788 | ||
1789 | /* get the raw path name for @track@ (might be an alias) */ | |
1790 | const char *trackdb_rawpath(const char *track) { | |
1791 | DB_TXN *tid; | |
1792 | struct kvp *t; | |
1793 | const char *path; | |
1794 | ||
1795 | for(;;) { | |
1796 | tid = trackdb_begin_transaction(); | |
1797 | if(gettrackdata(track, &t, 0, 0, 0, tid) == DB_LOCK_DEADLOCK) | |
1798 | goto fail; | |
1799 | break; | |
1800 | fail: | |
1801 | trackdb_abort_transaction(tid); | |
1802 | } | |
1803 | trackdb_commit_transaction(tid); | |
1804 | if(!(path = kvp_get(t, "_path"))) path = track; | |
1805 | return path; | |
1806 | } | |
1807 | ||
1808 | /* trackdb_list **************************************************************/ | |
1809 | ||
1810 | /* this is incredibly ugly, sorry, perhaps it will be rewritten to be actually | |
1811 | * readable at some point */ | |
1812 | ||
1813 | /* return true if the basename of TRACK[0..TL-1], as defined by DL, matches RE. | |
1814 | * If RE is a null pointer then it matches everything. */ | |
1815 | static int track_matches(size_t dl, const char *track, size_t tl, | |
1816 | const pcre *re) { | |
1817 | int ovec[3], rc; | |
1818 | ||
1819 | if(!re) | |
1820 | return 1; | |
1821 | track += dl + 1; | |
1822 | tl -= (dl + 1); | |
1823 | switch(rc = pcre_exec(re, 0, track, tl, 0, 0, ovec, 3)) { | |
1824 | case PCRE_ERROR_NOMATCH: return 0; | |
1825 | default: | |
1826 | if(rc < 0) { | |
1827 | error(0, "pcre_exec returned %d, subject '%s'", rc, track); | |
1828 | return 0; | |
1829 | } | |
1830 | return 1; | |
1831 | } | |
1832 | } | |
1833 | ||
1834 | static int do_list(struct vector *v, const char *dir, | |
1835 | enum trackdb_listable what, const pcre *re, DB_TXN *tid) { | |
1836 | DBC *cursor; | |
1837 | DBT k, d; | |
1838 | size_t dl; | |
1839 | char *ptr; | |
1840 | int err; | |
1841 | size_t l, last_dir_len = 0; | |
1842 | char *last_dir = 0, *track, *alias; | |
1843 | struct kvp *p; | |
f0feb22e | 1844 | |
460b9539 | 1845 | dl = strlen(dir); |
1846 | cursor = trackdb_opencursor(trackdb_tracksdb, tid); | |
1847 | make_key(&k, dir); | |
1848 | prepare_data(&d); | |
1849 | /* find the first key >= dir */ | |
1850 | err = cursor->c_get(cursor, &k, &d, DB_SET_RANGE); | |
1851 | /* keep going while we're dealing with <dir/anything> */ | |
1852 | while(err == 0 | |
1853 | && k.size > dl | |
1854 | && ((char *)k.data)[dl] == '/' | |
1855 | && !memcmp(k.data, dir, dl)) { | |
1856 | ptr = memchr((char *)k.data + dl + 1, '/', k.size - (dl + 1)); | |
1857 | if(ptr) { | |
1858 | /* we have <dir/component/anything>, so <dir/component> is a directory */ | |
1859 | l = ptr - (char *)k.data; | |
1860 | if(what & trackdb_directories) | |
1861 | if(!(last_dir | |
1862 | && l == last_dir_len | |
1863 | && !memcmp(last_dir, k.data, l))) { | |
1864 | last_dir = xstrndup(k.data, last_dir_len = l); | |
1865 | if(track_matches(dl, k.data, l, re)) | |
1866 | vector_append(v, last_dir); | |
1867 | } | |
1868 | } else { | |
1869 | /* found a plain file */ | |
1870 | if((what & trackdb_files)) { | |
1871 | track = xstrndup(k.data, k.size); | |
1872 | if((err = trackdb_getdata(trackdb_prefsdb, | |
1873 | track, &p, tid)) == DB_LOCK_DEADLOCK) | |
1874 | goto deadlocked; | |
1875 | /* if this file has an alias in the same directory then we skip it */ | |
1876 | if((err = compute_alias(&alias, track, p, tid))) | |
1877 | goto deadlocked; | |
1878 | if(!(alias && !strcmp(d_dirname(alias), d_dirname(track)))) | |
1879 | if(track_matches(dl, k.data, k.size, re)) | |
1880 | vector_append(v, track); | |
1881 | } | |
1882 | } | |
1883 | err = cursor->c_get(cursor, &k, &d, DB_NEXT); | |
1884 | } | |
1885 | switch(err) { | |
1886 | case 0: | |
1887 | break; | |
1888 | case DB_NOTFOUND: | |
1889 | err = 0; | |
1890 | break; | |
1891 | case DB_LOCK_DEADLOCK: | |
1892 | error(0, "error querying database: %s", db_strerror(err)); | |
1893 | break; | |
1894 | default: | |
1895 | fatal(0, "error querying database: %s", db_strerror(err)); | |
1896 | } | |
1897 | deadlocked: | |
1898 | if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK; | |
1899 | return err; | |
1900 | } | |
1901 | ||
1902 | /* return the directories or files below @dir@ */ | |
1903 | char **trackdb_list(const char *dir, int *np, enum trackdb_listable what, | |
1904 | const pcre *re) { | |
1905 | DB_TXN *tid; | |
1906 | int n; | |
1907 | struct vector v; | |
1908 | ||
1909 | vector_init(&v); | |
1910 | for(;;) { | |
1911 | tid = trackdb_begin_transaction(); | |
1912 | v.nvec = 0; | |
1913 | if(dir) { | |
1914 | if(do_list(&v, dir, what, re, tid)) | |
1915 | goto fail; | |
1916 | } else { | |
1917 | for(n = 0; n < config->collection.n; ++n) | |
1918 | if(do_list(&v, config->collection.s[n].root, what, re, tid)) | |
1919 | goto fail; | |
1920 | } | |
1921 | break; | |
1922 | fail: | |
1923 | trackdb_abort_transaction(tid); | |
1924 | } | |
1925 | trackdb_commit_transaction(tid); | |
1926 | vector_terminate(&v); | |
1927 | if(np) | |
1928 | *np = v.nvec; | |
1929 | return v.vec; | |
f0feb22e | 1930 | } |
460b9539 | 1931 | |
1932 | /* If S is tag:something, return something. Else return 0. */ | |
1933 | static const char *checktag(const char *s) { | |
1934 | if(!strncmp(s, "tag:", 4)) | |
1935 | return s + 4; | |
1936 | else | |
1937 | return 0; | |
1938 | } | |
1939 | ||
1940 | /* return a list of tracks containing all of the words given. If you | |
1941 | * ask for only stopwords you get no tracks. */ | |
1942 | char **trackdb_search(char **wordlist, int nwordlist, int *ntracks) { | |
1943 | const char **w, *best = 0, *tag; | |
1944 | char **twords, **tags; | |
d1d4a182 | 1945 | char *istag; |
460b9539 | 1946 | int i, j, n, err, what; |
1947 | DBC *cursor = 0; | |
1948 | DBT k, d; | |
1949 | struct vector u, v; | |
1950 | DB_TXN *tid; | |
1951 | struct kvp *p; | |
1952 | int ntags = 0; | |
1953 | DB *db; | |
1954 | const char *dbname; | |
1955 | ||
1956 | *ntracks = 0; /* for early returns */ | |
3c82b504 | 1957 | /* normalize all the words */ |
460b9539 | 1958 | w = xmalloc(nwordlist * sizeof (char *)); |
d1d4a182 | 1959 | istag = xmalloc_noptr(nwordlist); |
460b9539 | 1960 | for(n = 0; n < nwordlist; ++n) { |
3c82b504 RK |
1961 | uint32_t *w32; |
1962 | size_t nw32; | |
f0feb22e | 1963 | |
c85b7022 | 1964 | w[n] = utf8_casefold_compat(wordlist[n], strlen(wordlist[n]), 0); |
a3777123 RK |
1965 | if(checktag(w[n])) { |
1966 | ++ntags; /* count up tags */ | |
1967 | /* Normalize the tag */ | |
d1d4a182 RK |
1968 | w[n] = normalize_tag(w[n] + 4, strlen(w[n] + 4)); |
1969 | istag[n] = 1; | |
a3777123 RK |
1970 | } else { |
1971 | /* Normalize the search term by removing combining characters */ | |
1972 | if(!(w32 = utf8_to_utf32(w[n], strlen(w[n]), &nw32))) | |
1973 | return 0; | |
1974 | nw32 = remove_combining_chars(w32, nw32); | |
1975 | if(!(w[n] = utf32_to_utf8(w32, nw32, 0))) | |
1976 | return 0; | |
d1d4a182 | 1977 | istag[n] = 0; |
a3777123 | 1978 | } |
460b9539 | 1979 | } |
1980 | /* find the longest non-stopword */ | |
1981 | for(n = 0; n < nwordlist; ++n) | |
d1d4a182 | 1982 | if(!istag[n] && !stopword(w[n])) |
460b9539 | 1983 | if(!best || strlen(w[n]) > strlen(best)) |
1984 | best = w[n]; | |
1985 | /* TODO: we should at least in principal be able to identify the word or tag | |
1986 | * with the least matches in log time, and choose that as our primary search | |
1987 | * term. */ | |
1988 | if(ntags && !best) { | |
1989 | /* Only tags are listed. We limit to the first and narrow down with the | |
1990 | * rest. */ | |
d1d4a182 | 1991 | best = istag[0] ? w[0] : 0; |
460b9539 | 1992 | db = trackdb_tagsdb; |
1993 | dbname = "tags"; | |
1994 | } else if(best) { | |
1995 | /* We can limit to some word. */ | |
1996 | db = trackdb_searchdb; | |
1997 | dbname = "search"; | |
1998 | } else { | |
1999 | /* Only stopwords */ | |
2000 | return 0; | |
2001 | } | |
2002 | vector_init(&u); | |
2003 | vector_init(&v); | |
2004 | for(;;) { | |
2005 | tid = trackdb_begin_transaction(); | |
2006 | /* find all the tracks that have that word */ | |
2007 | make_key(&k, best); | |
2008 | prepare_data(&d); | |
2009 | what = DB_SET; | |
2010 | v.nvec = 0; | |
2011 | cursor = trackdb_opencursor(db, tid); | |
2012 | while(!(err = cursor->c_get(cursor, &k, &d, what))) { | |
2013 | vector_append(&v, xstrndup(d.data, d.size)); | |
2014 | what = DB_NEXT_DUP; | |
2015 | } | |
2016 | switch(err) { | |
2017 | case DB_NOTFOUND: | |
2018 | err = 0; | |
2019 | break; | |
2020 | case DB_LOCK_DEADLOCK: | |
2021 | error(0, "error querying %s database: %s", dbname, db_strerror(err)); | |
2022 | break; | |
2023 | default: | |
2024 | fatal(0, "error querying %s database: %s", dbname, db_strerror(err)); | |
2025 | } | |
2026 | if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK; | |
2027 | cursor = 0; | |
2028 | /* do a naive search over that (hopefuly fairly small) list of tracks */ | |
2029 | u.nvec = 0; | |
2030 | for(n = 0; n < v.nvec; ++n) { | |
2031 | if((err = gettrackdata(v.vec[n], 0, &p, 0, 0, tid) == DB_LOCK_DEADLOCK)) | |
2032 | goto fail; | |
2033 | else if(err) { | |
2034 | error(0, "track %s unexpected error: %s", v.vec[n], db_strerror(err)); | |
2035 | continue; | |
2036 | } | |
2037 | twords = track_to_words(v.vec[n], p); | |
2038 | tags = parsetags(kvp_get(p, "tags")); | |
2039 | for(i = 0; i < nwordlist; ++i) { | |
d1d4a182 RK |
2040 | if(istag[i]) { |
2041 | tag = w[i]; | |
460b9539 | 2042 | /* Track must have this tag */ |
2043 | for(j = 0; tags[j]; ++j) | |
2044 | if(!strcmp(tag, tags[j])) break; /* tag found */ | |
2045 | if(!tags[j]) break; /* tag not found */ | |
2046 | } else { | |
2047 | /* Track must contain this word */ | |
2048 | for(j = 0; twords[j]; ++j) | |
2049 | if(!strcmp(w[i], twords[j])) break; /* word found */ | |
2050 | if(!twords[j]) break; /* word not found */ | |
2051 | } | |
2052 | } | |
2053 | if(i >= nwordlist) /* all words found */ | |
2054 | vector_append(&u, v.vec[n]); | |
2055 | } | |
2056 | break; | |
2057 | fail: | |
2058 | trackdb_closecursor(cursor); | |
2059 | cursor = 0; | |
2060 | trackdb_abort_transaction(tid); | |
2061 | info("retrying search"); | |
2062 | } | |
2063 | trackdb_commit_transaction(tid); | |
2064 | vector_terminate(&u); | |
2065 | if(ntracks) | |
2066 | *ntracks = u.nvec; | |
2067 | return u.vec; | |
2068 | } | |
2069 | ||
2070 | /* trackdb_scan **************************************************************/ | |
2071 | ||
2072 | int trackdb_scan(const char *root, | |
2073 | int (*callback)(const char *track, | |
2074 | struct kvp *data, | |
2075 | void *u, | |
2076 | DB_TXN *tid), | |
2077 | void *u, | |
2078 | DB_TXN *tid) { | |
2079 | DBC *cursor; | |
2080 | DBT k, d; | |
d1694464 | 2081 | const size_t root_len = root ? strlen(root) : 0; |
2082 | int err, cberr; | |
460b9539 | 2083 | struct kvp *data; |
d1694464 | 2084 | const char *track; |
460b9539 | 2085 | |
2086 | cursor = trackdb_opencursor(trackdb_tracksdb, tid); | |
d1694464 | 2087 | if(root) |
2088 | err = cursor->c_get(cursor, make_key(&k, root), prepare_data(&d), | |
2089 | DB_SET_RANGE); | |
2090 | else { | |
2091 | memset(&k, 0, sizeof k); | |
2092 | err = cursor->c_get(cursor, &k, prepare_data(&d), | |
2093 | DB_FIRST); | |
2094 | } | |
460b9539 | 2095 | while(!err) { |
d1694464 | 2096 | if(!root |
2097 | || (k.size > root_len | |
2098 | && !strncmp(k.data, root, root_len) | |
2099 | && ((char *)k.data)[root_len] == '/')) { | |
460b9539 | 2100 | data = kvp_urldecode(d.data, d.size); |
d1694464 | 2101 | if(kvp_get(data, "_path")) { |
2102 | track = xstrndup(k.data, k.size); | |
2103 | /* Advance to the next track before the callback so that the callback | |
2104 | * may safely delete the track */ | |
2105 | err = cursor->c_get(cursor, &k, &d, DB_NEXT); | |
2106 | if((cberr = callback(track, data, u, tid))) { | |
2107 | err = cberr; | |
460b9539 | 2108 | break; |
d1694464 | 2109 | } |
2110 | } else | |
2111 | err = cursor->c_get(cursor, &k, &d, DB_NEXT); | |
460b9539 | 2112 | } else |
2113 | break; | |
2114 | } | |
2115 | trackdb_closecursor(cursor); | |
2116 | switch(err) { | |
2117 | case EINTR: | |
2118 | return err; | |
2119 | case 0: | |
2120 | case DB_NOTFOUND: | |
2121 | return 0; | |
2122 | case DB_LOCK_DEADLOCK: | |
2123 | error(0, "c->c_get: %s", db_strerror(err)); | |
2124 | return err; | |
2125 | default: | |
2126 | fatal(0, "c->c_get: %s", db_strerror(err)); | |
2127 | } | |
2128 | } | |
2129 | ||
2130 | /* trackdb_rescan ************************************************************/ | |
2131 | ||
2132 | /* called when the rescanner terminates */ | |
2133 | static int reap_rescan(ev_source attribute((unused)) *ev, | |
2134 | pid_t pid, | |
2135 | int status, | |
2136 | const struct rusage attribute((unused)) *rusage, | |
2137 | void attribute((unused)) *u) { | |
2138 | if(pid == rescan_pid) rescan_pid = -1; | |
2139 | if(status) | |
c0bcd134 | 2140 | error(0, RESCAN": %s", wstat(status)); |
460b9539 | 2141 | else |
c0bcd134 | 2142 | D((RESCAN" terminated: %s", wstat(status))); |
460b9539 | 2143 | /* Our cache of file lookups is out of date now */ |
2144 | cache_clean(&cache_files_type); | |
e025abff | 2145 | eventlog("rescanned", (char *)0); |
460b9539 | 2146 | return 0; |
2147 | } | |
2148 | ||
2149 | void trackdb_rescan(ev_source *ev) { | |
1a4a6350 | 2150 | int w; |
c0bcd134 | 2151 | |
460b9539 | 2152 | if(rescan_pid != -1) { |
2153 | error(0, "rescan already underway"); | |
2154 | return; | |
2155 | } | |
d6dde5a3 | 2156 | rescan_pid = subprogram(ev, RESCAN, -1); |
1a4a6350 RK |
2157 | if(ev) { |
2158 | ev_child(ev, rescan_pid, 0, reap_rescan, 0); | |
2159 | D(("started rescanner")); | |
2160 | } else { | |
2161 | /* This is the first rescan, we block until it is complete */ | |
2162 | while(waitpid(rescan_pid, &w, 0) < 0 && errno == EINTR) | |
2163 | ; | |
2164 | reap_rescan(0, rescan_pid, w, 0, 0); | |
2165 | } | |
460b9539 | 2166 | } |
2167 | ||
2168 | int trackdb_rescan_cancel(void) { | |
2169 | if(rescan_pid == -1) return 0; | |
2170 | if(kill(rescan_pid, SIGTERM) < 0) | |
2171 | fatal(errno, "error killing rescanner"); | |
2172 | rescan_pid = -1; | |
2173 | return 1; | |
2174 | } | |
2175 | ||
2176 | /* global prefs **************************************************************/ | |
2177 | ||
2178 | void trackdb_set_global(const char *name, | |
2179 | const char *value, | |
2180 | const char *who) { | |
2181 | DB_TXN *tid; | |
460b9539 | 2182 | int err; |
2183 | int state; | |
2184 | ||
460b9539 | 2185 | for(;;) { |
2186 | tid = trackdb_begin_transaction(); | |
f9635e06 RK |
2187 | if(!(err = trackdb_set_global_tid(name, value, tid))) |
2188 | break; | |
460b9539 | 2189 | trackdb_abort_transaction(tid); |
2190 | } | |
2191 | trackdb_commit_transaction(tid); | |
2192 | /* log important state changes */ | |
2193 | if(!strcmp(name, "playing")) { | |
2194 | state = !value || !strcmp(value, "yes"); | |
2195 | info("playing %s by %s", | |
2196 | state ? "enabled" : "disabled", | |
2197 | who ? who : "-"); | |
2198 | eventlog("state", state ? "enable_play" : "disable_play", (char *)0); | |
2199 | } | |
2200 | if(!strcmp(name, "random-play")) { | |
2201 | state = !value || !strcmp(value, "yes"); | |
2202 | info("random play %s by %s", | |
2203 | state ? "enabled" : "disabled", | |
2204 | who ? who : "-"); | |
2205 | eventlog("state", state ? "enable_random" : "disable_random", (char *)0); | |
2206 | } | |
2207 | if(!strcmp(name, "required-tags")) | |
2208 | reqtracks = 0; | |
2209 | } | |
2210 | ||
f9635e06 RK |
2211 | int trackdb_set_global_tid(const char *name, |
2212 | const char *value, | |
2213 | DB_TXN *tid) { | |
2214 | DBT k, d; | |
2215 | int err; | |
2216 | ||
2217 | memset(&k, 0, sizeof k); | |
2218 | memset(&d, 0, sizeof d); | |
2219 | k.data = (void *)name; | |
2220 | k.size = strlen(name); | |
2221 | if(value) { | |
2222 | d.data = (void *)value; | |
2223 | d.size = strlen(value); | |
2224 | } | |
2225 | if(value) | |
2226 | err = trackdb_globaldb->put(trackdb_globaldb, tid, &k, &d, 0); | |
2227 | else | |
2228 | err = trackdb_globaldb->del(trackdb_globaldb, tid, &k, 0); | |
2229 | if(err == DB_LOCK_DEADLOCK) return err; | |
2230 | if(err) | |
2231 | fatal(0, "error updating database: %s", db_strerror(err)); | |
2232 | return 0; | |
2233 | } | |
2234 | ||
460b9539 | 2235 | const char *trackdb_get_global(const char *name) { |
2236 | DB_TXN *tid; | |
2237 | int err; | |
2238 | const char *r; | |
2239 | ||
2240 | for(;;) { | |
2241 | tid = trackdb_begin_transaction(); | |
2242 | if(!(err = trackdb_get_global_tid(name, tid, &r))) | |
2243 | break; | |
2244 | trackdb_abort_transaction(tid); | |
2245 | } | |
2246 | trackdb_commit_transaction(tid); | |
2247 | return r; | |
2248 | } | |
2249 | ||
f9635e06 RK |
2250 | int trackdb_get_global_tid(const char *name, |
2251 | DB_TXN *tid, | |
2252 | const char **rp) { | |
460b9539 | 2253 | DBT k, d; |
2254 | int err; | |
2255 | ||
2256 | memset(&k, 0, sizeof k); | |
2257 | k.data = (void *)name; | |
2258 | k.size = strlen(name); | |
2259 | switch(err = trackdb_globaldb->get(trackdb_globaldb, tid, &k, | |
2260 | prepare_data(&d), 0)) { | |
2261 | case 0: | |
2262 | *rp = xstrndup(d.data, d.size); | |
2263 | return 0; | |
2264 | case DB_NOTFOUND: | |
2265 | *rp = 0; | |
2266 | return 0; | |
2267 | case DB_LOCK_DEADLOCK: | |
2268 | return err; | |
2269 | default: | |
f9635e06 | 2270 | fatal(0, "error reading database: %s", db_strerror(err)); |
460b9539 | 2271 | } |
2272 | } | |
2273 | ||
2a10b70b RK |
2274 | /** @brief Retrieve the most recently added tracks |
2275 | * @param ntracksp Where to put count, or 0 | |
2276 | * @param maxtracks Maximum number of tracks to retrieve | |
2277 | * @return null-terminated array of track names | |
2278 | * | |
2279 | * The most recently added track is first in the array. | |
2280 | */ | |
2281 | char **trackdb_new(int *ntracksp, | |
2282 | int maxtracks) { | |
2283 | DB_TXN *tid; | |
2284 | char **tracks; | |
2285 | ||
2286 | for(;;) { | |
2287 | tid = trackdb_begin_transaction(); | |
2288 | tracks = trackdb_new_tid(ntracksp, maxtracks, tid); | |
2289 | if(tracks) | |
2290 | break; | |
2291 | trackdb_abort_transaction(tid); | |
2292 | } | |
2293 | trackdb_commit_transaction(tid); | |
2294 | return tracks; | |
2295 | } | |
2296 | ||
2297 | /** @brief Retrieve the most recently added tracks | |
2298 | * @param ntracksp Where to put count, or 0 | |
2299 | * @param maxtracks Maximum number of tracks to retrieve, or 0 for all | |
2300 | * @param tid Transaction ID | |
2301 | * @return null-terminated array of track names, or NULL on deadlock | |
2302 | * | |
2303 | * The most recently added track is first in the array. | |
2304 | */ | |
2305 | static char **trackdb_new_tid(int *ntracksp, | |
2306 | int maxtracks, | |
2307 | DB_TXN *tid) { | |
2308 | DBC *c; | |
2309 | DBT k, d; | |
2310 | int err = 0; | |
2311 | struct vector tracks[1]; | |
2312 | ||
2313 | vector_init(tracks); | |
2314 | c = trackdb_opencursor(trackdb_noticeddb, tid); | |
2315 | while((maxtracks <= 0 || tracks->nvec < maxtracks) | |
2316 | && !(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_PREV))) | |
2317 | vector_append(tracks, xstrndup(d.data, d.size)); | |
2318 | switch(err) { | |
2319 | case 0: /* hit maxtracks */ | |
2320 | case DB_NOTFOUND: /* ran out of tracks */ | |
2321 | break; | |
2322 | case DB_LOCK_DEADLOCK: | |
2323 | trackdb_closecursor(c); | |
2324 | return 0; | |
2325 | default: | |
2326 | fatal(0, "error reading noticed.db: %s", db_strerror(err)); | |
2327 | } | |
2328 | if((err = trackdb_closecursor(c))) | |
2329 | return 0; /* deadlock */ | |
2330 | vector_terminate(tracks); | |
2331 | if(ntracksp) | |
2332 | *ntracksp = tracks->nvec; | |
2333 | return tracks->vec; | |
2334 | } | |
2335 | ||
1e64e9fb RK |
2336 | /** @brief Expire noticed.db |
2337 | * @param earliest Earliest timestamp to keep | |
2338 | */ | |
2339 | void trackdb_expire_noticed(time_t earliest) { | |
2340 | DB_TXN *tid; | |
2341 | ||
2342 | for(;;) { | |
2343 | tid = trackdb_begin_transaction(); | |
2344 | if(!trackdb_expire_noticed_tid(earliest, tid)) | |
2345 | break; | |
2346 | trackdb_abort_transaction(tid); | |
2347 | } | |
2348 | trackdb_commit_transaction(tid); | |
2349 | } | |
2350 | ||
2351 | /** @brief Expire noticed.db | |
2352 | * @param earliest Earliest timestamp to keep | |
2353 | * @param tid Transaction ID | |
2354 | * @return 0 or DB_LOCK_DEADLOCK | |
2355 | */ | |
2356 | static int trackdb_expire_noticed_tid(time_t earliest, DB_TXN *tid) { | |
2357 | DBC *c; | |
2358 | DBT k, d; | |
2359 | int err = 0, ret; | |
2360 | time_t when; | |
2361 | uint32_t *kk; | |
2362 | int count = 0; | |
2363 | ||
2364 | c = trackdb_opencursor(trackdb_noticeddb, tid); | |
2365 | while(!(err = c->c_get(c, prepare_data(&k), prepare_data(&d), DB_NEXT))) { | |
2366 | kk = k.data; | |
2367 | when = (time_t)(((uint64_t)ntohl(kk[0]) << 32) + ntohl(kk[1])); | |
2368 | if(when >= earliest) | |
2369 | break; | |
2370 | if((err = c->c_del(c, 0))) { | |
2371 | if(err != DB_LOCK_DEADLOCK) | |
2372 | fatal(0, "error deleting expired noticed.db entry: %s", | |
2373 | db_strerror(err)); | |
2374 | break; | |
2375 | } | |
2376 | ++count; | |
2377 | } | |
2378 | if(err == DB_NOTFOUND) | |
2379 | err = 0; | |
2380 | if(err && err != DB_LOCK_DEADLOCK) | |
2381 | fatal(0, "error expiring noticed.db: %s", db_strerror(err)); | |
2382 | ret = err; | |
2383 | if((err = trackdb_closecursor(c))) { | |
2384 | if(err != DB_LOCK_DEADLOCK) | |
2385 | fatal(0, "error closing cursor: %s", db_strerror(err)); | |
2386 | ret = err; | |
2387 | } | |
2388 | if(!ret && count) | |
2389 | info("expired %d tracks from noticed.db", count); | |
2390 | return ret; | |
2391 | } | |
2392 | ||
460b9539 | 2393 | /* tidying up ****************************************************************/ |
2394 | ||
2395 | void trackdb_gc(void) { | |
2396 | int err; | |
2397 | char **logfiles; | |
2398 | ||
2399 | if((err = trackdb_env->txn_checkpoint(trackdb_env, | |
2400 | config->checkpoint_kbyte, | |
2401 | config->checkpoint_min, | |
2402 | 0))) | |
2403 | fatal(0, "trackdb_env->txn_checkpoint: %s", db_strerror(err)); | |
2404 | if((err = trackdb_env->log_archive(trackdb_env, &logfiles, DB_ARCH_REMOVE))) | |
2405 | fatal(0, "trackdb_env->log_archive: %s", db_strerror(err)); | |
2406 | /* This makes catastrophic recovery impossible. However, the user can still | |
2407 | * preserve the important data by using disorder-dump to snapshot their | |
2408 | * prefs, and later to restore it. This is likely to have much small | |
2409 | * long-term storage requirements than record the db logfiles. */ | |
2410 | } | |
2411 | ||
f0feb22e RK |
2412 | /* user database *************************************************************/ |
2413 | ||
2414 | /** @brief Return true if @p user is trusted */ | |
2415 | static int trusted(const char *user) { | |
2416 | int n; | |
2417 | ||
2418 | for(n = 0; (n < config->trust.n | |
2419 | && strcmp(config->trust.s[n], user)); ++n) | |
2420 | ; | |
2421 | return n < config->trust.n; | |
2422 | } | |
2423 | ||
ba39faf6 RK |
2424 | /** @brief Return non-zero for a valid username |
2425 | * | |
2426 | * Currently we only allow the letters and digits in ASCII. We could be more | |
2427 | * liberal than this but it is a nice simple test. It is critical that | |
2428 | * semicolons are never allowed. | |
2429 | */ | |
2430 | static int valid_username(const char *user) { | |
2431 | if(!*user) | |
2432 | return 0; | |
2433 | while(*user) { | |
2434 | const uint8_t c = *user++; | |
2435 | /* For now we are very strict */ | |
2436 | if((c >= 'a' && c <= 'z') | |
2437 | || (c >= 'A' && c <= 'Z') | |
2438 | || (c >= '0' && c <= '9')) | |
2439 | /* ok */; | |
2440 | else | |
2441 | return 0; | |
2442 | } | |
2443 | return 1; | |
2444 | } | |
2445 | ||
f0feb22e RK |
2446 | /** @brief Add a user */ |
2447 | static int create_user(const char *user, | |
2448 | const char *password, | |
2449 | const char *rights, | |
2450 | const char *email, | |
ba39faf6 | 2451 | const char *confirmation, |
f0feb22e RK |
2452 | DB_TXN *tid, |
2453 | uint32_t flags) { | |
2454 | struct kvp *k = 0; | |
2455 | char s[64]; | |
2456 | ||
ba39faf6 RK |
2457 | /* sanity check user */ |
2458 | if(!valid_username(user)) { | |
2459 | error(0, "invalid username '%s'", user); | |
2460 | return -1; | |
2461 | } | |
0f55e905 RK |
2462 | if(parse_rights(rights, 0, 1)) { |
2463 | error(0, "invalid rights string"); | |
2464 | return -1; | |
2465 | } | |
f0feb22e RK |
2466 | /* data for this user */ |
2467 | if(password) | |
2468 | kvp_set(&k, "password", password); | |
2469 | kvp_set(&k, "rights", rights); | |
2470 | if(email) | |
2471 | kvp_set(&k, "email", email); | |
ba39faf6 RK |
2472 | if(confirmation) |
2473 | kvp_set(&k, "confirmation", confirmation); | |
f0feb22e RK |
2474 | snprintf(s, sizeof s, "%jd", (intmax_t)time(0)); |
2475 | kvp_set(&k, "created", s); | |
2476 | return trackdb_putdata(trackdb_usersdb, user, k, tid, flags); | |
2477 | } | |
2478 | ||
2479 | /** @brief Add one pre-existing user */ | |
2480 | static int one_old_user(const char *user, const char *password, | |
2481 | DB_TXN *tid) { | |
2482 | const char *rights; | |
2483 | ||
2484 | /* www-data doesn't get added */ | |
2485 | if(!strcmp(user, "www-data")) { | |
2486 | info("not adding www-data to user database"); | |
2487 | return 0; | |
2488 | } | |
2489 | /* pick rights */ | |
2490 | if(!strcmp(user, "root")) | |
2491 | rights = "all"; | |
0f55e905 RK |
2492 | else if(trusted(user)) { |
2493 | rights_type r; | |
2494 | ||
2495 | parse_rights(config->default_rights, &r, 1); | |
fce810c2 | 2496 | r &= ~(rights_type)(RIGHT_SCRATCH__MASK|RIGHT_MOVE__MASK|RIGHT_REMOVE__MASK); |
0f55e905 RK |
2497 | r |= (RIGHT_ADMIN|RIGHT_RESCAN |
2498 | |RIGHT_SCRATCH_ANY|RIGHT_MOVE_ANY|RIGHT_REMOVE_ANY); | |
2499 | rights = rights_string(r); | |
2500 | } else | |
2501 | rights = config->default_rights; | |
ba39faf6 RK |
2502 | return create_user(user, password, rights, 0/*email*/, 0/*confirmation*/, |
2503 | tid, DB_NOOVERWRITE); | |
f0feb22e RK |
2504 | } |
2505 | ||
2506 | static int trackdb_old_users_tid(DB_TXN *tid) { | |
2507 | int n; | |
2508 | ||
2509 | for(n = 0; n < config->allow.n; ++n) { | |
2510 | switch(one_old_user(config->allow.s[n].s[0], config->allow.s[n].s[1], | |
2511 | tid)) { | |
2512 | case 0: | |
2513 | info("created user %s from 'allow' directive", config->allow.s[n].s[0]); | |
2514 | break; | |
2515 | case DB_KEYEXIST: | |
2516 | error(0, "user %s already exists, delete 'allow' directive", | |
2517 | config->allow.s[n].s[0]); | |
2518 | /* This won't ever become fatal - eventually 'allow' will be | |
2519 | * disabled. */ | |
2520 | break; | |
2521 | case DB_LOCK_DEADLOCK: | |
2522 | return DB_LOCK_DEADLOCK; | |
2523 | } | |
2524 | } | |
2525 | return 0; | |
2526 | } | |
2527 | ||
2528 | /** @brief Read old 'allow' directives and copy them to the users database */ | |
2529 | void trackdb_old_users(void) { | |
2530 | int e; | |
2531 | ||
2532 | if(config->allow.n) | |
2533 | WITH_TRANSACTION(trackdb_old_users_tid(tid)); | |
2534 | } | |
2535 | ||
2536 | /** @brief Create a root user in the user database if there is none */ | |
2537 | void trackdb_create_root(void) { | |
2538 | int e; | |
2539 | uint8_t pwbin[12]; | |
2540 | char *pw; | |
2541 | ||
2542 | /* Choose a new root password */ | |
2543 | gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM); | |
2544 | pw = mime_to_base64(pwbin, sizeof pwbin); | |
2545 | /* Create the root user if it does not exist */ | |
ba39faf6 RK |
2546 | WITH_TRANSACTION(create_user("root", pw, "all", |
2547 | 0/*email*/, 0/*confirmation*/, | |
2548 | tid, DB_NOOVERWRITE)); | |
f0feb22e RK |
2549 | if(e == 0) |
2550 | info("created root user"); | |
2551 | } | |
2552 | ||
2553 | /** @brief Find a user's password from the database | |
2554 | * @param user Username | |
2555 | * @return Password or NULL | |
2556 | * | |
2557 | * Only works if running as a user that can read the database! | |
2558 | * | |
2559 | * If the user exists but has no password, "" is returned. | |
2560 | */ | |
2561 | const char *trackdb_get_password(const char *user) { | |
2562 | int e; | |
2563 | struct kvp *k; | |
2564 | const char *password; | |
2565 | ||
2566 | WITH_TRANSACTION(trackdb_getdata(trackdb_usersdb, user, &k, tid)); | |
2567 | if(e) | |
2568 | return 0; | |
2569 | password = kvp_get(k, "password"); | |
2570 | return password ? password : ""; | |
2571 | } | |
2572 | ||
2573 | /** @brief Add a new user | |
2574 | * @param user Username | |
2575 | * @param password Password or NULL | |
2576 | * @param rights Initial rights | |
158d0961 | 2577 | * @param email Email address or NULL |
2578 | * @param confirmation Confirmation string or NULL | |
f0feb22e RK |
2579 | * @return 0 on success, non-0 on error |
2580 | */ | |
2581 | int trackdb_adduser(const char *user, | |
2582 | const char *password, | |
0f55e905 | 2583 | const char *rights, |
ba39faf6 RK |
2584 | const char *email, |
2585 | const char *confirmation) { | |
f0feb22e | 2586 | int e; |
f0feb22e | 2587 | |
0f55e905 | 2588 | WITH_TRANSACTION(create_user(user, password, rights, email, confirmation, |
f0feb22e RK |
2589 | tid, DB_NOOVERWRITE)); |
2590 | if(e) { | |
2591 | error(0, "cannot created user '%s' because they already exist", user); | |
2592 | return -1; | |
2593 | } else { | |
2594 | if(email) | |
2595 | info("created user '%s' with rights '%s' and email address '%s'", | |
0f55e905 | 2596 | user, rights, email); |
f0feb22e | 2597 | else |
0f55e905 | 2598 | info("created user '%s' with rights '%s'", user, rights); |
f0feb22e RK |
2599 | return 0; |
2600 | } | |
2601 | } | |
2602 | ||
2603 | /** @brief Delete a user | |
2604 | * @param user User to delete | |
158d0961 | 2605 | * @return 0 on success, non-0 if the user didn't exist anyway |
f0feb22e RK |
2606 | */ |
2607 | int trackdb_deluser(const char *user) { | |
2608 | int e; | |
2609 | ||
2610 | WITH_TRANSACTION(trackdb_delkey(trackdb_usersdb, user, tid)); | |
2611 | if(e) { | |
2612 | error(0, "cannot delete user '%s' because they do not exist", user); | |
2613 | return -1; | |
2614 | } | |
2615 | info("deleted user '%s'", user); | |
2616 | return 0; | |
2617 | } | |
2618 | ||
2619 | /** @brief Get user information | |
2620 | * @param user User to query | |
2621 | * @return Linked list of user information or NULL if user does not exist | |
2622 | * | |
2623 | * Every user has at least a @c rights entry so NULL can be used to mean no | |
2624 | * such user safely. | |
2625 | */ | |
2626 | struct kvp *trackdb_getuserinfo(const char *user) { | |
2627 | int e; | |
2628 | struct kvp *k; | |
2629 | ||
2630 | WITH_TRANSACTION(trackdb_getdata(trackdb_usersdb, user, &k, tid)); | |
2631 | if(e) | |
2632 | return 0; | |
2633 | else | |
2634 | return k; | |
2635 | } | |
2636 | ||
2637 | /** @brief Edit user information | |
2638 | * @param user User to edit | |
2639 | * @param key Key to change | |
2640 | * @param value Value to set, or NULL to remove | |
2641 | * @param tid Transaction ID | |
2642 | * @return 0, DB_LOCK_DEADLOCK or DB_NOTFOUND | |
2643 | */ | |
2644 | static int trackdb_edituserinfo_tid(const char *user, const char *key, | |
2645 | const char *value, DB_TXN *tid) { | |
2646 | struct kvp *k; | |
2647 | int e; | |
2648 | ||
2649 | if((e = trackdb_getdata(trackdb_usersdb, user, &k, tid))) | |
2650 | return e; | |
2651 | if(!kvp_set(&k, key, value)) | |
2652 | return 0; /* no change */ | |
2653 | return trackdb_putdata(trackdb_usersdb, user, k, tid, 0); | |
2654 | } | |
2655 | ||
2656 | /** @brief Edit user information | |
2657 | * @param user User to edit | |
2658 | * @param key Key to change | |
2659 | * @param value Value to set, or NULL to remove | |
2660 | * @return 0 on success, non-0 on error | |
2661 | */ | |
2662 | int trackdb_edituserinfo(const char *user, | |
2663 | const char *key, const char *value) { | |
2664 | int e; | |
2665 | ||
5df73aeb RK |
2666 | if(!strcmp(key, "rights")) { |
2667 | if(!value) { | |
2668 | error(0, "cannot remove 'rights' key from user '%s'", user); | |
2669 | return -1; | |
2670 | } | |
0f55e905 | 2671 | if(parse_rights(value, 0, 1)) { |
5df73aeb RK |
2672 | error(0, "invalid rights string"); |
2673 | return -1; | |
2674 | } | |
2675 | } else if(!strcmp(key, "email")) { | |
2676 | if(!strchr(value, '@')) { | |
2677 | error(0, "invalid email address '%s' for user '%s'", user, value); | |
2678 | return -1; | |
2679 | } | |
2680 | } else if(!strcmp(key, "created")) { | |
2681 | error(0, "cannot change creation date for user '%s'", user); | |
2682 | return -1; | |
2683 | } else if(strcmp(key, "password") | |
2684 | && !strcmp(key, "confirmation")) { | |
2685 | error(0, "unknown user info key '%s' for user '%s'", key, user); | |
2686 | return -1; | |
2687 | } | |
f0feb22e | 2688 | WITH_TRANSACTION(trackdb_edituserinfo_tid(user, key, value, tid)); |
5df73aeb RK |
2689 | if(e) { |
2690 | error(0, "unknown user '%s'", user); | |
f0feb22e | 2691 | return -1; |
5df73aeb | 2692 | } else |
f0feb22e RK |
2693 | return 0; |
2694 | } | |
2695 | ||
c3be4f19 RK |
2696 | /** @brief List all users |
2697 | * @return NULL-terminated list of users | |
2698 | */ | |
2699 | char **trackdb_listusers(void) { | |
2700 | int e; | |
2701 | struct vector v[1]; | |
2702 | ||
2703 | vector_init(v); | |
2704 | WITH_TRANSACTION(trackdb_listkeys(trackdb_usersdb, v, tid)); | |
2705 | return v->vec; | |
2706 | } | |
2707 | ||
30365519 | 2708 | /** @brief Confirm a user registration |
2709 | * @param user Username | |
2710 | * @param confirmation Confirmation string | |
2711 | * @param rightsp Where to put user rights | |
2712 | * @param tid Transaction ID | |
2713 | * @return 0 on success, non-0 on error | |
2714 | */ | |
ba39faf6 | 2715 | static int trackdb_confirm_tid(const char *user, const char *confirmation, |
30365519 | 2716 | rights_type *rightsp, |
ba39faf6 RK |
2717 | DB_TXN *tid) { |
2718 | const char *stored_confirmation; | |
2719 | struct kvp *k; | |
2720 | int e; | |
30365519 | 2721 | const char *rights; |
ba39faf6 RK |
2722 | |
2723 | if((e = trackdb_getdata(trackdb_usersdb, user, &k, tid))) | |
2724 | return e; | |
2725 | if(!(stored_confirmation = kvp_get(k, "confirmation"))) { | |
2726 | error(0, "already confirmed user '%s'", user); | |
2727 | /* DB claims -30,800 to -30,999 so -1 should be a safe bet */ | |
2728 | return -1; | |
2729 | } | |
30365519 | 2730 | if(!(rights = kvp_get(k, "rights"))) { |
2731 | error(0, "no rights for unconfirmed user '%s'", user); | |
2732 | return -1; | |
2733 | } | |
2734 | if(parse_rights(rights, rightsp, 1)) | |
2735 | return -1; | |
ba39faf6 RK |
2736 | if(strcmp(confirmation, stored_confirmation)) { |
2737 | error(0, "wrong confirmation string for user '%s'", user); | |
2738 | return -1; | |
2739 | } | |
2740 | /* 'sall good */ | |
2741 | kvp_set(&k, "confirmation", 0); | |
2742 | return trackdb_putdata(trackdb_usersdb, user, k, tid, 0); | |
2743 | } | |
2744 | ||
2745 | /** @brief Confirm a user registration | |
2746 | * @param user Username | |
2747 | * @param confirmation Confirmation string | |
30365519 | 2748 | * @param rightsp Where to put user rights |
ba39faf6 RK |
2749 | * @return 0 on success, non-0 on error |
2750 | */ | |
30365519 | 2751 | int trackdb_confirm(const char *user, const char *confirmation, |
2752 | rights_type *rightsp) { | |
ba39faf6 RK |
2753 | int e; |
2754 | ||
30365519 | 2755 | WITH_TRANSACTION(trackdb_confirm_tid(user, confirmation, rightsp, tid)); |
ba39faf6 RK |
2756 | switch(e) { |
2757 | case 0: | |
2758 | info("registration confirmed for user '%s'", user); | |
2759 | return 0; | |
2760 | case DB_NOTFOUND: | |
2761 | error(0, "confirmation for nonexistent user '%s'", user); | |
2762 | return -1; | |
2763 | default: /* already reported */ | |
2764 | return -1; | |
2765 | } | |
2766 | } | |
2767 | ||
460b9539 | 2768 | /* |
2769 | Local Variables: | |
2770 | c-basic-offset:2 | |
2771 | comment-column:40 | |
2772 | fill-column:79 | |
2773 | indent-tabs-mode:nil | |
2774 | End: | |
2775 | */ |