Author: idra
Date: 2006-09-21 06:14:32 +0000 (Thu, 21 Sep 2006)
New Revision: 18777

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=18777

Log:

add helper functions to create an ldb_request structure


Modified:
   branches/SAMBA_4_0/source/lib/ldb/common/ldb.c
   branches/SAMBA_4_0/source/lib/ldb/include/ldb.h


Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb.c      2006-09-21 06:06:59 UTC 
(rev 18776)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb.c      2006-09-21 06:14:32 UTC 
(rev 18777)
@@ -559,34 +559,33 @@
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-/*
-  note that ldb_search() will automatically replace a NULL 'base' value with 
the 
-  defaultNamingContext from the rootDSE if available.
-*/
-int ldb_search(struct ldb_context *ldb, 
-              const struct ldb_dn *base,
-              enum ldb_scope scope,
-              const char *expression,
-              const char * const *attrs, 
-              struct ldb_result **res)
+int ldb_build_search_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       const struct ldb_dn *base,
+                       enum ldb_scope scope,
+                       const char *expression,
+                       const char * const *attrs,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
 {
        struct ldb_request *req;
-       int ret;
 
-       *res = NULL;
-       
-       req = talloc(ldb, struct ldb_request);
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
        if (req == NULL) {
                ldb_set_errstring(ldb, "Out of Memory");
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       req->operation = LDB_SEARCH;
        if (base == NULL) {
-               base = ldb_get_default_basedn(ldb);
+               req->op.search.base = ldb_dn_new(req);
+       } else {
+               req->op.search.base = base;
        }
-
-       req->operation = LDB_SEARCH;
-       req->op.search.base = base;
        req->op.search.scope = scope;
 
        req->op.search.tree = ldb_parse_tree(req, expression);
@@ -596,16 +595,163 @@
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       req->op.search.attrs = attrs;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+       return LDB_SUCCESS;
+}
+
+int ldb_build_add_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_message *message,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_ADD;
+       req->op.add.message = message;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_mod_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_message *message,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_MODIFY;
+       req->op.mod.message = message;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_del_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_dn *dn,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_DELETE;
+       req->op.del.dn = dn;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+int ldb_build_rename_req(struct ldb_request **ret_req,
+                       struct ldb_context *ldb,
+                       void *mem_ctx,
+                       struct ldb_dn *olddn,
+                       struct ldb_dn *newdn,
+                       struct ldb_control **controls,
+                       void *context,
+                       ldb_request_callback_t callback)
+{
+       struct ldb_request *req;
+
+       *ret_req = NULL;
+
+       req = talloc(mem_ctx, struct ldb_request);
+       if (req == NULL) {
+               ldb_set_errstring(ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       req->operation = LDB_RENAME;
+       req->op.rename.olddn = olddn;
+       req->op.rename.newdn = newdn;
+       req->controls = controls;
+       req->context = context;
+       req->callback = callback;
+
+       *ret_req = req;
+
+       return LDB_SUCCESS;
+}
+
+/*
+  note that ldb_search() will automatically replace a NULL 'base' value with 
the 
+  defaultNamingContext from the rootDSE if available.
+*/
+int ldb_search(struct ldb_context *ldb, 
+              const struct ldb_dn *base,
+              enum ldb_scope scope,
+              const char *expression,
+              const char * const *attrs, 
+              struct ldb_result **res)
+{
+       struct ldb_request *req;
+       int ret;
+
        *res = talloc_zero(ldb, struct ldb_result);
        if (! *res) {
-               talloc_free(req);
                return LDB_ERR_OPERATIONS_ERROR;
        }
+       
+       ret = ldb_build_search_req(&req, ldb, ldb,
+                                       base?base:ldb_get_default_basedn(ldb),
+                                       scope,
+                                       expression,
+                                       attrs,
+                                       NULL,
+                                       res,
+                                       ldb_search_callback);
 
-       req->op.search.attrs = attrs;
-       req->controls = NULL;
-       req->context = res;
-       req->callback = ldb_search_callback;
+       if (ret != LDB_SUCCESS) goto done;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        ret = ldb_request(ldb, req);
@@ -613,17 +759,18 @@
        if (ret == LDB_SUCCESS) {
                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
-       
+
+       talloc_free(req);
+
+done:
        if (ret != LDB_SUCCESS) {
                talloc_free(*res);
                *res = NULL;
        }
 
-       talloc_free(req);
        return ret;
 }
 
-
 /*
   add a record to the database. Will fail if a record with the given class and 
key
   already exists
@@ -638,18 +785,15 @@
        if (ret != LDB_SUCCESS) {
                return ret;
        }
-               
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
 
-       req->operation = LDB_ADD;
-       req->op.add.message = message;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
+       ret = ldb_build_add_req(&req, ldb, ldb,
+                                       message,
+                                       NULL,
+                                       NULL,
+                                       NULL);
+
+       if (ret != LDB_SUCCESS) return ret;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -671,17 +815,14 @@
        ret = ldb_msg_sanity_check(ldb, message);
        if (ret != LDB_SUCCESS) return ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_mod_req(&req, ldb, ldb,
+                                       message,
+                                       NULL,
+                                       NULL,
+                                       NULL);
 
-       req->operation = LDB_MODIFY;
-       req->op.add.message = message;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
+       if (ret != LDB_SUCCESS) return ret;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -700,17 +841,14 @@
        struct ldb_request *req;
        int ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_del_req(&req, ldb, ldb,
+                                       dn,
+                                       NULL,
+                                       NULL,
+                                       NULL);
 
-       req->operation = LDB_DELETE;
-       req->op.del.dn = dn;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
+       if (ret != LDB_SUCCESS) return ret;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */
@@ -728,18 +866,15 @@
        struct ldb_request *req;
        int ret;
 
-       req = talloc(ldb, struct ldb_request);
-       if (req == NULL) {
-               ldb_set_errstring(ldb, "Out of Memory!");
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
+       ret = ldb_build_rename_req(&req, ldb, ldb,
+                                       olddn,
+                                       newdn,
+                                       NULL,
+                                       NULL,
+                                       NULL);
 
-       req->operation = LDB_RENAME;
-       req->op.rename.olddn = olddn;
-       req->op.rename.newdn = newdn;
-       req->controls = NULL;
-       req->context = NULL;
-       req->callback = NULL;
+       if (ret != LDB_SUCCESS) return ret;
+
        ldb_set_timeout(ldb, req, 0); /* use default timeout */
 
        /* do request and autostart a transaction */

Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb.h     2006-09-21 06:06:59 UTC 
(rev 18776)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb.h     2006-09-21 06:14:32 UTC 
(rev 18777)
@@ -743,6 +743,7 @@
        uint64_t seq_num;
 };
 
+typedef int (*ldb_request_callback_t)(struct ldb_context *, void *, struct 
ldb_reply *);
 struct ldb_request {
 
        enum ldb_request_type operation;
@@ -761,7 +762,7 @@
        struct ldb_control **controls;
 
        void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
+       ldb_request_callback_t callback;
 
        int timeout;
        time_t starttime;

Reply via email to