Skip to contentSkip to conversation

sparse_image_fd: add function to re-use a file descriptor

PATCH In review Juergen Borleis 3 participants 5 messages Sep 14, 2023 – Sep 21, 2023 Library code

lore
Share & tools
lore
#0 lore raw

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

---
 include/image-sparse.h |  1 +
 lib/image-sparse.c     | 17 +++++++++++------
 2 files changed, 12 insertions(+), 6 deletions(-)
include/image-sparse.h +1 -0
index 6bff844..d969ca1 100644
@@ -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);
lib/image-sparse.c +11 -6
index eb5242e..b686d25 100644
@@ -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)
 {
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 uncompress: add Android sparse image support

An Android sparse image file generated by the 'genimage' tool can be seen as a kind of compression, since it contains only the data which should really be written to a device.

This implementation writes only the intended content and skips anything else.

---
 lib/uncompress.c | 82 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)
lib/uncompress.c +78 -4
index 0608e9f..f17e9ce 100644
@@ -25,6 +25,10 @@
 #include <malloc.h>
 #include <fs.h>
 #include <libfile.h>
+#ifdef CONFIG_IMAGE_SPARSE
+#include <image-sparse.h>
+#include <linux/sizes.h>
+#endif
 
 static void *uncompress_buf;
 static unsigned int uncompress_size;
@@ -60,6 +64,71 @@ static int uncompress_fill(void *buf, unsigned int len)
 	return total;
 }
 
+#ifdef CONFIG_IMAGE_SPARSE
+static int uncompress_sparse(int source, int destination)
+{
+	int ret = 0;
+	struct sparse_image_ctx *sparse;
+
+	/* rewind after checking the file type */
+	lseek(source, 0, SEEK_SET);
+
+	sparse = sparse_image_fd(source);
+	if (IS_ERR(sparse)) {
+		pr_err("Failed to open or interpret sparse image file: %m\n");
+		ret = 1;
+		goto on_error_dev;
+	}
+
+	const size_t transfer_buffer_length = SZ_128K;
+	unsigned char *buf = malloc(transfer_buffer_length);
+	if (!buf) {
+		pr_err("Failed to alloc memory for the transfers\n");
+		ret = 1;
+		goto on_error_sparse;
+	}
+
+	while (1) {
+		loff_t write_offset;
+		size_t write_length;
+
+		int rc = sparse_image_read(sparse, buf, &write_offset, transfer_buffer_length, &write_length);
+		if (rc) {
+			ret = 1;
+			goto on_error_memory;
+		}
+		if (!write_length)
+			break;
+
+		discard_range(destination, write_length, write_offset);
+
+		write_offset = lseek(destination, write_offset, SEEK_SET);
+		if (write_offset == -1) {
+			pr_err("Failed to set next data's destination offset: %m\n");
+			ret = 1;
+			goto on_error_memory;
+
+		}
+
+		rc = write_full(destination, buf, write_length);
+		if (rc < 0) {
+			pr_err("Failed to write destination's next data: %m\n");
+			ret = 1;
+			goto on_error_memory;
+		}
+	}
+
+on_error_memory:
+	free(buf);
+on_error_sparse:
+	free(sparse); /* Note: sparse_image_close(sparse); would also close the input file descriptor */
+on_error_dev:
+	return ret;
+}
+#endif
+
+static int uncompress_infd, uncompress_outfd;
+
 int uncompress(unsigned char *inbuf, int len,
 	   int(*fill)(void*, unsigned int),
 	   int(*flush)(void*, unsigned int),
@@ -121,6 +190,10 @@ int uncompress(unsigned char *inbuf, int len,
 	case filetype_xz_compressed:
 		compfn = decompress_unxz;
 		break;
+#endif
+#ifdef CONFIG_IMAGE_SPARSE
+	case filetype_android_sparse:
+		break;
 #endif
 	default:
 		err = basprintf("cannot handle filetype %s",
@@ -131,16 +204,17 @@ int uncompress(unsigned char *inbuf, int len,
 		goto err;
 	}
 
-	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
-			flush, output, pos, error_fn);
+	if (ft == filetype_android_sparse)
+		ret = uncompress_sparse(uncompress_infd, uncompress_outfd);
+	else
+		ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
+			     flush, output, pos, error_fn);
 err:
 	free(uncompress_buf);
 
 	return ret;
 }
 
-static int uncompress_infd, uncompress_outfd;
-
 static int fill_fd(void *buf, unsigned int len)
 {
 	return read_full(uncompress_infd, buf, len);
Signature · 1 line
2.30.2
Tested-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] uncompress: add Android sparse image support

Quoted 7 lines · Juergen Borleis

