X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=utils%2Fnfsdcltrack%2Fsqlite.c;h=bac678980938f9ff7438a2459c11ab3fb04946b5;hb=db39b773b415ced0ce00b7a3fe242de4d5b0a04d;hp=fc882c6f40ce8eb9d2966dc1c6503a3ee7e3b7d4;hpb=37ebcfc42478bbbf5784504072b682037352a90e;p=nfs-utils.git diff --git a/utils/nfsdcltrack/sqlite.c b/utils/nfsdcltrack/sqlite.c index fc882c6..bac6789 100644 --- a/utils/nfsdcltrack/sqlite.c +++ b/utils/nfsdcltrack/sqlite.c @@ -61,9 +61,6 @@ /* global variables */ -/* top level DB directory */ -static char *sqlite_topdir; - /* reusable pathname and sql command buffer */ static char buf[PATH_MAX]; @@ -74,7 +71,7 @@ static sqlite3 *dbh; /* make a directory, ignoring EEXIST errors unless it's not a directory */ static int -mkdir_if_not_exist(char *dirname) +mkdir_if_not_exist(const char *dirname) { int ret; struct stat statbuf; @@ -93,28 +90,17 @@ mkdir_if_not_exist(char *dirname) return ret; } -/* - * Open the "main" database, and attempt to initialize it by creating the - * parameters table and inserting the schema version into it. Ignore any errors - * from that, and then attempt to select the version out of it again. If the - * version appears wrong, then assume that the DB is corrupt or has been - * upgraded, and return an error. If all of that works, then attempt to create - * the "clients" table. - */ +/* Open the database and set up the database handle for it */ int -sqlite_maindb_init(char *topdir) +sqlite_prepare_dbh(const char *topdir) { int ret; - char *err = NULL; - sqlite3_stmt *stmt = NULL; - sqlite_topdir = topdir; + /* Do nothing if the database handle is already set up */ + if (dbh) + return 0; - ret = mkdir_if_not_exist(sqlite_topdir); - if (ret) - return ret; - - ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", sqlite_topdir); + ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", topdir); if (ret < 0) return ret; @@ -123,15 +109,43 @@ sqlite_maindb_init(char *topdir) ret = sqlite3_open(buf, &dbh); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to open main database: %d", ret); + dbh = NULL; return ret; } ret = sqlite3_busy_timeout(dbh, CLD_SQLITE_BUSY_TIMEOUT); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to set sqlite busy timeout: %d", ret); - goto out_err; + sqlite3_close(dbh); + dbh = NULL; } + return ret; +} + +/* + * Open the "main" database, and attempt to initialize it by creating the + * parameters table and inserting the schema version into it. Ignore any errors + * from that, and then attempt to select the version out of it again. If the + * version appears wrong, then assume that the DB is corrupt or has been + * upgraded, and return an error. If all of that works, then attempt to create + * the "clients" table. + */ +int +sqlite_maindb_init(const char *topdir) +{ + int ret; + char *err = NULL; + sqlite3_stmt *stmt = NULL; + + ret = mkdir_if_not_exist(topdir); + if (ret) + return ret; + + ret = sqlite_prepare_dbh(topdir); + if (ret) + return ret; + /* Try to create table */ ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS parameters " "(key TEXT PRIMARY KEY, value TEXT);",