patch to allow controls in add function for python bindings

Jelmer Vernooij jelmer at samba.org
Wed Sep 23 06:41:26 MDT 2009


Hi Matthieu,

Matthieu Patou wrote:
> Take 2
Almost there...
   
> 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;
> +}
>   
^^^ Doesn't LDB do this by itself already? I don't think it's necessary.

> +/*
> +  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;
> +}
>   
^^ Please keep an empty line in between functions, per the coding style.
This code also seems simple enough to fold into py_ldb_add. That way you
can also raise exceptions with clearer messages.

Cheers,

Jelmer


More information about the samba-technical mailing list