-
Notifications
You must be signed in to change notification settings - Fork 187
/
class.c
349 lines (286 loc) · 8.08 KB
/
class.c
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
/*
Copyright 2021 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 <class.h>
#include <map.h>
#include <alloc.h>
#include <string_lib.h> /* String*() */
#include <regex.h> /* CompileRegex,StringMatchFullWithPrecompiledRegex */
#include <files_names.h>
static void ClassDestroy(Class *cls); /* forward declaration */
static void ClassDestroy_untyped(void *p)
{
ClassDestroy(p);
}
/**
Define ClassMap.
Key: a string which is always the fully qualified class name,
for example "default:127_0_0_1"
*/
TYPED_MAP_DECLARE(Class, char *, Class *)
TYPED_MAP_DEFINE(Class, char *, Class *,
StringHash_untyped,
StringEqual_untyped,
free,
ClassDestroy_untyped)
struct ClassTable_
{
ClassMap *classes;
};
struct ClassTableIterator_
{
MapIterator iter;
char *ns;
bool is_hard;
bool is_soft;
};
/**
* @param #tags is a comma separated string of words.
* Both "" or NULL are equivalent.
*/
static void ClassInit(Class *cls,
const char *ns, const char *name,
bool is_soft, ContextScope scope, const char *tags)
{
if (ns == NULL || strcmp(ns, "default") == 0)
{
cls->ns = NULL;
}
else
{
cls->ns = xstrdup(ns);
}
cls->name = xstrdup(name);
CanonifyNameInPlace(cls->name);
cls->is_soft = is_soft;
cls->scope = scope;
cls->tags = StringSetFromString(tags, ',');
if (!is_soft && !StringSetContains(cls->tags, "hardclass"))
{
StringSetAdd(cls->tags, xstrdup("hardclass"));
}
}
static void ClassDestroySoft(Class *cls)
{
if (cls)
{
free(cls->ns);
free(cls->name);
StringSetDestroy(cls->tags);
}
}
static void ClassDestroy(Class *cls)
{
if (cls)
{
ClassDestroySoft(cls);
free(cls);
}
}
ClassTable *ClassTableNew(void)
{
ClassTable *table = xmalloc(sizeof(*table));
table->classes = ClassMapNew();
return table;
}
void ClassTableDestroy(ClassTable *table)
{
if (table)
{
ClassMapDestroy(table->classes);
free(table);
}
}
bool ClassTablePut(ClassTable *table,
const char *ns, const char *name,
bool is_soft, ContextScope scope, const char *tags)
{
assert(name);
assert(is_soft || (!ns || strcmp("default", ns) == 0)); // hard classes should have default namespace
assert(is_soft || scope == CONTEXT_SCOPE_NAMESPACE); // hard classes cannot be local
if (ns == NULL)
{
ns = "default";
}
Class *cls = xmalloc(sizeof(*cls));
ClassInit(cls, ns, name, is_soft, scope, tags);
/* (cls->name != name) because canonification has happened. */
char *fullname = StringConcatenate(3, ns, ":", cls->name);
Log(LOG_LEVEL_DEBUG, "Setting %sclass: %s",
is_soft ? "" : "hard ",
fullname);
return ClassMapInsert(table->classes, fullname, cls);
}
Class *ClassTableGet(const ClassTable *table, const char *ns, const char *name)
{
if (ns == NULL)
{
ns = "default";
}
char fullname[ strlen(ns) + 1 + strlen(name) + 1 ];
xsnprintf(fullname, sizeof(fullname), "%s:%s", ns, name);
return ClassMapGet(table->classes, fullname);
}
Class *ClassTableMatch(const ClassTable *table, const char *regex)
{
ClassTableIterator *it = ClassTableIteratorNew(table, NULL, true, true);
Class *cls = NULL;
pcre *pattern = CompileRegex(regex);
if (pattern == NULL)
{
// TODO: perhaps pcre has can give more info on this error?
Log(LOG_LEVEL_ERR, "Unable to pcre compile regex '%s'", regex);
return NULL;
}
while ((cls = ClassTableIteratorNext(it)))
{
bool matched;
if (cls->ns)
{
char *class_expr = ClassRefToString(cls->ns, cls->name);
matched = StringMatchFullWithPrecompiledRegex(pattern, class_expr);
free(class_expr);
}
else
{
matched = StringMatchFullWithPrecompiledRegex(pattern, cls->name);
}
if (matched)
{
break;
}
}
pcre_free(pattern);
ClassTableIteratorDestroy(it);
return cls;
}
bool ClassTableRemove(ClassTable *table, const char *ns, const char *name)
{
if (ns == NULL)
{
ns = "default";
}
char fullname[ strlen(ns) + 1 + strlen(name) + 1 ];
xsnprintf(fullname, sizeof(fullname), "%s:%s", ns, name);
return ClassMapRemove(table->classes, fullname);
}
bool ClassTableClear(ClassTable *table)
{
bool has_classes = (ClassMapSize(table->classes) > 0);
ClassMapClear(table->classes);
return has_classes;
}
ClassTableIterator *ClassTableIteratorNew(const ClassTable *table,
const char *ns,
bool is_hard, bool is_soft)
{
ClassTableIterator *iter = xmalloc(sizeof(*iter));
iter->ns = ns ? xstrdup(ns) : NULL;
iter->iter = MapIteratorInit(table->classes->impl);
iter->is_soft = is_soft;
iter->is_hard = is_hard;
return iter;
}
Class *ClassTableIteratorNext(ClassTableIterator *iter)
{
MapKeyValue *keyvalue;
while ((keyvalue = MapIteratorNext(&iter->iter)) != NULL)
{
Class *cls = keyvalue->value;
/* Make sure we never store "default" as namespace in the ClassTable,
* instead we have always ns==NULL in that case. */
CF_ASSERT_FIX(cls->ns == NULL ||
strcmp(cls->ns, "default") != 0,
(cls->ns = NULL),
"Class table contained \"default\" namespace,"
" should never happen!");
const char *key_ns = cls->ns ? cls->ns : "default";
if (iter->ns && strcmp(key_ns, iter->ns) != 0)
{
continue;
}
if (iter->is_soft && !iter->is_soft)
{
continue;
}
if (iter->is_hard && !iter->is_hard)
{
continue;
}
return cls;
}
return NULL;
}
void ClassTableIteratorDestroy(ClassTableIterator *iter)
{
if (iter)
{
free(iter->ns);
free(iter);
}
}
ClassRef ClassRefParse(const char *expr)
{
char *name_start = strchr(expr, ':');
if (!name_start)
{
return (ClassRef) { .ns = NULL, .name = xstrdup(expr) };
}
else
{
char *ns = NULL;
if ((name_start - expr) > 0)
{
ns = xstrndup(expr, name_start - expr);
}
else
{
// this would be invalid syntax
ns = xstrdup("");
}
char *name = xstrdup(name_start + 1);
return (ClassRef) { .ns = ns, .name = name };
}
}
char *ClassRefToString(const char *ns, const char *name)
{
assert(name != NULL);
if (ns == NULL ||
strcmp("default", ns) == 0)
{
return xstrdup(name);
}
else
{
return StringConcatenate(3, ns, ":", name);
}
}
bool ClassRefIsQualified(ClassRef ref)
{
return ref.ns != NULL;
}
void ClassRefQualify(ClassRef *ref, const char *ns)
{
free(ref->ns);
ref->ns = xstrdup(ns);
}
void ClassRefDestroy(ClassRef ref)
{
free(ref.ns);
free(ref.name);
}