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