2 Unix SMB/CIFS implementation.
3 Utility functions for the dbwrap API
4 Copyright (C) Volker Lendecke 2007
5 Copyright (C) Michael Adam 2009
6 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
8 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "lib/util/util_tdb.h"
29 NTSTATUS
dbwrap_fetch_int32_bystring(struct db_context
*db
, const char *keystr
,
36 return NT_STATUS_INVALID_PARAMETER
;
39 status
= dbwrap_fetch_bystring(db
, talloc_tos(), keystr
, &dbuf
);
40 if (!NT_STATUS_IS_OK(status
)) {
44 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(int32_t))) {
45 TALLOC_FREE(dbuf
.dptr
);
46 return NT_STATUS_NOT_FOUND
;
49 *result
= IVAL(dbuf
.dptr
, 0);
50 TALLOC_FREE(dbuf
.dptr
);
54 NTSTATUS
dbwrap_store_int32_bystring(struct db_context
*db
, const char *keystr
,
57 struct db_record
*rec
;
61 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
62 string_term_tdb_data(keystr
));
64 return NT_STATUS_UNSUCCESSFUL
;
67 SIVAL(&v_store
, 0, v
);
69 status
= dbwrap_record_store(rec
,
70 make_tdb_data((const uint8_t *)&v_store
,
77 NTSTATUS
dbwrap_fetch_uint32_bystring(struct db_context
*db
,
78 const char *keystr
, uint32_t *val
)
84 return NT_STATUS_INVALID_PARAMETER
;
87 status
= dbwrap_fetch_bystring(db
, talloc_tos(), keystr
, &dbuf
);
88 if (!NT_STATUS_IS_OK(status
)) {
92 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(uint32_t))) {
93 TALLOC_FREE(dbuf
.dptr
);
94 return NT_STATUS_NOT_FOUND
;
97 *val
= IVAL(dbuf
.dptr
, 0);
98 TALLOC_FREE(dbuf
.dptr
);
102 NTSTATUS
dbwrap_store_uint32_bystring(struct db_context
*db
,
103 const char *keystr
, uint32_t v
)
105 struct db_record
*rec
;
109 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
110 string_term_tdb_data(keystr
));
112 return NT_STATUS_INVALID_PARAMETER
;
115 SIVAL(&v_store
, 0, v
);
117 status
= dbwrap_record_store(rec
,
118 make_tdb_data((const uint8_t *)&v_store
,
126 * Atomic unsigned integer change (addition):
128 * if value does not exist yet in the db, use *oldval as initial old value.
129 * return old value in *oldval.
130 * store *oldval + change_val to db.
133 struct dbwrap_change_uint32_atomic_context
{
139 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
142 struct db_record
*rec
;
143 uint32_t val
= (uint32_t)-1;
146 struct dbwrap_change_uint32_atomic_context
*state
;
149 state
= (struct dbwrap_change_uint32_atomic_context
*)private_data
;
151 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
152 string_term_tdb_data(state
->keystr
));
154 return NT_STATUS_UNSUCCESSFUL
;
157 value
= dbwrap_record_get_value(rec
);
159 if (value
.dptr
== NULL
) {
160 val
= *(state
->oldval
);
161 } else if (value
.dsize
== sizeof(val
)) {
162 val
= IVAL(value
.dptr
, 0);
163 *(state
->oldval
) = val
;
165 ret
= NT_STATUS_UNSUCCESSFUL
;
169 val
+= state
->change_val
;
171 SIVAL(&v_store
, 0, val
);
173 ret
= dbwrap_record_store(rec
,
174 make_tdb_data((const uint8_t *)&v_store
,
183 NTSTATUS
dbwrap_change_uint32_atomic(struct db_context
*db
, const char *keystr
,
184 uint32_t *oldval
, uint32_t change_val
)
187 struct dbwrap_change_uint32_atomic_context state
;
189 state
.keystr
= keystr
;
190 state
.oldval
= oldval
;
191 state
.change_val
= change_val
;
193 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
198 NTSTATUS
dbwrap_trans_change_uint32_atomic(struct db_context
*db
,
204 struct dbwrap_change_uint32_atomic_context state
;
206 state
.keystr
= keystr
;
207 state
.oldval
= oldval
;
208 state
.change_val
= change_val
;
210 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
216 * Atomic integer change (addition):
218 * if value does not exist yet in the db, use *oldval as initial old value.
219 * return old value in *oldval.
220 * store *oldval + change_val to db.
223 struct dbwrap_change_int32_atomic_context
{
229 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
232 struct db_record
*rec
;
236 struct dbwrap_change_int32_atomic_context
*state
;
239 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
241 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
242 string_term_tdb_data(state
->keystr
));
244 return NT_STATUS_UNSUCCESSFUL
;
247 value
= dbwrap_record_get_value(rec
);
249 if (value
.dptr
== NULL
) {
250 val
= *(state
->oldval
);
251 } else if (value
.dsize
== sizeof(val
)) {
252 val
= IVAL(value
.dptr
, 0);
253 *(state
->oldval
) = val
;
255 ret
= NT_STATUS_UNSUCCESSFUL
;
259 val
+= state
->change_val
;
261 SIVAL(&v_store
, 0, val
);
263 ret
= dbwrap_record_store(rec
,
264 make_tdb_data((const uint8_t *)&v_store
,
273 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
, const char *keystr
,
274 int32_t *oldval
, int32_t change_val
)
277 struct dbwrap_change_int32_atomic_context state
;
279 state
.keystr
= keystr
;
280 state
.oldval
= oldval
;
281 state
.change_val
= change_val
;
283 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
288 NTSTATUS
dbwrap_trans_change_int32_atomic(struct db_context
*db
,
294 struct dbwrap_change_int32_atomic_context state
;
296 state
.keystr
= keystr
;
297 state
.oldval
= oldval
;
298 state
.change_val
= change_val
;
300 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
305 struct dbwrap_store_context
{
311 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
313 struct db_record
*rec
= NULL
;
315 struct dbwrap_store_context
*store_ctx
;
317 store_ctx
= (struct dbwrap_store_context
*)private_data
;
319 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
321 DEBUG(5, ("fetch_locked failed\n"));
322 return NT_STATUS_NO_MEMORY
;
325 status
= dbwrap_record_store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
326 if (!NT_STATUS_IS_OK(status
)) {
327 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
334 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
338 struct dbwrap_store_context store_ctx
;
340 store_ctx
.key
= &key
;
341 store_ctx
.dbuf
= &dbuf
;
342 store_ctx
.flag
= flag
;
344 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
349 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
352 struct db_record
*rec
;
353 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
355 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *key
);
357 DEBUG(5, ("fetch_locked failed\n"));
358 return NT_STATUS_NO_MEMORY
;
361 status
= dbwrap_record_delete(rec
);
362 if (!NT_STATUS_IS_OK(status
)) {
363 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
370 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
374 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
379 NTSTATUS
dbwrap_trans_store_int32(struct db_context
*db
, const char *keystr
,
384 SIVAL(&v_store
, 0, v
);
386 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
387 make_tdb_data((const uint8_t *)&v_store
,
392 NTSTATUS
dbwrap_trans_store_uint32(struct db_context
*db
, const char *keystr
,
397 SIVAL(&v_store
, 0, v
);
399 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
400 make_tdb_data((const uint8_t *)&v_store
,
405 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
406 TDB_DATA data
, int flags
)
408 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
411 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
413 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
417 * Wrap db action(s) into a transaction.
419 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
420 NTSTATUS (*action
)(struct db_context
*, void *),
426 res
= dbwrap_transaction_start(db
);
428 DEBUG(5, ("transaction_start failed\n"));
429 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
432 status
= action(db
, private_data
);
433 if (!NT_STATUS_IS_OK(status
)) {
434 if (dbwrap_transaction_cancel(db
) != 0) {
435 smb_panic("Cancelling transaction failed");
440 res
= dbwrap_transaction_commit(db
);
445 DEBUG(2, ("transaction_commit failed\n"));
446 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
449 struct dbwrap_trans_traverse_action_ctx
{
450 int (*f
)(struct db_record
* rec
, void* private_data
);
455 static NTSTATUS
dbwrap_trans_traverse_action(struct db_context
* db
, void* private_data
)
457 struct dbwrap_trans_traverse_action_ctx
* ctx
=
458 (struct dbwrap_trans_traverse_action_ctx
*)private_data
;
460 NTSTATUS status
= dbwrap_traverse(db
, ctx
->f
, ctx
->private_data
, NULL
);
465 NTSTATUS
dbwrap_trans_traverse(struct db_context
*db
,
466 int (*f
)(struct db_record
*, void*),
469 struct dbwrap_trans_traverse_action_ctx ctx
= {
471 .private_data
= private_data
,
473 return dbwrap_trans_do(db
, dbwrap_trans_traverse_action
, &ctx
);
476 NTSTATUS
dbwrap_delete_bystring(struct db_context
*db
, const char *key
)
478 return dbwrap_delete(db
, string_term_tdb_data(key
));
481 NTSTATUS
dbwrap_store_bystring(struct db_context
*db
, const char *key
,
482 TDB_DATA data
, int flags
)
484 return dbwrap_store(db
, string_term_tdb_data(key
), data
, flags
);
487 NTSTATUS
dbwrap_fetch_bystring(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
488 const char *key
, TDB_DATA
*value
)
490 return dbwrap_fetch(db
, mem_ctx
, string_term_tdb_data(key
), value
);
495 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
500 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
501 if (key_upper
== NULL
) {
502 return NT_STATUS_NO_MEMORY
;
505 status
= dbwrap_delete_bystring(db
, key_upper
);
507 talloc_free(key_upper
);
511 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
512 TDB_DATA data
, int flags
)
517 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
518 if (key_upper
== NULL
) {
519 return NT_STATUS_NO_MEMORY
;
522 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
524 talloc_free(key_upper
);
528 NTSTATUS
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
529 const char *key
, TDB_DATA
*value
)
534 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
535 if (key_upper
== NULL
) {
536 return NT_STATUS_NO_MEMORY
;
539 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
, value
);
541 talloc_free(key_upper
);