LDB:asq module - change counters to "unsigned" where appropriate
[Samba/cd1.git] / source4 / lib / ldb / tools / ldbdel.c
blob04884e1becb6df87f5db2389b2e28178a9e6c2b5
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
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
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.
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.
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/>.
25 * Name: ldb
27 * Component: ldbdel
29 * Description: utility to delete records - modelled on ldapdelete
31 * Author: Andrew Tridgell
34 #include "ldb.h"
35 #include "tools/cmdline.h"
36 #include "ldbutil.h"
37 #include "replace.h"
39 static int dn_cmp(struct ldb_message **msg1, struct ldb_message **msg2)
41 return ldb_dn_compare((*msg1)->dn, (*msg2)->dn);
44 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
46 int ret;
47 unsigned int i, total=0;
48 const char *attrs[] = { NULL };
49 struct ldb_result *res;
51 ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
52 if (ret != LDB_SUCCESS) return -1;
54 /* sort the DNs, deepest first */
55 TYPESAFE_QSORT(res->msgs, res->count, dn_cmp);
57 for (i = 0; i < res->count; i++) {
58 if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == 0) {
59 total++;
60 } else {
61 printf("Failed to delete '%s' - %s\n",
62 ldb_dn_get_linearized(res->msgs[i]->dn),
63 ldb_errstring(ldb));
67 talloc_free(res);
69 if (total == 0) {
70 return -1;
72 printf("Deleted %d records\n", total);
73 return 0;
76 static void usage(void)
78 printf("Usage: ldbdel <options> <DN...>\n");
79 printf("Deletes records from a ldb\n\n");
80 ldb_cmdline_help("ldbdel", stdout);
81 exit(1);
84 int main(int argc, const char **argv)
86 struct ldb_control **req_ctrls;
87 struct ldb_cmdline *options;
88 struct ldb_context *ldb;
89 int ret = 0, i;
91 ldb = ldb_init(NULL, NULL);
93 options = ldb_cmdline_process(ldb, argc, argv, usage);
95 if (options->argc < 1) {
96 usage();
97 exit(1);
100 req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
101 if (options->controls != NULL && req_ctrls== NULL) {
102 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
103 return -1;
106 for (i=0;i<options->argc;i++) {
107 struct ldb_dn *dn;
109 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
110 if ( ! ldb_dn_validate(dn)) {
111 printf("Invalid DN format\n");
112 exit(1);
114 if (options->recursive) {
115 ret = ldb_delete_recursive(ldb, dn,req_ctrls);
116 } else {
117 ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
118 if (ret == 0) {
119 printf("Deleted 1 record\n");
122 if (ret != 0) {
123 printf("delete of '%s' failed - (%s) %s\n",
124 ldb_dn_get_linearized(dn),
125 ldb_strerror(ret),
126 ldb_errstring(ldb));
130 talloc_free(ldb);
132 return ret;