Skip to contentSkip to conversation

erofs-utils: tar: support gzip index generation

PATCH Superseded Gao Xiang 1 participant 3 messages Sep 17, 2025 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: tar: support gzip index generation

Let's support AWS SOCI-compatible zinfo version v2 generation.

Since an OCI image layer cannot be randomly accessed, the new gzip index can be used for OCI gzip random access.

Example:

 $ mkfs.erofs --tar=i --gzinfo=foo.zinfo foo.tarmeta.erofs foo.tgz
---
 include/erofs/tar.h  |   7 +-
 lib/Makefile.am      |   2 +
 lib/gzran.c          | 216 +++++++++++++++++++++++++++++++++++++++++++
 lib/liberofs_gzran.h |  21 +++++
 lib/tar.c            |  20 ++++
 mkfs/main.c          |  23 +++++
 6 files changed, 288 insertions(+), 1 deletion(-)
 create mode 100644 lib/gzran.c
 create mode 100644 lib/liberofs_gzran.h
include/erofs/tar.h +6 -1
index 3bd4b15..cdaef31 100644
@@ -24,12 +24,17 @@ struct erofs_pax_header {
 #define EROFS_IOS_DECODER_NONE		0
 #define EROFS_IOS_DECODER_GZIP		1
 #define EROFS_IOS_DECODER_LIBLZMA	2
+#define EROFS_IOS_DECODER_GZRAN		3
 
 struct erofs_iostream_liblzma;
+struct erofs_gzran_builder;
 
 struct erofs_iostream {
 	union {
-		struct erofs_vfile vf;
+		struct {
+			struct erofs_vfile vf;
+			struct erofs_gzran_builder *gb;
+		};
 		void *handler;
 #ifdef HAVE_LIBLZMA
 		struct erofs_iostream_liblzma *lzma;
lib/Makefile.am +2 -0
index daf937c..72aa0e8 100644
@@ -31,6 +31,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_xxhash.h \
+      $(top_srcdir)/lib/liberofs_gzran.h \
       $(top_srcdir)/lib/liberofs_metabox.h \
       $(top_srcdir)/lib/liberofs_nbd.h \
       $(top_srcdir)/lib/liberofs_s3.h
@@ -86,3 +87,4 @@ endif
 liberofs_la_SOURCES += remotes/oci.c
 liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS}
 liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS}
+liberofs_la_SOURCES += gzran.c
lib/gzran.c new file +216 -0

216 lines not on this page · raw message

lib/liberofs_gzran.h new file +21 -0
new file mode 100644
index 0000000..4764506
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+/*
+ * Copyright (C) 2025 Alibaba Cloud
+ */
+#ifndef __EROFS_LIB_LIBEROFS_GZRAN_H
+#define __EROFS_LIB_LIBEROFS_GZRAN_H
+
+#include "erofs/io.h"
+
+#define EROFS_GZRAN_WINSIZE	32768
+
+struct erofs_gzran_builder;
+
+struct erofs_gzran_builder *erofs_gzran_builder_init(struct erofs_vfile *vf,
+						     u32 span_size);
+int erofs_gzran_builder_read(struct erofs_gzran_builder *gb, char *window);
+int erofs_gzran_builder_export_zinfo(struct erofs_gzran_builder *gb,
+				     struct erofs_vfile *zinfo_vf);
+int erofs_gzran_builder_final(struct erofs_gzran_builder *gb);
+
+#endif
lib/tar.c +20 -0
index 687da6c..8d068cb 100644
@@ -17,6 +17,7 @@
 #endif
 #include "liberofs_base64.h"
 #include "liberofs_cache.h"
+#include "liberofs_gzran.h"
 
 /* This file is a tape/volume header.  Ignore it on extraction.  */
 #define GNUTYPE_VOLHDR 'V'
@@ -65,6 +66,9 @@ void erofs_iostream_close(struct erofs_iostream *ios)
 		free(ios->lzma);
 #endif
 		return;
+	} else if (ios->decoder == EROFS_IOS_DECODER_GZRAN) {
+		erofs_gzran_builder_final(ios->gb);
+		return;
 	}
 	erofs_io_close(&ios->vf);
 }
@@ -105,6 +109,14 @@ int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)
 #else
 		return -EOPNOTSUPP;
 #endif
