s3-build Create dbwrap_util subsystem
[Samba.git] / source3 / lib / dbwrap_util.c
blobce5ef5270672c7b3ae4e81cf70a5811078937330
1 /*
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.
25 #include "includes.h"
26 #include "dbwrap.h"
27 #include "util_tdb.h"
29 int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
31 TDB_DATA dbuf;
32 int32 ret;
34 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
35 return -1;
38 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
39 TALLOC_FREE(dbuf.dptr);
40 return -1;
43 ret = IVAL(dbuf.dptr, 0);
44 TALLOC_FREE(dbuf.dptr);
45 return ret;
48 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
50 struct db_record *rec;
51 int32 v_store;
52 NTSTATUS status;
54 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
55 if (rec == NULL) {
56 return -1;
59 SIVAL(&v_store, 0, v);
61 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
62 sizeof(v_store)),
63 TDB_REPLACE);
64 TALLOC_FREE(rec);
65 return NT_STATUS_IS_OK(status) ? 0 : -1;
68 bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
69 uint32_t *val)
71 TDB_DATA dbuf;
73 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
74 return false;
77 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) {
78 TALLOC_FREE(dbuf.dptr);
79 return false;
82 *val = IVAL(dbuf.dptr, 0);
83 TALLOC_FREE(dbuf.dptr);
84 return true;
87 int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v)
89 struct db_record *rec;
90 uint32 v_store;
91 NTSTATUS status;
93 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
94 if (rec == NULL) {
95 return -1;
98 SIVAL(&v_store, 0, v);
100 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
101 sizeof(v_store)),
102 TDB_REPLACE);
103 TALLOC_FREE(rec);
104 return NT_STATUS_IS_OK(status) ? 0 : -1;
108 * Atomic unsigned integer change (addition):
110 * if value does not exist yet in the db, use *oldval as initial old value.
111 * return old value in *oldval.
112 * store *oldval + change_val to db.
115 struct dbwrap_change_uint32_atomic_context {
116 const char *keystr;
117 uint32_t *oldval;
118 uint32_t change_val;
121 static NTSTATUS dbwrap_change_uint32_atomic_action(struct db_context *db,
122 void *private_data)
124 struct db_record *rec;
125 uint32_t val = (uint32_t)-1;
126 uint32_t v_store;
127 NTSTATUS ret;
128 struct dbwrap_change_uint32_atomic_context *state;
130 state = (struct dbwrap_change_uint32_atomic_context *)private_data;
132 rec = db->fetch_locked(db, NULL, string_term_tdb_data(state->keystr));
133 if (!rec) {
134 return NT_STATUS_UNSUCCESSFUL;
137 if (rec->value.dptr == NULL) {
138 val = *(state->oldval);
139 } else if (rec->value.dsize == sizeof(val)) {
140 val = IVAL(rec->value.dptr, 0);
141 *(state->oldval) = val;
142 } else {
143 ret = NT_STATUS_UNSUCCESSFUL;
144 goto done;
147 val += state->change_val;
149 SIVAL(&v_store, 0, val);
151 ret = rec->store(rec,
152 make_tdb_data((const uint8 *)&v_store,
153 sizeof(v_store)),
154 TDB_REPLACE);
156 done:
157 TALLOC_FREE(rec);
158 return ret;
161 NTSTATUS dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr,
162 uint32_t *oldval, uint32_t change_val)
164 NTSTATUS ret;
165 struct dbwrap_change_uint32_atomic_context state;
167 state.keystr = keystr;
168 state.oldval = oldval;
169 state.change_val = change_val;
171 ret = dbwrap_change_uint32_atomic_action(db, &state);
173 return ret;
176 NTSTATUS dbwrap_trans_change_uint32_atomic(struct db_context *db,
177 const char *keystr,
178 uint32_t *oldval,
179 uint32_t change_val)
181 NTSTATUS ret;
182 struct dbwrap_change_uint32_atomic_context state;
184 state.keystr = keystr;
185 state.oldval = oldval;
186 state.change_val = change_val;
188 ret = dbwrap_trans_do(db, dbwrap_change_uint32_atomic_action, &state);
190 return ret;
194 * Atomic integer change (addition):
196 * if value does not exist yet in the db, use *oldval as initial old value.
197 * return old value in *oldval.
198 * store *oldval + change_val to db.
201 struct dbwrap_change_int32_atomic_context {
202 const char *keystr;
203 int32_t *oldval;
204 int32_t change_val;
207 static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db,
208 void *private_data)
210 struct db_record *rec;
211 int32_t val = -1;
212 int32_t v_store;
213 NTSTATUS ret;
214 struct dbwrap_change_int32_atomic_context *state;
216 state = (struct dbwrap_change_int32_atomic_context *)private_data;
218 rec = db->fetch_locked(db, NULL, string_term_tdb_data(state->keystr));
219 if (!rec) {
220 return NT_STATUS_UNSUCCESSFUL;
223 if (rec->value.dptr == NULL) {
224 val = *(state->oldval);
225 } else if (rec->value.dsize == sizeof(val)) {
226 val = IVAL(rec->value.dptr, 0);
227 *(state->oldval) = val;
228 } else {
229 ret = NT_STATUS_UNSUCCESSFUL;
230 goto done;
233 val += state->change_val;
235 SIVAL(&v_store, 0, val);
237 ret = rec->store(rec,
238 make_tdb_data((const uint8_t *)&v_store,
239 sizeof(v_store)),
240 TDB_REPLACE);
242 done:
243 TALLOC_FREE(rec);
244 return ret;
247 NTSTATUS dbwrap_change_int32_atomic(struct db_context *db, const char *keystr,
248 int32_t *oldval, int32_t change_val)
250 NTSTATUS ret;
251 struct dbwrap_change_int32_atomic_context state;
253 state.keystr = keystr;
254 state.oldval = oldval;
255 state.change_val = change_val;
257 ret = dbwrap_change_int32_atomic_action(db, &state);
259 return ret;
262 NTSTATUS dbwrap_trans_change_int32_atomic(struct db_context *db,
263 const char *keystr,
264 int32_t *oldval,
265 int32_t change_val)
267 NTSTATUS ret;
268 struct dbwrap_change_int32_atomic_context state;
270 state.keystr = keystr;
271 state.oldval = oldval;
272 state.change_val = change_val;
274 ret = dbwrap_trans_do(db, dbwrap_change_int32_atomic_action, &state);
276 return ret;
279 struct dbwrap_store_context {
280 TDB_DATA *key;
281 TDB_DATA *dbuf;
282 int flag;
285 static NTSTATUS dbwrap_store_action(struct db_context *db, void *private_data)
287 struct db_record *rec = NULL;
288 NTSTATUS status;
289 struct dbwrap_store_context *store_ctx;
291 store_ctx = (struct dbwrap_store_context *)private_data;
293 rec = db->fetch_locked(db, talloc_tos(), *(store_ctx->key));
294 if (rec == NULL) {
295 DEBUG(5, ("fetch_locked failed\n"));
296 return NT_STATUS_NO_MEMORY;
299 status = rec->store(rec, *(store_ctx->dbuf), store_ctx->flag);
300 if (!NT_STATUS_IS_OK(status)) {
301 DEBUG(5, ("store returned %s\n", nt_errstr(status)));
304 TALLOC_FREE(rec);
305 return status;
308 NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
309 int flag)
311 NTSTATUS status;
312 struct dbwrap_store_context store_ctx;
314 store_ctx.key = &key;
315 store_ctx.dbuf = &dbuf;
316 store_ctx.flag = flag;
318 status = dbwrap_trans_do(db, dbwrap_store_action, &store_ctx);
320 return status;
323 static NTSTATUS dbwrap_delete_action(struct db_context * db, void *private_data)
325 NTSTATUS status;
326 struct db_record *rec;
327 TDB_DATA *key = (TDB_DATA *)private_data;
329 rec = db->fetch_locked(db, talloc_tos(), *key);
330 if (rec == NULL) {
331 DEBUG(5, ("fetch_locked failed\n"));
332 return NT_STATUS_NO_MEMORY;
335 status = rec->delete_rec(rec);
336 if (!NT_STATUS_IS_OK(status)) {
337 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status)));
340 talloc_free(rec);
341 return status;
344 NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
346 NTSTATUS status;
348 status = dbwrap_trans_do(db, dbwrap_delete_action, &key);
350 return status;
353 NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr,
354 int32_t v)
356 int32 v_store;
358 SIVAL(&v_store, 0, v);
360 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
361 make_tdb_data((const uint8 *)&v_store,
362 sizeof(v_store)),
363 TDB_REPLACE);
366 NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
367 uint32_t v)
369 uint32 v_store;
371 SIVAL(&v_store, 0, v);
373 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
374 make_tdb_data((const uint8 *)&v_store,
375 sizeof(v_store)),
376 TDB_REPLACE);
379 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
380 TDB_DATA data, int flags)
382 return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
385 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
387 return dbwrap_trans_delete(db, string_term_tdb_data(key));
391 * Wrap db action(s) into a transaction.
393 NTSTATUS dbwrap_trans_do(struct db_context *db,
394 NTSTATUS (*action)(struct db_context *, void *),
395 void *private_data)
397 int res;
398 NTSTATUS status;
400 res = db->transaction_start(db);
401 if (res != 0) {
402 DEBUG(5, ("transaction_start failed\n"));
403 return NT_STATUS_INTERNAL_DB_CORRUPTION;
406 status = action(db, private_data);
407 if (!NT_STATUS_IS_OK(status)) {
408 if (db->transaction_cancel(db) != 0) {
409 smb_panic("Cancelling transaction failed");
411 return status;
414 res = db->transaction_commit(db);
415 if (res == 0) {
416 return NT_STATUS_OK;
419 DEBUG(2, ("transaction_commit failed\n"));
420 return NT_STATUS_INTERNAL_DB_CORRUPTION;
423 struct dbwrap_trans_traverse_action_ctx {
424 int (*f)(struct db_record* rec, void* private_data);
425 void* private_data;
429 static NTSTATUS dbwrap_trans_traverse_action(struct db_context* db, void* private_data)
431 struct dbwrap_trans_traverse_action_ctx* ctx =
432 (struct dbwrap_trans_traverse_action_ctx*)private_data;
434 int ret = db->traverse(db, ctx->f, ctx->private_data);
436 return (ret == -1) ? NT_STATUS_INTERNAL_DB_CORRUPTION : NT_STATUS_OK;
439 NTSTATUS dbwrap_trans_traverse(struct db_context *db,
440 int (*f)(struct db_record*, void*),
441 void *private_data)
443 struct dbwrap_trans_traverse_action_ctx ctx = {
444 .f = f,
445 .private_data = private_data,
447 return dbwrap_trans_do(db, dbwrap_trans_traverse_action, &ctx);
450 NTSTATUS dbwrap_traverse(struct db_context *db,
451 int (*f)(struct db_record*, void*),
452 void *private_data)
454 int ret = db->traverse(db, f, private_data);
455 return (ret == -1) ? NT_STATUS_INTERNAL_DB_CORRUPTION : NT_STATUS_OK;
460 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
462 struct db_record *rec;
463 NTSTATUS status;
465 rec = db->fetch_locked(db, talloc_tos(), key);
466 if (rec == NULL) {
467 return NT_STATUS_NO_MEMORY;
469 status = rec->delete_rec(rec);
470 TALLOC_FREE(rec);
471 return status;
474 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
475 TDB_DATA data, int flags)
477 struct db_record *rec;
478 NTSTATUS status;
480 rec = db->fetch_locked(db, talloc_tos(), key);
481 if (rec == NULL) {
482 return NT_STATUS_NO_MEMORY;
485 status = rec->store(rec, data, flags);
486 TALLOC_FREE(rec);
487 return status;
490 TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
491 TDB_DATA key)
493 TDB_DATA result;
495 if (db->fetch(db, mem_ctx, key, &result) == -1) {
496 return make_tdb_data(NULL, 0);
499 return result;
502 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
504 return dbwrap_delete(db, string_term_tdb_data(key));
507 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
508 TDB_DATA data, int flags)
510 return dbwrap_store(db, string_term_tdb_data(key), data, flags);
513 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
514 const char *key)
516 return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key));
521 NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key)
523 char *key_upper;
524 NTSTATUS status;
526 key_upper = talloc_strdup_upper(talloc_tos(), key);
527 if (key_upper == NULL) {
528 return NT_STATUS_NO_MEMORY;
531 status = dbwrap_delete_bystring(db, key_upper);
533 talloc_free(key_upper);
534 return status;
537 NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
538 TDB_DATA data, int flags)
540 char *key_upper;
541 NTSTATUS status;
543 key_upper = talloc_strdup_upper(talloc_tos(), key);
544 if (key_upper == NULL) {
545 return NT_STATUS_NO_MEMORY;
548 status = dbwrap_store_bystring(db, key_upper, data, flags);
550 talloc_free(key_upper);
551 return status;
554 TDB_DATA dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
555 const char *key)
557 char *key_upper;
558 TDB_DATA result;
560 key_upper = talloc_strdup_upper(talloc_tos(), key);
561 if (key_upper == NULL) {
562 return make_tdb_data(NULL, 0);
565 result = dbwrap_fetch_bystring(db, mem_ctx, key_upper);
567 talloc_free(key_upper);
568 return result;