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>
38 #include "configuration.h"
44 #include "inputline.h"
50 #include "trackdb-int.h"
51 #include "trackname.h"
53 static DB_TXN
*global_tid
;
55 static const struct option options
[] = {
56 { "help", no_argument
, 0, 'h' },
57 { "version", no_argument
, 0, 'V' },
58 { "config", required_argument
, 0, 'c' },
59 { "debug", no_argument
, 0, 'd' },
60 { "no-debug", no_argument
, 0, 'D' },
64 /* display usage message and terminate */
65 static void help(void) {
67 " disorder-rescan [OPTIONS] [PATH...]\n"
69 " --help, -h Display usage message\n"
70 " --version, -V Display version number\n"
71 " --config PATH, -c PATH Set configuration file\n"
72 " --debug, -d Turn on debugging\n"
74 "Rescanner for DisOrder. Not intended to be run\n"
80 /* display version number and terminate */
81 static void version(void) {
82 xprintf("disorder-rescan version %s\n", disorder_version_string
);
87 static volatile sig_atomic_t signalled
;
89 static void signal_handler(int sig
) {
90 if(sig
== 0) _exit(-1); /* "Cannot happen" */
94 static int aborted(void) {
95 return signalled
|| getppid() == 1;
98 /* Exit if our parent has gone away or we have been told to stop. */
99 static void checkabort(void) {
101 info("parent has terminated");
102 trackdb_abort_transaction(global_tid
);
106 info("received signal %d", signalled
);
107 trackdb_abort_transaction(global_tid
);
112 /* rescan a collection */
113 static void rescan_collection(const struct collection
*c
) {
118 long ntracks
= 0, nnew
= 0;
121 info("rescanning %s with %s", c
->root
, c
->module
);
122 /* plugin runs in a subprocess */
124 if(!(pid
= xfork())) {
129 scan(c
->module
, c
->root
);
130 if(fflush(stdout
) < 0)
131 fatal(errno
, "error writing to scanner pipe");
135 if(!(fp
= fdopen(p
[0], "r")))
136 fatal(errno
, "error calling fdopen");
137 /* read tracks from the plugin */
138 while(!inputline("rescanner", fp
, &path
, 0)) {
140 /* actually we can cope relatively well within the server, but they'll go
141 * wrong in track listings */
142 if(strchr(path
, '\n')) {
143 error(0, "cannot cope with tracks with newlines in the name");
146 if(!(track
= any2utf8(c
->encoding
, path
))) {
147 error(0, "cannot convert track path to UTF-8: %s", path
);
150 D(("track %s", track
));
151 /* only tracks with a known player are admitted */
152 for(n
= 0; (n
< config
->player
.n
153 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
155 if(n
< config
->player
.n
) {
156 nnew
+= !!trackdb_notice(track
, path
);
162 error(errno
, "error reading from scanner pipe");
167 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
169 if(r
< 0) fatal(errno
, "error calling waitpid");
172 error(0, "scanner subprocess: %s", wstat(w
));
175 info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
180 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
184 struct recheck_state
{
185 const struct collection
*c
;
186 long nobsolete
, nnocollection
, nlength
;
189 /* called for each non-alias track */
190 static int recheck_callback(const char *track
,
194 struct recheck_state
*cs
= u
;
195 const struct collection
*c
= cs
->c
;
196 const char *path
= kvp_get(data
, "_path");
201 if(aborted()) return EINTR
;
202 D(("rechecking %s", track
));
203 /* if we're not checking a specific collection, find the right collection */
205 if(!(c
= find_track_collection(track
))) {
206 D(("obsoleting %s", track
));
207 if((err
= trackdb_obsolete(track
, tid
))) return err
;
212 /* see if the track has evaporated */
213 if(check(c
->module
, c
->root
, path
) == 0) {
214 D(("obsoleting %s", track
));
215 if((err
= trackdb_obsolete(track
, tid
))) return err
;
219 /* make sure we know the length */
220 if(!kvp_get(data
, "_length")) {
221 D(("recalculating length of %s", track
));
222 n
= tracklength(track
, path
);
224 byte_snprintf(buffer
, sizeof buffer
, "%ld", n
);
225 kvp_set(&data
, "_length", buffer
);
226 if((err
= trackdb_putdata(trackdb_tracksdb
, track
, data
, tid
, 0)))
234 /* recheck a collection */
235 static void recheck_collection(const struct collection
*c
) {
236 struct recheck_state cs
;
239 info("rechecking %s", c
->root
);
241 info("rechecking all tracks");
244 global_tid
= trackdb_begin_transaction();
245 memset(&cs
, 0, sizeof cs
);
247 if(trackdb_scan(c ? c
->root
: 0, recheck_callback
, &cs
, global_tid
))
251 /* Maybe we need to shut down */
253 /* Abort the transaction and try again in a bit. */
254 trackdb_abort_transaction(global_tid
);
256 /* Let anything else that is going on get out of the way. */
260 info("resuming recheck of %s", c
->root
);
262 info("resuming global recheck");
264 trackdb_commit_transaction(global_tid
);
267 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
268 c
->root
, cs
.nobsolete
, cs
.nlength
);
270 info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
271 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
274 /* rescan/recheck a collection by name */
275 static void do_directory(const char *s
,
276 void (*fn
)(const struct collection
*c
)) {
279 for(n
= 0; (n
< config
->collection
.n
280 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
282 if(n
< config
->collection
.n
)
283 fn(&config
->collection
.s
[n
]);
285 error(0, "no collection has root '%s'", s
);
288 /* rescan/recheck all collections */
289 static void do_all(void (*fn
)(const struct collection
*c
)) {
292 for(n
= 0; n
< config
->collection
.n
; ++n
)
293 fn(&config
->collection
.s
[n
]);
294 /* TODO: we need to tidy up tracks from collections now removed. We could do
295 * this two ways: either remember collections we think there are and spot
296 * their disappearance, or iterate over all tracks and gc any that don't fit
297 * into some collection.
299 * Having a way to rename collections would be rather convenient too but
300 * that's another kettle of monkeys.
304 int main(int argc
, char **argv
) {
310 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
311 while((n
= getopt_long(argc
, argv
, "hVc:dD", options
, 0)) >= 0) {
315 case 'c': configfile
= optarg
; break;
316 case 'd': debugging
= 1; break;
317 case 'D': debugging
= 0; break;
318 default: fatal(0, "invalid option");
321 /* If stderr is a TTY then log there, otherwise to syslog. */
323 openlog(progname
, LOG_PID
, LOG_DAEMON
);
324 log_default
= &log_syslog
;
326 if(config_read()) fatal(0, "cannot read configuration");
327 xnice(config
->nice_rescan
);
328 sa
.sa_handler
= signal_handler
;
329 sa
.sa_flags
= SA_RESTART
;
330 sigemptyset(&sa
.sa_mask
);
331 xsigaction(SIGTERM
, &sa
, 0);
332 xsigaction(SIGINT
, &sa
, 0);
337 /* Rescan all collections */
338 do_all(rescan_collection
);
339 /* Check that every track still exists */
340 recheck_collection(0);
343 /* Rescan specified collections */
344 for(n
= optind
; n
< argc
; ++n
)
345 do_directory(argv
[n
], rescan_collection
);
346 /* Check specified collections for tracks that have gone */
347 for(n
= optind
; n
< argc
; ++n
)
348 do_directory(argv
[n
], recheck_collection
);