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 "../tdb/include/tdb.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 TDB_DATA
string_tdb_data(const char *string
)
43 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) : 0 );
46 TDB_DATA
string_term_tdb_data(const char *string
)
48 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) + 1 : 0);
51 /****************************************************************************
52 Lock a chain by string. Return -1 if lock failed.
53 ****************************************************************************/
55 int tdb_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
57 TDB_DATA key
= string_term_tdb_data(keyval
);
59 return tdb_chainlock(tdb
, key
);
62 /****************************************************************************
63 Unlock a chain by string.
64 ****************************************************************************/
66 void tdb_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
68 TDB_DATA key
= string_term_tdb_data(keyval
);
70 tdb_chainunlock(tdb
, key
);
73 /****************************************************************************
74 Read lock a chain by string. Return -1 if lock failed.
75 ****************************************************************************/
77 int tdb_read_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
79 TDB_DATA key
= string_term_tdb_data(keyval
);
81 return tdb_chainlock_read(tdb
, key
);
84 /****************************************************************************
85 Read unlock a chain by string.
86 ****************************************************************************/
88 void tdb_read_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
90 TDB_DATA key
= string_term_tdb_data(keyval
);
92 tdb_chainunlock_read(tdb
, key
);
96 /****************************************************************************
97 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
98 Output is int32_t in native byte order.
99 ****************************************************************************/
101 int32_t tdb_fetch_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
)
106 data
= tdb_fetch(tdb
, key
);
107 if (!data
.dptr
|| data
.dsize
!= sizeof(int32_t)) {
108 SAFE_FREE(data
.dptr
);
112 ret
= IVAL(data
.dptr
,0);
113 SAFE_FREE(data
.dptr
);
117 /****************************************************************************
118 Fetch a int32_t value by string key, return -1 if not found.
119 Output is int32_t in native byte order.
120 ****************************************************************************/
122 int32_t tdb_fetch_int32(struct tdb_context
*tdb
, const char *keystr
)
124 return tdb_fetch_int32_byblob(tdb
, string_term_tdb_data(keystr
));
127 /****************************************************************************
128 Store a int32_t value by an arbitary blob key, return 0 on success, -1 on failure.
129 Input is int32_t in native byte order. Output in tdb is in little-endian.
130 ****************************************************************************/
132 int tdb_store_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, int32_t v
)
138 data
.dptr
= (unsigned char *)&v_store
;
139 data
.dsize
= sizeof(int32_t);
141 return tdb_store(tdb
, key
, data
, TDB_REPLACE
);
144 /****************************************************************************
145 Store a int32_t value by string key, return 0 on success, -1 on failure.
146 Input is int32_t in native byte order. Output in tdb is in little-endian.
147 ****************************************************************************/
149 int tdb_store_int32(struct tdb_context
*tdb
, const char *keystr
, int32_t v
)
151 return tdb_store_int32_byblob(tdb
, string_term_tdb_data(keystr
), v
);
154 /****************************************************************************
155 Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
156 Output is uint32_t in native byte order.
157 ****************************************************************************/
159 bool tdb_fetch_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t *value
)
163 data
= tdb_fetch(tdb
, key
);
164 if (!data
.dptr
|| data
.dsize
!= sizeof(uint32_t)) {
165 SAFE_FREE(data
.dptr
);
169 *value
= IVAL(data
.dptr
,0);
170 SAFE_FREE(data
.dptr
);
174 /****************************************************************************
175 Fetch a uint32_t value by string key, return -1 if not found.
176 Output is uint32_t in native byte order.
177 ****************************************************************************/
179 bool tdb_fetch_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t *value
)
181 return tdb_fetch_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
184 /****************************************************************************
185 Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
186 Input is uint32_t in native byte order. Output in tdb is in little-endian.
187 ****************************************************************************/
189 bool tdb_store_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t value
)
195 SIVAL(&v_store
, 0, value
);
196 data
.dptr
= (unsigned char *)&v_store
;
197 data
.dsize
= sizeof(uint32_t);
199 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) == -1)
205 /****************************************************************************
206 Store a uint32_t value by string key, return 0 on success, -1 on failure.
207 Input is uint32_t in native byte order. Output in tdb is in little-endian.
208 ****************************************************************************/
210 bool tdb_store_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t value
)
212 return tdb_store_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
214 /****************************************************************************
215 Store a buffer by a null terminated string key. Return 0 on success, -1
217 ****************************************************************************/
219 int tdb_store_bystring(struct tdb_context
*tdb
, const char *keystr
, TDB_DATA data
, int flags
)
221 TDB_DATA key
= string_term_tdb_data(keystr
);
223 return tdb_store(tdb
, key
, data
, flags
);
226 /****************************************************************************
227 Fetch a buffer using a null terminated string key. Don't forget to call
228 free() on the result dptr.
229 ****************************************************************************/
231 TDB_DATA
tdb_fetch_bystring(struct tdb_context
*tdb
, const char *keystr
)
233 TDB_DATA key
= string_term_tdb_data(keystr
);
235 return tdb_fetch(tdb
, key
);
238 /****************************************************************************
239 Delete an entry using a null terminated string key.
240 ****************************************************************************/
242 int tdb_delete_bystring(struct tdb_context
*tdb
, const char *keystr
)
244 TDB_DATA key
= string_term_tdb_data(keystr
);
246 return tdb_delete(tdb
, key
);
249 /****************************************************************************
250 Atomic integer change. Returns old value. To create, set initial value in *oldval.
251 ****************************************************************************/
253 int32_t tdb_change_int32_atomic(struct tdb_context
*tdb
, const char *keystr
, int32_t *oldval
, int32_t change_val
)
258 if (tdb_lock_bystring(tdb
, keystr
) == -1)
261 if ((val
= tdb_fetch_int32(tdb
, keystr
)) == -1) {
262 /* The lookup failed */
263 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
264 /* but not because it didn't exist */
268 /* Start with 'old' value */
272 /* It worked, set return value (oldval) to tdb data */
276 /* Increment value for storage and return next time */
279 if (tdb_store_int32(tdb
, keystr
, val
) == -1)
286 tdb_unlock_bystring(tdb
, keystr
);
290 /****************************************************************************
291 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
292 ****************************************************************************/
294 bool tdb_change_uint32_atomic(struct tdb_context
*tdb
, const char *keystr
, uint32_t *oldval
, uint32_t change_val
)
299 if (tdb_lock_bystring(tdb
, keystr
) == -1)
302 if (!tdb_fetch_uint32(tdb
, keystr
, &val
)) {
304 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
305 /* and not because it didn't exist */
309 /* Start with 'old' value */
313 /* it worked, set return value (oldval) to tdb data */
318 /* get a new value to store */
321 if (!tdb_store_uint32(tdb
, keystr
, val
))
328 tdb_unlock_bystring(tdb
, keystr
);
332 /****************************************************************************
333 Allow tdb_delete to be used as a tdb_traversal_fn.
334 ****************************************************************************/
336 int tdb_traverse_delete_fn(struct tdb_context
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
339 return tdb_delete(the_tdb
, key
);