mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Teresa Remmet <t.remmet@phytec.de>
To: barebox@lists.infradead.org
Subject: [PATCH 1/6] libfile: copy_file: Only open regular files with O_TRUNC
Date: Fri, 25 Nov 2016 09:06:02 +0100	[thread overview]
Message-ID: <1480061167-21590-2-git-send-email-t.remmet@phytec.de> (raw)
In-Reply-To: <1480061167-21590-1-git-send-email-t.remmet@phytec.de>

Device files can not truncate in the most cases. Check if the
destination is a regular file and open only those with O_TRUNC.

Signed-off-by: Teresa Remmet <t.remmet@phytec.de>
---
 lib/libfile.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index cba2f02..049ec32 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -263,9 +263,10 @@ int copy_file(const char *src, const char *dst, int verbose)
 	int srcfd = 0, dstfd = 0;
 	int r, w;
 	int ret = 1, err1 = 0;
+	int mode;
 	void *buf;
 	int total = 0;
-	struct stat statbuf;
+	struct stat srcstat, dststat;
 
 	rw_buf = xmalloc(RW_BUF_SIZE);
 
@@ -275,17 +276,26 @@ int copy_file(const char *src, const char *dst, int verbose)
 		goto out;
 	}
 
-	dstfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC);
+	ret = stat(dst, &dststat);
+	if (ret)
+		goto out;
+
+	mode = O_WRONLY | O_CREAT;
+
+	if (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());
 		goto out;
 	}
 
 	if (verbose) {
-		if (stat(src, &statbuf) < 0)
-			statbuf.st_size = 0;
+		if (stat(src, &srcstat) < 0)
+			srcstat.st_size = 0;
 
-		init_progression_bar(statbuf.st_size);
+		init_progression_bar(srcstat.st_size);
 	}
 
 	while (1) {
@@ -310,7 +320,7 @@ int copy_file(const char *src, const char *dst, int verbose)
 		}
 
 		if (verbose) {
-			if (statbuf.st_size && statbuf.st_size != FILESIZE_MAX)
+			if (srcstat.st_size && srcstat.st_size != FILESIZE_MAX)
 				show_progress(total);
 			else
 				show_progress(total / 16384);
-- 
1.9.1


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

  reply	other threads:[~2016-11-25  8:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-25  8:06 [PATCH 0/6] ubi: remove character device flag from static volumes Teresa Remmet
2016-11-25  8:06 ` Teresa Remmet [this message]
2016-11-25  8:06 ` [PATCH 2/6] devfs: Add optional truncate callback for device files Teresa Remmet
2016-11-25  8:06 ` [PATCH 3/6] ubi: Add truncate callback Teresa Remmet
2016-11-25  8:06 ` [PATCH 4/6] fs: Remove O_TRUNC check for devices when open files Teresa Remmet
2016-11-25  8:06 ` [PATCH 5/6] commands: ubi: ubiupdatevol: Open device with O_TRUNC Teresa Remmet
2016-11-25  8:06 ` [PATCH 6/6] ubi: barebox: Remove character device flag from static volumes Teresa Remmet
2016-12-07 20:13 ` [PATCH 0/6] ubi: remove " Sascha Hauer

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=1480061167-21590-2-git-send-email-t.remmet@phytec.de \
    --to=t.remmet@phytec.de \
    --cc=barebox@lists.infradead.org \
    /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