From: "Rafał Miłecki" <zajec5@gmail.com>
To: Brian Norris <computersforpeace@gmail.com>,
linux-mtd@lists.infradead.org
Cc: "Rafał Miłecki" <zajec5@gmail.com>
Subject: [PATCH mtd-utils] nanddump: check write function result for errors
Date: Mon, 18 Jul 2016 11:18:13 +0200 [thread overview]
Message-ID: <1468833493-28311-1-git-send-email-zajec5@gmail.com> (raw)
Errors may happen, it's e.g. easy on embedded devices to run out of space
when dumping big partitions. This patch adds a helper function for
writing. It deals with partial writes and just returns 0 on success or
error number.
The old code didn't check for errors at all which could result in
incomplete dumps without exiting with an error.
Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
---
nand-utils/nanddump.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 44 insertions(+), 6 deletions(-)
diff --git a/nand-utils/nanddump.c b/nand-utils/nanddump.c
index 4ee7ed4..0bbc97f 100644
--- a/nand-utils/nanddump.c
+++ b/nand-utils/nanddump.c
@@ -293,6 +293,25 @@ nil:
linebuf[lx++] = '\0';
}
+/**
+ * ofd_write - writes whole buffer to the file associated with a descriptor
+ *
+ * On failure an error (negative number) is returned. Otherwise 0 is returned.
+ */
+static int ofd_write(int ofd, const unsigned char *buf, size_t nbyte)
+{
+ ssize_t bytes;
+
+ while (nbyte) {
+ bytes = write(ofd, buf, nbyte);
+ if (bytes < 0)
+ return -errno;
+ buf += bytes;
+ nbyte -= bytes;
+ }
+
+ return 0;
+}
/*
* Main program
@@ -309,6 +328,7 @@ int main(int argc, char * const argv[])
bool eccstats = false;
unsigned char *readbuf = NULL, *oobbuf = NULL;
libmtd_t mtd_desc;
+ int err;
process_options(argc, argv);
@@ -443,10 +463,19 @@ int main(int argc, char * const argv[])
for (i = 0; i < bs; i += PRETTY_ROW_SIZE) {
pretty_dump_to_buffer(readbuf + i, PRETTY_ROW_SIZE,
pretty_buf, PRETTY_BUF_LEN, true, canonical, ofs + i);
- write(ofd, pretty_buf, strlen(pretty_buf));
+ err = write(ofd, pretty_buf, strlen(pretty_buf));
+ if (err) {
+ errmsg("ofd_write: %s\n", strerror(-err));
+ goto closeall;
+ }
}
- } else
- write(ofd, readbuf, bs);
+ } else {
+ err = ofd_write(ofd, readbuf, bs);
+ if (err) {
+ errmsg("ofd_write: %d %s\n", err, strerror(-err));
+ goto closeall;
+ }
+ }
if (omitoob)
continue;
@@ -466,10 +495,19 @@ int main(int argc, char * const argv[])
for (i = 0; i < mtd.oob_size; i += PRETTY_ROW_SIZE) {
pretty_dump_to_buffer(oobbuf + i, mtd.oob_size - i,
pretty_buf, PRETTY_BUF_LEN, false, canonical, 0);
- write(ofd, pretty_buf, strlen(pretty_buf));
+ err = write(ofd, pretty_buf, strlen(pretty_buf));
+ if (err) {
+ errmsg("ofd_write: %s\n", strerror(-err));
+ goto closeall;
+ }
}
- } else
- write(ofd, oobbuf, mtd.oob_size);
+ } else {
+ err = ofd_write(ofd, oobbuf, mtd.oob_size);
+ if (err) {
+ errmsg("ofd_write: %d %s\n", err, strerror(-err));
+ goto closeall;
+ }
+ }
}
/* Close the output file and MTD device, free memory */
--
1.8.4.5
next reply other threads:[~2016-07-18 9:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-18 9:18 Rafał Miłecki [this message]
2016-07-18 11:33 ` [PATCH mtd-utils] nanddump: check write function result for errors Richard Weinberger
2016-07-18 11:59 ` Rafał Miłecki
2016-07-18 14:10 ` Richard Weinberger
2016-07-18 14:51 ` Rafał Miłecki
2016-07-18 18:31 ` Richard Weinberger
2016-07-18 15:09 ` [PATCH V2] " Rafał Miłecki
2016-09-06 19:42 ` Rafał Miłecki
2016-09-07 14:38 ` David Oberhollenzer
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=1468833493-28311-1-git-send-email-zajec5@gmail.com \
--to=zajec5@gmail.com \
--cc=computersforpeace@gmail.com \
--cc=linux-mtd@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;
as well as URLs for NNTP newsgroup(s).