Rev 48: merge db wrap code from samba4 in http://samba.org/~tridge/ctdb

tridge at samba.org tridge at samba.org
Thu Jan 25 04:11:36 GMT 2007


------------------------------------------------------------
revno: 48
revision-id: tridge at samba.org-20070125041136-nb577ztw57xietqd
parent: tridge at samba.org-20070125041040-xb5n349ylp5fy32z
committer: Andrew Tridgell <tridge at samba.org>
branch nick: tridge
timestamp: Thu 2007-01-25 15:11:36 +1100
message:
  merge db wrap code from samba4
added:
  lib/util/db_wrap.c             db_wrap.c-20070125041102-mfc39bu6uof09lw6-1
  lib/util/db_wrap.h             db_wrap.h-20070125041102-mfc39bu6uof09lw6-2
=== added file 'lib/util/db_wrap.c'
--- a/lib/util/db_wrap.c	1970-01-01 00:00:00 +0000
+++ b/lib/util/db_wrap.c	2007-01-25 04:11:36 +0000
@@ -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;
+}

=== added file 'lib/util/db_wrap.h'
--- a/lib/util/db_wrap.h	1970-01-01 00:00:00 +0000
+++ b/lib/util/db_wrap.h	2007-01-25 04:11:36 +0000
@@ -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);



More information about the samba-cvs mailing list