return write_config_file(config_tree, filename);
}
-bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, bool replace) {
- assert(mesh && name && key && (replace || value));
+bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim) {
+ assert(mesh && name && key);
char filename[PATH_MAX];
char tmpname[PATH_MAX];
char buf[4096];
char *sep;
- bool found = false;
+ int found = 0;
+
+ if(value) {
+ fprintf(fw, "%s = %s\n", key, value);
+ found++;
+ }
while(readline(fr, buf, sizeof(buf))) {
if(!*buf || *buf == '#')
goto copy;
}
- if(!value) {
- found = true;
+ // We found the key and the value. We already added it at the top, so ignore this one.
+ if(sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value))
continue;
- }
-
- // We found the key and the value. Keep one copy around.
- if(sep[1] == '=' && sep[2] == ' ' && !strcmp(sep + 3, value)) {
- if(found)
- continue;
- found = true;
- }
// We found the key but with a different value, delete it if wanted.
- if(!found && replace)
+ found++;
+ if((!value || trim) && found > trim)
continue;
*sep = ' ';
fclose(fr);
- // Add new key/value pair if necessary
- if(!found && value)
- fprintf(fw, "%s = %s\n", key, value);
-
if(ferror(fw))
error = true;
}
bool append_config_file(meshlink_handle_t *mesh, const char *name, const char *key, const char *value) {
- return modify_config_file(mesh, name, key, value, false);
+ return modify_config_file(mesh, name, key, value, 0);
}
extern bool read_server_config(struct meshlink_handle *mesh);
extern bool read_host_config(struct meshlink_handle *mesh, struct splay_tree_t *, const char *);
extern bool write_host_config(struct meshlink_handle *mesh, const struct splay_tree_t *, const char *);
-extern bool modify_config_file(struct meshlink_handle *mesh, const char *, const char *, const char *, bool);
-extern bool append_config_file(struct meshlink_handle *mesh, const char *, const char *, const char *);
+extern bool modify_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value, int trim);
+extern bool append_config_file(struct meshlink_handle *mesh, const char *name, const char *key, const char *value);
#endif
if(host && port) {
xasprintf(&str, "%s %s", host, port);
if((strncmp("fe80", host, 4) != 0) && (strncmp("127.", host, 4) != 0) && (strcmp("localhost", host) != 0))
- append_config_file(mesh, node->name, "Address", str);
+ modify_config_file(mesh, node->name, "Address", str, 5);
else
logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n");
}