chiark / gitweb /
Do some basic compatibility checking when installing a new server
[disorder] / server / dbupgrade.c
CommitLineData
3dc3d7db
RK
1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2007, 2008 Richard Kettlewell
3dc3d7db 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
3dc3d7db 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
3dc3d7db
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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.
14 *
3dc3d7db 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3dc3d7db 17 */
132a5a4a
RK
18/** @file server/dbupgrade.c
19 * @brief Database upgrade utility
20 *
21 * Invoked by the server when necessary.
22 */
05b75f8d 23#include "disorder-server.h"
3dc3d7db
RK
24
25static DB_TXN *global_tid;
26
de3bd7ad 27#define BADKEY_WARN 0
28#define BADKEY_FAIL 1
29#define BADKEY_DELETE 2
30
31/** @brief Bad key behavior */
32static int badkey = BADKEY_WARN;
33
34static long aliases_removed, keys_normalized, values_normalized, renoticed;
35static long keys_already_ok, values_already_ok;
36
3dc3d7db
RK
37static const struct option options[] = {
38 { "help", no_argument, 0, 'h' },
39 { "version", no_argument, 0, 'V' },
40 { "config", required_argument, 0, 'c' },
41 { "debug", no_argument, 0, 'd' },
42 { "no-debug", no_argument, 0, 'D' },
de3bd7ad 43 { "delete-bad-keys", no_argument, 0, 'x' },
44 { "fail-bad-keys", no_argument, 0, 'X' },
3dc3d7db
RK
45 { "syslog", no_argument, 0, 's' },
46 { "no-syslog", no_argument, 0, 'S' },
47 { 0, 0, 0, 0 }
48};
49
50/* display usage message and terminate */
51static void help(void) {
52 xprintf("Usage:\n"
53 " disorder-dbupgrade [OPTIONS]\n"
54 "Options:\n"
55 " --help, -h Display usage message\n"
56 " --version, -V Display version number\n"
57 " --config PATH, -c PATH Set configuration file\n"
58 " --debug, -d Turn on debugging\n"
59 " --[no-]syslog Force logging\n"
de3bd7ad 60 " --delete-bad-keys, -x Delete unconvertible keys\n"
61 " --fail-bad-keys, -X Fail if bad keys are found\n"
3dc3d7db
RK
62 "\n"
63 "Database upgrader for DisOrder. Not intended to be run\n"
64 "directly.\n");
65 xfclose(stdout);
66 exit(0);
67}
68
3dc3d7db
RK
69/** @brief Visit each key in a database and call @p callback
70 * @return 0 or DB_LOCK_DEADLOCK
71 *
72 * @p global_tid must be set. @p callback should return 0 or DB_LOCK_DEADLOCK.
73 */
74static int scan_core(const char *name, DB *db,
75 int (*callback)(const char *name, DB *db, DBC *c,
76 DBT *k, DBT *d)) {
77 long count = 0;
78 DBC *c = trackdb_opencursor(db, global_tid);
79 int err, r = 0;
80 DBT k[1], d[1];
81
de3bd7ad 82 values_normalized = 0;
83 keys_normalized = 0;
84 aliases_removed = 0;
85 renoticed = 0;
86 keys_already_ok = 0;
87 values_already_ok = 0;
3dc3d7db
RK
88 memset(k, 0, sizeof k);
89 memset(d, 0, sizeof d);
90 while((err = c->c_get(c, k, d, DB_NEXT)) == 0) {
91 if((err = callback(name, db, c, k, d)))
92 break;
93 ++count;
94 if(count % 1000 == 0)
95 info("scanning %s, %ld so far", name, count);
96 }
97 if(err && err != DB_NOTFOUND && err != DB_LOCK_DEADLOCK)
98 fatal(0, "%s: error scanning database: %s", name, db_strerror(err));
99 r = (err == DB_LOCK_DEADLOCK ? err : 0);
100 if((err = c->c_close(c)))
101 fatal(0, "%s: error closing cursor: %s", name, db_strerror(err));
de3bd7ad 102 info("%s: %ld entries scanned", name, count);
103 if(values_normalized || values_already_ok)
104 info("%s: %ld values converted, %ld already ok", name,
105 values_normalized, values_already_ok);
106 if(keys_normalized || keys_already_ok)
107 info("%s: %ld keys converted, %ld already OK", name,
108 keys_normalized, keys_already_ok);
109 if(aliases_removed)
110 info("%s: %ld aliases removed", name, aliases_removed);
111 if(renoticed)
112 info("%s: %ld tracks re-noticed", name, renoticed);
3dc3d7db
RK
113 return r;
114}
115
116/** @brief Visit each key in a database and call @p callback
117 *
118 * Everything happens inside the @p global_tid tranasction. @p callback
119 * should return 0 or DB_LOCK_DEADLOCK.
120 */
05b75f8d
RK
121static void scandb(const char *name, DB *db,
122 int (*callback)(const char *name, DB *db, DBC *c,
123 DBT *k, DBT *d)) {
3dc3d7db
RK
124 info("scanning %s", name);
125 for(;;) {
126 global_tid = trackdb_begin_transaction();
127 if(scan_core(name, db, callback)) {
128 trackdb_abort_transaction(global_tid);
129 global_tid = 0;
130 error(0, "detected deadlock, restarting scan");
131 continue;
132 } else {
133 trackdb_commit_transaction(global_tid);
134 global_tid = 0;
135 break;
136 }
137 }
138}
139
140/** @brief Truncate database @p db */
141static void truncate_database(const char *name, DB *db) {
142 u_int32_t count;
143 int err;
144
145 do {
146 err = db->truncate(db, 0, &count, DB_AUTO_COMMIT);
147 } while(err == DB_LOCK_DEADLOCK);
148 if(err)
149 fatal(0, "error truncating %s: %s", name, db_strerror(err));
150}
151
152/* scan callbacks */
153
154static int normalize_keys(const char *name, DB *db, DBC *c,
155 DBT *k, DBT *d) {
156 char *knfc;
157 size_t nknfc;
158 int err;
159
160 /* Find the normalized form of the key */
161 knfc = utf8_compose_canon(k->data, k->size, &nknfc);
de3bd7ad 162 if(!knfc) {
163 switch(badkey) {
164 case BADKEY_WARN:
165 error(0, "%s: invalid key: %.*s", name,
166 (int)k->size, (const char *)k->data);
167 break;
168 case BADKEY_DELETE:
169 error(0, "%s: deleting invalid key: %.*s", name,
170 (int)k->size, (const char *)k->data);
171 if((err = c->c_del(c, 0))) {
172 if(err != DB_LOCK_DEADLOCK)
173 fatal(0, "%s: error removing denormalized key: %s",
174 name, db_strerror(err));
175 return err;
176 }
177 break;
178 case BADKEY_FAIL:
179 fatal(0, "%s: invalid key: %.*s", name,
180 (int)k->size, (const char *)k->data);
181 }
182 return 0;
183 }
3dc3d7db 184 /* If the key is already in NFC then do nothing */
de3bd7ad 185 if(nknfc == k->size && !memcmp(k->data, knfc, nknfc)) {
186 ++keys_already_ok;
3dc3d7db 187 return 0;
de3bd7ad 188 }
3dc3d7db
RK
189 /* To rename the key we must delete the old one and insert a new one */
190 if((err = c->c_del(c, 0))) {
191 if(err != DB_LOCK_DEADLOCK)
192 fatal(0, "%s: error removing denormalized key: %s",
193 name, db_strerror(err));
194 return err;
195 }
196 k->size = nknfc;
197 k->data = knfc;
198 if((err = db->put(db, global_tid, k, d, DB_NOOVERWRITE))) {
199 if(err != DB_LOCK_DEADLOCK)
200 fatal(0, "%s: error storing normalized key: %s", name, db_strerror(err));
201 return err;
202 }
de3bd7ad 203 ++keys_normalized;
3dc3d7db
RK
204 return 0;
205}
206
207static int normalize_values(const char *name, DB *db,
208 DBC attribute((unused)) *c,
209 DBT *k, DBT *d) {
210 char *dnfc;
211 size_t ndnfc;
212 int err;
213
214 /* Find the normalized form of the value */
215 dnfc = utf8_compose_canon(d->data, d->size, &ndnfc);
216 if(!dnfc)
217 fatal(0, "%s: cannot convert data to NFC: %.*s", name,
218 (int)d->size, (const char *)d->data);
219 /* If the key is already in NFC then do nothing */
de3bd7ad 220 if(ndnfc == d->size && !memcmp(d->data, dnfc, ndnfc)) {
221 ++values_already_ok;
3dc3d7db 222 return 0;
de3bd7ad 223 }
3dc3d7db
RK
224 d->size = ndnfc;
225 d->data = dnfc;
226 if((err = db->put(db, global_tid, k, d, 0))) {
227 if(err != DB_LOCK_DEADLOCK)
228 fatal(0, "%s: error storing normalized data: %s", name, db_strerror(err));
229 return err;
230 }
de3bd7ad 231 ++values_normalized;
3dc3d7db
RK
232 return 0;
233}
234
235static int renotice(const char *name, DB attribute((unused)) *db,
236 DBC attribute((unused)) *c,
237 DBT *k, DBT *d) {
238 const struct kvp *const t = kvp_urldecode(d->data, d->size);
239 const char *const track = xstrndup(k->data, k->size);
240 const char *const path = kvp_get(t, "_path");
241 int err;
242
de3bd7ad 243 if(!path) {
244 /* If an alias sorts later than the actual filename then it'll appear
245 * in the scan. */
246 if(kvp_get(t, "_alias_for"))
247 return 0;
3dc3d7db
RK
248 fatal(0, "%s: no '_path' for %.*s", name,
249 (int)k->size, (const char *)k->data);
de3bd7ad 250 }
3dc3d7db
RK
251 switch(err = trackdb_notice_tid(track, path, global_tid)) {
252 case 0:
de3bd7ad 253 ++renoticed;
3dc3d7db
RK
254 return 0;
255 case DB_LOCK_DEADLOCK:
256 return err;
257 default:
258 fatal(0, "%s: unexpected return from trackdb_notice_tid: %s",
259 name, db_strerror(err));
260 }
261}
262
263static int remove_aliases_normalize_keys(const char *name, DB *db, DBC *c,
264 DBT *k, DBT *d) {
265 const struct kvp *const t = kvp_urldecode(d->data, d->size);
266 int err;
267
268 if(kvp_get(t, "_alias_for")) {
269 /* This is an alias. We remove all the alias entries. */
270 if((err = c->c_del(c, 0))) {
271 if(err != DB_LOCK_DEADLOCK)
272 fatal(0, "%s: error removing alias: %s", name, db_strerror(err));
273 return err;
274 }
de3bd7ad 275 ++aliases_removed;
3dc3d7db 276 return 0;
de3bd7ad 277 } else if(!kvp_get(t, "_path"))
278 error(0, "%s: %.*s has neither _alias_for nor _path", name,
279 (int)k->size, (const char *)k->data);
3dc3d7db
RK
280 return normalize_keys(name, db, c, k, d);
281}
282
283/** @brief Upgrade the database to the current version
284 *
285 * This function is supposed to be idempotent, so if it is interrupted
286 * half way through it is safe to restart.
287 */
288static void upgrade(void) {
289 char buf[32];
290
291 info("upgrading database to dbversion %ld", config->dbversion);
292 /* Normalize keys and values as required. We will also remove aliases as
293 * they will be regenerated when we re-noticed the tracks. */
294 info("renormalizing keys");
05b75f8d
RK
295 scandb("tracks.db", trackdb_tracksdb, remove_aliases_normalize_keys);
296 scandb("prefs.db", trackdb_prefsdb, normalize_keys);
297 scandb("global.db", trackdb_globaldb, normalize_keys);
298 scandb("noticed.db", trackdb_noticeddb, normalize_values);
3dc3d7db
RK
299 /* search.db and tags.db we will rebuild */
300 info("regenerating search database and aliases");
301 truncate_database("search.db", trackdb_searchdb);
302 truncate_database("tags.db", trackdb_tagsdb);
303 /* Regenerate the search database and aliases */
05b75f8d 304 scandb("tracks.db", trackdb_tracksdb, renotice);
3dc3d7db
RK
305 /* Finally update the database version */
306 snprintf(buf, sizeof buf, "%ld", config->dbversion);
307 trackdb_set_global("_dbversion", buf, 0);
308 info("completed database upgrade");
309}
310
311int main(int argc, char **argv) {
312 int n, logsyslog = !isatty(2);
313
314 set_progname(argv);
315 mem_init();
316 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
de3bd7ad 317 while((n = getopt_long(argc, argv, "hVc:dDSsxX", options, 0)) >= 0) {
3dc3d7db
RK
318 switch(n) {
319 case 'h': help();
3fbdc96d 320 case 'V': version("disorder-dbupgrade");
3dc3d7db
RK
321 case 'c': configfile = optarg; break;
322 case 'd': debugging = 1; break;
323 case 'D': debugging = 0; break;
324 case 'S': logsyslog = 0; break;
325 case 's': logsyslog = 1; break;
de3bd7ad 326 case 'x': badkey = BADKEY_DELETE; break;
327 case 'X': badkey = BADKEY_FAIL; break;
3dc3d7db
RK
328 default: fatal(0, "invalid option");
329 }
330 }
331 /* If stderr is a TTY then log there, otherwise to syslog. */
332 if(logsyslog) {
333 openlog(progname, LOG_PID, LOG_DAEMON);
334 log_default = &log_syslog;
335 }
02ba7921 336 if(config_read(0, NULL)) fatal(0, "cannot read configuration");
3dc3d7db 337 /* Open the database */
d25c4615
RK
338 trackdb_init(TRACKDB_NO_RECOVER);
339 trackdb_open(TRACKDB_OPEN_FOR_UPGRADE);
3dc3d7db
RK
340 upgrade();
341 return 0;
342}
343
344/*
345Local Variables:
346c-basic-offset:2
347comment-column:40
348fill-column:79
349indent-tabs-mode:nil
350End:
351*/