]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/tools.c
Previously added modrdn restriction to restrictive. Need to
[openldap] / servers / slapd / back-ldbm / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 static LDBMCursor *cursorp = NULL;
19 static DBCache *id2entry = NULL;
20
21 int ldbm_tool_entry_open(
22         BackendDB *be, int mode )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         int flags;
26
27         assert( slapMode & SLAP_TOOL_MODE );
28         assert( id2entry == NULL );
29
30         switch( mode ) {
31         case 1:
32                 flags = LDBM_WRCREAT;
33                 break;
34         case 2:
35 #ifdef TRUNCATE_MODE
36                 flags = LDBM_NEWDB;
37 #else
38                 flags = LDBM_WRCREAT;
39 #endif
40                 break;
41         default:
42                 flags = LDBM_READER;
43         }
44
45         li->li_dbwritesync = 0;
46
47         if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
48             == NULL ) {
49 #ifdef NEW_LOGGING
50                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
51                            "Could not open/create id2entry%s\n", LDBM_SUFFIX ));
52 #else
53                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
54                     0, 0, 0 );
55 #endif
56
57                 return( -1 );
58         }
59
60         return 0;
61 }
62
63 int ldbm_tool_entry_close(
64         BackendDB *be )
65 {
66         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67
68         assert( slapMode & SLAP_TOOL_MODE );
69         assert( id2entry != NULL );
70
71         ldbm_cache_close( be, id2entry );
72         li->li_dbwritesync = 1;
73         id2entry = NULL;
74
75         return 0;
76 }
77
78 ID ldbm_tool_entry_first(
79         BackendDB *be )
80 {
81         Datum key;
82         ID id;
83
84         assert( slapMode & SLAP_TOOL_MODE );
85         assert( id2entry != NULL );
86
87         key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
88
89         if( key.dptr == NULL ) {
90                 return NOID;
91         }
92
93         AC_MEMCPY( &id, key.dptr, key.dsize );
94
95         ldbm_datum_free( id2entry->dbc_db, key );
96
97         return id;
98 }
99
100 ID ldbm_tool_entry_next(
101         BackendDB *be )
102 {
103         Datum key;
104         ID id;
105
106         assert( slapMode & SLAP_TOOL_MODE );
107         assert( id2entry != NULL );
108
109         /* allow for NEXTID */
110         ldbm_datum_init( key );
111
112         key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
113
114         if( key.dptr == NULL ) {
115                 return NOID;
116         }
117
118         AC_MEMCPY( &id, key.dptr, key.dsize );
119
120         ldbm_datum_free( id2entry->dbc_db, key );
121
122         return id;
123 }
124
125 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
126 {
127         Entry *e;
128         Datum key, data;
129         assert( slapMode & SLAP_TOOL_MODE );
130         assert( id2entry != NULL );
131
132         ldbm_datum_init( key );
133
134         key.dptr = (char *) &id;
135         key.dsize = sizeof(ID);
136
137         data = ldbm_cache_fetch( id2entry, key );
138
139         if ( data.dptr == NULL ) {
140                 return NULL;
141         }
142
143         e = str2entry( data.dptr );
144         ldbm_datum_free( id2entry->dbc_db, data );
145
146         if( e != NULL ) {
147                 e->e_id = id;
148         }
149
150         return e;
151 }
152
153 ID ldbm_tool_entry_put(
154         BackendDB *be,
155         Entry *e )
156 {
157         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
158         Datum key, data;
159         int rc, len;
160
161         assert( slapMode & SLAP_TOOL_MODE );
162         assert( id2entry != NULL );
163
164         if( next_id_get( be ) == NOID ) {
165                 return NOID;
166         }
167
168         e->e_id = li->li_nextid++;
169
170 #ifdef NEW_LOGGING
171         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
172                    "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ));
173 #else
174         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
175                 e->e_id, e->e_dn, 0 );
176 #endif
177
178
179         rc = index_entry_add( be, e, e->e_attrs );
180
181         if( rc != 0 ) {
182                 return NOID;
183         }
184
185         rc = dn2id_add( be, e->e_ndn, e->e_id );
186
187         if( rc != 0 ) {
188                 return NOID;
189         }
190
191         ldbm_datum_init( key );
192         ldbm_datum_init( data );
193
194         key.dptr = (char *) &e->e_id;
195         key.dsize = sizeof(ID);
196
197         data.dptr = entry2str( e, &len );
198         data.dsize = len + 1;
199
200         /* store it */
201         rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
202
203         if( rc != 0 ) {
204                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
205                 return NOID;
206         }
207
208         return e->e_id;
209 }
210
211 int ldbm_tool_entry_reindex(
212         BackendDB *be,
213         ID id )
214 {
215         int rc;
216         Entry *e;
217
218 #ifdef NEW_LOGGING
219         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
220                    "ldbm_tool_entry_reindex: ID=%ld\n", (long)id ));
221 #else
222         Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
223                 (long) id, 0, 0 );
224 #endif
225
226
227         e = ldbm_tool_entry_get( be, id );
228
229         if( e == NULL ) {
230 #ifdef NEW_LOGGING
231                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
232                            "ldbm_tool_entry_reindex: could not locate id %ld\n", 
233                            (long)id ));
234 #else
235                 Debug( LDAP_DEBUG_ANY,
236                         "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
237                         (long) id, 0, 0 );
238 #endif
239
240                 return -1;
241         }
242
243         /*
244          * just (re)add them for now
245          * assume that some other routine (not yet implemented)
246          * will zap index databases
247          *
248          */
249
250 #ifdef NEW_LOGGING
251         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
252                    "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id ));
253 #else
254         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
255                 id, e->e_dn, 0 );
256 #endif
257
258
259         rc = index_entry_add( be, e, e->e_attrs );
260
261         entry_free( e );
262
263         return rc;
264 }
265
266 int ldbm_tool_sync( BackendDB *be )
267 {
268         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
269
270         assert( slapMode & SLAP_TOOL_MODE );
271
272         if ( li->li_nextid != NOID ) {
273                 next_id_write( be, li->li_nextid );
274         }
275
276         return 0;
277 }