+
+/**
+ * cleanupdb - De-initialize the key database.
+ */
+static void fs_cleanupdb(struct onak_dbctx *dbctx)
+{
+ struct onak_fs_dbctx *privctx = (struct onak_fs_dbctx *) dbctx->priv;
+
+ /* Mmmm nothing to do here? */
+ close(privctx->lockfile_fd);
+}
+
+/**
+ * initdb - Initialize the key database.
+ */
+struct onak_dbctx *keydb_fs_init(struct onak_db_config *dbcfg, bool readonly)
+{
+ char buffer[PATH_MAX];
+ struct onak_dbctx *dbctx;
+ struct onak_fs_dbctx *privctx;
+
+ dbctx = malloc(sizeof(struct onak_dbctx));
+ if (dbctx == NULL) {
+ return NULL;
+ }
+ dbctx->config = dbcfg;
+ dbctx->priv = privctx = malloc(sizeof(*privctx));
+ if (privctx == NULL) {
+ free(dbctx);
+ return NULL;
+ }
+
+ privctx->lockfile_readonly = readonly;
+
+ snprintf(buffer, sizeof(buffer), "%s/.lock", dbcfg->location);
+
+ if (access(dbcfg->location, R_OK | W_OK | X_OK) == -1) {
+ if (errno != ENOENT) {
+ logthing(LOGTHING_CRITICAL,
+ "Unable to access keydb_fs root of '%s'. (%s)",
+ dbcfg->location, strerror(errno));
+ exit(1); /* Lacking rwx on the key dir */
+ }
+ mkdir(dbcfg->location, 0777);
+ privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
+ }
+ if (chdir(dbcfg->location) == -1) {
+ /* Shouldn't happen after the above */
+ logthing(LOGTHING_CRITICAL,
+ "Couldn't change to database directory: %s",
+ strerror(errno));
+ free(dbctx->priv);
+ free(dbctx);
+ return NULL;
+ }
+ privctx->lockfile_fd = open(buffer,
+ (privctx->lockfile_readonly) ?
+ O_RDONLY : O_RDWR);
+ if (privctx->lockfile_fd == -1)
+ privctx->lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
+ if (privctx->lockfile_fd == -1) {
+ logthing(LOGTHING_CRITICAL,
+ "Unable to open lockfile '%s'. (%s)",
+ buffer, strerror(errno));
+ exit(1); /* Lacking rwx on the key dir */
+ }
+
+ dbctx->cleanupdb = fs_cleanupdb;
+ dbctx->starttrans = fs_starttrans;
+ dbctx->endtrans = fs_endtrans;
+ dbctx->fetch_key_id = fs_fetch_key_id;
+ dbctx->fetch_key_fp = generic_fetch_key_fp;
+ dbctx->fetch_key_text = fs_fetch_key_text;
+ dbctx->fetch_key_skshash = fs_fetch_key_skshash;
+ dbctx->store_key = fs_store_key;
+ dbctx->update_keys = generic_update_keys;
+ dbctx->delete_key = fs_delete_key;
+ dbctx->getkeysigs = generic_getkeysigs;
+ dbctx->cached_getkeysigs = generic_cached_getkeysigs;
+ dbctx->keyid2uid = generic_keyid2uid;
+ dbctx->iterate_keys = fs_iterate_keys;
+
+ return dbctx;
+}