| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2009 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 3 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, |
| 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 | * |
| 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/>. |
| 17 | */ |
| 18 | /** @file server/dbparams.c |
| 19 | * @brief Parameters affecting the database |
| 20 | * |
| 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 |
| 24 | * if they change. |
| 25 | * |
| 26 | * The solution is a global pref _dbparams which contains the hash of |
| 27 | * the alias, stopword and namepart data. |
| 28 | */ |
| 29 | #include "disorder-server.h" |
| 30 | #include <gcrypt.h> |
| 31 | |
| 32 | static int dbparams_cleanup(DB_TXN *tid); |
| 33 | static void h_write_string(gcrypt_hash_handle h, |
| 34 | const char *s); |
| 35 | static char *compute_dbparams(void); |
| 36 | |
| 37 | /** @brief Check whether database parameters have changed |
| 38 | * |
| 39 | * If the database parameters have changed then deletes the search and |
| 40 | * tag database contents and all aliases. The subsequent rescan will |
| 41 | * regenerate them. |
| 42 | */ |
| 43 | void dbparams_check(void) { |
| 44 | const char *newparams = compute_dbparams(); |
| 45 | const char *oldparams = trackdb_get_global("_dbparams"); |
| 46 | |
| 47 | // If the parameters match, return straight away |
| 48 | if(oldparams && !strcmp(newparams, oldparams)) |
| 49 | return; |
| 50 | // Log what we're going to do |
| 51 | for(;;) { |
| 52 | DB_TXN *tid; |
| 53 | if(oldparams) |
| 54 | info("database parameter string changed from %s to %s - removing old data", |
| 55 | oldparams, newparams); |
| 56 | else { |
| 57 | info("new database parameter string %s - removing old data", |
| 58 | newparams); |
| 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). |
| 62 | * |
| 63 | * The new installation case doesn't matter much; clearing an empty |
| 64 | * search database and iterating over a likewise track database won't |
| 65 | * take long. |
| 66 | * |
| 67 | * However for upgrade this will throw away a lot of data and laboriously |
| 68 | * regenerate it, which is rather a shame. |
| 69 | */ |
| 70 | } |
| 71 | tid = trackdb_begin_transaction(); |
| 72 | int err = dbparams_cleanup(tid); |
| 73 | if(!err) |
| 74 | err = trackdb_set_global_tid("_dbparams", newparams, tid); |
| 75 | switch(err) { |
| 76 | case 0: |
| 77 | trackdb_commit_transaction(tid); |
| 78 | info("removed old data OK, will regenerate on rescan"); |
| 79 | return; |
| 80 | case DB_LOCK_DEADLOCK: |
| 81 | /* Deadlocked, try again */ |
| 82 | trackdb_abort_transaction(tid); |
| 83 | break; |
| 84 | default: |
| 85 | fatal(0, "error updating database: %s", db_strerror(err)); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** @brief Clean up databases */ |
| 91 | static int dbparams_cleanup(DB_TXN *tid) { |
| 92 | int err; |
| 93 | u_int32_t count; |
| 94 | |
| 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 | error(err, "truncating search.db: %s", db_strerror(err)); |
| 98 | return err; |
| 99 | } |
| 100 | /* We'll regenerate aliases based on the new alias/namepart settings, so |
| 101 | * delete all the alias records currently present |
| 102 | * |
| 103 | * TODO this looks suspiciously similar to part of dump.c |
| 104 | */ |
| 105 | DBC *cursor; |
| 106 | DBT k, d; |
| 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 | error(0, "cursor->c_get: %s", db_strerror(err)); |
| 111 | goto done; |
| 112 | } |
| 113 | while(err == 0) { |
| 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 | error(0, "cursor->c_del: %s", db_strerror(err)); |
| 118 | goto done; |
| 119 | } |
| 120 | } |
| 121 | err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d), DB_NEXT); |
| 122 | } |
| 123 | if(err == DB_LOCK_DEADLOCK) { |
| 124 | error(0, "cursor operation: %s", db_strerror(err)); |
| 125 | goto done; |
| 126 | } |
| 127 | if(err != DB_NOTFOUND) |
| 128 | fatal(0, "cursor->c_get: %s", db_strerror(err)); |
| 129 | err = 0; |
| 130 | done: |
| 131 | if(trackdb_closecursor(cursor) && !err) err = DB_LOCK_DEADLOCK; |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | /** @brief Write a string into a gcrypt hash function |
| 136 | * @param h Hash handle |
| 137 | * @param s String to write |
| 138 | * |
| 139 | * The 0 terminator is included in the byte written. |
| 140 | */ |
| 141 | static void h_write_string(gcrypt_hash_handle h, |
| 142 | const char *s) { |
| 143 | gcry_md_write(h, s, strlen(s) + 1); |
| 144 | } |
| 145 | |
| 146 | /** @brief Compute database parameters hash |
| 147 | * @return Opaque string encapsulating database parameters |
| 148 | */ |
| 149 | static char *compute_dbparams(void) { |
| 150 | gcry_error_t e; |
| 151 | gcrypt_hash_handle h; |
| 152 | |
| 153 | if((e = gcry_md_open(&h, GCRY_MD_SHA256, 0))) |
| 154 | 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]); |
| 160 | } |
| 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); |
| 167 | char buffer[64]; |
| 168 | snprintf(buffer, sizeof buffer, "%u", config->namepart.s[n].reflags); |
| 169 | h_write_string(h, buffer); |
| 170 | } |
| 171 | char *result; |
| 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))); |
| 175 | gcry_md_close(h); |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | Local Variables: |
| 181 | c-basic-offset:2 |
| 182 | comment-column:40 |
| 183 | fill-column:79 |
| 184 | indent-tabs-mode:nil |
| 185 | End: |
| 186 | */ |