An Android sparse image file generated by the 'genimage' tool can be seen as a kind of compression, since it contains only the data which should really be written to a device.

This implementation writes only the intended content and skips anything else.

while this obviously works, I still have a comment below:

Quoted 103 lines
---
 lib/uncompress.c | 82 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)
lib/uncompress.c +73 -0
index 0608e9f..f17e9ce 100644
@@ -25,6 +25,10 @@
 #include <malloc.h>
 #include <fs.h>
 #include <libfile.h>
+#ifdef CONFIG_IMAGE_SPARSE
+#include <image-sparse.h>
+#include <linux/sizes.h>
+#endif
 
 static void *uncompress_buf;
 static unsigned int uncompress_size;
@@ -60,6 +64,71 @@ static int uncompress_fill(void *buf, unsigned int len)
 	return total;
 }
 
+#ifdef CONFIG_IMAGE_SPARSE
+static int uncompress_sparse(int source, int destination)
+{
+	int ret = 0;
+	struct sparse_image_ctx *sparse;
+
+	/* rewind after checking the file type */
+	lseek(source, 0, SEEK_SET);
+
+	sparse = sparse_image_fd(source);
+	if (IS_ERR(sparse)) {
+		pr_err("Failed to open or interpret sparse image file: %m\n");
+		ret = 1;
+		goto on_error_dev;
+	}
+
+	const size_t transfer_buffer_length = SZ_128K;
+	unsigned char *buf = malloc(transfer_buffer_length);
+	if (!buf) {
+		pr_err("Failed to alloc memory for the transfers\n");
+		ret = 1;
+		goto on_error_sparse;
+	}
+
+	while (1) {
+		loff_t write_offset;
+		size_t write_length;
+
+		int rc = sparse_image_read(sparse, buf, &write_offset, transfer_buffer_length, &write_length);
+		if (rc) {
+			ret = 1;
+			goto on_error_memory;
+		}
+		if (!write_length)
+			break;
+
+		discard_range(destination, write_length, write_offset);
+
+		write_offset = lseek(destination, write_offset, SEEK_SET);
+		if (write_offset == -1) {
+			pr_err("Failed to set next data's destination offset: %m\n");
+			ret = 1;
+			goto on_error_memory;
+
+		}
+
+		rc = write_full(destination, buf, write_length);
+		if (rc < 0) {
+			pr_err("Failed to write destination's next data: %m\n");
+			ret = 1;
+			goto on_error_memory;
+		}
+	}
+
+on_error_memory:
+	free(buf);
+on_error_sparse:
+	free(sparse); /* Note: sparse_image_close(sparse); would also close the input file descriptor */
+on_error_dev:
+	return ret;
+}
+#endif
+
+static int uncompress_infd, uncompress_outfd;
+
 int uncompress(unsigned char *inbuf, int len,
 	   int(*fill)(void*, unsigned int),
 	   int(*flush)(void*, unsigned int),
@@ -121,6 +190,10 @@ int uncompress(unsigned char *inbuf, int len,
 	case filetype_xz_compressed:
 		compfn = decompress_unxz;
 		break;
+#endif
+#ifdef CONFIG_IMAGE_SPARSE
+	case filetype_android_sparse:
+		break;
 #endif
 	default:
 		err = basprintf("cannot handle filetype %s",

lib/uncompress.c int uncompress(unsigned char *inbuf, int len, · line 211

Quoted 10 lines
lib/uncompress.c +5 -2
@@ -131,16 +204,17 @@ int uncompress(unsigned char *inbuf, int len,
 		goto err;
 	}
 
-	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
-			flush, output, pos, error_fn);
+	if (ft == filetype_android_sparse)
+		ret = uncompress_sparse(uncompress_infd, uncompress_outfd);
+	else
+		ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
+			     flush, output, pos, error_fn);

Wouldn't it be more natural to make uncompress_sparse use the same prototype as the other uncompress functions and then just have:

#ifdef CONFIG_IMAGE_SPARSE
	case filetype_android_sparse:
		compfn = decompress_sparse;
		break;

#endif

Without the need to adapt the compfn call?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#3 lore raw

Re: [PATCH 2/2] uncompress: add Android sparse image support

Quoted 130 lines · Uwe Kleine-König
Quoted 7 lines · Juergen Borleis

An Android sparse image file generated by the 'genimage' tool can be seen as a kind of compression, since it contains only the data which should really be written to a device.

This implementation writes only the intended content and skips anything else.

while this obviously works, I still have a comment below:

Quoted 103 lines
---
 lib/uncompress.c | 82 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)
lib/uncompress.c +73 -0
index 0608e9f..f17e9ce 100644
@@ -25,6 +25,10 @@
 #include <malloc.h>
 #include <fs.h>
 #include <libfile.h>
+#ifdef CONFIG_IMAGE_SPARSE
+#include <image-sparse.h>
+#include <linux/sizes.h>
+#endif
 
 static void *uncompress_buf;
 static unsigned int uncompress_size;
@@ -60,6 +64,71 @@ static int uncompress_fill(void *buf, unsigned int len)
 	return total;
 }
 
