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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "slap.h"
#include "slapi-private.h"
static const CSNSet *csnset_get_csnset_node_from_csn(const CSNSet *csnset, const CSN *csn);
static const CSNSet *csnset_get_csnset_node_from_type(const CSNSet *csnset, CSNType t);
static CSNSet *csnset_get_previous_csnset_node(CSNSet *csnset, const CSN *csn);
/*
* The CSN is always added to the end of the list.
*/
void
csnset_add_csn(CSNSet **csnset, CSNType t, const CSN *csn)
{
if (csn != NULL) {
CSNSet *newcsn = (CSNSet *)slapi_ch_malloc(sizeof(CSNSet));
newcsn->type = t;
csn_init_by_csn(&newcsn->csn, csn);
newcsn->next = NULL;
{
CSNSet **p = csnset;
CSNSet *n = *csnset;
while (n != NULL) {
p = &(n->next);
n = n->next;
}
*p = newcsn;
}
}
}
/*
* The CSN is inserted into the list at the appropriate point..
*/
void
csnset_insert_csn(CSNSet **csnset, CSNType t, const CSN *csn)
{
if ((csn != NULL) && (*csnset == NULL)) {
csnset_add_csn(csnset, t, csn);
} else if (csn != NULL) {
CSNSet *newcsn = (CSNSet *)slapi_ch_malloc(sizeof(CSNSet));
CSNSet *f = csnset_get_previous_csnset_node(*csnset, csn);
newcsn->type = t;
csn_init_by_csn(&newcsn->csn, csn);
if (f == NULL) {
/* adding to the list head */
newcsn->next = *csnset;
*csnset = newcsn;
} else {
newcsn->next = f->next;
f->next = newcsn;
}
}
}
/*
* Find the CSN of the given type and update it.
*/
void
csnset_update_csn(CSNSet **csnset, CSNType t, const CSN *csn)
{
const CSNSet *f = csnset_get_csnset_node_from_type(*csnset, t);
if (f == NULL) {
csnset_add_csn(csnset, t, csn);
} else {
if (csn_compare(csn, (CSN *)(&f->csn)) > 0) {
csn_init_by_csn((CSN *)(&f->csn), csn);
}
}
}
/*
* Check if the set CSN of CSNs contains a given CSN.
*/
int
csnset_contains(const CSNSet *csnset, const CSN *csn)
{
const CSNSet *f = csnset_get_csnset_node_from_csn(csnset, csn);
return (f != NULL);
}
/*
* Remove the first CSN of the given type.
*/
void
csnset_remove_csn(CSNSet **csnset, CSNType t)
{
CSNSet **p = csnset;
CSNSet *n = *csnset;
while (n != NULL) {
if (n->type == t) {
*p = n->next;
slapi_ch_free((void **)&n);
} else {
p = &n->next;
n = n->next;
}
}
}
void
csnset_free(CSNSet **csnset)
{
csnset_purge(csnset, NULL);
}
/*
* Get the first CSN of the given type.
*/
const CSN *
csnset_get_csn_of_type(const CSNSet *csnset, CSNType t)
{
const CSN *csn = NULL;
const CSNSet *f = csnset_get_csnset_node_from_type(csnset, t);
if (f != NULL) {
csn = &f->csn;
}
return csn;
}
const CSN *
csnset_get_previous_csn(const CSNSet *csnset, const CSN *csn)
{
const CSN *prevcsn = NULL;
CSNSet *f = csnset_get_previous_csnset_node((CSNSet *)csnset, csn);
if (f != NULL) {
prevcsn = &f->csn;
}
return prevcsn;
}
const CSN *
csnset_get_last_csn(const CSNSet *csnset)
{
const CSN *csn = NULL;
const CSNSet *n = csnset;
while (n != NULL) {
if (n->next == NULL) {
csn = &n->csn;
}
n = n->next;
}
return csn;
}
void *
csnset_get_first_csn(const CSNSet *csnset, CSN **csn, CSNType *t)
{
if (csnset) {
*csn = (CSN *)&csnset->csn;
*t = csnset->type;
return (void *)csnset;
} else
return NULL;
}
void *
csnset_get_next_csn(const CSNSet *csnset, void *cookie, CSN **csn, CSNType *t)
{
CSNSet *node;
if (csnset && cookie) {
node = ((CSNSet *)cookie)->next;
if (node) {
*csn = (CSN *)&node->csn;
*t = node->type;
return node;
} else
return NULL;
} else
return NULL;
}
static CSNSet *
csnset_get_previous_csnset_node(CSNSet *csnset, const CSN *csn)
{
CSNSet *f = NULL;
CSNSet *p = NULL;
CSNSet *n = csnset;
while (n != NULL) {
if (csn_compare(&n->csn, csn) > 0) {
f = p;
n = NULL;
} else {
p = n;
n = n->next;
if (n == NULL) {
/* Got to the end of the list... */
f = p;
}
}
}
return f;
}
static const CSNSet *
csnset_get_csnset_node_from_csn(const CSNSet *csnset, const CSN *csn)
{
const CSNSet *f = NULL;
const CSNSet *n = csnset;
while (n != NULL) {
if (csn_compare(&n->csn, csn) == 0) {
f = n;
n = NULL;
} else {
n = n->next;
}
}
return f;
}
static const CSNSet *
csnset_get_csnset_node_from_type(const CSNSet *csnset, CSNType t)
{
const CSNSet *f = NULL;
const CSNSet *n = csnset;
while (n != NULL) {
if (n->type == t) {
f = n;
n = NULL;
} else {
n = n->next;
}
}
return f;
}
/*
* Remove any CSNs older than csnUpTo. If csnUpTo is NULL,
* remove all CSNs.
*/
void
csnset_purge(CSNSet **csnset, const CSN *csnUpTo)
{
if (csnset != NULL) {
CSNSet *n = *csnset, *nprev = NULL, *nnext;
while (n != NULL) {
if (NULL == csnUpTo || (csn_compare(&n->csn, csnUpTo) < 0)) {
nnext = n->next;
if (*csnset == n) {
/* Deletion of head */
*csnset = nnext;
} else if (nprev) {
/* nprev was not purged, but n will be */
nprev->next = nnext;
}
slapi_ch_free((void **)&n);
n = nnext;
} else {
nprev = n;
n = n->next;
}
}
}
}
size_t
csnset_string_size(CSNSet *csnset)
{
size_t s = 0;
CSNSet *n = csnset;
while (n != NULL) {
/* sizeof(;vucsn-011111111222233334444) */
s += 1 + LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
n = n->next;
}
return s;
}
size_t
csnset_size(CSNSet *csnset)
{
size_t s = 0;
CSNSet *n = csnset;
while (n != NULL) {
s += sizeof(CSNSet);
n = n->next;
}
return s;
}
CSNSet *
csnset_dup(const CSNSet *csnset)
{
CSNSet *newcsnset = NULL;
CSNSet **curnode = &newcsnset;
const CSNSet *n = csnset;
while (n != NULL) {
csnset_add_csn(curnode, n->type, &n->csn);
n = n->next;
curnode = &((*curnode)->next);
}
return newcsnset;
}
void
csnset_as_string(const CSNSet *csnset, char *s)
{
const CSNSet *n = csnset;
while (n != NULL) {
/* coverity false positive: there is no alloc involved in this case because s is not NULL */
/* coverity[leaked_storage] */
csn_as_attr_option_string(n->type, &n->csn, s);
/* sizeof(;vucsn-011111111222233334444) */
s += 1 + LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
n = n->next;
}
}
|