2 * This file is part of DisOrder
3 * Copyright (C) 2009 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 /** @file server/dbparams.c
19 * @brief Parameters affecting the database
21 * Rescan can regenerate aliases and the search and tag databases but
22 * we rather assume that they are either empty or good. Therefore we
23 * need to store anything that can affect these values and erase them
26 * The solution is a global pref _dbparams which contains the hash of
27 * the alias, stopword and namepart data.
29 #include "disorder-server.h"
32 static int dbparams_cleanup(DB_TXN *tid);
33 static void h_write_string(gcrypt_hash_handle h,
35 static char *compute_dbparams(void);
37 /** @brief Check whether database parameters have changed
39 * If the database parameters have changed then deletes the search and
40 * tag database contents and all aliases. The subsequent rescan will
43 void dbparams_check(void) {
44 const char *newparams = compute_dbparams();
45 const char *oldparams = trackdb_get_global("_dbparams");
47 // If the parameters match, return straight away
48 if(oldparams && !strcmp(newparams, oldparams))
50 // Log what we're going to do
54 disorder_info("database parameter string changed from %s to %s - removing old data",
55 oldparams, newparams);
57 disorder_info("new database parameter string %s - removing old data",
59 /* This is a slightly annoying case; the global pref wasn't present. In
60 * practice this is almost certainly either an upgrade (with no change to
61 * any relevant parameters) or a new installation (with no tracks).
63 * The new installation case doesn't matter much; clearing an empty
64 * search database and iterating over a likewise track database won't
67 * However for upgrade this will throw away a lot of data and laboriously
68 * regenerate it, which is rather a shame.
71 tid = trackdb_begin_transaction();
72 int err = dbparams_cleanup(tid);
74 err = trackdb_set_global_tid("_dbparams", newparams, tid);
77 trackdb_commit_transaction(tid);
78 disorder_info("removed old data OK, will regenerate on rescan");
80 case DB_LOCK_DEADLOCK:
81 /* Deadlocked, try again */
82 trackdb_abort_transaction(tid);
85 disorder_fatal(0, "error updating database: %s", db_strerror(err));
90 /** @brief Clean up databases */
91 static int dbparams_cleanup(DB_TXN *tid) {
95 /* We'll regenerate search.db based on the new set of stopwords */
96 if((err = trackdb_searchdb->truncate(trackdb_searchdb, tid, &count, 0))) {
97 disorder_error(err, "truncating search.db: %s", db_strerror(err));
100 /* We'll regenerate aliases based on the new alias/namepart settings, so
101 * delete all the alias records currently present
103 * TODO this looks suspiciously similar to part of dump.c
107 cursor = trackdb_opencursor(trackdb_tracksdb, tid);
108 if((err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
109 DB_FIRST)) == DB_LOCK_DEADLOCK) {
110 disorder_error(0, "cursor->c_get: %s", db_strerror(err));
114 struct kvp *data = kvp_urldecode(d.data, d.size);
115 if(kvp_get(data, "_alias_for")) {
116 if((err = cursor->c_del(cursor, 0))) {
117 disorder_error(0, "cursor->c_del: %s", db_strerror(err));
121 err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT);
123 if(err == DB_LOCK_DEADLOCK) {
124 disorder_error(0, "cursor operation: %s", db_strerror(err));
127 if(err != DB_NOTFOUND)
128 disorder_fatal(0, "cursor->c_get: %s", db_strerror(err));
131 if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK;
135 /** @brief Write a string into a gcrypt hash function
136 * @param h Hash handle
137 * @param s String to write
139 * The 0 terminator is included in the byte written.
141 static void h_write_string(gcrypt_hash_handle h,
143 gcry_md_write(h, s, strlen(s) + 1);
146 /** @brief Compute database parameters hash
147 * @return Opaque string encapsulating database parameters
149 static char *compute_dbparams(void) {
151 gcrypt_hash_handle h;
153 if((e = gcry_md_open(&h, GCRY_MD_SHA256, 0)))
154 disorder_fatal(0, "gcry_md_open: %s", gcry_strerror(e));
155 h_write_string(h, "alias");
156 h_write_string(h, config->alias);
157 for(int n = 0; n < config->stopword.n; ++n) {
158 h_write_string(h, "stopword");
159 h_write_string(h, config->stopword.s[n]);
161 for(int n = 0; n < config->namepart.n; ++n) {
162 h_write_string(h, "namepart");
163 h_write_string(h, config->namepart.s[n].part);
164 h_write_string(h, config->namepart.s[n].res);
165 h_write_string(h, config->namepart.s[n].replace);
166 h_write_string(h, config->namepart.s[n].context);
168 snprintf(buffer, sizeof buffer, "%u", config->namepart.s[n].reflags);
169 h_write_string(h, buffer);
172 byte_xasprintf(&result, "dbparams-0-sha256:%s",
173 hex(gcry_md_read(h, GCRY_MD_SHA256),
174 gcry_md_get_algo_dlen(GCRY_MD_SHA256)));