s4-ldb: Add ldb_msg_normalize() to accept a memory context from client
authorKamen Mazdrashki <kamenim@samba.org>
Fri, 16 Jul 2010 10:55:42 +0000 (13:55 +0300)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 19 Jul 2010 07:33:34 +0000 (17:33 +1000)
Previos implementation from ldb_msg_canonicalize()
was moved into this function and now ldb_msg_canonicalize()
is based on ldb_msg_normalize()

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
source4/lib/ldb/common/ldb_msg.c
source4/lib/ldb/include/ldb.h

index a2a1ba8110101ee235ecf506f7b971bc64893123..a6430046051af72c9cbda6e50bfeb0fa8a7f2d12 100644 (file)
@@ -577,36 +577,64 @@ failed:
 }
 
 
-/*
 canonicalise a message, merging elements of the same name
-*/
+/**
* Canonicalize a message, merging elements of the same name
+ */
 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
                                         const struct ldb_message *msg)
+{
+       int ret;
+       struct ldb_message *msg2;
+
+       /*
+        * Preserve previous behavior and allocate
+        * *msg2 into *ldb context
+        */
+       ret = ldb_msg_normalize(ldb, ldb, msg, &msg2);
+       if (ret != LDB_SUCCESS) {
+               return NULL;
+       }
+
+       return msg2;
+}
+
+/**
+ * Canonicalize a message, merging elements of the same name
+ */
+int ldb_msg_normalize(struct ldb_context *ldb,
+                     TALLOC_CTX *mem_ctx,
+                     const struct ldb_message *msg,
+                     struct ldb_message **_msg_out)
 {
        unsigned int i;
        struct ldb_message *msg2;
 
-       msg2 = ldb_msg_copy(ldb, msg);
-       if (msg2 == NULL) return NULL;
+       msg2 = ldb_msg_copy(mem_ctx, msg);
+       if (msg2 == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        ldb_msg_sort_elements(msg2);
 
-       for (i=1;i<msg2->num_elements;i++) {
+       for (i=1; i < msg2->num_elements; i++) {
                struct ldb_message_element *el1 = &msg2->elements[i-1];
                struct ldb_message_element *el2 = &msg2->elements[i];
+
                if (ldb_msg_element_compare_name(el1, el2) == 0) {
-                       el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 
-                                                      el1->num_values + el2->num_values);
+                       el1->values = talloc_realloc(msg2->elements,
+                                                    el1->values, struct ldb_val,
+                                                    el1->num_values + el2->num_values);
                        if (el1->num_values + el2->num_values > 0 && el1->values == NULL) {
-                               return NULL;
+                               talloc_free(msg2);
+                               return LDB_ERR_OPERATIONS_ERROR;
                        }
                        memcpy(el1->values + el1->num_values,
                               el2->values,
                               sizeof(struct ldb_val) * el2->num_values);
                        el1->num_values += el2->num_values;
                        talloc_free(discard_const_p(char, el2->name));
-                       if (i+1<msg2->num_elements) {
-                               memmove(el2, el2+1, sizeof(struct ldb_message_element) * 
+                       if ((i+1) < msg2->num_elements) {
+                               memmove(el2, el2+1, sizeof(struct ldb_message_element) *
                                        (msg2->num_elements - (i+1)));
                        }
                        msg2->num_elements--;
@@ -614,7 +642,8 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
                }
        }
 
-       return msg2;
+       *_msg_out = msg2;
+       return LDB_SUCCESS;
 }
 
 
index 667c91b34d06235bcd94db3f7dbaf08fab17c628..065aa31682d42f32456872d7f956710dd7f3284b 100644 (file)
@@ -1854,6 +1854,11 @@ struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
                                         const struct ldb_message *msg);
 
+int ldb_msg_normalize(struct ldb_context *ldb,
+                     TALLOC_CTX *mem_ctx,
+                     const struct ldb_message *msg,
+                     struct ldb_message **_msg_out);
+
 
 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 
                                 struct ldb_message *msg1,