]> git.sur5r.net Git - openldap/blob - servers/slapd/entry.c
Merged in per cache entry reader/writer locks from OPENLDAP_DEVEL_THREAD
[openldap] / servers / slapd / entry.c
1 /* entry.c - routines for dealing with entries */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include "slap.h"
9
10 void    entry_free();
11 char    *entry2str();
12
13 static unsigned char    *ebuf;  /* buf returned by entry2str             */
14 static unsigned char    *ecur;  /* pointer to end of currently used ebuf */
15 static int              emaxsize;/* max size of ebuf                     */
16
17 Entry *
18 str2entry( char *s )
19 {
20         int             i;
21         Entry           *e;
22         Attribute       **a;
23         char            *type;
24         char            *value;
25         char            *next;
26         int             vlen, nvals, maxvals;
27         struct berval   bval;
28         struct berval   *vals[2];
29         char            ptype[64];
30
31         /*
32          * In string format, an entry looks like this:
33          *
34          *      <id>\n
35          *      dn: <dn>\n
36          *      [<attr>:[:] <value>\n]
37          *      [<tab><continuedvalue>\n]*
38          *      ...
39          *
40          * If a double colon is used after a type, it means the
41          * following value is encoded as a base 64 string.  This
42          * happens if the value contains a non-printing character
43          * or newline.
44          */
45
46         Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
47                 s ? s : "NULL", 0, 0 );
48
49         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
50         /* initialize reader/writer lock */
51         entry_rdwr_init(e);
52
53         /* check to see if there's an id included */
54         next = s;
55         if ( isdigit( *s ) ) {
56                 e->e_id = atoi( s );
57                 if ( (s = str_getline( &next )) == NULL ) {
58                         Debug( LDAP_DEBUG_TRACE,
59                             "<= str2entry NULL (missing newline after id)\n",
60                             0, 0, 0 );
61                         return( NULL );
62                 }
63         }
64
65         /* dn + attributes */
66         e->e_attrs = NULL;
67         vals[0] = &bval;
68         vals[1] = NULL;
69         ptype[0] = '\0';
70         while ( (s = str_getline( &next )) != NULL ) {
71                 if ( *s == '\n' || *s == '\0' ) {
72                         break;
73                 }
74
75                 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
76                         Debug( LDAP_DEBUG_TRACE,
77                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
78                         continue;
79                 }
80
81                 if ( strcasecmp( type, ptype ) != 0 ) {
82                         strncpy( ptype, type, sizeof(ptype) - 1 );
83                         nvals = 0;
84                         maxvals = 0;
85                         a = NULL;
86                 }
87                 if ( strcasecmp( type, "dn" ) == 0 ) {
88                         if ( e->e_dn != NULL ) {
89                                 Debug( LDAP_DEBUG_ANY,
90     "str2entry: entry %d has multiple dns \"%s\" and \"%s\" (second ignored)\n",
91                                     e->e_id, e->e_dn, value );
92                                 continue;
93                         }
94                         e->e_dn = strdup( value );
95                         continue;
96                 }
97
98                 bval.bv_val = value;
99                 bval.bv_len = vlen;
100                 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
101                     != 0 ) {
102                         Debug( LDAP_DEBUG_TRACE,
103                             "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
104                         return( NULL );
105                 }
106                 nvals++;
107         }
108
109         /* check to make sure there was a dn: line */
110         if ( e->e_dn == NULL ) {
111                 Debug( LDAP_DEBUG_ANY, "str2entry: entry %d has no dn\n",
112                     e->e_id, 0, 0 );
113                 entry_free( e );
114                 return( NULL );
115         }
116
117         Debug( LDAP_DEBUG_TRACE, "<= str2entry 0x%x\n", e, 0, 0 );
118
119         return( e );
120 }
121
122 #define GRABSIZE        BUFSIZ
123
124 #define MAKE_SPACE( n ) { \
125                 while ( ecur + (n) > ebuf + emaxsize ) { \
126                         int     offset; \
127                         offset = (int) (ecur - ebuf); \
128                         ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
129                             emaxsize + GRABSIZE ); \
130                         emaxsize += GRABSIZE; \
131                         ecur = ebuf + offset; \
132                 } \
133 }
134
135 char *
136 entry2str(
137     Entry       *e,
138     int         *len,
139     int         printid
140 )
141 {
142         Attribute       *a;
143         struct berval   *bv;
144         int             i, tmplen;
145
146         /*
147          * In string format, an entry looks like this:
148          *      <id>\n
149          *      dn: <dn>\n
150          *      [<attr>: <value>\n]*
151          */
152
153         ecur = ebuf;
154
155         if ( printid ) {
156                 /* id + newline */
157                 MAKE_SPACE( 10 );
158                 sprintf( (char *) ecur, "%ld\n", e->e_id );
159                 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
160         }
161
162         /* put the dn */
163         if ( e->e_dn != NULL ) {
164                 /* put "dn: <dn>" */
165                 tmplen = strlen( e->e_dn );
166                 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
167                 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
168         }
169
170         /* put the attributes */
171         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
172                 /* put "<type>:[:] <value>" line for each value */
173                 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
174                         bv = a->a_vals[i];
175                         tmplen = strlen( a->a_type );
176                         MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
177                         put_type_and_value( (char **) &ecur, a->a_type,
178                             bv->bv_val, bv->bv_len );
179                 }
180         }
181         MAKE_SPACE( 1 );
182         *ecur = '\0';
183         *len = ecur - ebuf;
184
185         return( (char *) ebuf );
186 }
187
188 void
189 entry_free( Entry *e )
190 {
191         int             i;
192         Attribute       *a, *next;
193
194         /* XXX check that no reader/writer locks exist */
195         assert( !pthread_rdwr_wchk_np(&e->e_rdwr) &&
196                 !pthread_rdwr_rchk_np(&e->e_rdwr) );
197
198         if ( e->e_dn != NULL ) {
199                 free( e->e_dn );
200         }
201         for ( a = e->e_attrs; a != NULL; a = next ) {
202                 next = a->a_next;
203                 attr_free( a );
204         }
205         free( e );
206 }
207
208 int
209 entry_rdwr_lock(Entry *e, int rw)
210 {
211         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
212                 rw ? "w" : "r", e->e_id, 0);
213         if (rw)
214                 return pthread_rdwr_wlock_np(&e->e_rdwr);
215         else
216                 return pthread_rdwr_rlock_np(&e->e_rdwr);
217 }
218
219 int
220 entry_rdwr_rlock(Entry *e)
221 {
222         return entry_rdwr_lock( e, 0 );
223 }
224
225 int
226 entry_rdwr_wlock(Entry *e)
227 {
228         return entry_rdwr_lock( e, 1 );
229 }
230
231 int
232 entry_rdwr_unlock(Entry *e, int rw)
233 {
234         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
235                 rw ? "w" : "r", e->e_id, 0);
236         if (rw)
237                 return pthread_rdwr_wunlock_np(&e->e_rdwr);
238         else
239                 return pthread_rdwr_runlock_np(&e->e_rdwr);
240 }
241
242 int
243 entry_rdwr_runlock(Entry *e)
244 {
245         return entry_rdwr_unlock( e, 0 );
246 }
247
248 int
249 entry_rdwr_wunlock(Entry *e)
250 {
251         return entry_rdwr_unlock( e, 1 );
252 }
253
254 int
255 entry_rdwr_init(Entry *e)
256 {
257         return pthread_rdwr_init_np(&e->e_rdwr, NULL);
258 }
259