~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/tdb/tdbutil.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
   Unix SMB/CIFS implementation.
3
 
   tdb utility functions
4
 
   Copyright (C) Andrew Tridgell   1992-1998
5
 
   Copyright (C) Rafal Szczesniak  2002
6
 
   
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.
11
 
   
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.
16
 
   
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.
20
 
*/
21
 
 
22
 
#include "tdb_private.h"
23
 
#include <fnmatch.h>
24
 
 
25
 
/***************************************************************
26
 
 Allow a caller to set a "alarm" flag that tdb can check to abort
27
 
 a blocking lock on SIGALRM.
28
 
***************************************************************/
29
 
 
30
 
static sig_atomic_t *palarm_fired;
31
 
 
32
 
static void tdb_set_lock_alarm(sig_atomic_t *palarm)
33
 
{
34
 
        palarm_fired = palarm;
35
 
}
36
 
 
37
 
/* these are little tdb utility functions that are meant to make
38
 
   dealing with a tdb database a little less cumbersome in Samba */
39
 
 
40
 
static SIG_ATOMIC_T gotalarm;
41
 
 
42
 
/***************************************************************
43
 
 Signal function to tell us we timed out.
44
 
****************************************************************/
45
 
 
46
 
static void gotalarm_sig(void)
47
 
{
48
 
        gotalarm = 1;
49
 
}
50
 
 
51
 
/***************************************************************
52
 
 Make a TDB_DATA and keep the const warning in one place
53
 
****************************************************************/
54
 
 
55
 
TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
56
 
{
57
 
        TDB_DATA ret;
58
 
        ret.dptr = CONST_DISCARD(char *, dptr);
59
 
        ret.dsize = dsize;
60
 
        return ret;
61
 
}
62
 
 
63
 
TDB_DATA string_tdb_data(const char *string)
64
 
{
65
 
        return make_tdb_data(string, strlen(string));
66
 
}
67
 
 
68
 
/****************************************************************************
69
 
 Lock a chain with timeout (in seconds).
70
 
****************************************************************************/
71
 
 
72
 
static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
73
 
{
74
 
        /* Allow tdb_chainlock to be interrupted by an alarm. */
75
 
        int ret;
76
 
        gotalarm = 0;
77
 
        tdb_set_lock_alarm(CONST_DISCARD(sig_atomic_t *, &gotalarm));
78
 
 
79
 
        if (timeout) {
80
 
                CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
81
 
                alarm(timeout);
82
 
        }
83
 
 
84
 
        if (rw_type == F_RDLCK)
85
 
                ret = tdb_chainlock_read(tdb, key);
86
 
        else
87
 
                ret = tdb_chainlock(tdb, key);
88
 
 
89
 
        if (timeout) {
90
 
                alarm(0);
91
 
                CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
92
 
                if (gotalarm) {
93
 
                        DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
94
 
                                timeout, key.dptr, tdb->name ));
95
 
                        /* TODO: If we time out waiting for a lock, it might
96
 
                         * be nice to use F_GETLK to get the pid of the
97
 
                         * process currently holding the lock and print that
98
 
                         * as part of the debugging message. -- mbp */
99
 
                        return -1;
100
 
                }
101
 
        }
102
 
 
103
 
        return ret;
104
 
}
105
 
 
106
 
/****************************************************************************
107
 
 Write lock a chain. Return -1 if timeout or lock failed.
108
 
****************************************************************************/
109
 
 
110
 
int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
111
 
{
112
 
        return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
113
 
}
114
 
 
115
 
/****************************************************************************
116
 
 Lock a chain by string. Return -1 if timeout or lock failed.
117
 
****************************************************************************/
118
 
 
119
 
int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
120
 
{
121
 
        TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
122
 
        
123
 
        return tdb_chainlock(tdb, key);
124
 
}
125
 
 
126
 
int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
127
 
                                   int timeout)
128
 
{
129
 
        TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
130
 
        
131
 
        return tdb_chainlock_with_timeout(tdb, key, timeout);
132
 
}
133
 
 
134
 
/****************************************************************************
135
 
 Unlock a chain by string.
136
 
****************************************************************************/
137
 
 
138
 
void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
139
 
{
140
 
        TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
141
 
 
142
 
        tdb_chainunlock(tdb, key);
143
 
}
144
 
 
145
 
/****************************************************************************
146
 
 Read lock a chain by string. Return -1 if timeout or lock failed.
147
 
****************************************************************************/
148
 
 
149
 
