From d25c461540eadff2230653059ea2f692ee8dfc63 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Thu, 22 Nov 2007 09:44:43 +0000 Subject: [PATCH] tidy up upgrade/recovery flags a bit Organization: Straylight/Edgeware From: Richard Kettlewell --- server/dbupgrade.c | 4 +-- server/deadlock.c | 2 +- server/dump.c | 2 +- server/rescan.c | 4 +-- server/state.c | 6 +++-- server/stats.c | 4 +-- server/trackdb.c | 62 +++++++++++++++++++++++++++++++++------------- server/trackdb.h | 33 ++++++++++++++++++++---- 8 files changed, 85 insertions(+), 32 deletions(-) diff --git a/server/dbupgrade.c b/server/dbupgrade.c index 492cf64..93c99ea 100644 --- a/server/dbupgrade.c +++ b/server/dbupgrade.c @@ -292,8 +292,8 @@ int main(int argc, char **argv) { } if(config_read(0)) fatal(0, "cannot read configuration"); /* Open the database */ - trackdb_init(0); - trackdb_open(1/*dbupgrade*/); + trackdb_init(TRACKDB_NO_RECOVER); + trackdb_open(TRACKDB_OPEN_FOR_UPGRADE); upgrade(); return 0; } diff --git a/server/deadlock.c b/server/deadlock.c index cda6d0d..e45694f 100644 --- a/server/deadlock.c +++ b/server/deadlock.c @@ -100,7 +100,7 @@ int main(int argc, char **argv) { } if(config_read(0)) fatal(0, "cannot read configuration"); info("started"); - trackdb_init(0); + trackdb_init(TRACKDB_NO_RECOVER); while(getppid() != 1) { if((err = trackdb_env->lock_detect(trackdb_env, 0, diff --git a/server/dump.c b/server/dump.c index cedcc73..84b45d0 100644 --- a/server/dump.c +++ b/server/dump.c @@ -425,7 +425,7 @@ int main(int argc, char **argv) { } if(config_read(0)) fatal(0, "cannot read configuration"); trackdb_init(recover); - trackdb_open(0); + trackdb_open(TRACKDB_NO_UPGRADE); if(dump) { /* we write to a temporary file and rename into place */ byte_xasprintf(&tmp, "%s.%lx.tmp", path, (unsigned long)getpid()); diff --git a/server/rescan.c b/server/rescan.c index b2beac8..b878929 100644 --- a/server/rescan.c +++ b/server/rescan.c @@ -361,8 +361,8 @@ int main(int argc, char **argv) { xsigaction(SIGTERM, &sa, 0); xsigaction(SIGINT, &sa, 0); info("started"); - trackdb_init(0); - trackdb_open(0); + trackdb_init(TRACKDB_NO_RECOVER); + trackdb_open(TRACKDB_NO_UPGRADE); if(optind == argc) { /* Rescan all collections */ do_all(rescan_collection); diff --git a/server/state.c b/server/state.c index 91bc078..66760f0 100644 --- a/server/state.c +++ b/server/state.c @@ -151,8 +151,10 @@ int reconfigure(ev_source *ev, int reload) { speaker_reload(); info("%s: installed new configuration", configfile); } - } - trackdb_open(0); + trackdb_open(TRACKDB_NO_UPGRADE); + } else + /* We only allow for upgrade at startup */ + trackdb_open(TRACKDB_CAN_UPGRADE); if(need_another_rescan) trackdb_rescan(ev); if(!ret) { diff --git a/server/stats.c b/server/stats.c index 0c354e6..a12b8f6 100644 --- a/server/stats.c +++ b/server/stats.c @@ -95,8 +95,8 @@ int main(int argc, char **argv) { } if(config_read(0)) fatal(0, "cannot read configuration"); - trackdb_init(0); - trackdb_open(0); + trackdb_init(TRACKDB_NO_RECOVER); + trackdb_open(TRACKDB_NO_UPGRADE); stats = trackdb_stats(0); while(*stats) xprintf("%s\n", *stats++); diff --git a/server/trackdb.c b/server/trackdb.c index 2318bd9..1e643ef 100644 --- a/server/trackdb.c +++ b/server/trackdb.c @@ -17,6 +17,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file server/trackdb.c + * @brief Track database */ #include #include "types.h" @@ -144,9 +146,17 @@ static int compare(DB attribute((unused)) *db_, return compare_path_raw(a->data, a->size, b->data, b->size); } -/* open environment */ -void trackdb_init(int recover) { +/** @brief Open database environment + * @param flags Flags word + * + * Flags should be one of: + * - @ref TRACKDB_NO_RECOVER + * - @ref TRACKDB_NORMAL_RECOVER + * - @ref TRACKDB_FATAL_RECOVER + */ +void trackdb_init(int flags) { int err; + const int recover = flags & TRACKDB_RECOVER_MASK; static int recover_type[] = { 0, DB_RECOVER, DB_RECOVER_FATAL }; /* sanity checks */ @@ -286,9 +296,14 @@ static DB *open_db(const char *path, } /** @brief Open track databases - * @param dbupgrade Non-0 to allow non-current database versions + * @param Flags flags word + * + * @p flags should be one of: + * - @p TRACKDB_NO_UPGRADE, if no upgrade should be attempted + * - @p TRACKDB_CAN_UPGRADE, if an upgrade may be attempted + * - @p TRACKDB_OPEN_FOR_UPGRADE, if this is disorder-dbupgrade */ -void trackdb_open(int dbupgrade) { +void trackdb_open(int flags) { int newdb, err; /* sanity checks */ @@ -302,29 +317,41 @@ void trackdb_open(int dbupgrade) { long oldversion; s = trackdb_get_global("_dbversion"); + /* Close the database again, we'll open it property below */ + if((err = trackdb_globaldb->close(trackdb_globaldb, 0))) + fatal(0, "error closing global.db: %s", db_strerror(err)); + trackdb_globaldb = 0; + /* Convert version string to an integer */ oldversion = s ? atol(s) : 1; if(oldversion > config->dbversion) { - /* Database is from the future */ + /* Database is from the future; we never allow this. */ fatal(0, "this version of DisOrder is too old for database version %ld", oldversion); } - if(oldversion < config->dbversion && !dbupgrade) { - /* This database needs upgrading. This isn't implemented yet so we just - * fail. */ - fatal(0, "database needs upgrading from %ld to %ld", - oldversion, config->dbversion); + if(oldversion < config->dbversion) { + /* Database version is out of date */ + switch(flags & TRACKDB_UPGRADE_MASK) { + case TRACKDB_NO_UPGRADE: + /* This database needs upgrading but this is not permitted */ + fatal(0, "database needs upgrading from %ld to %ld", + oldversion, config->dbversion); + case TRACKDB_CAN_UPGRADE: + /* This database needs upgrading */ + fatal(0, "database needs upgrading from %ld to %ld, which is not yet supported", + oldversion, config->dbversion); + case TRACKDB_OPEN_FOR_UPGRADE: + break; + default: + abort(); + } } - if(oldversion == config->dbversion && dbupgrade) { + if(oldversion == config->dbversion && (flags & TRACKDB_OPEN_FOR_UPGRADE)) { /* This doesn't make any sense */ fatal(0, "database is already at current version"); } newdb = 0; - /* Close the database again, we'll open it property below */ - if((err = trackdb_globaldb->close(trackdb_globaldb, 0))) - fatal(0, "error closing global.db: %s", db_strerror(err)); - trackdb_globaldb = 0; } else { - if(dbupgrade) { + if(flags & TRACKDB_OPEN_FOR_UPGRADE) { /* Cannot upgrade a new database */ fatal(0, "cannot upgrade a database that does not exist"); } @@ -342,10 +369,11 @@ void trackdb_open(int dbupgrade) { trackdb_globaldb = open_db("global.db", 0, DB_HASH, DB_CREATE, 0666); trackdb_noticeddb = open_db("noticed.db", DB_DUPSORT, DB_BTREE, DB_CREATE, 0666); - if(newdb && !dbupgrade) { + if(newdb) { /* Stash the database version */ char buf[32]; + assert(!(flags & TRACKDB_OPEN_FOR_UPGRADE)); snprintf(buf, sizeof buf, "%ld", config->dbversion); trackdb_set_global("_dbversion", buf, 0); } diff --git a/server/trackdb.h b/server/trackdb.h index 9fca722..3cce6a5 100644 --- a/server/trackdb.h +++ b/server/trackdb.h @@ -17,6 +17,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file server/trackdb.h + * @brief Track database public interface */ #ifndef TRACKDB_H #define TRACKDB_H @@ -27,17 +29,38 @@ extern const struct cache_type cache_files_type; extern unsigned long cache_files_hits, cache_files_misses; /* Cache entry type and tracking for regexp-based lookups */ -void trackdb_init(int recover); -#define TRACKDB_NO_RECOVER 0 -#define TRACKDB_NORMAL_RECOVER 1 -#define TRACKDB_FATAL_RECOVER 2 +/** @brief Do not attempt database recovery (trackdb_init()) */ +#define TRACKDB_NO_RECOVER 0x0000 + +/** @brief Attempt normal recovery (trackdb_init()) */ +#define TRACKDB_NORMAL_RECOVER 0x0001 + +/** @brief Attempt catastrophic trcovery (trackdb_init()) */ +#define TRACKDB_FATAL_RECOVER 0x0002 + +/** @brief Mask of recovery bits (trackdb_init()) */ +#define TRACKDB_RECOVER_MASK 0x0003 + +/** @brief Open for database upgrade (trackdb_open()) */ +#define TRACKDB_OPEN_FOR_UPGRADE 0x0004 + +/** @brief Permit upgrade (trackdb_open()) */ +#define TRACKDB_CAN_UPGRADE 0x0008 + +/** @brief Do not permit upgrade (trackdb_open()) */ +#define TRACKDB_NO_UPGRADE 0x0000 + +/** @brief Mask of upgrade bits (trackdb_open()) */ +#define TRACKDB_UPGRADE_MASK 0x000C + +void trackdb_init(int flags); void trackdb_deinit(void); /* close/close environment */ void trackdb_master(struct ev_source *ev); /* start deadlock manager */ -void trackdb_open(int dbupgrade); +void trackdb_open(int flags); void trackdb_close(void); /* open/close track databases */ -- [mdw]