2 Unix SMB/CIFS implementation.
6 Copyright (C) Rusty Russell 2012
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.
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.
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/>.
22 #include "util_ntdb.h"
23 #include "lib/param/param.h"
25 static void *ntdb_talloc(const void *owner, size_t len, void *priv_data)
27 return talloc_size(owner, len);
30 static void *ntdb_expand(void *old, size_t newlen, void *priv_data)
32 return talloc_realloc_size(NULL, old, newlen);
35 static void ntdb_free(void *old, void *priv_data)
40 static int ntdb_destroy(struct ntdb_context *ntdb)
46 static void ntdb_log(struct ntdb_context *ntdb,
47 enum ntdb_log_level level,
48 enum NTDB_ERROR ecode,
53 const char *name = ntdb_name(ntdb);
56 case NTDB_LOG_USE_ERROR:
60 case NTDB_LOG_WARNING:
67 DEBUG(dl, ("ntdb(%s):%s: %s\n", name ? name : "unnamed",
68 ntdb_errorstr(ecode), message));
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)
77 union ntdb_attribute log_attr, alloc_attr;
78 struct ntdb_context *ntdb;
80 if (lp_ctx && !lpcfg_use_mmap(lp_ctx)) {
81 ntdb_flags |= NTDB_NOMMAP;
84 /* Great hack for speeding testing! */
85 if (getenv("TDB_NO_FSYNC")) {
86 ntdb_flags |= NTDB_NOSYNC;
89 log_attr.base.next = attr;
90 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
91 log_attr.log.fn = ntdb_log;
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;
99 ntdb = ntdb_open(name, ntdb_flags, open_flags, mode, &alloc_attr);
104 /* We can re-use the tdb's path name for the talloc name */
105 name = ntdb_name(ntdb);
107 talloc_set_name_const(ntdb, name);
109 talloc_set_name_const(ntdb, "unnamed ntdb");
111 talloc_set_destructor(ntdb, ntdb_destroy);
113 return talloc_steal(ctx, ntdb);