mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] scripts: imx-image: Be more informative on open errors
@ 2019-02-05  8:59 Sascha Hauer
  2019-02-05  8:59 ` [PATCH 2/2] scripts: imx-image: make read_file behaviour consistent Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2019-02-05  8:59 UTC (permalink / raw)
  To: Barebox List

When opening a file fails then consistently print a message which
file we cannot open and why. In one place no error checking was done at
all, so add this.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 34a072039d..4933703fdc 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -153,7 +153,8 @@ static int extract_key(const char *certfile, uint8_t **modulus, int *modulus_len
 
 	fp = fopen(certfile, "r");
 	if (!fp) {
-		fprintf(stderr, "unable to open certfile: %s\n", certfile);
+		fprintf(stderr, "unable to open certfile %s: %s\n", certfile,
+			strerror(errno));
 		return -errno;
 	}
 
@@ -458,7 +459,8 @@ static void write_dcd(const char *outfile)
 
 	outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (outfd < 0) {
-		perror("open");
+		fprintf(stderr, "Cannot open %s for wrinting: %s\n", outfile,
+			strerror(errno));
 		exit(1);
 	}
 
@@ -671,6 +673,11 @@ static int hab_sign(struct config_data *data)
 	}
 
 	outfd = open(data->outfile, O_WRONLY | O_APPEND);
+	if (outfd < 0) {
+		fprintf(stderr, "Cannot open %s for writing: %s\n", data->outfile,
+			strerror(errno));
+		exit(1);
+	}
 
 	ret = xwrite(outfd, buf, csf_space);
 	if (ret < 0) {
@@ -695,7 +702,7 @@ static void *read_file(const char *filename, size_t *size)
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0) {
-		perror("open");
+		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
 		exit(1);
 	}
 
@@ -930,7 +937,8 @@ int main(int argc, char *argv[])
 
 	outfd = open(data.outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (outfd < 0) {
-		perror("open");
+		fprintf(stderr, "Cannot open %s for writing: %s\n", data.outfile,
+			strerror(errno));
 		exit(1);
 	}
 
-- 
2.20.1


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] scripts: imx-image: make read_file behaviour consistent
  2019-02-05  8:59 [PATCH 1/2] scripts: imx-image: Be more informative on open errors Sascha Hauer
@ 2019-02-05  8:59 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2019-02-05  8:59 UTC (permalink / raw)
  To: Barebox List

When read_file fails then it exits the program in most cases. Let's exit
the program when stat() fails aswell, so that we *always* exit the
program on failure. Drop all error handling for read_file since that's
no longer necessary and rename the function to xread_file to make
clear it won't fail.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 scripts/imx/imx-image.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 4933703fdc..a7f1421fa3 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -694,7 +694,7 @@ static int hab_sign(struct config_data *data)
 	return 0;
 }
 
-static void *read_file(const char *filename, size_t *size)
+static void *xread_file(const char *filename, size_t *size)
 {
 	int fd, ret;
 	void *buf;
@@ -707,13 +707,17 @@ static void *read_file(const char *filename, size_t *size)
 	}
 
 	ret = fstat(fd, &s);
-	if (ret)
-		return NULL;
+	if (ret) {
+		fprintf(stderr, "Cannot stat %s: %s\n", filename, strerror(errno));
+		exit(1);
+	}
 
 	*size = s.st_size;
 	buf = malloc(*size);
-	if (!buf)
+	if (!buf) {
+		perror("malloc");
 		exit(1);
+	}
 
 	xread(fd, buf, *size);
 
@@ -884,12 +888,8 @@ int main(int argc, char *argv[])
 
 		if (data.signed_hdmi_firmware_file) {
 			free(buf);
-			buf = read_file(data.signed_hdmi_firmware_file,
+			buf = xread_file(data.signed_hdmi_firmware_file,
 					&signed_hdmi_firmware_size);
-			if (!buf) {
-				perror("read_file");
-				exit(1);
-			}
 
 			signed_hdmi_firmware_size =
 				roundup(signed_hdmi_firmware_size,
@@ -931,9 +931,7 @@ int main(int argc, char *argv[])
 	bb_header[0] = data.first_opcode;
 	bb_header[ARM_HEAD_SIZE_INDEX] = barebox_image_size;
 
-	infile = read_file(imagename, &insize);
-	if (!infile)
-		exit(1);
+	infile = xread_file(imagename, &insize);
 
 	outfd = open(data.outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
 	if (outfd < 0) {
@@ -998,7 +996,7 @@ int main(int argc, char *argv[])
 	if (create_usb_image) {
 		uint32_t *dcd;
 
-		infile = read_file(data.outfile, &insize);
+		infile = xread_file(data.outfile, &insize);
 
 		dcd = infile + dcd_ptr_offset;
 		*dcd = dcd_ptr_content;
-- 
2.20.1


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-02-05  8:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-05  8:59 [PATCH 1/2] scripts: imx-image: Be more informative on open errors Sascha Hauer
2019-02-05  8:59 ` [PATCH 2/2] scripts: imx-image: make read_file behaviour consistent Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox