s3: Add talloc_dict.[ch]
authorVolker Lendecke <vl@samba.org>
Sun, 16 Aug 2009 08:44:06 +0000 (10:44 +0200)
committerVolker Lendecke <vl@samba.org>
Sun, 16 Aug 2009 10:38:19 +0000 (12:38 +0200)
source3/Makefile.in
source3/include/includes.h
source3/include/talloc_dict.h [new file with mode: 0644]
source3/lib/talloc_dict.c [new file with mode: 0644]
source3/torture/torture.c

index f1c645a576f65aee9bd02af1bc6c7cc6ae41191e..3cb510cef4828a7085aecc48910a58db8711f72e 100644 (file)
@@ -375,6 +375,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
          lib/messages.o librpc/gen_ndr/ndr_messaging.o lib/messages_local.o \
          lib/messages_ctdbd.o lib/packet.o lib/ctdbd_conn.o \
          lib/interfaces.o lib/memcache.o \
+         lib/talloc_dict.o \
          lib/util_transfer_file.o ../lib/async_req/async_sock.o \
          $(TDB_LIB_OBJ) \
          $(VERSION_OBJ) lib/charcnv.o lib/debug.o lib/fault.o \
index 2d82e6e57e274705c7f5e874d9eeb1887a90fb39..87df3b7d31b599ba533ce522a9c9f43709d0a655 100644 (file)
@@ -689,6 +689,7 @@ struct smb_iconv_convenience *lp_iconv_convenience(void *lp_ctx);
 #include "memcache.h"
 #include "async_smb.h"
 #include "../lib/async_req/async_sock.h"
+#include "talloc_dict.h"
 #include "services.h"
 #include "eventlog.h"
 #include "../lib/util/smb_threads.h"
