2 * This file is part of DisOrder
3 * Copyright (C) 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
39 #include "trackdb-int.h"
41 #include "configuration.h"
44 static DB_TXN
*global_tid
;
48 #define BADKEY_DELETE 2
50 /** @brief Bad key behavior */
51 static int badkey
= BADKEY_WARN
;
53 static long aliases_removed
, keys_normalized
, values_normalized
, renoticed
;
54 static long keys_already_ok
, values_already_ok
;
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' },
62 { "delete-bad-keys", no_argument
, 0, 'x' },
63 { "fail-bad-keys", no_argument
, 0, 'X' },
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-dbupgrade [OPTIONS]\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"
79 " --delete-bad-keys, -x Delete unconvertible keys\n"
80 " --fail-bad-keys, -X Fail if bad keys are found\n"
82 "Database upgrader for DisOrder. Not intended to be run\n"
88 /* display version number and terminate */
89 static void version(void) {
90 xprintf("%s", disorder_version_string
);
95 /** @brief Visit each key in a database and call @p callback
96 * @return 0 or DB_LOCK_DEADLOCK
98 * @p global_tid must be set. @p callback should return 0 or DB_LOCK_DEADLOCK.
100 static int scan_core(const char *name
, DB
*db
,
101 int (*callback
)(const char *name
, DB
*db
, DBC
*c
,
104 DBC
*c
= trackdb_opencursor(db
, global_tid
);
108 values_normalized
= 0;
113 values_already_ok
= 0;
114 memset(k
, 0, sizeof k
);
115 memset(d
, 0, sizeof d
);
116 while((err
= c
->c_get(c
, k
, d
, DB_NEXT
)) == 0) {
117 if((err
= callback(name
, db
, c
, k
, d
)))
120 if(count
% 1000 == 0)
121 info("scanning %s, %ld so far", name
, count
);
123 if(err
&& err
!= DB_NOTFOUND
&& err
!= DB_LOCK_DEADLOCK
)
124 fatal(0, "%s: error scanning database: %s", name
, db_strerror(err
));
125 r
= (err
== DB_LOCK_DEADLOCK ? err
: 0);
126 if((err
= c
->c_close(c
)))
127 fatal(0, "%s: error closing cursor: %s", name
, db_strerror(err
));
128 info("%s: %ld entries scanned", name
, count
);
129 if(values_normalized
|| values_already_ok
)
130 info("%s: %ld values converted, %ld already ok", name
,
131 values_normalized
, values_already_ok
);
132 if(keys_normalized
|| keys_already_ok
)
133 info("%s: %ld keys converted, %ld already OK", name
,
134 keys_normalized
, keys_already_ok
);
136 info("%s: %ld aliases removed", name
, aliases_removed
);
138 info("%s: %ld tracks re-noticed", name
, renoticed
);
142 /** @brief Visit each key in a database and call @p callback
144 * Everything happens inside the @p global_tid tranasction. @p callback
145 * should return 0 or DB_LOCK_DEADLOCK.
147 static void scan(const char *name
, DB
*db
,
148 int (*callback
)(const char *name
, DB
*db
, DBC
*c
,
150 info("scanning %s", name
);
152 global_tid
= trackdb_begin_transaction();
153 if(scan_core(name
, db
, callback
)) {
154 trackdb_abort_transaction(global_tid
);
156 error(0, "detected deadlock, restarting scan");
159 trackdb_commit_transaction(global_tid
);
166 /** @brief Truncate database @p db */
167 static void truncate_database(const char *name
, DB
*db
) {
172 err
= db
->truncate(db
, 0, &count
, DB_AUTO_COMMIT
);
173 } while(err
== DB_LOCK_DEADLOCK
);
175 fatal(0, "error truncating %s: %s", name
, db_strerror(err
));
180 static int normalize_keys(const char *name
, DB
*db
, DBC
*c
,
186 /* Find the normalized form of the key */
187 knfc
= utf8_compose_canon(k
->data
, k
->size
, &nknfc
);
191 error(0, "%s: invalid key: %.*s", name
,
192 (int)k
->size
, (const char *)k
->data
);
195 error(0, "%s: deleting invalid key: %.*s", name
,
196 (int)k
->size
, (const char *)k
->data
);
197 if((err
= c
->c_del(c
, 0))) {
198 if(err
!= DB_LOCK_DEADLOCK
)
199 fatal(0, "%s: error removing denormalized key: %s",
200 name
, db_strerror(err
));
205 fatal(0, "%s: invalid key: %.*s", name
,
206 (int)k
->size
, (const char *)k
->data
);
210 /* If the key is already in NFC then do nothing */
211 if(nknfc
== k
->size
&& !memcmp(k
->data
, knfc
, nknfc
)) {
215 /* To rename the key we must delete the old one and insert a new one */
216 if((err
= c
->c_del(c
, 0))) {
217 if(err
!= DB_LOCK_DEADLOCK
)
218 fatal(0, "%s: error removing denormalized key: %s",
219 name
, db_strerror(err
));
224 if((err
= db
->put(db
, global_tid
, k
, d
, DB_NOOVERWRITE
))) {
225 if(err
!= DB_LOCK_DEADLOCK
)
226 fatal(0, "%s: error storing normalized key: %s", name
, db_strerror(err
));
233 static int normalize_values(const char *name
, DB
*db
,
234 DBC
attribute((unused
)) *c
,
240 /* Find the normalized form of the value */
241 dnfc
= utf8_compose_canon(d
->data
, d
->size
, &ndnfc
);
243 fatal(0, "%s: cannot convert data to NFC: %.*s", name
,
244 (int)d
->size
, (const char *)d
->data
);
245 /* If the key is already in NFC then do nothing */
246 if(ndnfc
== d
->size
&& !memcmp(d
->data
, dnfc
, ndnfc
)) {
252 if((err
= db
->put(db
, global_tid
, k
, d
, 0))) {
253 if(err
!= DB_LOCK_DEADLOCK
)
254 fatal(0, "%s: error storing normalized data: %s", name
, db_strerror(err
));
261 static int renotice(const char *name
, DB
attribute((unused
)) *db
,
262 DBC
attribute((unused
)) *c
,
264 const struct kvp
*const t
= kvp_urldecode(d
->data
, d
->size
);
265 const char *const track
= xstrndup(k
->data
, k
->size
);
266 const char *const path
= kvp_get(t
, "_path");
270 /* If an alias sorts later than the actual filename then it'll appear
272 if(kvp_get(t
, "_alias_for"))
274 fatal(0, "%s: no '_path' for %.*s", name
,
275 (int)k
->size
, (const char *)k
->data
);
277 switch(err
= trackdb_notice_tid(track
, path
, global_tid
)) {
281 case DB_LOCK_DEADLOCK
:
284 fatal(0, "%s: unexpected return from trackdb_notice_tid: %s",
285 name
, db_strerror(err
));
289 static int remove_aliases_normalize_keys(const char *name
, DB
*db
, DBC
*c
,
291 const struct kvp
*const t
= kvp_urldecode(d
->data
, d
->size
);
294 if(kvp_get(t
, "_alias_for")) {
295 /* This is an alias. We remove all the alias entries. */
296 if((err
= c
->c_del(c
, 0))) {
297 if(err
!= DB_LOCK_DEADLOCK
)
298 fatal(0, "%s: error removing alias: %s", name
, db_strerror(err
));
303 } else if(!kvp_get(t
, "_path"))
304 error(0, "%s: %.*s has neither _alias_for nor _path", name
,
305 (int)k
->size
, (const char *)k
->data
);
306 return normalize_keys(name
, db
, c
, k
, d
);
309 /** @brief Upgrade the database to the current version
311 * This function is supposed to be idempotent, so if it is interrupted
312 * half way through it is safe to restart.
314 static void upgrade(void) {
317 info("upgrading database to dbversion %ld", config
->dbversion
);
318 /* Normalize keys and values as required. We will also remove aliases as
319 * they will be regenerated when we re-noticed the tracks. */
320 info("renormalizing keys");
321 scan("tracks.db", trackdb_tracksdb
, remove_aliases_normalize_keys
);
322 scan("prefs.db", trackdb_prefsdb
, normalize_keys
);
323 scan("global.db", trackdb_globaldb
, normalize_keys
);
324 scan("noticed.db", trackdb_noticeddb
, normalize_values
);
325 /* search.db and tags.db we will rebuild */
326 info("regenerating search database and aliases");
327 truncate_database("search.db", trackdb_searchdb
);
328 truncate_database("tags.db", trackdb_tagsdb
);
329 /* Regenerate the search database and aliases */
330 scan("tracks.db", trackdb_tracksdb
, renotice
);
331 /* Finally update the database version */
332 snprintf(buf
, sizeof buf
, "%ld", config
->dbversion
);
333 trackdb_set_global("_dbversion", buf
, 0);
334 info("completed database upgrade");
337 int main(int argc
, char **argv
) {
338 int n
, logsyslog
= !isatty(2);
342 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
343 while((n
= getopt_long(argc
, argv
, "hVc:dDSsxX", options
, 0)) >= 0) {
347 case 'c': configfile
= optarg
; break;
348 case 'd': debugging
= 1; break;
349 case 'D': debugging
= 0; break;
350 case 'S': logsyslog
= 0; break;
351 case 's': logsyslog
= 1; break;
352 case 'x': badkey
= BADKEY_DELETE
; break;
353 case 'X': badkey
= BADKEY_FAIL
; break;
354 default: fatal(0, "invalid option");
357 /* If stderr is a TTY then log there, otherwise to syslog. */
359 openlog(progname
, LOG_PID
, LOG_DAEMON
);
360 log_default
= &log_syslog
;
362 if(config_read(0)) fatal(0, "cannot read configuration");
363 /* Open the database */
364 trackdb_init(TRACKDB_NO_RECOVER
);
365 trackdb_open(TRACKDB_OPEN_FOR_UPGRADE
);