mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/3] lib: open_and_lseek(): return error code on error
Date: Wed, 16 Aug 2023 11:39:01 +0200	[thread overview]
Message-ID: <20230816093902.2537934-2-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20230816093902.2537934-1-s.hauer@pengutronix.de>

Some callers of open_and_lseek() expect an error code instead of -1
as return value, so consistently return an error code.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/libfile.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index ebd1de3d8e..c7ea4e497f 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -589,7 +589,7 @@ int compare_file(const char *f1, const char *f2)
  * @pos:	The position to lseek to
  *
  * Return: If successful this function returns a positive
- *         filedescriptor number, otherwise -1 is returned
+ *         filedescriptor number, otherwise a negative error code is returned
  */
 int open_and_lseek(const char *filename, int mode, loff_t pos)
 {
@@ -607,26 +607,30 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 	if (mode & (O_WRONLY | O_RDWR)) {
 		struct stat s;
 
-		if (fstat(fd, &s)) {
+		ret = fstat(fd, &s);
+		if (ret < 0) {
 			perror("fstat");
 			goto out;
 		}
 
-		if (s.st_size < pos && ftruncate(fd, pos)) {
-			perror("ftruncate");
-			goto out;
-		}
+		if (s.st_size < pos) {
+			ret = ftruncate(fd, pos));
+			if (ret) {
+				perror("ftruncate");
+				goto out;
+			}
 	}
 
 	if (lseek(fd, pos, SEEK_SET) != pos) {
 		perror("lseek");
+		ret = -errno;
 		goto out;
 	}
 
 	return fd;
 out:
 	close(fd);
-	return -1;
+	return ret;
 }
 
 /**
-- 
2.39.2




  reply	other threads:[~2023-08-16  9:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-16  9:39 [PATCH 1/3] filetype: return error and pass filetype as pointer argument Sascha Hauer
2023-08-16  9:39 ` Sascha Hauer [this message]
2023-08-16  9:39 ` [PATCH 3/3] lib: open_and_lseek(): move error messages to callers Sascha Hauer

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=20230816093902.2537934-2-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@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