+#ifdef CONFIG_IMAGE_SPARSE
+static int uncompress_sparse(int source, int destination)
+{
+	int ret = 0;
+	struct sparse_image_ctx *sparse;
+
+	/* rewind after checking the file type */
+	lseek(source, 0, SEEK_SET);
+
+	sparse = sparse_image_fd(source);
+	if (IS_ERR(sparse)) {
+		pr_err("Failed to open or interpret sparse image file: %m\n");
+		ret = 1;
+		goto on_error_dev;
+	}
+
+	const size_t transfer_buffer_length = SZ_128K;
+	unsigned char *buf = malloc(transfer_buffer_length);
+	if (!buf) {
+		pr_err("Failed to alloc memory for the transfers\n");
+		ret = 1;
+		goto on_error_sparse;
+	}
+
+	while (1) {
+		loff_t write_offset;
+		size_t write_length;
+
+		int rc = sparse_image_read(sparse, buf, &write_offset, transfer_buffer_length, &write_length);
+		if (rc) {
+			ret = 1;
+			goto on_error_memory;
+		}
+		if (!write_length)
+			break;
+
+		discard_range(destination, write_length, write_offset);
+
+		write_offset = lseek(destination, write_offset, SEEK_SET);
+		if (write_offset == -1) {
+			pr_err("Failed to set next data's destination offset: %m\n");
+			ret = 1;
+			goto on_error_memory;
+
+		}
+
+		rc = write_full(destination, buf, write_length);
+		if (rc < 0) {
+			pr_err("Failed to write destination's next data: %m\n");
+			ret = 1;
+			goto on_error_memory;
+		}
+	}
+
+on_error_memory:
+	free(buf);
+on_error_sparse:
+	free(sparse); /* Note: sparse_image_close(sparse); would also close the input file descriptor */
+on_error_dev:
+	return ret;
+}
+#endif
+
+static int uncompress_infd, uncompress_outfd;
+
 int uncompress(unsigned char *inbuf, int len,
 	   int(*fill)(void*, unsigned int),
 	   int(*flush)(void*, unsigned int),
@@ -121,6 +190,10 @@ int uncompress(unsigned char *inbuf, int len,
 	case filetype_xz_compressed:
 		compfn = decompress_unxz;
 		break;
+#endif
+#ifdef CONFIG_IMAGE_SPARSE
+	case filetype_android_sparse:
+		break;
 #endif
 	default:
 		err = basprintf("cannot handle filetype %s",

lib/uncompress.c int uncompress(unsigned char *inbuf, int len, · line 211

Quoted 10 lines
lib/uncompress.c +5 -2
@@ -131,16 +204,17 @@ int uncompress(unsigned char *inbuf, int len,
 		goto err;
 	}
 
-	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
-			flush, output, pos, error_fn);
+	if (ft == filetype_android_sparse)
+		ret = uncompress_sparse(uncompress_infd, uncompress_outfd);
+	else
+		ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
+			     flush, output, pos, error_fn);

Wouldn't it be more natural to make uncompress_sparse use the same prototype as the other uncompress functions and then just have:

Quoted 7 lines
#ifdef CONFIG_IMAGE_SPARSE
	case filetype_android_sparse:
		compfn = decompress_sparse;
		break;

#endif

Without the need to adapt the compfn call?

compfn() doesn't have access to the file descriptors which the current sparse implementation needs.

It would be great though to let our sparse image support use an input buffer and a fill() function.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH 2/2] uncompress: add Android sparse image support

In reply to Juergen Borleis
Quoted 27 lines · Juergen Borleis

An Android sparse image file generated by the 'genimage' tool can be seen as a kind of compression, since it contains only the data which should really be written to a device.

This implementation writes only the intended content and skips anything else.

---
 lib/uncompress.c | 82 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)
lib/uncompress.c +4 -0
index 0608e9f..f17e9ce 100644
@@ -25,6 +25,10 @@
 #include <malloc.h>
 #include <fs.h>
 #include <libfile.h>
+#ifdef CONFIG_IMAGE_SPARSE
+#include <image-sparse.h>
+#include <linux/sizes.h>
+#endif
 
 static void *uncompress_buf;
 static unsigned int uncompress_size;

