From 400204ad90917fd1d9fe63a273b9372d042fed30 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 16 Apr 2009 11:37:22 -0400 Subject: Avoid unnecessary reloads of config.ldb Add code to check if the file has changed since the last update was performed. Avoid dumping and reloading the config ldb if the modification time of the configuration file has not changed at all. --- server/confdb/confdb.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'server/confdb/confdb.c') diff --git a/server/confdb/confdb.c b/server/confdb/confdb.c index 1f642ca1d..fcb4c8ab4 100644 --- a/server/confdb/confdb.c +++ b/server/confdb/confdb.c @@ -21,11 +21,9 @@ #define _GNU_SOURCE -#include "talloc.h" -#include "tevent.h" +#include #include "config.h" #include "ldb.h" -#include "ldb_errors.h" #include "util/util.h" #include "confdb/confdb.h" #include "confdb/confdb_private.h" @@ -875,9 +873,36 @@ int confdb_init_db(const char *config_file, struct confdb_ctx *cdb) char *config_ldif; struct ldb_ldif *ldif; TALLOC_CTX *tmp_ctx; + char *lasttimestr, timestr[21]; + const char *vals[2] = { timestr, NULL }; + struct stat cstat; tmp_ctx = talloc_new(cdb); - if(tmp_ctx == NULL) return ENOMEM; + if (tmp_ctx == NULL) return ENOMEM; + + /* ok, first of all stat conf file */ + ret = stat(config_file, &cstat); + if (ret != 0) { + DEBUG(0, ("Unable to stat config file [%s]! (%d [%s])\n", + config_file, errno, strerror(errno))); + return errno; + } + ret = snprintf(timestr, 21, "%llu", (long long unsigned)cstat.st_mtime); + if (ret <= 0 || ret >= 21) { + DEBUG(0, ("Failed to convert time_t to string ??\n")); + return errno ? errno: EFAULT; + } + + /* check if we need to re-init the db */ + ret = confdb_get_string(cdb, tmp_ctx, "config", "lastUpdate", NULL, &lasttimestr); + if (ret == EOK && lasttimestr != NULL) { + + /* now check if we lastUpdate and last file modification change differ*/ + if (strcmp(lasttimestr, timestr) == 0) { + /* not changed, get out, nothing more to do */ + return EOK; + } + } /* Set up a transaction to replace the configuration */ ret = ldb_transaction_start(cdb->ldb); @@ -927,6 +952,14 @@ int confdb_init_db(const char *config_file, struct confdb_ctx *cdb) ldb_ldif_read_free(cdb->ldb, ldif); } + /* now store the lastUpdate time so that we do not re-init if nothing + * changed on restart */ + + ret = confdb_add_param(cdb, true, "config", "lastUpdate", vals); + if (ret != EOK) { + DEBUG(1, ("Failed to set last update time on db!\n")); + } + ret = EOK; done: -- cgit