int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
150
 
{
151
 
        TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
152
 
        
153
 
        return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
154
 
}
155
 
 
156
 
/****************************************************************************
157
 
 Read unlock a chain by string.
158
 
****************************************************************************/
159
 
 
160
 
void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
161
 
{
162
 
        TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
163
 
        
164
 
        tdb_chainunlock_read(tdb, key);
165
 
}
166
 
 
167
 
 
168
 
/****************************************************************************
169
 
 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
170
 
 Output is int32 in native byte order.
171
 
****************************************************************************/
172
 
 
173
 
int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
174
 
{
175
 
        TDB_DATA key = make_tdb_data(keyval, len);
176
 
        TDB_DATA data;
177
 
        int32 ret;
178
 
 
179
 
        data = tdb_fetch(tdb, key);
180
 
        if (!data.dptr || data.dsize != sizeof(int32)) {
181
 
                SAFE_FREE(data.dptr);
182
 
                return -1;
183
 
        }
184
 
 
185
 
        ret = IVAL(data.dptr,0);
186
 
        SAFE_FREE(data.dptr);
187
 
        return ret;
188
 
}
189
 
 
190
 
/****************************************************************************
191
 
 Fetch a int32 value by string key, return -1 if not found.
192
 
 Output is int32 in native byte order.
193
 
****************************************************************************/
194
 
 
195
 
int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
196
 
{
197
 
        return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
198
 
}
199
 
 
200
 
/****************************************************************************
201
 
 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
202
 
 Input is int32 in native byte order. Output in tdb is in little-endian.
203
 
****************************************************************************/
204
 
 
205
 
int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
206
 
{
207
 
        TDB_DATA key = make_tdb_data(keystr, len);
208
 
        TDB_DATA data;
209
 
        int32 v_store;
210
 
 
211
 
        SIVAL(&v_store,0,v);
212
 
        data.dptr = (void *)&v_store;
213
 
        data.dsize = sizeof(int32);
214
 
 
215
 
        return tdb_store(tdb, key, data, TDB_REPLACE);
216
 
}
217
 
 
218
 
/****************************************************************************
219
 
 Store a int32 value by string key, return 0 on success, -1 on failure.
220
 
 Input is int32 in native byte order. Output in tdb is in little-endian.
221
 
****************************************************************************/
222
 
 
223
 
int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
224
 
{
225
 
        return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
226
 
}
227
 
 
228
 
/****************************************************************************
229
 
 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
230
 
 Output is uint32 in native byte order.
231
 
****************************************************************************/
232
 
 
233
 
BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
234
 
{
235
 
        TDB_DATA key = make_tdb_data(keyval, len);
236
 
        TDB_DATA data;
237
 
 
238
 
        data = tdb_fetch(tdb, key);
239
 
        if (!data.dptr || data.dsize != sizeof(uint32)) {
240
 
                SAFE_FREE(data.dptr);
241
 
                return False;
242
 
        }
243
 
 
244
 
        *value = IVAL(data.dptr,0);
245
 
        SAFE_FREE(data.dptr);
246
 
        return True;
247
 
}
248
 
 
249
 
/****************************************************************************
250
 
 Fetch a uint32 value by string key, return -1 if not found.
251
 
 Output is uint32 in native byte order.
252
 
****************************************************************************/
253
 
 
254
 
BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
255
 
{
256
 
        return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
257
 
}
258
 
 
259
 
/****************************************************************************
260
 
 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
261
 
 Input is uint32 in native byte order. Output in tdb is in little-endian.
262
 
****************************************************************************/
263
 
 
264
 
BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
265
 
{
266
 
        TDB_DATA key = make_tdb_data(keystr, len);
267
 
        TDB_DATA data;
268
 
        uint32 v_store;
269
 
        BOOL ret = True;
270
 
 
271
 
        SIVAL(&v_store, 0, value);
272
 
        data.dptr = (void *)&v_store;
273
 
        data.dsize = sizeof(uint32);
274
 
 
275
 
        if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
276
 
                ret = False;
277
 
 
278
 
        return ret;
279
 
}
280
 
 
281
 
/****************************************************************************
282
 
 Store a uint32 value by string key, return 0 on success, -1 on failure.
283
 
 Input is uint32 in native byte order. Output in tdb is in little-endian.
284
 
****************************************************************************/
285
 
 
286
 
BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
287
 
