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