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