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"
51 #include "trackdb-int.h"
52 #include "trackname.h"
54 static DB_TXN
*global_tid
;
56 static const struct option options
[] = {
57 { "help", no_argument
, 0, 'h' },
58 { "version", no_argument
, 0, 'V' },
59 { "config", required_argument
, 0, 'c' },
60 { "debug", no_argument
, 0, 'd' },
61 { "no-debug", no_argument
, 0, 'D' },
65 /* display usage message and terminate */
66 static void help(void) {
68 " disorder-rescan [OPTIONS] [PATH...]\n"
70 " --help, -h Display usage message\n"
71 " --version, -V Display version number\n"
72 " --config PATH, -c PATH Set configuration file\n"
73 " --debug, -d Turn on debugging\n"
75 "Rescanner for DisOrder. Not intended to be run\n"
81 /* display version number and terminate */
82 static void version(void) {
83 xprintf("disorder-rescan version %s\n", disorder_version_string
);
88 static volatile sig_atomic_t signalled
;
90 static void signal_handler(int sig
) {
91 if(sig
== 0) _exit(-1); /* "Cannot happen" */
95 static int aborted(void) {
96 return signalled
|| getppid() == 1;
99 /* Exit if our parent has gone away or we have been told to stop. */
100 static void checkabort(void) {
102 info("parent has terminated");
103 trackdb_abort_transaction(global_tid
);
107 info("received signal %d", signalled
);
108 trackdb_abort_transaction(global_tid
);
113 /* rescan a collection */
114 static void rescan_collection(const struct collection
*c
) {
119 long ntracks
= 0, nnew
= 0;
122 info("rescanning %s with %s", c
->root
, c
->module
);
123 /* plugin runs in a subprocess */
125 if(!(pid
= xfork())) {
130 scan(c
->module
, c
->root
);
131 if(fflush(stdout
) < 0)
132 fatal(errno
, "error writing to scanner pipe");
136 if(!(fp
= fdopen(p
[0], "r")))
137 fatal(errno
, "error calling fdopen");
138 /* read tracks from the plugin */
139 while(!inputline("rescanner", fp
, &path
, 0)) {
141 /* actually we can cope relatively well within the server, but they'll go
142 * wrong in track listings */
143 if(strchr(path
, '\n')) {
144 error(0, "cannot cope with tracks with newlines in the name");
147 if(!(track
= any2utf8(c
->encoding
, path
))) {
148 error(0, "cannot convert track path to UTF-8: %s", path
);
151 D(("track %s", track
));
152 /* only tracks with a known player are admitted */
153 for(n
= 0; (n
< config
->player
.n
154 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
156 if(n
< config
->player
.n
) {
157 nnew
+= !!trackdb_notice(track
, path
);
159 if(ntracks
% 1000 == 0)
160 info("rescanning %s, %ld tracks so far", c
->root
, ntracks
);
165 error(errno
, "error reading from scanner pipe");
170 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
172 if(r
< 0) fatal(errno
, "error calling waitpid");
175 error(0, "scanner subprocess: %s", wstat(w
));
178 info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
183 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
187 struct recheck_state
{
188 const struct collection
*c
;
189 long nobsolete
, nnocollection
, nlength
;
192 /* called for each non-alias track */
193 static int recheck_callback(const char *track
,
197 struct recheck_state
*cs
= u
;
198 const struct collection
*c
= cs
->c
;
199 const char *path
= kvp_get(data
, "_path");
204 if(aborted()) return EINTR
;
205 D(("rechecking %s", track
));
206 /* if we're not checking a specific collection, find the right collection */
208 if(!(c
= find_track_collection(track
))) {
209 D(("obsoleting %s", track
));
210 if((err
= trackdb_obsolete(track
, tid
))) return err
;
215 /* see if the track has evaporated */
216 if(check(c
->module
, c
->root
, path
) == 0) {
217 D(("obsoleting %s", track
));
218 if((err
= trackdb_obsolete(track
, tid
))) return err
;
222 /* make sure we know the length */
223 if(!kvp_get(data
, "_length")) {
224 D(("recalculating length of %s", track
));
225 n
= tracklength(track
, path
);
227 byte_snprintf(buffer
, sizeof buffer
, "%ld", n
);
228 kvp_set(&data
, "_length", buffer
);
229 if((err
= trackdb_putdata(trackdb_tracksdb
, track
, data
, tid
, 0)))
237 /* recheck a collection */
238 static void recheck_collection(const struct collection
*c
) {
239 struct recheck_state cs
;
242 info("rechecking %s", c
->root
);
244 info("rechecking all tracks");
247 global_tid
= trackdb_begin_transaction();
248 memset(&cs
, 0, sizeof cs
);
250 if(trackdb_scan(c ? c
->root
: 0, recheck_callback
, &cs
, global_tid
))
254 /* Maybe we need to shut down */
256 /* Abort the transaction and try again in a bit. */
257 trackdb_abort_transaction(global_tid
);
259 /* Let anything else that is going on get out of the way. */
263 info("resuming recheck of %s", c
->root
);
265 info("resuming global recheck");
267 trackdb_commit_transaction(global_tid
);
270 info("rechecked %s, %ld obsoleted, %ld lengths calculated",
271 c
->root
, cs
.nobsolete
, cs
.nlength
);
273 info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
274 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
277 /* rescan/recheck a collection by name */
278 static void do_directory(const char *s
,
279 void (*fn
)(const struct collection
*c
)) {
282 for(n
= 0; (n
< config
->collection
.n
283 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
285 if(n
< config
->collection
.n
)
286 fn(&config
->collection
.s
[n
]);
288 error(0, "no collection has root '%s'", s
);
291 /* rescan/recheck all collections */
292 static void do_all(void (*fn
)(const struct collection
*c
)) {
295 for(n
= 0; n
< config
->collection
.n
; ++n
)
296 fn(&config
->collection
.s
[n
]);
297 /* TODO: we need to tidy up tracks from collections now removed. We could do
298 * this two ways: either remember collections we think there are and spot
299 * their disappearance, or iterate over all tracks and gc any that don't fit
300 * into some collection.
302 * Having a way to rename collections would be rather convenient too but
303 * that's another kettle of monkeys.
307 /** @brief Expire noticed.db */
308 static void expire_noticed(void) {
312 trackdb_expire_noticed(now
- config
->noticed_history
* 86400);
315 int main(int argc
, char **argv
) {
321 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
322 while((n
= getopt_long(argc
, argv
, "hVc:dD", options
, 0)) >= 0) {
326 case 'c': configfile
= optarg
; break;
327 case 'd': debugging
= 1; break;
328 case 'D': debugging
= 0; break;
329 default: fatal(0, "invalid option");
332 /* If stderr is a TTY then log there, otherwise to syslog. */
334 openlog(progname
, LOG_PID
, LOG_DAEMON
);
335 log_default
= &log_syslog
;
337 if(config_read(0)) fatal(0, "cannot read configuration");
338 xnice(config
->nice_rescan
);
339 sa
.sa_handler
= signal_handler
;
340 sa
.sa_flags
= SA_RESTART
;
341 sigemptyset(&sa
.sa_mask
);
342 xsigaction(SIGTERM
, &sa
, 0);
343 xsigaction(SIGINT
, &sa
, 0);
348 /* Rescan all collections */
349 do_all(rescan_collection
);
350 /* Check that every track still exists */
351 recheck_collection(0);
352 /* Expire noticed.db */
356 /* Rescan specified collections */
357 for(n
= optind
; n
< argc
; ++n
)
358 do_directory(argv
[n
], rescan_collection
);
359 /* Check specified collections for tracks that have gone */
360 for(n
= optind
; n
< argc
; ++n
)
361 do_directory(argv
[n
], recheck_collection
);