aboutsummaryrefslogtreecommitdiff
path: root/src/util/configuration.c
diff options
context:
space:
mode:
authorSafey A.Halim <safey.allah@gmail.com>2009-11-09 17:56:54 +0000
committerSafey A.Halim <safey.allah@gmail.com>2009-11-09 17:56:54 +0000
commit8518be376f51fc3e682597a97157914d4745d5bc (patch)
treeceed7ce24abfc7170d78f94e3dc31e8ccddaeb28 /src/util/configuration.c
parenta6ed6706bc790380caabdde179877f1589867e0b (diff)
downloadgnunet-8518be376f51fc3e682597a97157914d4745d5bc.tar.gz
gnunet-8518be376f51fc3e682597a97157914d4745d5bc.zip
* test_peer.c is a test case for peer.c
* Changes in configuration.c and gnunet_configuration_lib.h for dumping configuration diffs between the default configuration object and an edited configuration.
Diffstat (limited to 'src/util/configuration.c')
-rw-r--r--src/util/configuration.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/util/configuration.c b/src/util/configuration.c
index c14645067..e35dfacfe 100644
--- a/src/util/configuration.c
+++ b/src/util/configuration.c
@@ -462,6 +462,107 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
462 462
463 463
464/** 464/**
465 * A callback function, compares entries from two configurations (default against a new configuration)
466 * and write the diffs in a diff-configuration object (the callback object).
467 * @param cls the diff configuration (ConfigurationDiffHandle*)
468 * @param section section for the value (of the default conf.)
469 * @param option option name of the value (of the default conf.)
470 * @param value value to copy (of the default conf.)
471 */
472void
473compareEntries(
474 void *cls,
475 const char *section,
476 const char *option,
477 const char *value)
478{
479 struct ConfigSection *secNew;
480 struct ConfigEntry *entNew;
481 GNUNNET_CONFIGURATION_Diff_Handle* cfgDiff = (GNUNNET_CONFIGURATION_Diff_Handle*)cls;
482
483 secNew = findSection(cfgDiff->cfgNew, section);
484 entNew = findEntry(cfgDiff->cfgNew, section, option);
485 if (secNew && strcmp(entNew->val, value) != 0) {
486 /* Value in the new configuration has been changed */
487 /* Add the changed value to the diff configuration object */
488 struct ConfigEntry *diffEntry = NULL;
489 struct ConfigSection *diffSection = NULL;
490
491 diffSection = cfgDiff->cfgDiff->sections;
492 if (diffSection == NULL) {
493 /* First section */
494 diffSection = GNUNET_malloc(sizeof(struct ConfigSection));
495 memcpy(diffSection, secNew, sizeof(struct ConfigSection));
496 cfgDiff->cfgDiff->sections = diffSection;
497 diffSection->entries = NULL;
498 diffSection->next = NULL;
499 }
500 else {
501 while ((strcmp(diffSection->name, secNew->name) != 0) && (diffSection->next != NULL)) {
502 diffSection = diffSection->next;
503 }
504 if (strcmp(diffSection->name, secNew->name) != 0) {
505 /* Section not found in diffs configuration */
506 diffSection->next = GNUNET_malloc(sizeof(struct ConfigSection));
507 memcpy(diffSection->next, secNew, sizeof(struct ConfigSection));
508 diffSection->next->entries = NULL;
509 diffSection->next->next = NULL;
510 }
511 else {
512 diffEntry = diffSection->entries;
513 }
514 }
515
516 if (diffEntry == NULL) {
517 /* First Entry */
518 diffEntry = GNUNET_malloc(sizeof(struct ConfigEntry));
519 memcpy(diffEntry, entNew, sizeof(struct ConfigEntry));
520 if (diffSection->next == NULL)
521 /* The first Entry of the first Section */
522 diffSection->entries = diffEntry;
523 else
524 /* The first entry of the non-first Section */
525 diffSection->next->entries = diffEntry;
526 diffEntry->next = NULL;
527 }
528 else {
529 while (diffEntry->next != NULL) {
530 diffEntry = diffEntry->next;
531 }
532 diffEntry->next = GNUNET_malloc(sizeof(struct ConfigEntry));
533 memcpy(diffEntry->next, entNew, sizeof(struct ConfigEntry));
534 diffEntry->next->next = NULL;
535 }
536 }
537}
538
539
540/**
541 * Write only configuration entries that have been changed to configuration file
542 * @param cfgDefault default configuration
543 * @param cfgNew new configuration
544 * @param filename where to write the configuration diff between default and new
545 * @return GNUNET_OK on success, GNUNET_SYSERR on error
546 */
547int
548GNUNET_CONFIGURATION_write_diffs(
549 struct GNUNET_CONFIGURATION_Handle *cfgDefault,
550 struct GNUNET_CONFIGURATION_Handle *cfgNew,
551 const char* filename
552 )
553{
554 GNUNNET_CONFIGURATION_Diff_Handle *diffHandle =
555 GNUNET_malloc(sizeof(GNUNNET_CONFIGURATION_Diff_Handle));
556 diffHandle->cfgDiff = GNUNET_CONFIGURATION_create();
557 diffHandle->cfgDiff->sections = NULL;
558 diffHandle->cfgNew = cfgNew;
559 GNUNET_CONFIGURATION_iterate(cfgDefault, compareEntries, diffHandle);
560
561 return GNUNET_CONFIGURATION_write(diffHandle->cfgDiff, filename);
562}
563
564
565/**
465 * Set a configuration value that should be a string. 566 * Set a configuration value that should be a string.
466 * 567 *
467 * @param cfg configuration to update 568 * @param cfg configuration to update