r5585: LDB interfaces change:
[gd/samba-autobuild/.git] / source4 / lib / ldb / tools / ldbmodify.c
index a93c710a7a6040c9281b0ff9ebad356386baa136..78baa0e36ca88835759d607d8d3e47d5ff97f3c5 100644 (file)
  */
 
 #include "includes.h"
+#include "ldb/include/ldb.h"
+#include "ldb/include/ldb_private.h"
+
+#ifdef _SAMBA_BUILD_
+#include "system/filesys.h"
+#endif
 
 static int failures;
 
@@ -41,6 +47,8 @@ static void usage(void)
        printf("Usage: ldbmodify <options> <ldif...>\n");
        printf("Options:\n");
        printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
+       printf("  -o options       pass options like modules to activate\n");
+       printf("              e.g: -o modules:timestamps\n");
        printf("\n");
        printf("Modifies a ldb based upon ldif change records\n\n");
        exit(1);
@@ -52,29 +60,29 @@ static void usage(void)
 static int process_file(struct ldb_context *ldb, FILE *f)
 {
        struct ldb_ldif *ldif;
-       int ret, count = 0;
+       int ret = -1, count = 0;
        
-       while ((ldif = ldif_read_file(f))) {
+       while ((ldif = ldb_ldif_read_file(ldb, f))) {
                switch (ldif->changetype) {
                case LDB_CHANGETYPE_NONE:
                case LDB_CHANGETYPE_ADD:
-                       ret = ldb_add(ldb, &ldif->msg);
+                       ret = ldb_add(ldb, ldif->msg);
                        break;
                case LDB_CHANGETYPE_DELETE:
-                       ret = ldb_delete(ldb, ldif->msg.dn);
+                       ret = ldb_delete(ldb, ldif->msg->dn);
                        break;
                case LDB_CHANGETYPE_MODIFY:
-                       ret = ldb_modify(ldb, &ldif->msg);
+                       ret = ldb_modify(ldb, ldif->msg);
                        break;
                }
                if (ret != 0) {
                        fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
-                               ldb_errstring(ldb), ldif->msg.dn);
+                               ldb_errstring(ldb), ldif->msg->dn);
                        failures++;
                } else {
                        count++;
                }
-               ldif_read_free(ldif);
+               ldb_ldif_read_free(ldb, ldif);
        }
 
        return count;
@@ -85,16 +93,23 @@ static int process_file(struct ldb_context *ldb, FILE *f)
        struct ldb_context *ldb;
        int count=0;
        const char *ldb_url;
+       const char **options = NULL;
+       int ldbopts;
        int opt, i;
 
        ldb_url = getenv("LDB_URL");
 
-       while ((opt = getopt(argc, argv, "hH:")) != EOF) {
+       ldbopts = 0;
+       while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
                switch (opt) {
                case 'H':
                        ldb_url = optarg;
                        break;
 
+               case 'o':
+                       options = ldb_options_parse(options, &ldbopts, optarg);
+                       break;
+
                case 'h':
                default:
                        usage();
@@ -103,20 +118,22 @@ static int process_file(struct ldb_context *ldb, FILE *f)
        }
 
        if (!ldb_url) {
-               fprintf(stderr, "You must specify a ldb URL\n");
-               exit(1);
+               fprintf(stderr, "You must specify a ldb URL\n\n");
+               usage();
        }
 
        argc -= optind;
        argv += optind;
 
-       ldb = ldb_connect(ldb_url, 0, NULL);
+       ldb = ldb_connect(ldb_url, 0, options);
 
        if (!ldb) {
                perror("ldb_connect");
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        if (argc == 0) {
                usage();
                exit(1);
@@ -139,9 +156,13 @@ static int process_file(struct ldb_context *ldb, FILE *f)
                }
        }
 
-       ldb_close(ldb);
+       talloc_free(ldb);
 
        printf("Modified %d records with %d failures\n", count, failures);
+
+       if (failures != 0) {
+               return -1;
+       }
        
        return 0;
 }