From 61bf43f5d17c9ac3fc9a36cab74a81f26ac97ccb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 20 Jun 2011 16:58:15 +0930 Subject: lib/tdb_compat: header for tdb1 vs tdb2. TDB2's API is slightly different from TDB1. In particular, all functions return 0 (TDB_SUCCESS) or a negative error number, rather than -1 or tdb_null and storing the error in tdb_error() (though TDB2 does that as well). The simplest fix is to replace all the different functions with a wrapper, and that is done here. Compatibility functions: tdb_null: not used as an error return, so not defined by tdb2. tdb_fetch_compat: TDB1-style data-returning tdb_fetch. tdb_firstkey_compat: TDB1-style data-returning tdb_firstkey tdb_nextkey_compat: TDB1-style data-returning tdb_nextkey, with TDB2-style free of old key. tdb_errorstr_compat: TDB1-style tdb_errorstr() which takes TDB instead of ecode. TDB_CONTEXT: TDB1-style typedef for struct tdb_context. tdb_open_compat: Simplified open routine which takes log function, sets TDB_ALLOW_NESTING as Samba expects, and adds TDB_CLEAR_IF_FIRST support. Things defined away in TDB2 wrappers: tdb_traverse_read: TDB2's tdb_traverse only uses read-locks anyway. tdb_reopen/tdb_reopen_all: TDB2 detects this error itself. TDB_INCOMPATIBLE_HASH: TDB2 uses the Jenkins hash already. TDB_VOLATILE: TDB2 shouldn't have freelist scaling issues. Signed-off-by: Rusty Russell --- lib/tdb_compat/tdb_compat.c | 96 +++++++++++++++++++++++++++++++ lib/tdb_compat/tdb_compat.h | 136 ++++++++++++++++++++++++++++++++++++++++++++ lib/tdb_compat/wscript | 7 +++ 3 files changed, 239 insertions(+) create mode 100644 lib/tdb_compat/tdb_compat.c create mode 100644 lib/tdb_compat/tdb_compat.h create mode 100644 lib/tdb_compat/wscript (limited to 'lib') diff --git a/lib/tdb_compat/tdb_compat.c b/lib/tdb_compat/tdb_compat.c new file mode 100644 index 0000000000..f432296a40 --- /dev/null +++ b/lib/tdb_compat/tdb_compat.c @@ -0,0 +1,96 @@ +#include + +/* Note: for the moment, we only need this file for TDB2, so we can + * assume waf. */ +#if BUILD_TDB2 +TDB_DATA tdb_null = { NULL, 0 }; + +/* Proxy which sets waitflag to false so we never block. */ +static int lock_nonblock(int fd, int rw, off_t off, off_t len, bool waitflag, + void *_orig) +{ + struct tdb_attribute_flock *orig = _orig; + + return orig->lock(fd, rw, off, len, false, orig->data); +} + +enum TDB_ERROR tdb_transaction_start_nonblock(struct tdb_context *tdb) +{ + union tdb_attribute locking, orig; + enum TDB_ERROR ecode; + + orig.base.attr = TDB_ATTRIBUTE_FLOCK; + ecode = tdb_get_attribute(tdb, &orig); + if (ecode != TDB_SUCCESS) + return ecode; + + /* Replace locking function with our own. */ + locking = orig; + locking.flock.data = &orig; + locking.flock.lock = lock_nonblock; + + ecode = tdb_set_attribute(tdb, &locking); + if (ecode != TDB_SUCCESS) + return ecode; + + ecode = tdb_transaction_start(tdb); + tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK); + return ecode; +} + +/* + * This handles TDB_CLEAR_IF_FIRST. + */ +static enum TDB_ERROR clear_if_first(int fd, void *unused) +{ + /* We hold a lock offset 63 always, so we can tell if anyone else is. */ + struct flock fl; + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 63; + fl.l_len = 1; + + if (fcntl(fd, F_SETLK, &fl) == 0) { + /* We must be first ones to open it w/ TDB_CLEAR_IF_FIRST! */ + if (ftruncate(fd, 0) != 0) { + return TDB_ERR_IO; + } + } + fl.l_type = F_RDLCK; + if (fcntl(fd, F_SETLKW, &fl) != 0) { + return TDB_ERR_IO; + } + return TDB_SUCCESS; +} + +struct tdb_context * +tdb_open_compat_(const char *name, int hash_size_unused, + int tdb_flags, int open_flags, mode_t mode, + void (*log_fn)(struct tdb_context *, + enum tdb_log_level, + const char *message, + void *data), + void *log_data) +{ + union tdb_attribute cif, log, *attr = NULL; + + if (log_fn) { + log.log.base.attr = TDB_ATTRIBUTE_LOG; + log.log.base.next = NULL; + log.log.fn = log_fn; + log.log.data = log_data; + attr = &log; + } + + if (tdb_flags & TDB_CLEAR_IF_FIRST) { + cif.openhook.base.attr = TDB_ATTRIBUTE_OPENHOOK; + cif.openhook.base.next = attr; + cif.openhook.fn = clear_if_first; + attr = &cif; + tdb_flags &= ~TDB_CLEAR_IF_FIRST; + } + return tdb_open(name, tdb_flags|TDB_ALLOW_NESTING, open_flags, mode, + attr); +} +#endif diff --git a/lib/tdb_compat/tdb_compat.h b/lib/tdb_compat/tdb_compat.h new file mode 100644 index 0000000000..ea401cba49 --- /dev/null +++ b/lib/tdb_compat/tdb_compat.h @@ -0,0 +1,136 @@ +/* + Unix SMB/CIFS implementation. + + Compatibility layer for TDB1 vs TDB2. + + Copyright (C) Rusty Russell 2011 + + ** NOTE! The following LGPL license applies to the tdb_compat + ** 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 3 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, see . +*/ +#ifndef TDB_COMPAT_H +#define TDB_COMPAT_H + +#include "replace.h" +#include +#if BUILD_TDB2 +#include +#include +#include + +extern TDB_DATA tdb_null; + +/* Old-style tdb_fetch. */ +static inline TDB_DATA tdb_fetch_compat(struct tdb_context *tdb, TDB_DATA k) +{ + TDB_DATA dbuf; + if (tdb_fetch(tdb, k, &dbuf) != TDB_SUCCESS) { + return tdb_null; + } + return dbuf; +} + +static inline TDB_DATA tdb_firstkey_compat(struct tdb_context *tdb) +{ + TDB_DATA k; + if (tdb_firstkey(tdb, &k) != TDB_SUCCESS) { + return tdb_null; + } + return k; +} + +/* Note: this frees the old key.dptr. */ +static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k) +{ + if (tdb_nextkey(tdb, &k) != TDB_SUCCESS) { + return tdb_null; + } + return k; +} + +/* tdb_traverse_read and tdb_traverse are equal: both only take read locks. */ +#define tdb_traverse_read tdb_traverse + +/* Old-style tdb_errorstr */ +#define tdb_errorstr_compat(tdb) tdb_errorstr(tdb_error(tdb)) + +/* This typedef doesn't exist in TDB2. */ +typedef struct tdb_context TDB_CONTEXT; + +/* We don't need these any more. */ +#define tdb_reopen_all(flag) 0 +#define tdb_reopen(tdb) 0 + +/* These no longer exist in tdb2. */ +#define TDB_CLEAR_IF_FIRST 1048576 +#define TDB_INCOMPATIBLE_HASH 0 +#define TDB_VOLATILE 0 + +/* tdb2 does nonblocking functions via attibutes. */ +enum TDB_ERROR tdb_transaction_start_nonblock(struct tdb_context *tdb); + +/* Convenient (typesafe) wrapper for tdb open with logging */ +#define tdb_open_compat(name, hsize, tdb_fl, open_fl, mode, log_fn, log_data) \ + tdb_open_compat_((name), (hsize), (tdb_fl), (open_fl), (mode), \ + typesafe_cb_preargs(void, void *, \ + (log_fn), (log_data), \ + struct tdb_context *, \ + enum tdb_log_level, \ + const char *), \ + (log_data)) + +struct tdb_context * +tdb_open_compat_(const char *name, int hash_size_unused, + int tdb_flags, int open_flags, mode_t mode, + void (*log_fn)(struct tdb_context *, + enum tdb_log_level, + const char *message, + void *data), + void *log_data); +#else +#include + +/* FIXME: Inlining this is a bit lazy, but eases S3 build. */ +static inline struct tdb_context * +tdb_open_compat(const char *name, int hash_size, + int tdb_flags, int open_flags, mode_t mode, + tdb_log_func log_fn, void *log_private) +{ + struct tdb_logging_context lctx; + lctx.log_fn = log_fn; + lctx.log_private = log_private; + + if (log_fn) + return tdb_open_ex(name, hash_size, tdb_flags, open_flags, + mode, &lctx, NULL); + else + return tdb_open(name, hash_size, tdb_flags, open_flags, mode); +} + +#define tdb_firstkey_compat tdb_firstkey +/* Note: this frees the old key.dptr. */ +static inline TDB_DATA tdb_nextkey_compat(struct tdb_context *tdb, TDB_DATA k) +{ + TDB_DATA next = tdb_nextkey(tdb, k); + free(k.dptr); + return next; +} +#define tdb_errorstr_compat(tdb) tdb_errorstr(tdb) +#define tdb_fetch_compat tdb_fetch +#endif + +#endif /* TDB_COMPAT_H */ diff --git a/lib/tdb_compat/wscript b/lib/tdb_compat/wscript new file mode 100644 index 0000000000..14be4e7fe0 --- /dev/null +++ b/lib/tdb_compat/wscript @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +def build(bld): + bld.SAMBA_LIBRARY('tdb_compat', + source='tdb_compat.c', + deps='replace tdb', + private_library=True) -- cgit