2 Unix SMB/CIFS implementation.
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/>.
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
)
36 ret
.dptr
= discard_const_p(uint8_t, dptr
);
41 bool tdb_data_equal(TDB_DATA t1
, TDB_DATA t2
)
43 if (t1
.dsize
!= t2
.dsize
) {
46 return (memcmp(t1
.dptr
, t2
.dptr
, t1
.dsize
) == 0);
49 bool tdb_data_is_empty(TDB_DATA d
)
51 return (d
.dsize
== 0) || (d
.dptr
== NULL
);
54 TDB_DATA
string_tdb_data(const char *string
)
56 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) : 0 );
59 TDB_DATA
string_term_tdb_data(const char *string
)
61 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) + 1 : 0);
64 /****************************************************************************
65 Lock a chain by string. Return non-zero if lock failed.
66 ****************************************************************************/
68 int tdb_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
70 TDB_DATA key
= string_term_tdb_data(keyval
);
72 return tdb_chainlock(tdb
, key
);
75 /****************************************************************************
76 Unlock a chain by string.
77 ****************************************************************************/
79 void tdb_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
81 TDB_DATA key
= string_term_tdb_data(keyval
);
83 tdb_chainunlock(tdb
, key
);
86 /****************************************************************************
87 Read lock a chain by string. Return non-zero if lock failed.
88 ****************************************************************************/
90 int tdb_read_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
92 TDB_DATA key
= string_term_tdb_data(keyval
);
94 return tdb_chainlock_read(tdb
, key
);
97 /****************************************************************************
98 Read unlock a chain by string.
99 ****************************************************************************/
101 void tdb_read_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
103 TDB_DATA key
= string_term_tdb_data(keyval
);
105 tdb_chainunlock_read(tdb
, key
);
109 /****************************************************************************
110 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
111 Output is int32_t in native byte order.
112 ****************************************************************************/
114 int32_t tdb_fetch_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
)
119 data
= tdb_fetch_compat(tdb
, key
);
120 if (!data
.dptr
|| data
.dsize
!= sizeof(int32_t)) {
121 SAFE_FREE(data
.dptr
);
125 ret
= IVAL(data
.dptr
,0);
126 SAFE_FREE(data
.dptr
);
130 /****************************************************************************
131 Fetch a int32_t value by string key, return -1 if not found.
132 Output is int32_t in native byte order.
133 ****************************************************************************/
135 int32_t tdb_fetch_int32(struct tdb_context
*tdb
, const char *keystr
)
137 return tdb_fetch_int32_byblob(tdb
, string_term_tdb_data(keystr
));
140 /****************************************************************************
141 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
142 Input is int32_t in native byte order. Output in tdb is in little-endian.
143 ****************************************************************************/
145 int tdb_store_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, int32_t v
)
151 data
.dptr
= (unsigned char *)&v_store
;
152 data
.dsize
= sizeof(int32_t);
154 return tdb_store(tdb
, key
, data
, TDB_REPLACE
);
157 /****************************************************************************
158 Store a int32_t value by string key, return 0 on success, -ve on failure.
159 Input is int32_t in native byte order. Output in tdb is in little-endian.
160 ****************************************************************************/
162 int tdb_store_int32(struct tdb_context
*tdb
, const char *keystr
, int32_t v
)
164 return tdb_store_int32_byblob(tdb
, string_term_tdb_data(keystr
), v
);
167 /****************************************************************************
168 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
169 Output is uint32_t in native byte order.
170 ****************************************************************************/
172 bool tdb_fetch_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t *value
)
176 data
= tdb_fetch_compat(tdb
, key
);
177 if (!data
.dptr
|| data
.dsize
!= sizeof(uint32_t)) {
178 SAFE_FREE(data
.dptr
);
182 *value
= IVAL(data
.dptr
,0);
183 SAFE_FREE(data
.dptr
);
187 /****************************************************************************
188 Fetch a uint32_t value by string key, return false if not found.
189 Output is uint32_t in native byte order.
190 ****************************************************************************/
192 bool tdb_fetch_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t *value
)
194 return tdb_fetch_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
197 /****************************************************************************
198 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
199 Input is uint32_t in native byte order. Output in tdb is in little-endian.
200 ****************************************************************************/
202 bool tdb_store_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t value
)
208 SIVAL(&v_store
, 0, value
);
209 data
.dptr
= (unsigned char *)&v_store
;
210 data
.dsize
= sizeof(uint32_t);
212 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0)
218 /****************************************************************************
219 Store a uint32_t value by string key, return true on success, false on failure.
220 Input is uint32_t in native byte order. Output in tdb is in little-endian.
221 ****************************************************************************/
223 bool tdb_store_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t value
)
225 return tdb_store_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
227 /****************************************************************************
228 Store a buffer by a null terminated string key. Return 0 on success, -ve
230 ****************************************************************************/
232 int tdb_store_bystring(struct tdb_context
*tdb
, const char *keystr
, TDB_DATA data
, int flags
)
234 TDB_DATA key
= string_term_tdb_data(keystr
);
236 return tdb_store(tdb
, key
, data
, flags
);
239 /****************************************************************************
240 Fetch a buffer using a null terminated string key. Don't forget to call
241 free() on the result dptr.
242 ****************************************************************************/
244 TDB_DATA
tdb_fetch_bystring(struct tdb_context
*tdb
, const char *keystr
)
246 TDB_DATA key
= string_term_tdb_data(keystr
);
248 return tdb_fetch_compat(tdb
, key
);
251 /****************************************************************************
252 Delete an entry using a null terminated string key.
253 ****************************************************************************/
255 int tdb_delete_bystring(struct tdb_context
*tdb
, const char *keystr
)
257 TDB_DATA key
= string_term_tdb_data(keystr
);
259 return tdb_delete(tdb
, key
);
262 /****************************************************************************
263 Atomic integer change. Returns old value. To create, set initial value in *oldval.
264 ****************************************************************************/
266 int32_t tdb_change_int32_atomic(struct tdb_context
*tdb
, const char *keystr
, int32_t *oldval
, int32_t change_val
)
271 if (tdb_lock_bystring(tdb
, keystr
) != 0)
274 if ((val
= tdb_fetch_int32(tdb
, keystr
)) == -1) {
275 /* The lookup failed */
276 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
277 /* but not because it didn't exist */
281 /* Start with 'old' value */
285 /* It worked, set return value (oldval) to tdb data */
289 /* Increment value for storage and return next time */
292 if (tdb_store_int32(tdb
, keystr
, val
) != 0)
299 tdb_unlock_bystring(tdb
, keystr
);
303 /****************************************************************************
304 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
305 ****************************************************************************/
307 bool tdb_change_uint32_atomic(struct tdb_context
*tdb
, const char *keystr
, uint32_t *oldval
, uint32_t change_val
)
312 if (tdb_lock_bystring(tdb
, keystr
) != 0)
315 if (!tdb_fetch_uint32(tdb
, keystr
, &val
)) {
317 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
318 /* and not because it didn't exist */
322 /* Start with 'old' value */
326 /* it worked, set return value (oldval) to tdb data */
331 /* get a new value to store */
334 if (!tdb_store_uint32(tdb
, keystr
, val
))
341 tdb_unlock_bystring(tdb
, keystr
);
345 /****************************************************************************
346 Allow tdb_delete to be used as a tdb_traversal_fn.
347 ****************************************************************************/
349 int tdb_traverse_delete_fn(struct tdb_context
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
352 return tdb_delete(the_tdb
, key
);