mirror of
https://github.com/samba-team/samba.git
synced 2025-01-11 05:18:09 +03:00
merge db wrap code from samba4
(This used to be ctdb commit 2f3c299c76ce463cd866dfb1900ff45928f32ba6)
This commit is contained in:
parent
e1797cf6be
commit
e90f9aa084
83
ctdb/lib/util/db_wrap.c
Normal file
83
ctdb/lib/util/db_wrap.c
Normal file
@ -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;
|
||||
}
|
33
ctdb/lib/util/db_wrap.h
Normal file
33
ctdb/lib/util/db_wrap.h
Normal file
@ -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);
|
Loading…
Reference in New Issue
Block a user