2 * This file is part of DisOrder
3 * Copyright (C) 2007, 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 #include "disorder-server.h"
20 static DB_TXN *global_tid;
24 #define BADKEY_DELETE 2
26 /** @brief Bad key behavior */
27 static int badkey = BADKEY_WARN;
29 static long aliases_removed, keys_normalized, values_normalized, renoticed;
30 static long keys_already_ok, values_already_ok;
32 static const struct option options[] = {
33 { "help", no_argument, 0, 'h' },
34 { "version", no_argument, 0, 'V' },
35 { "config", required_argument, 0, 'c' },
36 { "debug", no_argument, 0, 'd' },
37 { "no-debug", no_argument, 0, 'D' },
38 { "delete-bad-keys", no_argument, 0, 'x' },
39 { "fail-bad-keys", no_argument, 0, 'X' },
40 { "syslog", no_argument, 0, 's' },
41 { "no-syslog", no_argument, 0, 'S' },
45 /* display usage message and terminate */
46 static void help(void) {
48 " disorder-dbupgrade [OPTIONS]\n"
50 " --help, -h Display usage message\n"
51 " --version, -V Display version number\n"
52 " --config PATH, -c PATH Set configuration file\n"
53 " --debug, -d Turn on debugging\n"
54 " --[no-]syslog Force logging\n"
55 " --delete-bad-keys, -x Delete unconvertible keys\n"
56 " --fail-bad-keys, -X Fail if bad keys are found\n"
58 "Database upgrader for DisOrder. Not intended to be run\n"
64 /** @brief Visit each key in a database and call @p callback
65 * @return 0 or DB_LOCK_DEADLOCK
67 * @p global_tid must be set. @p callback should return 0 or DB_LOCK_DEADLOCK.
69 static int scan_core(const char *name, DB *db,
70 int (*callback)(const char *name, DB *db, DBC *c,
73 DBC *c = trackdb_opencursor(db, global_tid);
77 values_normalized = 0;
82 values_already_ok = 0;
83 memset(k, 0, sizeof k);
84 memset(d, 0, sizeof d);
85 while((err = c->c_get(c, k, d, DB_NEXT)) == 0) {
86 if((err = callback(name, db, c, k, d)))
90 info("scanning %s, %ld so far", name, count);
92 if(err && err != DB_NOTFOUND && err != DB_LOCK_DEADLOCK)
93 fatal(0, "%s: error scanning database: %s", name, db_strerror(err));
94 r = (err == DB_LOCK_DEADLOCK ? err : 0);
95 if((err = c->c_close(c)))
96 fatal(0, "%s: error closing cursor: %s", name, db_strerror(err));
97 info("%s: %ld entries scanned", name, count);
98 if(values_normalized || values_already_ok)
99 info("%s: %ld values converted, %ld already ok", name,
100 values_normalized, values_already_ok);
101 if(keys_normalized || keys_already_ok)
102 info("%s: %ld keys converted, %ld already OK", name,
103 keys_normalized, keys_already_ok);
105 info("%s: %ld aliases removed", name, aliases_removed);
107 info("%s: %ld tracks re-noticed", name, renoticed);
111 /** @brief Visit each key in a database and call @p callback
113 * Everything happens inside the @p global_tid tranasction. @p callback
114 * should return 0 or DB_LOCK_DEADLOCK.
116 static void scandb(const char *name, DB *db,
117 int (*callback)(const char *name, DB *db, DBC *c,
119 info("scanning %s", name);
121 global_tid = trackdb_begin_transaction();
122 if(scan_core(name, db, callback)) {
123 trackdb_abort_transaction(global_tid);
125 error(0, "detected deadlock, restarting scan");
128 trackdb_commit_transaction(global_tid);
135 /** @brief Truncate database @p db */
136 static void truncate_database(const char *name, DB *db) {
141 err = db->truncate(db, 0, &count, DB_AUTO_COMMIT);
142 } while(err == DB_LOCK_DEADLOCK);
144 fatal(0, "error truncating %s: %s", name, db_strerror(err));
149 static int normalize_keys(const char *name, DB *db, DBC *c,
155 /* Find the normalized form of the key */
156 knfc = utf8_compose_canon(k->data, k->size, &nknfc);
160 error(0, "%s: invalid key: %.*s", name,
161 (int)k->size, (const char *)k->data);
164 error(0, "%s: deleting invalid key: %.*s", name,
165 (int)k->size, (const char *)k->data);
166 if((err = c->c_del(c, 0))) {
167 if(err != DB_LOCK_DEADLOCK)
168 fatal(0, "%s: error removing denormalized key: %s",
169 name, db_strerror(err));
174 fatal(0, "%s: invalid key: %.*s", name,
175 (int)k->size, (const char *)k->data);
179 /* If the key is already in NFC then do nothing */
180 if(nknfc == k->size && !memcmp(k->data, knfc, nknfc)) {
184 /* To rename the key we must delete the old one and insert a new one */
185 if((err = c->c_del(c, 0))) {
186 if(err != DB_LOCK_DEADLOCK)
187 fatal(0, "%s: error removing denormalized key: %s",
188 name, db_strerror(err));
193 if((err = db->put(db, global_tid, k, d, DB_NOOVERWRITE))) {
194 if(err != DB_LOCK_DEADLOCK)
195 fatal(0, "%s: error storing normalized key: %s", name, db_strerror(err));
202 static int normalize_values(const char *name, DB *db,
203 DBC attribute((unused)) *c,
209 /* Find the normalized form of the value */
210 dnfc = utf8_compose_canon(d->data, d->size, &ndnfc);
212 fatal(0, "%s: cannot convert data to NFC: %.*s", name,
213 (int)d->size, (const char *)d->data);
214 /* If the key is already in NFC then do nothing */
215 if(ndnfc == d->size && !memcmp(d->data, dnfc, ndnfc)) {
221 if((err = db->put(db, global_tid, k, d, 0))) {
222 if(err != DB_LOCK_DEADLOCK)
223 fatal(0, "%s: error storing normalized data: %s", name, db_strerror(err));
230 static int renotice(const char *name, DB attribute((unused)) *db,
231 DBC attribute((unused)) *c,
233 const struct kvp *const t = kvp_urldecode(d->data, d->size);
234 const char *const track = xstrndup(k->data, k->size);
235 const char *const path = kvp_get(t, "_path");
239 /* If an alias sorts later than the actual filename then it'll appear
241 if(kvp_get(t, "_alias_for"))
243 fatal(0, "%s: no '_path' for %.*s", name,
244 (int)k->size, (const char *)k->data);
246 switch(err = trackdb_notice_tid(track, path, global_tid)) {
250 case DB_LOCK_DEADLOCK:
253 fatal(0, "%s: unexpected return from trackdb_notice_tid: %s",
254 name, db_strerror(err));
258 static int remove_aliases_normalize_keys(const char *name, DB *db, DBC *c,
260 const struct kvp *const t = kvp_urldecode(d->data, d->size);
263 if(kvp_get(t, "_alias_for")) {
264 /* This is an alias. We remove all the alias entries. */
265 if((err = c->c_del(c, 0))) {
266 if(err != DB_LOCK_DEADLOCK)
267 fatal(0, "%s: error removing alias: %s", name, db_strerror(err));
272 } else if(!kvp_get(t, "_path"))
273 error(0, "%s: %.*s has neither _alias_for nor _path", name,
274 (int)k->size, (const char *)k->data);
275 return normalize_keys(name, db, c, k, d);
278 /** @brief Upgrade the database to the current version
280 * This function is supposed to be idempotent, so if it is interrupted
281 * half way through it is safe to restart.
283 static void upgrade(void) {
286 info("upgrading database to dbversion %ld", config->dbversion);
287 /* Normalize keys and values as required. We will also remove aliases as
288 * they will be regenerated when we re-noticed the tracks. */
289 info("renormalizing keys");
290 scandb("tracks.db", trackdb_tracksdb, remove_aliases_normalize_keys);
291 scandb("prefs.db", trackdb_prefsdb, normalize_keys);
292 scandb("global.db", trackdb_globaldb, normalize_keys);
293 scandb("noticed.db", trackdb_noticeddb, normalize_values);
294 /* search.db and tags.db we will rebuild */
295 info("regenerating search database and aliases");
296 truncate_database("search.db", trackdb_searchdb);
297 truncate_database("tags.db", trackdb_tagsdb);
298 /* Regenerate the search database and aliases */
299 scandb("tracks.db", trackdb_tracksdb, renotice);
300 /* Finally update the database version */
301 snprintf(buf, sizeof buf, "%ld", config->dbversion);
302 trackdb_set_global("_dbversion", buf, 0);
303 info("completed database upgrade");
306 int main(int argc, char **argv) {
307 int n, logsyslog = !isatty(2);
311 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
312 while((n = getopt_long(argc, argv, "hVc:dDSsxX", options, 0)) >= 0) {
315 case 'V': version("disorder-dbupgrade");
316 case 'c': configfile = optarg; break;
317 case 'd': debugging = 1; break;
318 case 'D': debugging = 0; break;
319 case 'S': logsyslog = 0; break;
320 case 's': logsyslog = 1; break;
321 case 'x': badkey = BADKEY_DELETE; break;
322 case 'X': badkey = BADKEY_FAIL; break;
323 default: fatal(0, "invalid option");
326 /* If stderr is a TTY then log there, otherwise to syslog. */
328 openlog(progname, LOG_PID, LOG_DAEMON);
329 log_default = &log_syslog;
331 if(config_read(0)) fatal(0, "cannot read configuration");
332 /* Open the database */
333 trackdb_init(TRACKDB_NO_RECOVER);
334 trackdb_open(TRACKDB_OPEN_FOR_UPGRADE);