From ec443ce35ef6912b5cb5acdac8e4e88b49f8139e Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 17 Dec 2022 12:26:52 +0000 Subject: [PATCH 1/1] util/ifdtool: print errno message on disk i/o fail Ifdtool was re-inventing its own error messages, for failures pertaining to I/O on the disk. The errm() function outputs the errno message, if set, and the functions used by ifdtool for file reads/writes would set errno accordingly. An example of the effect this has: ./ifdtool -x foo.bin if `foo.bin` doesn't exist, it will output on my test system the following message: ./ifdtool: foo.bin: No such file or directory The benefit of this change is that the user's own system would output the correct error message, and in their own language (if the libc has translated error messages). Signed-off-by: Leah Rowe Change-Id: I154da44ccc3d3589394aa6b69e61e4d86825f7bd --- util/ifdtool/ifdtool.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/util/ifdtool/ifdtool.c b/util/ifdtool/ifdtool.c index eda389de84..7e3053df77 100644 --- a/util/ifdtool/ifdtool.c +++ b/util/ifdtool/ifdtool.c @@ -736,10 +736,10 @@ cmd_extract_regions(char *image, int size) O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (region_fd < 0) - errm("Error while trying to open file\n"); + errm("%s: ", region_filename); if (write(region_fd, image + region.base, region.size) != region.size) - errm("Error while writing\n"); + errm("%s: ", region_filename); close(region_fd); } @@ -763,9 +763,9 @@ cmd_inject_region(const char *new_filename, char *image, int size, region_fd = open(region_fname, O_RDONLY | O_BINARY); if (region_fd == -1) - errm("Could not open file\n"); + errm("%s: ", region_fname); if (fstat(region_fd, &buf) == -1) - errm("Could not stat file\n"); + errm("%s: ", region_fname); region_size = buf.st_size; printf("File %s is %d bytes\n", region_fname, region_size); @@ -793,7 +793,7 @@ cmd_inject_region(const char *new_filename, char *image, int size, if (read(region_fd, image + region.base + offset, region_size) != region_size) - errm("Could not read file\n"); + errm("%s: ", region_fname); close(region_fd); @@ -830,9 +830,8 @@ cmd_new_layout(const char *new_filename, char *image, int size, /* read new layout */ romlayout = fopen(layout_fname, "r"); - if (!romlayout) - errm("Could not read layout file.\n"); + errm("%s: ", layout_fname); while (!feof(romlayout)) { if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, @@ -1227,9 +1226,9 @@ read_image(const char *romname, char *rombuf) int romsize, romfd; if ((romfd = open(romname, O_RDONLY | O_BINARY)) == -1) - errm("Could not open file\n"); + errm("%s: ", romname); else if (fstat(romfd, &buf) == -1) - errm("Could not stat file\n"); + errm("%s: ", romname); else romsize = buf.st_size; @@ -1238,7 +1237,7 @@ read_image(const char *romname, char *rombuf) if ((rombuf = malloc(romsize)) == NULL) err(1, NULL); else if (read(romfd, rombuf, romsize) != romsize) - errm("Could not read file\n"); + errm("%s: ", romname); else close(romfd); @@ -1256,9 +1255,9 @@ write_image(const char *filename, char *image, int size) O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (new_fd < 0) - errm("Error while trying to open file\n"); + errm("%s: ", filename); if (write(new_fd, image, size) != size) - errm("Error while writing\n"); + errm("%s: ", filename); close(new_fd); } @@ -1272,7 +1271,7 @@ dump_frba_layout(const frba_t *frba, const char *layout_fname) int layout_fd = open(layout_fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (layout_fd == -1) - errm("Could not open file\n"); + errm("%s: ", layout_fname); for (i = 0; i < max_regions; i++) { region_t region = get_region(frba, i); @@ -1282,7 +1281,7 @@ dump_frba_layout(const frba_t *frba, const char *layout_fname) dump_region_layout(buf, bufsize, i, frba); if (write(layout_fd, buf, strlen(buf)) < 0) - errm("Could not write to file\n"); + errm("%s: ", layout_fname); } close(layout_fd); printf("Wrote layout to %s\n", layout_fname); -- 2.25.1