summaryrefslogtreecommitdiffstats
path: root/lib/libfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libfile.c')
-rw-r--r--lib/libfile.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/libfile.c b/lib/libfile.c
index 049ec32d77..6b70306dbd 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -276,13 +276,14 @@ int copy_file(const char *src, const char *dst, int verbose)
goto out;
}
+ mode = O_WRONLY | O_CREAT;
+
ret = stat(dst, &dststat);
- if (ret)
+ if (ret && ret != -ENOENT)
goto out;
- mode = O_WRONLY | O_CREAT;
-
- if (S_ISREG(dststat.st_mode))
+ /* Set O_TRUNC only if file exist and is a regular file */
+ if (!ret && S_ISREG(dststat.st_mode))
mode |= O_TRUNC;
dstfd = open(dst, mode);