ldb_tdb: Rework ltdb_modify_internal() to use ltdb_search_dn1() internally
authorAndrew Bartlett <abartlet@samba.org>
Wed, 16 Aug 2017 00:51:09 +0000 (12:51 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 29 Aug 2017 09:13:50 +0000 (11:13 +0200)
This avoids duplicate code and allows us to use the allocation-avoiding
LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC flag.

We can not use LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC as el2->values
is talloc_realloc()ed in the routine.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Tue Aug 29 11:13:50 CEST 2017 on sn-devel-144

lib/ldb/ldb_tdb/ldb_tdb.c

index 2ac1967ee15bd45014786cc710acb93e1f3c5157..bc8780ad7eebd2be8900fcac21e43fc8157b2660 100644 (file)
@@ -686,52 +686,34 @@ int ltdb_modify_internal(struct ldb_module *module,
                         struct ldb_request *req)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
-       void *data = ldb_module_get_private(module);
-       struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
-       TDB_DATA tdb_key, tdb_data;
-       struct ldb_val ldb_data;
        struct ldb_message *msg2;
        unsigned int i, j;
        int ret = LDB_SUCCESS, idx;
        struct ldb_control *control_permissive = NULL;
+       TALLOC_CTX *mem_ctx = talloc_new(req);
 
+       if (mem_ctx == NULL) {
+               return ldb_module_oom(module);
+       }
+       
        if (req) {
                control_permissive = ldb_request_get_control(req,
                                        LDB_CONTROL_PERMISSIVE_MODIFY_OID);
        }
 
-       tdb_key = ltdb_key(module, msg->dn);
-       if (!tdb_key.dptr) {
-               return LDB_ERR_OTHER;
-       }
-
-       tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
-       if (!tdb_data.dptr) {
-               talloc_free(tdb_key.dptr);
-               return ltdb_err_map(tdb_error(ltdb->tdb));
-       }
-
-       msg2 = ldb_msg_new(tdb_key.dptr);
+       msg2 = ldb_msg_new(mem_ctx);
        if (msg2 == NULL) {
-               free(tdb_data.dptr);
                ret = LDB_ERR_OTHER;
                goto done;
        }
 
-       ldb_data.data = tdb_data.dptr;
-       ldb_data.length = tdb_data.dsize;
-
-       ret = ldb_unpack_data(ldb_module_get_ctx(module), &ldb_data, msg2);
-       free(tdb_data.dptr);
-       if (ret == -1) {
-               ret = LDB_ERR_OTHER;
+       ret = ltdb_search_dn1(module, msg->dn,
+                             msg2,
+                             LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+       if (ret != LDB_SUCCESS) {
                goto done;
        }
 
-       if (!msg2->dn) {
-               msg2->dn = msg->dn;
-       }
-
        for (i=0; i<msg->num_elements; i++) {
                struct ldb_message_element *el = &msg->elements[i], *el2;
                struct ldb_val *vals;
@@ -1018,7 +1000,7 @@ int ltdb_modify_internal(struct ldb_module *module,
        }
 
 done:
-       talloc_free(tdb_key.dptr);
+       TALLOC_FREE(mem_ctx);
        return ret;
 }