From: Daniel P. Berrangé <[email protected]>

When writing the dnsmasq addnhosts file to a temporary file, there
is a fallback which tries to open the original file. We expect
creating the temporary file to always succeed, and if it fails
something serious has gone wrong that we want to have reported.
So this fallback is expected to be dead code.

Signed-off-by: Daniel P. Berrangé <[email protected]>
---
 src/util/virdnsmasq.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c
index 7d6de943cf..817e060a44 100644
--- a/src/util/virdnsmasq.c
+++ b/src/util/virdnsmasq.c
@@ -161,7 +161,6 @@ addnhostsWrite(const char *path,
 {
     g_autofree char *tmp = NULL;
     FILE *f;
-    bool istmp = true;
     size_t i, j;
     int rc = 0;
 
@@ -171,20 +170,14 @@ addnhostsWrite(const char *path,
 
     tmp = g_strdup_printf("%s.new", path);
 
-    if (!(f = fopen(tmp, "w"))) {
-        istmp = false;
-        if (!(f = fopen(path, "w")))
-            return -errno;
-    }
+    if (!(f = fopen(tmp, "w")))
+        return -errno;
 
     for (i = 0; i < nhosts; i++) {
         if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) {
             rc = -errno;
             VIR_FORCE_FCLOSE(f);
-
-            if (istmp)
-                unlink(tmp);
-
+            unlink(tmp);
             return rc;
         }
 
@@ -192,10 +185,7 @@ addnhostsWrite(const char *path,
             if (fputs(hosts[i].hostnames[j], f) == EOF || fputc('\t', f) == 
EOF) {
                 rc = -errno;
                 VIR_FORCE_FCLOSE(f);
-
-                if (istmp)
-                    unlink(tmp);
-
+                unlink(tmp);
                 return rc;
             }
         }
@@ -203,10 +193,7 @@ addnhostsWrite(const char *path,
         if (fputc('\n', f) == EOF) {
             rc = -errno;
             VIR_FORCE_FCLOSE(f);
-
-            if (istmp)
-                unlink(tmp);
-
+            unlink(tmp);
             return rc;
         }
     }
@@ -214,7 +201,7 @@ addnhostsWrite(const char *path,
     if (VIR_FCLOSE(f) == EOF)
         return -errno;
 
-    if (istmp && rename(tmp, path) < 0) {
+    if (rename(tmp, path) < 0) {
         rc = -errno;
         unlink(tmp);
         return rc;
-- 
2.56.0

Reply via email to