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/>.
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 TDB_DATA
string_tdb_data(const char *string
)
51 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) : 0 );
54 TDB_DATA
string_term_tdb_data(const char *string
)
56 return make_tdb_data((const uint8_t *)string
, string
? strlen(string
) + 1 : 0);
59 /****************************************************************************
60 Lock a chain by string. Return -1 if lock failed.
61 ****************************************************************************/
63 int tdb_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
65 TDB_DATA key
= string_term_tdb_data(keyval
);
67 return tdb_chainlock(tdb
, key
);
70 /****************************************************************************
71 Unlock a chain by string.
72 ****************************************************************************/
74 void tdb_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
76 TDB_DATA key
= string_term_tdb_data(keyval
);
78 tdb_chainunlock(tdb
, key
);
81 /****************************************************************************
82 Read lock a chain by string. Return -1 if lock failed.
83 ****************************************************************************/
85 int tdb_read_lock_bystring(struct tdb_context
*tdb
, const char *keyval
)
87 TDB_DATA key
= string_term_tdb_data(keyval
);
89 return tdb_chainlock_read(tdb
, key
);
92 /****************************************************************************
93 Read unlock a chain by string.
94 ****************************************************************************/
96 void tdb_read_unlock_bystring(struct tdb_context
*tdb
, const char *keyval
)
98 TDB_DATA key
= string_term_tdb_data(keyval
);
100 tdb_chainunlock_read(tdb
, key
);
104 /****************************************************************************
105 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
106 Output is int32_t in native byte order.
107 ****************************************************************************/
109 int32_t tdb_fetch_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
)
114 data
= tdb_fetch(tdb
, key
);
115 if (!data
.dptr
|| data
.dsize
!= sizeof(int32_t)) {
116 SAFE_FREE(data
.dptr
);
120 ret
= IVAL(data
.dptr
,0);
121 SAFE_FREE(data
.dptr
);
125 /****************************************************************************
126 Fetch a int32_t value by string key, return -1 if not found.
127 Output is int32_t in native byte order.
128 ****************************************************************************/
130 int32_t tdb_fetch_int32(struct tdb_context
*tdb
, const char *keystr
)
132 return tdb_fetch_int32_byblob(tdb
, string_term_tdb_data(keystr
));
135 /****************************************************************************
136 Store a int32_t value by an arbitrary blob key, return 0 on success, -1 on failure.
137 Input is int32_t in native byte order. Output in tdb is in little-endian.
138 ****************************************************************************/
140 int tdb_store_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, int32_t v
)
146 data
.dptr
= (unsigned char *)&v_store
;
147 data
.dsize
= sizeof(int32_t);
149 return tdb_store(tdb
, key
, data
, TDB_REPLACE
);
152 /****************************************************************************
153 Store a int32_t value by string key, return 0 on success, -1 on failure.
154 Input is int32_t in native byte order. Output in tdb is in little-endian.
155 ****************************************************************************/
157 int tdb_store_int32(struct tdb_context
*tdb
, const char *keystr
, int32_t v
)
159 return tdb_store_int32_byblob(tdb
, string_term_tdb_data(keystr
), v
);
162 /****************************************************************************
163 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
164 Output is uint32_t in native byte order.
165 ****************************************************************************/
167 bool tdb_fetch_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t *value
)
171 data
= tdb_fetch(tdb
, key
);
172 if (!data
.dptr
|| data
.dsize
!= sizeof(uint32_t)) {
173 SAFE_FREE(data
.dptr
);
177 *value
= IVAL(data
.dptr
,0);
178 SAFE_FREE(data
.dptr
);
182 /****************************************************************************
183 Fetch a uint32_t value by string key, return false if not found.
184 Output is uint32_t in native byte order.
185 ****************************************************************************/
187 bool tdb_fetch_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t *value
)
189 return tdb_fetch_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
192 /****************************************************************************
193 Store a uint32_t value by an arbitrary blob key, return 0 on success, -1 on failure.
194 Input is uint32_t in native byte order. Output in tdb is in little-endian.
195 ****************************************************************************/
197 bool tdb_store_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
, uint32_t value
)
203 SIVAL(&v_store
, 0, value
);
204 data
.dptr
= (unsigned char *)&v_store
;
205 data
.dsize
= sizeof(uint32_t);
207 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) == -1)
213 /****************************************************************************
214 Store a uint32_t value by string key, return 0 on success, -1 on failure.
215 Input is uint32_t in native byte order. Output in tdb is in little-endian.
216 ****************************************************************************/
218 bool tdb_store_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t value
)
220 return tdb_store_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
222 /****************************************************************************
223 Store a buffer by a null terminated string key. Return 0 on success, -1
225 ****************************************************************************/
227 int tdb_store_bystring(struct tdb_context
*tdb
, const char *keystr
, TDB_DATA data
, int flags
)
229 TDB_DATA key
= string_term_tdb_data(keystr
);
231 return tdb_store(tdb
, key
, data
, flags
);
234 /****************************************************************************
235 Fetch a buffer using a null terminated string key. Don't forget to call
236 free() on the result dptr.
237 ****************************************************************************/
239 TDB_DATA
tdb_fetch_bystring(struct tdb_context
*tdb
, const char *keystr
)
241 TDB_DATA key
= string_term_tdb_data(keystr
);
243 return tdb_fetch(tdb
, key
);
246 /****************************************************************************
247 Delete an entry using a null terminated string key.
248 ****************************************************************************/
250 int tdb_delete_bystring(struct tdb_context
*tdb
, const char *keystr
)
252 TDB_DATA key
= string_term_tdb_data(keystr
);
254 return tdb_delete(tdb
, key
);
257 /****************************************************************************
258 Atomic integer change. Returns old value. To create, set initial value in *oldval.
259 ****************************************************************************/
261 int32_t tdb_change_int32_atomic(struct tdb_context
*tdb
, const char *keystr
, int32_t *oldval
, int32_t change_val
)
266 if (tdb_lock_bystring(tdb
, keystr
) == -1)
269 if ((val
= tdb_fetch_int32(tdb
, keystr
)) == -1) {
270 /* The lookup failed */
271 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
272 /* but not because it didn't exist */
276 /* Start with 'old' value */
280 /* It worked, set return value (oldval) to tdb data */
284 /* Increment value for storage and return next time */
287 if (tdb_store_int32(tdb
, keystr
, val
) == -1)
294 tdb_unlock_bystring(tdb
, keystr
);
298 /****************************************************************************
299 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
300 ****************************************************************************/
302 bool tdb_change_uint32_atomic(struct tdb_context
*tdb
, const char *keystr
, uint32_t *oldval
, uint32_t change_val
)
307 if (tdb_lock_bystring(tdb
, keystr
) == -1)
310 if (!tdb_fetch_uint32(tdb
, keystr
, &val
)) {
312 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
313 /* and not because it didn't exist */
317 /* Start with 'old' value */
321 /* it worked, set return value (oldval) to tdb data */
326 /* get a new value to store */
329 if (!tdb_store_uint32(tdb
, keystr
, val
))
336 tdb_unlock_bystring(tdb
, keystr
);
340 /****************************************************************************
341 Allow tdb_delete to be used as a tdb_traversal_fn.
342 ****************************************************************************/
344 int tdb_traverse_delete_fn(struct tdb_context
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
347 return tdb_delete(the_tdb
, key
);