python-ldb: allow ldb_rename to take optional control(s)
authorMatthieu Patou <mat@matws.net>
Sat, 4 Sep 2010 22:57:16 +0000 (02:57 +0400)
committerMatthieu Patou <mat@matws.net>
Sun, 5 Sep 2010 08:29:20 +0000 (12:29 +0400)
source4/lib/ldb/pyldb.c

index f1b73a96fb79e68aa843b43b14ce85a09d5558a0..b60f4fc3ee27277c678864305c1936b0a451d511 100644 (file)
@@ -898,18 +898,33 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
        int ret;
        struct ldb_context *ldb;
        TALLOC_CTX *mem_ctx;
+       PyObject *py_controls = Py_None;
+       struct ldb_control **parsed_controls;
+       struct ldb_context *ldb_ctx;
+       struct ldb_request *req;
 
-       if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
+       ldb_ctx = PyLdb_AsLdbContext(self);
+
+       if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls))
                return NULL;
 
+
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
                PyErr_NoMemory();
                return NULL;
        }
-
        ldb = PyLdb_AsLdbContext(self);
 
+       if (py_controls == Py_None) {
+               parsed_controls = NULL;
+       } else {
+               const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
+               talloc_free(controls);
+       }
+
+
        if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
                talloc_free(mem_ctx);
                return NULL;
@@ -920,9 +935,40 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ret = ldb_rename(ldb, dn1, dn2);
+       ret = ldb_build_rename_req(&req, ldb_ctx, mem_ctx, dn1, dn2, parsed_controls,
+                               NULL, ldb_op_default_callback, NULL);
+       if (ret != LDB_SUCCESS) {
+               PyErr_SetString(PyExc_TypeError, "failed to build request");
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       /* do request and autostart a transaction */
+       /* Then let's LDB handle the message error in case of pb as they are meaningful */
+
+       ret = ldb_transaction_start(ldb_ctx);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(mem_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+       }
+
+       ret = ldb_request(ldb_ctx, req);
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_transaction_commit(ldb_ctx);
+       } else {
+               ldb_transaction_cancel(ldb_ctx);
+               if (ldb_ctx->err_string == NULL) {
+                       /* no error string was setup by the backend */
+                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
+               }
+       }
+
        talloc_free(mem_ctx);
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }