s4-ldb: when -v is specified, show progress of ldbadd/ldbmodify
[ira/wip.git] / source4 / lib / ldb / tools / ldbadd.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbadd
28  *
29  *  Description: utility to add records - modelled on ldapadd
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb.h"
35 #include "tools/cmdline.h"
36
37 static int failures;
38 static struct ldb_cmdline *options;
39
40 static void usage(void)
41 {
42         printf("Usage: ldbadd <options> <ldif...>\n");  
43         printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
44         ldb_cmdline_help("ldbadd", stdout);
45         exit(1);
46 }
47
48
49 /*
50   add records from an opened file
51 */
52 static int process_file(struct ldb_context *ldb, FILE *f, int *count)
53 {
54         struct ldb_ldif *ldif;
55         int ret = LDB_SUCCESS;
56
57         while ((ldif = ldb_ldif_read_file(ldb, f))) {
58                 if (ldif->changetype != LDB_CHANGETYPE_ADD &&
59                     ldif->changetype != LDB_CHANGETYPE_NONE) {
60                         fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n");
61                         break;
62                 }
63
64                 ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);
65
66                 ret = ldb_add(ldb, ldif->msg);
67                 if (ret != LDB_SUCCESS) {
68                         fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
69                                 ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
70                         failures++;
71                 } else {
72                         (*count)++;
73                         if (options->verbose) {
74                                 printf("Added %s\n", ldb_dn_get_linearized(ldif->msg->dn));
75                         }
76                 }
77                 ldb_ldif_read_free(ldb, ldif);
78         }
79
80         return ret;
81 }
82
83
84
85 int main(int argc, const char **argv)
86 {
87         struct ldb_context *ldb;
88         int i, ret=0, count=0;
89
90         ldb = ldb_init(NULL, NULL);
91
92         options = ldb_cmdline_process(ldb, argc, argv, usage);
93
94         if (ldb_transaction_start(ldb) != 0) {
95                 printf("Failed to start transaction: %s\n", ldb_errstring(ldb));
96                 exit(1);
97         }
98
99         if (options->argc == 0) {
100                 ret = process_file(ldb, stdin, &count);
101         } else {
102                 for (i=0;i<options->argc;i++) {
103                         const char *fname = options->argv[i];
104                         FILE *f;
105                         f = fopen(fname, "r");
106                         if (!f) {
107                                 perror(fname);
108                                 exit(1);
109                         }
110                         ret = process_file(ldb, f, &count);
111                         fclose(f);
112                 }
113         }
114
115         if (count != 0) {
116                 if (ldb_transaction_commit(ldb) != 0) {
117                         printf("Failed to commit transaction: %s\n", ldb_errstring(ldb));
118                         exit(1);
119                 }
120         } else {
121                 ldb_transaction_cancel(ldb);
122         }
123
124         talloc_free(ldb);
125
126         printf("Added %d records with %d failures\n", count, failures);
127         
128         return ret;
129 }