]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Import alias deref finding bug fix from devel
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
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/socket.h>
13
14 #include "slap.h"
15 #include "back-ldbm.h"
16
17 /*
18  * This routine adds (or updates) an entry on disk.
19  * The cache should already be updated. 
20  */
21
22 int
23 id2entry_add( Backend *be, Entry *e )
24 {
25         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
26         DBCache *db;
27         Datum           key, data;
28         int             len, rc, flags;
29
30         ldbm_datum_init( key );
31         ldbm_datum_init( data );
32
33         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %ld, \"%s\" )\n", e->e_id,
34             e->e_dn, 0 );
35
36         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
37             == NULL ) {
38                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
39                     LDBM_SUFFIX, 0, 0 );
40                 return( -1 );
41         }
42
43         key.dptr = (char *) &e->e_id;
44         key.dsize = sizeof(ID);
45
46         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
47         data.dptr = entry2str( e, &len );
48         data.dsize = len + 1;
49
50         /* store it */
51         flags = LDBM_REPLACE;
52         rc = ldbm_cache_store( db, key, data, flags );
53
54         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
55
56         ldbm_cache_close( be, db );
57
58         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
59
60         return( rc );
61 }
62
63 int
64 id2entry_delete( Backend *be, Entry *e )
65 {
66         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67         DBCache *db;
68         Datum           key;
69         int             rc;
70
71         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %ld, \"%s\" )\n", e->e_id,
72             e->e_dn, 0 );
73
74 #ifdef notdef
75 #ifdef LDAP_RDWR_DEBUG
76         /* check for writer lock */
77         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
78 #endif
79 #endif
80
81         ldbm_datum_init( key );
82
83         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
84                 == NULL ) {
85                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
86                     LDBM_SUFFIX, 0, 0 );
87                 return( -1 );
88         }
89
90         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
91                 Debug(LDAP_DEBUG_ANY, "could not delete %ld (%s) from cache\n",
92                     e->e_id, e->e_dn, 0 );
93         }
94
95         key.dptr = (char *) &e->e_id;
96         key.dsize = sizeof(ID);
97
98         rc = ldbm_cache_delete( db, key );
99
100         ldbm_cache_close( be, db );
101
102         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
103         return( rc );
104 }
105
106 /* returns entry with reader/writer lock */
107 Entry *
108 id2entry_rw( Backend *be, ID id, int rw )
109 {
110         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
111         DBCache *db;
112         Datum           key, data;
113         Entry           *e;
114
115         ldbm_datum_init( key );
116         ldbm_datum_init( data );
117
118         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
119                 rw ? "w" : "r", id, 0 );
120
121         if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
122                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
123                         rw ? "w" : "r", id, (unsigned long) e );
124                 return( e );
125         }
126
127         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
128                 == NULL ) {
129                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
130                     LDBM_SUFFIX, 0, 0 );
131                 return( NULL );
132         }
133
134         key.dptr = (char *) &id;
135         key.dsize = sizeof(ID);
136
137         data = ldbm_cache_fetch( db, key );
138
139         if ( data.dptr == NULL ) {
140                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
141                         rw ? "w" : "r", id, 0 );
142                 ldbm_cache_close( be, db );
143                 return( NULL );
144         }
145
146         e = str2entry( data.dptr );
147         ldbm_datum_free( db->dbc_db, data );
148         ldbm_cache_close( be, db );
149
150         if ( e == NULL ) {
151                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (failed)\n",
152                         rw ? "w" : "r", id, 0 );
153                 return( NULL );
154         }
155
156         e->e_id = id;
157
158         if( cache_add_entry_rw( &li->li_cache, e, rw ) != 0 ) {
159                 entry_free( e );
160
161                 /* XXX this is a kludge.
162                  * maybe the entry got added underneath us
163                  * There are many underlying race condtions in the cache/disk code.
164                  */
165                 if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
166                         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (cache)\n",
167                                 rw ? "w" : "r", id, (unsigned long) e );
168                         return( e );
169                 }
170
171                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (cache add failed)\n",
172                         rw ? "w" : "r", id, 0 );
173                 return NULL;
174         }
175
176         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) 0x%lx (disk)\n",
177                 rw ? "w" : "r", id, (unsigned long) e );
178
179         return( e );
180 }