Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
4363757e | 3 | * Copyright (C) 2005-2008 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 | 25 | static time_t last_report; |
460b9539 | 26 | static DB_TXN *global_tid; |
27 | ||
28 | static 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 */ | |
42 | static 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 | 59 | static volatile sig_atomic_t signalled; |
60 | ||
61 | static void signal_handler(int sig) { | |
62 | if(sig == 0) _exit(-1); /* "Cannot happen" */ | |
63 | signalled = sig; | |
64 | } | |
65 | ||
66 | static 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. */ | |
71 | static void checkabort(void) { | |
72 | if(getppid() == 1) { | |
73 | info("parent has terminated"); | |
74 | trackdb_abort_transaction(global_tid); | |
75 | exit(0); | |
76 | } | |
77 | if(signalled) { | |
78 | info("received signal %d", signalled); | |
79 | trackdb_abort_transaction(global_tid); | |
80 | exit(0); | |
81 | } | |
82 | } | |
83 | ||
84 | /* rescan a collection */ | |
85 | static 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(); | |
93 | info("rescanning %s with %s", c->root, c->module); | |
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) | |
103 | fatal(errno, "error writing to scanner pipe"); | |
104 | _exit(0); | |
105 | } | |
106 | xclose(p[1]); | |
107 | if(!(fp = fdopen(p[0], "r"))) | |
108 | fatal(errno, "error calling fdopen"); | |
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')) { | |
115 | error(0, "cannot cope with tracks with newlines in the name"); | |
116 | continue; | |
117 | } | |
118 | if(!(track = any2utf8(c->encoding, path))) { | |
119 | error(0, "cannot convert track path to UTF-8: %s", path); | |
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))) { | |
125 | error(0, "cannot convert track path to NFC: %s", path); | |
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; | |
807d2644 | 137 | if(ntracks % 100 == 0 && time(0) > last_report + 10) { |
e4ba53fd | 138 | info("rescanning %s, %ld tracks so far", c->root, ntracks); |
807d2644 RK |
139 | time(&last_report); |
140 | } | |
460b9539 | 141 | } |
142 | } | |
143 | /* tidy up */ | |
144 | if(ferror(fp)) { | |
145 | error(errno, "error reading from scanner pipe"); | |
146 | goto done; | |
147 | } | |
148 | xfclose(fp); | |
149 | fp = 0; | |
150 | while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR) | |
151 | ; | |
152 | if(r < 0) fatal(errno, "error calling waitpid"); | |
153 | pid = 0; | |
154 | if(w) { | |
155 | error(0, "scanner subprocess: %s", wstat(w)); | |
156 | goto done; | |
157 | } | |
158 | info("rescanned %s, %ld tracks, %ld new", c->root, ntracks, nnew); | |
159 | done: | |
160 | if(fp) | |
161 | xfclose(fp); | |
162 | if(pid) | |
163 | while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR) | |
164 | ; | |
165 | } | |
166 | ||
167 | struct recheck_state { | |
168 | const struct collection *c; | |
d1694464 | 169 | long nobsolete, nnocollection, nlength; |
ffac51d7 RK |
170 | struct recheck_track *tracks; |
171 | }; | |
172 | ||
173 | struct recheck_track { | |
174 | struct recheck_track *next; | |
175 | const char *track; | |
460b9539 | 176 | }; |
177 | ||
178 | /* called for each non-alias track */ | |
ffac51d7 RK |
179 | static int recheck_list_callback(const char *track, |
180 | struct kvp attribute((unused)) *data, | |
bea6f6d5 | 181 | struct kvp attribute((unused)) *prefs, |
ffac51d7 RK |
182 | void *u, |
183 | DB_TXN attribute((unused)) *tid) { | |
460b9539 | 184 | struct recheck_state *cs = u; |
ffac51d7 RK |
185 | struct recheck_track *t = xmalloc(sizeof *t); |
186 | ||
187 | t->next = cs->tracks; | |
188 | t->track = track; | |
189 | cs->tracks = t; | |
190 | return 0; | |
191 | } | |
192 | ||
193 | static int recheck_track_tid(struct recheck_state *cs, | |
194 | const struct recheck_track *t, | |
195 | DB_TXN *tid) { | |
460b9539 | 196 | const struct collection *c = cs->c; |
ffac51d7 | 197 | const char *path; |
460b9539 | 198 | char buffer[20]; |
62dc3748 RK |
199 | int err, n; |
200 | long length; | |
ffac51d7 | 201 | struct kvp *data; |
460b9539 | 202 | |
ffac51d7 RK |
203 | if((err = trackdb_getdata(trackdb_tracksdb, t->track, &data, tid))) |
204 | return err; | |
205 | path = kvp_get(data, "_path"); | |
206 | D(("rechecking %s", t->track)); | |
d1694464 | 207 | /* if we're not checking a specific collection, find the right collection */ |
208 | if(!c) { | |
ffac51d7 RK |
209 | if(!(c = find_track_collection(t->track))) { |
210 | D(("obsoleting %s", t->track)); | |
211 | if((err = trackdb_obsolete(t->track, tid))) | |
212 | return err; | |
d1694464 | 213 | ++cs->nnocollection; |
214 | return 0; | |
215 | } | |
216 | } | |
460b9539 | 217 | /* see if the track has evaporated */ |
218 | if(check(c->module, c->root, path) == 0) { | |
ffac51d7 RK |
219 | D(("obsoleting %s", t->track)); |
220 | if((err = trackdb_obsolete(t->track, tid))) | |
221 | return err; | |
460b9539 | 222 | ++cs->nobsolete; |
223 | return 0; | |
224 | } | |
225 | /* make sure we know the length */ | |
226 | if(!kvp_get(data, "_length")) { | |
ffac51d7 | 227 | D(("recalculating length of %s", t->track)); |
62dc3748 | 228 | for(n = 0; n < config->tracklength.n; ++n) |
ffac51d7 | 229 | if(fnmatch(config->tracklength.s[n].s[0], t->track, 0) == 0) |
62dc3748 RK |
230 | break; |
231 | if(n >= config->tracklength.n) | |
ffac51d7 | 232 | error(0, "no tracklength plugin found for %s", t->track); |
62dc3748 | 233 | else { |
ffac51d7 | 234 | length = tracklength(config->tracklength.s[n].s[1], t->track, path); |
62dc3748 RK |
235 | if(length > 0) { |
236 | byte_snprintf(buffer, sizeof buffer, "%ld", length); | |
237 | kvp_set(&data, "_length", buffer); | |
ffac51d7 | 238 | if((err = trackdb_putdata(trackdb_tracksdb, t->track, data, tid, 0))) |
62dc3748 RK |
239 | return err; |
240 | ++cs->nlength; | |
241 | } | |
460b9539 | 242 | } |
243 | } | |
244 | return 0; | |
245 | } | |
246 | ||
ffac51d7 RK |
247 | static int recheck_track(struct recheck_state *cs, |
248 | const struct recheck_track *t) { | |
249 | int e; | |
250 | ||
251 | WITH_TRANSACTION(recheck_track_tid(cs, t, tid)); | |
252 | return e; | |
253 | } | |
254 | ||
460b9539 | 255 | /* recheck a collection */ |
256 | static void recheck_collection(const struct collection *c) { | |
257 | struct recheck_state cs; | |
ffac51d7 RK |
258 | const struct recheck_track *t; |
259 | long nrc; | |
460b9539 | 260 | |
d1694464 | 261 | if(c) |
262 | info("rechecking %s", c->root); | |
263 | else | |
264 | info("rechecking all tracks"); | |
ffac51d7 RK |
265 | /* Doing the checking inside a transaction locks up the server for much too |
266 | * long (because it spends lots of time thinking about each track). So we | |
267 | * pull the full track list into memory and work from that. | |
268 | * | |
269 | * 100,000 tracks at, say, 80 bytes per track name, gives 8MB, which is quite | |
270 | * reasonable. | |
271 | */ | |
460b9539 | 272 | for(;;) { |
273 | checkabort(); | |
ffac51d7 | 274 | info("getting track list"); |
460b9539 | 275 | global_tid = trackdb_begin_transaction(); |
276 | memset(&cs, 0, sizeof cs); | |
277 | cs.c = c; | |
ffac51d7 | 278 | if(trackdb_scan(c ? c->root : 0, recheck_list_callback, &cs, global_tid)) |
d1694464 | 279 | goto fail; |
460b9539 | 280 | break; |
281 | fail: | |
282 | /* Maybe we need to shut down */ | |
283 | checkabort(); | |
284 | /* Abort the transaction and try again in a bit. */ | |
285 | trackdb_abort_transaction(global_tid); | |
286 | global_tid = 0; | |
287 | /* Let anything else that is going on get out of the way. */ | |
288 | sleep(10); | |
289 | checkabort(); | |
d1694464 | 290 | if(c) |
291 | info("resuming recheck of %s", c->root); | |
292 | else | |
293 | info("resuming global recheck"); | |
460b9539 | 294 | } |
295 | trackdb_commit_transaction(global_tid); | |
296 | global_tid = 0; | |
ffac51d7 RK |
297 | nrc = 0; |
298 | for(t = cs.tracks; t; t = t->next) { | |
299 | if(aborted()) | |
300 | return; | |
301 | recheck_track(&cs, t); | |
302 | ++nrc; | |
807d2644 | 303 | if(nrc % 100 == 0 && time(0) > last_report + 10) { |
ffac51d7 RK |
304 | if(c) |
305 | info("rechecking %s, %ld tracks so far", c->root, nrc); | |
306 | else | |
307 | info("rechecking all tracks, %ld tracks so far", nrc); | |
807d2644 | 308 | time(&last_report); |
ffac51d7 RK |
309 | } |
310 | } | |
d1694464 | 311 | if(c) |
312 | info("rechecked %s, %ld obsoleted, %ld lengths calculated", | |
313 | c->root, cs.nobsolete, cs.nlength); | |
314 | else | |
315 | info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated", | |
316 | cs.nnocollection, cs.nobsolete, cs.nlength); | |
460b9539 | 317 | } |
318 | ||
319 | /* rescan/recheck a collection by name */ | |
320 | static void do_directory(const char *s, | |
321 | void (*fn)(const struct collection *c)) { | |
322 | int n; | |
323 | ||
324 | for(n = 0; (n < config->collection.n | |
325 | && strcmp(config->collection.s[n].root, s)); ++n) | |
326 | ; | |
327 | if(n < config->collection.n) | |
328 | fn(&config->collection.s[n]); | |
329 | else | |
330 | error(0, "no collection has root '%s'", s); | |
331 | } | |
332 | ||
333 | /* rescan/recheck all collections */ | |
334 | static void do_all(void (*fn)(const struct collection *c)) { | |
335 | int n; | |
336 | ||
337 | for(n = 0; n < config->collection.n; ++n) | |
338 | fn(&config->collection.s[n]); | |
6aba3f6c RK |
339 | /* TODO: we need to tidy up tracks from collections now removed. We could do |
340 | * this two ways: either remember collections we think there are and spot | |
341 | * their disappearance, or iterate over all tracks and gc any that don't fit | |
342 | * into some collection. | |
343 | * | |
344 | * Having a way to rename collections would be rather convenient too but | |
345 | * that's another kettle of monkeys. | |
346 | */ | |
460b9539 | 347 | } |
348 | ||
2a10b70b RK |
349 | /** @brief Expire noticed.db */ |
350 | static void expire_noticed(void) { | |
1e64e9fb RK |
351 | time_t now; |
352 | ||
353 | time(&now); | |
354 | trackdb_expire_noticed(now - config->noticed_history * 86400); | |
2a10b70b RK |
355 | } |
356 | ||
460b9539 | 357 | int main(int argc, char **argv) { |
0ca6d097 | 358 | int n, logsyslog = !isatty(2); |
460b9539 | 359 | struct sigaction sa; |
ffac51d7 | 360 | int do_check = 1; |
460b9539 | 361 | |
362 | set_progname(argv); | |
320598d4 | 363 | mem_init(); |
460b9539 | 364 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
ffac51d7 | 365 | while((n = getopt_long(argc, argv, "hVc:dDSsKC", options, 0)) >= 0) { |
460b9539 | 366 | switch(n) { |
367 | case 'h': help(); | |
3fbdc96d | 368 | case 'V': version("disorder-rescan"); |
460b9539 | 369 | case 'c': configfile = optarg; break; |
370 | case 'd': debugging = 1; break; | |
371 | case 'D': debugging = 0; break; | |
0ca6d097 RK |
372 | case 'S': logsyslog = 0; break; |
373 | case 's': logsyslog = 1; break; | |
ffac51d7 RK |
374 | case 'K': do_check = 1; break; |
375 | case 'C': do_check = 0; break; | |
460b9539 | 376 | default: fatal(0, "invalid option"); |
377 | } | |
378 | } | |
5464a25a | 379 | if(logsyslog) { |
460b9539 | 380 | openlog(progname, LOG_PID, LOG_DAEMON); |
381 | log_default = &log_syslog; | |
382 | } | |
c00fce3a | 383 | if(config_read(0)) fatal(0, "cannot read configuration"); |
460b9539 | 384 | xnice(config->nice_rescan); |
385 | sa.sa_handler = signal_handler; | |
386 | sa.sa_flags = SA_RESTART; | |
387 | sigemptyset(&sa.sa_mask); | |
388 | xsigaction(SIGTERM, &sa, 0); | |
389 | xsigaction(SIGINT, &sa, 0); | |
390 | info("started"); | |
d25c4615 RK |
391 | trackdb_init(TRACKDB_NO_RECOVER); |
392 | trackdb_open(TRACKDB_NO_UPGRADE); | |
460b9539 | 393 | if(optind == argc) { |
d1694464 | 394 | /* Rescan all collections */ |
460b9539 | 395 | do_all(rescan_collection); |
d1694464 | 396 | /* Check that every track still exists */ |
ffac51d7 RK |
397 | if(do_check) |
398 | recheck_collection(0); | |
2a10b70b RK |
399 | /* Expire noticed.db */ |
400 | expire_noticed(); | |
460b9539 | 401 | } |
402 | else { | |
d1694464 | 403 | /* Rescan specified collections */ |
460b9539 | 404 | for(n = optind; n < argc; ++n) |
405 | do_directory(argv[n], rescan_collection); | |
d1694464 | 406 | /* Check specified collections for tracks that have gone */ |
ffac51d7 RK |
407 | if(do_check) |
408 | for(n = optind; n < argc; ++n) | |
409 | do_directory(argv[n], recheck_collection); | |
460b9539 | 410 | } |
411 | trackdb_close(); | |
412 | trackdb_deinit(); | |
413 | info("completed"); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | /* | |
418 | Local Variables: | |
419 | c-basic-offset:2 | |
420 | comment-column:40 | |
421 | fill-column:79 | |
422 | indent-tabs-mode:nil | |
423 | End: | |
424 | */ |