LDB:sort module - change counters to "unsigned" where appropriate
[ira/wip.git] / source4 / lib / ldb / modules / sort.c
index 2b2a1ab1e3672a0069ed9cc2043640f0ee4b8106..f7c381f163a39b6e201b581e0ff2cf6214510d13 100644 (file)
@@ -44,15 +44,15 @@ struct opaque {
 struct sort_context {
        struct ldb_module *module;
 
-       char *attributeName;
-       char *orderingRule;
+       const char *attributeName;
+       const char *orderingRule;
        int reverse;
 
        struct ldb_request *req;
        struct ldb_message **msgs;
        char **referrals;
-       int num_msgs;
-       int num_refs;
+       unsigned int num_msgs;
+       unsigned int num_refs;
 
        const struct ldb_schema_attribute *a;
        int sort_result;
@@ -62,7 +62,7 @@ static int build_response(void *mem_ctx, struct ldb_control ***ctrls, int result
 {
        struct ldb_control **controls;
        struct ldb_sort_resp_control *resp;
-       int i;
+       unsigned int i;
 
        if (*ctrls) {
                controls = *ctrls;
@@ -108,7 +108,7 @@ static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, vo
 
        ldb = ldb_module_get_ctx(ac->module);
 
-       if (!ac || ac->sort_result != 0) {
+       if (ac->sort_result != 0) {
                /* an error occurred previously,
                 * let's exit the sorting by returning always 0 */
                return 0;
@@ -117,10 +117,13 @@ static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, vo
        el1 = ldb_msg_find_element(*msg1, ac->attributeName);
        el2 = ldb_msg_find_element(*msg2, ac->attributeName);
 
-       if (!el1 || !el2) {
-               /* the attribute was not found return and
-                * set an error */
-               ac->sort_result = LDB_ERR_UNWILLING_TO_PERFORM;
+       if (!el1 && el2) {
+               return 1;
+       }
+       if (el1 && !el2) {
+               return -1;
+       }
+       if (!el1 && !el2) {
                return 0;
        }
 
@@ -134,16 +137,15 @@ static int server_sort_results(struct sort_context *ac)
 {
        struct ldb_context *ldb;
        struct ldb_reply *ares;
-       int i, ret;
+       unsigned int i;
+       int ret;
 
        ldb = ldb_module_get_ctx(ac->module);
 
        ac->a = ldb_schema_attribute_by_name(ldb, ac->attributeName);
        ac->sort_result = 0;
 
-       ldb_qsort(ac->msgs, ac->num_msgs,
-                 sizeof(struct ldb_message *),
-                 ac, (ldb_qsort_cmp_fn_t)sort_compare);
+       LDB_TYPESAFE_QSORT(ac->msgs, ac->num_msgs, ac, sort_compare);
 
        if (ac->sort_result != LDB_SUCCESS) {
                return ac->sort_result;
@@ -255,7 +257,7 @@ static int server_sort_search(struct ldb_module *module, struct ldb_request *req
 
        ldb = ldb_module_get_ctx(module);
 
-       /* check if there's a paged request control */
+       /* check if there's a server sort control */
        control = ldb_request_get_control(req, LDB_CONTROL_SERVER_SORT_OID);
        if (control == NULL) {
                /* not found go on */
@@ -312,7 +314,7 @@ static int server_sort_search(struct ldb_module *module, struct ldb_request *req
                                        server_sort_search_callback,
                                        req);
        if (ret != LDB_SUCCESS) {
-               return LDB_ERR_OPERATIONS_ERROR;
+               return ret;
        }
 
        /* save it locally and remove it from the list */
@@ -336,7 +338,7 @@ static int server_sort_init(struct ldb_module *module)
        if (ret != LDB_SUCCESS) {
                ldb_debug(ldb, LDB_DEBUG_WARNING,
                        "server_sort:"
-                       "Unable to register control with rootdse!\n");
+                       "Unable to register control with rootdse!");
        }
 
        return ldb_next_init(module);