{
288
 
        return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
289
 
}
290
 
/****************************************************************************
291
 
 Store a buffer by a null terminated string key.  Return 0 on success, -1
292
 
 on failure.
293
 
****************************************************************************/
294
 
 
295
 
int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
296
 
{
297
 
        TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
298
 
        
299
 
        return tdb_store(tdb, key, data, flags);
300
 
}
301
 
 
302
 
/****************************************************************************
303
 
 Fetch a buffer using a null terminated string key.  Don't forget to call
304
 
 free() on the result dptr.
305
 
****************************************************************************/
306
 
 
307
 
TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
308
 
{
309
 
        TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
310
 
 
311
 
        return tdb_fetch(tdb, key);
312
 
}
313
 
 
314
 
/****************************************************************************
315
 
 Delete an entry using a null terminated string key. 
316
 
****************************************************************************/
317
 
 
318
 
int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
319
 
{
320
 
        TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
321
 
 
322
 
        return tdb_delete(tdb, key);
323
 
}
324
 
 
325
 
/****************************************************************************
326
 
 Atomic integer change. Returns old value. To create, set initial value in *oldval. 
327
 
****************************************************************************/
328
 
 
329
 
int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
330
 
{
331
 
        int32 val;
332
 
        int32 ret = -1;
333
 
 
334
 
        if (tdb_lock_bystring(tdb, keystr) == -1)
335
 
                return -1;
336
 
 
337
 
        if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
338
 
                /* The lookup failed */
339
 
                if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
340
 
                        /* but not because it didn't exist */
341
 
                        goto err_out;
342
 
                }
343
 
                
344
 
                /* Start with 'old' value */
345
 
                val = *oldval;
346
 
 
347
 
        } else {
348
 
                /* It worked, set return value (oldval) to tdb data */
349
 
                *oldval = val;
350
 
        }
351
 
 
352
 
        /* Increment value for storage and return next time */
353
 
        val += change_val;
354
 
                
355
 
        if (tdb_store_int32(tdb, keystr, val) == -1)
356
 
                goto err_out;
357
 
 
358
 
        ret = 0;
359
 
 
360
 
  err_out:
361
 
 
362
 
        tdb_unlock_bystring(tdb, keystr);
363
 
        return ret;
364
 
}
365
 
 
366
 
/****************************************************************************
367
 
 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
368
 
****************************************************************************/
369
 
 
370
 
BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
371
 
{
372
 
        uint32 val;
373
 
        BOOL ret = False;
374
 
 
375
 
        if (tdb_lock_bystring(tdb, keystr) == -1)
376
 
                return False;
377
 
 
378
 
        if (!tdb_fetch_uint32(tdb, keystr, &val)) {
379
 
                /* It failed */
380
 
                if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
381
 
                        /* and not because it didn't exist */
382
 
                        goto err_out;
383
 
                }
384
 
 
385
 
                /* Start with 'old' value */
386
 
                val = *oldval;
387
 
 
388
 
        } else {
389
 
                /* it worked, set return value (oldval) to tdb data */
390
 
                *oldval = val;
391
 
 
392
 
        }
393
 
 
394
 
        /* get a new value to store */
395
 
        val += change_val;
396
 
                
397
 
        if (!tdb_store_uint32(tdb, keystr, val))
398
 
                goto err_out;
399
 
 
400
 
        ret = True;
401
 
 
402
 
  err_out:
403
 
 
404
 
        tdb_unlock_bystring(tdb, keystr);
405
 
        return ret;
406
 
}
407
 
 
408
 
/****************************************************************************
409
 
 Useful pair of routines for packing/unpacking data consisting of
410
 
 integers and strings.
411
 
****************************************************************************/
412
 
 
413
 
size_t tdb_pack_va(char *buf, int bufsize, const char *fmt, va_list ap)
414
 
