1 /* entry.c - routines for dealing with entries */
10 #include <ac/string.h>
14 static unsigned char *ebuf; /* buf returned by entry2str */
15 static unsigned char *ecur; /* pointer to end of currently used ebuf */
16 static int emaxsize;/* max size of ebuf */
27 int vlen, nvals, maxvals;
29 struct berval *vals[2];
33 * In string format, an entry looks like this:
37 * [<attr>:[:] <value>\n]
38 * [<tab><continuedvalue>\n]*
41 * If a double colon is used after a type, it means the
42 * following value is encoded as a base 64 string. This
43 * happens if the value contains a non-printing character
47 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
48 s ? s : "NULL", 0, 0 );
50 /* check to see if there's an id included */
52 if ( isdigit( (unsigned char) *s ) ) {
54 if ( (s = str_getline( &next )) == NULL ) {
55 Debug( LDAP_DEBUG_TRACE,
56 "<= str2entry NULL (missing newline after id)\n",
62 /* initialize reader/writer lock */
63 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
66 Debug( LDAP_DEBUG_TRACE,
67 "<= str2entry NULL (entry allocation failed)\n",
80 while ( (s = str_getline( &next )) != NULL ) {
81 if ( *s == '\n' || *s == '\0' ) {
85 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
86 Debug( LDAP_DEBUG_TRACE,
87 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
91 if ( strcasecmp( type, ptype ) != 0 ) {
92 strncpy( ptype, type, sizeof(ptype) - 1 );
98 if ( strcasecmp( type, "dn" ) == 0 ) {
99 if ( e->e_dn != NULL ) {
100 Debug( LDAP_DEBUG_ANY,
101 "str2entry: entry %lu has multiple dns \"%s\" and \"%s\" (second ignored)\n",
102 e->e_id, e->e_dn, value );
105 e->e_dn = ch_strdup( value );
107 if ( e->e_ndn != NULL ) {
108 Debug( LDAP_DEBUG_ANY,
109 "str2entry: entry %lu already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
110 e->e_id, e->e_ndn, value );
113 e->e_ndn = dn_normalize_case( ch_strdup( value ) );
119 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
121 Debug( LDAP_DEBUG_TRACE,
122 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
129 /* check to make sure there was a dn: line */
130 if ( e->e_dn == NULL ) {
131 Debug( LDAP_DEBUG_ANY, "str2entry: entry %lu has no dn\n",
137 if ( e->e_ndn == NULL ) {
138 Debug( LDAP_DEBUG_ANY,
139 "str2entry: entry %lu (\"%s\") has no normalized dn\n",
140 e->e_id, e->e_dn, 0 );
145 Debug(LDAP_DEBUG_TRACE, "<= str2entry 0x%lx\n", (unsigned long)e, 0,0);
150 #define GRABSIZE BUFSIZ
152 #define MAKE_SPACE( n ) { \
153 while ( ecur + (n) > ebuf + emaxsize ) { \
155 offset = (int) (ecur - ebuf); \
156 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
157 emaxsize + GRABSIZE ); \
158 emaxsize += GRABSIZE; \
159 ecur = ebuf + offset; \
175 * In string format, an entry looks like this:
178 * [<attr>: <value>\n]*
186 sprintf( (char *) ecur, "%ld\n", e->e_id );
187 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
191 if ( e->e_dn != NULL ) {
193 tmplen = strlen( e->e_dn );
194 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
195 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
198 /* put the attributes */
199 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
200 /* put "<type>:[:] <value>" line for each value */
201 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
203 tmplen = strlen( a->a_type );
204 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
205 put_type_and_value( (char **) &ecur, a->a_type,
206 bv->bv_val, bv->bv_len );
213 return( (char *) ebuf );
217 entry_free( Entry *e )
222 /* check that no reader/writer locks exist */
224 if ( ldap_pvt_thread_rdwr_wtrylock( &e->e_rdwr ) ==
225 LDAP_PVT_THREAD_EBUSY )
227 Debug( LDAP_DEBUG_ANY, "entry_free(%ld): active (%d, %d)\n",
229 ldap_pvt_thread_rdwr_readers( &e->e_rdwr ),
230 ldap_pvt_thread_rdwr_writers( &e->e_rdwr ));
233 assert(!ldap_pvt_thread_rdwr_active( &e->e_rdwr ));
237 if ( e->e_dn != NULL ) {
240 if ( e->e_ndn != NULL ) {
243 for ( a = e->e_attrs; a != NULL; a = next ) {
251 entry_rdwr_lock(Entry *e, int rw)
253 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
254 rw ? "w" : "r", e->e_id, 0);
256 return ldap_pvt_thread_rdwr_wlock(&e->e_rdwr);
258 return ldap_pvt_thread_rdwr_rlock(&e->e_rdwr);
262 entry_rdwr_rlock(Entry *e)
264 return entry_rdwr_lock( e, 0 );
268 entry_rdwr_wlock(Entry *e)
270 return entry_rdwr_lock( e, 1 );
274 entry_rdwr_trylock(Entry *e, int rw)
276 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
277 rw ? "w" : "r", e->e_id, 0);
279 return ldap_pvt_thread_rdwr_wtrylock(&e->e_rdwr);
281 return ldap_pvt_thread_rdwr_rtrylock(&e->e_rdwr);
285 entry_rdwr_unlock(Entry *e, int rw)
287 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
288 rw ? "w" : "r", e->e_id, 0);
290 return ldap_pvt_thread_rdwr_wunlock(&e->e_rdwr);
292 return ldap_pvt_thread_rdwr_runlock(&e->e_rdwr);
296 entry_rdwr_runlock(Entry *e)
298 return entry_rdwr_unlock( e, 0 );
302 entry_rdwr_wunlock(Entry *e)
304 return entry_rdwr_unlock( e, 1 );
308 entry_rdwr_init(Entry *e)
310 return ldap_pvt_thread_rdwr_init( &e->e_rdwr );