summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeresa Remmet <t.remmet@phytec.de>2017-01-16 10:07:39 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2017-01-17 07:57:16 +0100
commitd7253200f53a2a3ca3739f2b2bf106ee6ba09496 (patch)
tree3ff3c447817deac4c69cbca281b50ef3160db879
parente1e8656a7d24133758dc65512a5714c9d0bf1abd (diff)
downloadbarebox-d7253200f53a2a3ca3739f2b2bf106ee6ba09496.tar.gz
barebox-d7253200f53a2a3ca3739f2b2bf106ee6ba09496.tar.xz
lib: libfile: Fix copying files to a non existing destination
If the destination file does not exist the stat call to check the file type fails. Extend the check of the stat return value. To allow to copy files to a new destination. Fixes commit 0ec6bd3e1be8 ("libfile: copy_file: Only open regular files with O_TRUNC") Signed-off-by: Teresa Remmet <t.remmet@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-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);