Skip to contentSkip to conversation

erofs-utils: mkfs: support tgz streams for tarerofs

PATCH In review Gao Xiang 2 participants 2 messages Sep 19, 2023 – Sep 20, 2023 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: mkfs: support tgz streams for tarerofs

Introduce iostream to wrap up the input tarball stream for tarerofs.

Besides, add bultin tgz support if zlib is linked to mkfs.

---
 include/erofs/tar.h |  21 +++
 lib/tar.c           | 309 ++++++++++++++++++++++++++++++++------------
 mkfs/main.c         |  34 ++++-
 3 files changed, 278 insertions(+), 86 deletions(-)
include/erofs/tar.h +21 -0
index b50db1d..a76f740 100644
@@ -7,6 +7,9 @@ extern "C"
 {
 #endif
 
+#if defined(HAVE_ZLIB)
+#include <zlib.h>
+#endif
 #include <sys/stat.h>
 
 #include "internal.h"
@@ -21,8 +24,24 @@ struct erofs_pax_header {
 	char *path, *link;
 };
 
+#define EROFS_IOS_DECODER_NONE		0
+#define EROFS_IOS_DECODER_GZIP		1
+
+struct erofs_iostream {
+	union {
+		int fd;			/* original fd */
+		void *handler;
+	};
+	u64 sz;
+	char *buffer;
+	unsigned int head, tail, bufsize;
+	int decoder;
+	bool feof;
+};
+
 struct erofs_tarfile {
 	struct erofs_pax_header global;
+	struct erofs_iostream ios;
 	char *mapfile;
 
 	int fd;
@@ -30,6 +49,8 @@ struct erofs_tarfile {
 	bool index_mode, aufs;
 };
 
+void erofs_iostream_close(struct erofs_iostream *ios);
+int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder);
 int tarerofs_parse_tar(struct erofs_inode *root, struct erofs_tarfile *tar);
 
 #ifdef __cplusplus
lib/tar.c +228 -81

504 lines not on this page · raw message

mkfs/main.c +29 -5
index ea868bb..6d2b700 100644
@@ -66,6 +66,9 @@ static struct option long_options[] = {
 	{"block-list-file", required_argument, NULL, 515},
 #endif
 	{"ovlfs-strip", optional_argument, NULL, 516},
+#ifdef HAVE_ZLIB
+	{"gzip", no_argument, NULL, 517},
+#endif
 	{0, 0, 0, 0},
 };
 
@@ -111,6 +114,9 @@ static void usage(void)
 	      " --force-gid=#         set all file gids to # (# = GID)\n"
 	      " --uid-offset=#        add offset # to all file uids (# = id offset)\n"
 	      " --gid-offset=#        add offset # to all file gids (# = id offset)\n"
+#ifdef HAVE_ZLIB
+	      " --gzip                try to filter the tarball stream through gzip\n"
+#endif
 	      " --help                display this help and exit\n"
 	      " --ignore-mtime        use build time instead of strict per-file modification time\n"
 	      " --max-extent-bytes=#  set maximum decompressed extent size # in bytes\n"
@@ -139,7 +145,7 @@ static unsigned int pclustersize_packed, pclustersize_max;
 static struct erofs_tarfile erofstar = {
 	.global.xattrs = LIST_HEAD_INIT(erofstar.global.xattrs)
 };
-static bool tar_mode, rebuild_mode;
+static bool tar_mode, rebuild_mode, gzip_supported;
 
 static unsigned int rebuild_src_count;
 static LIST_HEAD(rebuild_src_list);
@@ -525,6 +531,9 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			else
 				cfg.c_ovlfs_strip = false;
 			break;
+		case 517:
+			gzip_supported = true;
+			break;
 		case 1:
 			usage();
 			exit(0);
@@ -560,7 +569,17 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			erofs_err("missing argument: SOURCE(s)");
 			return -EINVAL;
 		} else {
-			erofstar.fd = STDIN_FILENO;
+			int dupfd;
+
+			dupfd = dup(STDIN_FILENO);
+			if (dupfd < 0) {
+				erofs_err("failed to duplicate STDIN_FILENO: %s",
+					  strerror(errno));
+				return -errno;
+			}
+			err = erofs_iostream_open(&erofstar.ios, dupfd, gzip_supported);
+			if (err)
+				return err;
 		}
 	} else {
 		struct stat st;
@@ -573,12 +592,15 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		}
 
 		if (tar_mode) {
-			erofstar.fd = open(cfg.c_src_path, O_RDONLY);
-			if (erofstar.fd < 0) {
+			int fd = open(cfg.c_src_path, O_RDONLY);
+
+			if (fd < 0) {
 				erofs_err("failed to open file: %s", cfg.c_src_path);
-				usage();
 				return -errno;
 			}
+			err = erofs_iostream_open(&erofstar.ios, fd, gzip_supported);
+			if (err)
+				return err;
 		} else {
 			err = lstat(cfg.c_src_path, &st);
 			if (err)
@@ -1182,6 +1204,8 @@ exit:
 	erofs_rebuild_cleanup();
 	erofs_diskbuf_exit();
 	erofs_exit_configure();
+	if (tar_mode)
+		erofs_iostream_close(&erofstar.ios);
 
 	if (err) {
 		erofs_err("\tCould not format the device : %s\n",
Signature · 1 line
2.39.3
#1 lore raw
Quoted 5 lines · Gao Xiang

Introduce iostream to wrap up the input tarball stream for tarerofs.

Besides, add bultin tgz support if zlib is linked to mkfs.

Apply the following diff to fix pax header regression:

lib/tar.c +1 -1
index 52036a6..0744972 100644
@@ -191,7 +191,7 @@ int erofs_iostream_bread(struct erofs_iostream *ios, void *buf, u64 bytes)
 		rem -= ret;
 	} while (rem && ret);
 
-	return rem;
+	return bytes - rem;
 }
 
 int erofs_iostream_lskip(struct erofs_iostream *ios, u64 sz)