{
415
 
        uint8 bt;
416
 
        uint16 w;
417
 
        uint32 d;
418
 
        int i;
419
 
        void *p;
420
 
        int len;
421
 
        char *s;
422
 
        char c;
423
 
        char *buf0 = buf;
424
 
        const char *fmt0 = fmt;
425
 
        int bufsize0 = bufsize;
426
 
 
427
 
        while (*fmt) {
428
 
                switch ((c = *fmt++)) {
429
 
                case 'b': /* unsigned 8-bit integer */
430
 
                        len = 1;
431
 
                        bt = (uint8)va_arg(ap, int);
432
 
                        if (bufsize && bufsize >= len)
433
 
                                SSVAL(buf, 0, bt);
434
 
                        break;
435
 
                case 'w': /* unsigned 16-bit integer */
436
 
                        len = 2;
437
 
                        w = (uint16)va_arg(ap, int);
438
 
                        if (bufsize && bufsize >= len)
439
 
                                SSVAL(buf, 0, w);
440
 
                        break;
441
 
                case 'd': /* signed 32-bit integer (standard int in most systems) */
442
 
                        len = 4;
443
 
                        d = va_arg(ap, uint32);
444
 
                        if (bufsize && bufsize >= len)
445
 
                                SIVAL(buf, 0, d);
446
 
                        break;
447
 
                case 'p': /* pointer */
448
 
                        len = 4;
449
 
                        p = va_arg(ap, void *);
450
 
                        d = p?1:0;
451
 
                        if (bufsize && bufsize >= len)
452
 
                                SIVAL(buf, 0, d);
453
 
                        break;
454
 
                case 'P': /* null-terminated string */
455
 
                        s = va_arg(ap,char *);
456
 
                        w = strlen(s);
457
 
                        len = w + 1;
458
 
                        if (bufsize && bufsize >= len)
459
 
                                memcpy(buf, s, len);
460
 
                        break;
461
 
                case 'f': /* null-terminated string */
462
 
                        s = va_arg(ap,char *);
463
 
                        w = strlen(s);
464
 
                        len = w + 1;
465
 
                        if (bufsize && bufsize >= len)
466
 
                                memcpy(buf, s, len);
467
 
                        break;
468
 
                case 'B': /* fixed-length string */
469
 
                        i = va_arg(ap, int);
470
 
                        s = va_arg(ap, char *);
471
 
                        len = 4+i;
472
 
                        if (bufsize && bufsize >= len) {
473
 
                                SIVAL(buf, 0, i);
474
 
                                memcpy(buf+4, s, i);
475
 
                        }
476
 
                        break;
477
 
                default:
478
 
                        DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
479
 
                                 c, fmt));
480
 
                        len = 0;
481
 
                        break;
482
 
                }
483
 
 
484
 
                buf += len;
485
 
                if (bufsize)
486
 
                        bufsize -= len;
487
 
                if (bufsize < 0)
488
 
                        bufsize = 0;
489
 
        }
490
 
 
491
 
        DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
492
 
                 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
493
 
        
494
 
        return PTR_DIFF(buf, buf0);
495
 
}
496
 
 
497
 
size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
498
 
{
499
 
        va_list ap;
500
 
        size_t result;
501
 
 
502
 
        va_start(ap, fmt);
503
 
        result = tdb_pack_va(buf, bufsize, fmt, ap);
504
 
        va_end(ap);
505
 
        return result;
506
 
}
507
 
 
508
 
BOOL tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
509
 
                     const char *fmt, ...)
510
 
{
511
 
        va_list ap;
512
 
        size_t len1, len2;
513
 
 
514
 
        va_start(ap, fmt);
515
 
        len1 = tdb_pack_va(NULL, 0, fmt, ap);
516
 
        va_end(ap);
517
 
 
518
 
        if (mem_ctx != NULL) {
519
 
                *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
520
 
                                            (*len) + len1);
521
 
        } else {
522
 
                *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
523
 
        }
524
 
 
525
 
        if (*buf == NULL) {
526
 
                return False;
527
 
        }
528
 
 
529
 
        va_start(ap, fmt);
530
 
        len2 = tdb_pack_va((char *)(*buf)+(*len), len1, fmt, ap);
531
 
        va_end(ap);
532
 
 
533
 
        if (len1 != len2) {
534
 
                return False;
535
 
        }
536
 
 
537
 
        *len += len2;
538
 
 
539
 
        return True;
540
 
}
541
 
 
542
 
/****************************************************************************
543
 
 Useful pair of routines for packing/unpacking data consisting of
544
 
 integers and strings.
545
 
****************************************************************************/
546
 
 
547
 
int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
548
 
