util: util_ntdb.c
[ddiss/samba.git] / lib / util / util_ntdb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    ntdb utility functions
5
6    Copyright (C) Rusty Russell 2012
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21 #include "includes.h"
22 #include "util_ntdb.h"
23 #include "lib/param/param.h"
24
25 static void *ntdb_talloc(const void *owner, size_t len, void *priv_data)
26 {
27         return talloc_size(owner, len);
28 }
29
30 static void *ntdb_expand(void *old, size_t newlen, void *priv_data)
31 {
32         return talloc_realloc_size(NULL, old, newlen);
33 }
34
35 static void ntdb_free(void *old, void *priv_data)
36 {
37         talloc_free(old);
38 }
39
40 static int ntdb_destroy(struct ntdb_context *ntdb)
41 {
42         ntdb_close(ntdb);
43         return 0;
44 }
45
46 static void ntdb_log(struct ntdb_context *ntdb,
47                      enum ntdb_log_level level,
48                      enum NTDB_ERROR ecode,
49                      const char *message,
50                      void *unused)
51 {
52         int dl;
53         const char *name = ntdb_name(ntdb);
54
55         switch (level) {
56         case NTDB_LOG_USE_ERROR:
57         case NTDB_LOG_ERROR:
58                 dl = 0;
59                 break;
60         case NTDB_LOG_WARNING:
61                 dl = 2;
62                 break;
63         default:
64                 dl = 0;
65         }
66
67         DEBUG(dl, ("ntdb(%s):%s: %s\n", name ? name : "unnamed",
68                    ntdb_errorstr(ecode), message));
69 }
70
71 struct ntdb_context *ntdb_new(TALLOC_CTX *ctx,
72                               const char *name, int ntdb_flags,
73                               int open_flags, mode_t mode,
74                               union ntdb_attribute *attr,
75                               struct loadparm_context *lp_ctx)
76 {
77         union ntdb_attribute log_attr, alloc_attr;
78         struct ntdb_context *ntdb;
79
80         if (lp_ctx && !lpcfg_use_mmap(lp_ctx)) {
81                 ntdb_flags |= NTDB_NOMMAP;
82         }
83
84         /* Great hack for speeding testing! */
85         if (getenv("TDB_NO_FSYNC")) {
86                 ntdb_flags |= NTDB_NOSYNC;
87         }
88
89         log_attr.base.next = attr;
90         log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
91         log_attr.log.fn = ntdb_log;
92
93         alloc_attr.base.next = &log_attr;
94         alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
95         alloc_attr.alloc.alloc = ntdb_talloc;
96         alloc_attr.alloc.expand = ntdb_expand;
97         alloc_attr.alloc.free = ntdb_free;
98
99         ntdb = ntdb_open(name, ntdb_flags, open_flags, mode, &alloc_attr);
100         if (!ntdb) {
101                 return NULL;
102         }
103
104         /* We can re-use the tdb's path name for the talloc name */
105         name = ntdb_name(ntdb);
106         if (name) {
107                 talloc_set_name_const(ntdb, name);
108         } else {
109                 talloc_set_name_const(ntdb, "unnamed ntdb");
110         }
111         talloc_set_destructor(ntdb, ntdb_destroy);
112
113         return talloc_steal(ctx, ntdb);
114 }