]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Remove broken MSVC build from REL_ENG branch.
[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-2003 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 #include "back-bdb.h"
23 #include "external.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 int bdb_tool_entry_open(
39         BackendDB *be, int mode )
40 {
41         /* initialize key and data thangs */
42         DBTzero( &key );
43         DBTzero( &data );
44         key.flags = DB_DBT_REALLOC;
45         data.flags = DB_DBT_REALLOC;
46
47         return 0;
48 }
49
50 int bdb_tool_entry_close(
51         BackendDB *be )
52 {
53         assert( be != NULL );
54
55         if( key.data ) {
56                 ch_free( key.data );
57                 key.data = NULL;
58         }
59         if( data.data ) {
60                 ch_free( data.data );
61                 data.data = NULL;
62         }
63
64         if( cursor ) {
65                 cursor->c_close( cursor );
66                 cursor = NULL;
67         }
68
69         if( nholes ) {
70                 unsigned i;
71                 fprintf( stderr, "Error, entries missing!\n");
72                 for (i=0; i<nholes; i++) {
73                         fprintf(stderr, "  entry %ld: %s\n",
74                                 holes[i].id, holes[i].dn.bv_val);
75                 }
76                 return -1;
77         }
78                         
79         return 0;
80 }
81
82 ID bdb_tool_entry_next(
83         BackendDB *be )
84 {
85         int rc;
86         ID id;
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88
89         assert( be != NULL );
90         assert( slapMode & SLAP_TOOL_MODE );
91         assert( bdb != NULL );
92         
93         if (cursor == NULL) {
94                 rc = bdb->bi_id2entry->bdi_db->cursor(
95                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
96                         bdb->bi_db_opflags );
97                 if( rc != 0 ) {
98                         return NOID;
99                 }
100         }
101
102         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
103
104         if( rc != 0 ) {
105                 return NOID;
106         }
107
108         if( data.data == NULL ) {
109                 return NOID;
110         }
111
112         AC_MEMCPY( &id, key.data, key.size );
113         return id;
114 }
115
116 ID bdb_tool_dn2id_get(
117         Backend *be,
118         struct berval *dn
119 )
120 {
121         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122         DB *db = bdb->bi_dn2id->bdi_db;
123         int rc;
124         DBT     key, data;
125         ID      id;
126
127         DBTzero( &key );
128         key.size = dn->bv_len + 2;
129         key.data = ch_malloc( key.size );
130         ((char*)key.data)[0] = DN_BASE_PREFIX;
131         AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
132
133         DBTzero( &data );
134         data.data = &id;
135         data.ulen = sizeof(ID);
136         data.flags = DB_DBT_USERMEM;
137
138         rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
139
140     if( rc != 0 ) {
141 #ifdef NEW_LOGGING
142                 LDAP_LOG ( INDEX, ERR, "bdb_tool_dn2id_get: get failed %s (%d)\n",
143                                 db_strerror(rc), rc, 0 );
144 #else
145                 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
146                                 db_strerror( rc ), rc, 0 );
147 #endif
148                 id = NOID;
149         }
150
151         ch_free( key.data );
152         return id;
153 }
154
155 int bdb_tool_id2entry_get(
156         Backend *be,
157         ID id,
158         Entry **e
159 )
160 {
161         return bdb_id2entry( be, NULL, id, e );
162 }
163
164 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
165 {
166         int rc;
167         Entry *e = NULL;
168         struct berval bv;
169
170         assert( be != NULL );
171         assert( slapMode & SLAP_TOOL_MODE );
172         assert( data.data != NULL );
173
174 #ifndef BDB_HIER
175         DBT2bv( &data, &bv );
176
177         rc = entry_decode( &bv, &e );
178
179         if( rc == LDAP_SUCCESS ) {
180                 e->e_id = id;
181         }
182 #else
183         {
184                 EntryInfo *ei = NULL;
185                 Operation op = {0};
186
187                 op.o_bd = be;
188                 op.o_tmpmemctx = NULL;
189                 op.o_tmpmfuncs = &ch_mfuncs;
190
191                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
192                 if ( rc == LDAP_SUCCESS )
193                         e = ei->bei_e;
194         }
195 #endif
196         return e;
197 }
198
199 static int bdb_tool_next_id(
200         Operation *op,
201         DB_TXN *tid,
202         Entry *e,
203         struct berval *text,
204         int hole )
205 {
206         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
207         struct berval dn = e->e_nname;
208         struct berval pdn;
209         EntryInfo *ei = NULL;
210         int rc;
211
212         rc = bdb_cache_find_ndn( op, tid, &dn, &ei );
213         if ( ei ) bdb_cache_entryinfo_unlock( ei );
214         if ( rc == DB_NOTFOUND ) {
215                 if ( be_issuffix( op->o_bd, &dn ) ) {
216                         pdn = slap_empty_bv;
217                 } else {
218                         dnParent( &dn, &pdn );
219                         e->e_nname = pdn;
220                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
221                         if ( rc ) {
222                                 return rc;
223                         }
224                 }
225                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
226                 if ( rc ) {
227                         snprintf( text->bv_val, text->bv_len,
228                                 "next_id failed: %s (%d)",
229                                 db_strerror(rc), rc );
230 #ifdef NEW_LOGGING
231                 LDAP_LOG ( TOOLS, ERR, 
232                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
233 #else
234                 Debug( LDAP_DEBUG_ANY,
235                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
236 #endif
237                         return rc;
238                 }
239                 e->e_nname = dn;
240                 rc = bdb_dn2id_add( op, tid, ei, e );
241                 if ( rc ) {
242                         snprintf( text->bv_val, text->bv_len, 
243                                 "dn2id_add failed: %s (%d)",
244                                 db_strerror(rc), rc );
245 #ifdef NEW_LOGGING
246                 LDAP_LOG ( TOOLS, ERR, 
247                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
248 #else
249                 Debug( LDAP_DEBUG_ANY,
250                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
251 #endif
252                 } else if ( hole ) {
253                         if ( nholes == nhmax - 1 ) {
254                                 if ( holes == hbuf ) {
255                                         holes = ch_malloc( nhmax * sizeof(ID) * 2 );
256                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
257                                 } else {
258                                         holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
259                                 }
260                                 nhmax *= 2;
261                         }
262                         ber_dupbv( &holes[nholes].dn, &dn );
263                         holes[nholes++].id = e->e_id;
264                 }
265         } else if ( !hole ) {
266                 unsigned i;
267
268                 for ( i=0; i<nholes; i++) {
269                         if ( holes[i].id == e->e_id ) {
270                                 int j;
271                                 free(holes[i].dn.bv_val);
272                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
273                                 holes[j].id = 0;
274                                 nholes--;
275                                 break;
276                         } else if ( holes[i].id > e->e_id ) {
277                                 break;
278                         }
279                 }
280         }
281         return rc;
282 }
283
284 ID bdb_tool_entry_put(
285         BackendDB *be,
286         Entry *e,
287         struct berval *text )
288 {
289         int rc;
290         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
291         DB_TXN *tid = NULL;
292         Operation op = {0};
293
294         assert( be != NULL );
295         assert( slapMode & SLAP_TOOL_MODE );
296
297         assert( text );
298         assert( text->bv_val );
299         assert( text->bv_val[0] == '\0' );      /* overconservative? */
300
301 #ifdef NEW_LOGGING
302         LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
303                 (long) e->e_id, e->e_dn, 0 );
304 #else
305         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
306                 (long) e->e_id, e->e_dn, 0 );
307 #endif
308
309         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
310                 bdb->bi_db_opflags );
311         if( rc != 0 ) {
312                 snprintf( text->bv_val, text->bv_len,
313                         "txn_begin failed: %s (%d)",
314                         db_strerror(rc), rc );
315 #ifdef NEW_LOGGING
316         LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
317 #else
318                 Debug( LDAP_DEBUG_ANY,
319                         "=> bdb_tool_entry_put: %s\n",
320                          text->bv_val, 0, 0 );
321 #endif
322                 return NOID;
323         }
324
325         op.o_bd = be;
326         op.o_tmpmemctx = NULL;
327         op.o_tmpmfuncs = &ch_mfuncs;
328
329         /* add dn2id indices */
330         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
331         if( rc != 0 ) {
332                 goto done;
333         }
334
335         /* id2entry index */
336         rc = bdb_id2entry_add( be, tid, e );
337         if( rc != 0 ) {
338                 snprintf( text->bv_val, text->bv_len,
339                                 "id2entry_add failed: %s (%d)",
340                                 db_strerror(rc), rc );
341 #ifdef NEW_LOGGING
342                 LDAP_LOG ( TOOLS, ERR, 
343                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
344 #else
345                 Debug( LDAP_DEBUG_ANY,
346                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
347 #endif
348                 goto done;
349         }
350
351         rc = bdb_index_entry_add( &op, tid, e );
352         if( rc != 0 ) {
353                 snprintf( text->bv_val, text->bv_len,
354                                 "index_entry_add failed: %s (%d)",
355                                 db_strerror(rc), rc );
356 #ifdef NEW_LOGGING
357                 LDAP_LOG ( TOOLS, ERR, 
358                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
359 #else
360                 Debug( LDAP_DEBUG_ANY,
361                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
362 #endif
363                 goto done;
364         }
365
366 done:
367         if( rc == 0 ) {
368                 rc = TXN_COMMIT( tid, 0 );
369                 if( rc != 0 ) {
370                         snprintf( text->bv_val, text->bv_len,
371                                         "txn_commit failed: %s (%d)",
372                                         db_strerror(rc), rc );
373 #ifdef NEW_LOGGING
374                         LDAP_LOG ( TOOLS, ERR, 
375                                 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
376 #else
377                         Debug( LDAP_DEBUG_ANY,
378                                 "=> bdb_tool_entry_put: %s\n",
379                                 text->bv_val, 0, 0 );
380 #endif
381                         e->e_id = NOID;
382                 }
383
384         } else {
385                 TXN_ABORT( tid );
386                 snprintf( text->bv_val, text->bv_len,
387                         "txn_aborted! %s (%d)",
388                         db_strerror(rc), rc );
389 #ifdef NEW_LOGGING
390                 LDAP_LOG ( TOOLS, ERR, 
391                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
392 #else
393                 Debug( LDAP_DEBUG_ANY,
394                         "=> bdb_tool_entry_put: %s\n",
395                         text->bv_val, 0, 0 );
396 #endif
397                 e->e_id = NOID;
398         }
399
400         return e->e_id;
401 }
402
403 int bdb_tool_entry_reindex(
404         BackendDB *be,
405         ID id )
406 {
407         struct bdb_info *bi = (struct bdb_info *) be->be_private;
408         int rc;
409         Entry *e;
410         DB_TXN *tid = NULL;
411         Operation op = {0};
412
413 #ifdef NEW_LOGGING
414         LDAP_LOG ( TOOLS, ARGS, 
415                 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
416 #else
417         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
418                 (long) id, 0, 0 );
419 #endif
420
421         e = bdb_tool_entry_get( be, id );
422
423         if( e == NULL ) {
424 #ifdef NEW_LOGGING
425                 LDAP_LOG ( TOOLS, DETAIL1, 
426                         "bdb_tool_entry_reindex:: could not locate id=%ld\n", 
427                         (long) id, 0, 0 );
428 #else
429                 Debug( LDAP_DEBUG_ANY,
430                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
431                         (long) id, 0, 0 );
432 #endif
433                 return -1;
434         }
435
436         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
437         if( rc != 0 ) {
438 #ifdef NEW_LOGGING
439                 LDAP_LOG ( TOOLS, ERR, 
440                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n", 
441                         db_strerror(rc), rc, 0 );
442 #else
443                 Debug( LDAP_DEBUG_ANY,
444                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
445                         db_strerror(rc), rc, 0 );
446 #endif
447                 goto done;
448         }
449         
450         /*
451          * just (re)add them for now
452          * assume that some other routine (not yet implemented)
453          * will zap index databases
454          *
455          */
456
457 #ifdef NEW_LOGGING
458         LDAP_LOG ( TOOLS, ERR, 
459                 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
460 #else
461         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
462                 (long) id, e->e_dn, 0 );
463 #endif
464
465         op.o_bd = be;
466         op.o_tmpmemctx = NULL;
467         op.o_tmpmfuncs = &ch_mfuncs;
468
469 #ifndef BDB_HIER
470         /* add dn2id indices */
471         rc = bdb_dn2id_add( &op, tid, NULL, e );
472         if( rc != 0 && rc != DB_KEYEXIST ) {
473 #ifdef NEW_LOGGING
474                 LDAP_LOG ( TOOLS, ERR, 
475                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n", 
476                         db_strerror(rc), rc, 0 );
477 #else
478                 Debug( LDAP_DEBUG_ANY,
479                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
480                         db_strerror(rc), rc, 0 );
481 #endif
482                 goto done;
483         }
484 #endif
485
486         rc = bdb_index_entry_add( &op, tid, e );
487
488 done:
489         if( rc == 0 ) {
490                 rc = TXN_COMMIT( tid, 0 );
491                 if( rc != 0 ) {
492 #ifdef NEW_LOGGING
493                         LDAP_LOG ( TOOLS, ERR, 
494                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n", 
495                                 db_strerror(rc), rc, 0 );
496 #else
497                         Debug( LDAP_DEBUG_ANY,
498                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
499                                 db_strerror(rc), rc, 0 );
500 #endif
501                         e->e_id = NOID;
502                 }
503
504         } else {
505                 TXN_ABORT( tid );
506 #ifdef NEW_LOGGING
507                 LDAP_LOG ( TOOLS, DETAIL1, 
508                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n", 
509                         db_strerror(rc), rc, 0 );
510 #else
511                 Debug( LDAP_DEBUG_ANY,
512                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
513                         db_strerror(rc), rc, 0 );
514 #endif
515                 e->e_id = NOID;
516         }
517         bdb_entry_release( &op, e, 0 );
518
519         return rc;
520 }
521
522 ID bdb_tool_entry_modify(
523         BackendDB *be,
524         Entry *e,
525         struct berval *text )
526 {
527         int rc;
528         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
529         DB_TXN *tid = NULL;
530         Operation op = {0};
531
532         assert( be != NULL );
533         assert( slapMode & SLAP_TOOL_MODE );
534
535         assert( text );
536         assert( text->bv_val );
537         assert( text->bv_val[0] == '\0' );      /* overconservative? */
538
539         assert ( e->e_id != NOID );
540         assert ( e->e_id != 0 );
541
542 #ifdef NEW_LOGGING
543         LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
544                 (long) e->e_id, e->e_dn, 0 );
545 #else
546         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
547                 (long) e->e_id, e->e_dn, 0 );
548 #endif
549
550         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
551                 bdb->bi_db_opflags );
552         if( rc != 0 ) {
553                 snprintf( text->bv_val, text->bv_len,
554                         "txn_begin failed: %s (%d)",
555                         db_strerror(rc), rc );
556 #ifdef NEW_LOGGING
557         LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
558 #else
559                 Debug( LDAP_DEBUG_ANY,
560                         "=> bdb_tool_entry_put: %s\n",
561                          text->bv_val, 0, 0 );
562 #endif
563                 return NOID;
564         }
565
566         op.o_bd = be;
567         op.o_tmpmemctx = NULL;
568         op.o_tmpmfuncs = &ch_mfuncs;
569
570         /* id2entry index */
571         rc = bdb_id2entry_update( be, tid, e );
572         if( rc != 0 ) {
573                 snprintf( text->bv_val, text->bv_len,
574                                 "id2entry_add failed: %s (%d)",
575                                 db_strerror(rc), rc );
576 #ifdef NEW_LOGGING
577                 LDAP_LOG ( TOOLS, ERR, 
578                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
579 #else
580                 Debug( LDAP_DEBUG_ANY,
581                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
582 #endif
583                 goto done;
584         }
585
586         rc = bdb_index_entry_del( &op, tid, e );
587         if( rc != 0 ) {
588                 snprintf( text->bv_val, text->bv_len,
589                                 "index_entry_del failed: %s (%d)",
590                                 db_strerror(rc), rc );
591 #ifdef NEW_LOGGING
592                 LDAP_LOG ( TOOLS, ERR, 
593                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
594 #else
595                 Debug( LDAP_DEBUG_ANY,
596                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
597 #endif
598                 goto done;
599         }
600
601         rc = bdb_index_entry_add( &op, tid, e );
602         if( rc != 0 ) {
603                 snprintf( text->bv_val, text->bv_len,
604                                 "index_entry_add failed: %s (%d)",
605                                 db_strerror(rc), rc );
606 #ifdef NEW_LOGGING
607                 LDAP_LOG ( TOOLS, ERR, 
608                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
609 #else
610                 Debug( LDAP_DEBUG_ANY,
611                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
612 #endif
613                 goto done;
614         }
615
616 done:
617         if( rc == 0 ) {
618                 rc = TXN_COMMIT( tid, 0 );
619                 if( rc != 0 ) {
620                         snprintf( text->bv_val, text->bv_len,
621                                         "txn_commit failed: %s (%d)",
622                                         db_strerror(rc), rc );
623 #ifdef NEW_LOGGING
624                         LDAP_LOG ( TOOLS, ERR, 
625                                 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
626 #else
627                         Debug( LDAP_DEBUG_ANY,
628                                 "=> bdb_tool_entry_put: %s\n",
629                                 text->bv_val, 0, 0 );
630 #endif
631                         e->e_id = NOID;
632                 }
633
634         } else {
635                 TXN_ABORT( tid );
636                 snprintf( text->bv_val, text->bv_len,
637                         "txn_aborted! %s (%d)",
638                         db_strerror(rc), rc );
639 #ifdef NEW_LOGGING
640                 LDAP_LOG ( TOOLS, ERR, 
641                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
642 #else
643                 Debug( LDAP_DEBUG_ANY,
644                         "=> bdb_tool_entry_put: %s\n",
645                         text->bv_val, 0, 0 );
646 #endif
647                 e->e_id = NOID;
648         }
649
650         return e->e_id;
651 }