]> git.meshlink.io Git - meshlink/commitdiff
Don't append duplicate entries to configuration files.
authorGuus Sliepen <guus@meshlink.io>
Tue, 13 Oct 2015 12:12:13 +0000 (14:12 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sun, 25 Jun 2017 08:19:17 +0000 (10:19 +0200)
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

index 2195d69c42ff32230e85f54c67bae00aeac9a674..8acb64b9cea55fffc1fad869d526a2b4c91353bb 100644 (file)
@@ -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;
 }