2 * This file is part of DisOrder
3 * Copyright (C) 2005-2008 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
21 #include "disorder-server.h"
23 static time_t last_report
;
24 static DB_TXN
*global_tid
;
26 static const struct option options
[] = {
27 { "help", no_argument
, 0, 'h' },
28 { "version", no_argument
, 0, 'V' },
29 { "config", required_argument
, 0, 'c' },
30 { "debug", no_argument
, 0, 'd' },
31 { "no-debug", no_argument
, 0, 'D' },
32 { "syslog", no_argument
, 0, 's' },
33 { "no-syslog", no_argument
, 0, 'S' },
34 { "check", no_argument
, 0, 'K' },
35 { "no-check", no_argument
, 0, 'C' },
39 /* display usage message and terminate */
40 static void help(void) {
42 " disorder-rescan [OPTIONS] [PATH...]\n"
44 " --help, -h Display usage message\n"
45 " --version, -V Display version number\n"
46 " --config PATH, -c PATH Set configuration file\n"
47 " --debug, -d Turn on debugging\n"
48 " --[no-]syslog Enable/disable logging to syslog\n"
49 " --[no-]check Enable/disable track length check\n"
51 "Rescanner for DisOrder. Not intended to be run\n"
57 static volatile sig_atomic_t signalled
;
59 static void signal_handler(int sig
) {
60 if(sig
== 0) _exit(-1); /* "Cannot happen" */
64 static int aborted(void) {
65 return signalled
|| getppid() == 1;
68 /* Exit if our parent has gone away or we have been told to stop. */
69 static void checkabort(void) {
71 info("parent has terminated");
72 trackdb_abort_transaction(global_tid
);
76 info("received signal %d", signalled
);
77 trackdb_abort_transaction(global_tid
);
82 /* rescan a collection */
83 static void rescan_collection(const struct collection
*c
) {
88 long ntracks
= 0, nnew
= 0;
91 info("rescanning %s with %s", c
->root
, c
->module
);
92 /* plugin runs in a subprocess */
94 if(!(pid
= xfork())) {
99 scan(c
->module
, c
->root
);
100 if(fflush(stdout
) < 0)
101 fatal(errno
, "error writing to scanner pipe");
105 if(!(fp
= fdopen(p
[0], "r")))
106 fatal(errno
, "error calling fdopen");
107 /* read tracks from the plugin */
108 while(!inputline("rescanner", fp
, &path
, 0)) {
110 /* actually we can cope relatively well within the server, but they'll go
111 * wrong in track listings */
112 if(strchr(path
, '\n')) {
113 error(0, "cannot cope with tracks with newlines in the name");
116 if(!(track
= any2utf8(c
->encoding
, path
))) {
117 error(0, "cannot convert track path to UTF-8: %s", path
);
120 if(config
->dbversion
> 1) {
121 /* We use NFC track names */
122 if(!(track
= utf8_compose_canon(track
, strlen(track
), 0))) {
123 error(0, "cannot convert track path to NFC: %s", path
);
127 D(("track %s", track
));
128 /* only tracks with a known player are admitted */
129 for(n
= 0; (n
< config
->player
.n
130 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
132 if(n
< config
->player
.n
) {
133 nnew
+= !!trackdb_notice(track
, path
);
135 if(ntracks
% 100 == 0 && time(0) > last_report
+ 10) {
136 info("rescanning %s, %ld tracks so far", c
->root
, ntracks
);
143 error(errno
, "error reading from scanner pipe");
148 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
150 if(r
< 0) fatal(errno
, "error calling waitpid");
153 error(0, "scanner subprocess: %s", wstat(w
));
156 info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
161 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
165 struct recheck_state
{
166 const struct collection
*c
;
167 long nobsolete
, nnocollection
, nlength
;
168 struct recheck_track
*tracks
;
171 struct recheck_track
{
172 struct recheck_track
*next
;
176 /* called for each non-alias track */
177 static int recheck_list_callback(const char *track
,
178 struct kvp
attribute((unused
)) *data
,
179 struct kvp
attribute((unused
)) *prefs
,
181 DB_TXN
attribute((unused
)) *tid
) {
182 struct recheck_state
*cs
= u
;
183 struct recheck_track
*t
= xmalloc(sizeof *t
);
185 t
->next
= cs
->tracks
;
191 static int recheck_track_tid(struct recheck_state
*cs
,
192 const struct recheck_track
*t
,
194 const struct collection
*c
= cs
->c
;
201 if((err
= trackdb_getdata(trackdb_tracksdb
, t
->track
, &data
, tid
)))
203 path
= kvp_get(data
, "_path");
204 D(("rechecking %s", t
->track
));
205 /* if we're not checking a specific collection, find the right collection */
207 if(!(c
= find_track_collection(t
->track
))) {
208 D(("obsoleting %s", t
->track
));
209 if((err
= trackdb_obsolete(t
->track
, tid
)))
215 /* see if the track has evaporated */
216 if(check(c
->module
, c
->root
, path
) == 0) {
217 D(("obsoleting %s", t
->track
));
218 if((err
= trackdb_obsolete(t
->track
, tid
)))
223 /* make sure we know the length */
224 if(!kvp_get(data
, "_length")) {
225 D(("recalculating length of %s", t
->track
));
226 for(n
= 0; n
< config
->tracklength
.n
; ++n
)
227 if(fnmatch(config
->tracklength
.s
[n
].s
[0], t
->track
, 0) == 0)
229 if(n
>= config
->tracklength
.n
)
230 error(0, "no tracklength plugin found for %s", t
->track
);
232 length
= tracklength(config
->tracklength
.s
[n
].s
[1], t
->track
, path
);
234 byte_snprintf(buffer
, sizeof buffer
, "%ld", length
);
235 kvp_set(&data
, "_length", buffer
);
236 if((err
= trackdb_putdata(trackdb_tracksdb
, t
->track
, data
, tid
, 0)))
245 static int recheck_track(struct recheck_state
*cs
,
246 const struct recheck_track
*t
) {
249 WITH_TRANSACTION(recheck_track_tid(cs
, t
, tid
));
253 /* recheck a collection */
254 static void recheck_collection(const struct collection
*c
) {
255 struct recheck_state cs
;
256 const struct recheck_track
*t
;
260 info("rechecking %s", c
->root
);
262 info("rechecking all tracks");
263 /* Doing the checking inside a transaction locks up the server for much too
264 * long (because it spends lots of time thinking about each track). So we
265 * pull the full track list into memory and work from that.
267 * 100,000 tracks at, say, 80 bytes per track name, gives 8MB, which is quite
272 info("getting track list");
273 global_tid
= trackdb_begin_transaction();
274 memset(&cs
, 0, sizeof cs
);
276 if(trackdb_scan(c ? c
->root
: 0, recheck_list_callback
, &cs
, global_tid
))
280 /* Maybe we need to shut down */
282 /* Abort the transaction and try again in a bit. */
283 trackdb_abort_transaction(global_tid
);
285 /* Let anything else that is going on get out of the way. */
289 info("resuming recheck of %s", c
->root
);
291 info("resuming global recheck");
293 trackdb_commit_transaction(global_tid
);
296 for(t
= cs
.tracks
; t
; t
= t
->next
) {
299 recheck_track(&cs
, t
);
301 if(nrc
% 100 == 0 && time(0) > last_report
+ 10) {
303 info("rechecking %s, %ld tracks so far", c
->root
, nrc
);
305 info("rechecking all tracks, %ld tracks so far", nrc
);
310 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
311 c
->root
, cs
.nobsolete
, cs
.nlength
);
313 info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
314 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
317 /* rescan/recheck a collection by name */
318 static void do_directory(const char *s
,
319 void (*fn
)(const struct collection
*c
)) {
322 for(n
= 0; (n
< config
->collection
.n
323 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
325 if(n
< config
->collection
.n
)
326 fn(&config
->collection
.s
[n
]);
328 error(0, "no collection has root '%s'", s
);
331 /* rescan/recheck all collections */
332 static void do_all(void (*fn
)(const struct collection
*c
)) {
335 for(n
= 0; n
< config
->collection
.n
; ++n
)
336 fn(&config
->collection
.s
[n
]);
337 /* TODO: we need to tidy up tracks from collections now removed. We could do
338 * this two ways: either remember collections we think there are and spot
339 * their disappearance, or iterate over all tracks and gc any that don't fit
340 * into some collection.
342 * Having a way to rename collections would be rather convenient too but
343 * that's another kettle of monkeys.
347 /** @brief Expire noticed.db */
348 static void expire_noticed(void) {
352 trackdb_expire_noticed(now
- config
->noticed_history
* 86400);
355 int main(int argc
, char **argv
) {
356 int n
, logsyslog
= !isatty(2);
362 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
363 while((n
= getopt_long(argc
, argv
, "hVc:dDSsKC", options
, 0)) >= 0) {
366 case 'V': version("disorder-rescan");
367 case 'c': configfile
= optarg
; break;
368 case 'd': debugging
= 1; break;
369 case 'D': debugging
= 0; break;
370 case 'S': logsyslog
= 0; break;
371 case 's': logsyslog
= 1; break;
372 case 'K': do_check
= 1; break;
373 case 'C': do_check
= 0; break;
374 default: fatal(0, "invalid option");
378 openlog(progname
, LOG_PID
, LOG_DAEMON
);
379 log_default
= &log_syslog
;
381 if(config_read(0)) fatal(0, "cannot read configuration");
382 xnice(config
->nice_rescan
);
383 sa
.sa_handler
= signal_handler
;
384 sa
.sa_flags
= SA_RESTART
;
385 sigemptyset(&sa
.sa_mask
);
386 xsigaction(SIGTERM
, &sa
, 0);
387 xsigaction(SIGINT
, &sa
, 0);
389 trackdb_init(TRACKDB_NO_RECOVER
);
390 trackdb_open(TRACKDB_NO_UPGRADE
);
392 /* Rescan all collections */
393 do_all(rescan_collection
);
394 /* Check that every track still exists */
396 recheck_collection(0);
397 /* Expire noticed.db */
401 /* Rescan specified collections */
402 for(n
= optind
; n
< argc
; ++n
)
403 do_directory(argv
[n
], rescan_collection
);
404 /* Check specified collections for tracks that have gone */
406 for(n
= optind
; n
< argc
; ++n
)
407 do_directory(argv
[n
], recheck_collection
);