mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/2] scripts: bareboximd: fix write_file error handling
Date: Mon,  4 May 2020 10:35:41 +0200	[thread overview]
Message-ID: <20200504083542.4095-1-s.trumtrar@pengutronix.de> (raw)

write will never return 0 on POSIX conformant systems. Remove this error
path.
Also, close the file on error.

Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
 scripts/bareboximd.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index b332d435a66f..6ba6dabf3a2c 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -56,7 +56,7 @@ int imd_command_setenv(const char *variable_name, const char *value)
 
 static int write_file(const char *filename, const void *buf, size_t size)
 {
-	int fd, ret;
+	int fd, ret = 0;
 	int now;
 
 	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
@@ -65,22 +65,17 @@ static int write_file(const char *filename, const void *buf, size_t size)
 
 	while (size) {
 		now = write(fd, buf, size);
-		if (now == 0) {
-			errno = ENOSPC;
-			return -1;
+		if (now < 0) {
+			ret = now;
+			goto out;
 		}
-		if (now < 0)
-			return now;
 		size -= now;
 		buf += now;
 	}
 
+out:
 	close(fd);
-
-	if (ret < 0)
-		return ret;
-
-	return 0;
+	return ret;
 }
 
 static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
-- 
2.26.2


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

             reply	other threads:[~2020-05-04  8:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-04  8:35 Steffen Trumtrar [this message]
2020-05-04  8:35 ` [PATCH 2/2] common: imd: handle error in imd_write_crc32 Steffen Trumtrar
2020-05-05  5:51 ` [PATCH 1/2] scripts: bareboximd: fix write_file error handling 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=20200504083542.4095-1-s.trumtrar@pengutronix.de \
    --to=s.trumtrar@pengutronix.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