s3:fix endianness bug in dbwrap_change_int32_atomic() (bug #6901)
[Samba.git] / source3 / lib / dbwrap_util.c
blob728eb99759a5730f5755615355ed69e0e0efb5d6
1 /*
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.
21 #include "includes.h"
23 int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
25 TDB_DATA dbuf;
26 int32 ret;
28 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
29 return -1;
32 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
33 TALLOC_FREE(dbuf.dptr);
34 return -1;
37 ret = IVAL(dbuf.dptr, 0);
38 TALLOC_FREE(dbuf.dptr);
39 return ret;
42 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
44 struct db_record *rec;
45 int32 v_store;
46 NTSTATUS status;
48 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
49 if (rec == NULL) {
50 return -1;
53 SIVAL(&v_store, 0, v);
55 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
56 sizeof(v_store)),
57 TDB_REPLACE);
58 TALLOC_FREE(rec);
59 return NT_STATUS_IS_OK(status) ? 0 : -1;
62 bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
63 uint32_t *val)
65 TDB_DATA dbuf;
67 if (db->fetch(db, NULL, string_term_tdb_data(keystr), &dbuf) != 0) {
68 return false;
71 if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) {
72 TALLOC_FREE(dbuf.dptr);
73 return false;
76 *val = IVAL(dbuf.dptr, 0);
77 TALLOC_FREE(dbuf.dptr);
78 return true;
81 int dbwrap_store_uint32(struct db_context *db, const char *keystr, uint32_t v)
83 struct db_record *rec;
84 uint32 v_store;
85 NTSTATUS status;
87 rec = db->fetch_locked(db, NULL, string_term_tdb_data(keystr));
88 if (rec == NULL) {
89 return -1;
92 SIVAL(&v_store, 0, v);
94 status = rec->store(rec, make_tdb_data((const uint8 *)&v_store,
95 sizeof(v_store)),
96 TDB_REPLACE);
97 TALLOC_FREE(rec);
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;
112 uint32 val = -1;
113 uint32_t v_store;
115 if (!(rec = db->fetch_locked(db, NULL,
116 string_term_tdb_data(keystr)))) {
117 return -1;
120 if (rec->value.dptr == NULL) {
121 val = *oldval;
122 } else if (rec->value.dsize == sizeof(val)) {
123 val = IVAL(rec->value.dptr, 0);
124 *oldval = val;
125 } else {
126 return -1;
129 val += change_val;
131 SIVAL(&v_store, 0, val);
133 rec->store(rec,
134 make_tdb_data((const uint8_t *)&v_store, sizeof(v_store)),
135 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 int32_t v_store;
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 SIVAL(&v_store, 0, val);
174 rec->store(rec,
175 make_tdb_data((const uint8_t *)&v_store, sizeof(v_store)),
176 TDB_REPLACE);
178 TALLOC_FREE(rec);
180 return 0;
183 NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
184 int flag)
186 int res;
187 struct db_record *rec = NULL;
188 NTSTATUS status;
190 res = db->transaction_start(db);
191 if (res != 0) {
192 DEBUG(5, ("transaction_start failed\n"));
193 return NT_STATUS_INTERNAL_DB_CORRUPTION;
196 rec = db->fetch_locked(db, talloc_tos(), key);
197 if (rec == NULL) {
198 DEBUG(5, ("fetch_locked failed\n"));
199 status = NT_STATUS_NO_MEMORY;
200 goto cancel;
203 status = rec->store(rec, dbuf, flag);
204 if (!NT_STATUS_IS_OK(status)) {
205 DEBUG(5, ("store returned %s\n", nt_errstr(status)));
206 goto cancel;
209 TALLOC_FREE(rec);
211 res = db->transaction_commit(db);
212 if (res != 0) {
213 DEBUG(5, ("tdb_transaction_commit failed\n"));
214 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
215 TALLOC_FREE(rec);
216 return status;
219 return NT_STATUS_OK;
221 cancel:
222 TALLOC_FREE(rec);
224 if (db->transaction_cancel(db) != 0) {
225 smb_panic("Cancelling transaction failed");
227 return status;
230 NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
232 int res;
233 struct db_record *rec = NULL;
234 NTSTATUS status;
236 res = db->transaction_start(db);
237 if (res != 0) {
238 DEBUG(5, ("transaction_start failed\n"));
239 return NT_STATUS_INTERNAL_DB_CORRUPTION;
242 rec = db->fetch_locked(db, talloc_tos(), key);
243 if (rec == NULL) {
244 DEBUG(5, ("fetch_locked failed\n"));
245 status = NT_STATUS_NO_MEMORY;
246 goto cancel;
249 status = rec->delete_rec(rec);
250 if (!NT_STATUS_IS_OK(status)) {
251 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status)));
252 goto cancel;
255 TALLOC_FREE(rec);
257 res = db->transaction_commit(db);
258 if (res != 0) {
259 DEBUG(5, ("tdb_transaction_commit failed\n"));
260 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
261 TALLOC_FREE(rec);
262 return status;
265 return NT_STATUS_OK;
267 cancel:
268 TALLOC_FREE(rec);
270 if (db->transaction_cancel(db) != 0) {
271 smb_panic("Cancelling transaction failed");
273 return status;
276 NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr,
277 int32_t v)
279 int32 v_store;
281 SIVAL(&v_store, 0, v);
283 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
284 make_tdb_data((const uint8 *)&v_store,
285 sizeof(v_store)),
286 TDB_REPLACE);
289 NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
290 uint32_t v)
292 uint32 v_store;
294 SIVAL(&v_store, 0, v);
296 return dbwrap_trans_store(db, string_term_tdb_data(keystr),
297 make_tdb_data((const uint8 *)&v_store,
298 sizeof(v_store)),
299 TDB_REPLACE);
302 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
303 TDB_DATA data, int flags)
305 return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
308 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
310 return dbwrap_trans_delete(db, string_term_tdb_data(key));