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 struct dbwrap_fetch_int32_state
{
34 static void dbwrap_fetch_int32_parser(TDB_DATA key
, TDB_DATA data
,
37 struct dbwrap_fetch_int32_state
*state
=
38 (struct dbwrap_fetch_int32_state
*)private_data
;
40 if (data
.dsize
!= sizeof(state
->result
)) {
41 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
44 state
->result
= IVAL(data
.dptr
, 0);
45 state
->status
= NT_STATUS_OK
;
48 NTSTATUS
dbwrap_fetch_int32(struct db_context
*db
, TDB_DATA key
,
51 struct dbwrap_fetch_int32_state state
;
54 return NT_STATUS_INVALID_PARAMETER
;
57 state
.status
= NT_STATUS_INTERNAL_ERROR
;
59 dbwrap_parse_record(db
, key
, dbwrap_fetch_int32_parser
, &state
);
61 if (NT_STATUS_IS_OK(state
.status
)) {
62 *result
= state
.result
;
67 NTSTATUS
dbwrap_fetch_int32_bystring(struct db_context
*db
, const char *keystr
,
70 return dbwrap_fetch_int32(db
, string_term_tdb_data(keystr
), result
);
73 NTSTATUS
dbwrap_store_int32_bystring(struct db_context
*db
, const char *keystr
,
76 struct db_record
*rec
;
80 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
81 string_term_tdb_data(keystr
));
83 return NT_STATUS_UNSUCCESSFUL
;
86 SIVAL(&v_store
, 0, v
);
88 status
= dbwrap_record_store(rec
,
89 make_tdb_data((const uint8_t *)&v_store
,
96 NTSTATUS
dbwrap_fetch_uint32_bystring(struct db_context
*db
,
97 const char *keystr
, uint32_t *val
)
103 return NT_STATUS_INVALID_PARAMETER
;
106 status
= dbwrap_fetch_bystring(db
, talloc_tos(), keystr
, &dbuf
);
107 if (!NT_STATUS_IS_OK(status
)) {
111 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(uint32_t))) {
112 TALLOC_FREE(dbuf
.dptr
);
113 return NT_STATUS_NOT_FOUND
;
116 *val
= IVAL(dbuf
.dptr
, 0);
117 TALLOC_FREE(dbuf
.dptr
);
121 NTSTATUS
dbwrap_store_uint32_bystring(struct db_context
*db
,
122 const char *keystr
, uint32_t v
)
124 struct db_record
*rec
;
128 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
129 string_term_tdb_data(keystr
));
131 return NT_STATUS_INVALID_PARAMETER
;
134 SIVAL(&v_store
, 0, v
);
136 status
= dbwrap_record_store(rec
,
137 make_tdb_data((const uint8_t *)&v_store
,
145 * Atomic unsigned integer change (addition):
147 * if value does not exist yet in the db, use *oldval as initial old value.
148 * return old value in *oldval.
149 * store *oldval + change_val to db.
152 struct dbwrap_change_uint32_atomic_context
{
158 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
161 struct db_record
*rec
;
162 uint32_t val
= (uint32_t)-1;
165 struct dbwrap_change_uint32_atomic_context
*state
;
168 state
= (struct dbwrap_change_uint32_atomic_context
*)private_data
;
170 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
171 string_term_tdb_data(state
->keystr
));
173 return NT_STATUS_UNSUCCESSFUL
;
176 value
= dbwrap_record_get_value(rec
);
178 if (value
.dptr
== NULL
) {
179 val
= *(state
->oldval
);
180 } else if (value
.dsize
== sizeof(val
)) {
181 val
= IVAL(value
.dptr
, 0);
182 *(state
->oldval
) = val
;
184 ret
= NT_STATUS_UNSUCCESSFUL
;
188 val
+= state
->change_val
;
190 SIVAL(&v_store
, 0, val
);
192 ret
= dbwrap_record_store(rec
,
193 make_tdb_data((const uint8_t *)&v_store
,
202 NTSTATUS
dbwrap_change_uint32_atomic_bystring(struct db_context
*db
,
208 struct dbwrap_change_uint32_atomic_context state
;
210 state
.keystr
= keystr
;
211 state
.oldval
= oldval
;
212 state
.change_val
= change_val
;
214 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
219 NTSTATUS
dbwrap_trans_change_uint32_atomic_bystring(struct db_context
*db
,
225 struct dbwrap_change_uint32_atomic_context state
;
227 state
.keystr
= keystr
;
228 state
.oldval
= oldval
;
229 state
.change_val
= change_val
;
231 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
237 * Atomic integer change (addition):
239 * if value does not exist yet in the db, use *oldval as initial old value.
240 * return old value in *oldval.
241 * store *oldval + change_val to db.
244 struct dbwrap_change_int32_atomic_context
{
250 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
253 struct db_record
*rec
;
257 struct dbwrap_change_int32_atomic_context
*state
;
260 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
262 rec
= dbwrap_fetch_locked(db
, talloc_tos(), state
->key
);
264 return NT_STATUS_UNSUCCESSFUL
;
267 value
= dbwrap_record_get_value(rec
);
269 if (value
.dptr
== NULL
) {
270 val
= *(state
->oldval
);
271 } else if (value
.dsize
== sizeof(val
)) {
272 val
= IVAL(value
.dptr
, 0);
273 *(state
->oldval
) = val
;
275 ret
= NT_STATUS_UNSUCCESSFUL
;
279 val
+= state
->change_val
;
281 SIVAL(&v_store
, 0, val
);
283 ret
= dbwrap_record_store(rec
,
284 make_tdb_data((const uint8_t *)&v_store
,
293 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
,
299 struct dbwrap_change_int32_atomic_context state
;
302 state
.oldval
= oldval
;
303 state
.change_val
= change_val
;
305 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
310 NTSTATUS
dbwrap_change_int32_atomic_bystring(struct db_context
*db
,
315 return dbwrap_change_int32_atomic(db
, string_term_tdb_data(keystr
),
319 NTSTATUS
dbwrap_trans_change_int32_atomic_bystring(struct db_context
*db
,
325 struct dbwrap_change_int32_atomic_context state
;
327 state
.key
= string_term_tdb_data(keystr
);
328 state
.oldval
= oldval
;
329 state
.change_val
= change_val
;
331 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
336 struct dbwrap_store_context
{
342 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
344 struct db_record
*rec
= NULL
;
346 struct dbwrap_store_context
*store_ctx
;
348 store_ctx
= (struct dbwrap_store_context
*)private_data
;
350 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
352 DEBUG(5, ("fetch_locked failed\n"));
353 return NT_STATUS_NO_MEMORY
;
356 status
= dbwrap_record_store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
357 if (!NT_STATUS_IS_OK(status
)) {
358 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
365 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
369 struct dbwrap_store_context store_ctx
;
371 store_ctx
.key
= &key
;
372 store_ctx
.dbuf
= &dbuf
;
373 store_ctx
.flag
= flag
;
375 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
380 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
383 struct db_record
*rec
;
384 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
386 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *key
);
388 DEBUG(5, ("fetch_locked failed\n"));
389 return NT_STATUS_NO_MEMORY
;
392 status
= dbwrap_record_delete(rec
);
393 if (!NT_STATUS_IS_OK(status
)) {
394 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
401 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
405 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
410 NTSTATUS
dbwrap_trans_store_int32_bystring(struct db_context
*db
,
416 SIVAL(&v_store
, 0, v
);
418 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
419 make_tdb_data((const uint8_t *)&v_store
,
424 NTSTATUS
dbwrap_trans_store_uint32_bystring(struct db_context
*db
,
430 SIVAL(&v_store
, 0, v
);
432 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
433 make_tdb_data((const uint8_t *)&v_store
,
438 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
439 TDB_DATA data
, int flags
)
441 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
444 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
446 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
450 * Wrap db action(s) into a transaction.
452 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
453 NTSTATUS (*action
)(struct db_context
*, void *),
459 res
= dbwrap_transaction_start(db
);
461 DEBUG(5, ("transaction_start failed\n"));
462 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
465 status
= action(db
, private_data
);
466 if (!NT_STATUS_IS_OK(status
)) {
467 if (dbwrap_transaction_cancel(db
) != 0) {
468 smb_panic("Cancelling transaction failed");
473 res
= dbwrap_transaction_commit(db
);
478 DEBUG(2, ("transaction_commit failed\n"));
479 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
482 struct dbwrap_trans_traverse_action_ctx
{
483 int (*f
)(struct db_record
* rec
, void* private_data
);
488 static NTSTATUS
dbwrap_trans_traverse_action(struct db_context
* db
, void* private_data
)
490 struct dbwrap_trans_traverse_action_ctx
* ctx
=
491 (struct dbwrap_trans_traverse_action_ctx
*)private_data
;
493 NTSTATUS status
= dbwrap_traverse(db
, ctx
->f
, ctx
->private_data
, NULL
);
498 NTSTATUS
dbwrap_trans_traverse(struct db_context
*db
,
499 int (*f
)(struct db_record
*, void*),
502 struct dbwrap_trans_traverse_action_ctx ctx
= {
504 .private_data
= private_data
,
506 return dbwrap_trans_do(db
, dbwrap_trans_traverse_action
, &ctx
);
509 NTSTATUS
dbwrap_delete_bystring(struct db_context
*db
, const char *key
)
511 return dbwrap_delete(db
, string_term_tdb_data(key
));
514 NTSTATUS
dbwrap_store_bystring(struct db_context
*db
, const char *key
,
515 TDB_DATA data
, int flags
)
517 return dbwrap_store(db
, string_term_tdb_data(key
), data
, flags
);
520 NTSTATUS
dbwrap_fetch_bystring(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
521 const char *key
, TDB_DATA
*value
)
523 return dbwrap_fetch(db
, mem_ctx
, string_term_tdb_data(key
), value
);
528 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
533 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
534 if (key_upper
== NULL
) {
535 return NT_STATUS_NO_MEMORY
;
538 status
= dbwrap_delete_bystring(db
, key_upper
);
540 talloc_free(key_upper
);
544 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
545 TDB_DATA data
, int flags
)
550 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
551 if (key_upper
== NULL
) {
552 return NT_STATUS_NO_MEMORY
;
555 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
557 talloc_free(key_upper
);
561 NTSTATUS
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
562 const char *key
, TDB_DATA
*value
)
567 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
568 if (key_upper
== NULL
) {
569 return NT_STATUS_NO_MEMORY
;
572 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
, value
);
574 talloc_free(key_upper
);