From 8518be376f51fc3e682597a97157914d4745d5bc Mon Sep 17 00:00:00 2001 From: "Safey A.Halim" Date: Mon, 9 Nov 2009 17:56:54 +0000 Subject: * 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. --- src/util/configuration.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'src/util/configuration.c') 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 @@ -461,6 +461,107 @@ findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, } +/** + * A callback function, compares entries from two configurations (default against a new configuration) + * and write the diffs in a diff-configuration object (the callback object). + * @param cls the diff configuration (ConfigurationDiffHandle*) + * @param section section for the value (of the default conf.) + * @param option option name of the value (of the default conf.) + * @param value value to copy (of the default conf.) + */ +void +compareEntries( + void *cls, + const char *section, + const char *option, + const char *value) +{ + struct ConfigSection *secNew; + struct ConfigEntry *entNew; + GNUNNET_CONFIGURATION_Diff_Handle* cfgDiff = (GNUNNET_CONFIGURATION_Diff_Handle*)cls; + + secNew = findSection(cfgDiff->cfgNew, section); + entNew = findEntry(cfgDiff->cfgNew, section, option); + if (secNew && strcmp(entNew->val, value) != 0) { + /* Value in the new configuration has been changed */ + /* Add the changed value to the diff configuration object */ + struct ConfigEntry *diffEntry = NULL; + struct ConfigSection *diffSection = NULL; + + diffSection = cfgDiff->cfgDiff->sections; + if (diffSection == NULL) { + /* First section */ + diffSection = GNUNET_malloc(sizeof(struct ConfigSection)); + memcpy(diffSection, secNew, sizeof(struct ConfigSection)); + cfgDiff->cfgDiff->sections = diffSection; + diffSection->entries = NULL; + diffSection->next = NULL; + } + else { + while ((strcmp(diffSection->name, secNew->name) != 0) && (diffSection->next != NULL)) { + diffSection = diffSection->next; + } + if (strcmp(diffSection->name, secNew->name) != 0) { + /* Section not found in diffs configuration */ + diffSection->next = GNUNET_malloc(sizeof(struct ConfigSection)); + memcpy(diffSection->next, secNew, sizeof(struct ConfigSection)); + diffSection->next->entries = NULL; + diffSection->next->next = NULL; + } + else { + diffEntry = diffSection->entries; + } + } + + if (diffEntry == NULL) { + /* First Entry */ + diffEntry = GNUNET_malloc(sizeof(struct ConfigEntry)); + memcpy(diffEntry, entNew, sizeof(struct ConfigEntry)); + if (diffSection->next == NULL) + /* The first Entry of the first Section */ + diffSection->entries = diffEntry; + else + /* The first entry of the non-first Section */ + diffSection->next->entries = diffEntry; + diffEntry->next = NULL; + } + else { + while (diffEntry->next != NULL) { + diffEntry = diffEntry->next; + } + diffEntry->next = GNUNET_malloc(sizeof(struct ConfigEntry)); + memcpy(diffEntry->next, entNew, sizeof(struct ConfigEntry)); + diffEntry->next->next = NULL; + } + } +} + + +/** + * Write only configuration entries that have been changed to configuration file + * @param cfgDefault default configuration + * @param cfgNew new configuration + * @param filename where to write the configuration diff between default and new + * @return GNUNET_OK on success, GNUNET_SYSERR on error + */ +int +GNUNET_CONFIGURATION_write_diffs( + struct GNUNET_CONFIGURATION_Handle *cfgDefault, + struct GNUNET_CONFIGURATION_Handle *cfgNew, + const char* filename + ) +{ + GNUNNET_CONFIGURATION_Diff_Handle *diffHandle = + GNUNET_malloc(sizeof(GNUNNET_CONFIGURATION_Diff_Handle)); + diffHandle->cfgDiff = GNUNET_CONFIGURATION_create(); + diffHandle->cfgDiff->sections = NULL; + diffHandle->cfgNew = cfgNew; + GNUNET_CONFIGURATION_iterate(cfgDefault, compareEntries, diffHandle); + + return GNUNET_CONFIGURATION_write(diffHandle->cfgDiff, filename); +} + + /** * Set a configuration value that should be a string. * -- cgit v1.2.3