+	} else if (decoder == EROFS_IOS_DECODER_GZRAN) {
+		ios->vf.fd = fd;
+		ios->feof = false;
+		ios->sz = 0;
+		ios->bufsize = EROFS_GZRAN_WINSIZE * 2;
+		ios->gb = erofs_gzran_builder_init(&ios->vf, 4194304);
+		if (IS_ERR(ios->gb))
+			return PTR_ERR(ios->gb);
 	} else {
 		ios->vf.fd = fd;
 		fsz = lseek(fd, 0, SEEK_END);
@@ -204,6 +216,14 @@ int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)
 #else
 			return -EOPNOTSUPP;
 #endif
+		} else if (ios->decoder == EROFS_IOS_DECODER_GZRAN) {
+			ret = erofs_gzran_builder_read(ios->gb, ios->buffer + rabytes);
+			if (ret < 0)
+				return ret;
+			ios->tail += ret;
+			DBG_BUGON(ios->tail > ios->bufsize);
+			if (!ret)
+				ios->feof = true;
 		} else {
 			ret = erofs_io_read(&ios->vf, ios->buffer + rabytes,
 					    ios->bufsize - rabytes);
mkfs/main.c +23 -0
index a8208d4..3cf30c6 100644
@@ -33,6 +33,7 @@
 #include "../lib/liberofs_metabox.h"
 #include "../lib/liberofs_s3.h"
 #include "../lib/liberofs_oci.h"
+#include "../lib/liberofs_gzran.h"
 #include "../lib/compressor.h"
 
 static struct option long_options[] = {
@@ -72,6 +73,7 @@ static struct option long_options[] = {
 #ifdef HAVE_ZLIB
 	{"gzip", no_argument, NULL, 518},
 	{"ungzip", optional_argument, NULL, 518},
+	{"gzinfo", optional_argument, NULL, 535},
 #endif
 #ifdef HAVE_LIBLZMA
 	{"unlzma", optional_argument, NULL, 519},
@@ -233,6 +235,9 @@ static void usage(int argc, char **argv)
 #ifdef HAVE_LIBLZMA
 		" --unxz[=X]            try to filter the tarball stream through xz/lzma/lzip\n"
 		"                       (and optionally dump the raw stream to X together)\n"
+#endif
+#ifdef HAVE_ZLIB
+		" --gzinfo[=X]          generate AWS SOCI-compatible zinfo in order to support random gzip access\n"
 #endif
 		" --vmdk-desc=X         generate a VMDK descriptor file to merge sub-filesystems\n"
 #ifdef EROFS_MT_ENABLED
@@ -298,6 +303,7 @@ static bool valid_fixeduuid;
 static unsigned int dsunit;
 static int tarerofs_decoder;
 static FILE *vmdk_dcf;
+static char *mkfs_aws_zinfo_file;
 
 static int erofs_mkfs_feat_set_legacy_compress(bool en, const char *val,
 					       unsigned int vallen)
@@ -1338,6 +1344,11 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 			source_mode = EROFS_MKFS_SOURCE_OCI;
 			break;
 #endif
+		case 535:
+			if (optarg)
+				mkfs_aws_zinfo_file = strdup(optarg);
+			tarerofs_decoder = EROFS_IOS_DECODER_GZRAN;
+			break;
 		case 'V':
 			version();
 			exit(0);
@@ -1920,6 +1931,18 @@ exit:
 	erofs_rebuild_cleanup();
 	erofs_diskbuf_exit();
 	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
+		if (mkfs_aws_zinfo_file) {
+			struct erofs_vfile vf;
+			int fd;
+
+			fd = open(mkfs_aws_zinfo_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+			if (fd < 0) {
+				err = -errno;
+			} else {
+				vf = (struct erofs_vfile){ .fd = fd };
+				err = erofs_gzran_builder_export_zinfo(erofstar.ios.gb, &vf);
+			}
+		}
 		erofs_iostream_close(&erofstar.ios);
 		if (erofstar.ios.dumpfd >= 0)
 			close(erofstar.ios.dumpfd);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: lib: add gzran virtual file interfaces

Note that support for multiple gzip streams (e.g., stargz/estargz) will be added later.

---
 lib/gzran.c          | 176 +++++++++++++++++++++++++++++++++++++++++++
 lib/liberofs_gzran.h |   2 +
 2 files changed, 178 insertions(+)
lib/gzran.c +176 -0
index ce2759b..6a6fddc 100644
@@ -194,6 +194,176 @@ int erofs_gzran_builder_final(struct erofs_gzran_builder *gb)
 	free(gb);
 	return 0;
 }
+
+struct erofs_gzran_iostream {
+	struct erofs_vfile *vin;
+	struct erofs_gzran_cutpoint *cp;
+	u32 entries;
+	u32 span_size;
+};
+
+static void erofs_gzran_ios_vfclose(struct erofs_vfile *vf)
+{
+	struct erofs_gzran_iostream *ios =
+		(struct erofs_gzran_iostream *)vf->payload;
+	free(ios->cp);
+	free(vf);
+}
+
+static ssize_t erofs_gzran_ios_vfpread(struct erofs_vfile *vf, void *buf, size_t len, u64 offset)
+{
+	struct erofs_gzran_iostream *ios =
+		(struct erofs_gzran_iostream *)vf->payload;
+	struct erofs_gzran_cutpoint *cp = ios->cp;
+	u8 src[1 << 14], discard[EROFS_GZRAN_WINSIZE];
+	unsigned int bits;
+	bool skip = true;
+	u64 inpos;
+	z_stream strm;
+	int ret;
+
+	while (cp < ios->cp + ios->entries - 1 && cp[1].outpos <= offset)
+		++cp;
+
+	strm.zalloc = Z_NULL;
+	strm.zfree = Z_NULL;
+	strm.opaque = Z_NULL;
+	strm.avail_in = 0;
+	strm.next_in = Z_NULL;
+	ret = inflateInit2(&strm, -15);		/* raw inflate */
+	if (ret != Z_OK)
+		return -EFAULT;
+
+	bits = cp->in_bitpos & 7;
+	inpos = (cp->in_bitpos >> 3) - (bits ? 1 : 0);
+	ret = erofs_io_pread(ios->vin, src, sizeof(src), inpos);
+	if (ret < 0)
+		return ret;
+
+	if (bits) {
+		inflatePrime(&strm, bits, src[0] >> (8 - bits));
+		strm.next_in = src + 1;
+		strm.avail_in = ret - 1;
+	} else {
+		strm.next_in = src;
+		strm.avail_in = ret;
+	}
+	inpos += ret;
+	(void)inflateSetDictionary(&strm, cp->window, sizeof(cp->window));
+
+	offset -= cp->outpos;
+	do {
+		/* define where to put uncompressed data, and how much */
+		if (!offset && skip) {          /* at offset now */
+			strm.avail_out = len;
+			strm.next_out = buf;
+			skip = false;		/* only do this once */
+		} else if (offset > sizeof(discard)) {	/* skip WINSIZE bytes */
+			strm.avail_out = sizeof(discard);
+			strm.next_out = discard;
+			offset -= sizeof(discard);
+		} else if (offset) {			/* last skip */
+			strm.avail_out = (unsigned int)offset;
+			strm.next_out = discard;
+			offset = 0;
+		}
+
+		/* uncompress until avail_out filled, or end of stream */
+		do {
+			if (!strm.avail_in) {
+				ret = erofs_io_pread(ios->vin, src, sizeof(src),
+						     inpos);
+				if (ret < 0)
+					return ret;
+				if (!ret)
+					return -EIO;
+				inpos += ret;
+				strm.avail_in = ret;
+				strm.next_in = src;
+			}
+			ret = inflate(&strm, Z_NO_FLUSH);       /* normal inflate */
+			if (ret == Z_NEED_DICT)
+				ret = Z_DATA_ERROR;
+			if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
+				return -EIO;
+			if (ret == Z_STREAM_END)
+				break;
+		} while (strm.avail_out);
+
+		/* if reach end of stream, then don't keep trying to get more */
+		if (ret == Z_STREAM_END)
+			break;
+
+		/* do until offset reached and requested data read, or stream ends */
+	} while (skip);
+	return len - strm.avail_out;
+}
+
+static struct erofs_vfops erofs_gzran_ios_vfops = {
+	.pread = erofs_gzran_ios_vfpread,
+	.close = erofs_gzran_ios_vfclose,
+};
+
+struct erofs_vfile *erofs_gzran_zinfo_open(struct erofs_vfile *vin,
+					   void *zinfo_buf, unsigned int len)
+{
+	struct aws_soci_zinfo_header *h;
+	struct aws_soci_zinfo_ckpt *c;
+	struct erofs_vfile *vf;
+	struct erofs_gzran_iostream *ios;
+	unsigned int v2_size, version;
+	int ret, i;
+
+	if (len < sizeof(*h))
+		return ERR_PTR(-EINVAL);
+
+	vf = malloc(sizeof(*vf) + sizeof(*ios));
+	if (!vf)
+		return ERR_PTR(-ENOMEM);
+
+	ios = (struct erofs_gzran_iostream *)vf->payload;
+	h = zinfo_buf;
+	ios->entries = le32_to_cpu(h->have);
+	ios->span_size = le32_to_cpu(h->span_size);
+
+	v2_size = sizeof(*c) * ios->entries + sizeof(*h);
+	if (v2_size - sizeof(*c) == len) {
+		version = 1;
+	} else if (v2_size == len) {
+		version = 2;
+	} else {
+		ret = -EOPNOTSUPP;
+		goto err_ios;
+	}
+
+	ios->cp = malloc(sizeof(*ios->cp) * ios->entries);
+	if (!ios->cp) {
+		ret = -ENOMEM;
+		goto err_ios;
+	}
+
+	i = 0;
+	if (version == 1) {
+		ios->cp[0] = (struct erofs_gzran_cutpoint) {
+			.in_bitpos = 10 << 3,
+			.outpos = 0,
+		};
+		i = 1;
+	}
+
+	c = (struct aws_soci_zinfo_ckpt *)(h + 1);
+	for (; i < ios->entries; ++i, ++c) {
+		ios->cp[i].in_bitpos = (c->in << 3) | c->bits;
+		ios->cp[i].outpos = c->out;
+		memcpy(ios->cp[i].window, c->window, sizeof(*c->window));
+	}
+	ios->vin = vin;
+	vf->ops = &erofs_gzran_ios_vfops;
+	return vf;
+err_ios:
+	free(vf);
+	return ERR_PTR(ret);
+}
 #else
 struct erofs_gzran_builder *erofs_gzran_builder_init(struct erofs_vfile *vf,
 						     u32 span_size)
@@ -213,4 +383,10 @@ int erofs_gzran_builder_final(struct erofs_gzran_builder *gb)
 {
 	return 0;
 }
+
+struct erofs_vfile *erofs_gzran_zinfo_open(struct erofs_vfile *vin,
+					   void *zinfo_buf, unsigned int len)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
 #endif
lib/liberofs_gzran.h +2 -0
index 4764506..443fe15 100644
@@ -18,4 +18,6 @@ int erofs_gzran_builder_export_zinfo(struct erofs_gzran_builder *gb,
 				     struct erofs_vfile *zinfo_vf);
 int erofs_gzran_builder_final(struct erofs_gzran_builder *gb);
 
+struct erofs_vfile *erofs_gzran_zinfo_open(struct erofs_vfile *vin,
+					   void *zinfo_buf, unsigned int len);
 #endif
Signature · 1 line
2.43.5
author
#2 lore raw

[PATCH 3/2] TEST CODE

In reply to Gao Xiang

This reverts commit ad879589e4a281cbd05a1fd9c8d8865a34cd8d87.

---
 lib/gzran.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
lib/gzran.c +51 -0
index 6a6fddc..6d55c4a 100644
@@ -390,3 +390,54 @@ struct erofs_vfile *erofs_gzran_zinfo_open(struct erofs_vfile *vin,
 	return ERR_PTR(-EOPNOTSUPP);
 }
 #endif
+
+#ifdef TEST
+#include <fcntl.h>
+
+int main(int argc, char *argv[])
+{
+	struct erofs_vfile vin, *vout;
+	int zinfo_len, fd, len, offset;
+	char *zinfo_buf;
+	char *outbuf;
+
+	if (argc < 5)
+		exit(1);
+	fd = open(argv[2], O_RDONLY);
+	if (fd < 0)
+		exit(1);
+	zinfo_len = lseek(fd, 0, SEEK_END);
+	if (zinfo_len < 0)
+		exit(1);
+	(void)lseek(fd, 0, SEEK_SET);
+
+	zinfo_buf = malloc(zinfo_len);
+	if (!zinfo_buf)
+		exit(1);
+	if (read(fd, zinfo_buf, zinfo_len) < zinfo_len)
+		exit(1);
+	close(fd);
+
+	fd = open(argv[1], O_RDONLY);
+	if (fd < 0)
+		exit(1);
+	vin = (struct erofs_vfile) {.fd = fd};
+
+	vout = erofs_gzran_zinfo_open(&vin, zinfo_buf, zinfo_len);
+	if (IS_ERR(vout))
+		exit(1);
+	free(zinfo_buf);
+
+	len = atoi(argv[4]);
+	offset = atoi(argv[3]);
+
+	outbuf = malloc(len);
+	if (!outbuf)
+		exit(1);
+	erofs_io_pread(vout, outbuf, len, offset);
+	write(STDOUT_FILENO, outbuf, len);
+	erofs_io_close(vout);
+	return 0;
+}
+
+#endif
Signature · 1 line
2.43.5