s4-ldb: use TYPESAFE_QSORT() in the rest of the ldb code
[ira/wip.git] / source4 / lib / ldb / common / ldb_dn.c
index f11ccf35d752b4287573f604798c9acc3e1f9d97..c395be29007813ad365114422f0152f94cc3f5fd 100644 (file)
@@ -103,7 +103,13 @@ struct ldb_dn *ldb_dn_from_ldb_val(void *mem_ctx,
        dn = talloc_zero(mem_ctx, struct ldb_dn);
        LDB_DN_NULL_FAILED(dn);
 
-       dn->ldb = ldb;
+       dn->ldb = talloc_get_type(ldb, struct ldb_context);
+       if (dn->ldb == NULL) {
+               /* the caller probably got the arguments to
+                  ldb_dn_new() mixed up */
+               talloc_free(dn);
+               return NULL;
+       }
 
        if (strdn->data && strdn->length) {
                const char *data = (const char *)strdn->data;
@@ -818,8 +824,8 @@ char *ldb_dn_get_extended_linearized(void *mem_ctx, struct ldb_dn *dn, int mode)
         * the resulting DNs consistent, plus to ensure that we put
         * 'DELETED' first, so it can be very quickly recognised
         */
-       qsort(dn->ext_components, dn->ext_comp_num, sizeof(dn->ext_components[0]),
-             ldb_dn_extended_component_compare);
+       TYPESAFE_QSORT(dn->ext_components, dn->ext_comp_num,
+                      ldb_dn_extended_component_compare);
 
        for (i = 0; i < dn->ext_comp_num; i++) {
                const struct ldb_dn_extended_syntax *ext_syntax;
@@ -2001,3 +2007,26 @@ bool ldb_dn_is_null(struct ldb_dn *dn)
        return false;
 }
 
+/*
+  this updates dn->components, taking the components from ref_dn.
+  This is used by code that wants to update the DN path of a DN
+  while not impacting on the extended DN components
+ */
+int ldb_dn_update_components(struct ldb_dn *dn, const struct ldb_dn *ref_dn)
+{
+       dn->components = talloc_realloc(dn, dn->components,
+                                       struct ldb_dn_component, ref_dn->comp_num);
+       if (!dn->components) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       memcpy(dn->components, ref_dn->components,
+              sizeof(struct ldb_dn_component)*ref_dn->comp_num);
+       dn->comp_num = ref_dn->comp_num;
+
+       talloc_free(dn->linearized);
+       talloc_free(dn->ext_linearized);
+       dn->ext_linearized = NULL;
+       dn->linearized = NULL;
+
+       return LDB_SUCCESS;
+}