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_bystring(struct db_context *db,
403 SIVAL(&v_store, 0, v);
405 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
406 make_tdb_data((const uint8_t *)&v_store,
411 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
412 TDB_DATA data, int flags)
414 return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
417 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
419 return dbwrap_trans_delete(db, string_term_tdb_data(key));
423 * Wrap db action(s) into a transaction.
425 NTSTATUS dbwrap_trans_do(struct db_context *db,
426 NTSTATUS (*action)(struct db_context *, void *),
432 res = dbwrap_transaction_start(db);
434 DEBUG(5, ("transaction_start failed\n"));
435 return NT_STATUS_INTERNAL_DB_CORRUPTION;
438 status = action(db, private_data);
439 if (!NT_STATUS_IS_OK(status)) {
440 if (dbwrap_transaction_cancel(db) != 0) {
441 smb_panic("Cancelling transaction failed");
446 res = dbwrap_transaction_commit(db);
451 DEBUG(2, ("transaction_commit failed\n"));
452 return NT_STATUS_INTERNAL_DB_CORRUPTION;
455 struct dbwrap_trans_traverse_action_ctx {
456 int (*f)(struct db_record* rec, void* private_data);
461 static NTSTATUS dbwrap_trans_traverse_action(struct db_context* db, void* private_data)
463 struct dbwrap_trans_traverse_action_ctx* ctx =
464 (struct dbwrap_trans_traverse_action_ctx*)private_data;
466 NTSTATUS status = dbwrap_traverse(db, ctx->f, ctx->private_data, NULL);
471 NTSTATUS dbwrap_trans_traverse(struct db_context *db,
472 int (*f)(struct db_record*, void*),
475 struct dbwrap_trans_traverse_action_ctx ctx = {
477 .private_data = private_data,
479 return dbwrap_trans_do(db, dbwrap_trans_traverse_action, &ctx);
482 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
484 return dbwrap_delete(db, string_term_tdb_data(key));
487 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
488 TDB_DATA data, int flags)
490 return dbwrap_store(db, string_term_tdb_data(key), data, flags);
493 NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
494 const char *key, TDB_DATA *value)
496 return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key), value);
501 NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key)
506 key_upper = talloc_strdup_upper(talloc_tos(), key);
507 if (key_upper == NULL) {
508 return NT_STATUS_NO_MEMORY;
511 status = dbwrap_delete_bystring(db, key_upper);
513 talloc_free(key_upper);
517 NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
518 TDB_DATA data, int flags)
523 key_upper = talloc_strdup_upper(talloc_tos(), key);
524 if (key_upper == NULL) {
525 return NT_STATUS_NO_MEMORY;
528 status = dbwrap_store_bystring(db, key_upper, data, flags);
530 talloc_free(key_upper);
534 NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
535 const char *key, TDB_DATA *value)
540 key_upper = talloc_strdup_upper(talloc_tos(), key);
541 if (key_upper == NULL) {
542 return NT_STATUS_NO_MEMORY;
545 status = dbwrap_fetch_bystring(db, mem_ctx, key_upper, value);
547 talloc_free(key_upper);