s4-dsdb Allow repl server to start even when no master NCs are present
[Samba/gebeck_regimport.git] / lib / util / util_tdb.c
blob02c7095f66b9fcd8e5c02435e9e97d79c112ef1d
1 /*
2 Unix SMB/CIFS implementation.
4 tdb utility functions
6 Copyright (C) Andrew Tridgell 1992-2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "../lib/tdb_compat/tdb_compat.h"
24 #include "../lib/util/util_tdb.h"
26 /* these are little tdb utility functions that are meant to make
27 dealing with a tdb database a little less cumbersome in Samba */
29 /***************************************************************
30 Make a TDB_DATA and keep the const warning in one place
31 ****************************************************************/
33 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
35 TDB_DATA ret;
36 ret.dptr = discard_const_p(uint8_t, dptr);
37 ret.dsize = dsize;
38 return ret;
41 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
43 if (t1.dsize != t2.dsize) {
44 return false;
46 return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
49 TDB_DATA string_tdb_data(const char *string)
51 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
54 TDB_DATA string_term_tdb_data(const char *string)
56 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
59 /****************************************************************************
60 Lock a chain by string. Return non-zero if lock failed.
61 ****************************************************************************/
63 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
65 TDB_DATA key = string_term_tdb_data(keyval);
67 return tdb_chainlock(tdb, key);
70 /****************************************************************************
71 Unlock a chain by string.
72 ****************************************************************************/
74 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
76 TDB_DATA key = string_term_tdb_data(keyval);
78 tdb_chainunlock(tdb, key);
81 /****************************************************************************
82 Read lock a chain by string. Return non-zero if lock failed.
83 ****************************************************************************/
85 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
87 TDB_DATA key = string_term_tdb_data(keyval);
89 return tdb_chainlock_read(tdb, key);
92 /****************************************************************************
93 Read unlock a chain by string.
94 ****************************************************************************/
96 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
98 TDB_DATA key = string_term_tdb_data(keyval);
100 tdb_chainunlock_read(tdb, key);
104 /****************************************************************************
105 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
106 Output is int32_t in native byte order.
107 ****************************************************************************/
109 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
111 TDB_DATA data;
112 int32_t ret;
114 data = tdb_fetch_compat(tdb, key);
115 if (!data.dptr || data.dsize != sizeof(int32_t)) {
116 SAFE_FREE(data.dptr);
117 return -1;
120 ret = IVAL(data.dptr,0);
121 SAFE_FREE(data.dptr);
122 return ret;
125 /****************************************************************************
126 Fetch a int32_t value by string key, return -1 if not found.
127 Output is int32_t in native byte order.
128 ****************************************************************************/
130 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
132 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
135 /****************************************************************************
136 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
137 Input is int32_t in native byte order. Output in tdb is in little-endian.
138 ****************************************************************************/
140 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
142 TDB_DATA data;
143 int32_t v_store;
145 SIVAL(&v_store,0,v);
146 data.dptr = (unsigned char *)&v_store;
147 data.dsize = sizeof(int32_t);
149 return tdb_store(tdb, key, data, TDB_REPLACE);
152 /****************************************************************************
153 Store a int32_t value by string key, return 0 on success, -ve on failure.
154 Input is int32_t in native byte order. Output in tdb is in little-endian.
155 ****************************************************************************/
157 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
159 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
162 /****************************************************************************
163 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
164 Output is uint32_t in native byte order.
165 ****************************************************************************/
167 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
169 TDB_DATA data;
171 data = tdb_fetch_compat(tdb, key);
172 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
173 SAFE_FREE(data.dptr);
174 return false;
177 *value = IVAL(data.dptr,0);
178 SAFE_FREE(data.dptr);
179 return true;
182 /****************************************************************************
183 Fetch a uint32_t value by string key, return false if not found.
184 Output is uint32_t in native byte order.
185 ****************************************************************************/
187 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
189 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
192 /****************************************************************************
193 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
194 Input is uint32_t in native byte order. Output in tdb is in little-endian.
195 ****************************************************************************/
197 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
199 TDB_DATA data;
200 uint32_t v_store;
201 bool ret = true;
203 SIVAL(&v_store, 0, value);
204 data.dptr = (unsigned char *)&v_store;
205 data.dsize = sizeof(uint32_t);
207 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
208 ret = false;
210 return ret;
213 /****************************************************************************
214 Store a uint32_t value by string key, return true on success, false on failure.
215 Input is uint32_t in native byte order. Output in tdb is in little-endian.
216 ****************************************************************************/
218 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
220 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
222 /****************************************************************************
223 Store a buffer by a null terminated string key. Return 0 on success, -ve
224 on failure.
225 ****************************************************************************/
227 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
229 TDB_DATA key = string_term_tdb_data(keystr);
231 return tdb_store(tdb, key, data, flags);
234 /****************************************************************************
235 Fetch a buffer using a null terminated string key. Don't forget to call
236 free() on the result dptr.
237 ****************************************************************************/
239 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
241 TDB_DATA key = string_term_tdb_data(keystr);
243 return tdb_fetch_compat(tdb, key);
246 /****************************************************************************
247 Delete an entry using a null terminated string key.
248 ****************************************************************************/
250 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
252 TDB_DATA key = string_term_tdb_data(keystr);
254 return tdb_delete(tdb, key);
257 /****************************************************************************
258 Atomic integer change. Returns old value. To create, set initial value in *oldval.
259 ****************************************************************************/
261 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
263 int32_t val;
264 int32_t ret = -1;
266 if (tdb_lock_bystring(tdb, keystr) != 0)
267 return -1;
269 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
270 /* The lookup failed */
271 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
272 /* but not because it didn't exist */
273 goto err_out;
276 /* Start with 'old' value */
277 val = *oldval;
279 } else {
280 /* It worked, set return value (oldval) to tdb data */
281 *oldval = val;
284 /* Increment value for storage and return next time */
285 val += change_val;
287 if (tdb_store_int32(tdb, keystr, val) != 0)
288 goto err_out;
290 ret = 0;
292 err_out:
294 tdb_unlock_bystring(tdb, keystr);
295 return ret;
298 /****************************************************************************
299 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
300 ****************************************************************************/
302 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
304 uint32_t val;
305 bool ret = false;
307 if (tdb_lock_bystring(tdb, keystr) != 0)
308 return false;
310 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
311 /* It failed */
312 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
313 /* and not because it didn't exist */
314 goto err_out;
317 /* Start with 'old' value */
318 val = *oldval;
320 } else {
321 /* it worked, set return value (oldval) to tdb data */
322 *oldval = val;
326 /* get a new value to store */
327 val += change_val;
329 if (!tdb_store_uint32(tdb, keystr, val))
330 goto err_out;
332 ret = true;
334 err_out:
336 tdb_unlock_bystring(tdb, keystr);
337 return ret;
340 /****************************************************************************
341 Allow tdb_delete to be used as a tdb_traversal_fn.
342 ****************************************************************************/
344 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
345 void *state)
347 return tdb_delete(the_tdb, key);