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.
29 int32_t dbwrap_fetch_int32(struct db_context
*db
, const char *keystr
)
34 if (db
->fetch(db
, NULL
, string_term_tdb_data(keystr
), &dbuf
) != 0) {
38 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(int32_t))) {
39 TALLOC_FREE(dbuf
.dptr
);
43 ret
= IVAL(dbuf
.dptr
, 0);
44 TALLOC_FREE(dbuf
.dptr
);
48 int dbwrap_store_int32(struct db_context
*db
, const char *keystr
, int32_t v
)
50 struct db_record
*rec
;
54 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(keystr
));
59 SIVAL(&v_store
, 0, v
);
61 status
= rec
->store(rec
, make_tdb_data((const uint8
*)&v_store
,
65 return NT_STATUS_IS_OK(status
) ? 0 : -1;
68 bool dbwrap_fetch_uint32(struct db_context
*db
, const char *keystr
,
73 if (db
->fetch(db
, NULL
, string_term_tdb_data(keystr
), &dbuf
) != 0) {
77 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(uint32_t))) {
78 TALLOC_FREE(dbuf
.dptr
);
82 *val
= IVAL(dbuf
.dptr
, 0);
83 TALLOC_FREE(dbuf
.dptr
);
87 int dbwrap_store_uint32(struct db_context
*db
, const char *keystr
, uint32_t v
)
89 struct db_record
*rec
;
93 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(keystr
));
98 SIVAL(&v_store
, 0, v
);
100 status
= rec
->store(rec
, make_tdb_data((const uint8
*)&v_store
,
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
{
121 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
124 struct db_record
*rec
;
125 uint32_t val
= (uint32_t)-1;
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
));
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
;
143 ret
= NT_STATUS_UNSUCCESSFUL
;
147 val
+= state
->change_val
;
149 SIVAL(&v_store
, 0, val
);
151 ret
= rec
->store(rec
,
152 make_tdb_data((const uint8
*)&v_store
,
161 NTSTATUS
dbwrap_change_uint32_atomic(struct db_context
*db
, const char *keystr
,
162 uint32_t *oldval
, uint32_t change_val
)
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
);
176 NTSTATUS
dbwrap_trans_change_uint32_atomic(struct db_context
*db
,
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
);
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
{
207 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
210 struct db_record
*rec
;
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
));
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
;
229 ret
= NT_STATUS_UNSUCCESSFUL
;
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
,
247 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
, const char *keystr
,
248 int32_t *oldval
, int32_t change_val
)
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
);
262 NTSTATUS
dbwrap_trans_change_int32_atomic(struct db_context
*db
,
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
);
279 struct dbwrap_store_context
{
285 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
287 struct db_record
*rec
= NULL
;
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
));
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
)));
308 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
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
);
323 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
326 struct db_record
*rec
;
327 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
329 rec
= db
->fetch_locked(db
, talloc_tos(), *key
);
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
)));
344 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
348 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
353 NTSTATUS
dbwrap_trans_store_int32(struct db_context
*db
, const char *keystr
,
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
,
366 NTSTATUS
dbwrap_trans_store_uint32(struct db_context
*db
, const char *keystr
,
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
,
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 *),
400 res
= db
->transaction_start(db
);
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");
414 res
= db
->transaction_commit(db
);
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
);
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*),
443 struct dbwrap_trans_traverse_action_ctx ctx
= {
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*),
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
;
465 rec
= db
->fetch_locked(db
, talloc_tos(), key
);
467 return NT_STATUS_NO_MEMORY
;
469 status
= rec
->delete_rec(rec
);
474 NTSTATUS
dbwrap_store(struct db_context
*db
, TDB_DATA key
,
475 TDB_DATA data
, int flags
)
477 struct db_record
*rec
;
480 rec
= db
->fetch_locked(db
, talloc_tos(), key
);
482 return NT_STATUS_NO_MEMORY
;
485 status
= rec
->store(rec
, data
, flags
);
490 TDB_DATA
dbwrap_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
495 if (db
->fetch(db
, mem_ctx
, key
, &result
) == -1) {
496 return make_tdb_data(NULL
, 0);
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
,
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
)
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
);
537 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
538 TDB_DATA data
, int flags
)
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
);
554 TDB_DATA
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
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
);