chiark / gitweb /
debian/control: db4.8 is obsolete in wheezy.
[disorder] / server / rescan.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
8a886602 3 * Copyright (C) 2005-2011 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file server/rescan.c
19 * @brief Rescanning utility
20 *
21 * Invoked by the server.
22 */
05b75f8d 23#include "disorder-server.h"
460b9539 24
807d2644 25static time_t last_report;
460b9539 26static DB_TXN *global_tid;
27
28static const struct option options[] = {
29 { "help", no_argument, 0, 'h' },
30 { "version", no_argument, 0, 'V' },
31 { "config", required_argument, 0, 'c' },
32 { "debug", no_argument, 0, 'd' },
33 { "no-debug", no_argument, 0, 'D' },
0ca6d097
RK
34 { "syslog", no_argument, 0, 's' },
35 { "no-syslog", no_argument, 0, 'S' },
ffac51d7
RK
36 { "check", no_argument, 0, 'K' },
37 { "no-check", no_argument, 0, 'C' },
460b9539 38 { 0, 0, 0, 0 }
39};
40
41/* display usage message and terminate */
42static void help(void) {
43 xprintf("Usage:\n"
44 " disorder-rescan [OPTIONS] [PATH...]\n"
45 "Options:\n"
46 " --help, -h Display usage message\n"
47 " --version, -V Display version number\n"
48 " --config PATH, -c PATH Set configuration file\n"
49 " --debug, -d Turn on debugging\n"
ffac51d7
RK
50 " --[no-]syslog Enable/disable logging to syslog\n"
51 " --[no-]check Enable/disable track length check\n"
460b9539 52 "\n"
53 "Rescanner for DisOrder. Not intended to be run\n"
54 "directly.\n");
55 xfclose(stdout);
56 exit(0);
57}
58
460b9539 59static volatile sig_atomic_t signalled;
60
61static void signal_handler(int sig) {
62 if(sig == 0) _exit(-1); /* "Cannot happen" */
63 signalled = sig;
64}
65
66static int aborted(void) {
67 return signalled || getppid() == 1;
68}
69
70/* Exit if our parent has gone away or we have been told to stop. */
71static void checkabort(void) {
72 if(getppid() == 1) {
2e9ba080 73 disorder_info("parent has terminated");
460b9539 74 trackdb_abort_transaction(global_tid);
75 exit(0);
76 }
77 if(signalled) {
2e9ba080 78 disorder_info("received signal %d", signalled);
460b9539 79 trackdb_abort_transaction(global_tid);
80 exit(0);
81 }
82}
83
84/* rescan a collection */
85static void rescan_collection(const struct collection *c) {
86 pid_t pid, r;
87 int p[2], n, w;
88 FILE *fp = 0;
89 char *path, *track;
90 long ntracks = 0, nnew = 0;
91
92 checkabort();
2e9ba080 93 disorder_info("rescanning %s with %s", c->root, c->module);
460b9539 94 /* plugin runs in a subprocess */
95 xpipe(p);
96 if(!(pid = xfork())) {
97 exitfn = _exit;
98 xclose(p[0]);
99 xdup2(p[1], 1);
100 xclose(p[1]);
101 scan(c->module, c->root);
102 if(fflush(stdout) < 0)
2e9ba080 103 disorder_fatal(errno, "error writing to scanner pipe");
460b9539 104 _exit(0);
105 }
106 xclose(p[1]);
107 if(!(fp = fdopen(p[0], "r")))
2e9ba080 108 disorder_fatal(errno, "error calling fdopen");
460b9539 109 /* read tracks from the plugin */
110 while(!inputline("rescanner", fp, &path, 0)) {
111 checkabort();
112 /* actually we can cope relatively well within the server, but they'll go
113 * wrong in track listings */
114 if(strchr(path, '\n')) {
2e9ba080 115 disorder_error(0, "cannot cope with tracks with newlines in the name");
460b9539 116 continue;
117 }
118 if(!(track = any2utf8(c->encoding, path))) {
2e9ba080 119 disorder_error(0, "cannot convert track path to UTF-8: %s", path);
460b9539 120 continue;
121 }
8818b7fc
RK
122 if(config->dbversion > 1) {
123 /* We use NFC track names */
124 if(!(track = utf8_compose_canon(track, strlen(track), 0))) {
2e9ba080 125 disorder_error(0, "cannot convert track path to NFC: %s", path);
8818b7fc
RK
126 continue;
127 }
f9635e06 128 }
460b9539 129 D(("track %s", track));
130 /* only tracks with a known player are admitted */
131 for(n = 0; (n < config->player.n
132 && fnmatch(config->player.s[n].s[0], track, 0) != 0); ++n)
133 ;
134 if(n < config->player.n) {
135 nnew += !!trackdb_notice(track, path);
136 ++ntracks;
4265e5d3 137 if(ntracks % 100 == 0 && xtime(0) > last_report + 10) {
2e9ba080 138 disorder_info("rescanning %s, %ld tracks so far", c->root, ntracks);
4265e5d3 139 xtime(&last_report);
807d2644 140 }
460b9539 141 }
142 }
143 /* tidy up */
144 if(ferror(fp)) {
2e9ba080 145 disorder_error(errno, "error reading from scanner pipe");
460b9539 146 goto done;
147 }
148 xfclose(fp);
149 fp = 0;
150 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
151 ;
2e9ba080 152 if(r < 0) disorder_fatal(errno, "error calling waitpid");
460b9539 153 pid = 0;
154 if(w) {
2e9ba080 155 disorder_error(0, "scanner subprocess: %s", wstat(w));
460b9539 156 goto done;
157 }
2e9ba080 158 disorder_info("rescanned %s, %ld tracks, %ld new", c->root, ntracks, nnew);
460b9539 159done:
160 if(fp)
161 xfclose(fp);
162 if(pid)
b3756e27 163 while((waitpid(pid, &w, 0)) == -1 && errno == EINTR)
460b9539 164 ;
165}
166
2c6ee627 167/** @brief State for the recheck phase of the rescan */
460b9539 168struct recheck_state {
2c6ee627 169 /** @brief Collection being rechecked */
460b9539 170 const struct collection *c;
2c6ee627
RK
171
172 /** @brief Number of tracks obsoleted */
173 long nobsolete;
174
175 /** @brief Number of tracks belonging to no collection */
176 long nnocollection;
177
178 /** @brief Number of lengths computed */
179 long nlength;
180
181 /** @brief Linked list of tracks to recheck */
ffac51d7
RK
182 struct recheck_track *tracks;
183};
184
2c6ee627
RK
185/** @brief A track to recheck
186 *
187 * A node in a linked list.
188 */
ffac51d7 189struct recheck_track {
2c6ee627 190 /** @brief Next track */
ffac51d7 191 struct recheck_track *next;
2c6ee627
RK
192
193 /** @brief Track */
ffac51d7 194 const char *track;
460b9539 195};
196
197/* called for each non-alias track */
ffac51d7
RK
198static int recheck_list_callback(const char *track,
199 struct kvp attribute((unused)) *data,
bea6f6d5 200 struct kvp attribute((unused)) *prefs,
ffac51d7
RK
201 void *u,
202 DB_TXN attribute((unused)) *tid) {
460b9539 203 struct recheck_state *cs = u;
ffac51d7
RK
204 struct recheck_track *t = xmalloc(sizeof *t);
205
206 t->next = cs->tracks;
207 t->track = track;
208 cs->tracks = t;
209 return 0;
210}
211
212static int recheck_track_tid(struct recheck_state *cs,
213 const struct recheck_track *t,
214 DB_TXN *tid) {
460b9539 215 const struct collection *c = cs->c;
ffac51d7 216 const char *path;
460b9539 217 char buffer[20];
62dc3748
RK
218 int err, n;
219 long length;
ffac51d7 220 struct kvp *data;
460b9539 221
ffac51d7
RK
222 if((err = trackdb_getdata(trackdb_tracksdb, t->track, &data, tid)))
223 return err;
224 path = kvp_get(data, "_path");
225 D(("rechecking %s", t->track));
d1694464 226 /* if we're not checking a specific collection, find the right collection */
227 if(!c) {
ffac51d7
RK
228 if(!(c = find_track_collection(t->track))) {
229 D(("obsoleting %s", t->track));
230 if((err = trackdb_obsolete(t->track, tid)))
231 return err;
d1694464 232 ++cs->nnocollection;
233 return 0;
234 }
235 }
c8a277f4
MW
236 /* see if the track has evaporated or no longer has a player */
237 for(n = 0; (n < config->player.n
238 && fnmatch(config->player.s[n].s[0], t->track, 0) != 0); ++n)
239 ;
240 if(n >= config->player.n || check(c->module, c->root, path) == 0) {
ffac51d7
RK
241 D(("obsoleting %s", t->track));
242 if((err = trackdb_obsolete(t->track, tid)))
243 return err;
460b9539 244 ++cs->nobsolete;
245 return 0;
246 }
247 /* make sure we know the length */
248 if(!kvp_get(data, "_length")) {
ffac51d7 249 D(("recalculating length of %s", t->track));
62dc3748 250 for(n = 0; n < config->tracklength.n; ++n)
ffac51d7 251 if(fnmatch(config->tracklength.s[n].s[0], t->track, 0) == 0)
62dc3748
RK
252 break;
253 if(n >= config->tracklength.n)
2e9ba080 254 disorder_error(0, "no tracklength plugin found for %s", t->track);
62dc3748 255 else {
ffac51d7 256 length = tracklength(config->tracklength.s[n].s[1], t->track, path);
62dc3748
RK
257 if(length > 0) {
258 byte_snprintf(buffer, sizeof buffer, "%ld", length);
259 kvp_set(&data, "_length", buffer);
ffac51d7 260 if((err = trackdb_putdata(trackdb_tracksdb, t->track, data, tid, 0)))
62dc3748
RK
261 return err;
262 ++cs->nlength;
263 }
460b9539 264 }
265 }
266 return 0;
267}
268
ffac51d7
RK
269static int recheck_track(struct recheck_state *cs,
270 const struct recheck_track *t) {
271 int e;
272
273 WITH_TRANSACTION(recheck_track_tid(cs, t, tid));
274 return e;
275}
276
460b9539 277/* recheck a collection */
278static void recheck_collection(const struct collection *c) {
279 struct recheck_state cs;
ffac51d7
RK
280 const struct recheck_track *t;
281 long nrc;
460b9539 282
d1694464 283 if(c)
2e9ba080 284 disorder_info("rechecking %s", c->root);
d1694464 285 else
2e9ba080 286 disorder_info("rechecking all tracks");
ffac51d7
RK
287 /* Doing the checking inside a transaction locks up the server for much too
288 * long (because it spends lots of time thinking about each track). So we
289 * pull the full track list into memory and work from that.
290 *
291 * 100,000 tracks at, say, 80 bytes per track name, gives 8MB, which is quite
292 * reasonable.
293 */
460b9539 294 for(;;) {
295 checkabort();
2e9ba080 296 disorder_info("getting track list");
460b9539 297 global_tid = trackdb_begin_transaction();
298 memset(&cs, 0, sizeof cs);
299 cs.c = c;
ffac51d7 300 if(trackdb_scan(c ? c->root : 0, recheck_list_callback, &cs, global_tid))
d1694464 301 goto fail;
460b9539 302 break;
303 fail:
304 /* Maybe we need to shut down */
305 checkabort();
306 /* Abort the transaction and try again in a bit. */
307 trackdb_abort_transaction(global_tid);
308 global_tid = 0;
309 /* Let anything else that is going on get out of the way. */
310 sleep(10);
311 checkabort();
d1694464 312 if(c)
2e9ba080 313 disorder_info("resuming recheck of %s", c->root);
d1694464 314 else
2e9ba080 315 disorder_info("resuming global recheck");
460b9539 316 }
317 trackdb_commit_transaction(global_tid);
318 global_tid = 0;
ffac51d7
RK
319 nrc = 0;
320 for(t = cs.tracks; t; t = t->next) {
321 if(aborted())
322 return;
323 recheck_track(&cs, t);
324 ++nrc;
4265e5d3 325 if(nrc % 100 == 0 && xtime(0) > last_report + 10) {
ffac51d7 326 if(c)
2e9ba080 327 disorder_info("rechecking %s, %ld tracks so far", c->root, nrc);
ffac51d7 328 else
2e9ba080 329 disorder_info("rechecking all tracks, %ld tracks so far", nrc);
4265e5d3 330 xtime(&last_report);
ffac51d7
RK
331 }
332 }
d1694464 333 if(c)
2e9ba080
RK
334 disorder_info("rechecked %s, %ld obsoleted, %ld lengths calculated",
335 c->root, cs.nobsolete, cs.nlength);
d1694464 336 else
2e9ba080 337 disorder_info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
d1694464 338 cs.nnocollection, cs.nobsolete, cs.nlength);
460b9539 339}
340
341/* rescan/recheck a collection by name */
342static void do_directory(const char *s,
343 void (*fn)(const struct collection *c)) {
344 int n;
345
346 for(n = 0; (n < config->collection.n
347 && strcmp(config->collection.s[n].root, s)); ++n)
348 ;
349 if(n < config->collection.n)
350 fn(&config->collection.s[n]);
351 else
2e9ba080 352 disorder_error(0, "no collection has root '%s'", s);
460b9539 353}
354
355/* rescan/recheck all collections */
356static void do_all(void (*fn)(const struct collection *c)) {
357 int n;
358
359 for(n = 0; n < config->collection.n; ++n)
360 fn(&config->collection.s[n]);
6aba3f6c
RK
361 /* TODO: we need to tidy up tracks from collections now removed. We could do
362 * this two ways: either remember collections we think there are and spot
363 * their disappearance, or iterate over all tracks and gc any that don't fit
364 * into some collection.
365 *
366 * Having a way to rename collections would be rather convenient too but
367 * that's another kettle of monkeys.
368 */
460b9539 369}
370
2a10b70b
RK
371/** @brief Expire noticed.db */
372static void expire_noticed(void) {
1e64e9fb
RK
373 time_t now;
374
4265e5d3 375 xtime(&now);
1e64e9fb 376 trackdb_expire_noticed(now - config->noticed_history * 86400);
2a10b70b
RK
377}
378
460b9539 379int main(int argc, char **argv) {
0ca6d097 380 int n, logsyslog = !isatty(2);
460b9539 381 struct sigaction sa;
ffac51d7 382 int do_check = 1;
460b9539 383
384 set_progname(argv);
320598d4 385 mem_init();
2e9ba080 386 if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
ffac51d7 387 while((n = getopt_long(argc, argv, "hVc:dDSsKC", options, 0)) >= 0) {
460b9539 388 switch(n) {
389 case 'h': help();
3fbdc96d 390 case 'V': version("disorder-rescan");
460b9539 391 case 'c': configfile = optarg; break;
392 case 'd': debugging = 1; break;
393 case 'D': debugging = 0; break;
0ca6d097
RK
394 case 'S': logsyslog = 0; break;
395 case 's': logsyslog = 1; break;
ffac51d7
RK
396 case 'K': do_check = 1; break;
397 case 'C': do_check = 0; break;
2e9ba080 398 default: disorder_fatal(0, "invalid option");
460b9539 399 }
400 }
5464a25a 401 if(logsyslog) {
460b9539 402 openlog(progname, LOG_PID, LOG_DAEMON);
403 log_default = &log_syslog;
404 }
2e9ba080 405 if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
460b9539 406 xnice(config->nice_rescan);
407 sa.sa_handler = signal_handler;
408 sa.sa_flags = SA_RESTART;
409 sigemptyset(&sa.sa_mask);
410 xsigaction(SIGTERM, &sa, 0);
411 xsigaction(SIGINT, &sa, 0);
2e9ba080 412 disorder_info("started");
d25c4615
RK
413 trackdb_init(TRACKDB_NO_RECOVER);
414 trackdb_open(TRACKDB_NO_UPGRADE);
460b9539 415 if(optind == argc) {
d1694464 416 /* Rescan all collections */
460b9539 417 do_all(rescan_collection);
d1694464 418 /* Check that every track still exists */
ffac51d7
RK
419 if(do_check)
420 recheck_collection(0);
2a10b70b
RK
421 /* Expire noticed.db */
422 expire_noticed();
460b9539 423 }
424 else {
d1694464 425 /* Rescan specified collections */
460b9539 426 for(n = optind; n < argc; ++n)
427 do_directory(argv[n], rescan_collection);
d1694464 428 /* Check specified collections for tracks that have gone */
ffac51d7
RK
429 if(do_check)
430 for(n = optind; n < argc; ++n)
431 do_directory(argv[n], recheck_collection);
460b9539 432 }
433 trackdb_close();
8cd7d4bc 434 trackdb_deinit(NULL);
2e9ba080 435 disorder_info("completed");
460b9539 436 return 0;
437}
438
439/*
440Local Variables:
441c-basic-offset:2
442comment-column:40
443fill-column:79
444indent-tabs-mode:nil
445End:
446*/