lib/uncompress.c static int uncompress_fill(void *buf, unsigned int len) · line 72

Quoted 9 lines
lib/uncompress.c +6 -0
@@ -60,6 +64,71 @@ static int uncompress_fill(void *buf, unsigned int len)
 	return total;
 }
 
+#ifdef CONFIG_IMAGE_SPARSE
+static int uncompress_sparse(int source, int destination)
+{
+	int ret = 0;
+	struct sparse_image_ctx *sparse;
+

You could drop all the #ifdefs and add a

	if (!IS_ENABLED(CONFIG_IMAGE_SPARSE)) {
		pr_err("Sparse image support disabled\n");
		return -ENOSYS;
	}

instead.

lib/uncompress.c static int uncompress_fill(void *buf, unsigned int len) · line 74

Quoted 2 lines
lib/uncompress.c +2 -0
+	/* rewind after checking the file type */
+	lseek(source, 0, SEEK_SET);

Why is this necessary? If this is necessary for sparse images it should also be necessary for other compression types as well and should be done by the caller.

lib/uncompress.c static int uncompress_fill(void *buf, unsigned int len) · line 79

Quoted 5 lines
lib/uncompress.c +5 -0
+
+	sparse = sparse_image_fd(source);
+	if (IS_ERR(sparse)) {
+		pr_err("Failed to open or interpret sparse image file: %m\n");
+		ret = 1;

'1' is not a valid error code. PTR_ERR(sparse) would be suitable here.

lib/uncompress.c static int uncompress_fill(void *buf, unsigned int len) · line 86

Quoted 7 lines
lib/uncompress.c +7 -0
+		goto on_error_dev;
+	}
+
+	const size_t transfer_buffer_length = SZ_128K;
+	unsigned char *buf = malloc(transfer_buffer_length);
+	if (!buf) {
+		pr_err("Failed to alloc memory for the transfers\n");

No need to print. just return -ENOMEM.

Please return valid error codes in the rest of this function as well.

Quoted 60 lines
lib/uncompress.c +49 -0
+		ret = 1;
+		goto on_error_sparse;
+	}
+
+	while (1) {
+		loff_t write_offset;
+		size_t write_length;
+
+		int rc = sparse_image_read(sparse, buf, &write_offset, transfer_buffer_length, &write_length);
+		if (rc) {
+			ret = 1;
+			goto on_error_memory;
+		}
+		if (!write_length)
+			break;
+
+		discard_range(destination, write_length, write_offset);
+
+		write_offset = lseek(destination, write_offset, SEEK_SET);
+		if (write_offset == -1) {
+			pr_err("Failed to set next data's destination offset: %m\n");
+			ret = 1;
+			goto on_error_memory;
+
+		}
+
+		rc = write_full(destination, buf, write_length);
+		if (rc < 0) {
+			pr_err("Failed to write destination's next data: %m\n");
+			ret = 1;
+			goto on_error_memory;
+		}
+	}
+
+on_error_memory:
+	free(buf);
+on_error_sparse:
+	free(sparse); /* Note: sparse_image_close(sparse); would also close the input file descriptor */
+on_error_dev:
+	return ret;
+}
+#endif
+
+static int uncompress_infd, uncompress_outfd;
+
 int uncompress(unsigned char *inbuf, int len,
 	   int(*fill)(void*, unsigned int),
 	   int(*flush)(void*, unsigned int),
@@ -121,6 +190,10 @@ int uncompress(unsigned char *inbuf, int len,
 	case filetype_xz_compressed:
 		compfn = decompress_unxz;
 		break;
+#endif
+#ifdef CONFIG_IMAGE_SPARSE
+	case filetype_android_sparse:
+		break;
 #endif
 	default:
 		err = basprintf("cannot handle filetype %s",

lib/uncompress.c int uncompress(unsigned char *inbuf, int len, · line 208

Quoted 7 lines
lib/uncompress.c +2 -2
@@ -131,16 +204,17 @@ int uncompress(unsigned char *inbuf, int len,
 		goto err;
 	}
 
-	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
-			flush, output, pos, error_fn);
+	if (ft == filetype_android_sparse)
+		ret = uncompress_sparse(uncompress_infd, uncompress_outfd);

This assumes both uncompress_infd and uncompress_outfd are valid which is the case for uncompress_fd_to_fd() only, but not for uncompress_buf_to_fd() or uncompress_buf_to_buf(). I think this limitation is okay for sparse image support, but the other cases should return an error instead of silently going crazy on invalid file descriptors.

BTW at least sparse image support for uncompress_fd_to_buf() could be implemented without much hassle.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |