pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.git] / lib / dbwrap / dbwrap_rbt.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface wrapper around red-black trees
4    Copyright (C) Volker Lendecke 2007, 2008
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_private.h"
23 #include "dbwrap/dbwrap_rbt.h"
24 #include "../lib/util/rbtree.h"
25 #include "../lib/util/dlinklist.h"
26
27 #define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15)
28
29 struct db_rbt_ctx {
30         struct rb_root tree;
31         struct db_rbt_node *nodes;
32         size_t traverse_read;
33         struct db_rbt_node **traverse_nextp;
34 };
35
36 struct db_rbt_rec {
37         struct db_rbt_node *node;
38 };
39
40 /* The structure that ends up in the tree */
41
42 struct db_rbt_node {
43         struct rb_node rb_node;
44         size_t keysize, valuesize;
45         struct db_rbt_node *prev, *next;
46 };
47
48 /*
49  * Hide the ugly pointer calculations in a function
50  */
51
52 static struct db_rbt_node *db_rbt2node(struct rb_node *node)
53 {
54         return (struct db_rbt_node *)
55                 ((char *)node - offsetof(struct db_rbt_node, rb_node));
56 }
57
58 /*
59  * Compare two keys
60  */
61
62 static int db_rbt_compare(TDB_DATA a, TDB_DATA b)
63 {
64         int res;
65
66         res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize));
67
68         if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) {
69                 return -1;
70         }
71         if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) {
72                 return 1;
73         }
74         return 0;
75 }
76
77 /*
78  * dissect a db_rbt_node into its implicit key and value parts
79  */
80
81 static void db_rbt_parse_node(struct db_rbt_node *node,
82                               TDB_DATA *key, TDB_DATA *value)
83 {
84         size_t key_offset, value_offset;
85
86         key_offset = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
87         key->dptr = ((uint8_t *)node) + key_offset;
88         key->dsize = node->keysize;
89
90         value_offset = DBWRAP_RBT_ALIGN(node->keysize);
91         value->dptr = key->dptr + value_offset;
92         value->dsize = node->valuesize;
93 }
94
95 static ssize_t db_rbt_reclen(size_t keylen, size_t valuelen)
96 {
97         size_t len, tmp;
98
99         len = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
100
101         tmp = DBWRAP_RBT_ALIGN(keylen);
102         if (tmp < keylen) {
103                 goto overflow;
104         }
105
106         len += tmp;
107         if (len < tmp) {
108                 goto overflow;
109         }
110
111         len += valuelen;
112         if (len < valuelen) {
113                 goto overflow;
114         }
115
116         return len;
117 overflow:
118         return -1;
119 }
120
121 static NTSTATUS db_rbt_storev(struct db_record *rec,
122                               const TDB_DATA *dbufs, int num_dbufs, int flag)
123 {
124         struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
125                 rec->db->private_data, struct db_rbt_ctx);
126         struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
127         struct db_rbt_node *node;
128
129         struct rb_node ** p;
130         struct rb_node *parent = NULL;
131         struct db_rbt_node *parent_node = NULL;
132
133         ssize_t reclen;
134         TDB_DATA data, this_key, this_val;
135         void *to_free = NULL;
136
137         if (db_ctx->traverse_read > 0) {
138                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
139         }
140
141         if (num_dbufs == 1) {
142                 data = dbufs[0];
143         } else {
144                 data = dbwrap_merge_dbufs(rec, dbufs, num_dbufs);
145                 if (data.dptr == NULL) {
146                         return NT_STATUS_NO_MEMORY;
147                 }
148                 to_free = data.dptr;
149         }
150
151         if (rec_priv->node != NULL) {
152
153                 /*
154                  * The record was around previously
155                  */
156
157                 db_rbt_parse_node(rec_priv->node, &this_key, &this_val);
158
159                 SMB_ASSERT(this_key.dsize == rec->key.dsize);
160                 SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr,
161                                   this_key.dsize) == 0);
162
163                 if (this_val.dsize >= data.dsize) {
164                         /*
165                          * The new value fits into the old space
166                          */
167                         memcpy(this_val.dptr, data.dptr, data.dsize);
168                         rec_priv->node->valuesize = data.dsize;
169                         TALLOC_FREE(to_free);
170                         return NT_STATUS_OK;
171                 }
172         }
173
174         reclen = db_rbt_reclen(rec->key.dsize, data.dsize);
175         if (reclen == -1) {
176                 TALLOC_FREE(to_free);
177                 return NT_STATUS_INSUFFICIENT_RESOURCES;
178         }
179
180         node = talloc_zero_size(db_ctx, reclen);
181         if (node == NULL) {
182                 TALLOC_FREE(to_free);
183                 return NT_STATUS_NO_MEMORY;
184         }
185
186         if (rec_priv->node != NULL) {
187                 if (db_ctx->traverse_nextp != NULL) {
188                         if (*db_ctx->traverse_nextp == rec_priv->node) {
189                                 *db_ctx->traverse_nextp = node;
190                         }
191                 }
192
193                 /*
194                  * We need to delete the key from the tree and start fresh,
195                  * there's not enough space in the existing record
196                  */
197
198                 rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
199                 DLIST_REMOVE(db_ctx->nodes, rec_priv->node);
200
201                 /*
202                  * Keep the existing node around for a while: If the record
203                  * existed before, we reference the key data in there.
204                  */
205         }
206
207         node->keysize = rec->key.dsize;
208         node->valuesize = data.dsize;
209
210         db_rbt_parse_node(node, &this_key, &this_val);
211
212         memcpy(this_key.dptr, rec->key.dptr, node->keysize);
213         TALLOC_FREE(rec_priv->node);
214         rec_priv->node = node;
215
216         memcpy(this_val.dptr, data.dptr, node->valuesize);
217
218         parent = NULL;
219         p = &db_ctx->tree.rb_node;
220
221         while (*p) {
222                 struct db_rbt_node *r;
223                 TDB_DATA search_key, search_val;
224                 int res;
225
226                 r = db_rbt2node(*p);
227
228                 parent = (*p);
229                 parent_node = r;
230
231                 db_rbt_parse_node(r, &search_key, &search_val);
232
233                 res = db_rbt_compare(this_key, search_key);
234
235                 if (res == -1) {
236                         p = &(*p)->rb_left;
237                 }
238                 else if (res == 1) {
239                         p = &(*p)->rb_right;
240                 }
241                 else {
242                         smb_panic("someone messed with the tree");
243                 }
244         }
245
246         rb_link_node(&node->rb_node, parent, p);
247         DLIST_ADD_AFTER(db_ctx->nodes, node, parent_node);
248         rb_insert_color(&node->rb_node, &db_ctx->tree);
249
250         TALLOC_FREE(to_free);
251
252         return NT_STATUS_OK;
253 }
254
255 static NTSTATUS db_rbt_delete(struct db_record *rec)
256 {
257         struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
258                 rec->db->private_data, struct db_rbt_ctx);
259         struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
260
261         if (db_ctx->traverse_read > 0) {
262                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
263         }
264
265         if (rec_priv->node == NULL) {
266                 return NT_STATUS_OK;
267         }
268
269         if (db_ctx->traverse_nextp != NULL) {
270                 if (*db_ctx->traverse_nextp == rec_priv->node) {
271                         *db_ctx->traverse_nextp = rec_priv->node->next;
272                 }
273         }
274
275         rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
276         DLIST_REMOVE(db_ctx->nodes, rec_priv->node);
277         TALLOC_FREE(rec_priv->node);
278
279         return NT_STATUS_OK;
280 }
281
282 struct db_rbt_search_result {
283         TDB_DATA key;
284         TDB_DATA val;
285         struct db_rbt_node* node;
286 };
287
288 static bool db_rbt_search_internal(struct db_context *db, TDB_DATA key,
289                                    struct db_rbt_search_result *result)
290 {
291         struct db_rbt_ctx *ctx = talloc_get_type_abort(
292                 db->private_data, struct db_rbt_ctx);
293
294         struct rb_node *n;
295         bool found = false;
296         struct db_rbt_node *r = NULL;
297         TDB_DATA search_key = { 0 };
298         TDB_DATA search_val = { 0 };
299
300         n = ctx->tree.rb_node;
301
302         while (n != NULL) {
303                 int res;
304
305                 r = db_rbt2node(n);
306
307                 db_rbt_parse_node(r, &search_key, &search_val);
308
309                 res = db_rbt_compare(key, search_key);
310
311                 if (res == -1) {
312                         n = n->rb_left;
313                 }
314                 else if (res == 1) {
315                         n = n->rb_right;
316                 }
317                 else {
318                         found = true;
319                         break;
320                 }
321         }
322         if (result != NULL) {
323                 if (found) {
324                         result->key = search_key;
325                         result->val = search_val;
326                         result->node = r;
327                 } else {
328                         ZERO_STRUCT(*result);
329                 }
330         }
331         return found;
332 }
333
334 static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx,
335                                              TALLOC_CTX *mem_ctx,
336                                              TDB_DATA key)
337 {
338         struct db_rbt_rec *rec_priv;
339         struct db_record *result;
340         size_t size;
341         bool found;
342         struct db_rbt_search_result res;
343
344         found = db_rbt_search_internal(db_ctx, key, &res);
345
346         /*
347          * In this low-level routine, play tricks to reduce the number of
348          * tallocs to one. Not recommened for general use, but here it pays
349          * off.
350          */
351
352         size = DBWRAP_RBT_ALIGN(sizeof(struct db_record))
353                 + sizeof(struct db_rbt_rec);
354
355         if (!found) {
356                 /*
357                  * We need to keep the key around for later store
358                  */
359                 size += key.dsize;
360         }
361
362         result = (struct db_record *)talloc_size(mem_ctx, size);
363         if (result == NULL) {
364                 return NULL;
365         }
366
367         rec_priv = (struct db_rbt_rec *)
368                 ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record)));
369
370         result->storev = db_rbt_storev;
371         result->delete_rec = db_rbt_delete;
372         result->private_data = rec_priv;
373
374         rec_priv->node = res.node;
375         result->value  = res.val;
376
377         if (found) {
378                 result->key = res.key;
379         }
380         else {
381                 result->key.dptr = (uint8_t *)
382                         ((char *)rec_priv + sizeof(*rec_priv));
383                 result->key.dsize = key.dsize;
384                 memcpy(result->key.dptr, key.dptr, key.dsize);
385         }
386
387         return result;
388 }
389
390 static int db_rbt_exists(struct db_context *db, TDB_DATA key)
391 {
392         return db_rbt_search_internal(db, key, NULL);
393 }
394
395 static int db_rbt_wipe(struct db_context *db)
396 {
397         struct db_rbt_ctx *old_ctx = talloc_get_type_abort(
398                 db->private_data, struct db_rbt_ctx);
399         struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx);
400         if (new_ctx == NULL) {
401                 return -1;
402         }
403         db->private_data = new_ctx;
404         talloc_free(old_ctx);
405         return 0;
406 }
407
408 static NTSTATUS db_rbt_parse_record(struct db_context *db, TDB_DATA key,
409                                     void (*parser)(TDB_DATA key, TDB_DATA data,
410                                                    void *private_data),
411                                     void *private_data)
412 {
413         struct db_rbt_search_result res;
414         bool found = db_rbt_search_internal(db, key, &res);
415
416         if (!found) {
417                 return NT_STATUS_NOT_FOUND;
418         }
419         parser(res.key, res.val, private_data);
420         return NT_STATUS_OK;
421 }
422
423 static int db_rbt_traverse_internal(struct db_context *db,
424                                     int (*f)(struct db_record *db,
425                                              void *private_data),
426                                     void *private_data, uint32_t* count,
427                                     bool rw)
428 {
429         struct db_rbt_ctx *ctx = talloc_get_type_abort(
430                 db->private_data, struct db_rbt_ctx);
431         struct db_rbt_node *cur = NULL;
432         struct db_rbt_node *next = NULL;
433         int ret;
434
435         for (cur = ctx->nodes; cur != NULL; cur = next) {
436                 struct db_record rec;
437                 struct db_rbt_rec rec_priv;
438
439                 rec_priv.node = cur;
440                 next = rec_priv.node->next;
441
442                 ZERO_STRUCT(rec);
443                 rec.db = db;
444                 rec.private_data = &rec_priv;
445                 rec.storev = db_rbt_storev;
446                 rec.delete_rec = db_rbt_delete;
447                 db_rbt_parse_node(rec_priv.node, &rec.key, &rec.value);
448
449                 if (rw) {
450                         ctx->traverse_nextp = &next;
451                 }
452                 ret = f(&rec, private_data);
453                 (*count) ++;
454                 if (rw) {
455                         ctx->traverse_nextp = NULL;
456                 }
457                 if (ret != 0) {
458                         return ret;
459                 }
460                 if (rec_priv.node != NULL) {
461                         next = rec_priv.node->next;
462                 }
463         }
464
465         return 0;
466 }
467
468 static int db_rbt_traverse_read(struct db_context *db,
469                                 int (*f)(struct db_record *db,
470                                          void *private_data),
471                                 void *private_data)
472 {
473         struct db_rbt_ctx *ctx = talloc_get_type_abort(
474                 db->private_data, struct db_rbt_ctx);
475         uint32_t count = 0;
476         int ret;
477
478         ctx->traverse_read++;
479         ret = db_rbt_traverse_internal(db,
480                                        f, private_data, &count,
481                                        false /* rw */);
482         ctx->traverse_read--;
483         if (ret != 0) {
484                 return -1;
485         }
486         if (count > INT_MAX) {
487                 return -1;
488         }
489         return count;
490 }
491
492 static int db_rbt_traverse(struct db_context *db,
493                            int (*f)(struct db_record *db,
494                                     void *private_data),
495                            void *private_data)
496 {
497         struct db_rbt_ctx *ctx = talloc_get_type_abort(
498                 db->private_data, struct db_rbt_ctx);
499         uint32_t count = 0;
500         int ret;
501
502         if (ctx->traverse_nextp != NULL) {
503                 return -1;
504         };
505
506         if (ctx->traverse_read > 0) {
507                 return db_rbt_traverse_read(db, f, private_data);
508         }
509
510         ret = db_rbt_traverse_internal(db,
511                                        f, private_data, &count,
512                                        true /* rw */);
513         if (ret != 0) {
514                 return -1;
515         }
516         if (count > INT_MAX) {
517                 return -1;
518         }
519         return count;
520 }
521
522 static int db_rbt_get_seqnum(struct db_context *db)
523 {
524         return 0;
525 }
526
527 static int db_rbt_trans_dummy(struct db_context *db)
528 {
529         /*
530          * Transactions are pretty pointless in-memory, just return success.
531          */
532         return 0;
533 }
534
535 static size_t db_rbt_id(struct db_context *db, uint8_t *id, size_t idlen)
536 {
537         if (idlen >= sizeof(struct db_context *)) {
538                 memcpy(id, &db, sizeof(struct db_context *));
539         }
540         return sizeof(struct db_context *);
541 }
542
543 struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx)
544 {
545         struct db_context *result;
546
547         result = talloc_zero(mem_ctx, struct db_context);
548
549         if (result == NULL) {
550                 return NULL;
551         }
552
553         result->private_data = talloc_zero(result, struct db_rbt_ctx);
554
555         if (result->private_data == NULL) {
556                 TALLOC_FREE(result);
557                 return NULL;
558         }
559
560         result->fetch_locked = db_rbt_fetch_locked;
561         result->traverse = db_rbt_traverse;
562         result->traverse_read = db_rbt_traverse_read;
563         result->get_seqnum = db_rbt_get_seqnum;
564         result->transaction_start = db_rbt_trans_dummy;
565         result->transaction_commit = db_rbt_trans_dummy;
566         result->transaction_cancel = db_rbt_trans_dummy;
567         result->exists = db_rbt_exists;
568         result->wipe = db_rbt_wipe;
569         result->parse_record = db_rbt_parse_record;
570         result->id = db_rbt_id;
571         result->name = "dbwrap rbt";
572
573         return result;
574 }