Commit 7dac81e5 authored by Daniel Salzman's avatar Daniel Salzman Committed by Jan Včelák

conf: add read-only open support

parent f4bf9040
......@@ -91,7 +91,8 @@ static void rm_dir(const char *path)
int conf_new(
conf_t **conf,
const yp_item_t *scheme,
const char *db_dir)
const char *db_dir,
bool read_only)
{
if (conf == NULL) {
return KNOT_EINVAL;
......@@ -117,6 +118,11 @@ int conf_new(
lmdb_opts.mapsize = 500 * 1024 * 1024;
lmdb_opts.flags.env = KNOT_DB_LMDB_NOTLS;
// Set read-only mode.
if (read_only) {
lmdb_opts.flags.env |= KNOT_DB_LMDB_RDONLY;
}
// Open database.
if (db_dir == NULL) {
char tpl[] = "/tmp/knot-confdb.XXXXXX";
......@@ -142,15 +148,16 @@ int conf_new(
goto new_error;
}
// Initialize/check database.
// Initialize and check the database.
knot_db_txn_t txn;
ret = out->api->txn_begin(out->db, &txn, 0);
unsigned flags = read_only ? KNOT_DB_RDONLY : 0;
ret = out->api->txn_begin(out->db, &txn, flags);
if (ret != KNOT_EOK) {
out->api->deinit(out->db);
goto new_error;
}
ret = conf_db_init(out, &txn);
ret = read_only ? conf_db_check(out, &txn) : conf_db_init(out, &txn);
if (ret != KNOT_EOK) {
out->api->txn_abort(&txn);
out->api->deinit(out->db);
......
......@@ -110,16 +110,18 @@ conf_t* conf(void);
/*!
* Creates new or opens old configuration database.
*
* \param[out] conf Configuration.
* \param[in] scheme Configuration scheme.
* \param[in] db_dir Database path or NULL.
* \param[out] conf Configuration.
* \param[in] scheme Configuration scheme.
* \param[in] db_dir Database path or NULL.
* \param[in] read_only Set to read-only access.
*
* \return Error code, KNOT_EOK if success.
*/
int conf_new(
conf_t **conf,
const yp_item_t *scheme,
const char *db_dir
const char *db_dir,
bool read_only
);
/*!
......
......@@ -108,20 +108,6 @@ static int db_check_version(
return KNOT_EOK;
}
static int db_check(
conf_t *conf,
knot_db_txn_t *txn)
{
int ret = conf->api->count(txn);
if (ret == 0) { // Empty DB.
return KNOT_CONF_EMPTY;
} else if (ret > 0) { // Check existing DB.
return db_check_version(conf, txn);
} else { // DB error.
return ret;
}
}
int conf_db_init(
conf_t *conf,
knot_db_txn_t *txn)
......@@ -139,7 +125,21 @@ int conf_db_init(
knot_db_val_t data = { d, sizeof(d) };
return conf->api->insert(txn, &key, &data, 0);
} else if (ret > 0) { // Check existing DB.
return db_check(conf, txn);
return conf_db_check(conf, txn);
} else { // DB error.
return ret;
}
}
int conf_db_check(
conf_t *conf,
knot_db_txn_t *txn)
{
int ret = conf->api->count(txn);
if (ret == 0) { // Empty DB.
return KNOT_CONF_EMPTY;
} else if (ret > 0) { // Check existing DB.
return db_check_version(conf, txn);
} else { // DB error.
return ret;
}
......
......@@ -42,7 +42,7 @@
#define CONF_MAX_DATA_LEN 65536
/*!
* Opens and checks old or initializes new configuration DB.
* Opens and checks or initializes the configuration DB.
*
* \param[in] conf Configuration.
* \param[in] txn Configuration DB transaction.
......@@ -54,6 +54,19 @@ int conf_db_init(
knot_db_txn_t *txn
);
/*!
* Checks the configuration DB.
*
* \param[in] conf Configuration.
* \param[in] txn Configuration DB transaction.
*
* \return Error code, KNOT_EOK if success.
*/
int conf_db_check(
conf_t *conf,
knot_db_txn_t *txn
);
/*!
* Sets the item with data in the configuration DB.
*
......
......@@ -434,7 +434,7 @@ int main(int argc, char **argv)
/* Open configuration. */
conf_t *new_conf = NULL;
if (config_db == NULL) {
int ret = conf_new(&new_conf, conf_scheme, NULL);
int ret = conf_new(&new_conf, conf_scheme, NULL, false);
if (ret != KNOT_EOK) {
log_fatal("failed to initialize configuration database "
"(%s)", knot_strerror(ret));
......@@ -455,7 +455,7 @@ int main(int argc, char **argv)
new_conf->filename = strdup(config_fn);
} else {
/* Open configuration database. */
int ret = conf_new(&new_conf, conf_scheme, config_db);
int ret = conf_new(&new_conf, conf_scheme, config_db, true);
if (ret != KNOT_EOK) {
log_fatal("failed to open configuration database '%s' "
"(%s)", config_db, knot_strerror(ret));
......@@ -609,7 +609,7 @@ static int cmd_conf_import(cmd_args_t *args)
}
conf_t *new_conf = NULL;
int ret = conf_new(&new_conf, conf_scheme, args->conf_db);
int ret = conf_new(&new_conf, conf_scheme, args->conf_db, false);
if (ret == KNOT_EOK) {
ret = conf_import(new_conf, args->argv[0], true);
}
......
......@@ -332,7 +332,7 @@ int main(int argc, char **argv)
/* Open configuration. */
conf_t *new_conf = NULL;
if (config_db == NULL) {
int ret = conf_new(&new_conf, conf_scheme, NULL);
int ret = conf_new(&new_conf, conf_scheme, NULL, false);
if (ret != KNOT_EOK) {
log_fatal("failed to initialize configuration database "
"(%s)", knot_strerror(ret));
......@@ -353,7 +353,7 @@ int main(int argc, char **argv)
new_conf->filename = strdup(config_fn);
} else {
/* Open configuration database. */
int ret = conf_new(&new_conf, conf_scheme, config_db);
int ret = conf_new(&new_conf, conf_scheme, config_db, false);
if (ret != KNOT_EOK) {
log_fatal("failed to open configuration database '%s' "
"(%s)", config_db, knot_strerror(ret));
......
......@@ -28,7 +28,7 @@ static inline int test_conf(const char *conf_str, const yp_item_t *scheme)
}
conf_t *conf;
int ret = conf_new(&conf, scheme, NULL);
int ret = conf_new(&conf, scheme, NULL, false);
if (ret != KNOT_EOK) {
return ret;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment