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 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"
45 static DB_TXN *global_tid;
49 #define BADKEY_DELETE 2
51 /** @brief Bad key behavior */
52 static int badkey = BADKEY_WARN;
54 static long aliases_removed, keys_normalized, values_normalized, renoticed;
55 static long keys_already_ok, values_already_ok;
57 static const struct option options[] = {
58 { "help", no_argument, 0, 'h' },
59 { "version", no_argument, 0, 'V' },
60 { "config", required_argument, 0, 'c' },
61 { "debug", no_argument, 0, 'd' },
62 { "no-debug", no_argument, 0, 'D' },
63 { "delete-bad-keys", no_argument, 0, 'x' },
64 { "fail-bad-keys", no_argument, 0, 'X' },
65 { "syslog", no_argument, 0, 's' },
66 { "no-syslog", no_argument, 0, 'S' },
70 /* display usage message and terminate */
71 static void help(void) {
73 " disorder-dbupgrade [OPTIONS]\n"
75 " --help, -h Display usage message\n"
76 " --version, -V Display version number\n"
77 " --config PATH, -c PATH Set configuration file\n"
78 " --debug, -d Turn on debugging\n"
79 " --[no-]syslog Force logging\n"
80 " --delete-bad-keys, -x Delete unconvertible keys\n"
81 " --fail-bad-keys, -X Fail if bad keys are found\n"
83 "Database upgrader for DisOrder. Not intended to be run\n"
89 /** @brief Visit each key in a database and call @p callback
90 * @return 0 or DB_LOCK_DEADLOCK
92 * @p global_tid must be set. @p callback should return 0 or DB_LOCK_DEADLOCK.
94 static int scan_core(const char *name, DB *db,
95 int (*callback)(const char *name, DB *db, DBC *c,
98 DBC *c = trackdb_opencursor(db, global_tid);
102 values_normalized = 0;
107 values_already_ok = 0;
108 memset(k, 0, sizeof k);
109 memset(d, 0, sizeof d);
110 while((err = c->c_get(c, k, d, DB_NEXT)) == 0) {
111 if((err = callback(name, db, c, k, d)))
114 if(count % 1000 == 0)
115 info("scanning %s, %ld so far", name, count);
117 if(err && err != DB_NOTFOUND && err != DB_LOCK_DEADLOCK)
118 fatal(0, "%s: error scanning database: %s", name, db_strerror(err));
119 r = (err == DB_LOCK_DEADLOCK ? err : 0);
120 if((err = c->c_close(c)))
121 fatal(0, "%s: error closing cursor: %s", name, db_strerror(err));
122 info("%s: %ld entries scanned", name, count);
123 if(values_normalized || values_already_ok)
124 info("%s: %ld values converted, %ld already ok", name,
125 values_normalized, values_already_ok);
126 if(keys_normalized || keys_already_ok)
127 info("%s: %ld keys converted, %ld already OK", name,
128 keys_normalized, keys_already_ok);
130 info("%s: %ld aliases removed", name, aliases_removed);
132 info("%s: %ld tracks re-noticed", name, renoticed);
136 /** @brief Visit each key in a database and call @p callback
138 * Everything happens inside the @p global_tid tranasction. @p callback
139 * should return 0 or DB_LOCK_DEADLOCK.
141 static void scan(const char *name, DB *db,
142 int (*callback)(const char *name, DB *db, DBC *c,
144 info("scanning %s", name);
146 global_tid = trackdb_begin_transaction();
147 if(scan_core(name, db, callback)) {
148 trackdb_abort_transaction(global_tid);
150 error(0, "detected deadlock, restarting scan");
153 trackdb_commit_transaction(global_tid);
160 /** @brief Truncate database @p db */
161 static void truncate_database(const char *name, DB *db) {
166 err = db->truncate(db, 0, &count, DB_AUTO_COMMIT);
167 } while(err == DB_LOCK_DEADLOCK);
169 fatal(0, "error truncating %s: %s", name, db_strerror(err));
174 static int normalize_keys(const char *name, DB *db, DBC *c,
180 /* Find the normalized form of the key */
181 knfc = utf8_compose_canon(k->data, k->size, &nknfc);
185 error(0, "%s: invalid key: %.*s", name,
186 (int)k->size, (const char *)k->data);
189 error(0, "%s: deleting invalid key: %.*s", name,
190 (int)k->size, (const char *)k->data);
191 if((err = c->c_del(c, 0))) {
192 if(err != DB_LOCK_DEADLOCK)
193 fatal(0, "%s: error removing denormalized key: %s",
194 name, db_strerror(err));
199 fatal(0, "%s: invalid key: %.*s", name,
200 (int)k->size, (const char *)k->data);
204 /* If the key is already in NFC then do nothing */
205 if(nknfc == k->size && !memcmp(k->data, knfc, nknfc)) {
209 /* To rename the key we must delete the old one and insert a new one */
210 if((err = c->c_del(c, 0))) {
211 if(err != DB_LOCK_DEADLOCK)
212 fatal(0, "%s: error removing denormalized key: %s",
213 name, db_strerror(err));
218 if((err = db->put(db, global_tid, k, d, DB_NOOVERWRITE))) {
219 if(err != DB_LOCK_DEADLOCK)
220 fatal(0, "%s: error storing normalized key: %s", name, db_strerror(err));
227 static int normalize_values(const char *name, DB *db,
228 DBC attribute((unused)) *c,
234 /* Find the normalized form of the value */
235 dnfc = utf8_compose_canon(d->data, d->size, &ndnfc);
237 fatal(0, "%s: cannot convert data to NFC: %.*s", name,
238 (int)d->size, (const char *)d->data);
239 /* If the key is already in NFC then do nothing */
240 if(ndnfc == d->size && !memcmp(d->data, dnfc, ndnfc)) {
246 if((err = db->put(db, global_tid, k, d, 0))) {
247 if(err != DB_LOCK_DEADLOCK)
248 fatal(0, "%s: error storing normalized data: %s", name, db_strerror(err));
255 static int renotice(const char *name, DB attribute((unused)) *db,
256 DBC attribute((unused)) *c,
258 const struct kvp *const t = kvp_urldecode(d->data, d->size);
259 const char *const track = xstrndup(k->data, k->size);
260 const char *const path = kvp_get(t, "_path");
264 /* If an alias sorts later than the actual filename then it'll appear
266 if(kvp_get(t, "_alias_for"))
268 fatal(0, "%s: no '_path' for %.*s", name,
269 (int)k->size, (const char *)k->data);
271 switch(err = trackdb_notice_tid(track, path, global_tid)) {
275 case DB_LOCK_DEADLOCK:
278 fatal(0, "%s: unexpected return from trackdb_notice_tid: %s",
279 name, db_strerror(err));
283 static int remove_aliases_normalize_keys(const char *name, DB *db, DBC *c,
285 const struct kvp *const t = kvp_urldecode(d->data, d->size);
288 if(kvp_get(t, "_alias_for")) {
289 /* This is an alias. We remove all the alias entries. */
290 if((err = c->c_del(c, 0))) {
291 if(err != DB_LOCK_DEADLOCK)
292 fatal(0, "%s: error removing alias: %s", name, db_strerror(err));
297 } else if(!kvp_get(t, "_path"))
298 error(0, "%s: %.*s has neither _alias_for nor _path", name,
299 (int)k->size, (const char *)k->data);
300 return normalize_keys(name, db, c, k, d);
303 /** @brief Upgrade the database to the current version
305 * This function is supposed to be idempotent, so if it is interrupted
306 * half way through it is safe to restart.
308 static void upgrade(void) {
311 info("upgrading database to dbversion %ld", config->dbversion);
312 /* Normalize keys and values as required. We will also remove aliases as
313 * they will be regenerated when we re-noticed the tracks. */
314 info("renormalizing keys");
315 scan("tracks.db", trackdb_tracksdb, remove_aliases_normalize_keys);
316 scan("prefs.db", trackdb_prefsdb, normalize_keys);
317 scan("global.db", trackdb_globaldb, normalize_keys);
318 scan("noticed.db", trackdb_noticeddb, normalize_values);
319 /* search.db and tags.db we will rebuild */
320 info("regenerating search database and aliases");
321 truncate_database("search.db", trackdb_searchdb);
322 truncate_database("tags.db", trackdb_tagsdb);
323 /* Regenerate the search database and aliases */
324 scan("tracks.db", trackdb_tracksdb, renotice);
325 /* Finally update the database version */
326 snprintf(buf, sizeof buf, "%ld", config->dbversion);
327 trackdb_set_global("_dbversion", buf, 0);
328 info("completed database upgrade");
331 int main(int argc, char **argv) {
332 int n, logsyslog = !isatty(2);
336 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
337 while((n = getopt_long(argc, argv, "hVc:dDSsxX", options, 0)) >= 0) {
340 case 'V': version("disorder-dbupgrade");
341 case 'c': configfile = optarg; break;
342 case 'd': debugging = 1; break;
343 case 'D': debugging = 0; break;
344 case 'S': logsyslog = 0; break;
345 case 's': logsyslog = 1; break;
346 case 'x': badkey = BADKEY_DELETE; break;
347 case 'X': badkey = BADKEY_FAIL; break;
348 default: fatal(0, "invalid option");
351 /* If stderr is a TTY then log there, otherwise to syslog. */
353 openlog(progname, LOG_PID, LOG_DAEMON);
354 log_default = &log_syslog;
356 if(config_read(0)) fatal(0, "cannot read configuration");
357 /* Open the database */
358 trackdb_init(TRACKDB_NO_RECOVER);
359 trackdb_open(TRACKDB_OPEN_FOR_UPGRADE);