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