summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/attrlist.c
blob: e57e473c61e4247d57578d2aae24f5243059f093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include "slap.h"

void
attrlist_free(Slapi_Attr *alist)
{
	Slapi_Attr *a, *next;
	for ( a = alist; a != NULL; a = next )
	{
		next = a->a_next;
		slapi_attr_free( &a );
	}
}

/*
 * Search for the attribute.
 * If not found then create it,
 * and add it to the end of the list.
 * Return 0 for found, 1 for created.
 */
int
attrlist_find_or_create(Slapi_Attr **alist, const char *type, Slapi_Attr ***a)
{
	return attrlist_find_or_create_locking_optional(alist, type, a, PR_TRUE, PR_TRUE);
}

int
attrlist_find_or_create_locking_optional(Slapi_Attr **alist, const char *type, Slapi_Attr ***a, PRBool use_lock, PRBool ref_count)
{
	int rc= 0; /* found */
	if ( *a==NULL )
	{
		for ( *a = alist; **a != NULL; *a = &(**a)->a_next ) {
			if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
				break;
			}
		}
	}

	if( **a==NULL )
	{
		**a = slapi_attr_new();
		slapi_attr_init_locking_optional(**a, type, use_lock, ref_count);
		rc= 1; /* created */
	}
	return rc;
}

/*
 * attrlist_merge - merge the given type and value with the list of
 * attributes in attrs.
 */
void
attrlist_merge(Slapi_Attr **alist, const char *type, struct berval **vals)
{
	Slapi_Value **values= NULL;
	valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
	attrlist_merge_valuearray(alist,type,values);
	valuearray_free(&values);
}


/*
 * attrlist_merge_valuearray - merge the given type and value with the list of
 * attributes in attrs.
 */
void
attrlist_merge_valuearray(Slapi_Attr **alist, const char *type, Slapi_Value **vals)
{
	Slapi_Attr	**a= NULL;
	attrlist_find_or_create(alist, type, &a);
	valueset_add_valuearray( &(*a)->a_present_values, vals );
}    


/*
 * attrlist_find - find and return attribute type in list a
 */

Slapi_Attr *
attrlist_find(Slapi_Attr *a, const char *type)
{
	for ( ; a != NULL; a = a->a_next ) {
		if ( strcasecmp( a->a_type, type ) == 0 ) {
			return( a );
		}
	}

	return( NULL );
}


/*
 * attrlist_count_subtypes
 *
 * Returns a count attributes which conform to type
 * in the attr list a.  This count includes all subtypes of
 * type
 */
int
attrlist_count_subtypes(Slapi_Attr *a, const char *type)
{
	int counter = 0;

	for ( ; a != NULL; a = a->a_next ) {
		if ( slapi_attr_type_cmp( type , a->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
			counter++;
		}
	}

	return( counter );
}

/*
 * attrlist_find_ex
 *
 * Finds the first subtype in the list which matches "type"
 * starting at the beginning or hint depending on whether
 * hint has a value
 * 
 * It is intended that hint be zero when first called and then
 * passed back in on subsequent calls until 0 is returned to mark
 * the end of the filtered list
 */
Slapi_Attr *
attrlist_find_ex(
    Slapi_Attr *a, 
    const char *type, 
    int *type_name_disposition, /* pass null if you're not interested */
    char** actual_type_name,    /* pass null if you're not interested */
    void **hint
)
{
	Slapi_Attr **attr_cursor = (Slapi_Attr **)hint;

	if (type_name_disposition) *type_name_disposition = 0;
	if (actual_type_name) *actual_type_name = NULL;

	if(*attr_cursor == NULL)
            *attr_cursor = a; /* start at the beginning of the list */
        else 
            *attr_cursor = (*attr_cursor)->a_next;

	while(*attr_cursor != NULL) {

		/* Determine whether the two types are related:*/
		if ( slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
			/* We got a match. Now figure out if we matched because it was a subtype */
                    if (type_name_disposition) {
			if ( 0 == slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_EXACT) ) {
				*type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
			} else {
				*type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE;
			}
                    }

                    if (actual_type_name) {
			*actual_type_name = (*attr_cursor)->a_type;
                    }

                    a = *attr_cursor;  /* the attribute to return */
                    return( a );

		}

		*attr_cursor = (*attr_cursor)->a_next; /* no match, move cursor */
	}

	return( NULL );
}

/*
 * attr_remove - remove the attribute from the list of attributes
 */
Slapi_Attr *
attrlist_remove(Slapi_Attr **attrs, const char *type)
{
	Slapi_Attr **a;
	Slapi_Attr *save= NULL;
	for ( a = attrs; *a != NULL; a = &(*a)->a_next )
	{
		if ( strcasecmp( (*a)->a_type, type ) == 0 )
		{
			break;
		}
	}
	if (*a != NULL)
	{
		save = *a;
		*a = (*a)->a_next;
	}
	return save;
}

void
attrlist_add(Slapi_Attr **attrs, Slapi_Attr *a)
{
	a->a_next= *attrs;
	*attrs= a;
}

/*
 * attrlist_delete - delete the attribute type in list pointed to by attrs
 * return	0	deleted ok
 * 		1	not found in list a
 * 		-1	something bad happened
 */

int
attrlist_delete(Slapi_Attr **attrs, const char *type)
{
	Slapi_Attr	**a;
	Slapi_Attr	*save;

	for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
		if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
			break;
		}
	}

	if ( *a == NULL ) {
		return( 1 );
	}

	save = *a;
	*a = (*a)->a_next;
	slapi_attr_free( &save );

	return( 0 );
}

/*
 * attrlist_replace - replace the attribute value(s) with this value(s)
 */
void attrlist_replace(Slapi_Attr **alist, const char *type, struct berval **vals)
{
    Slapi_Attr **a = NULL;
    Slapi_Value **values = NULL;

    if (vals == NULL || vals[0] == NULL) {
        (void)attrlist_delete(alist, type);
    } else {
        attrlist_find_or_create(alist, type, &a);
        valuearray_init_bervalarray(vals, &values);
        attr_replace(*a, values);
    }
}