{
549
 
        va_list ap;
550
 
        uint8 *bt;
551
 
        uint16 *w;
552
 
        uint32 *d;
553
 
        int len;
554
 
        int *i;
555
 
        void **p;
556
 
        char *s, **b;
557
 
        char c;
558
 
        char *buf0 = buf;
559
 
        const char *fmt0 = fmt;
560
 
        int bufsize0 = bufsize;
561
 
 
562
 
        va_start(ap, fmt);
563
 
        
564
 
        while (*fmt) {
565
 
                switch ((c=*fmt++)) {
566
 
                case 'b':
567
 
                        len = 1;
568
 
                        bt = va_arg(ap, uint8 *);
569
 
                        if (bufsize < len)
570
 
                                goto no_space;
571
 
                        *bt = SVAL(buf, 0);
572
 
                        break;
573
 
                case 'w':
574
 
                        len = 2;
575
 
                        w = va_arg(ap, uint16 *);
576
 
                        if (bufsize < len)
577
 
                                goto no_space;
578
 
                        *w = SVAL(buf, 0);
579
 
                        break;
580
 
                case 'd':
581
 
                        len = 4;
582
 
                        d = va_arg(ap, uint32 *);
583
 
                        if (bufsize < len)
584
 
                                goto no_space;
585
 
                        *d = IVAL(buf, 0);
586
 
                        break;
587
 
                case 'p':
588
 
                        len = 4;
589
 
                        p = va_arg(ap, void **);
590
 
                        if (bufsize < len)
591
 
                                goto no_space;
592
 
                        /* 
593
 
                         * This isn't a real pointer - only a token (1 or 0)
594
 
                         * to mark the fact a pointer is present.
595
 
                         */
596
 
 
597
 
                        *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
598
 
                        break;
599
 
                case 'P':
600
 
                        s = va_arg(ap,char *);
601
 
                        len = strlen(buf) + 1;
602
 
                        if (bufsize < len || len > sizeof(pstring))
603
 
                                goto no_space;
604
 
                        memcpy(s, buf, len);
605
 
                        break;
606
 
                case 'f':
607
 
                        s = va_arg(ap,char *);
608
 
                        len = strlen(buf) + 1;
609
 
                        if (bufsize < len || len > sizeof(fstring))
610
 
                                goto no_space;
611
 
                        memcpy(s, buf, len);
612
 
                        break;
613
 
                case 'B':
614
 
                        i = va_arg(ap, int *);
615
 
                        b = va_arg(ap, char **);
616
 
                        len = 4;
617
 
                        if (bufsize < len)
618
 
                                goto no_space;
619
 
                        *i = IVAL(buf, 0);
620
 
                        if (! *i) {
621
 
                                *b = NULL;
622
 
                                break;
623
 
                        }
624
 
                        len += *i;
625
 
                        if (bufsize < len)
626
 
                                goto no_space;
627
 
                        *b = (char *)SMB_MALLOC(*i);
628
 
                        if (! *b)
629
 
                                goto no_space;
630
 
                        memcpy(*b, buf+4, *i);
631
 
                        break;
632
 
                default:
633
 
                        DEBUG(0,("Unknown tdb_unpack format %c in %s\n", 
634
 
                                 c, fmt));
635
 
 
636
 
                        len = 0;
637
 
                        break;
638
 
                }
639
 
 
640
 
                buf += len;
641
 
                bufsize -= len;
642
 
        }
643
 
 
644
 
        va_end(ap);
645
 
 
646
 
        DEBUG(18,("tdb_unpack(%s, %d) -> %d\n", 
647
 
                 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
648
 
 
649
 
        return PTR_DIFF(buf, buf0);
650
 
 
651
 
 no_space:
652
 
        return -1;
653
 
}
654
 
 
655
 
 
656
 
/****************************************************************************
657
 
 Log tdb messages via DEBUG().
658
 
****************************************************************************/
659
 
 
660
 
static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
661
 
{
662
 
        va_list ap;
663
 
        char *ptr = NULL;
664
 
 
665
 
        va_start(ap, format);
666
 
        vasprintf(&ptr, format, ap);
667
 
        va_end(ap);
668
 
        
669
 
        if (!ptr || !*ptr)
670
 
                return;
671
 
 
672
 
        DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
673
 
        SAFE_FREE(ptr);
674
 
}
675
 
 
676
 
/****************************************************************************
677
 
 Like tdb_open() but also setup a logging function that redirects to
678
 
 the samba DEBUG() system.
679
 
****************************************************************************/
680
 
 
681
 
TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
682
 
                          int open_flags, mode_t mode)
683
 
{
684
 
        TDB_CONTEXT *tdb;
685
 
 
686
 
        if (!lp_use_mmap())
687
 
                tdb_flags |= TDB_NOMMAP;
688
 
 
689
 
        tdb = tdb_open_ex(name, hash_size, tdb_flags, 
690
 
                                    open_flags, mode, tdb_log, NULL);
691
 
        if (!tdb)
692
 
                return NULL;
693
 
 
694
 
        return tdb;
695
 
}
696
 
 
697
 
/****************************************************************************
698
 
 Allow tdb_delete to be used as a tdb_traversal_fn.
699
 
****************************************************************************/
700
 
 
701
 
int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
702
 
                     void *state)
