ldb_tdb: Split ltdb_index_onelevel() into a helper function
authorAndrew Bartlett <abartlet@samba.org>
Mon, 21 Aug 2017 00:58:58 +0000 (12:58 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 22 Sep 2017 19:20:21 +0000 (21:20 +0200)
This will allow the code to be re-used for storing the DN->GUID index

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
lib/ldb/ldb_tdb/ldb_index.c

index 52cf3385a5a9f721f58b0dc0992f420b34a0641d..9b5853f6c344666f512856d0fe5a5309eca9203d 100644 (file)
@@ -1424,43 +1424,25 @@ static int ltdb_index_add_all(struct ldb_module *module,
 
 
 /*
-  insert a one level index for a message
+  insert a DN index for a message
 */
-static int ltdb_index_onelevel(struct ldb_module *module,
-                              const struct ldb_message *msg, int add)
-{      
-       struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
-                                                   struct ltdb_private);
+static int ltdb_modify_index_dn(struct ldb_module *module,
+                               struct ltdb_private *ltdb,
+                               const struct ldb_message *msg,
+                               struct ldb_dn *dn,
+                               const char *index, int add)
+{
        struct ldb_message_element el;
        struct ldb_val val;
-       struct ldb_dn *pdn;
-       const char *dn;
        int ret;
 
-       /* We index for ONE Level only if requested */
-       if (!ltdb->cache->one_level_indexes) {
-               return LDB_SUCCESS;
-       }
-
-       pdn = ldb_dn_get_parent(module, msg->dn);
-       if (pdn == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       dn = ldb_dn_get_linearized(msg->dn);
-       if (dn == NULL) {
-               talloc_free(pdn);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(pdn));
+       val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
        if (val.data == NULL) {
-               talloc_free(pdn);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        val.length = strlen((char *)val.data);
-       el.name = LTDB_IDXONE;
+       el.name = index;
        el.values = &val;
        el.num_values = 1;
 
@@ -1470,6 +1452,44 @@ static int ltdb_index_onelevel(struct ldb_module *module,
                ret = ltdb_index_del_value(module, ltdb, msg, &el, 0);
        }
 
+       if (ret != LDB_SUCCESS) {
+               struct ldb_context *ldb = ldb_module_get_ctx(module);
+               const char *dn_str = ldb_dn_get_linearized(dn);
+               ldb_asprintf_errstring(ldb,
+                                      __location__
+                                      ": Failed to modify %s "
+                                      "against %s in %s - %s",
+                                      index,
+                                      ltdb->cache->GUID_index_attribute,
+                                      dn_str, ldb_errstring(ldb));
+               return ret;
+       }
+       return ret;
+}
+
+/*
+  insert a one level index for a message
+*/
+static int ltdb_index_onelevel(struct ldb_module *module,
+                              const struct ldb_message *msg, int add)
+{
+       struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
+                                                   struct ltdb_private);
+       struct ldb_dn *pdn;
+       int ret;
+
+       /* We index for ONE Level only if requested */
+       if (!ltdb->cache->one_level_indexes) {
+               return LDB_SUCCESS;
+       }
+
+       pdn = ldb_dn_get_parent(module, msg->dn);
+       if (pdn == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       ret = ltdb_modify_index_dn(module, ltdb,
+                                  msg, pdn, LTDB_IDXONE, add);
+
        talloc_free(pdn);
 
        return ret;