show the full set of command line options for ldb tools
[Samba/aatanasov.git] / source4 / lib / ldb / tools / ldbdel.c
blobddf168d5746321bf872843a59bf5bb47d499cd9d
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"
37 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
39 int ret, i, total=0;
40 const char *attrs[] = { NULL };
41 struct ldb_result *res;
43 ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
44 if (ret != LDB_SUCCESS) return -1;
46 for (i = 0; i < res->count; i++) {
47 if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
48 total++;
52 talloc_free(res);
54 if (total == 0) {
55 return -1;
57 printf("Deleted %d records\n", total);
58 return 0;
61 static void usage(void)
63 printf("Usage: ldbdel <options> <DN...>\n");
64 printf("Deletes records from a ldb\n\n");
65 ldb_cmdline_help("ldbdel", stdout);
66 exit(1);
69 int main(int argc, const char **argv)
71 struct ldb_context *ldb;
72 int ret = 0, i;
73 struct ldb_cmdline *options;
75 ldb = ldb_init(NULL, NULL);
77 options = ldb_cmdline_process(ldb, argc, argv, usage);
79 if (options->argc < 1) {
80 usage();
81 exit(1);
84 for (i=0;i<options->argc;i++) {
85 struct ldb_dn *dn;
87 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
88 if ( ! ldb_dn_validate(dn)) {
89 printf("Invalid DN format\n");
90 exit(1);
92 if (options->recursive) {
93 ret = ldb_delete_recursive(ldb, dn);
94 } else {
95 ret = ldb_delete(ldb, dn);
96 if (ret == 0) {
97 printf("Deleted 1 record\n");
100 if (ret != 0) {
101 printf("delete of '%s' failed - %s\n",
102 ldb_dn_get_linearized(dn),
103 ldb_errstring(ldb));
107 talloc_free(ldb);
109 return ret;