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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <variable.h>
#include <map.h>
#include <rlist.h>
#include <writer.h>
#include <conversion.h> /* DataTypeToString */
#define VARIABLE_TAG_SECRET "secret"
struct Variable_
{
VarRef *ref;
Rval rval;
DataType type;
StringSet *tags;
char *comment;
const Promise *promise; // The promise that set the present value
};
static Variable *VariableNew(VarRef *ref, Rval rval, DataType type,
StringSet *tags, char *comment,
const Promise *promise)
{
Variable *var = xmalloc(sizeof(Variable));
var->ref = ref;
var->rval = rval;
var->type = type;
var->tags = tags;
var->comment = comment;
var->promise = promise;
return var;
}
/* DO NOT EXPORT, this is for internal (hash table) use only, and it doesn't
* free everything in Variable, in particular it leaves var->ref to be handled
* by the Map implementation calling the key-destroy function. */
static void VariableDestroy(Variable *var)
{
if (var != NULL)
{
RvalDestroy(var->rval);
StringSetDestroy(var->tags);
free(var->comment);
// Nothing to do for ->promise
free(var);
}
}
static void VariableDestroy_untyped(void *var)
{
VariableDestroy(var);
}
const VarRef *VariableGetRef(const Variable *var)
{
assert(var != NULL);
return var->ref;
}
DataType VariableGetType(const Variable *var)
{
assert(var != NULL);
return var->type;
}
RvalType VariableGetRvalType(const Variable *var)
{
assert(var != NULL);
return var->rval.type;
}
StringSet *VariableGetTags(const Variable *var)
{
assert(var != NULL);
return var->tags;
}
const char *VariableGetComment(const Variable *var)
{
assert(var != NULL);
return var->comment;
}
const Promise *VariableGetPromise(const Variable *var)
{
assert(var != NULL);
return var->promise;
}
bool VariableIsSecret(const Variable *var)
{
assert(var != NULL);
return ((var->tags != NULL) && StringSetContains(var->tags, VARIABLE_TAG_SECRET));
}
Rval VariableGetRval(const Variable *var, bool get_secret)
{
assert(var != NULL);
if (!get_secret && VariableIsSecret(var))
{
return RvalNewSecret();
}
return var->rval;
}
void VariableSetRval(Variable *var, Rval new_rval)
{
assert(var != NULL);
RvalDestroy(var->rval);
var->rval = new_rval;
}
/**
Define "VarMap" hash table.
Key: VarRef
Value: Variable
*/
TYPED_MAP_DECLARE(Var, VarRef *, Variable *)
TYPED_MAP_DEFINE(Var, VarRef *, Variable *,
VarRefHash_untyped,
VarRefEqual_untyped,
VarRefDestroy_untyped,
VariableDestroy_untyped)
struct VariableTable_
{
VarMap *vars;
};
struct VariableTableIterator_
{
VarRef *ref;
MapIterator iter;
};
VariableTable *VariableTableNew(void)
{
VariableTable *table = xmalloc(sizeof(VariableTable));
table->vars = VarMapNew();
return table;
}
void VariableTableDestroy(VariableTable *table)
{
if (table)
{
VarMapDestroy(table->vars);
free(table);
}
}
/* NULL return value means variable not found. */
Variable *VariableTableGet(const VariableTable *table, const VarRef *ref)
{
Variable *v = VarMapGet(table->vars, ref);
char *ref_s = VarRefToString(ref, true); /* TODO optimise */
if (v != NULL)
{
CF_ASSERT(v->rval.item != NULL || DataTypeIsIterable(v->type),
"VariableTableGet(%s): "
"Only iterables (Rlists) are allowed to be NULL",
ref_s);
}
if (LogModuleEnabled(LOG_MOD_VARTABLE))
{
Buffer *buf = BufferNew();
BufferPrintf(buf, "VariableTableGet(%s): %s", ref_s,
v ? DataTypeToString(v->type) : "NOT FOUND");
if (v != NULL)
{
char *value_s;
BufferAppendString(buf, " => ");
if (DataTypeIsIterable(v->type) &&
v->rval.item == NULL)
{
value_s = xstrdup("EMPTY");
}
else
{
value_s = RvalToString(v->rval);
}
BufferAppendString(buf, value_s);
free(value_s);
}
LogDebug(LOG_MOD_VARTABLE, "%s", BufferGet(buf));
BufferDestroy(buf);
}
free(ref_s);
return v;
}
bool VariableTableRemove(VariableTable *table, const VarRef *ref)
{
return VarMapRemove(table->vars, ref);
}
bool VariableTablePut(VariableTable *table, const VarRef *ref,
const Rval *rval, DataType type,
StringSet *tags, char *comment,
const Promise *promise)
{
assert(VarRefIsQualified(ref));
/* TODO assert there are no CF_NS or '.' in the variable name? */
if (LogModuleEnabled(LOG_MOD_VARTABLE))
{
char *value_s = RvalToString(*rval);
LogDebug(LOG_MOD_VARTABLE, "VariableTablePut(%s): %s => %s",
ref->lval, DataTypeToString(type),
rval->item ? value_s : "EMPTY");
free(value_s);
}
CF_ASSERT(rval != NULL || DataTypeIsIterable(type),
"VariableTablePut(): "
"Only iterables (Rlists) are allowed to be NULL");
Variable *var = VariableNew(VarRefCopy(ref), RvalCopy(*rval), type,
tags, comment, promise);
return VarMapInsert(table->vars, var->ref, var);
}
bool VariableTableClear(VariableTable *table, const char *ns, const char *scope, const char *lval)
{
const size_t vars_num = VarMapSize(table->vars);
if (!ns && !scope && !lval)
{
VarMapClear(table->vars);
bool has_vars = (vars_num > 0);
return has_vars;
}
/* We can't remove elements from the hash table while we are iterating
* over it. So we first store the VarRef pointers on a list. */
VarRef **to_remove = xmalloc(vars_num * sizeof(*to_remove));
size_t remove_count = 0;
{
VariableTableIterator *iter = VariableTableIteratorNew(table, ns, scope, lval);
for (Variable *v = VariableTableIteratorNext(iter);
v != NULL;
v = VariableTableIteratorNext(iter))
{
to_remove[remove_count] = v->ref;
remove_count++;
}
VariableTableIteratorDestroy(iter);
}
if (remove_count == 0)
{
free(to_remove);
return false;
}
size_t removed = 0;
for(size_t i = 0; i < remove_count; i++)
{
if (VariableTableRemove(table, to_remove[i]))
{
removed++;
}
}
free(to_remove);
assert(removed == remove_count);
return true;
}
size_t VariableTableCount(const VariableTable *table, const char *ns, const char *scope, const char *lval)
{
if (!ns && !scope && !lval)
{
return VarMapSize(table->vars);
}
VariableTableIterator *iter = VariableTableIteratorNew(table, ns, scope, lval);
size_t count = 0;
while (VariableTableIteratorNext(iter))
{
count++;
}
VariableTableIteratorDestroy(iter);
return count;
}
VariableTableIterator *VariableTableIteratorNewFromVarRef(const VariableTable *table, const VarRef *ref)
{
VariableTableIterator *iter = xmalloc(sizeof(VariableTableIterator));
iter->ref = VarRefCopy(ref);
iter->iter = MapIteratorInit(table->vars->impl);
return iter;
}
VariableTableIterator *VariableTableIteratorNew(const VariableTable *table, const char *ns, const char *scope, const char *lval)
{
VarRef ref = { 0 };
ref.ns = (char *)ns;
ref.scope = (char *)scope;
ref.lval = (char *)lval;
return VariableTableIteratorNewFromVarRef(table, &ref);
}
Variable *VariableTableIteratorNext(VariableTableIterator *iter)
{
MapKeyValue *keyvalue;
while ((keyvalue = MapIteratorNext(&iter->iter)) != NULL)
{
Variable *var = keyvalue->value;
const char *key_ns = var->ref->ns ? var->ref->ns : "default";
if (iter->ref->ns && strcmp(key_ns, iter->ref->ns) != 0)
{
continue;
}
if (iter->ref->scope && strcmp(var->ref->scope, iter->ref->scope) != 0)
{
continue;
}
if (iter->ref->lval && strcmp(var->ref->lval, iter->ref->lval) != 0)
{
continue;
}
if (iter->ref->num_indices > 0)
{
if (iter->ref->num_indices > var->ref->num_indices)
{
continue;
}
bool match = true;
for (size_t i = 0; i < iter->ref->num_indices; i++)
{
if (strcmp(var->ref->indices[i], iter->ref->indices[i]) != 0)
{
match = false;
break;
}
}
if (!match)
{
continue;
}
}
return var;
}
return NULL;
}
void VariableTableIteratorDestroy(VariableTableIterator *iter)
{
if (iter)
{
VarRefDestroy(iter->ref);
free(iter);
}
}
|