1 /* entry.c - routines for dealing with entries */
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 */
26 int vlen, nvals, maxvals;
28 struct berval *vals[2];
32 * In string format, an entry looks like this:
36 * [<attr>:[:] <value>\n]
37 * [<tab><continuedvalue>\n]*
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
46 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
47 s ? s : "NULL", 0, 0 );
49 /* check to see if there's an id included */
51 if ( isdigit( *s ) ) {
53 if ( (s = ldif_getline( &next )) == NULL ) {
54 Debug( LDAP_DEBUG_TRACE,
55 "<= str2entry NULL (missing newline after id)\n",
61 /* initialize reader/writer lock */
62 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
65 Debug( LDAP_DEBUG_TRACE,
66 "<= str2entry NULL (entry allocation failed)\n",
79 while ( (s = ldif_getline( &next )) != NULL ) {
80 if ( *s == '\n' || *s == '\0' ) {
84 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
85 Debug( LDAP_DEBUG_TRACE,
86 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
90 if ( strcasecmp( type, ptype ) != 0 ) {
91 strncpy( ptype, type, sizeof(ptype) - 1 );
97 if ( strcasecmp( type, "dn" ) == 0 ) {
98 if ( e->e_dn != NULL ) {
99 Debug( LDAP_DEBUG_ANY,
100 "str2entry: entry %lu has multiple dns \"%s\" and \"%s\" (second ignored)\n",
101 e->e_id, e->e_dn, value );
104 e->e_dn = ch_strdup( value );
106 if ( e->e_ndn != NULL ) {
107 Debug( LDAP_DEBUG_ANY,
108 "str2entry: entry %lu already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
109 e->e_id, e->e_ndn, value );
112 e->e_ndn = dn_normalize_case( ch_strdup( value ) );
118 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
120 Debug( LDAP_DEBUG_TRACE,
121 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
128 /* check to make sure there was a dn: line */
129 if ( e->e_dn == NULL ) {
130 Debug( LDAP_DEBUG_ANY, "str2entry: entry %lu has no dn\n",
136 if ( e->e_ndn == NULL ) {
137 Debug( LDAP_DEBUG_ANY,
138 "str2entry: entry %lu (\"%s\") has no normalized dn\n",
139 e->e_id, e->e_dn, 0 );
144 Debug(LDAP_DEBUG_TRACE, "<= str2entry 0x%lx\n", (unsigned long)e, 0,0);
149 #define GRABSIZE BUFSIZ
151 #define MAKE_SPACE( n ) { \
152 while ( ecur + (n) > ebuf + emaxsize ) { \
154 offset = (int) (ecur - ebuf); \
155 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
156 emaxsize + GRABSIZE ); \
157 emaxsize += GRABSIZE; \
158 ecur = ebuf + offset; \
174 * In string format, an entry looks like this:
177 * [<attr>: <value>\n]*
185 sprintf( (char *) ecur, "%ld\n", e->e_id );
186 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
190 if ( e->e_dn != NULL ) {
192 tmplen = strlen( e->e_dn );
193 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
194 ldif_put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
197 /* put the attributes */
198 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
199 /* put "<type>:[:] <value>" line for each value */
200 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
202 tmplen = strlen( a->a_type );
203 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
204 ldif_put_type_and_value( (char **) &ecur, a->a_type,
205 bv->bv_val, bv->bv_len );
212 return( (char *) ebuf );
216 entry_free( Entry *e )
221 /* XXX check that no reader/writer locks exist */
223 assert( !ldap_pvt_thread_rdwr_wchk(&e->e_rdwr) &&
224 !ldap_pvt_thread_rdwr_rchk(&e->e_rdwr) );
227 if ( e->e_dn != NULL ) {
230 if ( e->e_ndn != NULL ) {
233 for ( a = e->e_attrs; a != NULL; a = next ) {
241 entry_rdwr_lock(Entry *e, int rw)
243 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
244 rw ? "w" : "r", e->e_id, 0);
246 return ldap_pvt_thread_rdwr_wlock(&e->e_rdwr);
248 return ldap_pvt_thread_rdwr_rlock(&e->e_rdwr);
252 entry_rdwr_rlock(Entry *e)
254 return entry_rdwr_lock( e, 0 );
258 entry_rdwr_wlock(Entry *e)
260 return entry_rdwr_lock( e, 1 );
264 entry_rdwr_unlock(Entry *e, int rw)
266 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
267 rw ? "w" : "r", e->e_id, 0);
269 return ldap_pvt_thread_rdwr_wunlock(&e->e_rdwr);
271 return ldap_pvt_thread_rdwr_runlock(&e->e_rdwr);
275 entry_rdwr_runlock(Entry *e)
277 return entry_rdwr_unlock( e, 0 );
281 entry_rdwr_wunlock(Entry *e)
283 return entry_rdwr_unlock( e, 1 );
287 entry_rdwr_init(Entry *e)
289 return ldap_pvt_thread_rdwr_init( &e->e_rdwr );