2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
30 #include <sys/types.h>
38 #include "configuration.h"
44 #include "inputline.h"
50 #include "trackdb-int.h"
52 static DB_TXN *global_tid;
54 static const struct option options[] = {
55 { "help", no_argument, 0, 'h' },
56 { "version", no_argument, 0, 'V' },
57 { "config", required_argument, 0, 'c' },
58 { "debug", no_argument, 0, 'd' },
59 { "no-debug", no_argument, 0, 'D' },
63 /* display usage message and terminate */
64 static void help(void) {
66 " disorder-rescan [OPTIONS] [PATH...]\n"
68 " --help, -h Display usage message\n"
69 " --version, -V Display version number\n"
70 " --config PATH, -c PATH Set configuration file\n"
71 " --debug, -d Turn on debugging\n"
73 "Rescanner for DisOrder. Not intended to be run\n"
79 /* display version number and terminate */
80 static void version(void) {
81 xprintf("disorder-rescan version %s\n", disorder_version_string);
86 static volatile sig_atomic_t signalled;
88 static void signal_handler(int sig) {
89 if(sig == 0) _exit(-1); /* "Cannot happen" */
93 static int aborted(void) {
94 return signalled || getppid() == 1;
97 /* Exit if our parent has gone away or we have been told to stop. */
98 static void checkabort(void) {
100 info("parent has terminated");
101 trackdb_abort_transaction(global_tid);
105 info("received signal %d", signalled);
106 trackdb_abort_transaction(global_tid);
111 /* rescan a collection */
112 static void rescan_collection(const struct collection *c) {
117 long ntracks = 0, nnew = 0;
120 info("rescanning %s with %s", c->root, c->module);
121 /* plugin runs in a subprocess */
123 if(!(pid = xfork())) {
128 scan(c->module, c->root);
129 if(fflush(stdout) < 0)
130 fatal(errno, "error writing to scanner pipe");
134 if(!(fp = fdopen(p[0], "r")))
135 fatal(errno, "error calling fdopen");
136 /* read tracks from the plugin */
137 while(!inputline("rescanner", fp, &path, 0)) {
139 /* actually we can cope relatively well within the server, but they'll go
140 * wrong in track listings */
141 if(strchr(path, '\n')) {
142 error(0, "cannot cope with tracks with newlines in the name");
145 if(!(track = any2utf8(c->encoding, path))) {
146 error(0, "cannot convert track path to UTF-8: %s", path);
149 D(("track %s", track));
150 /* only tracks with a known player are admitted */
151 for(n = 0; (n < config->player.n
152 && fnmatch(config->player.s[n].s[0], track, 0) != 0); ++n)
154 if(n < config->player.n) {
155 nnew += !!trackdb_notice(track, path);
161 error(errno, "error reading from scanner pipe");
166 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
168 if(r < 0) fatal(errno, "error calling waitpid");
171 error(0, "scanner subprocess: %s", wstat(w));
174 info("rescanned %s, %ld tracks, %ld new", c->root, ntracks, nnew);
179 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
183 struct recheck_state {
184 const struct collection *c;
185 long nobsolete, nlength;
188 /* called for each non-alias track */
189 static int recheck_callback(const char *track,
193 struct recheck_state *cs = u;
194 const struct collection *c = cs->c;
195 const char *path = kvp_get(data, "_path");
200 if(aborted()) return EINTR;
201 D(("rechecking %s", track));
202 /* see if the track has evaporated */
203 if(check(c->module, c->root, path) == 0) {
204 D(("obsoleting %s", track));
205 if((err = trackdb_obsolete(track, tid))) return err;
209 /* make sure we know the length */
210 if(!kvp_get(data, "_length")) {
211 D(("recalculating length of %s", track));
212 n = tracklength(track, path);
214 byte_snprintf(buffer, sizeof buffer, "%ld", n);
215 kvp_set(&data, "_length", buffer);
216 if((err = trackdb_putdata(trackdb_tracksdb, track, data, tid, 0)))
224 /* recheck a collection */
225 static void recheck_collection(const struct collection *c) {
226 struct recheck_state cs;
228 info("rechecking %s", c->root);
231 global_tid = trackdb_begin_transaction();
232 memset(&cs, 0, sizeof cs);
234 if(trackdb_scan(c->root, recheck_callback, &cs, global_tid)) goto fail;
237 /* Maybe we need to shut down */
239 /* Abort the transaction and try again in a bit. */
240 trackdb_abort_transaction(global_tid);
242 /* Let anything else that is going on get out of the way. */
245 info("resuming recheck of %s", c->root);
247 trackdb_commit_transaction(global_tid);
249 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
250 c->root, cs.nobsolete, cs.nlength);
253 /* rescan/recheck a collection by name */
254 static void do_directory(const char *s,
255 void (*fn)(const struct collection *c)) {
258 for(n = 0; (n < config->collection.n
259 && strcmp(config->collection.s[n].root, s)); ++n)
261 if(n < config->collection.n)
262 fn(&config->collection.s[n]);
264 error(0, "no collection has root '%s'", s);
267 /* rescan/recheck all collections */
268 static void do_all(void (*fn)(const struct collection *c)) {
271 for(n = 0; n < config->collection.n; ++n)
272 fn(&config->collection.s[n]);
273 /* TODO: we need to tidy up tracks from collections now removed. We could do
274 * this two ways: either remember collections we think there are and spot
275 * their disappearance, or iterate over all tracks and gc any that don't fit
276 * into some collection.
278 * Having a way to rename collections would be rather convenient too but
279 * that's another kettle of monkeys.
283 int main(int argc, char **argv) {
289 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
290 while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) {
294 case 'c': configfile = optarg; break;
295 case 'd': debugging = 1; break;
296 case 'D': debugging = 0; break;
297 default: fatal(0, "invalid option");
300 /* If stderr is a TTY then log there, otherwise to syslog. */
302 openlog(progname, LOG_PID, LOG_DAEMON);
303 log_default = &log_syslog;
305 if(config_read()) fatal(0, "cannot read configuration");
306 xnice(config->nice_rescan);
307 sa.sa_handler = signal_handler;
308 sa.sa_flags = SA_RESTART;
309 sigemptyset(&sa.sa_mask);
310 xsigaction(SIGTERM, &sa, 0);
311 xsigaction(SIGINT, &sa, 0);
316 do_all(rescan_collection);
317 do_all(recheck_collection);
320 for(n = optind; n < argc; ++n)
321 do_directory(argv[n], rescan_collection);
322 for(n = optind; n < argc; ++n)
323 do_directory(argv[n], recheck_collection);