]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Cleanup unused variables
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #define AVL_INTERNAL
23 #include "back-bdb.h"
24
25 static DBC *cursor = NULL;
26 static DBT key, data;
27
28 typedef struct dn_id {
29         ID id;
30         struct berval dn;
31 } dn_id;
32
33 #define HOLE_SIZE       4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
37
38 Avlnode *index_attrs, index_dummy;
39
40 int bdb_tool_entry_open(
41         BackendDB *be, int mode )
42 {
43         /* initialize key and data thangs */
44         DBTzero( &key );
45         DBTzero( &data );
46         key.flags = DB_DBT_REALLOC;
47         data.flags = DB_DBT_REALLOC;
48
49         return 0;
50 }
51
52 int bdb_tool_entry_close(
53         BackendDB *be )
54 {
55         assert( be != NULL );
56
57         if( key.data ) {
58                 ch_free( key.data );
59                 key.data = NULL;
60         }
61         if( data.data ) {
62                 ch_free( data.data );
63                 data.data = NULL;
64         }
65
66         if( cursor ) {
67                 cursor->c_close( cursor );
68                 cursor = NULL;
69         }
70
71         if( nholes ) {
72                 unsigned i;
73                 fprintf( stderr, "Error, entries missing!\n");
74                 for (i=0; i<nholes; i++) {
75                         fprintf(stderr, "  entry %ld: %s\n",
76                                 holes[i].id, holes[i].dn.bv_val);
77                 }
78                 return -1;
79         }
80                         
81         return 0;
82 }
83
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
85
86 ID bdb_tool_entry_next(
87         BackendDB *be )
88 {
89         int rc;
90         ID id;
91         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92
93         assert( be != NULL );
94         assert( slapMode & SLAP_TOOL_MODE );
95         assert( bdb != NULL );
96         
97         /* Initialization */
98         if (cursor == NULL) {
99                 rc = bdb->bi_id2entry->bdi_db->cursor(
100                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
101                         bdb->bi_db_opflags );
102                 if( rc != 0 ) {
103                         return NOID;
104                 }
105         }
106
107         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
108
109         if( rc != 0 ) {
110                 /* If we're doing linear indexing and there are more attrs to
111                  * index, and we're at the end of the database, start over.
112                  */
113                 if ( bdb->bi_attrs == &index_dummy ) {
114                         if ( index_attrs && rc == DB_NOTFOUND ) {
115                                 /* optional - do a checkpoint here? */
116                                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117                                 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
118                         }
119                         if ( rc ) {
120                                 bdb->bi_attrs = NULL;
121                                 return NOID;
122                         }
123                 } else {
124                         return NOID;
125                 }
126         }
127
128         if( data.data == NULL ) {
129                 return NOID;
130         }
131
132         BDB_DISK2ID( key.data, &id );
133         return id;
134 }
135
136 ID bdb_tool_dn2id_get(
137         Backend *be,
138         struct berval *dn
139 )
140 {
141         Operation op = {0};
142         Opheader ohdr = {0};
143         EntryInfo ei = {0};
144
145         op.o_hdr = &ohdr;
146         op.o_bd = be;
147         op.o_tmpmemctx = NULL;
148         op.o_tmpmfuncs = &ch_mfuncs;
149
150         ei.bei_id = NOID;
151
152         bdb_dn2id( &op, NULL, dn, &ei );
153         
154         return ei.bei_id;
155 }
156
157 int bdb_tool_id2entry_get(
158         Backend *be,
159         ID id,
160         Entry **e
161 )
162 {
163         return bdb_id2entry( be, NULL, id, e );
164 }
165
166 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
167 {
168         int rc;
169         Entry *e = NULL;
170         struct berval bv;
171
172         assert( be != NULL );
173         assert( slapMode & SLAP_TOOL_MODE );
174         assert( data.data != NULL );
175
176 #ifndef BDB_HIER
177         DBT2bv( &data, &bv );
178
179 #ifdef SLAP_ZONE_ALLOC
180         /* FIXME: will add ctx later */
181         rc = entry_decode( &bv, &e, NULL );
182 #else
183         rc = entry_decode( &bv, &e );
184 #endif
185
186         if( rc == LDAP_SUCCESS ) {
187                 e->e_id = id;
188         }
189 #else
190         {
191                 EntryInfo *ei = NULL;
192                 Operation op = {0};
193                 Opheader ohdr = {0};
194
195                 op.o_hdr = &ohdr;
196                 op.o_bd = be;
197                 op.o_tmpmemctx = NULL;
198                 op.o_tmpmfuncs = &ch_mfuncs;
199
200                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
201                 if ( rc == LDAP_SUCCESS )
202                         e = ei->bei_e;
203         }
204 #endif
205         return e;
206 }
207
208 static int bdb_tool_next_id(
209         Operation *op,
210         DB_TXN *tid,
211         Entry *e,
212         struct berval *text,
213         int hole )
214 {
215         struct berval dn = e->e_name;
216         struct berval ndn = e->e_nname;
217         struct berval pdn, npdn;
218         EntryInfo *ei = NULL;
219         int rc;
220
221         if (ndn.bv_len == 0) return 0;
222
223         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
224         if ( ei ) bdb_cache_entryinfo_unlock( ei );
225         if ( rc == DB_NOTFOUND ) {
226                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
227                         dnParent( &dn, &pdn );
228                         dnParent( &ndn, &npdn );
229                         e->e_name = pdn;
230                         e->e_nname = npdn;
231                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
232                         e->e_name = dn;
233                         e->e_nname = ndn;
234                         if ( rc ) {
235                                 return rc;
236                         }
237                 }
238                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
239                 if ( rc ) {
240                         snprintf( text->bv_val, text->bv_len,
241                                 "next_id failed: %s (%d)",
242                                 db_strerror(rc), rc );
243                 Debug( LDAP_DEBUG_ANY,
244                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
245                         return rc;
246                 }
247                 rc = bdb_dn2id_add( op, tid, ei, e );
248                 if ( rc ) {
249                         snprintf( text->bv_val, text->bv_len, 
250                                 "dn2id_add failed: %s (%d)",
251                                 db_strerror(rc), rc );
252                 Debug( LDAP_DEBUG_ANY,
253                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
254                 } else if ( hole ) {
255                         if ( nholes == nhmax - 1 ) {
256                                 if ( holes == hbuf ) {
257                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
258                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
259                                 } else {
260                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
261                                 }
262                                 nhmax *= 2;
263                         }
264                         ber_dupbv( &holes[nholes].dn, &ndn );
265                         holes[nholes++].id = e->e_id;
266                 }
267         } else if ( !hole ) {
268                 unsigned i;
269
270                 e->e_id = ei->bei_id;
271
272                 for ( i=0; i<nholes; i++) {
273                         if ( holes[i].id == e->e_id ) {
274                                 int j;
275                                 free(holes[i].dn.bv_val);
276                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
277                                 holes[j].id = 0;
278                                 nholes--;
279                                 break;
280                         } else if ( holes[i].id > e->e_id ) {
281                                 break;
282                         }
283                 }
284         }
285         return rc;
286 }
287
288 ID bdb_tool_entry_put(
289         BackendDB *be,
290         Entry *e,
291         struct berval *text )
292 {
293         int rc;
294         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
295         DB_TXN *tid = NULL;
296         Operation op = {0};
297         Opheader ohdr = {0};
298
299         assert( be != NULL );
300         assert( slapMode & SLAP_TOOL_MODE );
301
302         assert( text );
303         assert( text->bv_val );
304         assert( text->bv_val[0] == '\0' );      /* overconservative? */
305
306         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
307                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
308
309         if (! (slapMode & SLAP_TOOL_QUICK)) {
310         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
311                 bdb->bi_db_opflags );
312         if( rc != 0 ) {
313                 snprintf( text->bv_val, text->bv_len,
314                         "txn_begin failed: %s (%d)",
315                         db_strerror(rc), rc );
316                 Debug( LDAP_DEBUG_ANY,
317                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
318                          text->bv_val, 0, 0 );
319                 return NOID;
320         }
321         }
322
323         op.o_hdr = &ohdr;
324         op.o_bd = be;
325         op.o_tmpmemctx = NULL;
326         op.o_tmpmfuncs = &ch_mfuncs;
327
328         /* add dn2id indices */
329         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
330         if( rc != 0 ) {
331                 goto done;
332         }
333
334         /* id2entry index */
335         rc = bdb_id2entry_add( be, tid, e );
336         if( rc != 0 ) {
337                 snprintf( text->bv_val, text->bv_len,
338                                 "id2entry_add failed: %s (%d)",
339                                 db_strerror(rc), rc );
340                 Debug( LDAP_DEBUG_ANY,
341                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
342                         text->bv_val, 0, 0 );
343                 goto done;
344         }
345
346         if ( !bdb->bi_linear_index )
347                 rc = bdb_index_entry_add( &op, tid, e );
348         if( rc != 0 ) {
349                 snprintf( text->bv_val, text->bv_len,
350                                 "index_entry_add failed: %s (%d)",
351                                 db_strerror(rc), rc );
352                 Debug( LDAP_DEBUG_ANY,
353                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
354                         text->bv_val, 0, 0 );
355                 goto done;
356         }
357
358 done:
359         if( rc == 0 ) {
360                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
361                 rc = TXN_COMMIT( tid, 0 );
362                 if( rc != 0 ) {
363                         snprintf( text->bv_val, text->bv_len,
364                                         "txn_commit failed: %s (%d)",
365                                         db_strerror(rc), rc );
366                         Debug( LDAP_DEBUG_ANY,
367                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
368                                 text->bv_val, 0, 0 );
369                         e->e_id = NOID;
370                 }
371                 }
372
373         } else {
374                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
375                 TXN_ABORT( tid );
376                 snprintf( text->bv_val, text->bv_len,
377                         "txn_aborted! %s (%d)",
378                         db_strerror(rc), rc );
379                 Debug( LDAP_DEBUG_ANY,
380                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
381                         text->bv_val, 0, 0 );
382                 }
383                 e->e_id = NOID;
384         }
385
386         return e->e_id;
387 }
388
389 int bdb_tool_entry_reindex(
390         BackendDB *be,
391         ID id )
392 {
393         struct bdb_info *bi = (struct bdb_info *) be->be_private;
394         int rc;
395         Entry *e;
396         DB_TXN *tid = NULL;
397         Operation op = {0};
398         Opheader ohdr = {0};
399
400         Debug( LDAP_DEBUG_ARGS,
401                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
402                 (long) id, 0, 0 );
403
404         /* No indexes configured, nothing to do. Could return an
405          * error here to shortcut things.
406          */
407         if (!bi->bi_attrs) {
408                 return 0;
409         }
410
411         /* Get the first attribute to index */
412         if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
413                 index_attrs = bi->bi_attrs;
414                 bi->bi_attrs = &index_dummy;
415                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
416         }
417
418         e = bdb_tool_entry_get( be, id );
419
420         if( e == NULL ) {
421                 Debug( LDAP_DEBUG_ANY,
422                         LDAP_XSTRING(bdb_tool_entry_reindex)
423                         ": could not locate id=%ld\n",
424                         (long) id, 0, 0 );
425                 return -1;
426         }
427
428         if (! (slapMode & SLAP_TOOL_QUICK)) {
429         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
430         if( rc != 0 ) {
431                 Debug( LDAP_DEBUG_ANY,
432                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
433                         "txn_begin failed: %s (%d)\n",
434                         db_strerror(rc), rc, 0 );
435                 goto done;
436         }
437         }
438         
439         /*
440          * just (re)add them for now
441          * assume that some other routine (not yet implemented)
442          * will zap index databases
443          *
444          */
445
446         Debug( LDAP_DEBUG_TRACE,
447                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
448                 (long) id, e->e_dn, 0 );
449
450         op.o_hdr = &ohdr;
451         op.o_bd = be;
452         op.o_tmpmemctx = NULL;
453         op.o_tmpmfuncs = &ch_mfuncs;
454
455         rc = bdb_index_entry_add( &op, tid, e );
456
457 done:
458         if( rc == 0 ) {
459                 if (! (slapMode & SLAP_TOOL_QUICK)) {
460                 rc = TXN_COMMIT( tid, 0 );
461                 if( rc != 0 ) {
462                         Debug( LDAP_DEBUG_ANY,
463                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
464                                 ": txn_commit failed: %s (%d)\n",
465                                 db_strerror(rc), rc, 0 );
466                         e->e_id = NOID;
467                 }
468                 }
469
470         } else {
471                 if (! (slapMode & SLAP_TOOL_QUICK)) {
472                 TXN_ABORT( tid );
473                 Debug( LDAP_DEBUG_ANY,
474                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
475                         ": txn_aborted! %s (%d)\n",
476                         db_strerror(rc), rc, 0 );
477                 }
478                 e->e_id = NOID;
479         }
480         bdb_entry_release( &op, e, 0 );
481
482         return rc;
483 }
484
485 ID bdb_tool_entry_modify(
486         BackendDB *be,
487         Entry *e,
488         struct berval *text )
489 {
490         int rc;
491         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
492         DB_TXN *tid = NULL;
493         Operation op = {0};
494         Opheader ohdr = {0};
495
496         assert( be != NULL );
497         assert( slapMode & SLAP_TOOL_MODE );
498
499         assert( text );
500         assert( text->bv_val );
501         assert( text->bv_val[0] == '\0' );      /* overconservative? */
502
503         assert ( e->e_id != NOID );
504         assert ( e->e_id != 0 );
505
506         Debug( LDAP_DEBUG_TRACE,
507                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
508                 (long) e->e_id, e->e_dn, 0 );
509
510         if (! (slapMode & SLAP_TOOL_QUICK)) {
511         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
512                 bdb->bi_db_opflags );
513         if( rc != 0 ) {
514                 snprintf( text->bv_val, text->bv_len,
515                         "txn_begin failed: %s (%d)",
516                         db_strerror(rc), rc );
517                 Debug( LDAP_DEBUG_ANY,
518                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
519                          text->bv_val, 0, 0 );
520                 return NOID;
521         }
522         }
523
524         op.o_hdr = &ohdr;
525         op.o_bd = be;
526         op.o_tmpmemctx = NULL;
527         op.o_tmpmfuncs = &ch_mfuncs;
528
529         /* id2entry index */
530         rc = bdb_id2entry_update( be, tid, e );
531         if( rc != 0 ) {
532                 snprintf( text->bv_val, text->bv_len,
533                                 "id2entry_add failed: %s (%d)",
534                                 db_strerror(rc), rc );
535                 Debug( LDAP_DEBUG_ANY,
536                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
537                         text->bv_val, 0, 0 );
538                 goto done;
539         }
540
541         rc = bdb_index_entry_del( &op, tid, e );
542         if( rc != 0 ) {
543                 snprintf( text->bv_val, text->bv_len,
544                                 "index_entry_del failed: %s (%d)",
545                                 db_strerror(rc), rc );
546                 Debug( LDAP_DEBUG_ANY,
547                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
548                         text->bv_val, 0, 0 );
549                 goto done;
550         }
551
552         rc = bdb_index_entry_add( &op, tid, e );
553         if( rc != 0 ) {
554                 snprintf( text->bv_val, text->bv_len,
555                                 "index_entry_add failed: %s (%d)",
556                                 db_strerror(rc), rc );
557                 Debug( LDAP_DEBUG_ANY,
558                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
559                         text->bv_val, 0, 0 );
560                 goto done;
561         }
562
563 done:
564         if( rc == 0 ) {
565                 if (! (slapMode & SLAP_TOOL_QUICK)) {
566                 rc = TXN_COMMIT( tid, 0 );
567                 if( rc != 0 ) {
568                         snprintf( text->bv_val, text->bv_len,
569                                         "txn_commit failed: %s (%d)",
570                                         db_strerror(rc), rc );
571                         Debug( LDAP_DEBUG_ANY,
572                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
573                                 "%s\n", text->bv_val, 0, 0 );
574                         e->e_id = NOID;
575                 }
576                 }
577
578         } else {
579                 if (! (slapMode & SLAP_TOOL_QUICK)) {
580                 TXN_ABORT( tid );
581                 snprintf( text->bv_val, text->bv_len,
582                         "txn_aborted! %s (%d)",
583                         db_strerror(rc), rc );
584                 Debug( LDAP_DEBUG_ANY,
585                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
586                         text->bv_val, 0, 0 );
587                 }
588                 e->e_id = NOID;
589         }
590
591         return e->e_id;
592 }