r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / lib / ldb / tools / ldbadd.c
index 6d04800502b3b5cc5d15cdb2db04467c8e738925..c22cfde4ecea60b115857f34a0ecd9db35b44200 100644 (file)
  */
 
 #include "includes.h"
+#include "ldb/include/ldb.h"
+#include "ldb/include/ldb_errors.h"
+#include "ldb/tools/cmdline.h"
+
+#ifdef _SAMBA_BUILD_
+#include "system/filesys.h"
+#endif
 
 static int failures;
 
@@ -41,6 +48,8 @@ static void usage(void)
        printf("Usage: ldbadd <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("Adds records to a ldb, reading ldif the specified list of files\n\n");
        exit(1);
@@ -62,10 +71,12 @@ static int process_file(struct ldb_context *ldb, FILE *f)
                        break;
                }
 
-               ret = ldb_add(ldb, &ldif->msg);
-               if (ret != 0) {
+               ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);
+
+               ret = ldb_add(ldb, ldif->msg);
+               if (ret != LDB_SUCCESS) {
                        fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
-                               ldb_errstring(ldb), ldif->msg.dn);
+                               ldb_errstring(ldb), ldb_dn_linearize(ldb, ldif->msg->dn));
                        failures++;
                } else {
                        count++;
@@ -78,85 +89,33 @@ static int process_file(struct ldb_context *ldb, FILE *f)
 
 
 
- int main(int argc, char * const argv[])
+ int main(int argc, const char **argv)
 {
        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");
-
-       ldbopts = 0;
-       while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
-               switch (opt) {
-               case 'H':
-                       ldb_url = optarg;
-                       break;
-
-               case 'o':
-                       ldbopts++;
-                       if (options == NULL) {
-                               options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
-                       } else {
-                               options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
-                               if (options == NULL) {
-                                       fprintf(stderr, "Out of memory!\n");
-                                       exit(-1);
-                               }
+       int i, count=0;
+       struct ldb_cmdline *options;
+
+       ldb = ldb_init(NULL);
+
+       options = ldb_cmdline_process(ldb, argc, argv, usage);
+
+       if (options->argc == 0) {
+               count += process_file(ldb, stdin);
+       } else {
+               for (i=0;i<options->argc;i++) {
+                       const char *fname = options->argv[i];
+                       FILE *f;
+                       f = fopen(fname, "r");
+                       if (!f) {
+                               perror(fname);
+                               exit(1);
                        }
-                       options[ldbopts - 1] = optarg;
-                       options[ldbopts] = NULL;
-                       break;
-
-               case 'h':
-               default:
-                       usage();
-                       break;
-               }
-       }
-
-       if (!ldb_url) {
-               fprintf(stderr, "You must specify a ldb URL\n\n");
-               usage();
-       }
-
-       argc -= optind;
-       argv += optind;
-
-       ldb = ldb_connect(ldb_url, 0, options);
-
-       if (!ldb) {
-               perror("ldb_connect");
-               exit(1);
-       }
-
-       ldb_set_debug_stderr(ldb);
-
-       if (argc == 0) {
-               usage();
-       }
-
-       for (i=0;i<argc;i++) {
-               FILE *f;
-               if (strcmp(argv[i],"-") == 0) {
-                       f = stdin;
-               } else {
-                       f = fopen(argv[i], "r");
-               }
-               if (!f) {
-                       perror(argv[i]);
-                       exit(1);
-               }
-               count += process_file(ldb, f);
-               if (f != stdin) {
+                       count += process_file(ldb, f);
                        fclose(f);
                }
        }
 
-       ldb_close(ldb);
+       talloc_free(ldb);
 
        printf("Added %d records with %d failures\n", count, failures);