ldb:ldb tools - remove a superflous "return" (usage internally calls "exit")
[nivanova/samba-autobuild/.git] / source4 / lib / ldb / tools / ldbdel.c
index 22d4aa69769abac5aab2171c295e976cd4d8d6bf..35d01377083c8399a14a319e79db50eda73b5130 100644 (file)
  *  Author: Andrew Tridgell
  */
 
-#include "ldb_includes.h"
+#include "replace.h"
+#include "ldb.h"
 #include "tools/cmdline.h"
+#include "ldbutil.h"
 
-static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
+static int dn_cmp(struct ldb_message **msg1, struct ldb_message **msg2)
 {
-       int ret, i, total=0;
+       return ldb_dn_compare((*msg1)->dn, (*msg2)->dn);
+}
+
+static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
+{
+       int ret;
+       unsigned int i, total=0;
        const char *attrs[] = { NULL };
        struct ldb_result *res;
        
        ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
-       if (ret != LDB_SUCCESS) return -1;
+       if (ret != LDB_SUCCESS) return ret;
+
+       /* sort the DNs, deepest first */
+       TYPESAFE_QSORT(res->msgs, res->count, dn_cmp);
 
        for (i = 0; i < res->count; i++) {
-               if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
+               if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == LDB_SUCCESS) {
                        total++;
+               } else {
+                       printf("Failed to delete '%s' - %s\n",
+                              ldb_dn_get_linearized(res->msgs[i]->dn),
+                              ldb_errstring(ldb));
                }
        }
 
        talloc_free(res);
 
        if (total == 0) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       printf("Deleted %d records\n", total);
-       return 0;
+       printf("Deleted %u records\n", total);
+       return LDB_SUCCESS;
 }
 
-static void usage(void)
+static void usage(struct ldb_context *ldb)
 {
        printf("Usage: ldbdel <options> <DN...>\n");
-       printf("Options:\n");
-       printf("  -r               recursively delete the given subtree\n");
-       printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
-       printf("  -o options       pass options like modules to activate\n");
-       printf("              e.g: -o modules:timestamps\n");
-       printf("\n");
        printf("Deletes records from a ldb\n\n");
-       exit(1);
+       ldb_cmdline_help(ldb, "ldbdel", stdout);
+       exit(LDB_ERR_OPERATIONS_ERROR);
 }
 
 int main(int argc, const char **argv)
 {
+       struct ldb_control **req_ctrls;
+       struct ldb_cmdline *options;
        struct ldb_context *ldb;
        int ret = 0, i;
-       struct ldb_cmdline *options;
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
 
-       ldb = ldb_init(NULL, NULL);
+       ldb = ldb_init(mem_ctx, NULL);
+       if (ldb == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        options = ldb_cmdline_process(ldb, argc, argv, usage);
 
        if (options->argc < 1) {
-               usage();
-               exit(1);
+               usage(ldb);
+       }
+
+       req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
+       if (options->controls != NULL &&  req_ctrls== NULL) {
+               printf("parsing controls failed: %s\n", ldb_errstring(ldb));
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        for (i=0;i<options->argc;i++) {
@@ -92,24 +112,25 @@ int main(int argc, const char **argv)
                dn = ldb_dn_new(ldb, ldb, options->argv[i]);
                if ( ! ldb_dn_validate(dn)) {
                        printf("Invalid DN format\n");
-                       exit(1);
+                       return LDB_ERR_INVALID_DN_SYNTAX;
                }
                if (options->recursive) {
-                       ret = ldb_delete_recursive(ldb, dn);
+                       ret = ldb_delete_recursive(ldb, dn,req_ctrls);
                } else {
-                       ret = ldb_delete(ldb, dn);
-                       if (ret == 0) {
+                       ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
+                       if (ret == LDB_SUCCESS) {
                                printf("Deleted 1 record\n");
                        }
                }
-               if (ret != 0) {
-                       printf("delete of '%s' failed - %s\n",
-                               ldb_dn_get_linearized(dn),
-                               ldb_errstring(ldb));
+               if (ret != LDB_SUCCESS) {
+                       printf("delete of '%s' failed - (%s) %s\n",
+                              ldb_dn_get_linearized(dn),
+                              ldb_strerror(ret),
+                              ldb_errstring(ldb));
                }
        }
 
-       talloc_free(ldb);
+       talloc_free(mem_ctx);
 
        return ret;
 }