From 9bc18d95a44be1eea1b39b16e6a6916355b9db08 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 13 Oct 2015 14:12:13 +0200 Subject: [PATCH] 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. --- src/conf.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) 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; } -- 2.39.5