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_bystring(struct db_context
*db
,
189 struct dbwrap_change_uint32_atomic_context state
;
191 state
.keystr
= keystr
;
192 state
.oldval
= oldval
;
193 state
.change_val
= change_val
;
195 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
200 NTSTATUS
dbwrap_trans_change_uint32_atomic_bystring(struct db_context
*db
,
206 struct dbwrap_change_uint32_atomic_context state
;
208 state
.keystr
= keystr
;
209 state
.oldval
= oldval
;
210 state
.change_val
= change_val
;
212 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
218 * Atomic integer change (addition):
220 * if value does not exist yet in the db, use *oldval as initial old value.
221 * return old value in *oldval.
222 * store *oldval + change_val to db.
225 struct dbwrap_change_int32_atomic_context
{
231 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
234 struct db_record
*rec
;
238 struct dbwrap_change_int32_atomic_context
*state
;
241 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
243 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
244 string_term_tdb_data(state
->keystr
));
246 return NT_STATUS_UNSUCCESSFUL
;
249 value
= dbwrap_record_get_value(rec
);
251 if (value
.dptr
== NULL
) {
252 val
= *(state
->oldval
);
253 } else if (value
.dsize
== sizeof(val
)) {
254 val
= IVAL(value
.dptr
, 0);
255 *(state
->oldval
) = val
;
257 ret
= NT_STATUS_UNSUCCESSFUL
;
261 val
+= state
->change_val
;
263 SIVAL(&v_store
, 0, val
);
265 ret
= dbwrap_record_store(rec
,
266 make_tdb_data((const uint8_t *)&v_store
,
275 NTSTATUS
dbwrap_change_int32_atomic_bystring(struct db_context
*db
,
281 struct dbwrap_change_int32_atomic_context state
;
283 state
.keystr
= keystr
;
284 state
.oldval
= oldval
;
285 state
.change_val
= change_val
;
287 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
292 NTSTATUS
dbwrap_trans_change_int32_atomic_bystring(struct db_context
*db
,
298 struct dbwrap_change_int32_atomic_context state
;
300 state
.keystr
= keystr
;
301 state
.oldval
= oldval
;
302 state
.change_val
= change_val
;
304 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
309 struct dbwrap_store_context
{
315 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
317 struct db_record
*rec
= NULL
;
319 struct dbwrap_store_context
*store_ctx
;
321 store_ctx
= (struct dbwrap_store_context
*)private_data
;
323 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
325 DEBUG(5, ("fetch_locked failed\n"));
326 return NT_STATUS_NO_MEMORY
;
329 status
= dbwrap_record_store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
330 if (!NT_STATUS_IS_OK(status
)) {
331 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
338 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
342 struct dbwrap_store_context store_ctx
;
344 store_ctx
.key
= &key
;
345 store_ctx
.dbuf
= &dbuf
;
346 store_ctx
.flag
= flag
;
348 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
353 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
356 struct db_record
*rec
;
357 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
359 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *key
);
361 DEBUG(5, ("fetch_locked failed\n"));
362 return NT_STATUS_NO_MEMORY
;
365 status
= dbwrap_record_delete(rec
);
366 if (!NT_STATUS_IS_OK(status
)) {
367 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
374 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
378 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
383 NTSTATUS
dbwrap_trans_store_int32_bystring(struct db_context
*db
,
389 SIVAL(&v_store
, 0, v
);
391 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
392 make_tdb_data((const uint8_t *)&v_store
,
397 NTSTATUS
dbwrap_trans_store_uint32(struct db_context
*db
, const char *keystr
,
402 SIVAL(&v_store
, 0, v
);
404 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
405 make_tdb_data((const uint8_t *)&v_store
,
410 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
411 TDB_DATA data
, int flags
)
413 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
416 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
418 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
422 * Wrap db action(s) into a transaction.
424 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
425 NTSTATUS (*action
)(struct db_context
*, void *),
431 res
= dbwrap_transaction_start(db
);
433 DEBUG(5, ("transaction_start failed\n"));
434 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
437 status
= action(db
, private_data
);
438 if (!NT_STATUS_IS_OK(status
)) {
439 if (dbwrap_transaction_cancel(db
) != 0) {
440 smb_panic("Cancelling transaction failed");
445 res
= dbwrap_transaction_commit(db
);
450 DEBUG(2, ("transaction_commit failed\n"));
451 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
454 struct dbwrap_trans_traverse_action_ctx
{
455 int (*f
)(struct db_record
* rec
, void* private_data
);
460 static NTSTATUS
dbwrap_trans_traverse_action(struct db_context
* db
, void* private_data
)
462 struct dbwrap_trans_traverse_action_ctx
* ctx
=
463 (struct dbwrap_trans_traverse_action_ctx
*)private_data
;
465 NTSTATUS status
= dbwrap_traverse(db
, ctx
->f
, ctx
->private_data
, NULL
);
470 NTSTATUS
dbwrap_trans_traverse(struct db_context
*db
,
471 int (*f
)(struct db_record
*, void*),
474 struct dbwrap_trans_traverse_action_ctx ctx
= {
476 .private_data
= private_data
,
478 return dbwrap_trans_do(db
, dbwrap_trans_traverse_action
, &ctx
);
481 NTSTATUS
dbwrap_delete_bystring(struct db_context
*db
, const char *key
)
483 return dbwrap_delete(db
, string_term_tdb_data(key
));
486 NTSTATUS
dbwrap_store_bystring(struct db_context
*db
, const char *key
,
487 TDB_DATA data
, int flags
)
489 return dbwrap_store(db
, string_term_tdb_data(key
), data
, flags
);
492 NTSTATUS
dbwrap_fetch_bystring(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
493 const char *key
, TDB_DATA
*value
)
495 return dbwrap_fetch(db
, mem_ctx
, string_term_tdb_data(key
), value
);
500 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
505 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
506 if (key_upper
== NULL
) {
507 return NT_STATUS_NO_MEMORY
;
510 status
= dbwrap_delete_bystring(db
, key_upper
);
512 talloc_free(key_upper
);
516 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
517 TDB_DATA data
, int flags
)
522 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
523 if (key_upper
== NULL
) {
524 return NT_STATUS_NO_MEMORY
;
527 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
529 talloc_free(key_upper
);
533 NTSTATUS
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
534 const char *key
, TDB_DATA
*value
)
539 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
540 if (key_upper
== NULL
) {
541 return NT_STATUS_NO_MEMORY
;
544 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
, value
);
546 talloc_free(key_upper
);