diff --git a/source3/include/talloc_dict.h b/source3/include/talloc_dict.h
new file mode 100644 (file)
index 0000000..8305f8d
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+   Unix SMB/CIFS implementation.
+   Little dictionary style data structure based on dbwrap_rbt
+   Copyright (C) Volker Lendecke 2009
+
+   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 __TALLOC_DICT_H__
+#define __TALLOC_DICT_H__
+
+#include "includes.h"
+
+struct talloc_dict;
+
+/*
+ * Create a talloc_dict structure.
+ */
+
+struct talloc_dict *talloc_dict_init(TALLOC_CTX *mem_ctx);
+
+/*
+ * Add a talloced object to the dict. Nulls out the pointer to indicate that
+ * the talloc ownership has been taken. If an object for "key" already exists,
+ * the existing object is talloc_free()ed and overwritten by the new
+ * object. If "data" is NULL, object for key "key" is deleted. Return false
+ * for "no memory".
+ */
+
+bool talloc_dict_set(struct talloc_dict *dict, DATA_BLOB key, void *data);
+
+/*
+ * Fetch a talloced object. If "mem_ctx!=NULL", talloc_move the object there
+ * and delete it from the dict.
+ */
+
+void *talloc_dict_fetch(struct talloc_dict *dict, DATA_BLOB key,
+                       TALLOC_CTX *mem_ctx);
+
+/*
+ * Traverse a talloc_dict. If "fn" returns non-null, quit the traverse
+ */
+
+int talloc_dict_traverse(struct talloc_dict *dict,
+                        int (*fn)(DATA_BLOB key, void *data,
+                                  void *private_data),
+                        void *private_data);
+
+#endif
diff --git a/source3/lib/talloc_dict.c b/source3/lib/talloc_dict.c
new file mode 100644 (file)
index 0000000..18b88d9
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+   Unix SMB/CIFS implementation.
+   Little dictionary style data structure based on dbwrap_rbt
+   Copyright (C) Volker Lendecke 2009
+
+   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"
+
+struct talloc_dict {
+       struct db_context *db;
+};
+
+struct talloc_dict *talloc_dict_init(TALLOC_CTX *mem_ctx)
+{
+       struct talloc_dict *result;
+
+       result = talloc(mem_ctx, struct talloc_dict);
+       if (result == NULL) {
+               return NULL;
+       }
+       result->db = db_open_rbt(result);
+       if (result->db == NULL) {
+               TALLOC_FREE(result);
+               return NULL;
+       }
+       return result;
+}
+
+/*
+ * Add a talloced object to the dict. Nulls out the pointer to indicate that
+ * the talloc ownership has been taken. If an object for "key" already exists,
+ * the existing object is talloc_free()ed and overwritten by the new
+ * object. If "data" is NULL, object for key "key" is deleted. Return false
+ * for "no memory".
+ */
+
+bool talloc_dict_set(struct talloc_dict *dict, DATA_BLOB key, void *pdata)
+{
+       struct db_record *rec;
+       NTSTATUS status = NT_STATUS_OK;
+       void *data = *(void **)pdata;
+
+       rec = dict->db->fetch_locked(dict->db, talloc_tos(),
+                                    make_tdb_data(key.data, key.length));
+       if (rec == NULL) {
+               return false;
+       }
+       if (rec->value.dsize != 0) {
+               void *old_data;
+               if (rec->value.dsize != sizeof(void *)) {
+                       TALLOC_FREE(rec);
+                       return false;
+               }
+               old_data = *(void **)(rec->value.dptr);
+               TALLOC_FREE(old_data);
+               if (data == NULL) {
+                       status = rec->delete_rec(rec);
+               }
+       }
+       if (data != NULL) {
+               void *mydata = talloc_move(dict->db, &data);
+               *(void **)pdata = NULL;
+               status = rec->store(rec, make_tdb_data((uint8_t *)&mydata,
+                                                      sizeof(mydata)), 0);
+       }
+       TALLOC_FREE(rec);
+       return NT_STATUS_IS_OK(status);
+}
+
+/*
+ * Fetch a talloced object. If "mem_ctx!=NULL", talloc_move the object there
+ * and delete it from the dict.
+ */
+
+void *talloc_dict_fetch(struct talloc_dict *dict, DATA_BLOB key,
+                       TALLOC_CTX *mem_ctx)
+{
+       struct db_record *rec;
+       void *result;
+
+       rec = dict->db->fetch_locked(dict->db, talloc_tos(),
+                                    make_tdb_data(key.data, key.length));
+       if (rec == NULL) {
+               return NULL;
+       }
+       if (rec->value.dsize != sizeof(void *)) {
+               TALLOC_FREE(rec);
+               return NULL;
+       }
+       result = *(void **)rec->value.dptr;
+
+       if (mem_ctx != NULL) {
+               NTSTATUS status;
+               status = rec->delete_rec(rec);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(rec);
+                       return NULL;
+               }
+               result = talloc_move(mem_ctx, &result);
+       }
+
+       return result;
+}
+
+struct talloc_dict_traverse_state {
+       int (*fn)(DATA_BLOB key, void *data, void *private_data);
+       void *private_data;
+};
+
+static int talloc_dict_traverse_fn(struct db_record *rec, void *private_data)
+{
+       struct talloc_dict_traverse_state *state =
+               (struct talloc_dict_traverse_state *)private_data;
+
+       if (rec->value.dsize != sizeof(void *)) {
+               return -1;
+       }
+       return state->fn(data_blob_const(rec->key.dptr, rec->key.dsize),
+                        *(void **)rec->value.dptr, state->private_data);
+}
+
+/*
+ * Traverse a talloc_dict. If "fn" returns non-null, quit the traverse
+ */
+
+int talloc_dict_traverse(struct talloc_dict *dict,
+                        int (*fn)(DATA_BLOB key, void *data,
+                                  void *private_data),
+                        void *private_data)
+{
+       struct talloc_dict_traverse_state state;
+       state.fn = fn;
+       state.private_data = private_data;
+       return dict->db->traverse(dict->db, talloc_dict_traverse_fn, &state);
+}
index babcb1e7d0e0cfc6a0ba0418202fa4d4e32fd6c2..ed592f169bfd0bda2569ed23bf4e5a2c7741f6e4 100644 (file)
@@ -5968,6 +5968,53 @@ static bool run_local_rbtree(int dummy)
        return ret;
 }
 
+struct talloc_dict_test {
+       int content;
+};
+
+static int talloc_dict_traverse_fn(DATA_BLOB key, void *data, void *priv)
+{
+       int *count = (int *)priv;
+       *count += 1;
+       return 0;
+}
+
+static bool run_local_talloc_dict(int dummy)
+{
+       struct talloc_dict *dict;
+       struct talloc_dict_test *t;
+       int key, count;
+
+       dict = talloc_dict_init(talloc_tos());
+       if (dict == NULL) {
+               return false;
+       }
+
+       t = talloc(talloc_tos(), struct talloc_dict_test);
+       if (t == NULL) {
+               return false;
+       }
+
+       key = 1;
+       t->content = 1;
+       if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
+               return false;
+       }
+
+       count = 0;
+       if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
+               return false;
+       }
+
+       if (count != 1) {
+               return false;
+       }
+
+       TALLOC_FREE(dict);
+
+       return true;
+}
+
 /* Split a path name into filename and stream name components. Canonicalise
  * such that an implicit $DATA token is always explicit.
  *
@@ -6516,6 +6563,7 @@ static struct {
        { "STREAMERROR", run_streamerror },
        { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
        { "LOCAL-GENCACHE", run_local_gencache, 0},
+       { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
        { "LOCAL-BASE64", run_local_base64, 0},
        { "LOCAL-RBTREE", run_local_rbtree, 0},
        { "LOCAL-MEMCACHE", run_local_memcache, 0},