1 /* entry.c - routines for dealing with entries */
16 static unsigned char *ebuf; /* buf returned by entry2str */
17 static unsigned char *ecur; /* pointer to end of currently used ebuf */
18 static int emaxsize;/* max size of ebuf */
29 int vlen, nvals, maxvals;
31 struct berval *vals[2];
35 * In string format, an entry looks like this:
39 * [<attr>:[:] <value>\n]
40 * [<tab><continuedvalue>\n]*
43 * If a double colon is used after a type, it means the
44 * following value is encoded as a base 64 string. This
45 * happens if the value contains a non-printing character
49 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
50 s ? s : "NULL", 0, 0 );
52 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
53 /* initialize reader/writer lock */
56 /* check to see if there's an id included */
58 if ( isdigit( *s ) ) {
60 if ( (s = str_getline( &next )) == NULL ) {
61 Debug( LDAP_DEBUG_TRACE,
62 "<= str2entry NULL (missing newline after id)\n",
73 while ( (s = str_getline( &next )) != NULL ) {
74 if ( *s == '\n' || *s == '\0' ) {
78 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
79 Debug( LDAP_DEBUG_TRACE,
80 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
84 if ( strcasecmp( type, ptype ) != 0 ) {
85 strncpy( ptype, type, sizeof(ptype) - 1 );
90 if ( strcasecmp( type, "dn" ) == 0 ) {
91 if ( e->e_dn != NULL ) {
92 Debug( LDAP_DEBUG_ANY,
93 "str2entry: entry %d has multiple dns \"%s\" and \"%s\" (second ignored)\n",
94 e->e_id, e->e_dn, value );
97 e->e_dn = strdup( value );
103 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
105 Debug( LDAP_DEBUG_TRACE,
106 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
112 /* check to make sure there was a dn: line */
113 if ( e->e_dn == NULL ) {
114 Debug( LDAP_DEBUG_ANY, "str2entry: entry %d has no dn\n",
120 Debug( LDAP_DEBUG_TRACE, "<= str2entry 0x%x\n", e, 0, 0 );
125 #define GRABSIZE BUFSIZ
127 #define MAKE_SPACE( n ) { \
128 while ( ecur + (n) > ebuf + emaxsize ) { \
130 offset = (int) (ecur - ebuf); \
131 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
132 emaxsize + GRABSIZE ); \
133 emaxsize += GRABSIZE; \
134 ecur = ebuf + offset; \
150 * In string format, an entry looks like this:
153 * [<attr>: <value>\n]*
161 sprintf( (char *) ecur, "%ld\n", e->e_id );
162 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
166 if ( e->e_dn != NULL ) {
168 tmplen = strlen( e->e_dn );
169 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
170 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
173 /* put the attributes */
174 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
175 /* put "<type>:[:] <value>" line for each value */
176 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
178 tmplen = strlen( a->a_type );
179 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
180 put_type_and_value( (char **) &ecur, a->a_type,
181 bv->bv_val, bv->bv_len );
188 return( (char *) ebuf );
192 entry_free( Entry *e )
197 /* XXX check that no reader/writer locks exist */
199 assert( !pthread_rdwr_wchk_np(&e->e_rdwr) &&
200 !pthread_rdwr_rchk_np(&e->e_rdwr) );
203 if ( e->e_dn != NULL ) {
206 for ( a = e->e_attrs; a != NULL; a = next ) {
214 entry_rdwr_lock(Entry *e, int rw)
216 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
217 rw ? "w" : "r", e->e_id, 0);
219 return pthread_rdwr_wlock_np(&e->e_rdwr);
221 return pthread_rdwr_rlock_np(&e->e_rdwr);
225 entry_rdwr_rlock(Entry *e)
227 return entry_rdwr_lock( e, 0 );
231 entry_rdwr_wlock(Entry *e)
233 return entry_rdwr_lock( e, 1 );
237 entry_rdwr_unlock(Entry *e, int rw)
239 Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
240 rw ? "w" : "r", e->e_id, 0);
242 return pthread_rdwr_wunlock_np(&e->e_rdwr);
244 return pthread_rdwr_runlock_np(&e->e_rdwr);
248 entry_rdwr_runlock(Entry *e)
250 return entry_rdwr_unlock( e, 0 );
254 entry_rdwr_wunlock(Entry *e)
256 return entry_rdwr_unlock( e, 1 );
260 entry_rdwr_init(Entry *e)
262 return pthread_rdwr_init_np(&e->e_rdwr, NULL);