| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
#include <errno.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <dirent.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stddef.h> |
| 31 |
#include <limits.h> |
| 32 |
#include <sys/types.h> |
| 33 |
#include <regex.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include "trace.h" |
| 36 |
#include "collection.h" |
| 37 |
#include "collection_tools.h" |
| 38 |
#include "ini_configobj.h" |
| 39 |
#include "ini_config_priv.h" |
| 40 |
#include "ini_defines.h" |
| 41 |
#include "path_utils.h" |
| 42 |
|
| 43 |
|
| 44 |
#define INI_CURRENT_DIR "." |
| 45 |
#define INI_PARENT_DIR ".." |
| 46 |
|
| 47 |
|
| 48 |
#define INI_AUG_ARR_SIZE_INC 50 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static void ini_aug_add_string(struct ref_array *ra, |
| 53 |
const char *format, |
| 54 |
...) |
| 55 |
{ |
| 56 |
va_list args; |
| 57 |
char *result = NULL; |
| 58 |
|
| 59 |
TRACE_FLOW_ENTRY(); |
| 60 |
|
| 61 |
va_start(args, format); |
| 62 |
|
| 63 |
if(vasprintf(&result, format, args )) { |
| 64 |
TRACE_INFO_STRING("String:", result); |
| 65 |
|
| 66 |
(void)ref_array_append(ra, (void *)&result); |
| 67 |
} |
| 68 |
|
| 69 |
va_end(args); |
| 70 |
|
| 71 |
TRACE_FLOW_EXIT(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static void add_dir_open_error(int error, char *dirname, |
| 76 |
struct ref_array *ra_err) |
| 77 |
{ |
| 78 |
|
| 79 |
TRACE_FLOW_ENTRY(); |
| 80 |
|
| 81 |
switch(error) { |
| 82 |
case EACCES: |
| 83 |
ini_aug_add_string(ra_err, |
| 84 |
"Permission denied opening %s.", |
| 85 |
dirname); |
| 86 |
break; |
| 87 |
case EMFILE: |
| 88 |
case ENFILE: |
| 89 |
ini_aug_add_string(ra_err, |
| 90 |
"Too many file descriptors in use while opening %s.", |
| 91 |
dirname); |
| 92 |
break; |
| 93 |
case ENOENT: |
| 94 |
ini_aug_add_string(ra_err, |
| 95 |
"Directory %s does not exist.", |
| 96 |
dirname); |
| 97 |
break; |
| 98 |
case ENOTDIR: |
| 99 |
ini_aug_add_string(ra_err, |
| 100 |
"Path %s is not a directory.", |
| 101 |
dirname); |
| 102 |
break; |
| 103 |
case ENOMEM: |
| 104 |
ini_aug_add_string(ra_err, |
| 105 |
"Insufficient memory while opening %s.", |
| 106 |
dirname); |
| 107 |
break; |
| 108 |
default: |
| 109 |
ini_aug_add_string(ra_err, |
| 110 |
"Unknown error while opening %s.", |
| 111 |
dirname); |
| 112 |
break; |
| 113 |
} |
| 114 |
|
| 115 |
TRACE_FLOW_EXIT(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
static void regex_cleanup(void *elem, |
| 120 |
ref_array_del_enum type, |
| 121 |
void *data) |
| 122 |
{ |
| 123 |
TRACE_FLOW_ENTRY(); |
| 124 |
regfree(*((regex_t **)elem)); |
| 125 |
free(*((regex_t **)elem)); |
| 126 |
TRACE_FLOW_EXIT(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static int ini_aug_regex_prepare(const char *patterns[], |
| 132 |
struct ref_array *ra_err, |
| 133 |
struct ref_array **ra_regex) |
| 134 |
{ |
| 135 |
int error = EOK; |
| 136 |
int reg_err = 0; |
| 137 |
char const *pat = NULL; |
| 138 |
struct ref_array *ra = NULL; |
| 139 |
regex_t *preg = NULL; |
| 140 |
size_t buf_size = 0; |
| 141 |
char *err_str = NULL; |
| 142 |
size_t i; |
| 143 |
|
| 144 |
TRACE_FLOW_ENTRY(); |
| 145 |
|
| 146 |
if (patterns) { |
| 147 |
|
| 148 |
|
| 149 |
error = ref_array_create(&ra, |
| 150 |
sizeof(regex_t *), |
| 151 |
INI_AUG_ARR_SIZE_INC, |
| 152 |
regex_cleanup, |
| 153 |
NULL); |
| 154 |
if (error) { |
| 155 |
TRACE_ERROR_NUMBER("Failed to create array.", error); |
| 156 |
return error; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
for (i = 0; patterns[i] != NULL; i++) { |
| 161 |
pat = patterns[i]; |
| 162 |
|
| 163 |
TRACE_INFO_STRING("Pattern:", pat); |
| 164 |
|
| 165 |
preg = calloc(1, sizeof(regex_t)); |
| 166 |
if (preg == NULL) { |
| 167 |
TRACE_ERROR_NUMBER("Failed to create array.", ENOMEM); |
| 168 |
ref_array_destroy(ra); |
| 169 |
return ENOMEM; |
| 170 |
} |
| 171 |
reg_err = regcomp(preg, pat, REG_NOSUB); |
| 172 |
if (reg_err) { |
| 173 |
|
| 174 |
buf_size = regerror(reg_err, preg, NULL, 0); |
| 175 |
err_str = malloc (buf_size); |
| 176 |
if (err_str == NULL) { |
| 177 |
TRACE_ERROR_NUMBER("Failed to create array.", ENOMEM); |
| 178 |
ref_array_destroy(ra); |
| 179 |
free(preg); |
| 180 |
return ENOMEM; |
| 181 |
} |
| 182 |
regerror(reg_err, preg, err_str, buf_size); |
| 183 |
free(preg); |
| 184 |
ini_aug_add_string(ra_err, |
| 185 |
"Failed to process expression: %s." |
| 186 |
" Compilation returned error: %s", |
| 187 |
pat, err_str); |
| 188 |
free(err_str); |
| 189 |
|
| 190 |
|
| 191 |
pat++; |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
error = ref_array_append(ra, (void *)&preg); |
| 196 |
if (error) { |
| 197 |
TRACE_ERROR_NUMBER("Failed to add element to array.", error); |
| 198 |
ref_array_destroy(ra); |
| 199 |
free(preg); |
| 200 |
return error; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
*ra_regex = ra; |
| 206 |
|
| 207 |
|
| 208 |
TRACE_FLOW_EXIT(); |
| 209 |
return EOK; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static bool ini_aug_match_name(char *name, |
| 214 |
struct ref_array *ra_regex) |
| 215 |
{ |
| 216 |
uint32_t len = 0; |
| 217 |
uint32_t i = 0; |
| 218 |
bool match = false; |
| 219 |
regex_t *preg = NULL; |
| 220 |
|
| 221 |
TRACE_FLOW_ENTRY(); |
| 222 |
|
| 223 |
len = ref_array_len(ra_regex); |
| 224 |
if (len == 0) { |
| 225 |
|
| 226 |
TRACE_FLOW_EXIT(); |
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
TRACE_INFO_STRING("Name to match:", name); |
| 231 |
TRACE_INFO_NUMBER("Number of regexes:", len); |
| 232 |
|
| 233 |
|
| 234 |
for (i = 0; i < len; i++) { |
| 235 |
preg = *((regex_t **)ref_array_get(ra_regex, i, NULL)); |
| 236 |
if (preg == NULL) continue; |
| 237 |
if (regexec(preg, name, 0, NULL, 0) == 0) { |
| 238 |
TRACE_INFO_NUMBER("Name matched regex number:", i); |
| 239 |
match = true; |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
TRACE_FLOW_EXIT(); |
| 245 |
return match; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
static bool ini_check_file_perm(char *name, |
| 250 |
struct access_check *check_perm, |
| 251 |
struct ref_array *ra_err) |
| 252 |
{ |
| 253 |
bool ret = false; |
| 254 |
int error = EOK; |
| 255 |
struct stat file_info; |
| 256 |
|
| 257 |
TRACE_FLOW_ENTRY(); |
| 258 |
|
| 259 |
errno = 0; |
| 260 |
if (stat(name, &file_info) == -1) { |
| 261 |
error = errno; |
| 262 |
TRACE_ERROR_NUMBER("Failed to get file stats", error); |
| 263 |
ini_aug_add_string(ra_err, |
| 264 |
"Failed to read metadata for file %s." |
| 265 |
" Skipping.", |
| 266 |
name); |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
if (!S_ISREG(file_info.st_mode)) { |
| 271 |
ini_aug_add_string(ra_err, |
| 272 |
"File %s is not a regular file. Skipping.", |
| 273 |
name); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
if ((check_perm) && (check_perm->flags)) { |
| 278 |
error = access_check_int(&file_info, |
| 279 |
check_perm->flags, |
| 280 |
check_perm->uid, |
| 281 |
check_perm->gid, |
| 282 |
check_perm->mode, |
| 283 |
check_perm->mask); |
| 284 |
if(error) { |
| 285 |
TRACE_ERROR_NUMBER("Access check returned", error); |
| 286 |
ini_aug_add_string(ra_err, |
| 287 |
"File %s did not pass access check. Skipping.", |
| 288 |
name); |
| 289 |
return false; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
ret = true; |
| 294 |
|
| 295 |
TRACE_FLOW_EXIT(); |
| 296 |
return ret; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
static void ini_aug_sort_list(struct ref_array *ra_list) |
| 301 |
{ |
| 302 |
unsigned len = 0, j = 0, k = 0; |
| 303 |
char **item1 = NULL; |
| 304 |
char **item2 = NULL; |
| 305 |
|
| 306 |
TRACE_FLOW_ENTRY(); |
| 307 |
|
| 308 |
len = ref_array_len(ra_list); |
| 309 |
if (len == 0) return; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
for (k = 0; k < len-1; k++) { |
| 323 |
j = k + 1; |
| 324 |
while (j > 0) { |
| 325 |
item1 = (char **) ref_array_get(ra_list, j - 1, NULL); |
| 326 |
item2 = (char **) ref_array_get(ra_list, j, NULL); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
if (((item1 && item2) && |
| 333 |
(strcoll(*item1,*item2)) > 0) || |
| 334 |
(!item1 && item2)) { |
| 335 |
ref_array_swap(ra_list, j - 1, j); |
| 336 |
} |
| 337 |
j--; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
TRACE_FLOW_EXIT(); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
static int ini_aug_construct_list(char *dirname , |
| 357 |
const char *patterns[], |
| 358 |
struct access_check *check_perm, |
| 359 |
struct ref_array *ra_list, |
| 360 |
struct ref_array *ra_err) |
| 361 |
{ |
| 362 |
|
| 363 |
int error = EOK; |
| 364 |
DIR *dir = NULL; |
| 365 |
struct dirent *entryp = NULL; |
| 366 |
char *snipname = NULL; |
| 367 |
char fullname[PATH_MAX + 1] = {0}; |
| 368 |
struct ref_array *ra_regex = NULL; |
| 369 |
bool match = false; |
| 370 |
|
| 371 |
TRACE_FLOW_ENTRY(); |
| 372 |
|
| 373 |
|
| 374 |
error = ini_aug_regex_prepare(patterns, |
| 375 |
ra_err, |
| 376 |
&ra_regex); |
| 377 |
if (error) { |
| 378 |
TRACE_ERROR_NUMBER("Failed to prepare regex array.", error); |
| 379 |
return error; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
errno = 0; |
| 384 |
dir = opendir(dirname); |
| 385 |
if (!dir) { |
| 386 |
error = errno; |
| 387 |
if (error == ENOMEM) { |
| 388 |
TRACE_ERROR_NUMBER("No memory to open dir.", ENOMEM); |
| 389 |
ref_array_destroy(ra_regex); |
| 390 |
return ENOMEM; |
| 391 |
} |
| 392 |
|
| 393 |
add_dir_open_error(error, dirname, ra_err); |
| 394 |
ref_array_destroy(ra_regex); |
| 395 |
return EOK; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
while (true) |
| 400 |
{ |
| 401 |
errno = 0; |
| 402 |
entryp = readdir(dir); |
| 403 |
if (entryp == NULL && errno != 0) { |
| 404 |
error = errno; |
| 405 |
TRACE_ERROR_NUMBER("Failed to read directory.", error); |
| 406 |
ref_array_destroy(ra_regex); |
| 407 |
closedir(dir); |
| 408 |
return error; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
if (entryp == NULL) break; |
| 413 |
|
| 414 |
TRACE_INFO_STRING("Processing", entryp->d_name); |
| 415 |
|
| 416 |
|
| 417 |
if ((strncmp(entryp->d_name, |
| 418 |
INI_CURRENT_DIR, |
| 419 |
sizeof(INI_CURRENT_DIR)) == 0) || |
| 420 |
(strncmp(entryp->d_name, |
| 421 |
INI_PARENT_DIR, |
| 422 |
sizeof(INI_PARENT_DIR)) == 0)) continue; |
| 423 |
|
| 424 |
error = path_concat(fullname, PATH_MAX, dirname, entryp->d_name); |
| 425 |
if (error != EOK) { |
| 426 |
TRACE_ERROR_NUMBER("path_concat failed.", error); |
| 427 |
ref_array_destroy(ra_regex); |
| 428 |
closedir(dir); |
| 429 |
return error; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
match = ini_aug_match_name(entryp->d_name, ra_regex); |
| 434 |
if (match) { |
| 435 |
if(ini_check_file_perm(fullname, check_perm, ra_err)) { |
| 436 |
|
| 437 |
|
| 438 |
snipname = NULL; |
| 439 |
snipname = strdup(fullname); |
| 440 |
if (snipname == NULL) { |
| 441 |
TRACE_ERROR_NUMBER("Failed to dup string.", ENOMEM); |
| 442 |
ref_array_destroy(ra_regex); |
| 443 |
closedir(dir); |
| 444 |
return ENOMEM; |
| 445 |
} |
| 446 |
|
| 447 |
error = ref_array_append(ra_list, (void *)&snipname); |
| 448 |
if (error) { |
| 449 |
TRACE_ERROR_NUMBER("No memory to add file to " |
| 450 |
"the snippet list.", |
| 451 |
ENOMEM); |
| 452 |
ref_array_destroy(ra_regex); |
| 453 |
closedir(dir); |
| 454 |
return ENOMEM; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
else { |
| 459 |
TRACE_INFO_STRING("File did not match provided patterns." |
| 460 |
" Skipping:", |
| 461 |
fullname); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
closedir(dir); |
| 466 |
ref_array_destroy(ra_regex); |
| 467 |
|
| 468 |
ini_aug_sort_list(ra_list); |
| 469 |
|
| 470 |
TRACE_FLOW_EXIT(); |
| 471 |
return EOK; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
static int ini_aug_expand_path(const char *path, char **fullname) |
| 476 |
{ |
| 477 |
int error = EOK; |
| 478 |
char *dirname = NULL; |
| 479 |
|
| 480 |
TRACE_FLOW_ENTRY(); |
| 481 |
TRACE_INFO_STRING("Input path", path); |
| 482 |
|
| 483 |
dirname = malloc(PATH_MAX + 1); |
| 484 |
if (!dirname) { |
| 485 |
TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", ENOMEM); |
| 486 |
return ENOMEM; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
error = make_normalized_absolute_path(dirname, |
| 491 |
PATH_MAX, |
| 492 |
path); |
| 493 |
if (error) { |
| 494 |
TRACE_ERROR_NUMBER("Failed to resolve path", error); |
| 495 |
free(dirname); |
| 496 |
|
| 497 |
*fullname = NULL; |
| 498 |
} |
| 499 |
else *fullname = dirname; |
| 500 |
|
| 501 |
TRACE_INFO_STRING("Output path", *fullname); |
| 502 |
TRACE_FLOW_EXIT(); |
| 503 |
|
| 504 |
return EOK; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
static int ini_aug_preprare(const char *path, |
| 509 |
const char *patterns[], |
| 510 |
struct access_check *check_perm, |
| 511 |
struct ref_array *ra_list, |
| 512 |
struct ref_array *ra_err) |
| 513 |
{ |
| 514 |
int error = EOK; |
| 515 |
char *dirname = NULL; |
| 516 |
|
| 517 |
TRACE_FLOW_ENTRY(); |
| 518 |
|
| 519 |
|
| 520 |
error = ini_aug_expand_path(path, &dirname); |
| 521 |
if (error) { |
| 522 |
TRACE_ERROR_NUMBER("Failed to allocate memory for dir path.", error); |
| 523 |
return error; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
if (!dirname) { |
| 528 |
TRACE_ERROR_NUMBER("Failed to resolve path", error); |
| 529 |
ini_aug_add_string(ra_err, "Could not resolve directory path %s.", |
| 530 |
path); |
| 531 |
|
| 532 |
return EOK; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
error = ini_aug_construct_list(dirname, |
| 537 |
patterns, |
| 538 |
check_perm, |
| 539 |
ra_list, |
| 540 |
ra_err); |
| 541 |
free(dirname); |
| 542 |
|
| 543 |
TRACE_FLOW_EXIT(); |
| 544 |
return error; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
static void array_cleanup(void *elem, |
| 549 |
ref_array_del_enum type, |
| 550 |
void *data) |
| 551 |
{ |
| 552 |
TRACE_FLOW_ENTRY(); |
| 553 |
free(*((char **)elem)); |
| 554 |
TRACE_FLOW_EXIT(); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
static int ini_aug_match_sec(struct ini_cfgobj *snip_cfg, |
| 559 |
struct ref_array *ra_regex, |
| 560 |
struct ref_array *ra_err, |
| 561 |
char *snip_name, |
| 562 |
bool *skip) |
| 563 |
{ |
| 564 |
int error = EOK; |
| 565 |
char **section_list = NULL; |
| 566 |
char **section_iter = NULL; |
| 567 |
int size = 0; |
| 568 |
bool match = false; |
| 569 |
int match_count = 0; |
| 570 |
int section_count = 0; |
| 571 |
|
| 572 |
TRACE_FLOW_ENTRY(); |
| 573 |
|
| 574 |
section_list = ini_get_section_list(snip_cfg, &size, &error); |
| 575 |
if (error) { |
| 576 |
TRACE_ERROR_NUMBER("Failed create section list", error); |
| 577 |
return error; |
| 578 |
} |
| 579 |
|
| 580 |
if (section_list == NULL) { |
| 581 |
|
| 582 |
ini_aug_add_string(ra_err, "No sections found in file %s. Skipping.", |
| 583 |
snip_name); |
| 584 |
*skip = true; |
| 585 |
TRACE_FLOW_EXIT(); |
| 586 |
return EOK; |
| 587 |
} |
| 588 |
|
| 589 |
section_iter = section_list; |
| 590 |
|
| 591 |
while (*section_iter) { |
| 592 |
match = ini_aug_match_name(*section_iter, ra_regex); |
| 593 |
if (match) { |
| 594 |
match_count++; |
| 595 |
TRACE_INFO_STRING("Matched section", *section_iter); |
| 596 |
} |
| 597 |
else { |
| 598 |
TRACE_INFO_STRING("Section not matched", *section_iter); |
| 599 |
ini_aug_add_string(ra_err, "Section [%s] found in file %s is" |
| 600 |
" not allowed.", |
| 601 |
*section_iter, snip_name); |
| 602 |
} |
| 603 |
section_count++; |
| 604 |
section_iter++; |
| 605 |
} |
| 606 |
|
| 607 |
ini_free_section_list(section_list); |
| 608 |
|
| 609 |
|
| 610 |
if (section_count == 0) { |
| 611 |
TRACE_INFO_STRING("No sections found in file. Skipping:", |
| 612 |
snip_name); |
| 613 |
*skip = true; |
| 614 |
TRACE_FLOW_EXIT(); |
| 615 |
return EOK; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
if (section_count != match_count) { |
| 620 |
|
| 621 |
ini_aug_add_string(ra_err, "File %s contains sections that" |
| 622 |
" are not allowed. Skipping.", |
| 623 |
snip_name); |
| 624 |
*skip = true; |
| 625 |
TRACE_FLOW_EXIT(); |
| 626 |
return EOK; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
TRACE_INFO_STRING("File will be included", snip_name); |
| 631 |
*skip = false; |
| 632 |
TRACE_FLOW_EXIT(); |
| 633 |
return EOK; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
static int ini_aug_apply(struct ini_cfgobj *cfg, |
| 639 |
struct ref_array *ra_list, |
| 640 |
const char *sections[], |
| 641 |
int error_level, |
| 642 |
uint32_t collision_flags, |
| 643 |
uint32_t parse_flags, |
| 644 |
uint32_t merge_flags, |
| 645 |
struct ref_array *ra_err, |
| 646 |
struct ref_array *ra_ok, |
| 647 |
struct ini_cfgobj **out_cfg) |
| 648 |
{ |
| 649 |
int error = EOK; |
| 650 |
uint32_t len = 0; |
| 651 |
uint32_t i = 0; |
| 652 |
uint32_t j = 0; |
| 653 |
struct ini_cfgfile *file_ctx = NULL; |
| 654 |
struct ini_cfgobj *snip_cfg = NULL; |
| 655 |
struct ini_cfgobj *res_cfg = NULL; |
| 656 |
struct ini_cfgobj *tmp_cfg = NULL; |
| 657 |
char **error_list = NULL; |
| 658 |
unsigned cnt = 0; |
| 659 |
bool skip = false; |
| 660 |
struct ref_array *ra_regex = NULL; |
| 661 |
char *snip_name = NULL; |
| 662 |
char **snip_name_ptr = NULL; |
| 663 |
|
| 664 |
TRACE_FLOW_ENTRY(); |
| 665 |
|
| 666 |
error = ini_config_copy(cfg, &res_cfg); |
| 667 |
if (error) { |
| 668 |
TRACE_ERROR_NUMBER("Failed to copy config object", error); |
| 669 |
*out_cfg = NULL; |
| 670 |
return error; |
| 671 |
} |
| 672 |
|
| 673 |
len = ref_array_len(ra_list); |
| 674 |
if (len == 0) { |
| 675 |
|
| 676 |
*out_cfg = res_cfg; |
| 677 |
TRACE_FLOW_EXIT(); |
| 678 |
return EOK; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
error = ini_aug_regex_prepare(sections, |
| 683 |
ra_err, |
| 684 |
&ra_regex); |
| 685 |
if (error) { |
| 686 |
TRACE_ERROR_NUMBER("Failed to prepare regex array.", error); |
| 687 |
*out_cfg = res_cfg; |
| 688 |
return error; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
for (i = 0; i < len; i++) { |
| 693 |
|
| 694 |
|
| 695 |
error = ini_config_create(&snip_cfg); |
| 696 |
if (error) { |
| 697 |
TRACE_ERROR_NUMBER("Failed to create config object", error); |
| 698 |
goto err; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
snip_name_ptr = (char **)ref_array_get (ra_list, i, NULL); |
| 703 |
if (snip_name_ptr == NULL) continue; |
| 704 |
snip_name = *snip_name_ptr; |
| 705 |
if (snip_name == NULL) continue; |
| 706 |
|
| 707 |
TRACE_INFO_STRING("Processing", snip_name); |
| 708 |
|
| 709 |
|
| 710 |
error = ini_config_file_open(snip_name, |
| 711 |
INI_META_NONE, |
| 712 |
&file_ctx); |
| 713 |
if (error) { |
| 714 |
TRACE_ERROR_NUMBER("Failed to open snippet.", error); |
| 715 |
ini_aug_add_string(ra_err, "Failed to open file %s.", snip_name); |
| 716 |
ini_config_destroy(snip_cfg); |
| 717 |
|
| 718 |
continue; |
| 719 |
} |
| 720 |
|
| 721 |
TRACE_INFO_NUMBER("Error level:", error_level); |
| 722 |
TRACE_INFO_NUMBER("Collision flags:", collision_flags); |
| 723 |
TRACE_INFO_NUMBER("Parse level:", parse_flags); |
| 724 |
|
| 725 |
|
| 726 |
error = ini_config_parse(file_ctx, |
| 727 |
error_level, |
| 728 |
collision_flags, |
| 729 |
parse_flags, |
| 730 |
snip_cfg); |
| 731 |
|
| 732 |
ini_config_file_destroy(file_ctx); |
| 733 |
file_ctx = NULL; |
| 734 |
|
| 735 |
if (error) { |
| 736 |
TRACE_ERROR_NUMBER("Failed to parse configuration.", error); |
| 737 |
cnt = ini_config_error_count(snip_cfg); |
| 738 |
if (cnt) { |
| 739 |
ini_aug_add_string(ra_err, |
| 740 |
"Errors detected while parsing: %s.", |
| 741 |
snip_name); |
| 742 |
|
| 743 |
|
| 744 |
error = ini_config_get_errors(snip_cfg, &error_list); |
| 745 |
if (error) { |
| 746 |
TRACE_ERROR_NUMBER("Can't get errors.", error); |
| 747 |
ini_config_destroy(snip_cfg); |
| 748 |
goto err; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
for (j=0; j< cnt; j++) { |
| 753 |
ini_aug_add_string(ra_err, error_list[j]); |
| 754 |
} |
| 755 |
ini_config_free_errors(error_list); |
| 756 |
} |
| 757 |
|
| 758 |
if (error_level != INI_STOP_ON_NONE) { |
| 759 |
ini_aug_add_string(ra_err, |
| 760 |
"Due to errors file %s is not considered." |
| 761 |
" Skipping.", |
| 762 |
snip_name); |
| 763 |
ini_config_destroy(snip_cfg); |
| 764 |
continue; |
| 765 |
} |
| 766 |
|
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
if (sections) { |
| 771 |
|
| 772 |
|
| 773 |
skip = true; |
| 774 |
error = ini_aug_match_sec(snip_cfg, ra_regex, ra_err, |
| 775 |
snip_name, &skip); |
| 776 |
if (error) { |
| 777 |
TRACE_ERROR_NUMBER("Failed to validate section.", error); |
| 778 |
ini_config_destroy(snip_cfg); |
| 779 |
goto err; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
if (!skip) { |
| 785 |
|
| 786 |
error = ini_config_merge(res_cfg, snip_cfg, merge_flags, &tmp_cfg); |
| 787 |
if (error) { |
| 788 |
if (error == ENOMEM) { |
| 789 |
TRACE_ERROR_NUMBER("Merge failed.", error); |
| 790 |
ini_config_destroy(snip_cfg); |
| 791 |
goto err; |
| 792 |
} |
| 793 |
else if |
| 794 |
((error == EEXIST) && |
| 795 |
((ini_flags_have(INI_MS_DETECT, merge_flags) && |
| 796 |
((merge_flags & INI_MV2S_MASK) != INI_MV2S_ERROR)) || |
| 797 |
((!ini_flags_have(INI_MS_ERROR, merge_flags)) && |
| 798 |
((merge_flags & INI_MV2S_MASK) == INI_MV2S_DETECT)))) { |
| 799 |
TRACE_ERROR_NUMBER("Got error in detect mode", error); |
| 800 |
|
| 801 |
ini_aug_add_string(ra_err, "Duplicate section detected " |
| 802 |
"in snippet: %s.", snip_name); |
| 803 |
} |
| 804 |
else { |
| 805 |
ini_aug_add_string(ra_err, |
| 806 |
"Errors during merge." |
| 807 |
" Snippet ignored %s.", |
| 808 |
snip_name); |
| 809 |
|
| 810 |
TRACE_INFO_NUMBER("Merge failure.Continue. Error", error); |
| 811 |
ini_config_destroy(snip_cfg); |
| 812 |
continue; |
| 813 |
} |
| 814 |
} |
| 815 |
TRACE_INFO_STRING("Merged file.", snip_name); |
| 816 |
|
| 817 |
ini_config_destroy(res_cfg); |
| 818 |
res_cfg = tmp_cfg; |
| 819 |
|
| 820 |
|
| 821 |
ini_aug_add_string(ra_ok, "%s", snip_name); |
| 822 |
} |
| 823 |
|
| 824 |
ini_config_destroy(snip_cfg); |
| 825 |
} |
| 826 |
|
| 827 |
ref_array_destroy(ra_regex); |
| 828 |
*out_cfg = res_cfg; |
| 829 |
TRACE_FLOW_EXIT(); |
| 830 |
return error; |
| 831 |
|
| 832 |
err: |
| 833 |
ini_config_destroy(res_cfg); |
| 834 |
ref_array_destroy(ra_regex); |
| 835 |
|
| 836 |
if (ini_config_copy(cfg, &res_cfg)) { |
| 837 |
TRACE_ERROR_NUMBER("Failed to copy config object", error); |
| 838 |
*out_cfg = NULL; |
| 839 |
return error; |
| 840 |
} |
| 841 |
|
| 842 |
*out_cfg = res_cfg; |
| 843 |
|
| 844 |
return error; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
int ini_config_augment(struct ini_cfgobj *base_cfg, |
| 851 |
const char *path, |
| 852 |
const char *patterns[], |
| 853 |
const char *sections[], |
| 854 |
struct access_check *check_perm, |
| 855 |
int error_level, |
| 856 |
uint32_t collision_flags, |
| 857 |
uint32_t parse_flags, |
| 858 |
uint32_t merge_flags, |
| 859 |
struct ini_cfgobj **result_cfg, |
| 860 |
struct ref_array **error_list, |
| 861 |
struct ref_array **success_list) |
| 862 |
{ |
| 863 |
int error = EOK; |
| 864 |
|
| 865 |
struct ref_array *ra_list = NULL; |
| 866 |
|
| 867 |
struct ref_array *ra_err = NULL; |
| 868 |
|
| 869 |
struct ref_array *ra_ok = NULL; |
| 870 |
|
| 871 |
|
| 872 |
if (base_cfg == NULL) { |
| 873 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 874 |
return EINVAL; |
| 875 |
} |
| 876 |
|
| 877 |
if (result_cfg == NULL) { |
| 878 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 879 |
return EINVAL; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
if ((ref_array_create(&ra_list, |
| 885 |
sizeof(char *), |
| 886 |
INI_AUG_ARR_SIZE_INC, |
| 887 |
array_cleanup, |
| 888 |
NULL) != 0) || |
| 889 |
(ref_array_create(&ra_err, |
| 890 |
sizeof(char *), |
| 891 |
INI_AUG_ARR_SIZE_INC * 5, |
| 892 |
array_cleanup, |
| 893 |
NULL) != 0) || |
| 894 |
(ref_array_create(&ra_ok, |
| 895 |
sizeof(char *), |
| 896 |
INI_AUG_ARR_SIZE_INC * 5, |
| 897 |
array_cleanup, |
| 898 |
NULL) != 0)) { |
| 899 |
TRACE_ERROR_NUMBER("Failed to allocate memory for arrays.", |
| 900 |
ENOMEM); |
| 901 |
ref_array_destroy(ra_list); |
| 902 |
ref_array_destroy(ra_err); |
| 903 |
ref_array_destroy(ra_ok); |
| 904 |
return ENOMEM; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
error = ini_aug_preprare(path, |
| 909 |
patterns, |
| 910 |
check_perm, |
| 911 |
ra_list, |
| 912 |
ra_err); |
| 913 |
if (error) { |
| 914 |
TRACE_ERROR_NUMBER("Failed to prepare lists of snippets.", |
| 915 |
error); |
| 916 |
ref_array_destroy(ra_list); |
| 917 |
ref_array_destroy(ra_err); |
| 918 |
ref_array_destroy(ra_ok); |
| 919 |
return error; |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
error = ini_aug_apply(base_cfg, |
| 924 |
ra_list, |
| 925 |
sections, |
| 926 |
error_level, |
| 927 |
collision_flags, |
| 928 |
parse_flags, |
| 929 |
merge_flags, |
| 930 |
ra_err, |
| 931 |
ra_ok, |
| 932 |
result_cfg); |
| 933 |
|
| 934 |
|
| 935 |
ref_array_destroy(ra_list); |
| 936 |
|
| 937 |
if (error_list) { |
| 938 |
*error_list = ra_err; |
| 939 |
} |
| 940 |
else { |
| 941 |
ref_array_destroy(ra_err); |
| 942 |
} |
| 943 |
|
| 944 |
if (success_list) { |
| 945 |
*success_list = ra_ok; |
| 946 |
} |
| 947 |
else { |
| 948 |
ref_array_destroy(ra_ok); |
| 949 |
} |
| 950 |
|
| 951 |
TRACE_FLOW_EXIT(); |
| 952 |
return error; |
| 953 |
} |