2 Unix SMB/CIFS implementation.
3 Utility functions for the dbwrap API
4 Copyright (C) Volker Lendecke 2007
5 Copyright (C) Michael Adam 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 int32_t dbwrap_fetch_int32(struct db_context
*db
, const char *keystr
)
29 if (db
->fetch(db
, NULL
, string_term_tdb_data(keystr
), &dbuf
) != 0) {
33 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(int32_t))) {
34 TALLOC_FREE(dbuf
.dptr
);
38 ret
= IVAL(dbuf
.dptr
, 0);
39 TALLOC_FREE(dbuf
.dptr
);
43 int dbwrap_store_int32(struct db_context
*db
, const char *keystr
, int32_t v
)
45 struct db_record
*rec
;
49 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(keystr
));
54 SIVAL(&v_store
, 0, v
);
56 status
= rec
->store(rec
, make_tdb_data((const uint8
*)&v_store
,
60 return NT_STATUS_IS_OK(status
) ? 0 : -1;
63 bool dbwrap_fetch_uint32(struct db_context
*db
, const char *keystr
,
68 if (db
->fetch(db
, NULL
, string_term_tdb_data(keystr
), &dbuf
) != 0) {
72 if ((dbuf
.dptr
== NULL
) || (dbuf
.dsize
!= sizeof(uint32_t))) {
73 TALLOC_FREE(dbuf
.dptr
);
77 *val
= IVAL(dbuf
.dptr
, 0);
78 TALLOC_FREE(dbuf
.dptr
);
82 int dbwrap_store_uint32(struct db_context
*db
, const char *keystr
, uint32_t v
)
84 struct db_record
*rec
;
88 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(keystr
));
93 SIVAL(&v_store
, 0, v
);
95 status
= rec
->store(rec
, make_tdb_data((const uint8
*)&v_store
,
99 return NT_STATUS_IS_OK(status
) ? 0 : -1;
103 * Atomic unsigned integer change (addition):
105 * if value does not exist yet in the db, use *oldval as initial old value.
106 * return old value in *oldval.
107 * store *oldval + change_val to db.
110 struct dbwrap_change_uint32_atomic_context
{
116 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
119 struct db_record
*rec
;
123 struct dbwrap_change_uint32_atomic_context
*state
;
125 state
= (struct dbwrap_change_uint32_atomic_context
*)private_data
;
127 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(state
->keystr
));
129 return NT_STATUS_UNSUCCESSFUL
;
132 if (rec
->value
.dptr
== NULL
) {
133 val
= *(state
->oldval
);
134 } else if (rec
->value
.dsize
== sizeof(val
)) {
135 val
= IVAL(rec
->value
.dptr
, 0);
136 *(state
->oldval
) = val
;
138 ret
= NT_STATUS_UNSUCCESSFUL
;
142 val
+= state
->change_val
;
144 data
.dsize
= sizeof(val
);
145 data
.dptr
= (uint8
*)&val
;
147 ret
= rec
->store(rec
, data
, TDB_REPLACE
);
154 NTSTATUS
dbwrap_change_uint32_atomic(struct db_context
*db
, const char *keystr
,
155 uint32_t *oldval
, uint32_t change_val
)
158 struct dbwrap_change_uint32_atomic_context state
;
160 state
.keystr
= keystr
;
161 state
.oldval
= oldval
;
162 state
.change_val
= change_val
;
164 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
169 NTSTATUS
dbwrap_trans_change_uint32_atomic(struct db_context
*db
,
175 struct dbwrap_change_uint32_atomic_context state
;
177 state
.keystr
= keystr
;
178 state
.oldval
= oldval
;
179 state
.change_val
= change_val
;
181 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
187 * Atomic integer change (addition):
189 * if value does not exist yet in the db, use *oldval as initial old value.
190 * return old value in *oldval.
191 * store *oldval + change_val to db.
194 struct dbwrap_change_int32_atomic_context
{
200 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
203 struct db_record
*rec
;
207 struct dbwrap_change_int32_atomic_context
*state
;
209 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
211 rec
= db
->fetch_locked(db
, NULL
, string_term_tdb_data(state
->keystr
));
213 return NT_STATUS_UNSUCCESSFUL
;
216 if (rec
->value
.dptr
== NULL
) {
217 val
= *(state
->oldval
);
218 } else if (rec
->value
.dsize
== sizeof(val
)) {
219 val
= IVAL(rec
->value
.dptr
, 0);
220 *(state
->oldval
) = val
;
222 ret
= NT_STATUS_UNSUCCESSFUL
;
226 val
+= state
->change_val
;
228 data
.dsize
= sizeof(val
);
229 data
.dptr
= (uint8
*)&val
;
231 ret
= rec
->store(rec
, data
, TDB_REPLACE
);
238 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
, const char *keystr
,
239 int32_t *oldval
, int32_t change_val
)
242 struct dbwrap_change_int32_atomic_context state
;
244 state
.keystr
= keystr
;
245 state
.oldval
= oldval
;
246 state
.change_val
= change_val
;
248 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
253 NTSTATUS
dbwrap_trans_change_int32_atomic(struct db_context
*db
,
259 struct dbwrap_change_int32_atomic_context state
;
261 state
.keystr
= keystr
;
262 state
.oldval
= oldval
;
263 state
.change_val
= change_val
;
265 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
270 struct dbwrap_store_context
{
276 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
278 struct db_record
*rec
= NULL
;
280 struct dbwrap_store_context
*store_ctx
;
282 store_ctx
= (struct dbwrap_store_context
*)private_data
;
284 rec
= db
->fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
286 DEBUG(5, ("fetch_locked failed\n"));
287 return NT_STATUS_NO_MEMORY
;
290 status
= rec
->store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
291 if (!NT_STATUS_IS_OK(status
)) {
292 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
299 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
303 struct dbwrap_store_context store_ctx
;
305 store_ctx
.key
= &key
;
306 store_ctx
.dbuf
= &dbuf
;
307 store_ctx
.flag
= flag
;
309 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
314 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
317 struct db_record
*rec
;
318 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
320 rec
= db
->fetch_locked(db
, talloc_tos(), *key
);
322 DEBUG(5, ("fetch_locked failed\n"));
323 return NT_STATUS_NO_MEMORY
;
326 status
= rec
->delete_rec(rec
);
327 if (!NT_STATUS_IS_OK(status
)) {
328 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
335 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
339 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
344 NTSTATUS
dbwrap_trans_store_int32(struct db_context
*db
, const char *keystr
,
349 SIVAL(&v_store
, 0, v
);
351 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
352 make_tdb_data((const uint8
*)&v_store
,
357 NTSTATUS
dbwrap_trans_store_uint32(struct db_context
*db
, const char *keystr
,
362 SIVAL(&v_store
, 0, v
);
364 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
365 make_tdb_data((const uint8
*)&v_store
,
370 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
371 TDB_DATA data
, int flags
)
373 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
376 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
378 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
382 * Wrap db action(s) into a transaction.
384 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
385 NTSTATUS (*action
)(struct db_context
*, void *),
391 res
= db
->transaction_start(db
);
393 DEBUG(5, ("transaction_start failed\n"));
394 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
397 status
= action(db
, private_data
);
398 if (!NT_STATUS_IS_OK(status
)) {
399 if (db
->transaction_cancel(db
) != 0) {
400 smb_panic("Cancelling transaction failed");
405 res
= db
->transaction_commit(db
);
410 DEBUG(2, ("transaction_commit failed\n"));
411 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
414 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
419 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
420 if (key_upper
== NULL
) {
421 return NT_STATUS_NO_MEMORY
;
424 status
= dbwrap_delete_bystring(db
, key_upper
);
426 talloc_free(key_upper
);
430 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
431 TDB_DATA data
, int flags
)
436 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
437 if (key_upper
== NULL
) {
438 return NT_STATUS_NO_MEMORY
;
441 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
443 talloc_free(key_upper
);
447 TDB_DATA
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
453 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
454 if (key_upper
== NULL
) {
455 return make_tdb_data(NULL
, 0);
458 result
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
);
460 talloc_free(key_upper
);