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>
39 #include "configuration.h"
45 #include "inputline.h"
52 #include "trackdb-int.h"
53 #include "trackname.h"
56 static DB_TXN
*global_tid
;
58 static const struct option options
[] = {
59 { "help", no_argument
, 0, 'h' },
60 { "version", no_argument
, 0, 'V' },
61 { "config", required_argument
, 0, 'c' },
62 { "debug", no_argument
, 0, 'd' },
63 { "no-debug", no_argument
, 0, 'D' },
64 { "syslog", no_argument
, 0, 's' },
65 { "no-syslog", no_argument
, 0, 'S' },
69 /* display usage message and terminate */
70 static void help(void) {
72 " disorder-rescan [OPTIONS] [PATH...]\n"
74 " --help, -h Display usage message\n"
75 " --version, -V Display version number\n"
76 " --config PATH, -c PATH Set configuration file\n"
77 " --debug, -d Turn on debugging\n"
78 " --[no-]syslog Force logging\n"
80 "Rescanner for DisOrder. Not intended to be run\n"
86 /* display version number and terminate */
87 static void version(void) {
88 xprintf("%s", disorder_version_string
);
93 static volatile sig_atomic_t signalled
;
95 static void signal_handler(int sig
) {
96 if(sig
== 0) _exit(-1); /* "Cannot happen" */
100 static int aborted(void) {
101 return signalled
|| getppid() == 1;
104 /* Exit if our parent has gone away or we have been told to stop. */
105 static void checkabort(void) {
107 info("parent has terminated");
108 trackdb_abort_transaction(global_tid
);
112 info("received signal %d", signalled
);
113 trackdb_abort_transaction(global_tid
);
118 /* rescan a collection */
119 static void rescan_collection(const struct collection
*c
) {
124 long ntracks
= 0, nnew
= 0;
127 info("rescanning %s with %s", c
->root
, c
->module
);
128 /* plugin runs in a subprocess */
130 if(!(pid
= xfork())) {
135 scan(c
->module
, c
->root
);
136 if(fflush(stdout
) < 0)
137 fatal(errno
, "error writing to scanner pipe");
141 if(!(fp
= fdopen(p
[0], "r")))
142 fatal(errno
, "error calling fdopen");
143 /* read tracks from the plugin */
144 while(!inputline("rescanner", fp
, &path
, 0)) {
146 /* actually we can cope relatively well within the server, but they'll go
147 * wrong in track listings */
148 if(strchr(path
, '\n')) {
149 error(0, "cannot cope with tracks with newlines in the name");
152 if(!(track
= any2utf8(c
->encoding
, path
))) {
153 error(0, "cannot convert track path to UTF-8: %s", path
);
156 if(config
->dbversion
> 1) {
157 /* We use NFC track names */
158 if(!(track
= utf8_compose_canon(track
, strlen(track
), 0))) {
159 error(0, "cannot convert track path to NFC: %s", path
);
163 D(("track %s", track
));
164 /* only tracks with a known player are admitted */
165 for(n
= 0; (n
< config
->player
.n
166 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
168 if(n
< config
->player
.n
) {
169 nnew
+= !!trackdb_notice(track
, path
);
171 if(ntracks
% 1000 == 0)
172 info("rescanning %s, %ld tracks so far", c
->root
, ntracks
);
177 error(errno
, "error reading from scanner pipe");
182 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
184 if(r
< 0) fatal(errno
, "error calling waitpid");
187 error(0, "scanner subprocess: %s", wstat(w
));
190 info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
195 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
199 struct recheck_state
{
200 const struct collection
*c
;
201 long nobsolete
, nnocollection
, nlength
;
204 /* called for each non-alias track */
205 static int recheck_callback(const char *track
,
209 struct recheck_state
*cs
= u
;
210 const struct collection
*c
= cs
->c
;
211 const char *path
= kvp_get(data
, "_path");
216 if(aborted()) return EINTR
;
217 D(("rechecking %s", track
));
218 /* if we're not checking a specific collection, find the right collection */
220 if(!(c
= find_track_collection(track
))) {
221 D(("obsoleting %s", track
));
222 if((err
= trackdb_obsolete(track
, tid
))) return err
;
227 /* see if the track has evaporated */
228 if(check(c
->module
, c
->root
, path
) == 0) {
229 D(("obsoleting %s", track
));
230 if((err
= trackdb_obsolete(track
, tid
))) return err
;
234 /* make sure we know the length */
235 if(!kvp_get(data
, "_length")) {
236 D(("recalculating length of %s", track
));
237 for(n
= 0; n
< config
->tracklength
.n
; ++n
)
238 if(fnmatch(config
->tracklength
.s
[n
].s
[0], track
, 0) == 0)
240 if(n
>= config
->tracklength
.n
)
241 error(0, "no tracklength plugin found for %s", track
);
243 length
= tracklength(config
->tracklength
.s
[n
].s
[1], track
, path
);
245 byte_snprintf(buffer
, sizeof buffer
, "%ld", length
);
246 kvp_set(&data
, "_length", buffer
);
247 if((err
= trackdb_putdata(trackdb_tracksdb
, track
, data
, tid
, 0)))
256 /* recheck a collection */
257 static void recheck_collection(const struct collection
*c
) {
258 struct recheck_state cs
;
261 info("rechecking %s", c
->root
);
263 info("rechecking all tracks");
266 global_tid
= trackdb_begin_transaction();
267 memset(&cs
, 0, sizeof cs
);
269 if(trackdb_scan(c ? c
->root
: 0, recheck_callback
, &cs
, global_tid
))
273 /* Maybe we need to shut down */
275 /* Abort the transaction and try again in a bit. */
276 trackdb_abort_transaction(global_tid
);
278 /* Let anything else that is going on get out of the way. */
282 info("resuming recheck of %s", c
->root
);
284 info("resuming global recheck");
286 trackdb_commit_transaction(global_tid
);
289 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
290 c
->root
, cs
.nobsolete
, cs
.nlength
);
292 info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
293 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
296 /* rescan/recheck a collection by name */
297 static void do_directory(const char *s
,
298 void (*fn
)(const struct collection
*c
)) {
301 for(n
= 0; (n
< config
->collection
.n
302 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
304 if(n
< config
->collection
.n
)
305 fn(&config
->collection
.s
[n
]);
307 error(0, "no collection has root '%s'", s
);
310 /* rescan/recheck all collections */
311 static void do_all(void (*fn
)(const struct collection
*c
)) {
314 for(n
= 0; n
< config
->collection
.n
; ++n
)
315 fn(&config
->collection
.s
[n
]);
316 /* TODO: we need to tidy up tracks from collections now removed. We could do
317 * this two ways: either remember collections we think there are and spot
318 * their disappearance, or iterate over all tracks and gc any that don't fit
319 * into some collection.
321 * Having a way to rename collections would be rather convenient too but
322 * that's another kettle of monkeys.
326 /** @brief Expire noticed.db */
327 static void expire_noticed(void) {
331 trackdb_expire_noticed(now
- config
->noticed_history
* 86400);
334 int main(int argc
, char **argv
) {
335 int n
, logsyslog
= !isatty(2);
340 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
341 while((n
= getopt_long(argc
, argv
, "hVc:dDSs", options
, 0)) >= 0) {
345 case 'c': configfile
= optarg
; break;
346 case 'd': debugging
= 1; break;
347 case 'D': debugging
= 0; break;
348 case 'S': logsyslog
= 0; break;
349 case 's': logsyslog
= 1; break;
350 default: fatal(0, "invalid option");
354 openlog(progname
, LOG_PID
, LOG_DAEMON
);
355 log_default
= &log_syslog
;
357 if(config_read(0)) fatal(0, "cannot read configuration");
358 xnice(config
->nice_rescan
);
359 sa
.sa_handler
= signal_handler
;
360 sa
.sa_flags
= SA_RESTART
;
361 sigemptyset(&sa
.sa_mask
);
362 xsigaction(SIGTERM
, &sa
, 0);
363 xsigaction(SIGINT
, &sa
, 0);
365 trackdb_init(TRACKDB_NO_RECOVER
);
366 trackdb_open(TRACKDB_NO_UPGRADE
);
368 /* Rescan all collections */
369 do_all(rescan_collection
);
370 /* Check that every track still exists */
371 recheck_collection(0);
372 /* Expire noticed.db */
376 /* Rescan specified collections */
377 for(n
= optind
; n
< argc
; ++n
)
378 do_directory(argv
[n
], rescan_collection
);
379 /* Check specified collections for tracks that have gone */
380 for(n
= optind
; n
< argc
; ++n
)
381 do_directory(argv
[n
], recheck_collection
);