2 Unix SMB/CIFS implementation.
3 Utility functions for the dbwrap API
4 Copyright (C) Volker Lendecke 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
28 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
32 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
33 TALLOC_FREE(dbuf.dptr);
37 ret = IVAL(dbuf.dptr, 0);
38 TALLOC_FREE(dbuf.dptr);
42 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
44 struct db_record *rec;
48 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
53 SIVAL(&v_store, 0, v);
55 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
59 return NT_STATUS_IS_OK(status) ? 0 : -1;
62 bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
67 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
71 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) {
72 TALLOC_FREE(dbuf.dptr);
76 *val = IVAL(dbuf.dptr, 0);
77 TALLOC_FREE(dbuf.dptr);
81 bool dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v)
83 struct db_record *rec;
87 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
92 SIVAL(&v_store, 0, v);
94 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
98 return NT_STATUS_IS_OK(status) ? 0 : -1;
102 * Atomic unsigned integer change (addition):
104 * if value does not exist yet in the db, use *oldval as initial old value.
105 * return old value in *oldval.
106 * store *oldval + change_val to db.
108 uint32_t dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr,
109 uint32_t *oldval, uint32_t change_val)
111 struct db_record *rec;
115 if (!(rec = db->fetch_locked(db, NULL,
116 string_term_tdb_data(keystr)))) {
120 if (rec->value.dptr == NULL) {
122 } else if (rec->value.dsize == sizeof(val)) {
123 val = IVAL(rec->value.dptr, 0);
131 data.dsize = sizeof(val);
132 data.dptr = (uint8 *)&val;
134 rec->store(rec, data, TDB_REPLACE);
141 int32 dbwrap_change_int32_atomic(struct db_context *db, const char *keystr,
142 int32 *oldval, int32 change_val)
144 struct db_record *rec;
148 if (!(rec = db->fetch_locked(db, NULL,
149 string_term_tdb_data(keystr)))) {
153 if (rec->value.dptr == NULL) {
155 } else if (rec->value.dsize == sizeof(val)) {
156 val = IVAL(rec->value.dptr, 0);
164 data.dsize = sizeof(val);
165 data.dptr = (uint8 *)&val;
167 rec->store(rec, data, TDB_REPLACE);
174 NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
178 struct db_record *rec = NULL;
181 res = db->transaction_start(db);
183 DEBUG(5, ("transaction_start failed\n"));
184 return NT_STATUS_INTERNAL_DB_CORRUPTION;
187 rec = db->fetch_locked(db, talloc_tos(), key);
189 DEBUG(5, ("fetch_locked failed\n"));
190 status = NT_STATUS_NO_MEMORY;
194 status = rec->store(rec, dbuf, flag);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(5, ("store returned %s\n", nt_errstr(status)));
202 res = db->transaction_commit(db);
204 DEBUG(5, ("tdb_transaction_commit failed\n"));
205 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
214 if (db->transaction_cancel(db) != 0) {
215 smb_panic("Cancelling transaction failed");
220 NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
223 struct db_record *rec = NULL;
226 res = db->transaction_start(db);
228 DEBUG(5, ("transaction_start failed\n"));
229 return NT_STATUS_INTERNAL_DB_CORRUPTION;
232 rec = db->fetch_locked(db, talloc_tos(), key);
234 DEBUG(5, ("fetch_locked failed\n"));
235 status = NT_STATUS_NO_MEMORY;
239 status = rec->delete_rec(rec);
240 if (!NT_STATUS_IS_OK(status)) {
241 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status)));
247 res = db->transaction_commit(db);
249 DEBUG(5, ("tdb_transaction_commit failed\n"));
250 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
259 if (db->transaction_cancel(db) != 0) {
260 smb_panic("Cancelling transaction failed");
265 NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr,
270 SIVAL(&v_store, 0, v);
272 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
273 make_tdb_data((const uint8 *)&v_store,
278 NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
283 SIVAL(&v_store, 0, v);
285 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
286 make_tdb_data((const uint8 *)&v_store,
291 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
292 TDB_DATA data, int flags)
294 return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
297 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
299 return dbwrap_trans_delete(db, string_term_tdb_data(key));