]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Fix build errors
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         int rc;
22         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
23
24         assert( be != NULL );
25         assert( bdb != NULL );
26         
27         rc = bdb->bi_id2entry->bdi_db->cursor(
28                 bdb->bi_id2entry->bdi_db, NULL, &cursor, 0 );
29         if( rc != 0 ) {
30                 return NOID;
31         }
32
33         /* initialize key and data thangs */
34         DBTzero( &key );
35         DBTzero( &data );
36         key.flags = DB_DBT_REALLOC;
37         data.flags = DB_DBT_REALLOC;
38
39         return 0;
40 }
41
42 int bdb_tool_entry_close(
43         BackendDB *be )
44 {
45         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
46
47         assert( be != NULL );
48
49         if( key.data ) {
50                 ch_free( key.data );
51                 key.data = NULL;
52         }
53         if( data.data ) {
54                 ch_free( data.data );
55                 data.data = NULL;
56         }
57
58         if( cursor ) {
59                 cursor->c_close( cursor );
60                 cursor = NULL;
61         }
62
63         return 0;
64 }
65
66 ID bdb_tool_entry_next(
67         BackendDB *be )
68 {
69         int rc;
70         ID id;
71
72         assert( be != NULL );
73         assert( slapMode & SLAP_TOOL_MODE );
74         assert( cursor != NULL );
75
76         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
77
78         if( rc != 0 ) {
79                 return NOID;
80         }
81
82         if( data.data == NULL ) {
83                 return NOID;
84         }
85
86         AC_MEMCPY( &id, key.data, key.size );
87         return id;
88 }
89
90 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
91 {
92         int rc;
93         Entry *e;
94         struct berval bv;
95
96         assert( be != NULL );
97         assert( slapMode & SLAP_TOOL_MODE );
98         assert( data.data != NULL );
99
100         DBT2bv( &data, &bv );
101
102         rc = entry_decode( &bv, &e );
103
104         if( rc == LDAP_SUCCESS ) {
105                 e->e_id = id;
106         }
107
108         return e;
109 }
110
111 ID bdb_tool_entry_put(
112         BackendDB *be,
113         Entry *e )
114 {
115         int rc;
116         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
117         DB_TXN *tid;
118
119         assert( be != NULL );
120         assert( slapMode & SLAP_TOOL_MODE );
121
122         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
123                 e->e_id, e->e_dn, 0 );
124
125         rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 0 );
126         if( rc != 0 ) {
127                 Debug( LDAP_DEBUG_ANY,
128                         "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
129                         db_strerror(rc), rc, 0 );
130                 return NOID;
131         }
132
133         rc = bdb_next_id( be, tid, &e->e_id );
134         if( rc != 0 ) {
135                 Debug( LDAP_DEBUG_ANY,
136                         "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
137                         db_strerror(rc), rc, 0 );
138                 goto done;
139         }
140
141         /* add dn2id indices */
142         rc = bdb_dn2id_add( be, tid, e->e_ndn, e->e_id );
143         if( rc != 0 ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
146                         db_strerror(rc), rc, 0 );
147                 goto done;
148         }
149
150         /* id2entry index */
151         rc = bdb_id2entry_add( be, tid, e );
152         if( rc != 0 ) {
153                 Debug( LDAP_DEBUG_ANY,
154                         "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
155                         db_strerror(rc), rc, 0 );
156                 goto done;
157         }
158
159 #if 0
160         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
161         if( rc != 0 ) {
162                 goto done;
163         }
164 #endif
165
166 done:
167         if( rc == 0 ) {
168                 rc = txn_commit( tid, 0 );
169                 if( rc != 0 ) {
170                         Debug( LDAP_DEBUG_ANY,
171                                 "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
172                                 db_strerror(rc), rc, 0 );
173                         e->e_id = NOID;
174                 }
175
176         } else {
177                 Debug( LDAP_DEBUG_ANY,
178                         "=> bdb_tool_entry_put: txn_aborted!\n",
179                         0, 0, 0 );
180                 txn_abort( tid );
181                 e->e_id = NOID;
182         }
183
184         return e->e_id;
185 }
186
187 #if 0
188 int bdb_tool_entry_reindex(
189         BackendDB *be,
190         ID id )
191 {
192         struct bdb_dbinfo *bdi = (struct bdb_dbinfo *) be->be_private;
193         int rc;
194         Entry *e;
195         DB_TXN *tid;
196
197         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
198                 (long) id, 0, 0 );
199
200         rc = txn_begin( bdi->bdi_db_env, NULL, &tid, 0 );
201         
202         e = bdb_tool_entry_get( be, tid, id );
203
204         if( e == NULL ) {
205                 Debug( LDAP_DEBUG_ANY,
206                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
207                         (long) id, 0, 0 );
208                 txn_abort( tid );
209                 return -1;
210         }
211
212         /*
213          * just (re)add them for now
214          * assume that some other routine (not yet implemented)
215          * will zap index databases
216          *
217          */
218
219         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
220                 id, e->e_dn, 0 );
221
222         rc = index_entry_add( be, e, e->e_attrs );
223
224         entry_free( e );
225
226         return rc;
227 }
228 #endif