Print error strings when transactions fail in ldb tools
[kai/samba.git] / source4 / lib / ldb / tools / ldbedit.c
index 429febb75b04775fe90fda7337afbe4852440fcd..1a684c5c2db4d0c035efd6ffa33d0aaaa536288c 100644 (file)
@@ -10,7 +10,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,8 +18,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -32,9 +31,8 @@
  *  Author: Andrew Tridgell
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
-#include "ldb/tools/cmdline.h"
+#include "ldb_includes.h"
+#include "tools/cmdline.h"
 
 static struct ldb_cmdline *options;
 
@@ -78,7 +76,7 @@ static int modify_record(struct ldb_context *ldb,
 
        if (ldb_modify(ldb, mod) != 0) {
                fprintf(stderr, "failed to modify %s - %s\n", 
-                       ldb_dn_linearize(ldb, msg1->dn), ldb_errstring(ldb));
+                       ldb_dn_get_linearized(msg1->dn), ldb_errstring(ldb));
                return -1;
        }
 
@@ -114,6 +112,11 @@ static int merge_edits(struct ldb_context *ldb,
        int ret = 0;
        int adds=0, modifies=0, deletes=0;
 
+       if (ldb_transaction_start(ldb) != 0) {
+               fprintf(stderr, "Failed to start transaction: %s\n", ldb_errstring(ldb));
+               return -1;
+       }
+
        /* do the adds and modifies */
        for (i=0;i<count2;i++) {
                msg = msg_find(ldb, msgs1, count1, msgs2[i]->dn);
@@ -123,7 +126,7 @@ static int merge_edits(struct ldb_context *ldb,
                        }
                        if (ldb_add(ldb, msgs2[i]) != 0) {
                                fprintf(stderr, "failed to add %s - %s\n",
-                                       ldb_dn_linearize(ldb, msgs2[i]->dn),
+                                       ldb_dn_get_linearized(msgs2[i]->dn),
                                        ldb_errstring(ldb));
                                return -1;
                        }
@@ -144,7 +147,7 @@ static int merge_edits(struct ldb_context *ldb,
                        }
                        if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
                                fprintf(stderr, "failed to delete %s - %s\n",
-                                       ldb_dn_linearize(ldb, msgs1[i]->dn),
+                                       ldb_dn_get_linearized(msgs1[i]->dn),
                                        ldb_errstring(ldb));
                                return -1;
                        }
@@ -152,6 +155,11 @@ static int merge_edits(struct ldb_context *ldb,
                }
        }
 
+       if (ldb_transaction_commit(ldb) != 0) {
+               fprintf(stderr, "Failed to commit transaction: %s\n", ldb_errstring(ldb));
+               return -1;
+       }
+
        printf("# %d adds  %d modifies  %d deletes\n", adds, modifies, deletes);
 
        return ret;
@@ -281,9 +289,7 @@ int main(int argc, const char **argv)
        const char *expression = "(|(objectClass=*)(distinguishedName=*))";
        const char * const * attrs = NULL;
 
-       ldb_global_init();
-
-       ldb = ldb_init(NULL);
+       ldb = ldb_init(NULL, NULL);
 
        options = ldb_cmdline_process(ldb, argc, argv, usage);
 
@@ -307,7 +313,7 @@ int main(int argc, const char **argv)
                }
        }
 
-       ret = ldb_search(ldb, basedn, options->scope, expression, attrs, &result);
+       ret = ldb_search(ldb, ldb, &result, basedn, options->scope, attrs, "%s", expression);
        if (ret != LDB_SUCCESS) {
                printf("search failed - %s\n", ldb_errstring(ldb));
                exit(1);