r17930: Merge noinclude branch:
[jelmer/samba4-debian.git] / source / lib / tdb / common / tdbutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Andrew Tridgell 1992-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   NOTE: these utility functions are specific to Samba, and are not part
24   of the core tdb code
25 */
26
27 #include "includes.h"
28 #include "lib/tdb/include/tdbutil.h"
29 #include "system/glob.h"
30 #include "system/wait.h"
31 #include "system/filesys.h"
32 #include "lib/util/dlinklist.h"
33 #include "pstring.h"
34
35 /* these are little tdb utility functions that are meant to make
36    dealing with a tdb database a little less cumbersome in Samba */
37
38 /***************************************************************
39  Make a TDB_DATA and keep the const warning in one place
40 ****************************************************************/
41
42 static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
43 {
44         TDB_DATA ret;
45         ret.dptr = discard_const_p(unsigned char, dptr);
46         ret.dsize = dsize;
47         return ret;
48 }
49
50 /****************************************************************************
51  Lock a chain by string. Return -1 if lock failed.
52 ****************************************************************************/
53
54 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
55 {
56         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
57         
58         return tdb_chainlock(tdb, key);
59 }
60
61 /****************************************************************************
62  Unlock a chain by string.
63 ****************************************************************************/
64
65 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
66 {
67         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
68
69         tdb_chainunlock(tdb, key);
70 }
71
72 /****************************************************************************
73  Read lock a chain by string. Return -1 if lock failed.
74 ****************************************************************************/
75
76 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
77 {
78         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
79         
80         return tdb_chainlock_read(tdb, key);
81 }
82
83 /****************************************************************************
84  Read unlock a chain by string.
85 ****************************************************************************/
86
87 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
88 {
89         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
90         
91         tdb_chainunlock_read(tdb, key);
92 }
93
94
95 /****************************************************************************
96  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
97  Output is int32_t in native byte order.
98 ****************************************************************************/
99
100 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size_t len)
101 {
102         TDB_DATA key = make_tdb_data(keyval, len);
103         TDB_DATA data;
104         int32_t ret;
105
106         data = tdb_fetch(tdb, key);
107         if (!data.dptr || data.dsize != sizeof(int32_t)) {
108                 SAFE_FREE(data.dptr);
109                 return -1;
110         }
111
112         ret = IVAL(data.dptr,0);
113         SAFE_FREE(data.dptr);
114         return ret;
115 }
116
117 /****************************************************************************
118  Fetch a int32_t value by string key, return -1 if not found.
119  Output is int32_t in native byte order.
120 ****************************************************************************/
121
122 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
123 {
124         return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
125 }
126
127 /****************************************************************************
128  Store a int32_t value by an arbitary blob key, return 0 on success, -1 on failure.
129  Input is int32_t in native byte order. Output in tdb is in little-endian.
130 ****************************************************************************/
131
132 int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, int32_t v)
133 {
134         TDB_DATA key = make_tdb_data(keystr, len);
135         TDB_DATA data;
136         int32_t v_store;
137
138         SIVAL(&v_store,0,v);
139         data.dptr = (void *)&v_store;
140         data.dsize = sizeof(int32_t);
141
142         return tdb_store(tdb, key, data, TDB_REPLACE);
143 }
144
145 /****************************************************************************
146  Store a int32_t value by string key, return 0 on success, -1 on failure.
147  Input is int32_t in native byte order. Output in tdb is in little-endian.
148 ****************************************************************************/
149
150 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
151 {
152         return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
153 }
154
155 /****************************************************************************
156  Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
157  Output is uint32_t in native byte order.
158 ****************************************************************************/
159
160 BOOL tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t len, uint32_t *value)
161 {
162         TDB_DATA key = make_tdb_data(keyval, len);
163         TDB_DATA data;
164
165         data = tdb_fetch(tdb, key);
166         if (!data.dptr || data.dsize != sizeof(uint32_t)) {
167                 SAFE_FREE(data.dptr);
168                 return False;
169         }
170
171         *value = IVAL(data.dptr,0);
172         SAFE_FREE(data.dptr);
173         return True;
174 }
175
176 /****************************************************************************
177  Fetch a uint32_t value by string key, return -1 if not found.
178  Output is uint32_t in native byte order.
179 ****************************************************************************/
180
181 BOOL tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
182 {
183         return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
184 }
185
186 /****************************************************************************
187  Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
188  Input is uint32_t in native byte order. Output in tdb is in little-endian.
189 ****************************************************************************/
190
191 BOOL tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, uint32_t value)
192 {
193         TDB_DATA key = make_tdb_data(keystr, len);
194         TDB_DATA data;
195         uint32_t v_store;
196         BOOL ret = True;
197
198         SIVAL(&v_store, 0, value);
199         data.dptr = (void *)&v_store;
200         data.dsize = sizeof(uint32_t);
201
202         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
203                 ret = False;
204
205         return ret;
206 }
207
208 /****************************************************************************
209  Store a uint32_t value by string key, return 0 on success, -1 on failure.
210  Input is uint32_t in native byte order. Output in tdb is in little-endian.
211 ****************************************************************************/
212
213 BOOL tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
214 {
215         return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
216 }
217 /****************************************************************************
218  Store a buffer by a null terminated string key.  Return 0 on success, -1
219  on failure.
220 ****************************************************************************/
221
222 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
223 {
224         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
225         
226         return tdb_store(tdb, key, data, flags);
227 }
228
229 /****************************************************************************
230  Fetch a buffer using a null terminated string key.  Don't forget to call
231  free() on the result dptr.
232 ****************************************************************************/
233
234 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
235 {
236         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
237
238         return tdb_fetch(tdb, key);
239 }
240
241 /****************************************************************************
242  Delete an entry using a null terminated string key. 
243 ****************************************************************************/
244
245 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
246 {
247         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
248
249         return tdb_delete(tdb, key);
250 }
251
252 /****************************************************************************
253  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
254 ****************************************************************************/
255
256 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
257 {
258         int32_t val;
259         int32_t ret = -1;
260
261         if (tdb_lock_bystring(tdb, keystr) == -1)
262                 return -1;
263
264         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
265                 /* The lookup failed */
266                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
267                         /* but not because it didn't exist */
268                         goto err_out;
269                 }
270                 
271                 /* Start with 'old' value */
272                 val = *oldval;
273
274         } else {
275                 /* It worked, set return value (oldval) to tdb data */
276                 *oldval = val;
277         }
278
279         /* Increment value for storage and return next time */
280         val += change_val;
281                 
282         if (tdb_store_int32(tdb, keystr, val) == -1)
283                 goto err_out;
284
285         ret = 0;
286
287   err_out:
288
289         tdb_unlock_bystring(tdb, keystr);
290         return ret;
291 }
292
293 /****************************************************************************
294  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
295 ****************************************************************************/
296
297 BOOL tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
298 {
299         uint32_t val;
300         BOOL ret = False;
301
302         if (tdb_lock_bystring(tdb, keystr) == -1)
303                 return False;
304
305         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
306                 /* It failed */
307                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
308                         /* and not because it didn't exist */
309                         goto err_out;
310                 }
311
312                 /* Start with 'old' value */
313                 val = *oldval;
314
315         } else {
316                 /* it worked, set return value (oldval) to tdb data */
317                 *oldval = val;
318
319         }
320
321         /* get a new value to store */
322         val += change_val;
323                 
324         if (!tdb_store_uint32(tdb, keystr, val))
325                 goto err_out;
326
327         ret = True;
328
329   err_out:
330
331         tdb_unlock_bystring(tdb, keystr);
332         return ret;
333 }
334
335 /****************************************************************************
336  Allow tdb_delete to be used as a tdb_traversal_fn.
337 ****************************************************************************/
338
339 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
340                      void *state)
341 {
342     return tdb_delete(the_tdb, key);
343 }
344
345
346
347 /****************************************************************************
348  Useful pair of routines for packing/unpacking data consisting of
349  integers and strings.
350 ****************************************************************************/
351
352 size_t tdb_pack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
353 {
354         va_list ap;
355         uint8_t bt;
356         uint16_t w;
357         uint32_t d;
358         int i;
359         void *p;
360         int len;
361         char *s;
362         char c;
363         char *buf0 = buf;
364         const char *fmt0 = fmt;
365         int bufsize0 = bufsize;
366         tdb_log_func log_fn = tdb_log_fn(tdb);
367
368         va_start(ap, fmt);
369
370         while (*fmt) {
371                 switch ((c = *fmt++)) {
372                 case 'b': /* unsigned 8-bit integer */
373                         len = 1;
374                         bt = (uint8_t)va_arg(ap, int);
375                         if (bufsize && bufsize >= len)
376                                 SSVAL(buf, 0, bt);
377                         break;
378                 case 'w': /* unsigned 16-bit integer */
379                         len = 2;
380                         w = (uint16_t)va_arg(ap, int);
381                         if (bufsize && bufsize >= len)
382                                 SSVAL(buf, 0, w);
383                         break;
384                 case 'd': /* signed 32-bit integer (standard int in most systems) */
385                         len = 4;
386                         d = va_arg(ap, uint32_t);
387                         if (bufsize && bufsize >= len)
388                                 SIVAL(buf, 0, d);
389                         break;
390                 case 'p': /* pointer */
391                         len = 4;
392                         p = va_arg(ap, void *);
393                         d = p?1:0;
394                         if (bufsize && bufsize >= len)
395                                 SIVAL(buf, 0, d);
396                         break;
397                 case 'P': /* null-terminated string */
398                         s = va_arg(ap,char *);
399                         w = strlen(s);
400                         len = w + 1;
401                         if (bufsize && bufsize >= len)
402                                 memcpy(buf, s, len);
403                         break;
404                 case 'f': /* null-terminated string */
405                         s = va_arg(ap,char *);
406                         w = strlen(s);
407                         len = w + 1;
408                         if (bufsize && bufsize >= len)
409                                 memcpy(buf, s, len);
410                         break;
411                 case 'B': /* fixed-length string */
412                         i = va_arg(ap, int);
413                         s = va_arg(ap, char *);
414                         len = 4+i;
415                         if (bufsize && bufsize >= len) {
416                                 SIVAL(buf, 0, i);
417                                 memcpy(buf+4, s, i);
418                         }
419                         break;
420                 default:
421                         log_fn(tdb, 0,"Unknown tdb_pack format %c in %s\n", 
422                                c, fmt);
423                         len = 0;
424                         break;
425                 }
426
427                 buf += len;
428                 if (bufsize)
429                         bufsize -= len;
430                 if (bufsize < 0)
431                         bufsize = 0;
432         }
433
434         va_end(ap);
435
436         log_fn(tdb, 18,"tdb_pack(%s, %d) -> %d\n", 
437                fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
438         
439         return PTR_DIFF(buf, buf0);
440 }
441
442 /****************************************************************************
443  Useful pair of routines for packing/unpacking data consisting of
444  integers and strings.
445 ****************************************************************************/
446
447 int tdb_unpack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
448 {
449         va_list ap;
450         uint8_t *bt;
451         uint16_t *w;
452         uint32_t *d;
453         int len;
454         int *i;
455         void **p;
456         char *s, **b;
457         char c;
458         char *buf0 = buf;
459         const char *fmt0 = fmt;
460         int bufsize0 = bufsize;
461         tdb_log_func log_fn = tdb_log_fn(tdb);
462
463         va_start(ap, fmt);
464         
465         while (*fmt) {
466                 switch ((c=*fmt++)) {
467                 case 'b':
468                         len = 1;
469                         bt = va_arg(ap, uint8_t *);
470                         if (bufsize < len)
471                                 goto no_space;
472                         *bt = SVAL(buf, 0);
473                         break;
474                 case 'w':
475                         len = 2;
476                         w = va_arg(ap, uint16_t *);
477                         if (bufsize < len)
478                                 goto no_space;
479                         *w = SVAL(buf, 0);
480                         break;
481                 case 'd':
482                         len = 4;
483                         d = va_arg(ap, uint32_t *);
484                         if (bufsize < len)
485                                 goto no_space;
486                         *d = IVAL(buf, 0);
487                         break;
488                 case 'p':
489                         len = 4;
490                         p = va_arg(ap, void **);
491                         if (bufsize < len)
492                                 goto no_space;
493                         *p = (void *)IVAL(buf, 0);
494                         break;
495                 case 'P':
496                         s = va_arg(ap,char *);
497                         len = strlen(buf) + 1;
498                         if (bufsize < len || len > sizeof(pstring))
499                                 goto no_space;
500                         memcpy(s, buf, len);
501                         break;
502                 case 'f':
503                         s = va_arg(ap,char *);
504                         len = strlen(buf) + 1;
505                         if (bufsize < len || len > sizeof(fstring))
506                                 goto no_space;
507                         memcpy(s, buf, len);
508                         break;
509                 case 'B':
510                         i = va_arg(ap, int *);
511                         b = va_arg(ap, char **);
512                         len = 4;
513                         if (bufsize < len)
514                                 goto no_space;
515                         *i = IVAL(buf, 0);
516                         if (! *i) {
517                                 *b = NULL;
518                                 break;
519                         }
520                         len += *i;
521                         if (bufsize < len)
522                                 goto no_space;
523                         *b = (char *)malloc(*i);
524                         if (! *b)
525                                 goto no_space;
526                         memcpy(*b, buf+4, *i);
527                         break;
528                 default:
529                         log_fn(tdb, 0, "Unknown tdb_unpack format %c in %s\n", 
530                                c, fmt);
531
532                         len = 0;
533                         break;
534                 }
535
536                 buf += len;
537                 bufsize -= len;
538         }
539
540         va_end(ap);
541
542         log_fn(tdb, 18, "tdb_unpack(%s, %d) -> %d\n", 
543                fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
544
545         return PTR_DIFF(buf, buf0);
546
547  no_space:
548         return -1;
549 }