[PATCH 3/3] lib: open_and_lseek(): move error messages to callers

Sascha Hauer s.hauer at pengutronix.de
Wed Aug 16 02:39:02 PDT 2023


For some cases like in common/filetype.c the caller already prints an
error message, so to avoid duplicated error messages leave it up to
the caller to print an error. This also adds error messages to all
callers where necessary.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 arch/mips/mach-ath79/art.c |  3 +--
 commands/loadb.c           |  2 +-
 commands/loadxy.c          |  2 +-
 commands/md.c              |  4 +++-
 commands/memset.c          |  4 +++-
 commands/mm.c              |  4 +++-
 commands/mw.c              |  4 +++-
 common/ratp/md.c           |  4 +++-
 common/ratp/mw.c           |  1 +
 lib/libfile.c              | 13 +++----------
 lib/misc.c                 |  5 ++++-
 11 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/arch/mips/mach-ath79/art.c b/arch/mips/mach-ath79/art.c
index 585cccc5af..bdc14bee51 100644
--- a/arch/mips/mach-ath79/art.c
+++ b/arch/mips/mach-ath79/art.c
@@ -43,8 +43,7 @@ static int art_read_mac(struct device *dev, const char *file)
 
 	fd = open_and_lseek(file, O_RDONLY, AR93000_EPPROM_OFFSET);
 	if (fd < 0) {
-		dev_err(dev, "Failed to open eeprom path %s %d\n",
-		       file, -errno);
+		dev_err(dev, "Failed to open eeprom path \"%s\": %m\n", file);
 		return -errno;
 	}
 
diff --git a/commands/loadb.c b/commands/loadb.c
index 7ab989f459..140d3743f6 100644
--- a/commands/loadb.c
+++ b/commands/loadb.c
@@ -646,7 +646,7 @@ static int do_load_serial_bin(int argc, char *argv[])
 	/* File should exist */
 	ofd = open_and_lseek(output_file, O_WRONLY | O_CREAT, offset);
 	if (ofd < 0) {
-		perror(argv[0]);
+		printf("Could not open \"%s\": %m\n", output_file);
 		return 3;
 	}
 
diff --git a/commands/loadxy.c b/commands/loadxy.c
index 66daa117d9..e2d1a11a2c 100644
--- a/commands/loadxy.c
+++ b/commands/loadxy.c
@@ -165,7 +165,7 @@ static int do_loadx(int argc, char *argv[])
 	/* File should exist */
 	ofd = open_and_lseek(output_file, O_WRONLY | O_CREAT, offset);
 	if (ofd < 0) {
-		perror(argv[0]);
+		printf("Could not open \"%s\": %m\n", output_file);
 		return 3;
 	}
 
diff --git a/commands/md.c b/commands/md.c
index 7a96634e27..f3758f571f 100644
--- a/commands/md.c
+++ b/commands/md.c
@@ -49,8 +49,10 @@ static int do_mem_md(int argc, char *argv[])
 	}
 
 	fd = open_and_lseek(filename, mode | O_RDONLY, start);
-	if (fd < 0)
+	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", filename);
 		return 1;
+	}
 
 	map = memmap(fd, PROT_READ);
 	if (map != MAP_FAILED) {
diff --git a/commands/memset.c b/commands/memset.c
index e4412533f1..1139691f2f 100644
--- a/commands/memset.c
+++ b/commands/memset.c
@@ -41,8 +41,10 @@ static int do_memset(int argc, char *argv[])
 	n = strtoull_suffix(argv[optind + 2], NULL, 0);
 
 	fd = open_and_lseek(file, mode | O_WRONLY | O_CREAT, s);
-	if (fd < 0)
+	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", file);
 		return 1;
+	}
 
 	buf = xmalloc(RW_BUF_SIZE);
 	memset(buf, c, RW_BUF_SIZE);
diff --git a/commands/mm.c b/commands/mm.c
index 8fe87a80a1..8755a0f2c9 100644
--- a/commands/mm.c
+++ b/commands/mm.c
@@ -40,8 +40,10 @@ static int do_mem_mm(int argc, char *argv[])
 	mask = simple_strtoull(argv[optind++], NULL, 0);
 
 	fd = open_and_lseek(filename, mode | O_RDWR | O_CREAT, adr);
-	if (fd < 0)
+	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", filename);
 		return 1;
+	}
 
 	switch (mode) {
 	case O_RWSIZE_1:
diff --git a/commands/mw.c b/commands/mw.c
index 5dcef7e2fc..915f549216 100644
--- a/commands/mw.c
+++ b/commands/mw.c
@@ -39,8 +39,10 @@ static int do_mem_mw(int argc, char *argv[])
 	adr = strtoull_suffix(argv[optind++], NULL, 0);
 
 	fd = open_and_lseek(filename, mode | O_WRONLY | O_CREAT, adr);
-	if (fd < 0)
+	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", filename);
 		return 1;
+	}
 
 	while (optind < argc) {
 		u8 val8;
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 3e258c59a0..8221afaebc 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -68,8 +68,10 @@ static int do_ratp_mem_md(const char *filename,
 	char *buf = NULL;
 
 	fd = open_and_lseek(filename, O_RWSIZE_1 | O_RDONLY, start);
-	if (fd < 0)
+	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", filename);
 		return -errno;
+	}
 
 	map = memmap(fd, PROT_READ);
 	if (map != MAP_FAILED) {
diff --git a/common/ratp/mw.c b/common/ratp/mw.c
index 8945799f1d..87dc8cb95c 100644
--- a/common/ratp/mw.c
+++ b/common/ratp/mw.c
@@ -133,6 +133,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
 
 	fd = open_and_lseek(path, O_RWSIZE_1 | O_WRONLY, addr);
 	if (fd < 0) {
+		printf("Could not open \"%s\": %m\n", path);
 		ret = -errno;
 		goto out;
 	}
diff --git a/lib/libfile.c b/lib/libfile.c
index c7ea4e497f..a8654e6deb 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -596,10 +596,8 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 	int fd;
 
 	fd = open(filename, mode);
-	if (fd < 0) {
-		perror("open");
+	if (fd < 0)
 		return fd;
-	}
 
 	if (!pos)
 		return fd;
@@ -608,21 +606,16 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 		struct stat s;
 
 		ret = fstat(fd, &s);
-		if (ret < 0) {
-			perror("fstat");
+		if (ret < 0)
 			goto out;
-		}
 
 		if (s.st_size < pos) {
 			ret = ftruncate(fd, pos));
-			if (ret) {
-				perror("ftruncate");
+			if (ret)
 				goto out;
-			}
 	}
 
 	if (lseek(fd, pos, SEEK_SET) != pos) {
-		perror("lseek");
 		ret = -errno;
 		goto out;
 	}
diff --git a/lib/misc.c b/lib/misc.c
index 2d5f7c1985..1cb2a6b9b5 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -209,11 +209,14 @@ int memcpy_parse_options(int argc, char *argv[], int *sourcefd,
 	destfile = destfile ?: "/dev/mem";
 
 	*sourcefd = open_and_lseek(sourcefile, mode | O_RDONLY, src);
-	if (*sourcefd < 0)
+	if (*sourcefd < 0) {
+		printf("Could not open source file \"%s\": %m\n", sourcefile);
 		return -1;
+	}
 
 	*destfd = open_and_lseek(destfile, mode | destmode, dest);
 	if (*destfd < 0) {
+		printf("Could not open destination file \"%s\": %m\n", destfile);
 		close(*sourcefd);
 		return -1;
 	}
-- 
2.39.2




More information about the barebox mailing list