Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
index 3a18f5df88d2de0409190a5d920fde07d1651576..6ee8417e25409f865ec23c6524785c7d65e1d400 100644 (file)
@@ -10,7 +10,7 @@
    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.
+   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
    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
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-#include "ldb/ldb_tdb/ldb_tdb.h"
+#include "ldb_tdb.h"
 
 /*
   the purpose of this code is to work around the braindead posix locking
@@ -43,9 +39,8 @@ struct ltdb_wrap {
 static struct ltdb_wrap *tdb_list;
 
 /* destroy the last connection to a tdb */
-static int ltdb_wrap_destructor(void *ctx)
+static int ltdb_wrap_destructor(struct ltdb_wrap *w)
 {
-       struct ltdb_wrap *w = talloc_get_type(ctx, struct ltdb_wrap);
        tdb_close(w->tdb);
        if (w->next) {
                w->next->prev = w->prev;
@@ -59,6 +54,43 @@ static int ltdb_wrap_destructor(void *ctx)
        return 0;
 }                               
 
+static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
+static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
+{
+       va_list ap;
+       const char *name = tdb_name(tdb);
+       struct ldb_context *ldb = talloc_get_type(tdb_get_logging_private(tdb), struct ldb_context);
+       enum ldb_debug_level ldb_level;
+       char *message; 
+
+       if (ldb == NULL)
+               return;
+
+       va_start(ap, fmt);
+       message = talloc_vasprintf(ldb, fmt, ap);
+       va_end(ap);
+
+       switch (level) {
+       case TDB_DEBUG_FATAL:
+               ldb_level = LDB_DEBUG_FATAL;
+               break;
+       case TDB_DEBUG_ERROR:
+               ldb_level = LDB_DEBUG_ERROR;
+               break;
+       case TDB_DEBUG_WARNING:
+               ldb_level = LDB_DEBUG_WARNING;
+               break;
+       case TDB_DEBUG_TRACE:
+               ldb_level = LDB_DEBUG_TRACE;
+               break;
+       default:
+               ldb_level = LDB_DEBUG_FATAL;
+       }
+
+       ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
+       talloc_free(message);
+}
+
 /*
   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
@@ -66,16 +98,24 @@ static int ltdb_wrap_destructor(void *ctx)
   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)
+                                  const char *path, int hash_size, 
+                                  int tdb_flags,
+                                  int open_flags, mode_t mode, 
+                                  struct ldb_context *ldb)
 {
        struct ltdb_wrap *w;
        struct stat st;
+       struct tdb_logging_context log_ctx;
+
+       log_ctx.log_fn = ltdb_log_fn;
+       log_ctx.log_private = ldb;
 
        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);
+                               if (!talloc_reference(mem_ctx, w)) {
+                                       return NULL;
+                               }
                                return w->tdb;
                        }
                }
@@ -86,7 +126,7 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       w->tdb = tdb_open(path, hash_size, tdb_flags, open_flags, mode);
+       w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
        if (w->tdb == NULL) {
                talloc_free(w);
                return NULL;