s4-ldb: use TYPESAFE_QSORT() in the rest of the ldb code
[ira/wip.git] / source4 / lib / ldb / tools / ldbdel.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbdel
28  *
29  *  Description: utility to delete records - modelled on ldapdelete
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb.h"
35 #include "tools/cmdline.h"
36 #include "ldbutil.h"
37 #include "replace.h"
38
39 static int dn_cmp(struct ldb_message **msg1, struct ldb_message **msg2)
40 {
41         return ldb_dn_compare((*msg1)->dn, (*msg2)->dn);
42 }
43
44 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
45 {
46         int ret, i, total=0;
47         const char *attrs[] = { NULL };
48         struct ldb_result *res;
49         
50         ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
51         if (ret != LDB_SUCCESS) return -1;
52
53         /* sort the DNs, deepest first */
54         TYPESAFE_QSORT(res->msgs, res->count, dn_cmp);
55
56         for (i = 0; i < res->count; i++) {
57                 if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == 0) {
58                         total++;
59                 } else {
60                         printf("Failed to delete '%s' - %s\n",
61                                ldb_dn_get_linearized(res->msgs[i]->dn),
62                                ldb_errstring(ldb));
63                 }
64         }
65
66         talloc_free(res);
67
68         if (total == 0) {
69                 return -1;
70         }
71         printf("Deleted %d records\n", total);
72         return 0;
73 }
74
75 static void usage(void)
76 {
77         printf("Usage: ldbdel <options> <DN...>\n");
78         printf("Deletes records from a ldb\n\n");
79         ldb_cmdline_help("ldbdel", stdout);
80         exit(1);
81 }
82
83 int main(int argc, const char **argv)
84 {
85         struct ldb_control **req_ctrls;
86         struct ldb_cmdline *options;
87         struct ldb_context *ldb;
88         int ret = 0, i;
89
90         ldb = ldb_init(NULL, NULL);
91
92         options = ldb_cmdline_process(ldb, argc, argv, usage);
93
94         if (options->argc < 1) {
95                 usage();
96                 exit(1);
97         }
98
99         req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
100         if (options->controls != NULL &&  req_ctrls== NULL) {
101                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
102                 return -1;
103         }
104
105         for (i=0;i<options->argc;i++) {
106                 struct ldb_dn *dn;
107
108                 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
109                 if ( ! ldb_dn_validate(dn)) {
110                         printf("Invalid DN format\n");
111                         exit(1);
112                 }
113                 if (options->recursive) {
114                         ret = ldb_delete_recursive(ldb, dn,req_ctrls);
115                 } else {
116                         ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
117                         if (ret == 0) {
118                                 printf("Deleted 1 record\n");
119                         }
120                 }
121                 if (ret != 0) {
122                         printf("delete of '%s' failed - (%s) %s\n",
123                                ldb_dn_get_linearized(dn),
124                                ldb_strerror(ret),
125                                ldb_errstring(ldb));
126                 }
127         }
128
129         talloc_free(ldb);
130
131         return ret;
132 }