Make "tinc add" idempotent.
authorGuus Sliepen <guus@tinc-vpn.org>
Mon, 9 Feb 2015 14:23:59 +0000 (15:23 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Mon, 9 Feb 2015 14:23:59 +0000 (15:23 +0100)
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
doc/tinc.texi
src/tincctl.c

index aa4e83323c6dd6663e469f82c33f700e4aaeef58..02f1b9a70778680b358eb1140f38014e6b4a1890 100644 (file)
@@ -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 .
index 2f4949e12ed2e8b02a03b57553b7c4367483983d..442da2f96977cf95829e3aa58b45a8d4e1d7f4d4 100644 (file)
@@ -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}]
index 04fbdd5238b0166e7b36998972568efe9b85bed3..d46a378bd04bd2e7c6f0083743b340aa8d9c7280 100644 (file)
@@ -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;