1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Little dictionary style data structure based on dbwrap_rbt
|
---|
4 | Copyright (C) Volker Lendecke 2009
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "dbwrap/dbwrap.h"
|
---|
22 | #include "dbwrap/dbwrap_rbt.h"
|
---|
23 | #include "talloc_dict.h"
|
---|
24 | #include "util_tdb.h"
|
---|
25 |
|
---|
26 | struct talloc_dict {
|
---|
27 | struct db_context *db;
|
---|
28 | };
|
---|
29 |
|
---|
30 | struct talloc_dict *talloc_dict_init(TALLOC_CTX *mem_ctx)
|
---|
31 | {
|
---|
32 | struct talloc_dict *result;
|
---|
33 |
|
---|
34 | result = talloc(mem_ctx, struct talloc_dict);
|
---|
35 | if (result == NULL) {
|
---|
36 | return NULL;
|
---|
37 | }
|
---|
38 | result->db = db_open_rbt(result);
|
---|
39 | if (result->db == NULL) {
|
---|
40 | TALLOC_FREE(result);
|
---|
41 | return NULL;
|
---|
42 | }
|
---|
43 | return result;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Add a talloced object to the dict. Nulls out the pointer to indicate that
|
---|
48 | * the talloc ownership has been taken. If an object for "key" already exists,
|
---|
49 | * the existing object is talloc_free()ed and overwritten by the new
|
---|
50 | * object. If "data" is NULL, object for key "key" is deleted. Return false
|
---|
51 | * for "no memory".
|
---|
52 | */
|
---|
53 |
|
---|
54 | bool talloc_dict_set(struct talloc_dict *dict, DATA_BLOB key, void *pdata)
|
---|
55 | {
|
---|
56 | struct db_record *rec;
|
---|
57 | NTSTATUS status = NT_STATUS_OK;
|
---|
58 | void *data = *(void **)pdata;
|
---|
59 | TDB_DATA value;
|
---|
60 |
|
---|
61 | rec = dbwrap_fetch_locked(dict->db, talloc_tos(),
|
---|
62 | make_tdb_data(key.data, key.length));
|
---|
63 | if (rec == NULL) {
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | value = dbwrap_record_get_value(rec);
|
---|
68 |
|
---|
69 | if (value.dsize != 0) {
|
---|
70 | void *old_data;
|
---|
71 | if (value.dsize != sizeof(void *)) {
|
---|
72 | TALLOC_FREE(rec);
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | memcpy(&old_data, value.dptr, sizeof(old_data));
|
---|
76 | TALLOC_FREE(old_data);
|
---|
77 | if (data == NULL) {
|
---|
78 | status = dbwrap_record_delete(rec);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if (data != NULL) {
|
---|
82 | void *mydata = talloc_move(dict->db, &data);
|
---|
83 | *(void **)pdata = NULL;
|
---|
84 | status = dbwrap_record_store(rec,
|
---|
85 | make_tdb_data((uint8_t *)&mydata,
|
---|
86 | sizeof(mydata)), 0);
|
---|
87 | }
|
---|
88 | TALLOC_FREE(rec);
|
---|
89 | return NT_STATUS_IS_OK(status);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Fetch a talloced object. If "mem_ctx!=NULL", talloc_move the object there
|
---|
94 | * and delete it from the dict.
|
---|
95 | */
|
---|
96 |
|
---|
97 | void *talloc_dict_fetch(struct talloc_dict *dict, DATA_BLOB key,
|
---|
98 | TALLOC_CTX *mem_ctx)
|
---|
99 | {
|
---|
100 | struct db_record *rec;
|
---|
101 | void *result;
|
---|
102 | TDB_DATA value;
|
---|
103 |
|
---|
104 | rec = dbwrap_fetch_locked(dict->db, talloc_tos(),
|
---|
105 | make_tdb_data(key.data, key.length));
|
---|
106 | if (rec == NULL) {
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | value = dbwrap_record_get_value(rec);
|
---|
111 | if (value.dsize != sizeof(void *)) {
|
---|
112 | TALLOC_FREE(rec);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 | result = *(void **)value.dptr;
|
---|
116 |
|
---|
117 | if (mem_ctx != NULL) {
|
---|
118 | NTSTATUS status;
|
---|
119 | status = dbwrap_record_delete(rec);
|
---|
120 | if (!NT_STATUS_IS_OK(status)) {
|
---|
121 | TALLOC_FREE(rec);
|
---|
122 | return NULL;
|
---|
123 | }
|
---|
124 | result = talloc_move(mem_ctx, &result);
|
---|
125 | }
|
---|
126 |
|
---|
127 | return result;
|
---|
128 | }
|
---|
129 |
|
---|
130 | struct talloc_dict_traverse_state {
|
---|
131 | int (*fn)(DATA_BLOB key, void *data, void *private_data);
|
---|
132 | void *private_data;
|
---|
133 | };
|
---|
134 |
|
---|
135 | static int talloc_dict_traverse_fn(struct db_record *rec, void *private_data)
|
---|
136 | {
|
---|
137 | TDB_DATA key;
|
---|
138 | TDB_DATA value;
|
---|
139 | struct talloc_dict_traverse_state *state =
|
---|
140 | (struct talloc_dict_traverse_state *)private_data;
|
---|
141 | void *p;
|
---|
142 |
|
---|
143 | key = dbwrap_record_get_key(rec);
|
---|
144 | value = dbwrap_record_get_value(rec);
|
---|
145 |
|
---|
146 | if (value.dsize != sizeof(void *)) {
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | memcpy(&p, value.dptr, sizeof(p));
|
---|
151 | return state->fn(data_blob_const(key.dptr, key.dsize),
|
---|
152 | p, state->private_data);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Traverse a talloc_dict. If "fn" returns non-null, quit the traverse
|
---|
157 | */
|
---|
158 |
|
---|
159 | int talloc_dict_traverse(struct talloc_dict *dict,
|
---|
160 | int (*fn)(DATA_BLOB key, void *data,
|
---|
161 | void *private_data),
|
---|
162 | void *private_data)
|
---|
163 | {
|
---|
164 | struct talloc_dict_traverse_state state;
|
---|
165 | NTSTATUS status;
|
---|
166 | int count = 0;
|
---|
167 |
|
---|
168 | state.fn = fn;
|
---|
169 | state.private_data = private_data;
|
---|
170 | status = dbwrap_traverse(dict->db, talloc_dict_traverse_fn, &state,
|
---|
171 | &count);
|
---|
172 | if (!NT_STATUS_IS_OK(status)) {
|
---|
173 | return -1;
|
---|
174 | } else {
|
---|
175 | return count;
|
---|
176 | }
|
---|
177 | }
|
---|