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