s4: Remove trailing whitespaces
[Samba.git] / source4 / lib / ldb / tools / ldbutil.c
blob5f7ea894dfd001abbf51d84e38c69a318ad00b80
1 /*
2 ldb database library utility
4 Copyright (C) Matthieu Patou 2009
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Description: Common function used by ldb_add/ldb_modify/ldb_delete
29 * Author: Matthieu Patou
32 #include "ldb.h"
33 #include "ldb_module.h"
35 /* autostarts a transacion if none active */
36 static int ldb_do_autotransaction(struct ldb_context *ldb,
37 struct ldb_request *req)
39 int ret;
41 ret = ldb_transaction_start(ldb);
42 if (ret != LDB_SUCCESS) {
43 return ret;
46 ret = ldb_request(ldb, req);
47 if (ret == LDB_SUCCESS) {
48 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
51 if (ret == LDB_SUCCESS) {
52 return ldb_transaction_commit(ldb);
54 ldb_transaction_cancel(ldb);
56 if (ldb_errstring(ldb) == NULL) {
57 /* no error string was setup by the backend */
58 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
61 return ret;
64 Same as ldb_add but accept control
66 int ldb_add_ctrl(struct ldb_context *ldb,
67 const struct ldb_message *message,
68 struct ldb_control **controls)
70 struct ldb_request *req;
71 int ret;
73 ret = ldb_msg_sanity_check(ldb, message);
74 if (ret != LDB_SUCCESS) {
75 return ret;
78 ret = ldb_build_add_req(&req, ldb, ldb,
79 message,
80 controls,
81 NULL,
82 ldb_op_default_callback,
83 NULL);
85 if (ret != LDB_SUCCESS) return ret;
87 /* do request and autostart a transaction */
88 ret = ldb_do_autotransaction(ldb, req);
90 talloc_free(req);
91 return ret;
95 same as ldb_delete but accept control
97 int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
98 struct ldb_control **controls)
100 struct ldb_request *req;
101 int ret;
103 ret = ldb_build_del_req(&req, ldb, ldb,
105 controls,
106 NULL,
107 ldb_op_default_callback,
108 NULL);
110 if (ret != LDB_SUCCESS) return ret;
112 /* do request and autostart a transaction */
113 ret = ldb_do_autotransaction(ldb, req);
115 talloc_free(req);
116 return ret;
121 same as ldb_modify, but accepts controls
123 int ldb_modify_ctrl(struct ldb_context *ldb,
124 const struct ldb_message *message,
125 struct ldb_control **controls)
127 struct ldb_request *req;
128 int ret;
130 ret = ldb_msg_sanity_check(ldb, message);
131 if (ret != LDB_SUCCESS) {
132 return ret;
135 ret = ldb_build_mod_req(&req, ldb, ldb,
136 message,
137 controls,
138 NULL,
139 ldb_op_default_callback,
140 NULL);
142 if (ret != LDB_SUCCESS) return ret;
144 /* do request and autostart a transaction */
145 ret = ldb_do_autotransaction(ldb, req);
147 talloc_free(req);
148 return ret;