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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file server/rescan.c
19 * @brief Rescanning utility
21 * Invoked by the server.
23 #include "disorder-server.h"
25 static time_t last_report
;
26 static DB_TXN
*global_tid
;
28 static const struct option options
[] = {
29 { "help", no_argument
, 0, 'h' },
30 { "version", no_argument
, 0, 'V' },
31 { "config", required_argument
, 0, 'c' },
32 { "debug", no_argument
, 0, 'd' },
33 { "no-debug", no_argument
, 0, 'D' },
34 { "syslog", no_argument
, 0, 's' },
35 { "no-syslog", no_argument
, 0, 'S' },
36 { "check", no_argument
, 0, 'K' },
37 { "no-check", no_argument
, 0, 'C' },
41 /* display usage message and terminate */
42 static void help(void) {
44 " disorder-rescan [OPTIONS] [PATH...]\n"
46 " --help, -h Display usage message\n"
47 " --version, -V Display version number\n"
48 " --config PATH, -c PATH Set configuration file\n"
49 " --debug, -d Turn on debugging\n"
50 " --[no-]syslog Enable/disable logging to syslog\n"
51 " --[no-]check Enable/disable track length check\n"
53 "Rescanner for DisOrder. Not intended to be run\n"
59 static volatile sig_atomic_t signalled
;
61 static void signal_handler(int sig
) {
62 if(sig
== 0) _exit(-1); /* "Cannot happen" */
66 static int aborted(void) {
67 return signalled
|| getppid() == 1;
70 /* Exit if our parent has gone away or we have been told to stop. */
71 static void checkabort(void) {
73 disorder_info("parent has terminated");
74 trackdb_abort_transaction(global_tid
);
78 disorder_info("received signal %d", signalled
);
79 trackdb_abort_transaction(global_tid
);
84 /* rescan a collection */
85 static void rescan_collection(const struct collection
*c
) {
90 long ntracks
= 0, nnew
= 0;
93 disorder_info("rescanning %s with %s", c
->root
, c
->module
);
94 /* plugin runs in a subprocess */
96 if(!(pid
= xfork())) {
101 scan(c
->module
, c
->root
);
102 if(fflush(stdout
) < 0)
103 disorder_fatal(errno
, "error writing to scanner pipe");
107 if(!(fp
= fdopen(p
[0], "r")))
108 disorder_fatal(errno
, "error calling fdopen");
109 /* read tracks from the plugin */
110 while(!inputline("rescanner", fp
, &path
, 0)) {
112 /* actually we can cope relatively well within the server, but they'll go
113 * wrong in track listings */
114 if(strchr(path
, '\n')) {
115 disorder_error(0, "cannot cope with tracks with newlines in the name");
118 if(!(track
= any2utf8(c
->encoding
, path
))) {
119 disorder_error(0, "cannot convert track path to UTF-8: %s", path
);
122 if(config
->dbversion
> 1) {
123 /* We use NFC track names */
124 if(!(track
= utf8_compose_canon(track
, strlen(track
), 0))) {
125 disorder_error(0, "cannot convert track path to NFC: %s", path
);
129 D(("track %s", track
));
130 /* only tracks with a known player are admitted */
131 for(n
= 0; (n
< config
->player
.n
132 && fnmatch(config
->player
.s
[n
].s
[0], track
, 0) != 0); ++n
)
134 if(n
< config
->player
.n
) {
135 nnew
+= !!trackdb_notice(track
, path
);
137 if(ntracks
% 100 == 0 && xtime(0) > last_report
+ 10) {
138 disorder_info("rescanning %s, %ld tracks so far", c
->root
, ntracks
);
145 disorder_error(errno
, "error reading from scanner pipe");
150 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
152 if(r
< 0) disorder_fatal(errno
, "error calling waitpid");
155 disorder_error(0, "scanner subprocess: %s", wstat(w
));
158 disorder_info("rescanned %s, %ld tracks, %ld new", c
->root
, ntracks
, nnew
);
163 while((r
= waitpid(pid
, &w
, 0)) == -1 && errno
== EINTR
)
167 struct recheck_state
{
168 const struct collection
*c
;
169 long nobsolete
, nnocollection
, nlength
;
170 struct recheck_track
*tracks
;
173 struct recheck_track
{
174 struct recheck_track
*next
;
178 /* called for each non-alias track */
179 static int recheck_list_callback(const char *track
,
180 struct kvp
attribute((unused
)) *data
,
181 struct kvp
attribute((unused
)) *prefs
,
183 DB_TXN
attribute((unused
)) *tid
) {
184 struct recheck_state
*cs
= u
;
185 struct recheck_track
*t
= xmalloc(sizeof *t
);
187 t
->next
= cs
->tracks
;
193 static int recheck_track_tid(struct recheck_state
*cs
,
194 const struct recheck_track
*t
,
196 const struct collection
*c
= cs
->c
;
203 if((err
= trackdb_getdata(trackdb_tracksdb
, t
->track
, &data
, tid
)))
205 path
= kvp_get(data
, "_path");
206 D(("rechecking %s", t
->track
));
207 /* if we're not checking a specific collection, find the right collection */
209 if(!(c
= find_track_collection(t
->track
))) {
210 D(("obsoleting %s", t
->track
));
211 if((err
= trackdb_obsolete(t
->track
, tid
)))
217 /* see if the track has evaporated */
218 if(check(c
->module
, c
->root
, path
) == 0) {
219 D(("obsoleting %s", t
->track
));
220 if((err
= trackdb_obsolete(t
->track
, tid
)))
225 /* make sure we know the length */
226 if(!kvp_get(data
, "_length")) {
227 D(("recalculating length of %s", t
->track
));
228 for(n
= 0; n
< config
->tracklength
.n
; ++n
)
229 if(fnmatch(config
->tracklength
.s
[n
].s
[0], t
->track
, 0) == 0)
231 if(n
>= config
->tracklength
.n
)
232 disorder_error(0, "no tracklength plugin found for %s", t
->track
);
234 length
= tracklength(config
->tracklength
.s
[n
].s
[1], t
->track
, path
);
236 byte_snprintf(buffer
, sizeof buffer
, "%ld", length
);
237 kvp_set(&data
, "_length", buffer
);
238 if((err
= trackdb_putdata(trackdb_tracksdb
, t
->track
, data
, tid
, 0)))
247 static int recheck_track(struct recheck_state
*cs
,
248 const struct recheck_track
*t
) {
251 WITH_TRANSACTION(recheck_track_tid(cs
, t
, tid
));
255 /* recheck a collection */
256 static void recheck_collection(const struct collection
*c
) {
257 struct recheck_state cs
;
258 const struct recheck_track
*t
;
262 disorder_info("rechecking %s", c
->root
);
264 disorder_info("rechecking all tracks");
265 /* Doing the checking inside a transaction locks up the server for much too
266 * long (because it spends lots of time thinking about each track). So we
267 * pull the full track list into memory and work from that.
269 * 100,000 tracks at, say, 80 bytes per track name, gives 8MB, which is quite
274 disorder_info("getting track list");
275 global_tid
= trackdb_begin_transaction();
276 memset(&cs
, 0, sizeof cs
);
278 if(trackdb_scan(c ? c
->root
: 0, recheck_list_callback
, &cs
, global_tid
))
282 /* Maybe we need to shut down */
284 /* Abort the transaction and try again in a bit. */
285 trackdb_abort_transaction(global_tid
);
287 /* Let anything else that is going on get out of the way. */
291 disorder_info("resuming recheck of %s", c
->root
);
293 disorder_info("resuming global recheck");
295 trackdb_commit_transaction(global_tid
);
298 for(t
= cs
.tracks
; t
; t
= t
->next
) {
301 recheck_track(&cs
, t
);
303 if(nrc
% 100 == 0 && xtime(0) > last_report
+ 10) {
305 disorder_info("rechecking %s, %ld tracks so far", c
->root
, nrc
);
307 disorder_info("rechecking all tracks, %ld tracks so far", nrc
);
312 disorder_info("rechecked %s, %ld obsoleted, %ld lengths calculated",
313 c
->root
, cs
.nobsolete
, cs
.nlength
);
315 disorder_info("rechecked all tracks, %ld no collection, %ld obsoleted, %ld lengths calculated",
316 cs
.nnocollection
, cs
.nobsolete
, cs
.nlength
);
319 /* rescan/recheck a collection by name */
320 static void do_directory(const char *s
,
321 void (*fn
)(const struct collection
*c
)) {
324 for(n
= 0; (n
< config
->collection
.n
325 && strcmp(config
->collection
.s
[n
].root
, s
)); ++n
)
327 if(n
< config
->collection
.n
)
328 fn(&config
->collection
.s
[n
]);
330 disorder_error(0, "no collection has root '%s'", s
);
333 /* rescan/recheck all collections */
334 static void do_all(void (*fn
)(const struct collection
*c
)) {
337 for(n
= 0; n
< config
->collection
.n
; ++n
)
338 fn(&config
->collection
.s
[n
]);
339 /* TODO: we need to tidy up tracks from collections now removed. We could do
340 * this two ways: either remember collections we think there are and spot
341 * their disappearance, or iterate over all tracks and gc any that don't fit
342 * into some collection.
344 * Having a way to rename collections would be rather convenient too but
345 * that's another kettle of monkeys.
349 /** @brief Expire noticed.db */
350 static void expire_noticed(void) {
354 trackdb_expire_noticed(now
- config
->noticed_history
* 86400);
357 int main(int argc
, char **argv
) {
358 int n
, logsyslog
= !isatty(2);
364 if(!setlocale(LC_CTYPE
, "")) disorder_fatal(errno
, "error calling setlocale");
365 while((n
= getopt_long(argc
, argv
, "hVc:dDSsKC", options
, 0)) >= 0) {
368 case 'V': version("disorder-rescan");
369 case 'c': configfile
= optarg
; break;
370 case 'd': debugging
= 1; break;
371 case 'D': debugging
= 0; break;
372 case 'S': logsyslog
= 0; break;
373 case 's': logsyslog
= 1; break;
374 case 'K': do_check
= 1; break;
375 case 'C': do_check
= 0; break;
376 default: disorder_fatal(0, "invalid option");
380 openlog(progname
, LOG_PID
, LOG_DAEMON
);
381 log_default
= &log_syslog
;
383 if(config_read(0, NULL
)) disorder_fatal(0, "cannot read configuration");
384 xnice(config
->nice_rescan
);
385 sa
.sa_handler
= signal_handler
;
386 sa
.sa_flags
= SA_RESTART
;
387 sigemptyset(&sa
.sa_mask
);
388 xsigaction(SIGTERM
, &sa
, 0);
389 xsigaction(SIGINT
, &sa
, 0);
390 disorder_info("started");
391 trackdb_init(TRACKDB_NO_RECOVER
);
392 trackdb_open(TRACKDB_NO_UPGRADE
);
394 /* Rescan all collections */
395 do_all(rescan_collection
);
396 /* Check that every track still exists */
398 recheck_collection(0);
399 /* Expire noticed.db */
403 /* Rescan specified collections */
404 for(n
= optind
; n
< argc
; ++n
)
405 do_directory(argv
[n
], rescan_collection
);
406 /* Check specified collections for tracks that have gone */
408 for(n
= optind
; n
< argc
; ++n
)
409 do_directory(argv
[n
], recheck_collection
);
412 trackdb_deinit(NULL
);
413 disorder_info("completed");