From 4b2ddded2c8ae1a1a5930637552eeb48f30d6530 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 9 Feb 2015 15:23:59 +0100 Subject: [PATCH] Make "tinc add" idempotent. When calling "tinc add" multiple times with the same variable and value, make sure only one unique line is added to the configuration file. --- doc/tinc.8.in | 1 + doc/tinc.texi | 1 + src/tincctl.c | 7 ++++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/tinc.8.in b/doc/tinc.8.in index aa4e8332..02f1b9a7 100644 --- a/doc/tinc.8.in +++ b/doc/tinc.8.in @@ -88,6 +88,7 @@ To set a variable for a specific host, use the notation .Ar host Ns Li . Ns Ar variable . .It add Ar variable Ar value As above, but without removing any previously existing configuration variables. +If the variable already exists with the given value, nothing happens. .It del Ar variable Op Ar value Remove configuration variables with the same name and .Ar value . diff --git a/doc/tinc.texi b/doc/tinc.texi index 2f4949e1..442da2f9 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -2293,6 +2293,7 @@ To set a variable for a specific host, use the notation @var{host}.@var{variable @cindex add @item add @var{variable} @var{value} As above, but without removing any previously existing configuration variables. +If the variable already exists with the given value, nothing happens. @cindex del @item del @var{variable} [@var{value}] diff --git a/src/tincctl.c b/src/tincctl.c index 04fbdd52..d46a378b 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -1610,6 +1610,11 @@ static int cmd_config(int argc, char *argv[]) { } set = true; continue; + // Add + } else if(action > 0) { + // Check if we've already seen this variable with the same value + if(!strcasecmp(bvalue, value)) + found = true; } } @@ -1642,7 +1647,7 @@ static int cmd_config(int argc, char *argv[]) { } // Add new variable if necessary. - if(action > 0 || (action == 0 && !set)) { + if((action > 0 && !found)|| (action == 0 && !set)) { if(fprintf(tf, "%s = %s\n", variable, value) < 0) { fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno)); return 1; -- 2.20.1