r23790: LGPLv3+ conversion for our LGPLv2+ library code
[kai/samba-autobuild/.git] / source3 / lib / ldb / ldb_tdb / ldb_index.c
index c9a5dc9b0506c105ce9cd18cb8f5d1cbbd6d612b..379f5a599435284d2c877538dfa151b9d74368e8 100644 (file)
@@ -10,7 +10,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -41,8 +41,7 @@
   find an element in a list, using the given comparison function and
   assuming that the list is already sorted using comp_fn
 
-  return -1 if not found, or the index of the first occurance of needle if
-  found
+  return -1 if not found, or the index of the first occurance of needle if found
 */
 static int ldb_list_find(const void *needle, 
                         const void *base, size_t nmemb, size_t size, 
@@ -129,8 +128,7 @@ static struct ldb_dn *ldb_dn_key(struct ldb_context *ldb,
        if (ldb_should_b64_encode(&v)) {
                char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length);
                if (!vstr) return NULL;
-               dn = talloc_asprintf(ldb, "%s:%s::%s", LTDB_INDEX, attr_folded,
-                                    vstr);
+               dn = talloc_asprintf(ldb, "%s:%s::%s", LTDB_INDEX, attr_folded, vstr);
                talloc_free(vstr);
                if (v.data != value->data) {
                        talloc_free(v.data);
@@ -140,8 +138,8 @@ static struct ldb_dn *ldb_dn_key(struct ldb_context *ldb,
                goto done;
        }
 
-       dn = talloc_asprintf(ldb, "%s:%s:%.*s", LTDB_INDEX, attr_folded,
-                            (int)v.length, (char *)v.data);
+       dn = talloc_asprintf(ldb, "%s:%s:%.*s", 
+                             LTDB_INDEX, attr_folded, (int)v.length, (char *)v.data);
 
        if (v.data != value->data) {
                talloc_free(v.data);
@@ -166,8 +164,7 @@ static int ldb_msg_find_idx(const struct ldb_message *msg, const char *attr,
                        const struct ldb_message_element *el = 
                                &msg->elements[i];
                        for (j=0;j<el->num_values;j++) {
-                               if (ldb_attr_cmp((char *)el->values[j].data,
-                                                attr) == 0) {
+                               if (ldb_attr_cmp((char *)el->values[j].data, attr) == 0) {
                                        if (v_idx) {
                                                *v_idx = j;
                                        }
@@ -204,8 +201,7 @@ static int ltdb_index_dn_simple(struct ldb_module *module,
 
        /* if the attribute isn't in the list of indexed attributes then
           this node needs a full search */
-       if (ldb_msg_find_idx(index_list, tree->u.equality.attr, NULL,
-                            LTDB_IDXATTR) == -1) {
+       if (ldb_msg_find_idx(index_list, tree->u.equality.attr, NULL, LTDB_IDXATTR) == -1) {
                return -1;
        }
 
@@ -241,8 +237,8 @@ static int ltdb_index_dn_simple(struct ldb_module *module,
                }
 
                for (j=0;j<el->num_values;j++) {
-                       list->dn[list->count] = talloc_strdup(
-                               list->dn, (char *)el->values[j].data);
+                       list->dn[list->count] = 
+                               talloc_strdup(list->dn, (char *)el->values[j].data);
                        if (!list->dn[list->count]) {
                                talloc_free(msg);
                                return -1;
@@ -254,16 +250,14 @@ static int ltdb_index_dn_simple(struct ldb_module *module,
        talloc_free(msg);
 
        if (list->count > 1) {
-               qsort(list->dn, list->count, sizeof(char *),
-                     (comparison_fn_t) list_cmp);
+               qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t) list_cmp);
        }
 
        return 1;
 }
 
 
-static int list_union(struct ldb_context *, struct dn_list *,
-                     const struct dn_list *);
+static int list_union(struct ldb_context *, struct dn_list *, const struct dn_list *);
 
 /*
   return a list of dn's that might match a simple indexed search on
@@ -335,8 +329,7 @@ static int ltdb_index_dn_leaf(struct ldb_module *module,
                              struct dn_list *list)
 {
        if (ldb_attr_cmp(tree->u.equality.attr, LTDB_OBJECTCLASS) == 0) {
-               return ltdb_index_dn_objectclass(module, tree, index_list,
-                                                list);
+               return ltdb_index_dn_objectclass(module, tree, index_list, list);
        }
        if (ldb_attr_dn(tree->u.equality.attr) == 0) {
                list->dn = talloc_array(list, char *, 1);
@@ -344,8 +337,7 @@ static int ltdb_index_dn_leaf(struct ldb_module *module,
                        ldb_oom(module->ldb);
                        return -1;
                }
-               list->dn[0] = talloc_strdup(
-                       list, (char *)tree->u.equality.value.data);
+               list->dn[0] = talloc_strdup(list->dn, (char *)tree->u.equality.value.data);
                if (list->dn[0] == NULL) {
                        ldb_oom(module->ldb);
                        return -1;
@@ -388,8 +380,7 @@ static int list_intersect(struct ldb_context *ldb,
        for (i=0;i<list->count;i++) {
                if (ldb_list_find(list->dn[i], list2->dn, list2->count, 
                              sizeof(char *), (comparison_fn_t)strcmp) != -1) {
-                       list3->dn[list3->count] = talloc_move(
-                               list3->dn, &list->dn[i]);
+                       list3->dn[list3->count] = talloc_move(list3->dn, &list->dn[i]);
                        list3->count++;
                } else {
                        talloc_free(list->dn[i]);
@@ -431,8 +422,7 @@ static int list_union(struct ldb_context *ldb,
        for (i=0;i<list2->count;i++) {
                if (ldb_list_find(list2->dn[i], list->dn, count, 
                              sizeof(char *), (comparison_fn_t)strcmp) == -1) {
-                       list->dn[list->count] = talloc_strdup(
-                               list->dn, list2->dn[i]);
+                       list->dn[list->count] = talloc_strdup(list->dn, list2->dn[i]);
                        if (!list->dn[list->count]) {
                                return -1;
                        }
@@ -441,8 +431,7 @@ static int list_union(struct ldb_context *ldb,
        }
 
        if (list->count != count) {
-               qsort(list->dn, list->count, sizeof(char *),
-                     (comparison_fn_t)list_cmp);
+               qsort(list->dn, list->count, sizeof(char *), (comparison_fn_t)list_cmp);
        }
 
        return 0;
@@ -479,8 +468,7 @@ static int ltdb_index_dn_or(struct ldb_module *module,
                        return -1;
                }
 
-               v = ltdb_index_dn(module, tree->u.list.elements[i],
-                                 index_list, list2);
+               v = ltdb_index_dn(module, tree->u.list.elements[i], index_list, list2);
 
                if (v == 0) {
                        /* 0 || X == X */
@@ -564,8 +552,7 @@ static int ltdb_index_dn_and(struct ldb_module *module,
                        return -1;
                }
 
-               v = ltdb_index_dn(module, tree->u.list.elements[i],
-                                 index_list, list2);
+               v = ltdb_index_dn(module, tree->u.list.elements[i], index_list, list2);
 
                if (v == 0) {
                        /* 0 && X == 0 */
@@ -651,8 +638,7 @@ static int ltdb_index_dn(struct ldb_module *module,
 static int ltdb_index_filter(const struct dn_list *dn_list, 
                             struct ldb_handle *handle)
 {
-       struct ltdb_context *ac = talloc_get_type(handle->private_data,
-                                                 struct ltdb_context);
+       struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
        struct ldb_reply *ares = NULL;
        unsigned int i;
 
@@ -700,8 +686,7 @@ static int ltdb_index_filter(const struct dn_list *dn_list,
                        return LDB_ERR_OPERATIONS_ERROR;
                }
 
-               if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree,
-                                  ac->base, ac->scope)) {
+               if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree, ac->base, ac->scope)) {
                        talloc_free(ares);
                        continue;
                }
@@ -718,8 +703,7 @@ static int ltdb_index_filter(const struct dn_list *dn_list,
 
                ares->type = LDB_REPLY_ENTRY;
                handle->state = LDB_ASYNC_PENDING;
-               handle->status = ac->callback(ac->module->ldb, ac->context,
-                                             ares);
+               handle->status = ac->callback(ac->module->ldb, ac->context, ares);
 
                if (handle->status != LDB_SUCCESS) {
                        handle->state = LDB_ASYNC_DONE;
@@ -775,8 +759,7 @@ int ltdb_search_indexed(struct ldb_handle *handle)
                dn_list->count = 1;
                ret = 1;
        } else {
-               ret = ltdb_index_dn(ac->module, ac->tree,
-                                   ltdb->cache->indexlist, dn_list);
+               ret = ltdb_index_dn(ac->module, ac->tree, ltdb->cache->indexlist, dn_list);
        }
 
        if (ret == 1) {
@@ -804,26 +787,23 @@ static int ltdb_index_add1_new(struct ldb_context *ldb,
 
        /* add another entry */
        el2 = talloc_realloc(msg, msg->elements, 
-                            struct ldb_message_element, msg->num_elements+1);
+                              struct ldb_message_element, msg->num_elements+1);
        if (!el2) {
                return -1;
        }
 
        msg->elements = el2;
-       msg->elements[msg->num_elements].name = talloc_strdup(msg->elements,
-                                                             LTDB_IDX);
+       msg->elements[msg->num_elements].name = talloc_strdup(msg->elements, LTDB_IDX);
        if (!msg->elements[msg->num_elements].name) {
                return -1;
        }
        msg->elements[msg->num_elements].num_values = 0;
-       msg->elements[msg->num_elements].values = talloc(msg->elements,
-                                                        struct ldb_val);
+       msg->elements[msg->num_elements].values = talloc(msg->elements, struct ldb_val);
        if (!msg->elements[msg->num_elements].values) {
                return -1;
        }
        msg->elements[msg->num_elements].values[0].length = strlen(dn);
-       msg->elements[msg->num_elements].values[0].data =
-               discard_const_p(uint8_t, dn);
+       msg->elements[msg->num_elements].values[0].data = discard_const_p(uint8_t, dn);
        msg->elements[msg->num_elements].num_values = 1;
        msg->num_elements++;
 
@@ -846,8 +826,7 @@ static int ltdb_index_add1_add(struct ldb_context *ldb,
 
        /* for multi-valued attributes we can end up with repeats */
        for (i=0;i<msg->elements[idx].num_values;i++) {
-               if (strcmp(dn,
-                          (char *)msg->elements[idx].values[i].data) == 0) {
+               if (strcmp(dn, (char *)msg->elements[idx].values[i].data) == 0) {
                        return 0;
                }
        }
@@ -860,10 +839,8 @@ static int ltdb_index_add1_add(struct ldb_context *ldb,
        }
        msg->elements[idx].values = v2;
 
-       msg->elements[idx].values[msg->elements[idx].num_values].length =
-               strlen(dn);
-       msg->elements[idx].values[msg->elements[idx].num_values].data =
-               discard_const_p(uint8_t, dn);
+       msg->elements[idx].values[msg->elements[idx].num_values].length = strlen(dn);
+       msg->elements[idx].values[msg->elements[idx].num_values].data = discard_const_p(uint8_t, dn);
        msg->elements[idx].num_values++;
 
        return 0;
@@ -946,8 +923,8 @@ static int ltdb_index_add0(struct ldb_module *module, const char *dn,
        }
 
        for (i = 0; i < num_el; i++) {
-               ret = ldb_msg_find_idx(ltdb->cache->indexlist,
-                                      elements[i].name, NULL, LTDB_IDXATTR);
+               ret = ldb_msg_find_idx(ltdb->cache->indexlist, elements[i].name, 
+                                      NULL, LTDB_IDXATTR);
                if (ret == -1) {
                        continue;
                }
@@ -1020,8 +997,8 @@ int ltdb_index_del_value(struct ldb_module *module, const char *dn,
        }
 
        if (ret == 0) {
-               /* it wasn't indexed. Did we have an earlier error? If we did
-                  then its gone now */
+               /* it wasn't indexed. Did we have an earlier error? If we did then
+                  its gone now */
                talloc_free(dn_key);
                return 0;
        }
@@ -1083,15 +1060,13 @@ int ltdb_index_del(struct ldb_module *module, const struct ldb_message *msg)
        }
 
        for (i = 0; i < msg->num_elements; i++) {
-               ret = ldb_msg_find_idx(ltdb->cache->indexlist,
-                                      msg->elements[i].name, NULL,
-                                      LTDB_IDXATTR);
+               ret = ldb_msg_find_idx(ltdb->cache->indexlist, msg->elements[i].name, 
+                                      NULL, LTDB_IDXATTR);
                if (ret == -1) {
                        continue;
                }
                for (j = 0; j < msg->elements[i].num_values; j++) {
-                       ret = ltdb_index_del_value(module, dn,
-                                                  &msg->elements[i], j);
+                       ret = ltdb_index_del_value(module, dn, &msg->elements[i], j);
                        if (ret == -1) {
                                talloc_free(dn);
                                return -1;
@@ -1107,8 +1082,7 @@ int ltdb_index_del(struct ldb_module *module, const struct ldb_message *msg)
 /*
   traversal function that deletes all @INDEX records
 */
-static int delete_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
-                       void *state)
+static int delete_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
 {
        const char *dn = "DN=" LTDB_INDEX ":";
        if (strncmp((char *)key.dptr, dn, strlen(dn)) == 0) {
@@ -1120,8 +1094,7 @@ static int delete_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
 /*
   traversal function that adds @INDEX records during a re index
 */
-static int re_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
-                   void *state)
+static int re_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
 {
        struct ldb_module *module = (struct ldb_module *)state;
        struct ldb_message *msg;
@@ -1150,9 +1123,8 @@ static int re_index(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
        key2 = ltdb_key(module, msg->dn);
        if (key2.dptr == NULL) {
                /* probably a corrupt record ... darn */
-               ldb_debug(module->ldb, LDB_DEBUG_ERROR,
-                         "Invalid DN in re_index: %s\n",
-                         ldb_dn_linearize(msg, msg->dn));
+               ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s\n",
+                                                       ldb_dn_linearize(msg, msg->dn));
                talloc_free(msg);
                return 0;
        }