diff options
author | Michael Adam <obnox@samba.org> | 2012-05-11 22:11:42 +0200 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2012-05-14 04:04:55 +0200 |
commit | e945511aae52c0bc50007ffc703241c7da1db372 (patch) | |
tree | a8cfc6357fa4747417505d328a3a6d07f8dcd398 /source3/lib/dbwrap | |
parent | f5e5ed79ccc10e0b6aa50f6b6747d48d9505e7ee (diff) | |
download | samba-e945511aae52c0bc50007ffc703241c7da1db372.tar.gz samba-e945511aae52c0bc50007ffc703241c7da1db372.tar.bz2 samba-e945511aae52c0bc50007ffc703241c7da1db372.zip |
move the dbwrap library to the top level
Autobuild-User: Michael Adam <obnox@samba.org>
Autobuild-Date: Mon May 14 04:04:55 CEST 2012 on sn-devel-104
Diffstat (limited to 'source3/lib/dbwrap')
-rw-r--r-- | source3/lib/dbwrap/dbwrap.c | 398 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap.h | 131 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_cache.c | 225 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_cache.h | 32 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_file.c | 418 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_file.h | 36 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_private.h | 70 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_rbt.c | 450 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_rbt.h | 29 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_tdb.c | 447 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_tdb.h | 35 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_util.c | 539 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_watch.c | 2 | ||||
-rw-r--r-- | source3/lib/dbwrap/dbwrap_watch.h | 2 |
14 files changed, 2 insertions, 2812 deletions
diff --git a/source3/lib/dbwrap/dbwrap.c b/source3/lib/dbwrap/dbwrap.c deleted file mode 100644 index 14562bb6e4..0000000000 --- a/source3/lib/dbwrap/dbwrap.c +++ /dev/null @@ -1,398 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper - Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006 - - Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com) - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include "dbwrap/dbwrap.h" -#include "dbwrap/dbwrap_private.h" -#include "util_tdb.h" - -/* - * Fall back using fetch if no genuine exists operation is provided - */ - -static int dbwrap_fallback_exists(struct db_context *db, TDB_DATA key) -{ - NTSTATUS status = dbwrap_parse_record(db, key, NULL, NULL); - return NT_STATUS_IS_OK(status) ? 1 : 0; -} - -static int delete_record(struct db_record *rec, void *data) -{ - NTSTATUS status = dbwrap_record_delete(rec); - return NT_STATUS_IS_OK(status) ? 0 : -1; -} - -/* - * Fallback wipe implementation using traverse and delete if no genuine - * wipe operation is provided - */ -static int dbwrap_fallback_wipe(struct db_context *db) -{ - NTSTATUS status = dbwrap_trans_traverse(db, delete_record, NULL); - return NT_STATUS_IS_OK(status) ? 0 : -1; -} - - -/* - * Wrapper functions for the backend methods - */ - -TDB_DATA dbwrap_record_get_key(const struct db_record *rec) -{ - return rec->key; -} - -TDB_DATA dbwrap_record_get_value(const struct db_record *rec) -{ - return rec->value; -} - -NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags) -{ - NTSTATUS status; - struct db_context *db; - - status = rec->store(rec, data, flags); - if (!NT_STATUS_IS_OK(status)) { - return status; - } - db = rec->db; - if (db->stored_callback != NULL) { - db->stored_callback(db, rec, - db->stored_callback_private_data); - } - return NT_STATUS_OK; -} - -void dbwrap_set_stored_callback( - struct db_context *db, - void (*cb)(struct db_context *db, struct db_record *rec, - void *private_data), - void *private_data) -{ - db->stored_callback = cb; - db->stored_callback_private_data = private_data; -} - -NTSTATUS dbwrap_record_delete(struct db_record *rec) -{ - NTSTATUS status; - struct db_context *db; - - status = rec->delete_rec(rec); - if (!NT_STATUS_IS_OK(status)) { - return status; - } - db = rec->db; - if (db->stored_callback != NULL) { - db->stored_callback(db, rec, - db->stored_callback_private_data); - } - return NT_STATUS_OK; -} - -struct dbwrap_lock_order_state { - uint8_t *plock_order_mask; - uint8_t bitmask; -}; - -static int dbwrap_lock_order_state_destructor( - struct dbwrap_lock_order_state *s) -{ - *s->plock_order_mask &= ~s->bitmask; - return 0; -} - -static struct dbwrap_lock_order_state *dbwrap_check_lock_order( - struct db_context *db, TALLOC_CTX *mem_ctx) -{ - /* - * Store the lock_order of currently locked records as bits in - * "lock_order_mask". We only use levels 1,2,3 right now, so a - * single uint8_t is enough. - */ - static uint8_t lock_order_mask; - - struct dbwrap_lock_order_state *state; - uint8_t idx; - int used; - - if (db->lock_order == 0) { - /* - * lock order 0 is for example for dbwrap_rbt without - * real locking. Return state nevertheless to avoid - * special cases. - */ - return talloc(mem_ctx, struct dbwrap_lock_order_state); - } - - /* - * We fill bits from the high bits, to be able to use - * "ffs(lock_order_mask)" - */ - idx = sizeof(lock_order_mask)*8 - db->lock_order; - - used = ffs(lock_order_mask); - - DEBUG(5, ("used=%d, lock_order=%d, idx=%d\n", used, - (int)db->lock_order, (int)idx)); - - if ((used != 0) && (used-1 <= idx)) { - DEBUG(0, ("Lock order violation: Trying %d, order_mask=%x\n", - (int)db->lock_order, (int)lock_order_mask)); - return NULL; - } - - state = talloc(mem_ctx, struct dbwrap_lock_order_state); - if (state == NULL) { - DEBUG(1, ("talloc failed\n")); - return NULL; - } - state->bitmask = 1 << idx; - state->plock_order_mask = &lock_order_mask; - - talloc_set_destructor(state, dbwrap_lock_order_state_destructor); - lock_order_mask |= state->bitmask; - - return state; -} - -static struct db_record *dbwrap_fetch_locked_internal( - struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key, - struct db_record *(*db_fn)(struct db_context *db, TALLOC_CTX *mem_ctx, - TDB_DATA key)) -{ - struct db_record *rec; - struct dbwrap_lock_order_state *lock_order; - TALLOC_CTX *frame = talloc_stackframe(); - - lock_order = dbwrap_check_lock_order(db, frame); - if (lock_order == NULL) { - TALLOC_FREE(frame); - return NULL; - } - rec = db_fn(db, mem_ctx, key); - if (rec == NULL) { - TALLOC_FREE(frame); - return NULL; - } - (void)talloc_steal(rec, lock_order); - rec->db = db; - TALLOC_FREE(frame); - return rec; -} - -struct db_record *dbwrap_fetch_locked(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key) -{ - return dbwrap_fetch_locked_internal(db, mem_ctx, key, - db->fetch_locked); -} - -struct db_record *dbwrap_try_fetch_locked(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key) -{ - return dbwrap_fetch_locked_internal( - db, mem_ctx, key, - db->try_fetch_locked - ? db->try_fetch_locked : db->fetch_locked); -} - -struct db_context *dbwrap_record_get_db(struct db_record *rec) -{ - return rec->db; -} - -struct dbwrap_fetch_state { - TALLOC_CTX *mem_ctx; - TDB_DATA data; -}; - -static void dbwrap_fetch_parser(TDB_DATA key, TDB_DATA data, - void *private_data) -{ - struct dbwrap_fetch_state *state = - (struct dbwrap_fetch_state *)private_data; - - state->data.dsize = data.dsize; - state->data.dptr = (uint8_t *)talloc_memdup(state->mem_ctx, data.dptr, - data.dsize); -} - -NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, - TDB_DATA key, TDB_DATA *value) -{ - struct dbwrap_fetch_state state; - NTSTATUS status; - - if (value == NULL) { - return NT_STATUS_INVALID_PARAMETER; - } - - state.mem_ctx = mem_ctx; - - status = dbwrap_parse_record(db, key, dbwrap_fetch_parser, &state); - if (!NT_STATUS_IS_OK(status)) { - return status; - } - if ((state.data.dsize != 0) && (state.data.dptr == NULL)) { - return NT_STATUS_NO_MEMORY; - } - *value = state.data; - return NT_STATUS_OK; -} - -bool dbwrap_exists(struct db_context *db, TDB_DATA key) -{ - int result; - if (db->exists != NULL) { - result = db->exists(db, key); - } else { - result = dbwrap_fallback_exists(db,key); - } - return (result == 1); -} - -NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key, - TDB_DATA data, int flags) -{ - struct db_record *rec; - NTSTATUS status; - TALLOC_CTX *frame = talloc_stackframe(); - - rec = dbwrap_fetch_locked(db, frame, key); - if (rec == NULL) { - TALLOC_FREE(frame); - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_record_store(rec, data, flags); - TALLOC_FREE(frame); - return status; -} - -NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key) -{ - struct db_record *rec; - NTSTATUS status; - TALLOC_CTX *frame = talloc_stackframe(); - - rec = dbwrap_fetch_locked(db, frame, key); - if (rec == NULL) { - TALLOC_FREE(frame); - return NT_STATUS_NO_MEMORY; - } - status = dbwrap_record_delete(rec); - TALLOC_FREE(frame); - return status; -} - -NTSTATUS dbwrap_traverse(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data, - int *count) -{ - int ret = db->traverse(db, f, private_data); - - if (ret < 0) { - return NT_STATUS_INTERNAL_DB_CORRUPTION; - } - - if (count != NULL) { - *count = ret; - } - - return NT_STATUS_OK; -} - -NTSTATUS dbwrap_traverse_read(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data, - int *count) -{ - int ret = db->traverse_read(db, f, private_data); - - if (ret < 0) { - return NT_STATUS_INTERNAL_DB_CORRUPTION; - } - - if (count != NULL) { - *count = ret; - } - - return NT_STATUS_OK; -} - -static void dbwrap_null_parser(TDB_DATA key, TDB_DATA val, void* data) -{ - return; -} - -NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data) -{ - if (parser == NULL) { - parser = dbwrap_null_parser; - } - return db->parse_record(db, key, parser, private_data); -} - -int dbwrap_wipe(struct db_context *db) -{ - if (db->wipe == NULL) { - return dbwrap_fallback_wipe(db); - } - return db->wipe(db); -} - -int dbwrap_get_seqnum(struct db_context *db) -{ - return db->get_seqnum(db); -} - -int dbwrap_get_flags(struct db_context *db) -{ - return db->get_flags(db); -} - -int dbwrap_transaction_start(struct db_context *db) -{ - return db->transaction_start(db); -} - -int dbwrap_transaction_commit(struct db_context *db) -{ - return db->transaction_commit(db); -} - -int dbwrap_transaction_cancel(struct db_context *db) -{ - return db->transaction_cancel(db); -} - -void dbwrap_db_id(struct db_context *db, const uint8_t **id, size_t *idlen) -{ - db->id(db, id, idlen); -} diff --git a/source3/lib/dbwrap/dbwrap.h b/source3/lib/dbwrap/dbwrap.h deleted file mode 100644 index 59e5af069e..0000000000 --- a/source3/lib/dbwrap/dbwrap.h +++ /dev/null @@ -1,131 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around tdb - Copyright (C) Volker Lendecke 2005-2007 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_H__ -#define __DBWRAP_H__ - -#include "tdb_compat.h" - -struct db_record; -struct db_context; - -enum dbwrap_lock_order { - DBWRAP_LOCK_ORDER_1 = 1, - DBWRAP_LOCK_ORDER_2 = 2, - DBWRAP_LOCK_ORDER_3 = 3 -}; - -/* The following definitions come from lib/dbwrap.c */ - -TDB_DATA dbwrap_record_get_key(const struct db_record *rec); -TDB_DATA dbwrap_record_get_value(const struct db_record *rec); -NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags); -NTSTATUS dbwrap_record_delete(struct db_record *rec); -struct db_record *dbwrap_fetch_locked(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key); -struct db_record *dbwrap_try_fetch_locked(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key); -struct db_context *dbwrap_record_get_db(struct db_record *rec); -void dbwrap_set_stored_callback( - struct db_context *db, - void (*cb)(struct db_context *db, struct db_record *rec, - void *private_data), - void *private_data); - -NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key); -NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key, - TDB_DATA data, int flags); -NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, - TDB_DATA key, TDB_DATA *value); -bool dbwrap_exists(struct db_context *db, TDB_DATA key); -NTSTATUS dbwrap_traverse(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data, - int *count); -NTSTATUS dbwrap_traverse_read(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data, - int *count); -NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data); -int dbwrap_wipe(struct db_context *db); -int dbwrap_get_seqnum(struct db_context *db); -int dbwrap_get_flags(struct db_context *db); -int dbwrap_transaction_start(struct db_context *db); -int dbwrap_transaction_commit(struct db_context *db); -int dbwrap_transaction_cancel(struct db_context *db); -void dbwrap_db_id(struct db_context *db, const uint8_t **id, size_t *idlen); - - -/* The following definitions come from lib/dbwrap_util.c */ - -NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key); -NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key, - TDB_DATA data, int flags); -NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx, - const char *key, TDB_DATA *value); - -NTSTATUS dbwrap_fetch_int32(struct db_context *db, const char *keystr, - int32_t *result); -NTSTATUS dbwrap_store_int32(struct db_context *db, const char *keystr, - int32_t v); -NTSTATUS dbwrap_fetch_uint32(struct db_context *db, const char *keystr, - uint32_t *val); -NTSTATUS dbwrap_store_uint32(struct db_context *db, const char *keystr, - uint32_t v); -NTSTATUS dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr, - uint32_t *oldval, uint32_t change_val); -NTSTATUS dbwrap_trans_change_uint32_atomic(struct db_context *db, - const char *keystr, - uint32_t *oldval, - uint32_t change_val); -NTSTATUS dbwrap_change_int32_atomic(struct db_context *db, const char *keystr, - int32_t *oldval, int32_t change_val); -NTSTATUS dbwrap_trans_change_int32_atomic(struct db_context *db, - const char *keystr, - int32_t *oldval, - int32_t change_val); -NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf, - int flag); -NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key); -NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr, - int32_t v); -NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr, - uint32_t v); -NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key, - TDB_DATA data, int flags); -NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key); -NTSTATUS dbwrap_trans_do(struct db_context *db, - NTSTATUS (*action)(struct db_context *, void *), - void *private_data); -NTSTATUS dbwrap_trans_traverse(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data); - -NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key); -NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key, - TDB_DATA data, int flags); -NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx, - const char *key, TDB_DATA *value); - -#endif /* __DBWRAP_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_cache.c b/source3/lib/dbwrap/dbwrap_cache.c deleted file mode 100644 index 865fcff7a1..0000000000 --- a/source3/lib/dbwrap/dbwrap_cache.c +++ /dev/null @@ -1,225 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Cache db contents for parse_record based on seqnum - Copyright (C) Volker Lendecke 2012 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include "lib/dbwrap/dbwrap.h" -#include "lib/dbwrap/dbwrap_private.h" -#include "lib/dbwrap/dbwrap_rbt.h" -#include "lib/dbwrap/dbwrap_cache.h" - -struct db_cache_ctx { - int seqnum; - struct db_context *backing; - struct db_context *positive; - struct db_context *negative; -}; - -static bool dbwrap_cache_validate(struct db_cache_ctx *ctx) -{ - if (ctx->seqnum == dbwrap_get_seqnum(ctx->backing)) { - return true; - } - TALLOC_FREE(ctx->positive); - ctx->positive = db_open_rbt(ctx); - TALLOC_FREE(ctx->negative); - ctx->negative = db_open_rbt(ctx); - - return ((ctx->positive != NULL) && (ctx->negative != NULL)); -} - -static NTSTATUS dbwrap_cache_parse_record( - struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data), - void *private_data) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - TDB_DATA value; - NTSTATUS status; - - if (!dbwrap_cache_validate(ctx)) { - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_parse_record(ctx->positive, key, parser, private_data); - if (NT_STATUS_IS_OK(status)) { - return status; - } - if (dbwrap_exists(ctx->negative, key)) { - return NT_STATUS_NOT_FOUND; - } - - status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value); - - if (NT_STATUS_IS_OK(status)) { - dbwrap_store(ctx->positive, key, value, 0); - parser(key, value, private_data); - TALLOC_FREE(value.dptr); - return NT_STATUS_OK; - } - - if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) { - char c = '\0'; - value.dptr = (uint8_t *)&c; - value.dsize = sizeof(c); - dbwrap_store(ctx->negative, key, value, 0); - return NT_STATUS_NOT_FOUND; - } - return status; -} - -static struct db_record *dbwrap_cache_fetch_locked( - struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_fetch_locked(ctx->backing, mem_ctx, key); -} - -static int dbwrap_cache_traverse(struct db_context *db, - int (*f)(struct db_record *rec, - void *private_data), - void *private_data) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - NTSTATUS status; - int ret; - status = dbwrap_traverse(ctx->backing, f, private_data, &ret); - if (!NT_STATUS_IS_OK(status)) { - return -1; - } - return ret; -} - -static int dbwrap_cache_traverse_read(struct db_context *db, - int (*f)(struct db_record *rec, - void *private_data), - void *private_data) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - NTSTATUS status; - int ret; - status = dbwrap_traverse_read(ctx->backing, f, private_data, &ret); - if (!NT_STATUS_IS_OK(status)) { - return -1; - } - return ret; -} - -static int dbwrap_cache_get_seqnum(struct db_context *db) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_get_seqnum(ctx->backing); -} - -static int dbwrap_cache_get_flags(struct db_context *db) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_get_flags(ctx->backing); -} - -static int dbwrap_cache_transaction_start(struct db_context *db) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_transaction_start(ctx->backing); -} - -static int dbwrap_cache_transaction_commit(struct db_context *db) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_transaction_commit(ctx->backing); -} - -static int dbwrap_cache_transaction_cancel(struct db_context *db) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - return dbwrap_transaction_cancel(ctx->backing); -} - -static int dbwrap_cache_exists(struct db_context *db, TDB_DATA key) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - - if (ctx->positive && dbwrap_exists(ctx->positive, key)) { - return true; - } - if (ctx->negative && dbwrap_exists(ctx->negative, key)) { - return false; - } - return dbwrap_exists(ctx->backing, key); -} - -static void dbwrap_cache_id(struct db_context *db, const uint8_t **id, - size_t *idlen) -{ - struct db_cache_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_cache_ctx); - dbwrap_db_id(ctx->backing, id, idlen); -} - -struct db_context *db_open_cache(TALLOC_CTX *mem_ctx, - struct db_context *backing) -{ - struct db_context *db; - struct db_cache_ctx *ctx; - - db = talloc(mem_ctx, struct db_context); - if (db == NULL) { - return NULL; - } - ctx = talloc_zero(db, struct db_cache_ctx); - if (ctx == NULL) { - TALLOC_FREE(db); - return NULL; - } - - ctx->seqnum = -1; - ctx->backing = talloc_move(ctx, &backing); - db->private_data = ctx; - if (!dbwrap_cache_validate(ctx)) { - TALLOC_FREE(db); - return NULL; - } - - db->fetch_locked = dbwrap_cache_fetch_locked; - db->try_fetch_locked = NULL; - db->traverse = dbwrap_cache_traverse; - db->traverse_read = dbwrap_cache_traverse_read; - db->get_seqnum = dbwrap_cache_get_seqnum; - db->get_flags = dbwrap_cache_get_flags; - db->transaction_start = dbwrap_cache_transaction_start; - db->transaction_commit = dbwrap_cache_transaction_commit; - db->transaction_cancel = dbwrap_cache_transaction_cancel; - db->parse_record = dbwrap_cache_parse_record; - db->exists = dbwrap_cache_exists; - db->id = dbwrap_cache_id; - db->stored_callback = NULL; - db->wipe = NULL; - db->lock_order = 0; - db->persistent = false; - return db; -} diff --git a/source3/lib/dbwrap/dbwrap_cache.h b/source3/lib/dbwrap/dbwrap_cache.h deleted file mode 100644 index cd290e15f8..0000000000 --- a/source3/lib/dbwrap/dbwrap_cache.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around ctdbd - Copyright (C) Volker Lendecke 2012 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_CACHE_H__ -#define __DBWRAP_CACHE_H__ - -#include <talloc.h> - -#include "dbwrap/dbwrap_private.h" - -struct db_context; - -struct db_context *db_open_cache(TALLOC_CTX *mem_ctx, - struct db_context *backing); - -#endif /* __DBWRAP_CACHE_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_file.c b/source3/lib/dbwrap/dbwrap_file.c deleted file mode 100644 index e0fd4ebc73..0000000000 --- a/source3/lib/dbwrap/dbwrap_file.c +++ /dev/null @@ -1,418 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface using a file per record - Copyright (C) Volker Lendecke 2005 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include "dbwrap/dbwrap.h" -#include "dbwrap/dbwrap_file.h" -#include "dbwrap/dbwrap_private.h" -#include "lib/tdb_wrap/tdb_wrap.h" - -struct db_file_ctx { - const char *dirname; - - /* We only support one locked record at a time -- everything else - * would lead to a potential deadlock anyway! */ - struct db_record *locked_record; -}; - -struct db_locked_file { - int fd; - uint8_t hash; - const char *name; - const char *path; - struct db_file_ctx *parent; -}; - -/* Copy from statcache.c... */ - -static uint32_t fsh(const uint8_t *p, int len) -{ - uint32_t n = 0; - int i; - for (i=0; i<len; i++) { - n = ((n << 5) + n) ^ (uint32_t)(p[i]); - } - return n; -} - -static int db_locked_file_destr(struct db_locked_file *data) -{ - if (data->parent != NULL) { - data->parent->locked_record = NULL; - } - - if (close(data->fd) != 0) { - DEBUG(3, ("close failed: %s\n", strerror(errno))); - return -1; - } - - return 0; -} - -static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag); -static NTSTATUS db_file_delete(struct db_record *rec); - -static struct db_record *db_file_fetch_locked(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key) -{ - struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data, - struct db_file_ctx); - struct db_record *result; - struct db_locked_file *file; - struct flock fl; - SMB_STRUCT_STAT statbuf; - int ret; - - SMB_ASSERT(ctx->locked_record == NULL); - - again: - if (!(result = talloc(mem_ctx, struct db_record))) { - DEBUG(0, ("talloc failed\n")); - return NULL; - } - - if (!(file = talloc(result, struct db_locked_file))) { - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(result); - return NULL; - } - - result->private_data = file; - result->store = db_file_store; - result->delete_rec = db_file_delete; - - result->key.dsize = key.dsize; - result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr, - key.dsize); - if (result->key.dptr == NULL) { - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(result); - return NULL; - } - - /* Cut to 8 bits */ - file->hash = fsh(key.dptr, key.dsize); - file->name = hex_encode_talloc(file, (unsigned char *)key.dptr, key.dsize); - if (file->name == NULL) { - DEBUG(0, ("hex_encode failed\n")); - TALLOC_FREE(result); - return NULL; - } - - file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname, - file->hash, file->name); - if (file->path == NULL) { - DEBUG(0, ("talloc_asprintf failed\n")); - TALLOC_FREE(result); - return NULL; - } - - become_root(); - file->fd = open(file->path, O_RDWR|O_CREAT, 0644); - unbecome_root(); - - if (file->fd < 0) { - DEBUG(3, ("Could not open/create %s: %s\n", - file->path, strerror(errno))); - TALLOC_FREE(result); - return NULL; - } - - talloc_set_destructor(file, db_locked_file_destr); - - fl.l_type = F_WRLCK; - fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 1; - fl.l_pid = 0; - - do { - ret = fcntl(file->fd, F_SETLKW, &fl); - } while ((ret == -1) && (errno == EINTR)); - - if (ret == -1) { - DEBUG(3, ("Could not get lock on %s: %s\n", - file->path, strerror(errno))); - TALLOC_FREE(result); - return NULL; - } - - if (sys_fstat(file->fd, &statbuf, false) != 0) { - DEBUG(3, ("Could not fstat %s: %s\n", - file->path, strerror(errno))); - TALLOC_FREE(result); - return NULL; - } - - if (statbuf.st_ex_nlink == 0) { - /* Someone has deleted it under the lock, retry */ - TALLOC_FREE(result); - goto again; - } - - result->value.dsize = 0; - result->value.dptr = NULL; - - if (statbuf.st_ex_size != 0) { - NTSTATUS status; - - result->value.dsize = statbuf.st_ex_size; - result->value.dptr = talloc_array(result, uint8_t, - statbuf.st_ex_size); - if (result->value.dptr == NULL) { - DEBUG(1, ("talloc failed\n")); - TALLOC_FREE(result); - return NULL; - } - - status = read_data(file->fd, (char *)result->value.dptr, - result->value.dsize); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(3, ("read_data failed: %s\n", - nt_errstr(status))); - TALLOC_FREE(result); - return NULL; - } - } - - ctx->locked_record = result; - file->parent = (struct db_file_ctx *)talloc_reference(file, ctx); - - return result; -} - -static NTSTATUS db_file_store_root(int fd, TDB_DATA data) -{ - if (lseek(fd, 0, SEEK_SET) != 0) { - DEBUG(0, ("lseek failed: %s\n", strerror(errno))); - return map_nt_error_from_unix(errno); - } - - if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) { - DEBUG(3, ("write_data failed: %s\n", strerror(errno))); - return map_nt_error_from_unix(errno); - } - - if (ftruncate(fd, data.dsize) != 0) { - DEBUG(3, ("ftruncate failed: %s\n", strerror(errno))); - return map_nt_error_from_unix(errno); - } - - return NT_STATUS_OK; -} - -static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag) -{ - struct db_locked_file *file = - talloc_get_type_abort(rec->private_data, - struct db_locked_file); - NTSTATUS status; - - become_root(); - status = db_file_store_root(file->fd, data); - unbecome_root(); - - return status; -} - -static NTSTATUS db_file_delete(struct db_record *rec) -{ - struct db_locked_file *file = - talloc_get_type_abort(rec->private_data, - struct db_locked_file); - int res; - - become_root(); - res = unlink(file->path); - unbecome_root(); - - if (res == -1) { - DEBUG(3, ("unlink(%s) failed: %s\n", file->path, - strerror(errno))); - return map_nt_error_from_unix(errno); - } - - return NT_STATUS_OK; -} - -static int db_file_traverse(struct db_context *db, - int (*fn)(struct db_record *rec, - void *private_data), - void *private_data) -{ - struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data, - struct db_file_ctx); - TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname); - - int i; - int count = 0; - - for (i=0; i<256; i++) { - const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X", - ctx->dirname, i); - DIR *dir; - struct dirent *dirent; - - if (dirname == NULL) { - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(mem_ctx); - return -1; - } - - dir = opendir(dirname); - if (dir == NULL) { - DEBUG(3, ("Could not open dir %s: %s\n", dirname, - strerror(errno))); - TALLOC_FREE(mem_ctx); - return -1; - } - - while ((dirent = readdir(dir)) != NULL) { - DATA_BLOB keyblob; - TDB_DATA key; - struct db_record *rec; - - if ((dirent->d_name[0] == '.') && - ((dirent->d_name[1] == '\0') || - ((dirent->d_name[1] == '.') && - (dirent->d_name[2] == '\0')))) { - continue; - } - - keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name); - if (keyblob.data == NULL) { - DEBUG(5, ("strhex_to_data_blob failed\n")); - continue; - } - - key.dptr = keyblob.data; - key.dsize = keyblob.length; - - if ((ctx->locked_record != NULL) && - (key.dsize == ctx->locked_record->key.dsize) && - (memcmp(key.dptr, ctx->locked_record->key.dptr, - key.dsize) == 0)) { - count += 1; - if (fn(ctx->locked_record, - private_data) != 0) { - TALLOC_FREE(mem_ctx); - closedir(dir); - return count; - } - } - - rec = db_file_fetch_locked(db, mem_ctx, key); - if (rec == NULL) { - /* Someone might have deleted it */ - continue; - } - - if (rec->value.dptr == NULL) { - TALLOC_FREE(rec); - continue; - } - - count += 1; - - if (fn(rec, private_data) != 0) { - TALLOC_FREE(mem_ctx); - closedir(dir); - return count; - } - TALLOC_FREE(rec); - } - - closedir(dir); - } - - TALLOC_FREE(mem_ctx); - return count; -} - -struct db_context *db_open_file(TALLOC_CTX *mem_ctx, - struct messaging_context *msg_ctx, - const char *name, - int hash_size, int tdb_flags, - int open_flags, mode_t mode) -{ - struct db_context *result = NULL; - struct db_file_ctx *ctx; - - if (!(result = talloc_zero(mem_ctx, struct db_context))) { - DEBUG(0, ("talloc failed\n")); - return NULL; - } - - if (!(ctx = talloc(result, struct db_file_ctx))) { - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(result); - return NULL; - } - - result->private_data = ctx; - result->fetch_locked = db_file_fetch_locked; - result->try_fetch_locked = NULL; - result->traverse = db_file_traverse; - result->traverse_read = db_file_traverse; - result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); - - ctx->locked_record = NULL; - if (!(ctx->dirname = talloc_strdup(ctx, name))) { - DEBUG(0, ("talloc failed\n")); - TALLOC_FREE(result); - return NULL; - } - - if (open_flags & O_CREAT) { - int ret, i; - - mode |= (mode & S_IRUSR) ? S_IXUSR : 0; - mode |= (mode & S_IRGRP) ? S_IXGRP : 0; - mode |= (mode & S_IROTH) ? S_IXOTH : 0; - - ret = mkdir(name, mode); - if ((ret != 0) && (errno != EEXIST)) { - DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode, - strerror(errno))); - TALLOC_FREE(result); - return NULL; - } - - for (i=0; i<256; i++) { - char *path; - path = talloc_asprintf(result, "%s/%2.2X", name, i); - if (path == NULL) { - DEBUG(0, ("asprintf failed\n")); - TALLOC_FREE(result); - return NULL; - } - ret = mkdir(path, mode); - if ((ret != 0) && (errno != EEXIST)) { - DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path, - mode, strerror(errno))); - TALLOC_FREE(result); - return NULL; - } - TALLOC_FREE(path); - } - } - - return result; -} diff --git a/source3/lib/dbwrap/dbwrap_file.h b/source3/lib/dbwrap/dbwrap_file.h deleted file mode 100644 index 002f64dce5..0000000000 --- a/source3/lib/dbwrap/dbwrap_file.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface using a file per record - Copyright (C) Volker Lendecke 2005 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_FILE_H__ -#define __DBWRAP_FILE_H__ - -#include <talloc.h> - -struct db_context; - -struct messaging_context; - -struct db_context *db_open_file(TALLOC_CTX *mem_ctx, - struct messaging_context *msg_ctx, - const char *name, - int hash_size, int tdb_flags, - int open_flags, mode_t mode); - - -#endif /* __DBWRAP_FILE_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_private.h b/source3/lib/dbwrap/dbwrap_private.h deleted file mode 100644 index c197ffacf2..0000000000 --- a/source3/lib/dbwrap/dbwrap_private.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around tdb - private header - - Copyright (C) Volker Lendecke 2005-2007 - Copyright (C) Gregor Beck 2011 - Copyright (C) Michael Adam 2011 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_PRIVATE_H__ -#define __DBWRAP_PRIVATE_H__ - -struct db_record { - struct db_context *db; - TDB_DATA key, value; - NTSTATUS (*store)(struct db_record *rec, TDB_DATA data, int flag); - NTSTATUS (*delete_rec)(struct db_record *rec); - void *private_data; -}; - -struct db_context { - struct db_record *(*fetch_locked)(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key); - struct db_record *(*try_fetch_locked)(struct db_context *db, - TALLOC_CTX *mem_ctx, - TDB_DATA key); - int (*traverse)(struct db_context *db, - int (*f)(struct db_record *rec, - void *private_data), - void *private_data); - int (*traverse_read)(struct db_context *db, - int (*f)(struct db_record *rec, - void *private_data), - void *private_data); - int (*get_seqnum)(struct db_context *db); - int (*get_flags)(struct db_context *db); - int (*transaction_start)(struct db_context *db); - int (*transaction_commit)(struct db_context *db); - int (*transaction_cancel)(struct db_context *db); - NTSTATUS (*parse_record)(struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data); - int (*exists)(struct db_context *db,TDB_DATA key); - int (*wipe)(struct db_context *db); - void (*id)(struct db_context *db, const uint8_t **id, size_t *idlen); - void *private_data; - enum dbwrap_lock_order lock_order; - bool persistent; - void (*stored_callback)(struct db_context *db, struct db_record *rec, - void *private_data); - void *stored_callback_private_data; -}; - -#endif /* __DBWRAP_PRIVATE_H__ */ - diff --git a/source3/lib/dbwrap/dbwrap_rbt.c b/source3/lib/dbwrap/dbwrap_rbt.c deleted file mode 100644 index c25ea6ec64..0000000000 --- a/source3/lib/dbwrap/dbwrap_rbt.c +++ /dev/null @@ -1,450 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around red-black trees - Copyright (C) Volker Lendecke 2007, 2008 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include "dbwrap/dbwrap.h" -#include "dbwrap/dbwrap_private.h" -#include "dbwrap/dbwrap_rbt.h" -#include "../lib/util/rbtree.h" - -#define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15) - -struct db_rbt_ctx { - struct rb_root tree; -}; - -struct db_rbt_rec { - struct db_rbt_ctx *db_ctx; - struct db_rbt_node *node; -}; - -/* The structure that ends up in the tree */ - -struct db_rbt_node { - struct rb_node rb_node; - size_t keysize, valuesize; - - /* - * key and value are appended implicitly, "data" is only here as a - * target for offsetof() - */ - - char data[1]; -}; - -/* - * Hide the ugly pointer calculations in a function - */ - -static struct db_rbt_node *db_rbt2node(struct rb_node *node) -{ - return (struct db_rbt_node *) - ((char *)node - offsetof(struct db_rbt_node, rb_node)); -} - -/* - * Compare two keys - */ - -static int db_rbt_compare(TDB_DATA a, TDB_DATA b) -{ - int res; - - res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize)); - - if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) { - return -1; - } - if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) { - return 1; - } - return 0; -} - -/* - * dissect a db_rbt_node into its implicit key and value parts - */ - -static void db_rbt_parse_node(struct db_rbt_node *node, - TDB_DATA *key, TDB_DATA *value) -{ - key->dptr = ((uint8_t *)node) + offsetof(struct db_rbt_node, data); - key->dsize = node->keysize; - value->dptr = key->dptr + node->keysize; - value->dsize = node->valuesize; -} - -static NTSTATUS db_rbt_store(struct db_record *rec, TDB_DATA data, int flag) -{ - struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data; - struct db_rbt_node *node; - - struct rb_node ** p; - struct rb_node * parent; - - TDB_DATA this_key, this_val; - - if (rec_priv->node != NULL) { - - /* - * The record was around previously - */ - - db_rbt_parse_node(rec_priv->node, &this_key, &this_val); - - SMB_ASSERT(this_key.dsize == rec->key.dsize); - SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr, - this_key.dsize) == 0); - - if (this_val.dsize >= data.dsize) { - /* - * The new value fits into the old space - */ - memcpy(this_val.dptr, data.dptr, data.dsize); - rec_priv->node->valuesize = data.dsize; - return NT_STATUS_OK; - } - - /* - * We need to delete the key from the tree and start fresh, - * there's not enough space in the existing record - */ - - rb_erase(&rec_priv->node->rb_node, &rec_priv->db_ctx->tree); - - /* - * Keep the existing node around for a while: If the record - * existed before, we reference the key data in there. - */ - } - - node = (struct db_rbt_node *)talloc_size(rec_priv->db_ctx, - offsetof(struct db_rbt_node, data) + rec->key.dsize - + data.dsize); - - if (node == NULL) { - TALLOC_FREE(rec_priv->node); - return NT_STATUS_NO_MEMORY; - } - - ZERO_STRUCT(node->rb_node); - - node->keysize = rec->key.dsize; - node->valuesize = data.dsize; - - db_rbt_parse_node(node, &this_key, &this_val); - - memcpy(this_key.dptr, rec->key.dptr, node->keysize); - TALLOC_FREE(rec_priv->node); - - memcpy(this_val.dptr, data.dptr, node->valuesize); - - parent = NULL; - p = &rec_priv->db_ctx->tree.rb_node; - - while (*p) { - struct db_rbt_node *r; - TDB_DATA search_key, search_val; - int res; - - parent = (*p); - - r = db_rbt2node(*p); - - db_rbt_parse_node(r, &search_key, &search_val); - - res = db_rbt_compare(this_key, search_key); - - if (res == -1) { - p = &(*p)->rb_left; - } - else if (res == 1) { - p = &(*p)->rb_right; - } - else { - smb_panic("someone messed with the tree"); - } - } - - rb_link_node(&node->rb_node, parent, p); - rb_insert_color(&node->rb_node, &rec_priv->db_ctx->tree); - - return NT_STATUS_OK; -} - -static NTSTATUS db_rbt_delete(struct db_record *rec) -{ - struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data; - - if (rec_priv->node == NULL) { - return NT_STATUS_OK; - } - - rb_erase(&rec_priv->node->rb_node, &rec_priv->db_ctx->tree); - TALLOC_FREE(rec_priv->node); - - return NT_STATUS_OK; -} -struct db_rbt_search_result { - TDB_DATA key; - TDB_DATA val; - struct db_rbt_node* node; -}; - -static bool db_rbt_search_internal(struct db_context *db, TDB_DATA key, - struct db_rbt_search_result *result) -{ - struct db_rbt_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_rbt_ctx); - - struct rb_node *n; - bool found = false; - struct db_rbt_node *r = NULL; - TDB_DATA search_key, search_val; - - n = ctx->tree.rb_node; - - while (n != NULL) { - int res; - - r = db_rbt2node(n); - - db_rbt_parse_node(r, &search_key, &search_val); - - res = db_rbt_compare(key, search_key); - - if (res == -1) { - n = n->rb_left; - } - else if (res == 1) { - n = n->rb_right; - } - else { - found = true; - break; - } - } - if (result != NULL) { - if (found) { - result->key = search_key; - result->val = search_val; - result->node = r; - } else { - ZERO_STRUCT(*result); - } - } - return found; -} - -static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx, - TALLOC_CTX *mem_ctx, - TDB_DATA key) -{ - struct db_rbt_ctx *ctx = talloc_get_type_abort( - db_ctx->private_data, struct db_rbt_ctx); - - struct db_rbt_rec *rec_priv; - struct db_record *result; - size_t size; - bool found; - struct db_rbt_search_result res; - - found = db_rbt_search_internal(db_ctx, key, &res); - - /* - * In this low-level routine, play tricks to reduce the number of - * tallocs to one. Not recommened for general use, but here it pays - * off. - */ - - size = DBWRAP_RBT_ALIGN(sizeof(struct db_record)) - + sizeof(struct db_rbt_rec); - - if (!found) { - /* - * We need to keep the key around for later store - */ - size += key.dsize; - } - - result = (struct db_record *)talloc_size(mem_ctx, size); - if (result == NULL) { - return NULL; - } - - rec_priv = (struct db_rbt_rec *) - ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record))); - rec_priv->db_ctx = ctx; - - result->store = db_rbt_store; - result->delete_rec = db_rbt_delete; - result->private_data = rec_priv; - - rec_priv->node = res.node; - result->value = res.val; - - if (found) { - result->key = res.key; - } - else { - result->key.dptr = (uint8_t *) - ((char *)rec_priv + sizeof(*rec_priv)); - result->key.dsize = key.dsize; - memcpy(result->key.dptr, key.dptr, key.dsize); - } - - return result; -} - -static int db_rbt_exists(struct db_context *db, TDB_DATA key) -{ - return db_rbt_search_internal(db, key, NULL); -} - -static int db_rbt_wipe(struct db_context *db) -{ - struct db_rbt_ctx *old_ctx = talloc_get_type_abort( - db->private_data, struct db_rbt_ctx); - struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx); - if (new_ctx == NULL) { - return -1; - } - db->private_data = new_ctx; - talloc_free(old_ctx); - return 0; -} - -static NTSTATUS db_rbt_parse_record(struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data) -{ - struct db_rbt_search_result res; - bool found = db_rbt_search_internal(db, key, &res); - - if (!found) { - return NT_STATUS_NOT_FOUND; - } - parser(res.key, res.val, private_data); - return NT_STATUS_OK; -} - -static int db_rbt_traverse_internal(struct rb_node *n, - int (*f)(struct db_record *db, - void *private_data), - void *private_data, uint32_t* count) -{ - struct db_rbt_node *r; - struct db_record rec; - int ret; - - if (n == NULL) { - return 0; - } - - ret = db_rbt_traverse_internal(n->rb_left, f, private_data, count); - if (ret != 0) { - return ret; - } - - r = db_rbt2node(n); - ZERO_STRUCT(rec); - db_rbt_parse_node(r, &rec.key, &rec.value); - - ret = f(&rec, private_data); - (*count) ++; - if (ret != 0) { - return ret; - } - - return db_rbt_traverse_internal(n->rb_right, f, private_data, count); -} - -static int db_rbt_traverse(struct db_context *db, - int (*f)(struct db_record *db, - void *private_data), - void *private_data) -{ - struct db_rbt_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_rbt_ctx); - uint32_t count = 0; - - int ret = db_rbt_traverse_internal(ctx->tree.rb_node, f, private_data, &count); - if (ret != 0) { - return -1; - } - if (count > INT_MAX) { - return -1; - } - return count; -} - -static int db_rbt_get_seqnum(struct db_context *db) -{ - return 0; -} - -static int db_rbt_trans_dummy(struct db_context *db) -{ - /* - * Transactions are pretty pointless in-memory, just return success. - */ - return 0; -} - -static void db_rbt_id(struct db_context *db, const uint8_t **id, size_t *idlen) -{ - *id = (uint8_t *)db; - *idlen = sizeof(db); -} - -struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx) -{ - struct db_context *result; - - result = talloc(mem_ctx, struct db_context); - - if (result == NULL) { - return NULL; - } - - result->private_data = talloc_zero(result, struct db_rbt_ctx); - - if (result->private_data == NULL) { - TALLOC_FREE(result); - return NULL; - } - - result->fetch_locked = db_rbt_fetch_locked; - result->try_fetch_locked = NULL; - result->traverse = db_rbt_traverse; - result->traverse_read = db_rbt_traverse; - result->get_seqnum = db_rbt_get_seqnum; - result->transaction_start = db_rbt_trans_dummy; - result->transaction_commit = db_rbt_trans_dummy; - result->transaction_cancel = db_rbt_trans_dummy; - result->exists = db_rbt_exists; - result->wipe = db_rbt_wipe; - result->parse_record = db_rbt_parse_record; - result->lock_order = 0; - result->id = db_rbt_id; - result->stored_callback = NULL; - - return result; -} diff --git a/source3/lib/dbwrap/dbwrap_rbt.h b/source3/lib/dbwrap/dbwrap_rbt.h deleted file mode 100644 index 1716879de6..0000000000 --- a/source3/lib/dbwrap/dbwrap_rbt.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around red-black trees - Copyright (C) Volker Lendecke 2007, 2008 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_RBT_H__ -#define __DBWRAP_RBT_H__ - -#include <talloc.h> - -struct db_context; - -struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx); - -#endif /* __DBWRAP_RBT_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_tdb.c b/source3/lib/dbwrap/dbwrap_tdb.c deleted file mode 100644 index 798d391b1f..0000000000 --- a/source3/lib/dbwrap/dbwrap_tdb.c +++ /dev/null @@ -1,447 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around tdb - Copyright (C) Volker Lendecke 2005-2007 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include "dbwrap/dbwrap.h" -#include "dbwrap/dbwrap_private.h" -#include "dbwrap/dbwrap_tdb.h" -#include "lib/tdb_wrap/tdb_wrap.h" -#include "util_tdb.h" -#include "system/filesys.h" - -struct db_tdb_ctx { - struct tdb_wrap *wtdb; - - struct { - dev_t dev; - ino_t ino; - } id; -}; - -static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag); -static NTSTATUS db_tdb_delete(struct db_record *rec); - -static void db_tdb_log_key(const char *prefix, TDB_DATA key) -{ - size_t len; - char *keystr; - - if (DEBUGLEVEL < 10) { - return; - } - len = key.dsize; - if (DEBUGLEVEL == 10) { - /* - * Only fully spam at debuglevel > 10 - */ - len = MIN(10, key.dsize); - } - keystr = hex_encode_talloc(talloc_tos(), (unsigned char *)(key.dptr), - len); - DEBUG(10, ("%s key %s\n", prefix, keystr)); - TALLOC_FREE(keystr); -} - -static int db_tdb_record_destr(struct db_record* data) -{ - struct db_tdb_ctx *ctx = - talloc_get_type_abort(data->private_data, struct db_tdb_ctx); - - db_tdb_log_key("Unlocking", data->key); - tdb_chainunlock(ctx->wtdb->tdb, data->key); - return 0; -} - -struct tdb_fetch_locked_state { - TALLOC_CTX *mem_ctx; - struct db_record *result; -}; - -static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data, - void *private_data) -{ - struct tdb_fetch_locked_state *state = - (struct tdb_fetch_locked_state *)private_data; - struct db_record *result; - - result = (struct db_record *)talloc_size( - state->mem_ctx, - sizeof(struct db_record) + key.dsize + data.dsize); - - if (result == NULL) { - return 0; - } - state->result = result; - - result->key.dsize = key.dsize; - result->key.dptr = ((uint8_t *)result) + sizeof(struct db_record); - memcpy(result->key.dptr, key.dptr, key.dsize); - - result->value.dsize = data.dsize; - - if (data.dsize > 0) { - result->value.dptr = result->key.dptr+key.dsize; - memcpy(result->value.dptr, data.dptr, data.dsize); - } - else { - result->value.dptr = NULL; - } - - return 0; -} - -static struct db_record *db_tdb_fetch_locked_internal( - struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data, - struct db_tdb_ctx); - struct tdb_fetch_locked_state state; - - state.mem_ctx = mem_ctx; - state.result = NULL; - - if ((tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse, - &state) < 0) && - (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) { - tdb_chainunlock(ctx->wtdb->tdb, key); - return NULL; - } - - if (state.result == NULL) { - db_tdb_fetchlock_parse(key, tdb_null, &state); - } - - if (state.result == NULL) { - tdb_chainunlock(ctx->wtdb->tdb, key); - return NULL; - } - - talloc_set_destructor(state.result, db_tdb_record_destr); - - state.result->private_data = talloc_reference(state.result, ctx); - state.result->store = db_tdb_store; - state.result->delete_rec = db_tdb_delete; - - DEBUG(10, ("Allocated locked data 0x%p\n", state.result)); - - return state.result; -} - -static struct db_record *db_tdb_fetch_locked( - struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data, - struct db_tdb_ctx); - - db_tdb_log_key("Locking", key); - if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) { - DEBUG(3, ("tdb_chainlock failed\n")); - return NULL; - } - return db_tdb_fetch_locked_internal(db, mem_ctx, key); -} - -static struct db_record *db_tdb_try_fetch_locked( - struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data, - struct db_tdb_ctx); - - db_tdb_log_key("Trying to lock", key); - if (tdb_chainlock_nonblock(ctx->wtdb->tdb, key) != 0) { - DEBUG(3, ("tdb_chainlock_nonblock failed\n")); - return NULL; - } - return db_tdb_fetch_locked_internal(db, mem_ctx, key); -} - - -static int db_tdb_exists(struct db_context *db, TDB_DATA key) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_tdb_ctx); - return tdb_exists(ctx->wtdb->tdb, key); -} - -static int db_tdb_wipe(struct db_context *db) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_tdb_ctx); - return tdb_wipe_all(ctx->wtdb->tdb); -} - -struct db_tdb_parse_state { - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data); - void *private_data; -}; - -/* - * tdb_parse_record expects a parser returning int, mixing up tdb and - * parser errors. Wrap around that by always returning 0 and have - * dbwrap_parse_record expect a parser returning void. - */ - -static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data) -{ - struct db_tdb_parse_state *state = - (struct db_tdb_parse_state *)private_data; - state->parser(key, data, state->private_data); - return 0; -} - -static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key, - void (*parser)(TDB_DATA key, TDB_DATA data, - void *private_data), - void *private_data) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort( - db->private_data, struct db_tdb_ctx); - struct db_tdb_parse_state state; - int ret; - - state.parser = parser; - state.private_data = private_data; - - ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state); - - if (ret != 0) { - return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb)); - } - return NT_STATUS_OK; -} - -static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data, - struct db_tdb_ctx); - - /* - * This has a bug: We need to replace rec->value for correct - * operation, but right now brlock and locking don't use the value - * anymore after it was stored. - */ - - return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ? - NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; -} - -static NTSTATUS db_tdb_delete(struct db_record *rec) -{ - struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data, - struct db_tdb_ctx); - - if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) { - return NT_STATUS_OK; - } - - if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) { - return NT_STATUS_NOT_FOUND; - } - - return NT_STATUS_UNSUCCESSFUL; -} - -struct db_tdb_traverse_ctx { - struct db_context *db; - int (*f)(struct db_record *rec, void *private_data); - void *private_data; -}; - -static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, - void *private_data) -{ - struct db_tdb_traverse_ctx *ctx = - (struct db_tdb_traverse_ctx *)private_data; - struct db_record rec; - - rec.key = kbuf; - rec.value = dbuf; - rec.store = db_tdb_store; - rec.delete_rec = db_tdb_delete; - rec.private_data = ctx->db->private_data; - rec.db = ctx->db; - - return ctx->f(&rec, ctx->private_data); -} - -static int db_tdb_traverse(struct db_context *db, - int (*f)(struct db_record *rec, void *private_data), - void *private_data) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - struct db_tdb_traverse_ctx ctx; - - ctx.db = db; - ctx.f = f; - ctx.private_data = private_data; - return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx); -} - -static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag) -{ - return NT_STATUS_MEDIA_WRITE_PROTECTED; -} - -static NTSTATUS db_tdb_delete_deny(struct db_record *rec) -{ - return NT_STATUS_MEDIA_WRITE_PROTECTED; -} - -static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, - void *private_data) -{ - struct db_tdb_traverse_ctx *ctx = - (struct db_tdb_traverse_ctx *)private_data; - struct db_record rec; - - rec.key = kbuf; - rec.value = dbuf; - rec.store = db_tdb_store_deny; - rec.delete_rec = db_tdb_delete_deny; - rec.private_data = ctx->db->private_data; - rec.db = ctx->db; - - return ctx->f(&rec, ctx->private_data); -} - -static int db_tdb_traverse_read(struct db_context *db, - int (*f)(struct db_record *rec, void *private_data), - void *private_data) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - struct db_tdb_traverse_ctx ctx; - - ctx.db = db; - ctx.f = f; - ctx.private_data = private_data; - return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx); -} - -static int db_tdb_get_seqnum(struct db_context *db) - -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - return tdb_get_seqnum(db_ctx->wtdb->tdb); -} - -static int db_tdb_get_flags(struct db_context *db) - -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - return tdb_get_flags(db_ctx->wtdb->tdb); -} - -static int db_tdb_transaction_start(struct db_context *db) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0; -} - -static int db_tdb_transaction_commit(struct db_context *db) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - return tdb_transaction_commit(db_ctx->wtdb->tdb) ? -1 : 0; -} - -static int db_tdb_transaction_cancel(struct db_context *db) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - tdb_transaction_cancel(db_ctx->wtdb->tdb); - return 0; -} - -static void db_tdb_id(struct db_context *db, const uint8_t **id, size_t *idlen) -{ - struct db_tdb_ctx *db_ctx = - talloc_get_type_abort(db->private_data, struct db_tdb_ctx); - *id = (uint8_t *)&db_ctx->id; - *idlen = sizeof(db_ctx->id); -} - -struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, - struct loadparm_context *lp_ctx, - const char *name, - int hash_size, int tdb_flags, - int open_flags, mode_t mode, - enum dbwrap_lock_order lock_order) -{ - struct db_context *result = NULL; - struct db_tdb_ctx *db_tdb; - struct stat st; - - result = talloc_zero(mem_ctx, struct db_context); - if (result == NULL) { - DEBUG(0, ("talloc failed\n")); - goto fail; - } - - result->private_data = db_tdb = talloc(result, struct db_tdb_ctx); - if (db_tdb == NULL) { - DEBUG(0, ("talloc failed\n")); - goto fail; - } - result->lock_order = lock_order; - - db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags, - open_flags, mode, lp_ctx); - if (db_tdb->wtdb == NULL) { - DEBUG(3, ("Could not open tdb: %s\n", strerror(errno))); - goto fail; - } - - ZERO_STRUCT(db_tdb->id); - - if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) { - DEBUG(3, ("fstat failed: %s\n", strerror(errno))); - goto fail; - } - db_tdb->id.dev = st.st_dev; - db_tdb->id.ino = st.st_ino; - - result->fetch_locked = db_tdb_fetch_locked; - result->try_fetch_locked = db_tdb_try_fetch_locked; - result->traverse = db_tdb_traverse; - result->traverse_read = db_tdb_traverse_read; - result->parse_record = db_tdb_parse; - result->get_seqnum = db_tdb_get_seqnum; - result->get_flags = db_tdb_get_flags; - result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0); - result->transaction_start = db_tdb_transaction_start; - result->transaction_commit = db_tdb_transaction_commit; - result->transaction_cancel = db_tdb_transaction_cancel; - result->exists = db_tdb_exists; - result->wipe = db_tdb_wipe; - result->id = db_tdb_id; - result->stored_callback = NULL; - return result; - - fail: - if (result != NULL) { - TALLOC_FREE(result); - } - return NULL; -} diff --git a/source3/lib/dbwrap/dbwrap_tdb.h b/source3/lib/dbwrap/dbwrap_tdb.h deleted file mode 100644 index 6a6da45a08..0000000000 --- a/source3/lib/dbwrap/dbwrap_tdb.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Database interface wrapper around tdb - Copyright (C) Volker Lendecke 2005-2007 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef __DBWRAP_TDB_H__ -#define __DBWRAP_TDB_H__ - -#include <talloc.h> - -struct db_context; - -struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx, - struct loadparm_context *lp_ctx, - const char *name, - int hash_size, int tdb_flags, - int open_flags, mode_t mode, - enum dbwrap_lock_order lock_order); - - -#endif /* __DBWRAP_TDB_H__ */ diff --git a/source3/lib/dbwrap/dbwrap_util.c b/source3/lib/dbwrap/dbwrap_util.c deleted file mode 100644 index f5771c2398..0000000000 --- a/source3/lib/dbwrap/dbwrap_util.c +++ /dev/null @@ -1,539 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Utility functions for the dbwrap API - Copyright (C) Volker Lendecke 2007 - Copyright (C) Michael Adam 2009 - Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006 - - Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com) - - 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. -*/ - -#include "includes.h" -#include "dbwrap.h" -#include "util_tdb.h" - -NTSTATUS dbwrap_fetch_int32(struct db_context *db, const char *keystr, - int32_t *result) -{ - TDB_DATA dbuf; - NTSTATUS status; - - if (result == NULL) { - return NT_STATUS_INVALID_PARAMETER; - } - - status = dbwrap_fetch_bystring(db, NULL, keystr, &dbuf); - if (!NT_STATUS_IS_OK(status)) { - return status; - } - - if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) { - TALLOC_FREE(dbuf.dptr); - return NT_STATUS_NOT_FOUND; - } - - *result = IVAL(dbuf.dptr, 0); - TALLOC_FREE(dbuf.dptr); - return NT_STATUS_OK; -} - -NTSTATUS dbwrap_store_int32(struct db_context *db, const char *keystr, - int32_t v) -{ - struct db_record *rec; - int32_t v_store; - NTSTATUS status; - - rec = dbwrap_fetch_locked(db, NULL, string_term_tdb_data(keystr)); - if (rec == NULL) { - return NT_STATUS_UNSUCCESSFUL; - } - - SIVAL(&v_store, 0, v); - - status = dbwrap_record_store(rec, - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); - TALLOC_FREE(rec); - return status; -} - -NTSTATUS dbwrap_fetch_uint32(struct db_context *db, const char *keystr, - uint32_t *val) -{ - TDB_DATA dbuf; - NTSTATUS status; - - if (val == NULL) { - return NT_STATUS_INVALID_PARAMETER; - } - - status = dbwrap_fetch_bystring(db, NULL, keystr, &dbuf); - if (!NT_STATUS_IS_OK(status)) { - return status; - } - - if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(uint32_t))) { - TALLOC_FREE(dbuf.dptr); - return NT_STATUS_NOT_FOUND; - } - - *val = IVAL(dbuf.dptr, 0); - TALLOC_FREE(dbuf.dptr); - return NT_STATUS_OK; -} - -NTSTATUS dbwrap_store_uint32(struct db_context *db, const char *keystr, - uint32_t v) -{ - struct db_record *rec; - uint32_t v_store; - NTSTATUS status; - - rec = dbwrap_fetch_locked(db, NULL, string_term_tdb_data(keystr)); - if (rec == NULL) { - return NT_STATUS_INVALID_PARAMETER; - } - - SIVAL(&v_store, 0, v); - - status = dbwrap_record_store(rec, - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); - TALLOC_FREE(rec); - return status; -} - -/** - * Atomic unsigned integer change (addition): - * - * if value does not exist yet in the db, use *oldval as initial old value. - * return old value in *oldval. - * store *oldval + change_val to db. - */ - -struct dbwrap_change_uint32_atomic_context { - const char *keystr; - uint32_t *oldval; - uint32_t change_val; -}; - -static NTSTATUS dbwrap_change_uint32_atomic_action(struct db_context *db, - void *private_data) -{ - struct db_record *rec; - uint32_t val = (uint32_t)-1; - uint32_t v_store; - NTSTATUS ret; - struct dbwrap_change_uint32_atomic_context *state; - TDB_DATA value; - - state = (struct dbwrap_change_uint32_atomic_context *)private_data; - - rec = dbwrap_fetch_locked(db, NULL, string_term_tdb_data(state->keystr)); - if (!rec) { - return NT_STATUS_UNSUCCESSFUL; - } - - value = dbwrap_record_get_value(rec); - - if (value.dptr == NULL) { - val = *(state->oldval); - } else if (value.dsize == sizeof(val)) { - val = IVAL(value.dptr, 0); - *(state->oldval) = val; - } else { - ret = NT_STATUS_UNSUCCESSFUL; - goto done; - } - - val += state->change_val; - - SIVAL(&v_store, 0, val); - - ret = dbwrap_record_store(rec, - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); - -done: - TALLOC_FREE(rec); - return ret; -} - -NTSTATUS dbwrap_change_uint32_atomic(struct db_context *db, const char *keystr, - uint32_t *oldval, uint32_t change_val) -{ - NTSTATUS ret; - struct dbwrap_change_uint32_atomic_context state; - - state.keystr = keystr; - state.oldval = oldval; - state.change_val = change_val; - - ret = dbwrap_change_uint32_atomic_action(db, &state); - - return ret; -} - -NTSTATUS dbwrap_trans_change_uint32_atomic(struct db_context *db, - const char *keystr, - uint32_t *oldval, - uint32_t change_val) -{ - NTSTATUS ret; - struct dbwrap_change_uint32_atomic_context state; - - state.keystr = keystr; - state.oldval = oldval; - state.change_val = change_val; - - ret = dbwrap_trans_do(db, dbwrap_change_uint32_atomic_action, &state); - - return ret; -} - -/** - * Atomic integer change (addition): - * - * if value does not exist yet in the db, use *oldval as initial old value. - * return old value in *oldval. - * store *oldval + change_val to db. - */ - -struct dbwrap_change_int32_atomic_context { - const char *keystr; - int32_t *oldval; - int32_t change_val; -}; - -static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db, - void *private_data) -{ - struct db_record *rec; - int32_t val = -1; - int32_t v_store; - NTSTATUS ret; - struct dbwrap_change_int32_atomic_context *state; - TDB_DATA value; - - state = (struct dbwrap_change_int32_atomic_context *)private_data; - - rec = dbwrap_fetch_locked(db, NULL, string_term_tdb_data(state->keystr)); - if (!rec) { - return NT_STATUS_UNSUCCESSFUL; - } - - value = dbwrap_record_get_value(rec); - - if (value.dptr == NULL) { - val = *(state->oldval); - } else if (value.dsize == sizeof(val)) { - val = IVAL(value.dptr, 0); - *(state->oldval) = val; - } else { - ret = NT_STATUS_UNSUCCESSFUL; - goto done; - } - - val += state->change_val; - - SIVAL(&v_store, 0, val); - - ret = dbwrap_record_store(rec, - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); - -done: - TALLOC_FREE(rec); - return ret; -} - -NTSTATUS dbwrap_change_int32_atomic(struct db_context *db, const char *keystr, - int32_t *oldval, int32_t change_val) -{ - NTSTATUS ret; - struct dbwrap_change_int32_atomic_context state; - - state.keystr = keystr; - state.oldval = oldval; - state.change_val = change_val; - - ret = dbwrap_change_int32_atomic_action(db, &state); - - return ret; -} - -NTSTATUS dbwrap_trans_change_int32_atomic(struct db_context *db, - const char *keystr, - int32_t *oldval, - int32_t change_val) -{ - NTSTATUS ret; - struct dbwrap_change_int32_atomic_context state; - - state.keystr = keystr; - state.oldval = oldval; - state.change_val = change_val; - - ret = dbwrap_trans_do(db, dbwrap_change_int32_atomic_action, &state); - - return ret; -} - -struct dbwrap_store_context { - TDB_DATA *key; - TDB_DATA *dbuf; - int flag; -}; - -static NTSTATUS dbwrap_store_action(struct db_context *db, void *private_data) -{ - struct db_record *rec = NULL; - NTSTATUS status; - struct dbwrap_store_context *store_ctx; - - store_ctx = (struct dbwrap_store_context *)private_data; - - rec = dbwrap_fetch_locked(db, talloc_tos(), *(store_ctx->key)); - if (rec == NULL) { - DEBUG(5, ("fetch_locked failed\n")); - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_record_store(rec, *(store_ctx->dbuf), store_ctx->flag); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(5, ("store returned %s\n", nt_errstr(status))); - } - - TALLOC_FREE(rec); - return status; -} - -NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf, - int flag) -{ - NTSTATUS status; - struct dbwrap_store_context store_ctx; - - store_ctx.key = &key; - store_ctx.dbuf = &dbuf; - store_ctx.flag = flag; - - status = dbwrap_trans_do(db, dbwrap_store_action, &store_ctx); - - return status; -} - -static NTSTATUS dbwrap_delete_action(struct db_context * db, void *private_data) -{ - NTSTATUS status; - struct db_record *rec; - TDB_DATA *key = (TDB_DATA *)private_data; - - rec = dbwrap_fetch_locked(db, talloc_tos(), *key); - if (rec == NULL) { - DEBUG(5, ("fetch_locked failed\n")); - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_record_delete(rec); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status))); - } - - talloc_free(rec); - return status; -} - -NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key) -{ - NTSTATUS status; - - status = dbwrap_trans_do(db, dbwrap_delete_action, &key); - - return status; -} - -NTSTATUS dbwrap_trans_store_int32(struct db_context *db, const char *keystr, - int32_t v) -{ - int32_t v_store; - - SIVAL(&v_store, 0, v); - - return dbwrap_trans_store(db, string_term_tdb_data(keystr), - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); -} - -NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr, - uint32_t v) -{ - uint32_t v_store; - - SIVAL(&v_store, 0, v); - - return dbwrap_trans_store(db, string_term_tdb_data(keystr), - make_tdb_data((const uint8_t *)&v_store, - sizeof(v_store)), - TDB_REPLACE); -} - -NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key, - TDB_DATA data, int flags) -{ - return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags); -} - -NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key) -{ - return dbwrap_trans_delete(db, string_term_tdb_data(key)); -} - -/** - * Wrap db action(s) into a transaction. - */ -NTSTATUS dbwrap_trans_do(struct db_context *db, - NTSTATUS (*action)(struct db_context *, void *), - void *private_data) -{ - int res; - NTSTATUS status; - - res = dbwrap_transaction_start(db); - if (res != 0) { - DEBUG(5, ("transaction_start failed\n")); - return NT_STATUS_INTERNAL_DB_CORRUPTION; - } - - status = action(db, private_data); - if (!NT_STATUS_IS_OK(status)) { - if (dbwrap_transaction_cancel(db) != 0) { - smb_panic("Cancelling transaction failed"); - } - return status; - } - - res = dbwrap_transaction_commit(db); - if (res == 0) { - return NT_STATUS_OK; - } - - DEBUG(2, ("transaction_commit failed\n")); - return NT_STATUS_INTERNAL_DB_CORRUPTION; -} - -struct dbwrap_trans_traverse_action_ctx { - int (*f)(struct db_record* rec, void* private_data); - void* private_data; -}; - - -static NTSTATUS dbwrap_trans_traverse_action(struct db_context* db, void* private_data) -{ - struct dbwrap_trans_traverse_action_ctx* ctx = - (struct dbwrap_trans_traverse_action_ctx*)private_data; - - NTSTATUS status = dbwrap_traverse(db, ctx->f, ctx->private_data, NULL); - - return status; -} - -NTSTATUS dbwrap_trans_traverse(struct db_context *db, - int (*f)(struct db_record*, void*), - void *private_data) -{ - struct dbwrap_trans_traverse_action_ctx ctx = { - .f = f, - .private_data = private_data, - }; - return dbwrap_trans_do(db, dbwrap_trans_traverse_action, &ctx); -} - -NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key) -{ - return dbwrap_delete(db, string_term_tdb_data(key)); -} - -NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key, - TDB_DATA data, int flags) -{ - return dbwrap_store(db, string_term_tdb_data(key), data, flags); -} - -NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx, - const char *key, TDB_DATA *value) -{ - return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key), value); -} - - - -NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key) -{ - char *key_upper; - NTSTATUS status; - - key_upper = talloc_strdup_upper(talloc_tos(), key); - if (key_upper == NULL) { - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_delete_bystring(db, key_upper); - - talloc_free(key_upper); - return status; -} - -NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key, - TDB_DATA data, int flags) -{ - char *key_upper; - NTSTATUS status; - - key_upper = talloc_strdup_upper(talloc_tos(), key); - if (key_upper == NULL) { - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_store_bystring(db, key_upper, data, flags); - - talloc_free(key_upper); - return status; -} - -NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx, - const char *key, TDB_DATA *value) -{ - char *key_upper; - NTSTATUS status; - - key_upper = talloc_strdup_upper(talloc_tos(), key); - if (key_upper == NULL) { - return NT_STATUS_NO_MEMORY; - } - - status = dbwrap_fetch_bystring(db, mem_ctx, key_upper, value); - - talloc_free(key_upper); - return status; -} diff --git a/source3/lib/dbwrap/dbwrap_watch.c b/source3/lib/dbwrap/dbwrap_watch.c index 59288057f0..ea75427ffc 100644 --- a/source3/lib/dbwrap/dbwrap_watch.c +++ b/source3/lib/dbwrap/dbwrap_watch.c @@ -19,7 +19,7 @@ #include "includes.h" #include "system/filesys.h" -#include "dbwrap.h" +#include "dbwrap/dbwrap.h" #include "dbwrap_watch.h" #include "dbwrap_open.h" #include "msg_channel.h" diff --git a/source3/lib/dbwrap/dbwrap_watch.h b/source3/lib/dbwrap/dbwrap_watch.h index e242fa188a..66fef32698 100644 --- a/source3/lib/dbwrap/dbwrap_watch.h +++ b/source3/lib/dbwrap/dbwrap_watch.h @@ -21,7 +21,7 @@ #define __DBWRAP_WATCH_H__ #include <tevent.h> -#include "dbwrap.h" +#include "dbwrap/dbwrap.h" #include "messages.h" void dbwrap_watch_db(struct db_context *db, struct messaging_context *msg); |