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
;
55 return NT_STATUS_INVALID_PARAMETER
;
58 state
.status
= NT_STATUS_INTERNAL_ERROR
;
60 status
= dbwrap_parse_record(db
, key
, dbwrap_fetch_int32_parser
, &state
);
61 if (!NT_STATUS_IS_OK(status
)) {
65 if (NT_STATUS_IS_OK(state
.status
)) {
66 *result
= state
.result
;
71 NTSTATUS
dbwrap_fetch_int32_bystring(struct db_context
*db
, const char *keystr
,
74 return dbwrap_fetch_int32(db
, string_term_tdb_data(keystr
), result
);
77 NTSTATUS
dbwrap_store_int32_bystring(struct db_context
*db
, const char *keystr
,
80 struct db_record
*rec
;
84 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
85 string_term_tdb_data(keystr
));
87 return NT_STATUS_UNSUCCESSFUL
;
90 SIVAL(&v_store
, 0, v
);
92 status
= dbwrap_record_store(rec
,
93 make_tdb_data((const uint8_t *)&v_store
,
100 NTSTATUS
dbwrap_fetch_uint32_bystring(struct db_context
*db
,
101 const char *keystr
, uint32_t *val
)
107 return NT_STATUS_INVALID_PARAMETER
;
110 status
= dbwrap_fetch_bystring(db
, talloc_tos(), keystr
, &dbuf
);
111 if (!NT_STATUS_IS_OK(status
)) {
115 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(uint32_t))) {
116 TALLOC_FREE(dbuf
.dptr
);
117 return NT_STATUS_NOT_FOUND
;
120 *val
= IVAL(dbuf
.dptr
, 0);
121 TALLOC_FREE(dbuf
.dptr
);
125 NTSTATUS
dbwrap_store_uint32_bystring(struct db_context
*db
,
126 const char *keystr
, uint32_t v
)
128 struct db_record
*rec
;
132 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
133 string_term_tdb_data(keystr
));
135 return NT_STATUS_INVALID_PARAMETER
;
138 SIVAL(&v_store
, 0, v
);
140 status
= dbwrap_record_store(rec
,
141 make_tdb_data((const uint8_t *)&v_store
,
149 * Atomic unsigned integer change (addition):
151 * if value does not exist yet in the db, use *oldval as initial old value.
152 * return old value in *oldval.
153 * store *oldval + change_val to db.
156 struct dbwrap_change_uint32_atomic_context
{
162 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
165 struct db_record
*rec
;
166 uint32_t val
= (uint32_t)-1;
169 struct dbwrap_change_uint32_atomic_context
*state
;
172 state
= (struct dbwrap_change_uint32_atomic_context
*)private_data
;
174 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
175 string_term_tdb_data(state
->keystr
));
177 return NT_STATUS_UNSUCCESSFUL
;
180 value
= dbwrap_record_get_value(rec
);
182 if (value
.dptr
== NULL
) {
183 val
= *(state
->oldval
);
184 } else if (value
.dsize
== sizeof(val
)) {
185 val
= IVAL(value
.dptr
, 0);
186 *(state
->oldval
) = val
;
188 ret
= NT_STATUS_UNSUCCESSFUL
;
192 val
+= state
->change_val
;
194 SIVAL(&v_store
, 0, val
);
196 ret
= dbwrap_record_store(rec
,
197 make_tdb_data((const uint8_t *)&v_store
,
206 NTSTATUS
dbwrap_change_uint32_atomic_bystring(struct db_context
*db
,
212 struct dbwrap_change_uint32_atomic_context state
;
214 state
.keystr
= keystr
;
215 state
.oldval
= oldval
;
216 state
.change_val
= change_val
;
218 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
223 NTSTATUS
dbwrap_trans_change_uint32_atomic_bystring(struct db_context
*db
,
229 struct dbwrap_change_uint32_atomic_context state
;
231 state
.keystr
= keystr
;
232 state
.oldval
= oldval
;
233 state
.change_val
= change_val
;
235 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
241 * Atomic integer change (addition):
243 * if value does not exist yet in the db, use *oldval as initial old value.
244 * return old value in *oldval.
245 * store *oldval + change_val to db.
248 struct dbwrap_change_int32_atomic_context
{
254 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
257 struct db_record
*rec
;
261 struct dbwrap_change_int32_atomic_context
*state
;
264 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
266 rec
= dbwrap_fetch_locked(db
, talloc_tos(), state
->key
);
268 return NT_STATUS_UNSUCCESSFUL
;
271 value
= dbwrap_record_get_value(rec
);
273 if (value
.dptr
== NULL
) {
274 val
= *(state
->oldval
);
275 } else if (value
.dsize
== sizeof(val
)) {
276 val
= IVAL(value
.dptr
, 0);
277 *(state
->oldval
) = val
;
279 ret
= NT_STATUS_UNSUCCESSFUL
;
283 val
+= state
->change_val
;
285 SIVAL(&v_store
, 0, val
);
287 ret
= dbwrap_record_store(rec
,
288 make_tdb_data((const uint8_t *)&v_store
,
297 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
,
303 struct dbwrap_change_int32_atomic_context state
;
306 state
.oldval
= oldval
;
307 state
.change_val
= change_val
;
309 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
314 NTSTATUS
dbwrap_change_int32_atomic_bystring(struct db_context
*db
,
319 return dbwrap_change_int32_atomic(db
, string_term_tdb_data(keystr
),
323 NTSTATUS
dbwrap_trans_change_int32_atomic_bystring(struct db_context
*db
,
329 struct dbwrap_change_int32_atomic_context state
;
331 state
.key
= string_term_tdb_data(keystr
);
332 state
.oldval
= oldval
;
333 state
.change_val
= change_val
;
335 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
340 struct dbwrap_store_context
{
346 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
348 struct db_record
*rec
= NULL
;
350 struct dbwrap_store_context
*store_ctx
;
352 store_ctx
= (struct dbwrap_store_context
*)private_data
;
354 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
356 DEBUG(5, ("fetch_locked failed\n"));
357 return NT_STATUS_NO_MEMORY
;
360 status
= dbwrap_record_store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
361 if (!NT_STATUS_IS_OK(status
)) {
362 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
369 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
373 struct dbwrap_store_context store_ctx
;
375 store_ctx
.key
= &key
;
376 store_ctx
.dbuf
= &dbuf
;
377 store_ctx
.flag
= flag
;
379 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
384 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
387 struct db_record
*rec
;
388 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
390 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *key
);
392 DEBUG(5, ("fetch_locked failed\n"));
393 return NT_STATUS_NO_MEMORY
;
396 status
= dbwrap_record_delete(rec
);
397 if (!NT_STATUS_IS_OK(status
)) {
398 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
405 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
409 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
414 NTSTATUS
dbwrap_trans_store_int32_bystring(struct db_context
*db
,
420 SIVAL(&v_store
, 0, v
);
422 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
423 make_tdb_data((const uint8_t *)&v_store
,
428 NTSTATUS
dbwrap_trans_store_uint32_bystring(struct db_context
*db
,
434 SIVAL(&v_store
, 0, v
);
436 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
437 make_tdb_data((const uint8_t *)&v_store
,
442 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
443 TDB_DATA data
, int flags
)
445 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
448 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
450 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
454 * Wrap db action(s) into a transaction.
456 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
457 NTSTATUS (*action
)(struct db_context
*, void *),
463 res
= dbwrap_transaction_start(db
);
465 DEBUG(5, ("transaction_start failed\n"));
466 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
469 status
= action(db
, private_data
);
470 if (!NT_STATUS_IS_OK(status
)) {
471 if (dbwrap_transaction_cancel(db
) != 0) {
472 smb_panic("Cancelling transaction failed");
477 res
= dbwrap_transaction_commit(db
);
482 DEBUG(2, ("transaction_commit failed\n"));
483 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
486 struct dbwrap_trans_traverse_action_ctx
{
487 int (*f
)(struct db_record
* rec
, void* private_data
);
492 static NTSTATUS
dbwrap_trans_traverse_action(struct db_context
* db
, void* private_data
)
494 struct dbwrap_trans_traverse_action_ctx
* ctx
=
495 (struct dbwrap_trans_traverse_action_ctx
*)private_data
;
497 NTSTATUS status
= dbwrap_traverse(db
, ctx
->f
, ctx
->private_data
, NULL
);
502 NTSTATUS
dbwrap_trans_traverse(struct db_context
*db
,
503 int (*f
)(struct db_record
*, void*),
506 struct dbwrap_trans_traverse_action_ctx ctx
= {
508 .private_data
= private_data
,
510 return dbwrap_trans_do(db
, dbwrap_trans_traverse_action
, &ctx
);
513 NTSTATUS
dbwrap_delete_bystring(struct db_context
*db
, const char *key
)
515 return dbwrap_delete(db
, string_term_tdb_data(key
));
518 NTSTATUS
dbwrap_store_bystring(struct db_context
*db
, const char *key
,
519 TDB_DATA data
, int flags
)
521 return dbwrap_store(db
, string_term_tdb_data(key
), data
, flags
);
524 NTSTATUS
dbwrap_fetch_bystring(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
525 const char *key
, TDB_DATA
*value
)
527 return dbwrap_fetch(db
, mem_ctx
, string_term_tdb_data(key
), value
);
532 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
537 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
538 if (key_upper
== NULL
) {
539 return NT_STATUS_NO_MEMORY
;
542 status
= dbwrap_delete_bystring(db
, key_upper
);
544 talloc_free(key_upper
);
548 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
549 TDB_DATA data
, int flags
)
554 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
555 if (key_upper
== NULL
) {
556 return NT_STATUS_NO_MEMORY
;
559 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
561 talloc_free(key_upper
);
565 NTSTATUS
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
566 const char *key
, TDB_DATA
*value
)
571 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
572 if (key_upper
== NULL
) {
573 return NT_STATUS_NO_MEMORY
;
576 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
, value
);
578 talloc_free(key_upper
);