63d80d1fb5869ad45fbd6ebc0f20f2ad736e4eec
[nivanova/samba-autobuild/.git] / lib / util / util_tdb.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Andrew Tridgell 1992-2006
7
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.
12
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.
17
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/>.
20 */
21
22 #ifndef _____LIB_UTIL_UTIL_TDB_H__
23 #define _____LIB_UTIL_UTIL_TDB_H__
24
25 #include <tdb.h>
26
27 /***************************************************************
28  Make a TDB_DATA and keep the const warning in one place
29 ****************************************************************/
30 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize);
31 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2);
32 bool tdb_data_is_empty(TDB_DATA d);
33 TDB_DATA string_tdb_data(const char *string);
34 TDB_DATA string_term_tdb_data(const char *string);
35 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data);
36
37 /****************************************************************************
38  Lock a chain by string. Return non-zero if lock failed.
39 ****************************************************************************/
40 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval);
41
42 /****************************************************************************
43  Unlock a chain by string.
44 ****************************************************************************/
45 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval);
46
47 /****************************************************************************
48  Read lock a chain by string. Return non-zero if lock failed.
49 ****************************************************************************/
50 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval);
51
52 /****************************************************************************
53  Read unlock a chain by string.
54 ****************************************************************************/
55 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval);
56
57 /****************************************************************************
58  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
59  Output is int32_t in native byte order.
60 ****************************************************************************/
61 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key);
62
63 /****************************************************************************
64  Fetch a int32_t value by string key, return -1 if not found.
65  Output is int32_t in native byte order.
66 ****************************************************************************/
67 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr);
68
69 /****************************************************************************
70  Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
71  Input is int32_t in native byte order. Output in tdb is in little-endian.
72 ****************************************************************************/
73 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v);
74
75 /****************************************************************************
76  Store a int32_t value by string key, return 0 on success, -ve on failure.
77  Input is int32_t in native byte order. Output in tdb is in little-endian.
78 ****************************************************************************/
79 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v);
80
81 /****************************************************************************
82  Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
83  Output is uint32_t in native byte order.
84 ****************************************************************************/
85 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value);
86
87 /****************************************************************************
88  Fetch a uint32_t value by string key, return -1 if not found.
89  Output is uint32_t in native byte order.
90 ****************************************************************************/
91 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value);
92
93 /****************************************************************************
94  Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
95  Input is uint32_t in native byte order. Output in tdb is in little-endian.
96 ****************************************************************************/
97 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value);
98
99 /****************************************************************************
100  Store a uint32_t value by string key, return true on success, false on failure.
101  Input is uint32_t in native byte order. Output in tdb is in little-endian.
102 ****************************************************************************/
103 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value);
104
105 /****************************************************************************
106  Store a buffer by a null terminated string key.  Return 0 on success, -ve
107  on failure.
108 ****************************************************************************/
109 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags);
110
111 /****************************************************************************
112  Fetch a buffer using a null terminated string key.  Don't forget to call
113  free() on the result dptr.
114 ****************************************************************************/
115 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr);
116
117 /****************************************************************************
118  Delete an entry using a null terminated string key.  0 on success, -ve on err.
119 ****************************************************************************/
120 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr);
121
122 /****************************************************************************
123  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
124 ****************************************************************************/
125 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val);
126
127 /****************************************************************************
128  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
129 ****************************************************************************/
130 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val);
131
132 /****************************************************************************
133  Allow tdb_delete to be used as a tdb_traversal_fn.
134 ****************************************************************************/
135 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
136                      void *state);
137
138 /****************************************************************************
139  Return an NTSTATUS from a TDB_ERROR
140 ****************************************************************************/
141
142 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err);
143
144 /****************************************************************************
145  Return an errno from a TDB_ERROR
146 ****************************************************************************/
147
148 int map_unix_error_from_tdb(enum TDB_ERROR err);
149
150 int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
151                      TALLOC_CTX *mem_ctx, uint8_t **buf);
152
153 #endif /* _____LIB_UTIL_UTIL_TDB_H__ */