-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.c
300 lines (257 loc) · 8.46 KB
/
conf.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
/* ################################################################### */
/* Copyright 2022, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "cachestat.h"
#include "utils.h"
#include "log.h"
#include "list.h"
#include "bst.h"
#include "types.h"
#include "cache.h"
#include "xmalloc.h"
#include "strarray.h"
#include "conf.h"
#include "comp.h"
extern bst_node_t *var_tree;
extern bst_node_t *rule_tree;
extern long path_max;
/* ---------------------------------------------- */
/* Parameters in Parameter's value list accessors */
/* ---------------------------------------------- */
/* ================================================================ */
/* For a given rule and a param name, return a pointer on the first */
/* corresponding parameter in rule or NULL if not found. */
/* */
/* Returns: - NULL if the parameter 'name' does not exist */
/* - a pointer to the named parameter just found */
/* ================================================================ */
param_t *
get_param(rule_t *rule, char *name)
{
ll_t *param_list = rule->param_list;
ll_node_t *node = param_list->head;
param_t *par;
while (node)
{
par = (param_t *)node->data;
if (strcmp(par->name, name) == 0)
return par;
node = node->next;
}
return NULL;
}
/* ================================================================= */
/* For a given rule and a param name, return the list of its value */
/* If found, the parameter param will point to the parameter found */
/* */
/* Returns: - NULL if the parameter 'name' does not exist */
/* - a pointer to the list of values of the named parameter */
/* ================================================================= */
ll_t *
get_param_val_list(rule_t *rule, char *name)
{
param_t *par;
par = get_param(rule, name);
if (par == NULL)
return NULL;
return par->val_list;
}
/* ================================================================ */
/* Create a sorted list of all the data files to use. */
/* Only the designated directories in the cfg file are considered. */
/* ================================================================ */
int
create_data_file_list(const char *path, ll_t *file_list)
{
char *newpath;
int len = -1;
int rc = 1;
DIR *dir;
struct dirent *entry;
struct stat st;
newpath = xmalloc(path_max);
/* Try to open the given dir. */
/* """""""""""""""""""""""""" */
dir = opendir(path);
if (dir == NULL)
{
rc = 0;
goto out;
}
/* Store the directory filenames in the list. */
/* """""""""""""""""""""""""""""""""""""""""" */
while ((entry = readdir(dir)) != NULL)
{
if (strcmp(entry->d_name, ".") == 0)
continue;
if (strcmp(entry->d_name, "..") == 0)
continue;
/* The name must contain at least 5 characters as it must */
/* be terminated by ".dat". */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""" */
len = strlen(entry->d_name);
if (len < 5)
continue;
if (strcmp(entry->d_name + len - 4, ".dat") != 0)
continue;
/* forge the full path and add it to the list */
/* """""""""""""""""""""""""""""""""""""""""" */
snprintf(newpath, path_max, "%s/%s", path, entry->d_name);
if (stat(newpath, &st) == 0)
{
if (!S_ISREG(st.st_mode))
{
trace(LOG_WARN, "%s ignored, not a regular file.", newpath);
continue;
}
if ((st.st_mode & 0777) != 0600)
{
trace(LOG_WARN,
"%s ignored, must have read/write rights for root only.",
newpath);
continue;
}
if (st.st_uid != 0 || st.st_gid != 0)
{
trace(LOG_WARN, "%s ignored, must be owned by root:root.", newpath);
continue;
}
if (strcmp(entry->d_name + len - 4, ".dat") == 0)
{
ll_append(file_list, xstrdup(newpath));
}
}
}
/* Sort the resulting list if not empty as readdir doesn't do that. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (len < 0)
rc = 0;
else
ll_sort(file_list, str_comp, swap);
/* Done */
closedir(dir);
out:
free(newpath);
return rc;
}
/* ------------------- */
/* Variable management */
/* ------------------- */
/* ================================================================== */
/* Callback function used by bst_foreach. */
/* Copies global variables present in the variables bst tree into the */
/* new tree whose reference is passed in arg. */
/* ================================================================== */
void
copy_global_var(void *node, void *arg)
{
var_t *var, *new_var;
bst_node_t **tree_ptr;
var = node; /* node is a var_t instance. */
tree_ptr = (bst_node_t **)arg; /* arg points to a bst tree pointer. */
if (var->global)
{
new_var = xmalloc(sizeof(var_t));
memcpy(new_var, var, sizeof(var_t));
/* We do not case if new_var was inserted or replaced here. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""" */
*tree_ptr = bst_insert(*tree_ptr, new_var, item_comp, NULL);
}
}
/* ========================================= */
/* Function to expand variables in a string. */
/* variables reference are given as @{...} */
/* ========================================= */
void
expand_var_ref(char **str)
{
var_t var_node;
char *var;
char *expanded_var;
char *var_ref_name;
char *new_val;
unsigned finished = 0;
size_t offset = 0;
//char * old_str = xstrdup(*str);
/* While expansion is not finished */
/* """"""""""""""""""""""""""""""" */
do
{
/* While there remains a non expanded variable in the variable */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
while ((offset = get_next_var_reference(*str, offset, &var)) != 0)
{
var_t *found_var;
/* Locate the variable reference */
/* """"""""""""""""""""""""""""" */
var_node.name = var;
if ((found_var = bst_search(var_tree, &var_node, var_entry_comp)) != NULL)
expanded_var = xstrdup(found_var->value);
else
expanded_var = xstrdup("");
/* Update the variable content */
/* """"""""""""""""""""""""""" */
var_ref_name = xcalloc(strlen(var) + 4, 1);
strcat(var_ref_name, "@{");
strcat(var_ref_name, var);
strcat(var_ref_name, "}");
new_val = strrep(*str, var_ref_name, expanded_var);
free(var_ref_name);
free(expanded_var);
free(*str);
*str = new_val;
offset = 0;
}
if (get_next_var_reference(*str, 0, &var) != 0)
offset = 0;
else
finished = 1;
} while (!finished);
}
/* ====================================================== */
/* Extracts the next variable name from a string starting */
/* at offset start if any. */
/* Returns the first position after the discovered */
/* variable on success or 0 */
/* ====================================================== */
int
get_next_var_reference(char *str, int start, char **var)
{
char *pstart, *pend;
if ((pstart = strstr(str + start, "@{")) != NULL)
{
/* Skip \ protected variables reference */
/* """""""""""""""""""""""""""""""""""" */
while (pstart > str + start && *(pstart - 1) == '\\')
pstart = strstr(pstart + 1, "@{");
if (pstart != NULL)
{
pstart += 2;
pend = pstart;
while (*pend != '\0' && *pend != '}' && *str != '\0')
pend++;
if (*pend == '}')
{
if (pend > pstart)
{
*var = xcalloc(pend - pstart + 1, 1);
memcpy(*var, pstart, pend - pstart);
return pend - str + 2;
}
}
}
}
return 0;
}