ctdb-tests: Loosen match against pstree output in simple test
[samba.git] / lib / util / util_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Andrew Tridgell 1992-2006
7    Copyright (C) Volker Lendecke 2007-2011
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "replace.h"
24 #include <talloc.h>
25 #include "libcli/util/ntstatus.h"
26 #include "lib/util/memory.h"
27 #include "lib/util/byteorder.h"
28 #include "system/filesys.h"
29 #include "../lib/tdb/include/tdb.h"
30 #include "../lib/util/util_tdb.h"
31
32 /* these are little tdb utility functions that are meant to make
33    dealing with a tdb database a little less cumbersome in Samba */
34
35 /***************************************************************
36  Make a TDB_DATA and keep the const warning in one place
37 ****************************************************************/
38
39 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
40 {
41         TDB_DATA ret;
42         ret.dptr = discard_const_p(uint8_t, dptr);
43         ret.dsize = dsize;
44         return ret;
45 }
46
47 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
48 {
49         if (t1.dsize != t2.dsize) {
50                 return false;
51         }
52         return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
53 }
54
55 bool tdb_data_is_empty(TDB_DATA d)
56 {
57         return (d.dsize == 0) || (d.dptr == NULL);
58 }
59
60 TDB_DATA string_tdb_data(const char *string)
61 {
62         return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
63 }
64
65 TDB_DATA string_term_tdb_data(const char *string)
66 {
67         return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
68 }
69
70 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
71         TDB_DATA ret = {
72                 .dptr  = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
73                 .dsize = data.dsize
74         };
75         if (ret.dptr == NULL) {
76                 ret.dsize = 0;
77         } else {
78                 memcpy(ret.dptr, data.dptr, data.dsize);
79                 ret.dptr[ret.dsize] = '\0';
80         }
81         return ret;
82 }
83
84
85 /****************************************************************************
86  Lock a chain by string. Return non-zero if lock failed.
87 ****************************************************************************/
88
89 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
90 {
91         TDB_DATA key = string_term_tdb_data(keyval);
92         
93         return tdb_chainlock(tdb, key);
94 }
95
96 /****************************************************************************
97  Unlock a chain by string.
98 ****************************************************************************/
99
100 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
101 {
102         TDB_DATA key = string_term_tdb_data(keyval);
103
104         tdb_chainunlock(tdb, key);
105 }
106
107 /****************************************************************************
108  Read lock a chain by string. Return non-zero if lock failed.
109 ****************************************************************************/
110
111 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
112 {
113         TDB_DATA key = string_term_tdb_data(keyval);
114         
115         return tdb_chainlock_read(tdb, key);
116 }
117
118 /****************************************************************************
119  Read unlock a chain by string.
120 ****************************************************************************/
121
122 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
123 {
124         TDB_DATA key = string_term_tdb_data(keyval);
125         
126         tdb_chainunlock_read(tdb, key);
127 }
128
129
130 /****************************************************************************
131  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
132  Output is int32_t in native byte order.
133 ****************************************************************************/
134
135 static int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
136 {
137         TDB_DATA data;
138         int32_t ret;
139
140         data = tdb_fetch(tdb, key);
141         if (!data.dptr || data.dsize != sizeof(int32_t)) {
142                 SAFE_FREE(data.dptr);
143                 return -1;
144         }
145
146         ret = IVAL(data.dptr,0);
147         SAFE_FREE(data.dptr);
148         return ret;
149 }
150
151 /****************************************************************************
152  Fetch a int32_t value by string key, return -1 if not found.
153  Output is int32_t in native byte order.
154 ****************************************************************************/
155
156 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
157 {
158         return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
159 }
160
161 /****************************************************************************
162  Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
163  Input is int32_t in native byte order. Output in tdb is in little-endian.
164 ****************************************************************************/
165
166 static int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key,
167                                   int32_t v)
168 {
169         TDB_DATA data;
170         int32_t v_store;
171
172         SIVAL(&v_store,0,v);
173         data.dptr = (unsigned char *)&v_store;
174         data.dsize = sizeof(int32_t);
175
176         return tdb_store(tdb, key, data, TDB_REPLACE);
177 }
178
179 /****************************************************************************
180  Store a int32_t value by string key, return 0 on success, -ve on failure.
181  Input is int32_t in native byte order. Output in tdb is in little-endian.
182 ****************************************************************************/
183
184 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
185 {
186         return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
187 }
188
189 /****************************************************************************
190  Fetch a uint32_t value by a arbitrary blob key, return false if not found.
191  Output is uint32_t in native byte order.
192 ****************************************************************************/
193
194 static bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
195                                     uint32_t *value)
196 {
197         TDB_DATA data;
198
199         data = tdb_fetch(tdb, key);
200         if (!data.dptr || data.dsize != sizeof(uint32_t)) {
201                 SAFE_FREE(data.dptr);
202                 return false;
203         }
204
205         *value = IVAL(data.dptr,0);
206         SAFE_FREE(data.dptr);
207         return true;
208 }
209
210 /****************************************************************************
211  Fetch a uint32_t value by string key, return false if not found.
212  Output is uint32_t in native byte order.
213 ****************************************************************************/
214
215 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
216 {
217         return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
218 }
219
220 /****************************************************************************
221  Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
222  Input is uint32_t in native byte order. Output in tdb is in little-endian.
223 ****************************************************************************/
224
225 static bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
226                                     uint32_t value)
227 {
228         TDB_DATA data;
229         uint32_t v_store;
230         bool ret = true;
231
232         SIVAL(&v_store, 0, value);
233         data.dptr = (unsigned char *)&v_store;
234         data.dsize = sizeof(uint32_t);
235
236         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
237                 ret = false;
238
239         return ret;
240 }
241
242 /****************************************************************************
243  Store a uint32_t value by string key, return true on success, false on failure.
244  Input is uint32_t in native byte order. Output in tdb is in little-endian.
245 ****************************************************************************/
246
247 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
248 {
249         return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
250 }
251 /****************************************************************************
252  Store a buffer by a null terminated string key.  Return 0 on success, -ve
253  on failure.
254 ****************************************************************************/
255
256 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
257 {
258         TDB_DATA key = string_term_tdb_data(keystr);
259         
260         return tdb_store(tdb, key, data, flags);
261 }
262
263 /****************************************************************************
264  Fetch a buffer using a null terminated string key.  Don't forget to call
265  free() on the result dptr.
266 ****************************************************************************/
267
268 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
269 {
270         TDB_DATA key = string_term_tdb_data(keystr);
271
272         return tdb_fetch(tdb, key);
273 }
274
275 /****************************************************************************
276  Delete an entry using a null terminated string key. 
277 ****************************************************************************/
278
279 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
280 {
281         TDB_DATA key = string_term_tdb_data(keystr);
282
283         return tdb_delete(tdb, key);
284 }
285
286 /****************************************************************************
287  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
288 ****************************************************************************/
289
290 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
291 {
292         int32_t val;
293         int32_t ret = -1;
294
295         if (tdb_lock_bystring(tdb, keystr) != 0)
296                 return -1;
297
298         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
299                 /* The lookup failed */
300                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
301                         /* but not because it didn't exist */
302                         goto err_out;
303                 }
304                 
305                 /* Start with 'old' value */
306                 val = *oldval;
307
308         } else {
309                 /* It worked, set return value (oldval) to tdb data */
310                 *oldval = val;
311         }
312
313         /* Increment value for storage and return next time */
314         val += change_val;
315                 
316         if (tdb_store_int32(tdb, keystr, val) != 0)
317                 goto err_out;
318
319         ret = 0;
320
321   err_out:
322
323         tdb_unlock_bystring(tdb, keystr);
324         return ret;
325 }
326
327 /****************************************************************************
328  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
329 ****************************************************************************/
330
331 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
332 {
333         uint32_t val;
334         bool ret = false;
335
336         if (tdb_lock_bystring(tdb, keystr) != 0)
337                 return false;
338
339         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
340                 /* It failed */
341                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
342                         /* and not because it didn't exist */
343                         goto err_out;
344                 }
345
346                 /* Start with 'old' value */
347                 val = *oldval;
348
349         } else {
350                 /* it worked, set return value (oldval) to tdb data */
351                 *oldval = val;
352
353         }
354
355         /* get a new value to store */
356         val += change_val;
357                 
358         if (!tdb_store_uint32(tdb, keystr, val))
359                 goto err_out;
360
361         ret = true;
362
363   err_out:
364
365         tdb_unlock_bystring(tdb, keystr);
366         return ret;
367 }
368
369 /****************************************************************************
370  Allow tdb_delete to be used as a tdb_traversal_fn.
371 ****************************************************************************/
372
373 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
374                      void *state)
375 {
376     return tdb_delete(the_tdb, key);
377 }
378
379 /****************************************************************************
380  Return an NTSTATUS from a TDB_ERROR
381 ****************************************************************************/
382
383 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
384 {
385         NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
386
387         switch (err) {
388         case TDB_SUCCESS:
389                 result = NT_STATUS_OK;
390                 break;
391         case TDB_ERR_CORRUPT:
392                 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
393                 break;
394         case TDB_ERR_IO:
395                 result = NT_STATUS_UNEXPECTED_IO_ERROR;
396                 break;
397         case TDB_ERR_OOM:
398                 result = NT_STATUS_NO_MEMORY;
399                 break;
400         case TDB_ERR_EXISTS:
401                 result = NT_STATUS_OBJECT_NAME_COLLISION;
402                 break;
403
404         case TDB_ERR_LOCK:
405                 /*
406                  * TDB_ERR_LOCK is very broad, we could for example
407                  * distinguish between fcntl locks and invalid lock
408                  * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
409                  * compromise.
410                  */
411                 result = NT_STATUS_FILE_LOCK_CONFLICT;
412                 break;
413
414         case TDB_ERR_NOLOCK:
415         case TDB_ERR_LOCK_TIMEOUT:
416                 /*
417                  * These two ones in the enum are not actually used
418                  */
419                 result = NT_STATUS_FILE_LOCK_CONFLICT;
420                 break;
421         case TDB_ERR_NOEXIST:
422                 result = NT_STATUS_NOT_FOUND;
423                 break;
424         case TDB_ERR_EINVAL:
425                 result = NT_STATUS_INVALID_PARAMETER;
426                 break;
427         case TDB_ERR_RDONLY:
428                 result = NT_STATUS_ACCESS_DENIED;
429                 break;
430         case TDB_ERR_NESTING:
431                 result = NT_STATUS_INTERNAL_ERROR;
432                 break;
433         };
434         return result;
435 }
436
437 int map_unix_error_from_tdb(enum TDB_ERROR err)
438 {
439         int result = EINVAL;
440
441         switch (err) {
442         case TDB_SUCCESS:
443                 result = 0;
444                 break;
445         case TDB_ERR_CORRUPT:
446                 result = EILSEQ;
447                 break;
448         case TDB_ERR_IO:
449                 result = EIO;
450                 break;
451         case TDB_ERR_OOM:
452                 result = ENOMEM;
453                 break;
454         case TDB_ERR_EXISTS:
455                 result = EEXIST;
456                 break;
457
458         case TDB_ERR_LOCK:
459                 /*
460                  * TDB_ERR_LOCK is very broad, we could for example
461                  * distinguish between fcntl locks and invalid lock
462                  * sequences. EWOULDBLOCK is wrong, but there is no real
463                  * generic lock error code in errno.h
464                  */
465                 result = EWOULDBLOCK;
466                 break;
467
468         case TDB_ERR_NOLOCK:
469         case TDB_ERR_LOCK_TIMEOUT:
470                 /*
471                  * These two ones in the enum are not actually used
472                  */
473                 result = ENOLCK;
474                 break;
475         case TDB_ERR_NOEXIST:
476                 result = ENOENT;
477                 break;
478         case TDB_ERR_EINVAL:
479                 result = EINVAL;
480                 break;
481         case TDB_ERR_RDONLY:
482                 result = EROFS;
483                 break;
484         case TDB_ERR_NESTING:
485                 /*
486                  * Well, this db is already busy...
487                  */
488                 result = EBUSY;
489                 break;
490         };
491         return result;
492 }
493
494 struct tdb_fetch_talloc_state {
495         TALLOC_CTX *mem_ctx;
496         uint8_t *buf;
497 };
498
499 static int tdb_fetch_talloc_parser(TDB_DATA key, TDB_DATA data,
500                                    void *private_data)
501 {
502         struct tdb_fetch_talloc_state *state = private_data;
503         state->buf = talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
504         return 0;
505 }
506
507 int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
508                      TALLOC_CTX *mem_ctx, uint8_t **buf)
509 {
510         struct tdb_fetch_talloc_state state = { .mem_ctx = mem_ctx };
511         int ret;
512
513         ret = tdb_parse_record(tdb, key, tdb_fetch_talloc_parser, &state);
514         if (ret == -1) {
515                 enum TDB_ERROR err = tdb_error(tdb);
516                 return map_unix_error_from_tdb(err);
517         }
518
519         if (state.buf == NULL) {
520                 return ENOMEM;
521         }
522
523         *buf = state.buf;
524         return 0;
525 }