s3-net: fix query_service_state() error handling.
[Samba.git] / source3 / lib / talloc_dict.c
blobcbe5c7b5c2abcb9d32f05c2d67fe881d15766ab1
1 /*
2 Unix SMB/CIFS implementation.
3 Little dictionary style data structure based on dbwrap_rbt
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "dbwrap.h"
22 #include "talloc_dict.h"
24 struct talloc_dict {
25 struct db_context *db;
28 struct talloc_dict *talloc_dict_init(TALLOC_CTX *mem_ctx)
30 struct talloc_dict *result;
32 result = talloc(mem_ctx, struct talloc_dict);
33 if (result == NULL) {
34 return NULL;
36 result->db = db_open_rbt(result);
37 if (result->db == NULL) {
38 TALLOC_FREE(result);
39 return NULL;
41 return result;
45 * Add a talloced object to the dict. Nulls out the pointer to indicate that
46 * the talloc ownership has been taken. If an object for "key" already exists,
47 * the existing object is talloc_free()ed and overwritten by the new
48 * object. If "data" is NULL, object for key "key" is deleted. Return false
49 * for "no memory".
52 bool talloc_dict_set(struct talloc_dict *dict, DATA_BLOB key, void *pdata)
54 struct db_record *rec;
55 NTSTATUS status = NT_STATUS_OK;
56 void *data = *(void **)pdata;
58 rec = dict->db->fetch_locked(dict->db, talloc_tos(),
59 make_tdb_data(key.data, key.length));
60 if (rec == NULL) {
61 return false;
63 if (rec->value.dsize != 0) {
64 void *old_data;
65 if (rec->value.dsize != sizeof(void *)) {
66 TALLOC_FREE(rec);
67 return false;
69 old_data = *(void **)(rec->value.dptr);
70 TALLOC_FREE(old_data);
71 if (data == NULL) {
72 status = rec->delete_rec(rec);
75 if (data != NULL) {
76 void *mydata = talloc_move(dict->db, &data);
77 *(void **)pdata = NULL;
78 status = rec->store(rec, make_tdb_data((uint8_t *)&mydata,
79 sizeof(mydata)), 0);
81 TALLOC_FREE(rec);
82 return NT_STATUS_IS_OK(status);
86 * Fetch a talloced object. If "mem_ctx!=NULL", talloc_move the object there
87 * and delete it from the dict.
90 void *talloc_dict_fetch(struct talloc_dict *dict, DATA_BLOB key,
91 TALLOC_CTX *mem_ctx)
93 struct db_record *rec;
94 void *result;
96 rec = dict->db->fetch_locked(dict->db, talloc_tos(),
97 make_tdb_data(key.data, key.length));
98 if (rec == NULL) {
99 return NULL;
101 if (rec->value.dsize != sizeof(void *)) {
102 TALLOC_FREE(rec);
103 return NULL;
105 result = *(void **)rec->value.dptr;
107 if (mem_ctx != NULL) {
108 NTSTATUS status;
109 status = rec->delete_rec(rec);
110 if (!NT_STATUS_IS_OK(status)) {
111 TALLOC_FREE(rec);
112 return NULL;
114 result = talloc_move(mem_ctx, &result);
117 return result;
120 struct talloc_dict_traverse_state {
121 int (*fn)(DATA_BLOB key, void *data, void *private_data);
122 void *private_data;
125 static int talloc_dict_traverse_fn(struct db_record *rec, void *private_data)
127 struct talloc_dict_traverse_state *state =
128 (struct talloc_dict_traverse_state *)private_data;
130 if (rec->value.dsize != sizeof(void *)) {
131 return -1;
133 return state->fn(data_blob_const(rec->key.dptr, rec->key.dsize),
134 *(void **)rec->value.dptr, state->private_data);
138 * Traverse a talloc_dict. If "fn" returns non-null, quit the traverse
141 int talloc_dict_traverse(struct talloc_dict *dict,
142 int (*fn)(DATA_BLOB key, void *data,
143 void *private_data),
144 void *private_data)
146 struct talloc_dict_traverse_state state;
147 state.fn = fn;
148 state.private_data = private_data;
149 return dict->db->traverse(dict->db, talloc_dict_traverse_fn, &state);