]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
b01c4d7d968966bdd483d8bfb5c89be824f9c398
[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 #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                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_dn2id_get)
142                                 ": get failed: %s (%d)\n",
143                                 db_strerror( rc ), rc, 0 );
144                 id = NOID;
145         }
146
147         ch_free( key.data );
148         return id;
149 }
150
151 int bdb_tool_id2entry_get(
152         Backend *be,
153         ID id,
154         Entry **e
155 )
156 {
157         return bdb_id2entry( be, NULL, id, e );
158 }
159
160 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
161 {
162         int rc;
163         Entry *e = NULL;
164         struct berval bv;
165
166         assert( be != NULL );
167         assert( slapMode & SLAP_TOOL_MODE );
168         assert( data.data != NULL );
169
170 #ifndef BDB_HIER
171         DBT2bv( &data, &bv );
172
173         rc = entry_decode( &bv, &e );
174
175         if( rc == LDAP_SUCCESS ) {
176                 e->e_id = id;
177         }
178 #else
179         {
180                 EntryInfo *ei = NULL;
181                 Operation op = {0};
182
183                 op.o_bd = be;
184                 op.o_tmpmemctx = NULL;
185                 op.o_tmpmfuncs = &ch_mfuncs;
186
187                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
188                 if ( rc == LDAP_SUCCESS )
189                         e = ei->bei_e;
190         }
191 #endif
192         return e;
193 }
194
195 static int bdb_tool_next_id(
196         Operation *op,
197         DB_TXN *tid,
198         Entry *e,
199         struct berval *text,
200         int hole )
201 {
202         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
203         struct berval dn = e->e_name;
204         struct berval ndn = e->e_nname;
205         struct berval pdn, npdn;
206         EntryInfo *ei = NULL;
207         int rc;
208
209         if (ndn.bv_len == 0) return 0;
210
211         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
212         if ( ei ) bdb_cache_entryinfo_unlock( ei );
213         if ( rc == DB_NOTFOUND ) {
214                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
215                         dnParent( &dn, &pdn );
216                         dnParent( &ndn, &npdn );
217                         e->e_name = pdn;
218                         e->e_nname = npdn;
219                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
220                         e->e_name = dn;
221                         e->e_nname = ndn;
222                         if ( rc ) {
223                                 return rc;
224                         }
225                 }
226                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
227                 if ( rc ) {
228                         snprintf( text->bv_val, text->bv_len,
229                                 "next_id failed: %s (%d)",
230                                 db_strerror(rc), rc );
231                 Debug( LDAP_DEBUG_ANY,
232                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
233                         return rc;
234                 }
235                 rc = bdb_dn2id_add( op, tid, ei, e );
236                 if ( rc ) {
237                         snprintf( text->bv_val, text->bv_len, 
238                                 "dn2id_add failed: %s (%d)",
239                                 db_strerror(rc), rc );
240                 Debug( LDAP_DEBUG_ANY,
241                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
242                 } else if ( hole ) {
243                         if ( nholes == nhmax - 1 ) {
244                                 if ( holes == hbuf ) {
245                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
246                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
247                                 } else {
248                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
249                                 }
250                                 nhmax *= 2;
251                         }
252                         ber_dupbv( &holes[nholes].dn, &ndn );
253                         holes[nholes++].id = e->e_id;
254                 }
255         } else if ( !hole ) {
256                 unsigned i;
257
258                 e->e_id = ei->bei_id;
259
260                 for ( i=0; i<nholes; i++) {
261                         if ( holes[i].id == e->e_id ) {
262                                 int j;
263                                 free(holes[i].dn.bv_val);
264                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
265                                 holes[j].id = 0;
266                                 nholes--;
267                                 break;
268                         } else if ( holes[i].id > e->e_id ) {
269                                 break;
270                         }
271                 }
272         }
273         return rc;
274 }
275
276 ID bdb_tool_entry_put(
277         BackendDB *be,
278         Entry *e,
279         struct berval *text )
280 {
281         int rc;
282         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
283         DB_TXN *tid = NULL;
284         Operation op = {0};
285
286         assert( be != NULL );
287         assert( slapMode & SLAP_TOOL_MODE );
288
289         assert( text );
290         assert( text->bv_val );
291         assert( text->bv_val[0] == '\0' );      /* overconservative? */
292
293         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
294                 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
295
296         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
297                 bdb->bi_db_opflags );
298         if( rc != 0 ) {
299                 snprintf( text->bv_val, text->bv_len,
300                         "txn_begin failed: %s (%d)",
301                         db_strerror(rc), rc );
302                 Debug( LDAP_DEBUG_ANY,
303                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
304                          text->bv_val, 0, 0 );
305                 return NOID;
306         }
307
308         op.o_bd = be;
309         op.o_tmpmemctx = NULL;
310         op.o_tmpmfuncs = &ch_mfuncs;
311
312         /* add dn2id indices */
313         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
314         if( rc != 0 ) {
315                 goto done;
316         }
317
318         /* id2entry index */
319         rc = bdb_id2entry_add( be, tid, e );
320         if( rc != 0 ) {
321                 snprintf( text->bv_val, text->bv_len,
322                                 "id2entry_add failed: %s (%d)",
323                                 db_strerror(rc), rc );
324                 Debug( LDAP_DEBUG_ANY,
325                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
326                         text->bv_val, 0, 0 );
327                 goto done;
328         }
329
330         rc = bdb_index_entry_add( &op, tid, e );
331         if( rc != 0 ) {
332                 snprintf( text->bv_val, text->bv_len,
333                                 "index_entry_add failed: %s (%d)",
334                                 db_strerror(rc), rc );
335                 Debug( LDAP_DEBUG_ANY,
336                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
337                         text->bv_val, 0, 0 );
338                 goto done;
339         }
340
341 done:
342         if( rc == 0 ) {
343                 rc = TXN_COMMIT( tid, 0 );
344                 if( rc != 0 ) {
345                         snprintf( text->bv_val, text->bv_len,
346                                         "txn_commit failed: %s (%d)",
347                                         db_strerror(rc), rc );
348                         Debug( LDAP_DEBUG_ANY,
349                                 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
350                                 text->bv_val, 0, 0 );
351                         e->e_id = NOID;
352                 }
353
354         } else {
355                 TXN_ABORT( tid );
356                 snprintf( text->bv_val, text->bv_len,
357                         "txn_aborted! %s (%d)",
358                         db_strerror(rc), rc );
359                 Debug( LDAP_DEBUG_ANY,
360                         "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
361                         text->bv_val, 0, 0 );
362                 e->e_id = NOID;
363         }
364
365         return e->e_id;
366 }
367
368 int bdb_tool_entry_reindex(
369         BackendDB *be,
370         ID id )
371 {
372         struct bdb_info *bi = (struct bdb_info *) be->be_private;
373         int rc;
374         Entry *e;
375         DB_TXN *tid = NULL;
376         Operation op = {0};
377
378         Debug( LDAP_DEBUG_ARGS,
379                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
380                 (long) id, 0, 0 );
381
382         e = bdb_tool_entry_get( be, id );
383
384         if( e == NULL ) {
385                 Debug( LDAP_DEBUG_ANY,
386                         LDAP_XSTRING(bdb_tool_entry_reindex)
387                         ": could not locate id=%ld\n",
388                         (long) id, 0, 0 );
389                 return -1;
390         }
391
392         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
393         if( rc != 0 ) {
394                 Debug( LDAP_DEBUG_ANY,
395                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
396                         "txn_begin failed: %s (%d)\n",
397                         db_strerror(rc), rc, 0 );
398                 goto done;
399         }
400         
401         /*
402          * just (re)add them for now
403          * assume that some other routine (not yet implemented)
404          * will zap index databases
405          *
406          */
407
408         Debug( LDAP_DEBUG_TRACE,
409                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
410                 (long) id, e->e_dn, 0 );
411
412         op.o_bd = be;
413         op.o_tmpmemctx = NULL;
414         op.o_tmpmfuncs = &ch_mfuncs;
415
416 #ifndef BDB_HIER
417         /* add dn2id indices */
418         rc = bdb_dn2id_add( &op, tid, NULL, e );
419         if( rc != 0 && rc != DB_KEYEXIST ) {
420                 Debug( LDAP_DEBUG_ANY,
421                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
422                         ": dn2id_add failed: %s (%d)\n",
423                         db_strerror(rc), rc, 0 );
424                 goto done;
425         }
426 #endif
427
428         rc = bdb_index_entry_add( &op, tid, e );
429
430 done:
431         if( rc == 0 ) {
432                 rc = TXN_COMMIT( tid, 0 );
433                 if( rc != 0 ) {
434                         Debug( LDAP_DEBUG_ANY,
435                                 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
436                                 ": txn_commit failed: %s (%d)\n",
437                                 db_strerror(rc), rc, 0 );
438                         e->e_id = NOID;
439                 }
440
441         } else {
442                 TXN_ABORT( tid );
443                 Debug( LDAP_DEBUG_ANY,
444                         "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
445                         ": txn_aborted! %s (%d)\n",
446                         db_strerror(rc), rc, 0 );
447                 e->e_id = NOID;
448         }
449         bdb_entry_release( &op, e, 0 );
450
451         return rc;
452 }
453
454 ID bdb_tool_entry_modify(
455         BackendDB *be,
456         Entry *e,
457         struct berval *text )
458 {
459         int rc;
460         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
461         DB_TXN *tid = NULL;
462         Operation op = {0};
463
464         assert( be != NULL );
465         assert( slapMode & SLAP_TOOL_MODE );
466
467         assert( text );
468         assert( text->bv_val );
469         assert( text->bv_val[0] == '\0' );      /* overconservative? */
470
471         assert ( e->e_id != NOID );
472         assert ( e->e_id != 0 );
473
474         Debug( LDAP_DEBUG_TRACE,
475                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
476                 (long) e->e_id, e->e_dn, 0 );
477
478         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
479                 bdb->bi_db_opflags );
480         if( rc != 0 ) {
481                 snprintf( text->bv_val, text->bv_len,
482                         "txn_begin failed: %s (%d)",
483                         db_strerror(rc), rc );
484                 Debug( LDAP_DEBUG_ANY,
485                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
486                          text->bv_val, 0, 0 );
487                 return NOID;
488         }
489
490         op.o_bd = be;
491         op.o_tmpmemctx = NULL;
492         op.o_tmpmfuncs = &ch_mfuncs;
493
494         /* id2entry index */
495         rc = bdb_id2entry_update( be, tid, e );
496         if( rc != 0 ) {
497                 snprintf( text->bv_val, text->bv_len,
498                                 "id2entry_add failed: %s (%d)",
499                                 db_strerror(rc), rc );
500                 Debug( LDAP_DEBUG_ANY,
501                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
502                         text->bv_val, 0, 0 );
503                 goto done;
504         }
505
506         rc = bdb_index_entry_del( &op, tid, e );
507         if( rc != 0 ) {
508                 snprintf( text->bv_val, text->bv_len,
509                                 "index_entry_del failed: %s (%d)",
510                                 db_strerror(rc), rc );
511                 Debug( LDAP_DEBUG_ANY,
512                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
513                         text->bv_val, 0, 0 );
514                 goto done;
515         }
516
517         rc = bdb_index_entry_add( &op, tid, e );
518         if( rc != 0 ) {
519                 snprintf( text->bv_val, text->bv_len,
520                                 "index_entry_add failed: %s (%d)",
521                                 db_strerror(rc), rc );
522                 Debug( LDAP_DEBUG_ANY,
523                         "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
524                         text->bv_val, 0, 0 );
525                 goto done;
526         }
527
528 done:
529         if( rc == 0 ) {
530                 rc = TXN_COMMIT( tid, 0 );
531                 if( rc != 0 ) {
532                         snprintf( text->bv_val, text->bv_len,
533                                         "txn_commit failed: %s (%d)",
534                                         db_strerror(rc), rc );
535                         Debug( LDAP_DEBUG_ANY,
536                                 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
537                                 "%s\n", text->bv_val, 0, 0 );
538                         e->e_id = NOID;
539                 }
540
541         } else {
542                 TXN_ABORT( tid );
543                 snprintf( text->bv_val, text->bv_len,
544                         "txn_aborted! %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                 e->e_id = NOID;
550         }
551
552         return e->e_id;
553 }