703
 
{
704
 
    return tdb_delete(the_tdb, key);
705
 
}
706
 
 
707
 
 
708
 
 
709
 
/**
710
 
 * Search across the whole tdb for keys that match the given pattern
711
 
 * return the result as a list of keys
712
 
 *
713
 
 * @param tdb pointer to opened tdb file context
714
 
 * @param pattern searching pattern used by fnmatch(3) functions
715
 
 *
716
 
 * @return list of keys found by looking up with given pattern
717
 
 **/
718
 
TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
719
 
{
720
 
        TDB_DATA key, next;
721
 
        TDB_LIST_NODE *list = NULL;
722
 
        TDB_LIST_NODE *rec = NULL;
723
 
        TDB_LIST_NODE *tmp = NULL;
724
 
        
725
 
        for (key = tdb_firstkey(tdb); key.dptr; key = next) {
726
 
                /* duplicate key string to ensure null-termination */
727
 
                char *key_str = (char*) SMB_STRNDUP(key.dptr, key.dsize);
728
 
                if (!key_str) {
729
 
                        DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
730
 
                        smb_panic("strndup failed!\n");
731
 
                }
732
 
                
733
 
                DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
734
 
                
735
 
                next = tdb_nextkey(tdb, key);
736
 
 
737
 
                /* do the pattern checking */
738
 
                if (fnmatch(pattern, key_str, 0) == 0) {
739
 
                        rec = SMB_MALLOC_P(TDB_LIST_NODE);
740
 
                        ZERO_STRUCTP(rec);
741
 
 
742
 
                        rec->node_key = key;
743
 
        
744
 
                        DLIST_ADD_END(list, rec, tmp);
745
 
                
746
 
                        DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
747
 
                } else {
748
 
                        free(key.dptr);
749
 
                }
750
 
                
751
 
                /* free duplicated key string */
752
 
                free(key_str);
753
 
        }
754
 
        
755
 
        return list;
756
 
 
757
 
}
758
 
 
759
 
 
760
 
/**
761
 
 * Free the list returned by tdb_search_keys
762
 
 *
763
 
 * @param node list of results found by tdb_search_keys
764
 
 **/
765
 
void tdb_search_list_free(TDB_LIST_NODE* node)
766
 
{
767
 
        TDB_LIST_NODE *next_node;
768
 
        
769
 
        while (node) {
770
 
                next_node = node->next;
771
 
                SAFE_FREE(node->node_key.dptr);
772
 
                SAFE_FREE(node);
773
 
                node = next_node;
774
 
        };
775
 
}
776
 
 
777
 
size_t tdb_map_size(struct tdb_context *tdb)
778
 
{
779
 
        return tdb->map_size;
780
 
}
781
 
 
782
 
int tdb_get_flags(struct tdb_context *tdb)
783
 
{
784
 
        return tdb->flags;
785
 
}
786
 
 
787
 
/****************************************************************************
788
 
 tdb_store, wrapped in a transaction. This way we make sure that a process
789
 
 that dies within writing does not leave a corrupt tdb behind.
790
 
****************************************************************************/
791
 
 
792
 
int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
793
 
                    int flag)
794
 
{
795
 
        int res;
796
 
 
797
 
        if ((res = tdb_transaction_start(tdb)) != 0) {
798
 
                DEBUG(5, ("tdb_transaction_start failed\n"));
799
 
                return res;
800
 
        }
801
 
 
802
 
        if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
803
 
                DEBUG(10, ("tdb_store failed\n"));
804
 
                if (tdb_transaction_cancel(tdb) != 0) {
805
 
                        smb_panic("Cancelling transaction failed\n");
806
 
                }
807
 
                return res;
808
 
        }
809
 
 
810
 
        if ((res = tdb_transaction_commit(tdb)) != 0) {
811
 
                DEBUG(5, ("tdb_transaction_commit failed\n"));
812
 
        }
813
 
 
814
 
        return res;
815
 
}