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
30 #include <sys/types.h>
39 #include "configuration.h"
45 #include "inputline.h"
52 #include "trackdb-int.h"
53 #include "trackname.h"
57 static DB_TXN
*global_tid
;
59 static const struct option options
[] = {
60 { "help", no_argument
, 0, 'h' },
61 { "version", no_argument
, 0, 'V' },
62 { "config", required_argument
, 0, 'c' },
63 { "debug", no_argument
, 0, 'd' },
64 { "no-debug", no_argument
, 0, 'D' },
65 { "syslog", no_argument
, 0, 's' },
66 { "no-syslog", no_argument
, 0, 'S' },
67 { "check", no_argument
, 0, 'K' },
68 { "no-check", no_argument
, 0, 'C' },
72 /* display usage message and terminate */
73 static void help(void) {
75 " disorder-rescan [OPTIONS] [PATH...]\n"
77 " --help, -h Display usage message\n"
78 " --version, -V Display version number\n"
79 " --config PATH, -c PATH Set configuration file\n"
80 " --debug, -d Turn on debugging\n"
81 " --[no-]syslog Enable/disable logging to syslog\n"
82 " --[no-]check Enable/disable track length check\n"
84 "Rescanner for DisOrder. Not intended to be run\n"
90 static volatile sig_atomic_t signalled
;
92 static void signal_handler(int sig
) {
93 if(sig
== 0) _exit(-1); /* "Cannot happen" */
97 static int aborted(void) {
98 return signalled
|| getppid() == 1;
101 /* Exit if our parent has gone away or we have been told to stop. */
102 static void checkabort(void) {
104 info("parent has terminated");
105 trackdb_abort_transaction(global_tid
);
109 info("received signal %d", signalled
);
110 trackdb_abort_transaction(global_tid
);
115 /* rescan a collection */
116 static void rescan_collection(const struct collection
*c
) {
121 long ntracks
= 0, nnew
= 0;
124 info("rescanning %s with %s", c
->root
, c
->module
);
125 /* plugin runs in a subprocess */
127 if(!(pid
= xfork())) {
132 scan(c
->module
, c
->root
);
133 if(fflush(stdout
) < 0)
134 fatal(errno
, "error writing to scanner pipe");
138 if(!(fp
= fdopen(p
[0], "r")))
139 fatal(errno
, "error calling fdopen");
140 /* read tracks from the plugin */
141 while(!inputline("rescanner", fp
, &path
, 0)) {
143 /* actually we can cope relatively well within the server, but they'll go
144 * wrong in track listings */
145 if(strchr(path
, '\n')) {
146 error(0, "cannot cope with tracks with newlines in the name");
149 if(!(track
= any2utf8(c
->encoding
, path
))) {
150 error(0, "cannot convert track path to UTF-8: %s", path
);
153 if(config
->dbversion
> 1) {
154 /* We use NFC track names */
155 if(!(track
= utf8_compose_canon(track
, strlen(track
), 0))) {
156 error(0, "cannot convert track path to NFC: %s", path
);
160 D(("track %s", track
));
161 /* only tracks with a known player are admitted */
162 for(n
= 0; (n
< config
->player
.n
163 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
165 if(n
< config
->player
.n
) {
166 nnew
+= !!trackdb_notice(track
, path
);
168 if(ntracks
% 1000 == 0)
169 info("rescanning %s, %ld tracks so far", c
->root
, ntracks
);
174 error(errno
, "error reading from scanner pipe");
179 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
181 if(r
< 0) fatal(errno
, "error calling waitpid");
184 error(0, "scanner subprocess: %s", wstat(w
));
187 info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
192 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
196 struct recheck_state
{
197 const struct collection
*c
;
198 long nobsolete
, nnocollection
, nlength
;
199 struct recheck_track
*tracks
;
202 struct recheck_track
{
203 struct recheck_track
*next
;
207 /* called for each non-alias track */
208 static int recheck_list_callback(const char *track
,
209 struct kvp
attribute((unused
)) *data
,
211 DB_TXN
attribute((unused
)) *tid
) {
212 struct recheck_state
*cs
= u
;
213 struct recheck_track
*t
= xmalloc(sizeof *t
);
215 t
->next
= cs
->tracks
;
221 static int recheck_track_tid(struct recheck_state
*cs
,
222 const struct recheck_track
*t
,
224 const struct collection
*c
= cs
->c
;
231 if((err
= trackdb_getdata(trackdb_tracksdb
, t
->track
, &data
, tid
)))
233 path
= kvp_get(data
, "_path");
234 D(("rechecking %s", t
->track
));
235 /* if we're not checking a specific collection, find the right collection */
237 if(!(c
= find_track_collection(t
->track
))) {
238 D(("obsoleting %s", t
->track
));
239 if((err
= trackdb_obsolete(t
->track
, tid
)))
245 /* see if the track has evaporated */
246 if(check(c
->module
, c
->root
, path
) == 0) {
247 D(("obsoleting %s", t
->track
));
248 if((err
= trackdb_obsolete(t
->track
, tid
)))
253 /* make sure we know the length */
254 if(!kvp_get(data
, "_length")) {
255 D(("recalculating length of %s", t
->track
));
256 for(n
= 0; n
< config
->tracklength
.n
; ++n
)
257 if(fnmatch(config
->tracklength
.s
[n
].s
[0], t
->track
, 0) == 0)
259 if(n
>= config
->tracklength
.n
)
260 error(0, "no tracklength plugin found for %s", t
->track
);
262 length
= tracklength(config
->tracklength
.s
[n
].s
[1], t
->track
, path
);
264 byte_snprintf(buffer
, sizeof buffer
, "%ld", length
);
265 kvp_set(&data
, "_length", buffer
);
266 if((err
= trackdb_putdata(trackdb_tracksdb
, t
->track
, data
, tid
, 0)))
275 static int recheck_track(struct recheck_state
*cs
,
276 const struct recheck_track
*t
) {
279 WITH_TRANSACTION(recheck_track_tid(cs
, t
, tid
));
283 /* recheck a collection */
284 static void recheck_collection(const struct collection
*c
) {
285 struct recheck_state cs
;
286 const struct recheck_track
*t
;
290 info("rechecking %s", c
->root
);
292 info("rechecking all tracks");
293 /* Doing the checking inside a transaction locks up the server for much too
294 * long (because it spends lots of time thinking about each track). So we
295 * pull the full track list into memory and work from that.
297 * 100,000 tracks at, say, 80 bytes per track name, gives 8MB, which is quite
302 info("getting track list");
303 global_tid
= trackdb_begin_transaction();
304 memset(&cs
, 0, sizeof cs
);
306 if(trackdb_scan(c ? c
->root
: 0, recheck_list_callback
, &cs
, global_tid
))
310 /* Maybe we need to shut down */
312 /* Abort the transaction and try again in a bit. */
313 trackdb_abort_transaction(global_tid
);
315 /* Let anything else that is going on get out of the way. */
319 info("resuming recheck of %s", c
->root
);
321 info("resuming global recheck");
323 trackdb_commit_transaction(global_tid
);
326 for(t
= cs
.tracks
; t
; t
= t
->next
) {
329 recheck_track(&cs
, t
);
333 info("rechecking %s, %ld tracks so far", c
->root
, nrc
);
335 info("rechecking all tracks, %ld tracks so far", nrc
);
339 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
340 c
->root
, cs
.nobsolete
, cs
.nlength
);
342 info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
343 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
346 /* rescan/recheck a collection by name */
347 static void do_directory(const char *s
,
348 void (*fn
)(const struct collection
*c
)) {
351 for(n
= 0; (n
< config
->collection
.n
352 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
354 if(n
< config
->collection
.n
)
355 fn(&config
->collection
.s
[n
]);
357 error(0, "no collection has root '%s'", s
);
360 /* rescan/recheck all collections */
361 static void do_all(void (*fn
)(const struct collection
*c
)) {
364 for(n
= 0; n
< config
->collection
.n
; ++n
)
365 fn(&config
->collection
.s
[n
]);
366 /* TODO: we need to tidy up tracks from collections now removed. We could do
367 * this two ways: either remember collections we think there are and spot
368 * their disappearance, or iterate over all tracks and gc any that don't fit
369 * into some collection.
371 * Having a way to rename collections would be rather convenient too but
372 * that's another kettle of monkeys.
376 /** @brief Expire noticed.db */
377 static void expire_noticed(void) {
381 trackdb_expire_noticed(now
- config
->noticed_history
* 86400);
384 int main(int argc
, char **argv
) {
385 int n
, logsyslog
= !isatty(2);
391 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
392 while((n
= getopt_long(argc
, argv
, "hVc:dDSsKC", options
, 0)) >= 0) {
395 case 'V': version("disorder-rescan");
396 case 'c': configfile
= optarg
; break;
397 case 'd': debugging
= 1; break;
398 case 'D': debugging
= 0; break;
399 case 'S': logsyslog
= 0; break;
400 case 's': logsyslog
= 1; break;
401 case 'K': do_check
= 1; break;
402 case 'C': do_check
= 0; break;
403 default: fatal(0, "invalid option");
407 openlog(progname
, LOG_PID
, LOG_DAEMON
);
408 log_default
= &log_syslog
;
410 if(config_read(0)) fatal(0, "cannot read configuration");
411 xnice(config
->nice_rescan
);
412 sa
.sa_handler
= signal_handler
;
413 sa
.sa_flags
= SA_RESTART
;
414 sigemptyset(&sa
.sa_mask
);
415 xsigaction(SIGTERM
, &sa
, 0);
416 xsigaction(SIGINT
, &sa
, 0);
418 trackdb_init(TRACKDB_NO_RECOVER
);
419 trackdb_open(TRACKDB_NO_UPGRADE
);
421 /* Rescan all collections */
422 do_all(rescan_collection
);
423 /* Check that every track still exists */
425 recheck_collection(0);
426 /* Expire noticed.db */
430 /* Rescan specified collections */
431 for(n
= optind
; n
< argc
; ++n
)
432 do_directory(argv
[n
], rescan_collection
);
433 /* Check specified collections for tracks that have gone */
435 for(n
= optind
; n
< argc
; ++n
)
436 do_directory(argv
[n
], recheck_collection
);