[PATCH v2] lib: libfile: Fix copying files to a non existing destination

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 lib/libfile.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 049ec32..6b70306 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);
-- 
1.9.1


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux