1
0
mirror of https://github.com/samba-team/samba.git synced 2025-11-30 20:23:49 +03:00
Files
samba-mirror/source/lib/ldb/ldb_tdb/ldb_tdb_wrap.c
Andrew Tridgell 4673cdd0d2 r10253: a fairly large tdb cleanup and re-organise. Nearly all of this change
just involves splitting up the core tdb.c code into separate files on
logical boundaries, but there are some minor functional changes as well:

 - move the 'struct tdb_context' into tdb_private.h, hiding it from
   users. This was done to allow the structure to change without
   breaking code that uses tdb.

 - added accessor functions tdb_fd(), tdb_name(), and tdb_log_fn() to
   access the elements of struct tdb_context that were used by
   external code but are no longer visible

 - simplied tdb_append() to use tdb_fetch()/tdb_store(), which is just
   as good due to the way tdb locks work

 - changed some of the types (such as tdb_off to tdb_off_t) to make
   syntax highlighting work better

 - removed the old optional spinlock code. It was a bad idea.

 - fixed a bug in tdb_reopen_all() that caused tdbtorture to sometimes
   fail or report nasty looking errors. This is the only real bug
   fixed in this commit. Jeremy/Jerry, you might like to pickup this
   change for Samba3, as that could definately affect smbd in
   Samba3.

The aim of all of these changes is to make the tdb
transactions/journaling code I am working on easier to write. I
started to write it on top of the existing tdb.c code and it got very
messy. Splitting up the code makes it much easier to follow.

There are more cleanups we could do in tdb, such as using uint32_t
instead of u32 (suggested by metze). I'll leave those for another day.
2007-10-10 13:38:12 -05:00

116 lines
2.8 KiB
C

/*
ldb database library
Copyright (C) Andrew Tridgell 2005
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
#include "ldb/ldb_tdb/ldb_tdb.h"
/*
the purpose of this code is to work around the braindead posix locking
rules, to allow us to have a ldb open more than once while allowing
locking to work
*/
struct ltdb_wrap {
struct ltdb_wrap *next, *prev;
struct tdb_context *tdb;
dev_t device;
ino_t inode;
};
static struct ltdb_wrap *tdb_list;
/* destroy the last connection to a tdb */
static int ltdb_wrap_destructor(void *ctx)
{
struct ltdb_wrap *w = talloc_get_type(ctx, struct ltdb_wrap);
tdb_close(w->tdb);
if (w->next) {
w->next->prev = w->prev;
}
if (w->prev) {
w->prev->next = w->next;
}
if (w == tdb_list) {
tdb_list = w->next;
}
return 0;
}
/*
wrapped connection to a tdb database. The caller should _not_ free
this as it is not a talloc structure (as tdb does not use talloc
yet). It will auto-close when the caller frees the mem_ctx that is
passed to this call
*/
struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
const char *path, int hash_size, int tdb_flags,
int open_flags, mode_t mode)
{
struct ltdb_wrap *w;
struct stat st;
if (stat(path, &st) == 0) {
for (w=tdb_list;w;w=w->next) {
if (st.st_dev == w->device && st.st_ino == w->inode) {
talloc_reference(mem_ctx, w);
return w->tdb;
}
}
}
w = talloc(mem_ctx, struct ltdb_wrap);
if (w == NULL) {
return NULL;
}
w->tdb = tdb_open(path, hash_size, tdb_flags, open_flags, mode);
if (w->tdb == NULL) {
talloc_free(w);
return NULL;
}
if (fstat(tdb_fd(w->tdb), &st) != 0) {
tdb_close(w->tdb);
talloc_free(w);
return NULL;
}
w->device = st.st_dev;
w->inode = st.st_ino;
talloc_set_destructor(w, ltdb_wrap_destructor);
w->next = tdb_list;
w->prev = NULL;
if (tdb_list) {
tdb_list->prev = w;
}
tdb_list = w;
return w->tdb;
}