ldb_tdb: factor out the (to be) common init code
authorGarming Sam <garming@catalyst.net.nz>
Tue, 10 Jan 2017 22:36:48 +0000 (11:36 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 5 Mar 2018 19:50:15 +0000 (20:50 +0100)
Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/ldb_tdb/ldb_tdb.c
lib/ldb/ldb_tdb/ldb_tdb.h

index 6cdd5733b981305ec441ac13e8defd5f7f0b3ca7..6593dc2ee93a842afeb74cfcbdba267b90f7e1de 100644 (file)
@@ -1923,6 +1923,57 @@ static const struct ldb_module_ops ltdb_ops = {
        .read_unlock       = generic_unlock_read,
 };
 
+int init_store(struct ltdb_private *ltdb,
+                     const char *name,
+                     struct ldb_context *ldb,
+                     const char *options[],
+                     struct ldb_module **_module)
+{
+       struct ldb_module *module;
+
+       if (getenv("LDB_WARN_UNINDEXED")) {
+               ltdb->warn_unindexed = true;
+       }
+
+       if (getenv("LDB_WARN_REINDEX")) {
+               ltdb->warn_reindex = true;
+       }
+
+       ltdb->sequence_number = 0;
+
+       module = ldb_module_new(ldb, ldb, name, &ltdb_ops);
+       if (!module) {
+               ldb_oom(ldb);
+               talloc_free(ltdb);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       ldb_module_set_private(module, ltdb);
+       talloc_steal(module, ltdb);
+
+       if (ltdb_cache_load(module) != 0) {
+               ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
+                                      "records for backend '%s'", name);
+               talloc_free(module);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       *_module = module;
+       /*
+        * Set the maximum key length
+        */
+       {
+               const char *len_str =
+                       ldb_options_find(ldb, options,
+                                        "max_key_len_for_self_test");
+               if (len_str != NULL) {
+                       unsigned len = strtoul(len_str, NULL, 0);
+                       ltdb->max_key_length = len;
+               }
+       }
+
+       return LDB_SUCCESS;
+}
+
 /*
   connect to the database
 */
@@ -1930,7 +1981,6 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
                        unsigned int flags, const char *options[],
                        struct ldb_module **_module)
 {
-       struct ldb_module *module;
        const char *path;
        int tdb_flags, open_flags;
        struct ltdb_private *ltdb;
@@ -1995,6 +2045,8 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
                open_flags = O_CREAT | O_RDWR;
        }
 
+       ltdb->kv_ops = &key_value_ops;
+
        /* note that we use quite a large default hash size */
        ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000,
                                   tdb_flags, open_flags,
@@ -2011,48 +2063,7 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       if (getenv("LDB_WARN_UNINDEXED")) {
-               ltdb->warn_unindexed = true;
-       }
-
-       if (getenv("LDB_WARN_REINDEX")) {
-               ltdb->warn_reindex = true;
-       }
-
-       ltdb->sequence_number = 0;
-       ltdb->kv_ops = &key_value_ops;
-
-       module = ldb_module_new(ldb, ldb, "ldb_tdb backend", &ltdb_ops);
-       if (!module) {
-               ldb_oom(ldb);
-               talloc_free(ltdb);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       ldb_module_set_private(module, ltdb);
-       talloc_steal(module, ltdb);
-
-       if (ltdb_cache_load(module) != 0) {
-               ldb_asprintf_errstring(ldb,
-                                      "Unable to load ltdb cache records of tdb '%s'", path);
-               talloc_free(module);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       *_module = module;
-
-       /*
-        * Set the maximum key length
-        */
-       {
-               const char *len_str =
-                       ldb_options_find(ldb, options,
-                                        "max_key_len_for_self_test");
-               if (len_str != NULL) {
-                       unsigned len = strtoul(len_str, NULL, 0);
-                       ltdb->max_key_length = len;
-               }
-       }
-       return LDB_SUCCESS;
+       return init_store(ltdb, "ldb_tdb backend", ldb, options, _module);
 }
 
 int ldb_tdb_init(const char *version)
index 5c930bf997a10c4d182bc267d490db7b9c5706ee..a40bbad96996f6bded7877a28b61714633a16223 100644 (file)
@@ -196,3 +196,6 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
                                   const char *path, int hash_size, int tdb_flags,
                                   int open_flags, mode_t mode,
                                   struct ldb_context *ldb);
+int init_store(struct ltdb_private *ltdb, const char *name,
+              struct ldb_context *ldb, const char *options[],
+              struct ldb_module **_module);