]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index-bdb2.c
b0d010b45657ac63b85355c5f22acc17a107d952
[openldap] / servers / slapd / tools / ldif2index-bdb2.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/stdlib.h>
6
7 #include <ac/ctype.h>
8 #include <ac/string.h>
9 #include <ac/socket.h>
10 #include <ac/unistd.h>
11
12 #include "ldif2common.h"
13 #include "../back-bdb2/back-bdb2.h"
14 #include "ldif.h"
15
16 int
17 main( int argc, char **argv )
18 {
19         int             i, stop;
20         char            *linep, *buf, *attr;
21         char            line[BUFSIZ];
22         int             lineno, elineno;
23         int             lmax, lcur, indexmask, syntaxmask;
24         unsigned long   id;
25         Backend         *be = NULL;
26         struct ldbminfo *li;
27         struct berval   bv;
28         struct berval   *vals[2];
29
30         slap_ldif_init( argc, argv, LDIF2INDEX, "bdb2", SLAP_TOOL_MODE );
31         attr = attr_normalize( argv[argc - 1] );
32
33         slap_startup(dbnum);
34
35         be = &backends[dbnum];
36
37         /* disable write sync'ing */
38         li = (struct ldbminfo *) be->be_private;
39         li->li_dbcachewsync = 0;
40
41         bdb2i_attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
42         if ( indexmask == 0 ) {
43                 exit( EXIT_SUCCESS );
44         }
45
46         id = 0;
47         stop = 0;
48         lineno = 0;
49         buf = NULL;
50         lcur = lmax = 0;
51         vals[0] = &bv;
52         vals[1] = NULL;
53         while ( ! stop ) {
54                 char            *type, *val, *s;
55                 ber_len_t       vlen;
56
57                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
58                         int     len;
59
60                         lineno++;
61                         len = strlen( line );
62                         while ( lcur + len + 1 > lmax ) {
63                                 lmax += BUFSIZ;
64                                 buf = (char *) ch_realloc( buf, lmax );
65                         }
66                         strcpy( buf + lcur, line );
67                         lcur += len;
68                 } else {
69                         stop = 1;
70                 }
71                 if ( line[0] == '\n' || stop && buf && *buf ) {
72                         if ( *buf != '\n' ) {
73                                 if (isdigit((unsigned char) *buf)) {
74                                         id = atol(buf);
75                                 } else {
76                                         id++;
77                                 }
78                                 s = buf;
79                                 elineno = 0;
80                                 while ( (linep = ldif_getline( &s )) != NULL ) {
81                                         elineno++;
82                                         if ( ldif_parse_line( linep, &type, &val,
83                                             &vlen ) != 0 ) {
84                                                 Debug( LDAP_DEBUG_PARSE,
85                             "bad line %d in entry ending at line %d ignored\n",
86                                                     elineno, elineno, 0 );
87                                                 continue;
88                                         }
89
90                                         if ( strcasecmp( type, attr ) == 0 ) {
91                                                 bv.bv_val = val;
92                                                 bv.bv_len = vlen;
93                                                 bdb2i_index_add_values( be, attr,
94                                                     vals, id );
95                                         }
96                                 }
97                         }
98                         *buf = '\0';
99                         lcur = 0;
100                 }
101         }
102
103         slap_shutdown(dbnum);
104         slap_destroy();
105
106         exit( EXIT_SUCCESS );
107 }