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