chiark / gitweb /
copyright dates
[disorder] / server / rescan.c
CommitLineData
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>
37
38#include "configuration.h"
39#include "syscalls.h"
40#include "log.h"
41#include "defs.h"
42#include "mem.h"
43#include "plugin.h"
44#include "inputline.h"
45#include "charset.h"
46#include "wstat.h"
47#include "kvp.h"
48#include "printf.h"
49#include "trackdb.h"
50#include "trackdb-int.h"
51
52static DB_TXN *global_tid;
53
54static 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' },
60 { 0, 0, 0, 0 }
61};
62
63/* display usage message and terminate */
64static void help(void) {
65 xprintf("Usage:\n"
66 " disorder-rescan [OPTIONS] [PATH...]\n"
67 "Options:\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"
72 "\n"
73 "Rescanner for DisOrder. Not intended to be run\n"
74 "directly.\n");
75 xfclose(stdout);
76 exit(0);
77}
78
79/* display version number and terminate */
80static void version(void) {
81 xprintf("disorder-rescan version %s\n", disorder_version_string);
82 xfclose(stdout);
83 exit(0);
84}
85
86static volatile sig_atomic_t signalled;
87
88static void signal_handler(int sig) {
89 if(sig == 0) _exit(-1); /* "Cannot happen" */
90 signalled = sig;
91}
92
93static int aborted(void) {
94 return signalled || getppid() == 1;
95}
96
97/* Exit if our parent has gone away or we have been told to stop. */
98static void checkabort(void) {
99 if(getppid() == 1) {
100 info("parent has terminated");
101 trackdb_abort_transaction(global_tid);
102 exit(0);
103 }
104 if(signalled) {
105 info("received signal %d", signalled);
106 trackdb_abort_transaction(global_tid);
107 exit(0);
108 }
109}
110
111/* rescan a collection */
112static void rescan_collection(const struct collection *c) {
113 pid_t pid, r;
114 int p[2], n, w;
115 FILE *fp = 0;
116 char *path, *track;
117 long ntracks = 0, nnew = 0;
118
119 checkabort();
120 info("rescanning %s with %s", c->root, c->module);
121 /* plugin runs in a subprocess */
122 xpipe(p);
123 if(!(pid = xfork())) {
124 exitfn = _exit;
125 xclose(p[0]);
126 xdup2(p[1], 1);
127 xclose(p[1]);
128 scan(c->module, c->root);
129 if(fflush(stdout) < 0)
130 fatal(errno, "error writing to scanner pipe");
131 _exit(0);
132 }
133 xclose(p[1]);
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)) {
138 checkabort();
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");
143 continue;
144 }
145 if(!(track = any2utf8(c->encoding, path))) {
146 error(0, "cannot convert track path to UTF-8: %s", path);
147 continue;
148 }
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)
153 ;
154 if(n < config->player.n) {
155 nnew += !!trackdb_notice(track, path);
156 ++ntracks;
157 }
158 }
159 /* tidy up */
160 if(ferror(fp)) {
161 error(errno, "error reading from scanner pipe");
162 goto done;
163 }
164 xfclose(fp);
165 fp = 0;
166 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
167 ;
168 if(r < 0) fatal(errno, "error calling waitpid");
169 pid = 0;
170 if(w) {
171 error(0, "scanner subprocess: %s", wstat(w));
172 goto done;
173 }
174 info("rescanned %s, %ld tracks, %ld new", c->root, ntracks, nnew);
175done:
176 if(fp)
177 xfclose(fp);
178 if(pid)
179 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
180 ;
181}
182
183struct recheck_state {
184 const struct collection *c;
185 long nobsolete, nlength;
186};
187
188/* called for each non-alias track */
189static int recheck_callback(const char *track,
190 struct kvp *data,
191 void *u,
192 DB_TXN *tid) {
193 struct recheck_state *cs = u;
194 const struct collection *c = cs->c;
195 const char *path = kvp_get(data, "_path");
196 char buffer[20];
197 int err;
198 long n;
199
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;
206 ++cs->nobsolete;
207 return 0;
208 }
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);
213 if(n > 0) {
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)))
217 return err;
218 ++cs->nlength;
219 }
220 }
221 return 0;
222}
223
224/* recheck a collection */
225static void recheck_collection(const struct collection *c) {
226 struct recheck_state cs;
227
228 info("rechecking %s", c->root);
229 for(;;) {
230 checkabort();
231 global_tid = trackdb_begin_transaction();
232 memset(&cs, 0, sizeof cs);
233 cs.c = c;
234 if(trackdb_scan(c->root, recheck_callback, &cs, global_tid)) goto fail;
235 break;
236 fail:
237 /* Maybe we need to shut down */
238 checkabort();
239 /* Abort the transaction and try again in a bit. */
240 trackdb_abort_transaction(global_tid);
241 global_tid = 0;
242 /* Let anything else that is going on get out of the way. */
243 sleep(10);
244 checkabort();
245 info("resuming recheck of %s", c->root);
246 }
247 trackdb_commit_transaction(global_tid);
248 global_tid = 0;
249 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
250 c->root, cs.nobsolete, cs.nlength);
251}
252
253/* rescan/recheck a collection by name */
254static void do_directory(const char *s,
255 void (*fn)(const struct collection *c)) {
256 int n;
257
258 for(n = 0; (n < config->collection.n
259 && strcmp(config->collection.s[n].root, s)); ++n)
260 ;
261 if(n < config->collection.n)
262 fn(&config->collection.s[n]);
263 else
264 error(0, "no collection has root '%s'", s);
265}
266
267/* rescan/recheck all collections */
268static void do_all(void (*fn)(const struct collection *c)) {
269 int n;
270
271 for(n = 0; n < config->collection.n; ++n)
272 fn(&config->collection.s[n]);
6aba3f6c
RK
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.
277 *
278 * Having a way to rename collections would be rather convenient too but
279 * that's another kettle of monkeys.
280 */
460b9539 281}
282
283int main(int argc, char **argv) {
284 int n;
285 struct sigaction sa;
286
287 set_progname(argv);
288 mem_init(1);
289 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
290 while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) {
291 switch(n) {
292 case 'h': help();
293 case 'V': version();
294 case 'c': configfile = optarg; break;
295 case 'd': debugging = 1; break;
296 case 'D': debugging = 0; break;
297 default: fatal(0, "invalid option");
298 }
299 }
300 /* If stderr is a TTY then log there, otherwise to syslog. */
301 if(!isatty(2)) {
302 openlog(progname, LOG_PID, LOG_DAEMON);
303 log_default = &log_syslog;
304 }
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);
312 info("started");
313 trackdb_init(0);
314 trackdb_open();
315 if(optind == argc) {
316 do_all(rescan_collection);
317 do_all(recheck_collection);
318 }
319 else {
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);
324 }
325 trackdb_close();
326 trackdb_deinit();
327 info("completed");
328 return 0;
329}
330
331/*
332Local Variables:
333c-basic-offset:2
334comment-column:40
335fill-column:79
336indent-tabs-mode:nil
337End:
338*/