lib/util: factor out tdb_data_is_empty
[Samba/gebeck_regimport.git] / lib / util / util_tdb.c
blob0c30d7774dfe67d378d6d4950eac6c18d9f9bb00
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 bool tdb_data_is_empty(TDB_DATA d) {
50 return (d.dsize == 0) || (d.dptr == NULL);
53 TDB_DATA string_tdb_data(const char *string)
55 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
58 TDB_DATA string_term_tdb_data(const char *string)
60 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
63 /****************************************************************************
64 Lock a chain by string. Return non-zero if lock failed.
65 ****************************************************************************/
67 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
69 TDB_DATA key = string_term_tdb_data(keyval);
71 return tdb_chainlock(tdb, key);
74 /****************************************************************************
75 Unlock a chain by string.
76 ****************************************************************************/
78 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
80 TDB_DATA key = string_term_tdb_data(keyval);
82 tdb_chainunlock(tdb, key);
85 /****************************************************************************
86 Read lock a chain by string. Return non-zero if lock failed.
87 ****************************************************************************/
89 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
91 TDB_DATA key = string_term_tdb_data(keyval);
93 return tdb_chainlock_read(tdb, key);
96 /****************************************************************************
97 Read unlock a chain by string.
98 ****************************************************************************/
100 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
102 TDB_DATA key = string_term_tdb_data(keyval);
104 tdb_chainunlock_read(tdb, key);
108 /****************************************************************************
109 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
110 Output is int32_t in native byte order.
111 ****************************************************************************/
113 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
115 TDB_DATA data;
116 int32_t ret;
118 data = tdb_fetch_compat(tdb, key);
119 if (!data.dptr || data.dsize != sizeof(int32_t)) {
120 SAFE_FREE(data.dptr);
121 return -1;
124 ret = IVAL(data.dptr,0);
125 SAFE_FREE(data.dptr);
126 return ret;
129 /****************************************************************************
130 Fetch a int32_t value by string key, return -1 if not found.
131 Output is int32_t in native byte order.
132 ****************************************************************************/
134 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
136 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
139 /****************************************************************************
140 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
141 Input is int32_t in native byte order. Output in tdb is in little-endian.
142 ****************************************************************************/
144 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
146 TDB_DATA data;
147 int32_t v_store;
149 SIVAL(&v_store,0,v);
150 data.dptr = (unsigned char *)&v_store;
151 data.dsize = sizeof(int32_t);
153 return tdb_store(tdb, key, data, TDB_REPLACE);
156 /****************************************************************************
157 Store a int32_t value by string key, return 0 on success, -ve on failure.
158 Input is int32_t in native byte order. Output in tdb is in little-endian.
159 ****************************************************************************/
161 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
163 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
166 /****************************************************************************
167 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
168 Output is uint32_t in native byte order.
169 ****************************************************************************/
171 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
173 TDB_DATA data;
175 data = tdb_fetch_compat(tdb, key);
176 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
177 SAFE_FREE(data.dptr);
178 return false;
181 *value = IVAL(data.dptr,0);
182 SAFE_FREE(data.dptr);
183 return true;
186 /****************************************************************************
187 Fetch a uint32_t value by string key, return false if not found.
188 Output is uint32_t in native byte order.
189 ****************************************************************************/
191 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
193 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
196 /****************************************************************************
197 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
198 Input is uint32_t in native byte order. Output in tdb is in little-endian.
199 ****************************************************************************/
201 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
203 TDB_DATA data;
204 uint32_t v_store;
205 bool ret = true;
207 SIVAL(&v_store, 0, value);
208 data.dptr = (unsigned char *)&v_store;
209 data.dsize = sizeof(uint32_t);
211 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
212 ret = false;
214 return ret;
217 /****************************************************************************
218 Store a uint32_t value by string key, return true on success, false on failure.
219 Input is uint32_t in native byte order. Output in tdb is in little-endian.
220 ****************************************************************************/
222 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
224 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
226 /****************************************************************************
227 Store a buffer by a null terminated string key. Return 0 on success, -ve
228 on failure.
229 ****************************************************************************/
231 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
233 TDB_DATA key = string_term_tdb_data(keystr);
235 return tdb_store(tdb, key, data, flags);
238 /****************************************************************************
239 Fetch a buffer using a null terminated string key. Don't forget to call
240 free() on the result dptr.
241 ****************************************************************************/
243 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
245 TDB_DATA key = string_term_tdb_data(keystr);
247 return tdb_fetch_compat(tdb, key);
250 /****************************************************************************
251 Delete an entry using a null terminated string key.
252 ****************************************************************************/
254 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
256 TDB_DATA key = string_term_tdb_data(keystr);
258 return tdb_delete(tdb, key);
261 /****************************************************************************
262 Atomic integer change. Returns old value. To create, set initial value in *oldval.
263 ****************************************************************************/
265 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
267 int32_t val;
268 int32_t ret = -1;
270 if (tdb_lock_bystring(tdb, keystr) != 0)
271 return -1;
273 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
274 /* The lookup failed */
275 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
276 /* but not because it didn't exist */
277 goto err_out;
280 /* Start with 'old' value */
281 val = *oldval;
283 } else {
284 /* It worked, set return value (oldval) to tdb data */
285 *oldval = val;
288 /* Increment value for storage and return next time */
289 val += change_val;
291 if (tdb_store_int32(tdb, keystr, val) != 0)
292 goto err_out;
294 ret = 0;
296 err_out:
298 tdb_unlock_bystring(tdb, keystr);
299 return ret;
302 /****************************************************************************
303 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
304 ****************************************************************************/
306 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
308 uint32_t val;
309 bool ret = false;
311 if (tdb_lock_bystring(tdb, keystr) != 0)
312 return false;
314 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
315 /* It failed */
316 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
317 /* and not because it didn't exist */
318 goto err_out;
321 /* Start with 'old' value */
322 val = *oldval;
324 } else {
325 /* it worked, set return value (oldval) to tdb data */
326 *oldval = val;
330 /* get a new value to store */
331 val += change_val;
333 if (!tdb_store_uint32(tdb, keystr, val))
334 goto err_out;
336 ret = true;
338 err_out:
340 tdb_unlock_bystring(tdb, keystr);
341 return ret;
344 /****************************************************************************
345 Allow tdb_delete to be used as a tdb_traversal_fn.
346 ****************************************************************************/
348 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
349 void *state)
351 return tdb_delete(the_tdb, key);