Remove arch tags throughout
[disorder] / server / rescan.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006 Richard Kettlewell
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
52 static DB_TXN *global_tid;
53
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' },
60 { 0, 0, 0, 0 }
61 };
62
63 /* display usage message and terminate */
64 static 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 */
80 static void version(void) {
81 xprintf("disorder-rescan version %s\n", disorder_version_string);
82 xfclose(stdout);
83 exit(0);
84 }
85
86 static volatile sig_atomic_t signalled;
87
88 static void signal_handler(int sig) {
89 if(sig == 0) _exit(-1); /* "Cannot happen" */
90 signalled = sig;
91 }
92
93 static 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. */
98 static 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 */
112 static 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);
175 done:
176 if(fp)
177 xfclose(fp);
178 if(pid)
179 while((r = waitpid(pid, &w, 0)) == -1 && errno == EINTR)
180 ;
181 }
182
183 struct recheck_state {
184 const struct collection *c;
185 long nobsolete, nlength;
186 };
187
188 /* called for each non-alias track */
189 static 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 */
225 static 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 */
254 static 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 */
268 static 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]);
273 }
274
275 int main(int argc, char **argv) {
276 int n;
277 struct sigaction sa;
278
279 set_progname(argv);
280 mem_init(1);
281 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
282 while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) {
283 switch(n) {
284 case 'h': help();
285 case 'V': version();
286 case 'c': configfile = optarg; break;
287 case 'd': debugging = 1; break;
288 case 'D': debugging = 0; break;
289 default: fatal(0, "invalid option");
290 }
291 }
292 /* If stderr is a TTY then log there, otherwise to syslog. */
293 if(!isatty(2)) {
294 openlog(progname, LOG_PID, LOG_DAEMON);
295 log_default = &log_syslog;
296 }
297 if(config_read()) fatal(0, "cannot read configuration");
298 xnice(config->nice_rescan);
299 sa.sa_handler = signal_handler;
300 sa.sa_flags = SA_RESTART;
301 sigemptyset(&sa.sa_mask);
302 xsigaction(SIGTERM, &sa, 0);
303 xsigaction(SIGINT, &sa, 0);
304 info("started");
305 trackdb_init(0);
306 trackdb_open();
307 if(optind == argc) {
308 do_all(rescan_collection);
309 do_all(recheck_collection);
310 }
311 else {
312 for(n = optind; n < argc; ++n)
313 do_directory(argv[n], rescan_collection);
314 for(n = optind; n < argc; ++n)
315 do_directory(argv[n], recheck_collection);
316 }
317 trackdb_close();
318 trackdb_deinit();
319 info("completed");
320 return 0;
321 }
322
323 /*
324 Local Variables:
325 c-basic-offset:2
326 comment-column:40
327 fill-column:79
328 indent-tabs-mode:nil
329 End:
330 */