summaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-06-22 09:44:41 +0930
committerRusty Russell <rusty@rustcorp.com.au>2012-06-22 07:35:17 +0200
commit735290f474a34986be4c66eb0bdf0b0ba14da970 (patch)
tree40df3d0abda9f8053e7b0160caa631bffde67db8 /lib/util
parent7c1d9fb3c12d20779afe8293b6a867bd2061077f (diff)
downloadsamba-735290f474a34986be4c66eb0bdf0b0ba14da970.tar.gz
samba-735290f474a34986be4c66eb0bdf0b0ba14da970.tar.bz2
samba-735290f474a34986be4c66eb0bdf0b0ba14da970.zip
util: util_ntdb.c
The first function is ntdb_new: this is preferred over ntdb_open, as it makes the ntdb_context returned (and all NTDB_DATA returned from ntdb_fetch) valid talloc pointers. The API is very similar to tdb_wrap_open(). Note that we handle $TDB_NO_FSYNC here, since ntdb doesn't do that hack (and it's great for speeding up testing!). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/util_ntdb.c114
-rw-r--r--lib/util/util_ntdb.h47
-rwxr-xr-xlib/util/wscript_build7
3 files changed, 168 insertions, 0 deletions
diff --git a/lib/util/util_ntdb.c b/lib/util/util_ntdb.c
new file mode 100644
index 0000000000..9c3e2a5254
--- /dev/null
+++ b/lib/util/util_ntdb.c
@@ -0,0 +1,114 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ ntdb utility functions
+
+ Copyright (C) Rusty Russell 2012
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "includes.h"
+#include "util_ntdb.h"
+#include "lib/param/param.h"
+
+static void *ntdb_talloc(const void *owner, size_t len, void *priv_data)
+{
+ return talloc_size(owner, len);
+}
+
+static void *ntdb_expand(void *old, size_t newlen, void *priv_data)
+{
+ return talloc_realloc_size(NULL, old, newlen);
+}
+
+static void ntdb_free(void *old, void *priv_data)
+{
+ talloc_free(old);
+}
+
+static int ntdb_destroy(struct ntdb_context *ntdb)
+{
+ ntdb_close(ntdb);
+ return 0;
+}
+
+static void ntdb_log(struct ntdb_context *ntdb,
+ enum ntdb_log_level level,
+ enum NTDB_ERROR ecode,
+ const char *message,
+ void *unused)
+{
+ int dl;
+ const char *name = ntdb_name(ntdb);
+
+ switch (level) {
+ case NTDB_LOG_USE_ERROR:
+ case NTDB_LOG_ERROR:
+ dl = 0;
+ break;
+ case NTDB_LOG_WARNING:
+ dl = 2;
+ break;
+ default:
+ dl = 0;
+ }
+
+ DEBUG(dl, ("ntdb(%s):%s: %s\n", name ? name : "unnamed",
+ ntdb_errorstr(ecode), message));
+}
+
+struct ntdb_context *ntdb_new(TALLOC_CTX *ctx,
+ const char *name, int ntdb_flags,
+ int open_flags, mode_t mode,
+ union ntdb_attribute *attr,
+ struct loadparm_context *lp_ctx)
+{
+ union ntdb_attribute log_attr, alloc_attr;
+ struct ntdb_context *ntdb;
+
+ if (lp_ctx && !lpcfg_use_mmap(lp_ctx)) {
+ ntdb_flags |= NTDB_NOMMAP;
+ }
+
+ /* Great hack for speeding testing! */
+ if (getenv("TDB_NO_FSYNC")) {
+ ntdb_flags |= NTDB_NOSYNC;
+ }
+
+ log_attr.base.next = attr;
+ log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
+ log_attr.log.fn = ntdb_log;
+
+ alloc_attr.base.next = &log_attr;
+ alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
+ alloc_attr.alloc.alloc = ntdb_talloc;
+ alloc_attr.alloc.expand = ntdb_expand;
+ alloc_attr.alloc.free = ntdb_free;
+
+ ntdb = ntdb_open(name, ntdb_flags, open_flags, mode, &alloc_attr);
+ if (!ntdb) {
+ return NULL;
+ }
+
+ /* We can re-use the tdb's path name for the talloc name */
+ name = ntdb_name(ntdb);
+ if (name) {
+ talloc_set_name_const(ntdb, name);
+ } else {
+ talloc_set_name_const(ntdb, "unnamed ntdb");
+ }
+ talloc_set_destructor(ntdb, ntdb_destroy);
+
+ return talloc_steal(ctx, ntdb);
+}
diff --git a/lib/util/util_ntdb.h b/lib/util/util_ntdb.h
new file mode 100644
index 0000000000..a5eab7f436
--- /dev/null
+++ b/lib/util/util_ntdb.h
@@ -0,0 +1,47 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ tdb utility functions
+
+ Copyright (C) Rusty Russell 2012
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _____LIB_UTIL_UTIL_NTDB_H__
+#define _____LIB_UTIL_UTIL_NTDB_H__
+#include <ntdb.h>
+#include <talloc.h>
+
+struct loadparm_context;
+union ntdb_attribute;
+
+/***************************************************************
+ Open an NTDB using talloc: it will be allocated off the context, and
+ all NTDB_DATA.dptr are allocated as children of the ntdb context.
+ Sets up a logging function for you, and uses lp_ctx to decide whether
+ to disable mmap.
+
+ Any extra ntdb attributes can be handed through attr; usually it's
+ NULL, ntdb_new provides logging and allocator attributes.
+
+ The destructor for the struct ntdb_context will do ntdb_close()
+ for you.
+****************************************************************/
+struct ntdb_context *ntdb_new(TALLOC_CTX *ctx,
+ const char *name, int ntdb_flags,
+ int open_flags, mode_t mode,
+ union ntdb_attribute *attr,
+ struct loadparm_context *lp_ctx);
+#endif /* _____LIB_UTIL_UTIL_NTDB_H__ */
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
index 92221b9494..a955ab6a48 100755
--- a/lib/util/wscript_build
+++ b/lib/util/wscript_build
@@ -45,6 +45,13 @@ bld.SAMBA_LIBRARY('util_tdb',
private_library=True
)
+bld.SAMBA_LIBRARY('util_ntdb',
+ source='util_ntdb.c',
+ local_include=False,
+ public_deps='ntdb talloc samba-util samba-hostconfig',
+ private_library=True
+ )
+
bld.SAMBA_LIBRARY('tevent-util',
source='tevent_unix.c tevent_ntstatus.c tevent_werror.c',
local_include=False,