1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-27 03:21:53 +03:00

s4-ldb: fixed ldbdel with -r (recursive deletion)

We need to delete the deepest DNs first
This commit is contained in:
Andrew Tridgell 2009-12-08 14:40:20 +11:00
parent b2d8e1118d
commit ff984cdfac

View File

@ -34,6 +34,14 @@
#include "ldb.h"
#include "tools/cmdline.h"
static int dn_cmp(const void *p1, const void *p2)
{
const struct ldb_message *msg1, *msg2;
msg1 = *(const struct ldb_message * const *)p1;
msg2 = *(const struct ldb_message * const *)p2;
return ldb_dn_compare(msg1->dn, msg2->dn);
}
static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
{
int ret, i, total=0;
@ -43,9 +51,16 @@ static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
if (ret != LDB_SUCCESS) return -1;
/* sort the DNs, deepest first */
qsort(res->msgs, res->count, sizeof(res->msgs[0]), dn_cmp);
for (i = 0; i < res->count; i++) {
if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
total++;
} else {
printf("Failed to delete '%s' - %s\n",
ldb_dn_get_linearized(res->msgs[i]->dn),
ldb_errstring(ldb));
}
}