attrlist.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include "slap.h"
  39. void
  40. attrlist_free(Slapi_Attr *alist)
  41. {
  42. Slapi_Attr *a, *next;
  43. for ( a = alist; a != NULL; a = next )
  44. {
  45. next = a->a_next;
  46. slapi_attr_free( &a );
  47. }
  48. }
  49. /*
  50. * Search for the attribute.
  51. * If not found then create it,
  52. * and add it to the end of the list.
  53. * Return 0 for found, 1 for created.
  54. */
  55. int
  56. attrlist_find_or_create(Slapi_Attr **alist, const char *type, Slapi_Attr ***a)
  57. {
  58. return attrlist_find_or_create_locking_optional(alist, type, a, PR_TRUE, PR_TRUE);
  59. }
  60. int
  61. attrlist_find_or_create_locking_optional(Slapi_Attr **alist, const char *type, Slapi_Attr ***a, PRBool use_lock, PRBool ref_count)
  62. {
  63. int rc= 0; /* found */
  64. if ( *a==NULL )
  65. {
  66. for ( *a = alist; **a != NULL; *a = &(**a)->a_next ) {
  67. if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
  68. break;
  69. }
  70. }
  71. }
  72. if( **a==NULL )
  73. {
  74. **a = slapi_attr_new();
  75. slapi_attr_init_locking_optional(**a, type, use_lock, ref_count);
  76. rc= 1; /* created */
  77. }
  78. return rc;
  79. }
  80. /*
  81. * attrlist_merge - merge the given type and value with the list of
  82. * attributes in attrs.
  83. */
  84. void
  85. attrlist_merge(Slapi_Attr **alist, const char *type, struct berval **vals)
  86. {
  87. Slapi_Value **values= NULL;
  88. valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
  89. attrlist_merge_valuearray(alist,type,values);
  90. valuearray_free(&values);
  91. }
  92. /*
  93. * attrlist_merge_valuearray - merge the given type and value with the list of
  94. * attributes in attrs.
  95. */
  96. void
  97. attrlist_merge_valuearray(Slapi_Attr **alist, const char *type, Slapi_Value **vals)
  98. {
  99. Slapi_Attr **a= NULL;
  100. attrlist_find_or_create(alist, type, &a);
  101. valueset_add_valuearray( &(*a)->a_present_values, vals );
  102. }
  103. /*
  104. * attrlist_find - find and return attribute type in list a
  105. */
  106. Slapi_Attr *
  107. attrlist_find(Slapi_Attr *a, const char *type)
  108. {
  109. for ( ; a != NULL; a = a->a_next ) {
  110. if ( strcasecmp( a->a_type, type ) == 0 ) {
  111. return( a );
  112. }
  113. }
  114. return( NULL );
  115. }
  116. /*
  117. * attrlist_count_subtypes
  118. *
  119. * Returns a count attributes which conform to type
  120. * in the attr list a. This count includes all subtypes of
  121. * type
  122. */
  123. int
  124. attrlist_count_subtypes(Slapi_Attr *a, const char *type)
  125. {
  126. int counter = 0;
  127. for ( ; a != NULL; a = a->a_next ) {
  128. if ( slapi_attr_type_cmp( type , a->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
  129. counter++;
  130. }
  131. }
  132. return( counter );
  133. }
  134. /*
  135. * attrlist_find_ex
  136. *
  137. * Finds the first subtype in the list which matches "type"
  138. * starting at the beginning or hint depending on whether
  139. * hint has a value
  140. *
  141. * It is intended that hint be zero when first called and then
  142. * passed back in on subsequent calls until 0 is returned to mark
  143. * the end of the filtered list
  144. */
  145. Slapi_Attr *
  146. attrlist_find_ex(
  147. Slapi_Attr *a,
  148. const char *type,
  149. int *type_name_disposition, /* pass null if you're not interested */
  150. char** actual_type_name, /* pass null if you're not interested */
  151. void **hint
  152. )
  153. {
  154. Slapi_Attr **attr_cursor = (Slapi_Attr **)hint;
  155. if (type_name_disposition) *type_name_disposition = 0;
  156. if (actual_type_name) *actual_type_name = NULL;
  157. if(*attr_cursor == NULL)
  158. *attr_cursor = a; /* start at the beginning of the list */
  159. else
  160. *attr_cursor = (*attr_cursor)->a_next;
  161. while(*attr_cursor != NULL) {
  162. /* Determine whether the two types are related:*/
  163. if ( slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
  164. /* We got a match. Now figure out if we matched because it was a subtype */
  165. if (type_name_disposition) {
  166. if ( 0 == slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_EXACT) ) {
  167. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  168. } else {
  169. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE;
  170. }
  171. }
  172. if (actual_type_name) {
  173. *actual_type_name = (*attr_cursor)->a_type;
  174. }
  175. a = *attr_cursor; /* the attribute to return */
  176. return( a );
  177. }
  178. *attr_cursor = (*attr_cursor)->a_next; /* no match, move cursor */
  179. }
  180. return( NULL );
  181. }
  182. /*
  183. * attr_remove - remove the attribute from the list of attributes
  184. */
  185. Slapi_Attr *
  186. attrlist_remove(Slapi_Attr **attrs, const char *type)
  187. {
  188. Slapi_Attr **a;
  189. Slapi_Attr *save= NULL;
  190. for ( a = attrs; *a != NULL; a = &(*a)->a_next )
  191. {
  192. if ( strcasecmp( (*a)->a_type, type ) == 0 )
  193. {
  194. break;
  195. }
  196. }
  197. if (*a != NULL)
  198. {
  199. save = *a;
  200. *a = (*a)->a_next;
  201. }
  202. return save;
  203. }
  204. void
  205. attrlist_add(Slapi_Attr **attrs, Slapi_Attr *a)
  206. {
  207. a->a_next= *attrs;
  208. *attrs= a;
  209. }
  210. /*
  211. * attrlist_delete - delete the attribute type in list pointed to by attrs
  212. * return 0 deleted ok
  213. * 1 not found in list a
  214. * -1 something bad happened
  215. */
  216. int
  217. attrlist_delete(Slapi_Attr **attrs, const char *type)
  218. {
  219. Slapi_Attr **a;
  220. Slapi_Attr *save;
  221. for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
  222. if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
  223. break;
  224. }
  225. }
  226. if ( *a == NULL ) {
  227. return( 1 );
  228. }
  229. save = *a;
  230. *a = (*a)->a_next;
  231. slapi_attr_free( &save );
  232. return( 0 );
  233. }
  234. /*
  235. * attrlist_replace - replace the attribute value(s) with this value(s)
  236. */
  237. void attrlist_replace(Slapi_Attr **alist, const char *type, struct berval **vals)
  238. {
  239. Slapi_Attr **a = NULL;
  240. Slapi_Value **values = NULL;
  241. if (vals == NULL || vals[0] == NULL) {
  242. (void)attrlist_delete(alist, type);
  243. } else {
  244. attrlist_find_or_create(alist, type, &a);
  245. valuearray_init_bervalarray(vals, &values);
  246. attr_replace(*a, values);
  247. }
  248. }