lib: Add server_id_db_prune_name
[Samba.git] / lib / util / server_id_db.c
blob83547525188ce3f6aff64cb6e7a4970fc8ebca29
1 /*
2 * Map names to server_ids
4 * Copyright Volker Lendecke <vl@samba.org> 2014
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 "replace.h"
21 #include "system/filesys.h"
22 #include "lib/util/server_id_db.h"
23 #include "lib/tdb_wrap/tdb_wrap.h"
24 #include "lib/util/strv.h"
25 #include "lib/util/util_tdb.h"
26 #include "lib/util/samba_util.h"
28 static TDB_DATA talloc_tdb_data(void *ptr)
30 return (TDB_DATA) { .dptr = ptr, .dsize = talloc_get_size(ptr) };
33 struct server_id_db {
34 struct server_id pid;
35 struct tdb_wrap *tdb;
36 char *names;
39 static int server_id_db_destructor(struct server_id_db *db);
41 struct server_id_db *server_id_db_init(TALLOC_CTX *mem_ctx,
42 struct server_id pid,
43 const char *base_path,
44 int hash_size, int tdb_flags)
46 struct server_id_db *db;
47 size_t pathlen = strlen(base_path) + 11;
48 char path[pathlen];
50 db = talloc(mem_ctx, struct server_id_db);
51 if (db == NULL) {
52 return NULL;
54 db->pid = pid;
55 db->names = NULL;
57 snprintf(path, pathlen, "%s/names.tdb", base_path);
59 db->tdb = tdb_wrap_open(db, path, hash_size, tdb_flags,
60 O_RDWR|O_CREAT, 0660);
61 if (db->tdb == NULL) {
62 TALLOC_FREE(db);
63 return NULL;
66 talloc_set_destructor(db, server_id_db_destructor);
68 return db;
71 void server_id_db_reinit(struct server_id_db *db, struct server_id pid)
73 db->pid = pid;
74 TALLOC_FREE(db->names);
77 static int server_id_db_destructor(struct server_id_db *db)
79 char *name = NULL;
81 while ((name = strv_next(db->names, name)) != NULL) {
82 server_id_db_remove(db, name);
85 return 0;
88 int server_id_db_add(struct server_id_db *db, const char *name)
90 struct tdb_context *tdb = db->tdb->tdb;
91 struct server_id_buf buf;
92 TDB_DATA key, data;
93 char *n;
94 int ret;
96 n = strv_find(db->names, name);
97 if (n != NULL) {
98 return EEXIST;
101 ret = strv_add(db, &db->names, name);
102 if (ret != 0) {
103 return ret;
106 key = string_term_tdb_data(name);
108 server_id_str_buf(db->pid, &buf);
109 data = string_term_tdb_data(buf.buf);
111 ret = tdb_append(tdb, key, data);
112 if (ret != 0) {
113 enum TDB_ERROR err = tdb_error(tdb);
114 strv_delete(&db->names, strv_find(db->names, name));
115 return map_unix_error_from_tdb(err);
118 return 0;
121 int server_id_db_prune_name(struct server_id_db *db, const char *name,
122 struct server_id server)
124 struct tdb_context *tdb = db->tdb->tdb;
125 struct server_id_buf buf;
126 TDB_DATA key;
127 uint8_t *data;
128 char *ids, *id;
129 int ret;
131 key = string_term_tdb_data(name);
132 server_id_str_buf(server, &buf);
134 ret = tdb_chainlock(tdb, key);
135 if (ret == -1) {
136 enum TDB_ERROR err = tdb_error(tdb);
137 return map_unix_error_from_tdb(err);
140 ret = tdb_fetch_talloc(tdb, key, db, &data);
141 if (ret != 0) {
142 tdb_chainunlock(tdb, key);
143 return ret;
146 ids = (char *)data;
148 id = strv_find(ids, buf.buf);
149 if (id == NULL) {
150 tdb_chainunlock(tdb, key);
151 TALLOC_FREE(data);
152 return ENOENT;
155 strv_delete(&ids, id);
156 ret = tdb_store(tdb, key, talloc_tdb_data(ids), TDB_MODIFY);
157 TALLOC_FREE(data);
159 tdb_chainunlock(tdb, key);
161 return 0;
164 int server_id_db_remove(struct server_id_db *db, const char *name)
166 char *n;
167 int ret;
169 n = strv_find(db->names, name);
170 if (n == NULL) {
171 return ENOENT;
174 ret = server_id_db_prune_name(db, name, db->pid);
175 if (ret != 0) {
176 return ret;
179 strv_delete(&db->names, n);
180 return 0;
183 int server_id_db_lookup(struct server_id_db *db, const char *name,
184 TALLOC_CTX *mem_ctx, unsigned *pnum_servers,
185 struct server_id **pservers)
187 struct tdb_context *tdb = db->tdb->tdb;
188 TDB_DATA key;
189 uint8_t *data;
190 char *ids, *id;
191 unsigned num_servers;
192 struct server_id *servers;
193 int i, ret;
195 key = string_term_tdb_data(name);
197 ret = tdb_fetch_talloc(tdb, key, mem_ctx, &data);
198 if (ret != 0) {
199 return ret;
202 ids = (char *)data;
203 num_servers = strv_count(ids);
205 servers = talloc_array(mem_ctx, struct server_id, num_servers);
206 if (servers == NULL) {
207 TALLOC_FREE(data);
208 return ENOMEM;
211 i = 0;
213 for (id = ids; id != NULL; id = strv_next(ids, id)) {
214 servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
217 TALLOC_FREE(data);
219 *pnum_servers = num_servers;
220 *pservers = servers;
222 return 0;
225 bool server_id_db_lookup_one(struct server_id_db *db, const char *name,
226 struct server_id *server)
228 int ret;
229 unsigned num_servers;
230 struct server_id *servers;
232 ret = server_id_db_lookup(db, name, db, &num_servers, &servers);
233 if (ret != 0) {
234 return false;
236 if (num_servers == 0) {
237 TALLOC_FREE(servers);
238 return false;
240 *server = servers[0];
241 TALLOC_FREE(servers);
242 return true;
245 struct server_id_db_traverse_state {
246 TALLOC_CTX *mem_ctx;
247 int (*fn)(const char *name,
248 unsigned num_servers,
249 const struct server_id *servers,
250 void *private_data);
251 void *private_data;
254 static int server_id_db_traverse_fn(struct tdb_context *tdb,
255 TDB_DATA key, TDB_DATA data,
256 void *private_data)
258 struct server_id_db_traverse_state *state = private_data;
259 const char *name;
260 char *ids, *id;
261 unsigned num_servers;
262 struct server_id *servers;
263 int i, ret;
265 if (key.dsize == 0) {
266 return 0;
268 if (key.dptr[key.dsize-1] != '\0') {
269 return 0;
271 name = (const char *)key.dptr;
273 ids = (char *)talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
274 if (ids == NULL) {
275 return 0;
278 num_servers = strv_count(ids);
279 servers = talloc_array(ids, struct server_id, num_servers);
281 i = 0;
283 for (id = ids; id != NULL; id = strv_next(ids, id)) {
284 servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
287 ret = state->fn(name, num_servers, servers, state->private_data);
289 TALLOC_FREE(ids);
291 return ret;
294 int server_id_db_traverse_read(struct server_id_db *db,
295 int (*fn)(const char *name,
296 unsigned num_servers,
297 const struct server_id *servers,
298 void *private_data),
299 void *private_data)
301 struct server_id_db_traverse_state state;
302 int ret;
304 state = (struct server_id_db_traverse_state) {
305 .fn = fn, .private_data = private_data,
306 .mem_ctx = talloc_new(db)
309 if (state.mem_ctx == NULL) {
310 return ENOMEM;
313 ret = tdb_traverse_read(db->tdb->tdb, server_id_db_traverse_fn,
314 &state);
315 TALLOC_FREE(state.mem_ctx);
316 return ret;