spoolss-iremotewinspool-tests: Use more recent client OS version
[Samba.git] / lib / util / util_tdb.c
blob24fb6584899760329b9836b51f1de6a9af69656d
1 /*
2 Unix SMB/CIFS implementation.
4 tdb utility functions
6 Copyright (C) Andrew Tridgell 1992-2006
7 Copyright (C) Volker Lendecke 2007-2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "replace.h"
24 #include <talloc.h>
25 #include "libcli/util/ntstatus.h"
26 #include "lib/util/memory.h"
27 #include "lib/util/byteorder.h"
28 #include "system/filesys.h"
29 #include "../lib/tdb/include/tdb.h"
30 #include "../lib/util/util_tdb.h"
32 /* these are little tdb utility functions that are meant to make
33 dealing with a tdb database a little less cumbersome in Samba */
35 /***************************************************************
36 Make a TDB_DATA and keep the const warning in one place
37 ****************************************************************/
39 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
41 TDB_DATA ret;
42 ret.dptr = discard_const_p(uint8_t, dptr);
43 ret.dsize = dsize;
44 return ret;
47 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
49 if (t1.dsize != t2.dsize) {
50 return false;
52 return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
55 bool tdb_data_is_empty(TDB_DATA d)
57 return (d.dsize == 0) || (d.dptr == NULL);
60 TDB_DATA string_tdb_data(const char *string)
62 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
65 TDB_DATA string_term_tdb_data(const char *string)
67 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
70 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
71 TDB_DATA ret = {
72 .dptr = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
73 .dsize = data.dsize
75 if (ret.dptr == NULL) {
76 ret.dsize = 0;
77 } else {
78 memcpy(ret.dptr, data.dptr, data.dsize);
79 ret.dptr[ret.dsize] = '\0';
81 return ret;
85 /****************************************************************************
86 Lock a chain by string. Return non-zero if lock failed.
87 ****************************************************************************/
89 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
91 TDB_DATA key = string_term_tdb_data(keyval);
93 return tdb_chainlock(tdb, key);
96 /****************************************************************************
97 Unlock a chain by string.
98 ****************************************************************************/
100 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
102 TDB_DATA key = string_term_tdb_data(keyval);
104 tdb_chainunlock(tdb, key);
107 /****************************************************************************
108 Read lock a chain by string. Return non-zero if lock failed.
109 ****************************************************************************/
111 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
113 TDB_DATA key = string_term_tdb_data(keyval);
115 return tdb_chainlock_read(tdb, key);
118 /****************************************************************************
119 Read unlock a chain by string.
120 ****************************************************************************/
122 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
124 TDB_DATA key = string_term_tdb_data(keyval);
126 tdb_chainunlock_read(tdb, key);
130 /****************************************************************************
131 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
132 Output is int32_t in native byte order.
133 ****************************************************************************/
135 static int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
137 TDB_DATA data;
138 int32_t ret;
140 data = tdb_fetch(tdb, key);
141 if (!data.dptr || data.dsize != sizeof(int32_t)) {
142 SAFE_FREE(data.dptr);
143 return -1;
146 ret = IVAL(data.dptr,0);
147 SAFE_FREE(data.dptr);
148 return ret;
151 /****************************************************************************
152 Fetch a int32_t value by string key, return -1 if not found.
153 Output is int32_t in native byte order.
154 ****************************************************************************/
156 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
158 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
161 /****************************************************************************
162 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
163 Input is int32_t in native byte order. Output in tdb is in little-endian.
164 ****************************************************************************/
166 static int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key,
167 int32_t v)
169 TDB_DATA data;
170 int32_t v_store;
172 SIVAL(&v_store,0,v);
173 data.dptr = (unsigned char *)&v_store;
174 data.dsize = sizeof(int32_t);
176 return tdb_store(tdb, key, data, TDB_REPLACE);
179 /****************************************************************************
180 Store a int32_t value by string key, return 0 on success, -ve on failure.
181 Input is int32_t in native byte order. Output in tdb is in little-endian.
182 ****************************************************************************/
184 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
186 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
189 /****************************************************************************
190 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
191 Output is uint32_t in native byte order.
192 ****************************************************************************/
194 static bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
195 uint32_t *value)
197 TDB_DATA data;
199 data = tdb_fetch(tdb, key);
200 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
201 SAFE_FREE(data.dptr);
202 return false;
205 *value = IVAL(data.dptr,0);
206 SAFE_FREE(data.dptr);
207 return true;
210 /****************************************************************************
211 Fetch a uint32_t value by string key, return false if not found.
212 Output is uint32_t in native byte order.
213 ****************************************************************************/
215 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
217 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
220 /****************************************************************************
221 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
222 Input is uint32_t in native byte order. Output in tdb is in little-endian.
223 ****************************************************************************/
225 static bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
226 uint32_t value)
228 TDB_DATA data;
229 uint32_t v_store;
230 bool ret = true;
232 SIVAL(&v_store, 0, value);
233 data.dptr = (unsigned char *)&v_store;
234 data.dsize = sizeof(uint32_t);
236 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
237 ret = false;
239 return ret;
242 /****************************************************************************
243 Store a uint32_t value by string key, return true on success, false on failure.
244 Input is uint32_t in native byte order. Output in tdb is in little-endian.
245 ****************************************************************************/
247 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
249 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
251 /****************************************************************************
252 Store a buffer by a null terminated string key. Return 0 on success, -ve
253 on failure.
254 ****************************************************************************/
256 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
258 TDB_DATA key = string_term_tdb_data(keystr);
260 return tdb_store(tdb, key, data, flags);
263 /****************************************************************************
264 Fetch a buffer using a null terminated string key. Don't forget to call
265 free() on the result dptr.
266 ****************************************************************************/
268 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
270 TDB_DATA key = string_term_tdb_data(keystr);
272 return tdb_fetch(tdb, key);
275 /****************************************************************************
276 Delete an entry using a null terminated string key.
277 ****************************************************************************/
279 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
281 TDB_DATA key = string_term_tdb_data(keystr);
283 return tdb_delete(tdb, key);
286 /****************************************************************************
287 Atomic integer change. Returns old value. To create, set initial value in *oldval.
288 ****************************************************************************/
290 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
292 int32_t val;
293 int32_t ret = -1;
295 if (tdb_lock_bystring(tdb, keystr) != 0)
296 return -1;
298 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
299 /* The lookup failed */
300 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
301 /* but not because it didn't exist */
302 goto err_out;
305 /* Start with 'old' value */
306 val = *oldval;
308 } else {
309 /* It worked, set return value (oldval) to tdb data */
310 *oldval = val;
313 /* Increment value for storage and return next time */
314 val += change_val;
316 if (tdb_store_int32(tdb, keystr, val) != 0)
317 goto err_out;
319 ret = 0;
321 err_out:
323 tdb_unlock_bystring(tdb, keystr);
324 return ret;
327 /****************************************************************************
328 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
329 ****************************************************************************/
331 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
333 uint32_t val;
334 bool ret = false;
336 if (tdb_lock_bystring(tdb, keystr) != 0)
337 return false;
339 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
340 /* It failed */
341 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
342 /* and not because it didn't exist */
343 goto err_out;
346 /* Start with 'old' value */
347 val = *oldval;
349 } else {
350 /* it worked, set return value (oldval) to tdb data */
351 *oldval = val;
355 /* get a new value to store */
356 val += change_val;
358 if (!tdb_store_uint32(tdb, keystr, val))
359 goto err_out;
361 ret = true;
363 err_out:
365 tdb_unlock_bystring(tdb, keystr);
366 return ret;
369 /****************************************************************************
370 Allow tdb_delete to be used as a tdb_traversal_fn.
371 ****************************************************************************/
373 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
374 void *state)
376 return tdb_delete(the_tdb, key);
379 /****************************************************************************
380 Return an NTSTATUS from a TDB_ERROR
381 ****************************************************************************/
383 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
385 NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
387 switch (err) {
388 case TDB_SUCCESS:
389 result = NT_STATUS_OK;
390 break;
391 case TDB_ERR_CORRUPT:
392 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
393 break;
394 case TDB_ERR_IO:
395 result = NT_STATUS_UNEXPECTED_IO_ERROR;
396 break;
397 case TDB_ERR_OOM:
398 result = NT_STATUS_NO_MEMORY;
399 break;
400 case TDB_ERR_EXISTS:
401 result = NT_STATUS_OBJECT_NAME_COLLISION;
402 break;
404 case TDB_ERR_LOCK:
406 * TDB_ERR_LOCK is very broad, we could for example
407 * distinguish between fcntl locks and invalid lock
408 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
409 * compromise.
411 result = NT_STATUS_FILE_LOCK_CONFLICT;
412 break;
414 case TDB_ERR_NOLOCK:
415 case TDB_ERR_LOCK_TIMEOUT:
417 * These two ones in the enum are not actually used
419 result = NT_STATUS_FILE_LOCK_CONFLICT;
420 break;
421 case TDB_ERR_NOEXIST:
422 result = NT_STATUS_NOT_FOUND;
423 break;
424 case TDB_ERR_EINVAL:
425 result = NT_STATUS_INVALID_PARAMETER;
426 break;
427 case TDB_ERR_RDONLY:
428 result = NT_STATUS_ACCESS_DENIED;
429 break;
430 case TDB_ERR_NESTING:
431 result = NT_STATUS_INTERNAL_ERROR;
432 break;
434 return result;
437 int map_unix_error_from_tdb(enum TDB_ERROR err)
439 int result = EINVAL;
441 switch (err) {
442 case TDB_SUCCESS:
443 result = 0;
444 break;
445 case TDB_ERR_CORRUPT:
446 result = EILSEQ;
447 break;
448 case TDB_ERR_IO:
449 result = EIO;
450 break;
451 case TDB_ERR_OOM:
452 result = ENOMEM;
453 break;
454 case TDB_ERR_EXISTS:
455 result = EEXIST;
456 break;
458 case TDB_ERR_LOCK:
460 * TDB_ERR_LOCK is very broad, we could for example
461 * distinguish between fcntl locks and invalid lock
462 * sequences. EWOULDBLOCK is wrong, but there is no real
463 * generic lock error code in errno.h
465 result = EWOULDBLOCK;
466 break;
468 case TDB_ERR_NOLOCK:
469 case TDB_ERR_LOCK_TIMEOUT:
471 * These two ones in the enum are not actually used
473 result = ENOLCK;
474 break;
475 case TDB_ERR_NOEXIST:
476 result = ENOENT;
477 break;
478 case TDB_ERR_EINVAL:
479 result = EINVAL;
480 break;
481 case TDB_ERR_RDONLY:
482 result = EROFS;
483 break;
484 case TDB_ERR_NESTING:
486 * Well, this db is already busy...
488 result = EBUSY;
489 break;
491 return result;
494 struct tdb_fetch_talloc_state {
495 TALLOC_CTX *mem_ctx;
496 uint8_t *buf;
499 static int tdb_fetch_talloc_parser(TDB_DATA key, TDB_DATA data,
500 void *private_data)
502 struct tdb_fetch_talloc_state *state = private_data;
503 state->buf = talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
504 return 0;
507 int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
508 TALLOC_CTX *mem_ctx, uint8_t **buf)
510 struct tdb_fetch_talloc_state state = { .mem_ctx = mem_ctx };
511 int ret;
513 ret = tdb_parse_record(tdb, key, tdb_fetch_talloc_parser, &state);
514 if (ret == -1) {
515 enum TDB_ERROR err = tdb_error(tdb);
516 return map_unix_error_from_tdb(err);
519 if (state.buf == NULL) {
520 return ENOMEM;
523 *buf = state.buf;
524 return 0;