s4-ldb: Implement ldb_msg_diff_ex() function to accept a memory context from client
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_msg.c
index 9f90f3340562d8676c77a4403b09bb60a5a0a5b9..e54caa8811a31cc6b7df9f38b8cc46e3aca3d629 100644 (file)
@@ -160,7 +160,7 @@ int ldb_msg_add(struct ldb_message *msg,
        /* We have to copy this, just in case *el is a pointer into
         * what ldb_msg_add_empty() is about to realloc() */
        struct ldb_message_element el_copy = *el;
-       if (ldb_msg_add_empty(msg, el->name, flags, NULL) != 0) {
+       if (ldb_msg_add_empty(msg, el->name, flags, NULL) != LDB_SUCCESS) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
@@ -237,7 +237,7 @@ int ldb_msg_add_string(struct ldb_message *msg,
        val.length = strlen(str);
 
        if (val.length == 0) {
-               /* allow empty strings as non-existant attributes */
+               /* allow empty strings as non-existent attributes */
                return LDB_SUCCESS;
        }
 
@@ -255,6 +255,11 @@ int ldb_msg_add_steal_string(struct ldb_message *msg,
        val.data = (uint8_t *)str;
        val.length = strlen(str);
 
+       if (val.length == 0) {
+               /* allow empty strings as non-existent attributes */
+               return LDB_SUCCESS;
+       }
+
        return ldb_msg_add_steal_value(msg, attr_name, &val);
 }
 
@@ -294,7 +299,7 @@ int ldb_msg_add_fmt(struct ldb_message *msg,
 
 /*
   compare two ldb_message_element structures
-  assumes case senistive comparison
+  assumes case sensitive comparison
 */
 int ldb_msg_element_compare(struct ldb_message_element *el1, 
                            struct ldb_message_element *el2)
@@ -464,8 +469,8 @@ struct ldb_dn *ldb_msg_find_attr_as_dn(struct ldb_context *ldb,
 */
 void ldb_msg_sort_elements(struct ldb_message *msg)
 {
-       qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element), 
-             (comparison_fn_t)ldb_msg_element_compare_name);
+       TYPESAFE_QSORT(msg->elements, msg->num_elements,
+                      ldb_msg_element_compare_name);
 }
 
 /*
@@ -476,7 +481,7 @@ struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx,
                                         const struct ldb_message *msg)
 {
        struct ldb_message *msg2;
-       int i;
+       unsigned int i;
 
        msg2 = talloc(mem_ctx, struct ldb_message);
        if (msg2 == NULL) return NULL;
@@ -506,7 +511,7 @@ struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx,
                                 const struct ldb_message *msg)
 {
        struct ldb_message *msg2;
-       int i, j;
+       unsigned int i, j;
 
        msg2 = ldb_msg_copy_shallow(mem_ctx, msg);
        if (msg2 == NULL) return NULL;
@@ -542,7 +547,7 @@ failed:
 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
                                         const struct ldb_message *msg)
 {
-       int i;
+       unsigned int i;
        struct ldb_message *msg2;
 
        msg2 = ldb_msg_copy(ldb, msg);
@@ -637,10 +642,102 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
        return mod;
 }
 
+/**
+ * return a ldb_message representing the differences between msg1 and msg2.
+ * If you then use this in a ldb_modify() call it can be used to save edits to a message
+ *
+ * Result message is constructed as follows:
+ * - LDB_FLAG_MOD_ADD     - elements found only in msg2
+ * - LDB_FLAG_MOD_REPLACE - elements in msg2 that have different value in msg1
+ *                          Value for msg2 element is used
+ * - LDB_FLAG_MOD_DELETE  - elements found only in msg2
+ *
+ * @return LDB_SUCCESS or LDB_ERR_OPERATIONS_ERROR
+ */
+int ldb_msg_diff_ex(struct ldb_context *ldb,
+                   struct ldb_message *msg1,
+                   struct ldb_message *msg2,
+                   TALLOC_CTX *mem_ctx,
+                   struct ldb_message **_msg_out)
+{
+       int ldb_res;
+       unsigned int i;
+       struct ldb_message *mod;
+       struct ldb_message_element *el;
+       TALLOC_CTX *temp_ctx;
+
+       temp_ctx = talloc_new(mem_ctx);
+       if (!temp_ctx) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       mod = ldb_msg_new(temp_ctx);
+       if (mod == NULL) {
+               goto failed;
+       }
+
+       mod->dn = msg1->dn;
+       mod->num_elements = 0;
+       mod->elements = NULL;
+
+       /* canonicalize msg2 so we have no
+        * repeated elements */
+       msg2 = ldb_msg_canonicalize(ldb, msg2);
+       if (msg2 == NULL) {
+               goto failed;
+       }
+
+       /* steal msg2 into mod context as it is
+        * allocated in ldb's context */
+       talloc_steal(mod, msg2);
+
+       /* look in msg2 to find elements that need to be added
+          or modified */
+       for (i=0;i<msg2->num_elements;i++) {
+               el = ldb_msg_find_element(msg1, msg2->elements[i].name);
+
+               if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
+                       continue;
+               }
+
+               ldb_res = ldb_msg_add(mod,
+                                     &msg2->elements[i],
+                                     el ? LDB_FLAG_MOD_REPLACE : LDB_FLAG_MOD_ADD);
+               if (ldb_res != LDB_SUCCESS) {
+                       goto failed;
+               }
+       }
+
+       /* look in msg1 to find elements that need to be deleted */
+       for (i=0;i<msg1->num_elements;i++) {
+               el = ldb_msg_find_element(msg2, msg1->elements[i].name);
+               if (el == NULL) {
+                       ldb_res = ldb_msg_add_empty(mod,
+                                                   msg1->elements[i].name,
+                                                   LDB_FLAG_MOD_DELETE, NULL);
+                       if (ldb_res != LDB_SUCCESS) {
+                               goto failed;
+                       }
+               }
+       }
+
+       /* steal resulting message into supplied context */
+       talloc_steal(mem_ctx, mod);
+       *_msg_out = mod;
+
+       talloc_free(temp_ctx);
+       return LDB_SUCCESS;
+
+failed:
+       talloc_free(temp_ctx);
+       return LDB_ERR_OPERATIONS_ERROR;
+}
+
+
 int ldb_msg_sanity_check(struct ldb_context *ldb, 
                         const struct ldb_message *msg)
 {
-       int i, j;
+       unsigned int i, j;
 
        /* basic check on DN */
        if (msg->dn == NULL) {
@@ -678,7 +775,8 @@ int ldb_msg_sanity_check(struct ldb_context *ldb,
 const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
 {
        const char **ret;
-       int i;
+       unsigned int i;
+
        for (i=0;attrs && attrs[i];i++) /* noop */ ;
        ret = talloc_array(mem_ctx, const char *, i+1);
        if (ret == NULL) {
@@ -699,8 +797,9 @@ const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
 const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr)
 {
        const char **ret;
-       int i;
+       unsigned int i;
        bool found = false;
+
        for (i=0;attrs && attrs[i];i++) {
                if (ldb_attr_cmp(attrs[i], new_attr) == 0) {
                        found = true;
@@ -727,7 +826,7 @@ const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *att
 */
 int ldb_attr_in_list(const char * const *attrs, const char *attr)
 {
-       int i;
+       unsigned int i;
        for (i=0;attrs && attrs[i];i++) {
                if (ldb_attr_cmp(attrs[i], attr) == 0) {
                        return 1;
@@ -774,7 +873,7 @@ int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *rep
 */
 void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element *el)
 {
-       int n = (el - msg->elements);
+       ptrdiff_t n = (el - msg->elements);
        if (n >= msg->num_elements) {
                /* should we abort() here? */
                return;
@@ -791,8 +890,9 @@ void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element
 */
 void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
 {
-       struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
-       if (el) {
+       struct ldb_message_element *el;
+
+       while ((el = ldb_msg_find_element(msg, attr)) != NULL) {
                ldb_msg_remove_element(msg, el);
        }
 }
@@ -936,7 +1036,7 @@ time_t ldb_string_utc_to_time(const char *s)
 */
 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f)
 {
-       int i;
+       unsigned int i;
 
        for (i = 0; i < result->count; i++) {
                struct ldb_ldif ldif;