s3:dbwrap_util: add my C
[Samba.git] / source3 / lib / dbwrap_util.c
blob6c956729863810952c428220b5a494a4e7aa145c
1 /*
2 Unix SMB/CIFS implementation.
3 Utility functions for the dbwrap API
4 Copyright (C) Volker Lendecke 2007
5 Copyrithg (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.
22 #include "includes.h"
24 int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
26 TDB_DATA dbuf;
27 int32 ret;
29 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
30 return -1;
33 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
34 TALLOC_FREE(dbuf.dptr);
35 return -1;
38 ret = IVAL(dbuf.dptr, 0);
39 TALLOC_FREE(dbuf.dptr);
40 return ret;
43 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
45 struct db_record *rec;
46 int32 v_store;
47 NTSTATUS status;
49 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
50 if (rec == NULL) {
51 return -1;
54 SIVAL(&v_store, 0, v);
56 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
57 sizeof(v_store)),
58 TDB_REPLACE);
59 TALLOC_FREE(rec);
60 return NT_STATUS_IS_OK(status) ? 0 : -1;
63 bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
64 uint32_t *val)
66 TDB_DATA dbuf;
68 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
69 return false;
72 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) {
73 TALLOC_FREE(dbuf.dptr);
74 return false;
77 *val = IVAL(dbuf.dptr, 0);
78 TALLOC_FREE(dbuf.dptr);
79 return true;
82 int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v)
84 struct db_record *rec;
85 uint32 v_store;
86 NTSTATUS status;
88 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
89 if (rec == NULL) {
90 return -1;
93 SIVAL(&v_store, 0, v);
95 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
96 sizeof(v_store)),
97 TDB_REPLACE);
98 TALLOC_FREE(rec);
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.
109 uint32_t dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr,
110 uint32_t *oldval, uint32_t change_val)
112 struct db_record *rec;
113 uint32 val = -1;
114 TDB_DATA data;
116 if (!(rec = db->fetch_locked(db, NULL,
117 string_term_tdb_data(keystr)))) {
118 return -1;
121 if (rec->value.dptr == NULL) {
122 val = *oldval;
123 } else if (rec->value.dsize == sizeof(val)) {
124 val = IVAL(rec->value.dptr, 0);
125 *oldval = val;
126 } else {
127 return -1;
130 val += change_val;
132 data.dsize = sizeof(val);
133 data.dptr = (uint8 *)&val;
135 rec->store(rec, data, TDB_REPLACE);
137 TALLOC_FREE(rec);
139 return 0;
143 * Atomic integer change (addition):
145 * if value does not exist yet in the db, use *oldval as initial old value.
146 * return old value in *oldval.
147 * store *oldval + change_val to db.
149 int32 dbwrap_change_int32_atomic(struct db_context *db, const char *keystr,
150 int32 *oldval, int32 change_val)
152 struct db_record *rec;
153 int32 val = -1;
154 TDB_DATA data;
156 if (!(rec = db->fetch_locked(db, NULL,
157 string_term_tdb_data(keystr)))) {
158 return -1;
161 if (rec->value.dptr == NULL) {
162 val = *oldval;
163 } else if (rec->value.dsize == sizeof(val)) {
164 val = IVAL(rec->value.dptr, 0);
165 *oldval = val;
166 } else {
167 return -1;
170 val += change_val;
172 data.dsize = sizeof(val);
173 data.dptr = (uint8 *)&val;
175 rec->store(rec, data, TDB_REPLACE);
177 TALLOC_FREE(rec);
179 return 0;
182 NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
183 int flag)
185 int res;
186 struct db_record *rec = NULL;
187 NTSTATUS status;
189 res = db->transaction_start(db);
190 if (res != 0) {
191 DEBUG(5, ("transaction_start failed\n"));
192 return NT_STATUS_INTERNAL_DB_CORRUPTION;
195 rec = db->fetch_locked(db, talloc_tos(), key);
196 if (rec == NULL) {
197 DEBUG(5, ("fetch_locked failed\n"));
198 status = NT_STATUS_NO_MEMORY;
199 goto cancel;
202 status = rec->store(rec, dbuf, flag);
203 if (!NT_STATUS_IS_OK(status)) {
204 DEBUG(5, ("store returned %s\n", nt_errstr(status)));
205 goto cancel;
208 TALLOC_FREE(rec);
210 res = db->transaction_commit(db);
211 if (res != 0) {
212 DEBUG(5, ("tdb_transaction_commit failed\n"));
213 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
214 TALLOC_FREE(rec);
215 return status;
218 return NT_STATUS_OK;
220 cancel:
221 TALLOC_FREE(rec);
223 if (db->transaction_cancel(db) != 0) {
224 smb_panic("Cancelling transaction failed");
226 return status;
229 NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
231 int res;
232 struct db_record *rec = NULL;
233 NTSTATUS status;
235 res = db->transaction_start(db);
236 if (res != 0) {
237 DEBUG(5, ("transaction_start failed\n"));
238 return NT_STATUS_INTERNAL_DB_CORRUPTION;
241 rec = db->fetch_locked(db, talloc_tos(), key);
242 if (rec == NULL) {
243 DEBUG(5, ("fetch_locked failed\n"));
244 status = NT_STATUS_NO_MEMORY;
245 goto cancel;
248 status = rec->delete_rec(rec);
249 if (!NT_STATUS_IS_OK(status)) {
250 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status)));
251 goto cancel;
254 TALLOC_FREE(rec);
256 res = db->transaction_commit(db);
257 if (res != 0) {
258 DEBUG(5, ("tdb_transaction_commit failed\n"));
259 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
260 TALLOC_FREE(rec);
261 return status;
264 return NT_STATUS_OK;
266 cancel:
267 TALLOC_FREE(rec);
269 if (db->transaction_cancel(db) != 0) {
270 smb_panic("Cancelling transaction failed");
272 return status;
275 NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr,
276 int32_t v)
278 int32 v_store;
280 SIVAL(&v_store, 0, v);
282 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
283 make_tdb_data((const uint8 *)&v_store,
284 sizeof(v_store)),
285 TDB_REPLACE);
288 NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
289 uint32_t v)
291 uint32 v_store;
293 SIVAL(&v_store, 0, v);
295 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
296 make_tdb_data((const uint8 *)&v_store,
297 sizeof(v_store)),
298 TDB_REPLACE);
301 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
302 TDB_DATA data, int flags)
304 return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
307 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
309 return dbwrap_trans_delete(db, string_term_tdb_data(key));
313 * Wrap db action(s) into a transaction.
315 NTSTATUS dbwrap_trans_do(struct db_context *db,
316 NTSTATUS (*action)(struct db_context *, void *),
317 void *private_data)
319 int res;
320 NTSTATUS status;
322 res = db->transaction_start(db);
323 if (res != 0) {
324 DEBUG(5, ("transaction_start failed\n"));
325 return NT_STATUS_INTERNAL_DB_CORRUPTION;
328 status = action(db, private_data);
329 if (!NT_STATUS_IS_OK(status)) {
330 if (db->transaction_cancel(db) != 0) {
331 smb_panic("Cancelling transaction failed");
333 return status;
336 res = db->transaction_commit(db);
337 if (res == 0) {
338 return NT_STATUS_OK;
341 DEBUG(2, ("transaction_commit failed\n"));
342 return NT_STATUS_INTERNAL_DB_CORRUPTION;
345 NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key)
347 char *key_upper;
348 NTSTATUS status;
350 key_upper = talloc_strdup_upper(talloc_tos(), key);
351 if (key_upper == NULL) {
352 return NT_STATUS_NO_MEMORY;
355 status = dbwrap_delete_bystring(db, key_upper);
357 talloc_free(key_upper);
358 return status;
361 NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
362 TDB_DATA data, int flags)
364 char *key_upper;
365 NTSTATUS status;
367 key_upper = talloc_strdup_upper(talloc_tos(), key);
368 if (key_upper == NULL) {
369 return NT_STATUS_NO_MEMORY;
372 status = dbwrap_store_bystring(db, key_upper, data, flags);
374 talloc_free(key_upper);
375 return status;
378 TDB_DATA dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
379 const char *key)
381 char *key_upper;
382 TDB_DATA result;
384 key_upper = talloc_strdup_upper(talloc_tos(), key);
385 if (key_upper == NULL) {
386 return make_tdb_data(NULL, 0);
389 result = dbwrap_fetch_bystring(db, mem_ctx, key_upper);
391 talloc_free(key_upper);
392 return result;