]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Remove unused deprecated DN routines.
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 static DBC *cursor = NULL;
16 static DBT key, data;
17
18 int bdb_tool_entry_open(
19         BackendDB *be, int mode )
20 {
21         /* initialize key and data thangs */
22         DBTzero( &key );
23         DBTzero( &data );
24         key.flags = DB_DBT_REALLOC;
25         data.flags = DB_DBT_REALLOC;
26
27         return 0;
28 }
29
30 int bdb_tool_entry_close(
31         BackendDB *be )
32 {
33         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
34
35         assert( be != NULL );
36
37         if( key.data ) {
38                 ch_free( key.data );
39                 key.data = NULL;
40         }
41         if( data.data ) {
42                 ch_free( data.data );
43                 data.data = NULL;
44         }
45
46         if( cursor ) {
47                 cursor->c_close( cursor );
48                 cursor = NULL;
49         }
50
51         return 0;
52 }
53
54 ID bdb_tool_entry_next(
55         BackendDB *be )
56 {
57         int rc;
58         ID id;
59         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
60
61         assert( be != NULL );
62         assert( slapMode & SLAP_TOOL_MODE );
63         assert( bdb != NULL );
64         
65         if (cursor == NULL) {
66                 rc = bdb->bi_id2entry->bdi_db->cursor(
67                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
68                         bdb->bi_db_opflags );
69                 if( rc != 0 ) {
70                         return NOID;
71                 }
72         }
73
74         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
75
76         if( rc != 0 ) {
77                 return NOID;
78         }
79
80         if( data.data == NULL ) {
81                 return NOID;
82         }
83
84         AC_MEMCPY( &id, key.data, key.size );
85         return id;
86 }
87
88 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
89 {
90         int rc;
91         Entry *e;
92         struct berval bv;
93
94         assert( be != NULL );
95         assert( slapMode & SLAP_TOOL_MODE );
96         assert( data.data != NULL );
97
98         DBT2bv( &data, &bv );
99
100         rc = entry_decode( &bv, &e );
101
102         if( rc == LDAP_SUCCESS ) {
103                 e->e_id = id;
104         }
105
106 #ifdef BDB_HIER
107         bdb_fix_dn(be, id, e);
108 #endif
109
110         return e;
111 }
112
113 ID bdb_tool_entry_put(
114         BackendDB *be,
115         Entry *e,
116         struct berval *text )
117 {
118         int rc;
119         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
120         DB_TXN *tid = NULL;
121         struct berval pdn;
122
123         assert( be != NULL );
124         assert( slapMode & SLAP_TOOL_MODE );
125
126         assert( text );
127         assert( text->bv_val );
128         assert( text->bv_val[0] == '\0' );
129
130         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
131                 (long) e->e_id, e->e_dn, 0 );
132
133         if( bdb->bi_txn ) {
134                 rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 
135                         bdb->bi_db_opflags );
136                 if( rc != 0 ) {
137                         snprintf( text->bv_val, text->bv_len,
138                                         "txn_begin failed: %s (%d)",
139                                         db_strerror(rc), rc );
140                         Debug( LDAP_DEBUG_ANY,
141                                 "=> bdb_tool_entry_put: %s\n",
142                                  text->bv_val, 0, 0 );
143                         return NOID;
144                 }
145         }
146
147         rc = bdb_next_id( be, tid, &e->e_id );
148         if( rc != 0 ) {
149                 snprintf( text->bv_val, text->bv_len,
150                                 "next_id failed: %s (%d)",
151                                 db_strerror(rc), rc );
152                 Debug( LDAP_DEBUG_ANY,
153                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
154                 goto done;
155         }
156
157         /* add dn2id indices */
158         pdn.bv_val = dn_parent( be, e->e_ndn );
159         if (pdn.bv_val && *pdn.bv_val)
160                 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn);
161         else
162                 pdn.bv_len = 0;
163         rc = bdb_dn2id_add( be, tid, &pdn, e );
164         if( rc != 0 ) {
165                 snprintf( text->bv_val, text->bv_len, 
166                                 "dn2id_add failed: %s (%d)",
167                                 db_strerror(rc), rc );
168                 Debug( LDAP_DEBUG_ANY,
169                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
170                 goto done;
171         }
172
173         /* id2entry index */
174         rc = bdb_id2entry_add( be, tid, e );
175         if( rc != 0 ) {
176                 snprintf( text->bv_val, text->bv_len,
177                                 "id2entry_add failed: %s (%d)",
178                                 db_strerror(rc), rc );
179                 Debug( LDAP_DEBUG_ANY,
180                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
181                 goto done;
182         }
183
184         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
185         if( rc != 0 ) {
186                 snprintf( text->bv_val, text->bv_len,
187                                 "index_entry_add failed: %s (%d)",
188                                 db_strerror(rc), rc );
189                 Debug( LDAP_DEBUG_ANY,
190                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
191                 goto done;
192         }
193
194 done:
195         if( bdb->bi_txn ) {
196                 if( rc == 0 ) {
197                         rc = txn_commit( tid, 0 );
198                         if( rc != 0 ) {
199                                 snprintf( text->bv_val, text->bv_len,
200                                                 "txn_commit failed: %s (%d)",
201                                                 db_strerror(rc), rc );
202                                 Debug( LDAP_DEBUG_ANY,
203                                         "=> bdb_tool_entry_put: %s\n",
204                                         text->bv_val, 0, 0 );
205                                 e->e_id = NOID;
206                         }
207
208                 } else {
209                         txn_abort( tid );
210                         snprintf( text->bv_val, text->bv_len,
211                                         "txn_aborted! %s (%d)",
212                                         db_strerror(rc), rc );
213                         Debug( LDAP_DEBUG_ANY,
214                                 "=> bdb_tool_entry_put: %s\n",
215                                 text->bv_val, 0, 0 );
216                         e->e_id = NOID;
217                 }
218         }
219
220         return e->e_id;
221 }
222
223 int bdb_tool_entry_reindex(
224         BackendDB *be,
225         ID id )
226 {
227         struct bdb_info *bi = (struct bdb_info *) be->be_private;
228         int rc;
229         Entry *e;
230         DB_TXN *tid = NULL;
231         struct berval pdn;
232
233         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
234                 (long) id, 0, 0 );
235
236         e = bdb_tool_entry_get( be, id );
237
238         if( e == NULL ) {
239                 Debug( LDAP_DEBUG_ANY,
240                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
241                         (long) id, 0, 0 );
242                 return -1;
243         }
244
245         if( bi->bi_txn ) {
246                 rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
247                 if( rc != 0 ) {
248                         Debug( LDAP_DEBUG_ANY,
249                                 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
250                                 db_strerror(rc), rc, 0 );
251                         goto done;
252                 }
253         }
254         
255         /*
256          * just (re)add them for now
257          * assume that some other routine (not yet implemented)
258          * will zap index databases
259          *
260          */
261
262         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
263                 (long) id, e->e_dn, 0 );
264
265         /* add dn2id indices */
266         pdn.bv_val = dn_parent( be, e->e_ndn );
267         if (pdn.bv_val && *pdn.bv_val)
268                 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn);
269         else
270                 pdn.bv_len = 0;
271         rc = bdb_dn2id_add( be, tid, &pdn, e );
272         if( rc != 0 && rc != DB_KEYEXIST ) {
273                 Debug( LDAP_DEBUG_ANY,
274                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
275                         db_strerror(rc), rc, 0 );
276                 goto done;
277         }
278
279         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
280
281 done:
282         if( bi->bi_txn ) {
283                 if( rc == 0 ) {
284                         rc = txn_commit( tid, 0 );
285                         if( rc != 0 ) {
286                                 Debug( LDAP_DEBUG_ANY,
287                                         "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
288                                         db_strerror(rc), rc, 0 );
289                                 e->e_id = NOID;
290                         }
291
292                 } else {
293                         txn_abort( tid );
294                         Debug( LDAP_DEBUG_ANY,
295                                 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
296                                 db_strerror(rc), rc, 0 );
297                         e->e_id = NOID;
298                 }
299         }
300
301         return rc;
302 }