From patchwork Mon Jul 18 09:18:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 649413 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rtHlr3vnQz9s3s for ; Mon, 18 Jul 2016 19:23:20 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=TFbQwax2; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bP4jl-0006vj-Ig; Mon, 18 Jul 2016 09:21:17 +0000 Received: from mail-lf0-x242.google.com ([2a00:1450:4010:c07::242]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bP4hP-0003tu-Bj for linux-mtd@lists.infradead.org; Mon, 18 Jul 2016 09:18:59 +0000 Received: by mail-lf0-x242.google.com with SMTP id l69so6409236lfg.1 for ; Mon, 18 Jul 2016 02:18:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=1YrKfMi2QTC/mbJCatGIrWqPr6DGh0PNxrVU2nr1weM=; b=TFbQwax2ohVUY9qglyl9OKWyUA/e309IoUIjOzfjWE38tzMxcCfwD53fpCC09gyIsw XLtb62/ixmIRxW6RqhhINaxV/il04CB7mz8IR3LfiQjtZXxF7L0O/oKpt0dCW+mGsVpL eIwKqvzOMWf19WmK81clynTcdcdGcPBdXQn1xhGoBYuxMfbYIuGvITgRE1Hwe1jTVI+c qCWijoqyrpjgUoHKpjM9I0PfCTGXAjw4TWIUp5/qvRtwfUquhxIOW3S6P/m+clK1lUbo 2tcNMCbYp+B4ube5u0/zoT5U2Jg5GVI+BUofWaUTDCRICR5g03t91GrER+Z9SyTgvWnE zHYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=1YrKfMi2QTC/mbJCatGIrWqPr6DGh0PNxrVU2nr1weM=; b=QeZ44+qwPrn5Mh1+IbsDziCxpUYPy8A3L4bcyF6DOG6CDOlJa0iLfeWTmdvnIBL9JK Y7v2aP5mVZh6MONYnXREUF85UED7pSZAFDzehQjZlGyOxBpQydgXTn5eIMHcYrCwtBjS kDCxnHc3PSlsrL4M0Jfqiah8L8E9/d5kUvWFGv3s0H+HLbF47X+YnIcbc/pEduFv3EMm 0iRfpJ+txg07Msl36i9cg0MCiejGRx/L5DQmP3Ur4uxnp8Flt9BPrHUSSdJEiccpu5JV M8gWUSGMGf/G9f0FUA9lrYbY4ElMtyU686pWUJJGYL8Ctb3RVLfyyzEsrYn8w872hTcK O9Hg== X-Gm-Message-State: ALyK8tIe3DwGuK+iDG56WW/GyvYhcioVnRgdVYb64HJ9PutEWadFQbwQJB6yk0M01m56yg== X-Received: by 10.25.215.145 with SMTP id q17mr13929021lfi.30.1468833508622; Mon, 18 Jul 2016 02:18:28 -0700 (PDT) Received: from linux-samsung.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id 35sm1740158ljb.10.2016.07.18.02.18.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Jul 2016 02:18:27 -0700 (PDT) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: Brian Norris , linux-mtd@lists.infradead.org Subject: [PATCH mtd-utils] nanddump: check write function result for errors Date: Mon, 18 Jul 2016 11:18:13 +0200 Message-Id: <1468833493-28311-1-git-send-email-zajec5@gmail.com> X-Mailer: git-send-email 1.8.4.5 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160718_021852_355714_EE4AFCFB X-CRM114-Status: GOOD ( 12.91 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:4010:c07:0:0:0:242 listed in] [list.dnswl.org] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (zajec5[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (zajec5[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 --- 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 */