csnset.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "slap.h"
  42. #include "slapi-private.h"
  43. static const CSNSet *csnset_get_csnset_node_from_csn(const CSNSet *csnset, const CSN *csn);
  44. static const CSNSet *csnset_get_csnset_node_from_type(const CSNSet *csnset, CSNType t);
  45. static CSNSet *csnset_get_previous_csnset_node(CSNSet *csnset, const CSN *csn);
  46. /*
  47. * The CSN is always added to the end of the list.
  48. */
  49. void
  50. csnset_add_csn(CSNSet **csnset, CSNType t, const CSN *csn)
  51. {
  52. if(csn!=NULL)
  53. {
  54. CSNSet *newcsn= (CSNSet*)slapi_ch_malloc(sizeof(CSNSet));
  55. newcsn->type= t;
  56. csn_init_by_csn(&newcsn->csn,csn);
  57. newcsn->next= NULL;
  58. {
  59. CSNSet **p= csnset;
  60. CSNSet *n= *csnset;
  61. while(n!=NULL)
  62. {
  63. p= &(n->next);
  64. n= n->next;
  65. }
  66. *p= newcsn;
  67. }
  68. }
  69. }
  70. /*
  71. * The CSN is inserted into the list at the appropriate point..
  72. */
  73. void
  74. csnset_insert_csn(CSNSet **csnset, CSNType t, const CSN *csn)
  75. {
  76. if((csn!=NULL) && (*csnset==NULL))
  77. {
  78. csnset_add_csn(csnset, t, csn);
  79. }
  80. else if(csn!=NULL)
  81. {
  82. CSNSet *newcsn= (CSNSet*)slapi_ch_malloc(sizeof(CSNSet));
  83. CSNSet *f= csnset_get_previous_csnset_node(*csnset, csn);
  84. newcsn->type= t;
  85. csn_init_by_csn(&newcsn->csn,csn);
  86. if(f==NULL)
  87. {
  88. /* adding to the list head */
  89. newcsn->next= *csnset;
  90. *csnset= newcsn;
  91. }
  92. else
  93. {
  94. newcsn->next= f->next;
  95. f->next= newcsn;
  96. }
  97. }
  98. }
  99. /*
  100. * Find the CSN of the given type and update it.
  101. */
  102. void
  103. csnset_update_csn(CSNSet **csnset, CSNType t, const CSN *csn)
  104. {
  105. const CSNSet *f= csnset_get_csnset_node_from_type(*csnset, t);
  106. if(f==NULL)
  107. {
  108. csnset_add_csn(csnset,t,csn);
  109. }
  110. else
  111. {
  112. if (csn_compare(csn, (CSN*)(&f->csn)) > 0)
  113. {
  114. csn_init_by_csn((CSN*)(&f->csn),csn);
  115. }
  116. }
  117. }
  118. /*
  119. * Check if the set CSN of CSNs contains a given CSN.
  120. */
  121. int
  122. csnset_contains(const CSNSet *csnset, const CSN *csn)
  123. {
  124. const CSNSet *f= csnset_get_csnset_node_from_csn(csnset, csn);
  125. return(f!=NULL);
  126. }
  127. /*
  128. * Remove the first CSN of the given type.
  129. */
  130. void
  131. csnset_remove_csn(CSNSet **csnset, CSNType t)
  132. {
  133. CSNSet **p= csnset;
  134. CSNSet *n= *csnset;
  135. while(n!=NULL)
  136. {
  137. if(n->type==t)
  138. {
  139. *p= n->next;
  140. slapi_ch_free((void**)&n);
  141. }
  142. else
  143. {
  144. p= &n->next;
  145. n= n->next;
  146. }
  147. }
  148. }
  149. void
  150. csnset_free(CSNSet **csnset)
  151. {
  152. csnset_purge(csnset, NULL);
  153. }
  154. /*
  155. * Get the first CSN of the given type.
  156. */
  157. const CSN *
  158. csnset_get_csn_of_type(const CSNSet *csnset, CSNType t)
  159. {
  160. const CSN *csn= NULL;
  161. const CSNSet *f= csnset_get_csnset_node_from_type(csnset, t);
  162. if(f!=NULL)
  163. {
  164. csn= &f->csn;
  165. }
  166. return csn;
  167. }
  168. const CSN *
  169. csnset_get_previous_csn(const CSNSet *csnset, const CSN *csn)
  170. {
  171. const CSN *prevcsn= NULL;
  172. CSNSet *f= csnset_get_previous_csnset_node((CSNSet*)csnset, csn);
  173. if(f!=NULL)
  174. {
  175. prevcsn= &f->csn;
  176. }
  177. return prevcsn;
  178. }
  179. const CSN *
  180. csnset_get_last_csn(const CSNSet *csnset)
  181. {
  182. const CSN *csn= NULL;
  183. const CSNSet *n= csnset;
  184. while(n!=NULL)
  185. {
  186. if(n->next==NULL)
  187. {
  188. csn= &n->csn;
  189. }
  190. n= n->next;
  191. }
  192. return csn;
  193. }
  194. void*
  195. csnset_get_first_csn (const CSNSet *csnset, CSN **csn, CSNType *t)
  196. {
  197. if (csnset)
  198. {
  199. *csn = (CSN*)&csnset->csn;
  200. *t = csnset->type;
  201. return (void*)csnset;
  202. }
  203. else
  204. return NULL;
  205. }
  206. void*
  207. csnset_get_next_csn (const CSNSet *csnset, void *cookie, CSN **csn, CSNType *t)
  208. {
  209. CSNSet *node;
  210. if (csnset && cookie)
  211. {
  212. node = ((CSNSet*)cookie)->next;
  213. if (node)
  214. {
  215. *csn = (CSN*)&node->csn;
  216. *t = node->type;
  217. return node;
  218. }
  219. else
  220. return NULL;
  221. }
  222. else
  223. return NULL;
  224. }
  225. static CSNSet *
  226. csnset_get_previous_csnset_node(CSNSet *csnset, const CSN *csn)
  227. {
  228. CSNSet *f= NULL;
  229. CSNSet *p= NULL;
  230. CSNSet *n= csnset;
  231. while(n!=NULL)
  232. {
  233. if(csn_compare(&n->csn, csn)>0)
  234. {
  235. f= p;
  236. n= NULL;
  237. }
  238. else
  239. {
  240. p= n;
  241. n= n->next;
  242. if(n==NULL)
  243. {
  244. /* Got to the end of the list... */
  245. f= p;
  246. }
  247. }
  248. }
  249. return f;
  250. }
  251. static const CSNSet *
  252. csnset_get_csnset_node_from_csn(const CSNSet *csnset, const CSN *csn)
  253. {
  254. const CSNSet *f= NULL;
  255. const CSNSet *n= csnset;
  256. while(n!=NULL)
  257. {
  258. if(csn_compare(&n->csn, csn)==0)
  259. {
  260. f= n;
  261. n= NULL;
  262. }
  263. else
  264. {
  265. n= n->next;
  266. }
  267. }
  268. return f;
  269. }
  270. static const CSNSet *
  271. csnset_get_csnset_node_from_type(const CSNSet *csnset, CSNType t)
  272. {
  273. const CSNSet *f= NULL;
  274. const CSNSet *n= csnset;
  275. while(n!=NULL)
  276. {
  277. if(n->type==t)
  278. {
  279. f= n;
  280. n= NULL;
  281. }
  282. else
  283. {
  284. n= n->next;
  285. }
  286. }
  287. return f;
  288. }
  289. /*
  290. * Remove any CSNs older than csnUpTo. If csnUpTo is NULL,
  291. * remove all CSNs.
  292. */
  293. void
  294. csnset_purge(CSNSet **csnset, const CSN *csnUpTo)
  295. {
  296. if (csnset != NULL)
  297. {
  298. CSNSet *n = *csnset, *nprev = NULL, *nnext;
  299. while (n != NULL)
  300. {
  301. if (NULL == csnUpTo || (csn_compare(&n->csn, csnUpTo) < 0))
  302. {
  303. nnext = n->next;
  304. if (*csnset == n)
  305. {
  306. /* Deletion of head */
  307. *csnset = nnext;
  308. }
  309. else if (nprev)
  310. {
  311. /* nprev was not purged, but n will be */
  312. nprev->next = nnext;
  313. }
  314. slapi_ch_free((void**)&n);
  315. n = nnext;
  316. }
  317. else
  318. {
  319. nprev = n;
  320. n = n->next;
  321. }
  322. }
  323. }
  324. }
  325. size_t
  326. csnset_string_size(CSNSet *csnset)
  327. {
  328. size_t s= 0;
  329. CSNSet *n= csnset;
  330. while(n!=NULL)
  331. {
  332. /* sizeof(;vucsn-011111111222233334444) */
  333. s+= 1 + LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
  334. n= n->next;
  335. }
  336. return s;
  337. }
  338. size_t
  339. csnset_size(CSNSet *csnset)
  340. {
  341. size_t s= 0;
  342. CSNSet *n= csnset;
  343. while(n!=NULL)
  344. {
  345. s+= sizeof(CSNSet);
  346. n= n->next;
  347. }
  348. return s;
  349. }
  350. CSNSet *
  351. csnset_dup(const CSNSet *csnset)
  352. {
  353. CSNSet *newcsnset= NULL;
  354. const CSNSet *n= csnset;
  355. while(n!=NULL)
  356. {
  357. csnset_add_csn(&newcsnset,n->type,&n->csn);
  358. n= n->next;
  359. }
  360. return newcsnset;
  361. }
  362. void
  363. csnset_as_string(const CSNSet *csnset,char *s)
  364. {
  365. const CSNSet *n= csnset;
  366. while(n!=NULL)
  367. {
  368. csn_as_attr_option_string(n->type,&n->csn,s);
  369. /* sizeof(;vucsn-011111111222233334444) */
  370. s+= 1 + LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
  371. n= n->next;
  372. }
  373. }