[PATCH 1/2] sparse_image_fd: add function to re-use a file descriptor

Juergen Borleis jbe at pengutronix.de
Thu Sep 14 02:58:58 PDT 2023


Signed-off-by: Juergen Borleis <jbe at pengutronix.de>
---
 include/image-sparse.h |  1 +
 lib/image-sparse.c     | 17 +++++++++++------
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/include/image-sparse.h b/include/image-sparse.h
index 6bff844..d969ca1 100644
--- a/include/image-sparse.h
+++ b/include/image-sparse.h
@@ -59,6 +59,7 @@ static inline int is_sparse_image(const void *buf)
 struct sparse_image_ctx;
 
 struct sparse_image_ctx *sparse_image_open(const char *path);
+struct sparse_image_ctx *sparse_image_fd(int infd);
 int sparse_image_read(struct sparse_image_ctx *si, void *buf,
 		      loff_t *pos, size_t len, size_t *retlen);
 void sparse_image_close(struct sparse_image_ctx *si);
diff --git a/lib/image-sparse.c b/lib/image-sparse.c
index eb5242e..b686d25 100644
--- a/lib/image-sparse.c
+++ b/lib/image-sparse.c
@@ -146,7 +146,7 @@ loff_t sparse_image_size(struct sparse_image_ctx *si)
 	return (loff_t)si->sparse.blk_sz * si->sparse.total_blks;
 }
 
-struct sparse_image_ctx *sparse_image_open(const char *path)
+struct sparse_image_ctx *sparse_image_fd(int infd)
 {
 	struct sparse_image_ctx *si;
 	loff_t offs;
@@ -154,11 +154,7 @@ struct sparse_image_ctx *sparse_image_open(const char *path)
 
 	si = xzalloc(sizeof(*si));
 
-	si->fd = open(path, O_RDONLY);
-	if (si->fd < 0) {
-		ret = -errno;
-		goto out;
-	}
+	si->fd = infd;
 
 	/* Read and skip over sparse image header */
 	read(si->fd, &si->sparse, sizeof(struct sparse_header));
@@ -186,6 +182,15 @@ struct sparse_image_ctx *sparse_image_open(const char *path)
 	return ERR_PTR(ret);
 }
 
+struct sparse_image_ctx *sparse_image_open(const char *path)
+{
+	int fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return ERR_PTR(-errno);
+
+	return sparse_image_fd(fd);
+}
+
 int sparse_image_read(struct sparse_image_ctx *si, void *buf, loff_t *pos,
 		      size_t len, size_t *retlen)
 {
-- 
2.30.2




More information about the barebox mailing list