2698e3cc712ae18d98674475119c8bc3ba4f16d1
[jelmer/samba4-debian.git] / source / lib / tdb_helper.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include <fnmatch.h>
23
24 /* these are little tdb utility functions that are meant to make
25    dealing with a tdb database a little less cumbersome in Samba */
26
27
28 /****************************************************************************
29  Log tdb messages via DEBUG().
30 ****************************************************************************/
31
32 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
33
34 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
35 {
36         va_list ap;
37         char *ptr = NULL;
38
39         va_start(ap, format);
40         vasprintf(&ptr, format, ap);
41         va_end(ap);
42         
43         if (!ptr || !*ptr)
44                 return;
45
46         DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
47         SAFE_FREE(ptr);
48 }
49
50 /****************************************************************************
51  Like tdb_open() but also setup a logging function that redirects to
52  the samba DEBUG() system.
53 ****************************************************************************/
54
55 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
56                           int open_flags, mode_t mode)
57 {
58         TDB_CONTEXT *tdb;
59
60         if (!lp_use_mmap())
61                 tdb_flags |= TDB_NOMMAP;
62
63         tdb = tdb_open_ex(name, hash_size, tdb_flags, 
64                                     open_flags, mode, tdb_log, NULL);
65         if (!tdb)
66                 return NULL;
67
68         return tdb;
69 }