mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: Bastian Krause <bst@pengutronix.de>
Subject: [PATCH] libfile: copy_file: Fix ftruncate called on device files
Date: Tue, 18 Aug 2020 11:20:39 +0200	[thread overview]
Message-ID: <20200818092039.2521-1-s.hauer@pengutronix.de> (raw)

ftruncate may only be called on regular files. This became broken in
a5f73a6dcd. Revert to the previous behaviour and pass O_TRUNC to the
initial open() call instead of truncating it with ftruncate().

Also we can call ftruncate() to the final size also when the file
previously didn't exist.

Fixes: a5f73a6dcd ("libfile: copy_file: explicitly truncate to final size")
Reported-by: Bastian Krause <bst@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/libfile.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index 863b6833a5..20bb689a79 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -353,6 +353,10 @@ int copy_file(const char *src, const char *dst, int verbose)
 		goto out;
 	}
 
+	/* Set O_TRUNC only if file exists and is a regular file */
+	if (!s && S_ISREG(dststat.st_mode))
+		mode |= O_TRUNC;
+
 	dstfd = open(dst, mode);
 	if (dstfd < 0) {
 		printf("could not open %s: %s\n", dst, errno_str());
@@ -360,17 +364,13 @@ int copy_file(const char *src, const char *dst, int verbose)
 		goto out;
 	}
 
-	ret = ftruncate(dstfd, 0);
-	if (ret)
-		goto out;
-
 	ret = stat(src, &srcstat);
 	if (ret)
 		goto out;
 
 	if (srcstat.st_size != FILESIZE_MAX) {
 		discard_range(dstfd, srcstat.st_size, 0);
-		if (S_ISREG(dststat.st_mode)) {
+		if (s || S_ISREG(dststat.st_mode)) {
 			ret = ftruncate(dstfd, srcstat.st_size);
 			if (ret)
 				goto out;
-- 
2.28.0


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

                 reply	other threads:[~2020-08-18  9:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200818092039.2521-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=bst@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox