mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] lib: libfile: Fix copying files to a non existing destination
@ 2017-01-16  9:07 Teresa Remmet
  2017-01-17  6:57 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Teresa Remmet @ 2017-01-16  9:07 UTC (permalink / raw)
  To: barebox

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>
---
 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@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] lib: libfile: Fix copying files to a non existing destination
  2017-01-16  9:07 [PATCH v2] lib: libfile: Fix copying files to a non existing destination Teresa Remmet
@ 2017-01-17  6:57 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2017-01-17  6:57 UTC (permalink / raw)
  To: Teresa Remmet; +Cc: barebox

On Mon, Jan 16, 2017 at 10:07:39AM +0100, Teresa Remmet wrote:
> 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>
> ---
>  lib/libfile.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)

Applied (to master), thanks

Sascha

> 
> 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@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-01-17  6:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-16  9:07 [PATCH v2] lib: libfile: Fix copying files to a non existing destination Teresa Remmet
2017-01-17  6:57 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox