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" |
f9635e06 | 53 | #include "unicode.h" |
460b9539 | 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' }, | |
0ca6d097 RK |
63 | { "syslog", no_argument, 0, 's' }, |
64 | { "no-syslog", no_argument, 0, 'S' }, | |
460b9539 | 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" | |
0ca6d097 | 77 | " --[no-]syslog Force logging\n" |
460b9539 | 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 | } | |
8818b7fc RK |
155 | if(config->dbversion > 1) { |
156 | /* We use NFC track names */ | |
157 | if(!(track = utf8_compose_canon(track, strlen(track), 0))) { | |
158 | error(0, "cannot convert track path to NFC: %s", path); | |
159 | continue; | |
160 | } | |
f9635e06 | 161 | } |
460b9539 | 162 | D(("track %s", track)); |
163 | /* only tracks with a known player are admitted */ | |
164 | for(n = 0; (n < config->player.n | |
165 | && fnmatch(config->player.s[n].s[0], track, 0) != 0); ++n) | |
166 | ; | |
167 | if(n < config->player.n) { | |
168 | nnew += !!trackdb_notice(track, path); | |
169 | ++ntracks; | |
e4ba53fd RK |
170 | if(ntracks % 1000 == 0) |
171 | info("rescanning %s, %ld tracks so far", c->root, ntracks); | |
460b9539 | 172 | } |
173 | } | |
174 | /* tidy up */ | |
175 | if(ferror(fp)) { | |
176 | error(errno, "error reading from scanner pipe"); | |
177 | goto done; | |
178 | } | |
179 | xfclose(fp); | |
180 | fp = 0; | |
181 | while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR) | |
182 | ; | |
183 | if(r < 0) fatal(errno, "error calling waitpid"); | |
184 | pid = 0; | |
185 | if(w) { | |
186 | error(0, "scanner subprocess: %s", wstat(w)); | |
187 | goto done; | |
188 | } | |
189 | info("rescanned %s, %ld tracks, %ld new", c->root, ntracks, nnew); | |
190 | done: | |
191 | if(fp) | |
192 | xfclose(fp); | |
193 | if(pid) | |
194 | while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR) | |
195 | ; | |
196 | } | |
197 | ||
198 | struct recheck_state { | |
199 | const struct collection *c; | |
d1694464 | 200 | long nobsolete, nnocollection, nlength; |
460b9539 | 201 | }; |
202 | ||
203 | /* called for each non-alias track */ | |
204 | static int recheck_callback(const char *track, | |
205 | struct kvp *data, | |
206 | void *u, | |
207 | DB_TXN *tid) { | |
208 | struct recheck_state *cs = u; | |
209 | const struct collection *c = cs->c; | |
210 | const char *path = kvp_get(data, "_path"); | |
211 | char buffer[20]; | |
62dc3748 RK |
212 | int err, n; |
213 | long length; | |
460b9539 | 214 | |
215 | if(aborted()) return EINTR; | |
216 | D(("rechecking %s", track)); | |
d1694464 | 217 | /* if we're not checking a specific collection, find the right collection */ |
218 | if(!c) { | |
219 | if(!(c = find_track_collection(track))) { | |
220 | D(("obsoleting %s", track)); | |
221 | if((err = trackdb_obsolete(track, tid))) return err; | |
222 | ++cs->nnocollection; | |
223 | return 0; | |
224 | } | |
225 | } | |
460b9539 | 226 | /* see if the track has evaporated */ |
227 | if(check(c->module, c->root, path) == 0) { | |
228 | D(("obsoleting %s", track)); | |
229 | if((err = trackdb_obsolete(track, tid))) return err; | |
230 | ++cs->nobsolete; | |
231 | return 0; | |
232 | } | |
233 | /* make sure we know the length */ | |
234 | if(!kvp_get(data, "_length")) { | |
235 | D(("recalculating length of %s", track)); | |
62dc3748 RK |
236 | for(n = 0; n < config->tracklength.n; ++n) |
237 | if(fnmatch(config->tracklength.s[n].s[0], track, 0) == 0) | |
238 | break; | |
239 | if(n >= config->tracklength.n) | |
240 | error(0, "no tracklength plugin found for %s", track); | |
241 | else { | |
242 | length = tracklength(config->tracklength.s[n].s[1], track, path); | |
243 | if(length > 0) { | |
244 | byte_snprintf(buffer, sizeof buffer, "%ld", length); | |
245 | kvp_set(&data, "_length", buffer); | |
246 | if((err = trackdb_putdata(trackdb_tracksdb, track, data, tid, 0))) | |
247 | return err; | |
248 | ++cs->nlength; | |
249 | } | |
460b9539 | 250 | } |
251 | } | |
252 | return 0; | |
253 | } | |
254 | ||
255 | /* recheck a collection */ | |
256 | static void recheck_collection(const struct collection *c) { | |
257 | struct recheck_state cs; | |
258 | ||
d1694464 | 259 | if(c) |
260 | info("rechecking %s", c->root); | |
261 | else | |
262 | info("rechecking all tracks"); | |
460b9539 | 263 | for(;;) { |
264 | checkabort(); | |
265 | global_tid = trackdb_begin_transaction(); | |
266 | memset(&cs, 0, sizeof cs); | |
267 | cs.c = c; | |
d1694464 | 268 | if(trackdb_scan(c ? c->root : 0, recheck_callback, &cs, global_tid)) |
269 | goto fail; | |
460b9539 | 270 | break; |
271 | fail: | |
272 | /* Maybe we need to shut down */ | |
273 | checkabort(); | |
274 | /* Abort the transaction and try again in a bit. */ | |
275 | trackdb_abort_transaction(global_tid); | |
276 | global_tid = 0; | |
277 | /* Let anything else that is going on get out of the way. */ | |
278 | sleep(10); | |
279 | checkabort(); | |
d1694464 | 280 | if(c) |
281 | info("resuming recheck of %s", c->root); | |
282 | else | |
283 | info("resuming global recheck"); | |
460b9539 | 284 | } |
285 | trackdb_commit_transaction(global_tid); | |
286 | global_tid = 0; | |
d1694464 | 287 | if(c) |
288 | info("rechecked %s, %ld obsoleted, %ld lengths calculated", | |
289 | c->root, cs.nobsolete, cs.nlength); | |
290 | else | |
291 | info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated", | |
292 | cs.nnocollection, cs.nobsolete, cs.nlength); | |
460b9539 | 293 | } |
294 | ||
295 | /* rescan/recheck a collection by name */ | |
296 | static void do_directory(const char *s, | |
297 | void (*fn)(const struct collection *c)) { | |
298 | int n; | |
299 | ||
300 | for(n = 0; (n < config->collection.n | |
301 | && strcmp(config->collection.s[n].root, s)); ++n) | |
302 | ; | |
303 | if(n < config->collection.n) | |
304 | fn(&config->collection.s[n]); | |
305 | else | |
306 | error(0, "no collection has root '%s'", s); | |
307 | } | |
308 | ||
309 | /* rescan/recheck all collections */ | |
310 | static void do_all(void (*fn)(const struct collection *c)) { | |
311 | int n; | |
312 | ||
313 | for(n = 0; n < config->collection.n; ++n) | |
314 | fn(&config->collection.s[n]); | |
6aba3f6c RK |
315 | /* TODO: we need to tidy up tracks from collections now removed. We could do |
316 | * this two ways: either remember collections we think there are and spot | |
317 | * their disappearance, or iterate over all tracks and gc any that don't fit | |
318 | * into some collection. | |
319 | * | |
320 | * Having a way to rename collections would be rather convenient too but | |
321 | * that's another kettle of monkeys. | |
322 | */ | |
460b9539 | 323 | } |
324 | ||
2a10b70b RK |
325 | /** @brief Expire noticed.db */ |
326 | static void expire_noticed(void) { | |
1e64e9fb RK |
327 | time_t now; |
328 | ||
329 | time(&now); | |
330 | trackdb_expire_noticed(now - config->noticed_history * 86400); | |
2a10b70b RK |
331 | } |
332 | ||
460b9539 | 333 | int main(int argc, char **argv) { |
0ca6d097 | 334 | int n, logsyslog = !isatty(2); |
460b9539 | 335 | struct sigaction sa; |
336 | ||
337 | set_progname(argv); | |
320598d4 | 338 | mem_init(); |
460b9539 | 339 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
0ca6d097 | 340 | while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) { |
460b9539 | 341 | switch(n) { |
342 | case 'h': help(); | |
343 | case 'V': version(); | |
344 | case 'c': configfile = optarg; break; | |
345 | case 'd': debugging = 1; break; | |
346 | case 'D': debugging = 0; break; | |
0ca6d097 RK |
347 | case 'S': logsyslog = 0; break; |
348 | case 's': logsyslog = 1; break; | |
460b9539 | 349 | default: fatal(0, "invalid option"); |
350 | } | |
351 | } | |
5464a25a | 352 | if(logsyslog) { |
460b9539 | 353 | openlog(progname, LOG_PID, LOG_DAEMON); |
354 | log_default = &log_syslog; | |
355 | } | |
c00fce3a | 356 | if(config_read(0)) fatal(0, "cannot read configuration"); |
460b9539 | 357 | xnice(config->nice_rescan); |
358 | sa.sa_handler = signal_handler; | |
359 | sa.sa_flags = SA_RESTART; | |
360 | sigemptyset(&sa.sa_mask); | |
361 | xsigaction(SIGTERM, &sa, 0); | |
362 | xsigaction(SIGINT, &sa, 0); | |
363 | info("started"); | |
d25c4615 RK |
364 | trackdb_init(TRACKDB_NO_RECOVER); |
365 | trackdb_open(TRACKDB_NO_UPGRADE); | |
460b9539 | 366 | if(optind == argc) { |
d1694464 | 367 | /* Rescan all collections */ |
460b9539 | 368 | do_all(rescan_collection); |
d1694464 | 369 | /* Check that every track still exists */ |
370 | recheck_collection(0); | |
2a10b70b RK |
371 | /* Expire noticed.db */ |
372 | expire_noticed(); | |
460b9539 | 373 | } |
374 | else { | |
d1694464 | 375 | /* Rescan specified collections */ |
460b9539 | 376 | for(n = optind; n < argc; ++n) |
377 | do_directory(argv[n], rescan_collection); | |
d1694464 | 378 | /* Check specified collections for tracks that have gone */ |
460b9539 | 379 | for(n = optind; n < argc; ++n) |
380 | do_directory(argv[n], recheck_collection); | |
381 | } | |
382 | trackdb_close(); | |
383 | trackdb_deinit(); | |
384 | info("completed"); | |
385 | return 0; | |
386 | } | |
387 | ||
388 | /* | |
389 | Local Variables: | |
390 | c-basic-offset:2 | |
391 | comment-column:40 | |
392 | fill-column:79 | |
393 | indent-tabs-mode:nil | |
394 | End: | |
395 | */ |