summaryrefslogtreecommitdiffstats
path: root/ctdb/lib/util
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2007-01-25 15:11:36 +1100
committerAndrew Tridgell <tridge@samba.org>2007-01-25 15:11:36 +1100
commite90f9aa084fc03c7b99b079aac9aa6bb4b5f0346 (patch)
treee86a223181fa9cdb33549acfca67f020cb2ef798 /ctdb/lib/util
parente1797cf6be48b3c7ee00962283eb4e9d1b712961 (diff)
downloadsamba-e90f9aa084fc03c7b99b079aac9aa6bb4b5f0346.tar.gz
samba-e90f9aa084fc03c7b99b079aac9aa6bb4b5f0346.tar.xz
samba-e90f9aa084fc03c7b99b079aac9aa6bb4b5f0346.zip
merge db wrap code from samba4
(This used to be ctdb commit 2f3c299c76ce463cd866dfb1900ff45928f32ba6)
Diffstat (limited to 'ctdb/lib/util')
-rw-r--r--ctdb/lib/util/db_wrap.c83
-rw-r--r--ctdb/lib/util/db_wrap.h33
2 files changed, 116 insertions, 0 deletions
diff --git a/ctdb/lib/util/db_wrap.c b/ctdb/lib/util/db_wrap.c
new file mode 100644
index 0000000000..abf59c25f4
--- /dev/null
+++ b/ctdb/lib/util/db_wrap.c
@@ -0,0 +1,83 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ database wrap functions
+
+ Copyright (C) Andrew Tridgell 2004
+
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/*
+ the stupidity of the unix fcntl locking design forces us to never
+ allow a database file to be opened twice in the same process. These
+ wrappers provide convenient access to a tdb or ldb, taking advantage
+ of talloc destructors to ensure that only a single open is done
+*/
+
+#include "includes.h"
+#include "lib/util/dlinklist.h"
+#include "lib/events/events.h"
+#include "lib/tdb/include/tdb.h"
+#include "db_wrap.h"
+
+static struct tdb_wrap *tdb_list;
+
+
+
+/* destroy the last connection to a tdb */
+static int tdb_wrap_destructor(struct tdb_wrap *w)
+{
+ tdb_close(w->tdb);
+ DLIST_REMOVE(tdb_list, w);
+ return 0;
+}
+
+/*
+ wrapped connection to a tdb database
+ to close just talloc_free() the tdb_wrap pointer
+ */
+struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
+ const char *name, int hash_size, int tdb_flags,
+ int open_flags, mode_t mode)
+{
+ struct tdb_wrap *w;
+
+ for (w=tdb_list;w;w=w->next) {
+ if (strcmp(name, w->name) == 0) {
+ return talloc_reference(mem_ctx, w);
+ }
+ }
+
+ w = talloc(mem_ctx, struct tdb_wrap);
+ if (w == NULL) {
+ return NULL;
+ }
+
+ w->name = talloc_strdup(w, name);
+
+ w->tdb = tdb_open(name, hash_size, tdb_flags,
+ open_flags, mode);
+ if (w->tdb == NULL) {
+ talloc_free(w);
+ return NULL;
+ }
+
+ talloc_set_destructor(w, tdb_wrap_destructor);
+
+ DLIST_ADD(tdb_list, w);
+
+ return w;
+}
diff --git a/ctdb/lib/util/db_wrap.h b/ctdb/lib/util/db_wrap.h
new file mode 100644
index 0000000000..3d0488719d
--- /dev/null
+++ b/ctdb/lib/util/db_wrap.h
@@ -0,0 +1,33 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ database wrap headers
+
+ Copyright (C) Andrew Tridgell 2004
+
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+
+struct tdb_wrap {
+ struct tdb_context *tdb;
+
+ const char *name;
+ struct tdb_wrap *next, *prev;
+};
+
+struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
+ const char *name, int hash_size, int tdb_flags,
+ int open_flags, mode_t mode);