From: Guus Sliepen Date: Tue, 13 Oct 2015 12:12:13 +0000 (+0200) Subject: Don't append duplicate entries to configuration files. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=9bc18d95a44be1eea1b39b16e6a6916355b9db08;p=meshlink Don't append duplicate entries to configuration files. Automatically detected addresses are appended to host config files by Catta. However, it doesn't check whether the address is already known. Have append_config_file() check that it doesn't write duplicate entries. --- diff --git a/src/conf.c b/src/conf.c index 2195d69c..8acb64b9 100644 --- a/src/conf.c +++ b/src/conf.c @@ -477,10 +477,29 @@ bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *k if(!fp) { logger(mesh, MESHLINK_ERROR, "Cannot open config file %s: %s", filename, strerror(errno)); - } else { - fprintf(fp, "%s = %s\n", key, value); - fclose(fp); + return false; + } + + // Check if we don't add a duplicate entry + + char entry[MAX_STRING_SIZE]; + snprintf(entry, sizeof entry, "%s = %s", key, value); + + char buffer[MAX_STRING_SIZE]; + bool found = false; + + while(readline(fp, buffer, sizeof buffer)) { + if(!strcmp(buffer, entry)) { + found = true; + break; + } } - return fp != NULL; + // If not, append the new entry + + if(!found) + fprintf(fp, "%s\n", entry); + + fclose(fp); + return true; }