]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/tools.c
Remove ai_syntaxinfo (syntax) from AttrInfo. Call attr_syntax() directly.
[openldap] / servers / slapd / back-ldbm / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
50                     0, 0, 0 );
51                 return( -1 );
52         }
53
54         return 0;
55 }
56
57 int ldbm_tool_entry_close(
58         BackendDB *be )
59 {
60         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
61
62         assert( slapMode & SLAP_TOOL_MODE );
63         assert( id2entry != NULL );
64
65         ldbm_cache_close( be, id2entry );
66         li->li_dbwritesync = 1;
67         id2entry = NULL;
68
69         return 0;
70 }
71
72 ID ldbm_tool_entry_first(
73         BackendDB *be )
74 {
75         Datum key;
76         ID id;
77
78         assert( slapMode & SLAP_TOOL_MODE );
79         assert( id2entry != NULL );
80
81         key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
82
83         if( key.dptr == NULL ) {
84                 return NOID;
85         }
86
87         memcpy( &id, key.dptr, key.dsize );
88
89         ldbm_datum_free( id2entry->dbc_db, key );
90
91         return id;
92 }
93
94 ID ldbm_tool_entry_next(
95         BackendDB *be )
96 {
97         Datum key;
98         ID id;
99
100         assert( slapMode & SLAP_TOOL_MODE );
101         assert( id2entry != NULL );
102
103         /* allow for NEXTID */
104         ldbm_datum_init( key );
105
106         key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
107
108         if( key.dptr == NULL ) {
109                 return NOID;
110         }
111
112         memcpy( &id, key.dptr, key.dsize );
113
114         ldbm_datum_free( id2entry->dbc_db, key );
115
116         return id;
117 }
118
119 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
120 {
121         Entry *e;
122         Datum key, data;
123         assert( slapMode & SLAP_TOOL_MODE );
124         assert( id2entry != NULL );
125
126         ldbm_datum_init( key );
127
128         key.dptr = (char *) &id;
129         key.dsize = sizeof(ID);
130
131         data = ldbm_cache_fetch( id2entry, key );
132
133         if ( data.dptr == NULL ) {
134                 return NULL;
135         }
136
137         e = str2entry( data.dptr );
138         ldbm_datum_free( id2entry->dbc_db, data );
139
140         return e;
141 }
142
143 ID ldbm_tool_entry_put(
144         BackendDB *be,
145         Entry *e )
146 {
147         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
148         Datum key, data;
149         int rc, len;
150
151         assert( slapMode & SLAP_TOOL_MODE );
152         assert( id2entry != NULL );
153
154         if( next_id_get( be ) == NOID ) {
155                 return NOID;
156         }
157
158         e->e_id = li->li_nextid++;
159
160         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
161                 e->e_id, e->e_dn, 0 );
162
163         rc = index_add_entry( be, e );
164
165         if( rc != 0 ) {
166                 return NOID;
167         }
168
169         rc = dn2id_add( be, e->e_ndn, e->e_id );
170
171         if( rc != 0 ) {
172                 return NOID;
173         }
174
175         ldbm_datum_init( key );
176         ldbm_datum_init( data );
177
178         key.dptr = (char *) &e->e_id;
179         key.dsize = sizeof(ID);
180
181         data.dptr = entry2str( e, &len );
182         data.dsize = len + 1;
183
184         /* store it */
185         rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
186
187         if( rc != 0 ) {
188                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
189                 return NOID;
190         }
191
192         return e->e_id;
193 }
194
195 int ldbm_tool_index_attr(
196         BackendDB *be,
197         char* type )
198 {
199         static DBCache *db = NULL;
200         int indexmask, syntaxmask;
201         char * at_cn;
202 #ifndef SLAPD_SCHEMA_COMPAT
203         AttributeType *at;
204 #endif
205
206         assert( slapMode & SLAP_TOOL_MODE );
207
208 #ifdef SLAPD_SCHEMA_COMPAT
209         attr_normalize( type );
210         at_cn = at_canonical_name( type );
211
212         if( at_cn ) {
213                 Debug( LDAP_DEBUG_ANY, "<= index_attr NULL (attribute type %s has no canonical name)\n",
214                         type, 0, 0 );
215                 return 0;
216         }
217 #else
218         at = at_find( type );
219
220         if( at == NULL ) {
221                 Debug( LDAP_DEBUG_ANY,
222                 "<= index_attr NULL (could not find attribute type %s)\n",
223                         type, 0, 0 );
224                 return 0;
225         }
226
227         at_cn = at_canonical_name( at );
228
229         if( at_cn ) {
230                 Debug( LDAP_DEBUG_ANY, "<= index_attr NULL (attribute type %s (%s) has no canonical name)\n",
231                         at->sat_oid, type, 0 );
232                 return 0;
233         }
234 #endif
235
236         attr_mask( be->be_private, at_cn, &indexmask );
237
238         if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_NEWDB ))
239             == NULL )
240         {
241                 Debug( LDAP_DEBUG_ANY,
242                     "<= index_attr NULL (could not open %s%s)\n", at_cn,
243                     LDBM_SUFFIX, 0 );
244                 return 0;
245         }
246
247         ldbm_cache_close( be, db );
248
249         return indexmask != 0;
250 }
251
252 int ldbm_tool_index_change(
253         BackendDB *be,
254         char* type,
255         struct berval **bv,
256         ID id,
257         int op )
258 {
259         assert( slapMode & SLAP_TOOL_MODE );
260
261         index_change_values( be,
262                 type, bv, id, op );
263
264         return 0;
265 }
266
267 int ldbm_tool_sync( BackendDB *be )
268 {
269         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
270
271         assert( slapMode & SLAP_TOOL_MODE );
272
273         if ( li->li_nextid != NOID ) {
274                 next_id_write( be, li->li_nextid );
275         }
276
277         return 0;
278 }