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