[PATCH] pythonbindings: allow add() to have an array of controls as second parameter

Matthieu Patou mat at matws.net
Tue Sep 22 14:51:25 MDT 2009


---
 source4/lib/ldb/pyldb.c             |   79 +++++++++++++++++++++++++++++++++--
 source4/lib/ldb/tests/python/api.py |    8 ++++
 2 files changed, 83 insertions(+), 4 deletions(-)

diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c
index b4f03dc..8c2285d 100644
--- a/source4/lib/ldb/pyldb.c
+++ b/source4/lib/ldb/pyldb.c
@@ -656,6 +656,66 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
 	Py_RETURN_NONE;
 }
 
+/* autostarts a transacion if none active */
+static int ldb_autotransaction_request(struct ldb_context *ldb,
+                                       struct ldb_request *req)
+{
+        int ret;
+
+        ret = ldb_transaction_start(ldb);
+        if (ret != LDB_SUCCESS) {
+                return ret;
+        }
+
+        ret = ldb_request(ldb, req);
+        if (ret == LDB_SUCCESS) {
+                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+        }
+
+        if (ret == LDB_SUCCESS) {
+                return ldb_transaction_commit(ldb);
+        }
+        ldb_transaction_cancel(ldb);
+
+        if (ldb->err_string == NULL) {
+                /* no error string was setup by the backend */
+                ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
+        }
+
+        return ret;
+}
+/*
+  add a record to the database. Will fail if a record with the given class
+  and key already exists.
+  Allow to supply controls
+*/
+static int ldb_add_w_ctrls(struct ldb_context *ldb,
+            const struct ldb_message *message,
+            struct ldb_control **controls)
+{
+        struct ldb_request *req;
+        int ret;
+
+        ret = ldb_msg_sanity_check(ldb, message);
+        if (ret != LDB_SUCCESS) {
+                return ret;
+        }
+
+        ret = ldb_build_add_req(&req, ldb, ldb,
+                                        message,
+                                        controls,
+                                        NULL,
+                                        ldb_op_default_callback,
+                                        NULL);
+
+        if (ret != LDB_SUCCESS) return ret;
+
+        /* do request and autostart a transaction */
+        ret = ldb_autotransaction_request(ldb, req);
+
+        talloc_free(req);
+        return ret;
+}
 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 {
 	PyObject *py_msg;
@@ -663,21 +723,32 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 	Py_ssize_t dict_pos, msg_pos;
 	struct ldb_message_element *msgel;
 	struct ldb_message *msg;
+	struct ldb_control *ctrl;
+	struct ldb_context *ldb_ctx;
 	PyObject *key, *value;
+	PyObject *py_controls = Py_None;
 	TALLOC_CTX *mem_ctx;
+	struct ldb_control **parsed_controls;
 
-	if (!PyArg_ParseTuple(args, "O", &py_msg))
+	if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
 		return NULL;
+	ldb_ctx = PyLdb_AsLdbContext(self);
 
 	mem_ctx = talloc_new(NULL);
-
+	if (py_controls == Py_None) {
+		parsed_controls = NULL;
+	} else {
+		const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
+		parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+		talloc_free(controls);
+	}
 	if (PyDict_Check(py_msg)) {
 		PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
 		msg = ldb_msg_new(mem_ctx);
 		msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
 		msg_pos = dict_pos = 0;
 		if (dn_value) {
-		   	if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
+		   	if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
 		   		PyErr_SetString(PyExc_TypeError, "unable to import dn object");
 				talloc_free(mem_ctx);
 				return NULL;
@@ -714,7 +785,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 		msg = PyLdbMessage_AsMessage(py_msg);
 	}
 
-	ret = ldb_add(PyLdb_AsLdbContext(self), msg);
+	ret = ldb_add_w_ctrls(ldb_ctx, msg,parsed_controls);
 	talloc_free(mem_ctx);
 	PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
diff --git a/source4/lib/ldb/tests/python/api.py b/source4/lib/ldb/tests/python/api.py
index 133bd18..956908d 100755
--- a/source4/lib/ldb/tests/python/api.py
+++ b/source4/lib/ldb/tests/python/api.py
@@ -136,6 +136,14 @@ class SimpleLdb(unittest.TestCase):
         finally:
             l.delete(ldb.Dn(l, "dc=foo4"))
 
+    def test_add_w_unhandled_ctrl(self):
+        l = ldb.Ldb(filename())
+        m = ldb.Message()
+        m.dn = ldb.Dn(l, "dc=foo4")
+        m["bla"] = "bla"
+        self.assertEquals(len(l.search()), 1)
+        self.assertRaises(ldb.LdbError, lambda: l.add(m,["search_options:1:2"]))
+
     def test_add_dict(self):
         l = ldb.Ldb(filename())
         m = {"dn": ldb.Dn(l, "dc=foo5"),
-- 
1.6.0.4


--------------000309000708070802080304
Content-Type: text/x-patch;
 name="0003-s4-Improve-provisioning.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0003-s4-Improve-provisioning.patch"



More information about the samba-technical mailing list