summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/tools/ldbadd.c
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib/ldb/tools/ldbadd.c')
-rw-r--r--source4/lib/ldb/tools/ldbadd.c99
1 files changed, 81 insertions, 18 deletions
diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c
index 92ed29e6b8..446c0d80f9 100644
--- a/source4/lib/ldb/tools/ldbadd.c
+++ b/source4/lib/ldb/tools/ldbadd.c
@@ -34,28 +34,28 @@
#include "includes.h"
- int main(void)
-{
- static struct ldb_context *ldb;
- struct ldb_ldif *ldif;
- int ret;
- int count=0, failures=0;
- const char *ldb_url;
-
- ldb_url = getenv("LDB_URL");
- if (!ldb_url) {
- ldb_url = "tdb://test.ldb";
- }
+static int failures;
- ldb = ldb_connect(ldb_url, 0, NULL);
+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("\n");
+ printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
+ exit(1);
+}
- if (!ldb) {
- perror("ldb_connect");
- exit(1);
- }
- while ((ldif = ldif_read_file(stdin))) {
+/*
+ add records from an opened file
+*/
+static int process_file(struct ldb_context *ldb, FILE *f)
+{
+ struct ldb_ldif *ldif;
+ int ret, count=0;
+ while ((ldif = ldif_read_file(f))) {
if (ldif->changetype != LDB_CHANGETYPE_ADD &&
ldif->changetype != LDB_CHANGETYPE_NONE) {
fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n");
@@ -73,6 +73,69 @@
ldif_read_free(ldif);
}
+ return count;
+}
+
+
+
+ int main(int argc, char * const argv[])
+{
+ struct ldb_context *ldb;
+ int count=0;
+ const char *ldb_url;
+ int opt, i;
+
+ ldb_url = getenv("LDB_URL");
+
+ while ((opt = getopt(argc, argv, "hH:")) != EOF) {
+ switch (opt) {
+ case 'H':
+ ldb_url = optarg;
+ break;
+
+ case 'h':
+ default:
+ usage();
+ break;
+ }
+ }
+
+ if (!ldb_url) {
+ fprintf(stderr, "You must specify a ldb URL\n");
+ exit(1);
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ ldb = ldb_connect(ldb_url, 0, NULL);
+
+ if (!ldb) {
+ perror("ldb_connect");
+ exit(1);
+ }
+
+ 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) {
+ fclose(f);
+ }
+ }
+
ldb_close(ldb);
printf("Added %d records with %d failures\n", count, failures);