]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Update projects to use ldif2common.*
[openldap] / servers / slapd / tools / ldif2id2entry.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/unistd.h>
15
16 #include "ldif2common.h"
17 #include "../back-ldbm/back-ldbm.h"
18
19 int
20 main( int argc, char **argv )
21 {
22         int             stop;
23         char            *buf;
24         char            line[BUFSIZ], idbuf[BUFSIZ];
25         int             lmax, lcur;
26         ID              id;
27         ID              maxid;
28         DBCache *db;
29         Backend         *be = NULL;
30         struct ldbminfo *li;
31         struct berval   bv;
32         struct berval   *vals[2];
33         FILE            *fp;
34
35         ldbm_ignore_nextid_file = 1;
36
37         slap_ldif_init( argc, argv, LDIF2ID2ENTRY, "ldbm", SLAP_TOOL_MODE );
38
39         slap_startup(dbnum);
40
41         be = &backends[dbnum];
42
43         /* disable write sync'ing */
44         li = (struct ldbminfo *) be->be_private;
45         li->li_dbcachewsync = 0;
46
47         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
48             == NULL ) {
49                 perror( "id2entry file" );
50                 exit( EXIT_FAILURE );
51         }
52
53         id = 0;
54         maxid = 0;
55         stop = 0;
56         buf = NULL;
57         lcur = lmax = 0;
58         vals[0] = &bv;
59         vals[1] = NULL;
60         while ( ! stop ) {
61                 Datum           key, data;
62
63                 ldbm_datum_init( key );
64                 ldbm_datum_init( data );
65
66                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
67                         int     len, idlen;
68
69                         len = strlen( line );
70                         if ( buf == NULL || *buf == '\0' ) {
71                                 if (!isdigit((unsigned char) line[0])) {
72                                         sprintf( idbuf, "%ld\n", id + 1 );
73                                         idlen = strlen( idbuf );
74                                 } else {
75                                         id = atol(line) - 1;
76                                         idlen = 0;
77                                 }
78                         } else {
79                                 idlen = 0;
80                         }
81
82                         while ( lcur + len + idlen + 1 > lmax ) {
83                                 lmax += BUFSIZ;
84                                 buf = (char *) ch_realloc( buf, lmax );
85                         }
86
87                         if ( idlen > 0 ) {
88                                 strcpy( buf + lcur, idbuf );
89                                 lcur += idlen;
90                         }
91                         strcpy( buf + lcur, line );
92                         lcur += len;
93                 } else {
94                         stop = 1;
95                 }
96                 if ( line[0] == '\n' || stop && buf && *buf ) {
97                         if ( *buf != '\n' ) {
98                                 int len;
99
100                                 id++;
101                                 if ( id > maxid )
102                                         maxid = id;
103                                 key.dptr = (char *) &id;
104                                 key.dsize = sizeof(ID);
105                                 data.dptr = buf;
106                                 len = strlen(buf);
107                                 if (buf[len - 1] == '\n')
108                                         buf[--len] = '\0';
109                                 data.dsize = len + 1;
110                                 if ( ldbm_store( db->dbc_db, key, data,
111                                     LDBM_INSERT ) != 0 ) {
112                                         fputs("id2entry ldbm_store failed\n",
113                                               stderr);
114                                         exit( EXIT_FAILURE );
115                                 }
116                         }
117                         *buf = '\0';
118                         lcur = 0;
119                         line[0] = '\0';
120                 }
121         }
122
123 #ifdef SLAP_CLEANUP
124         ldbm_cache_close( be, db );
125 #endif
126
127         slap_shutdown(dbnum);
128
129         maxid++;
130         sprintf( line, "%s/NEXTID",
131             ((struct ldbminfo *) be->be_private)->li_directory );
132         if ( (fp = fopen( line, "w" )) == NULL ) {
133                 perror( line );
134                 fprintf( stderr, "Could not write next id %ld\n", maxid );
135         } else {
136                 fprintf( fp, "%ld\n", maxid );
137                 fclose( fp );
138         }
139
140         slap_destroy();
141
142         return( EXIT_SUCCESS );
143 }