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