Skip to contentSkip to conversation

erofs-utils: add manifest input support

RFC PATCH New adigitX 1 participant 6 messages Mar 21 Library code

lore
Share & tools
lore

adigitX

author
#0 lore raw

cover erofs-utils: add manifest input support

Hi,

This RFC series adds manifest-driven image construction support to mkfs.erofs.

The series is split into plumbing, loader interface, composefs support, proto support, and a final mkfs follow-up so each step is easier to review.

Current scope:

- add a manifest source mode to mkfs.erofs
- add a manifest loader interface in lib/
- implement a composefs-based manifest subset
- implement a proto manifest subset
- enable diskbuf staging for manifest-backed inputs

The current implementation is intentionally minimal. In particular, composefs xattrs, inline content, and digests are not implemented yet.

This series is based on:

b67812ce4eb5 ("erofs-utils: fix thread join loop in erofs_destroy_workqueue")

Build and validation:

- built locally with make -j2
- final tree passes ./tests/manifest.sh

Comments on the series split, manifest format handling, and any preferred follow-up direction would be appreciated.

Thanks,
Aditya

adigitX (5):
erofs-utils: mkfs: add manifest source mode plumbing
erofs-utils: lib: add manifest loader interface
erofs-utils: lib: implement composefs manifest subset
erofs-utils: lib: implement proto manifest subset
erofs-utils: mkfs: enable diskbuf staging for manifest inputs

 include/erofs/manifest.h |   26 +
 lib/Makefile.am          |    3 +-
 lib/manifest.c           | 1061 ++++++++++++++++++++++++++++++++++++++
 mkfs/main.c              |   67 ++-
 4 files changed, 1151 insertions(+), 6 deletions(-)
 create mode 100644 include/erofs/manifest.h
 create mode 100644 lib/manifest.c
Signature · 1 line
2.51.0
author
#1 lore raw

RFC PATCH 1/5 erofs-utils: mkfs: add manifest source mode plumbing

---
 mkfs/main.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 64 insertions(+), 5 deletions(-)
mkfs/main.c +64 -5
index 58c18f9..9641f2f 100644
@@ -60,6 +60,7 @@ static struct option long_options[] = {
 	{"gid-offset", required_argument, NULL, 17},
 	{"tar", optional_argument, NULL, 20},
 	{"aufs", no_argument, NULL, 21},
+	{"manifest", optional_argument, NULL, 22},
 	{"mount-point", required_argument, NULL, 512},
 #ifdef WITH_ANDROID
 	{"product-out", required_argument, NULL, 513},
@@ -209,6 +210,8 @@ static void usage(int argc, char **argv)
 		" --gid-offset=#         add offset # to all file gids (# = id offset)\n"
 		" --hard-dereference     dereference hardlinks, add links as separate inodes\n"
 		" --ignore-mtime         use build time instead of strict per-file modification time\n"
+		" --manifest[=X]         generate an image from a manifest source\n"
+		"                        (X = auto|composefs|proto; default: auto)\n"
 		" --max-extent-bytes=#   set maximum decompressed extent size # in bytes\n"
 		" --mount-point=X        X=prefix of target fs path (default: /)\n"
 		" --preserve-mtime       keep per-file modification time strictly\n"
@@ -316,12 +319,19 @@ enum {
 
 static enum {
 	EROFS_MKFS_SOURCE_LOCALDIR,
+	EROFS_MKFS_SOURCE_MANIFEST,
 	EROFS_MKFS_SOURCE_TAR,
 	EROFS_MKFS_SOURCE_S3,
 	EROFS_MKFS_SOURCE_OCI,
 	EROFS_MKFS_SOURCE_REBUILD,
 } source_mode;
 
+static enum {
+	EROFS_MKFS_MANIFEST_AUTO,
+	EROFS_MKFS_MANIFEST_COMPOSEFS,
+	EROFS_MKFS_MANIFEST_PROTO,
+} manifest_format;
+
 static unsigned int rebuild_src_count;
 static LIST_HEAD(rebuild_src_list);
 static u8 fixeduuid[16];
@@ -331,6 +341,33 @@ static int tarerofs_decoder;
 static FILE *vmdk_dcf;
 static char *mkfs_aws_zinfo_file;
 
+static int mkfs_parse_manifest_cfg(const char *arg)
+{
+	if (source_mode != EROFS_MKFS_SOURCE_LOCALDIR) {
+		erofs_err("--manifest cannot be used together with another source mode");
+		return -EINVAL;
+	}
+
+	source_mode = EROFS_MKFS_SOURCE_MANIFEST;
+	manifest_format = EROFS_MKFS_MANIFEST_AUTO;
+
+	if (!arg || !*arg)
+		return 0;
+	if (!strcmp(arg, "auto"))
+		return 0;
+	if (!strcmp(arg, "composefs")) {
+		manifest_format = EROFS_MKFS_MANIFEST_COMPOSEFS;
+		return 0;
+	}
+	if (!strcmp(arg, "proto")) {
+		manifest_format = EROFS_MKFS_MANIFEST_PROTO;
+		return 0;
+	}
+
+	erofs_err("invalid manifest format %s", arg);
+	return -EINVAL;
+}
+
 static int erofs_mkfs_feat_set_legacy_compress(struct erofs_importer_params *params,
 					       bool en, const char *val,
 					       unsigned int vallen)
@@ -612,20 +649,24 @@ static int mkfs_apply_zfeature_bits(struct erofs_importer_params *params,
 	return 0;
 }
 
-static void mkfs_parse_tar_cfg(char *cfg)
+static void mkfs_parse_tar_cfg(char *arg)
 {
 	char *p;
 
+	if (source_mode != EROFS_MKFS_SOURCE_LOCALDIR) {
+		erofs_err("--tar cannot be used together with another source mode");
+		return;
+	}
 	source_mode = EROFS_MKFS_SOURCE_TAR;
-	if (!cfg)
+	if (!arg)
 		return;
-	p = strchr(cfg, ',');
+	p = strchr(arg, ',');
 	if (p) {
 		*p = '\0';
 		if ((*++p) != '\0')
 			erofstar.mapfile = strdup(p);
 	}
-	if (!strcmp(cfg, "headerball"))
+	if (!strcmp(arg, "headerball"))
 		erofstar.headeronly_mode = true;
 
 	if (erofstar.headeronly_mode || !strcmp(optarg, "i") ||
@@ -1047,6 +1088,11 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
 			source_mode = EROFS_MKFS_SOURCE_REBUILD;
 		}
 		break;
+	case EROFS_MKFS_SOURCE_MANIFEST:
+		cfg.c_src_path = strdup(argv[optind++]);
+		if (!cfg.c_src_path)
+			return -ENOMEM;
+		break;
 	case EROFS_MKFS_SOURCE_TAR:
 		cfg.c_src_path = strdup(argv[optind++]);
 		if (!cfg.c_src_path)
@@ -1363,6 +1409,11 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 		case 21:
 			erofstar.aufs = true;
 			break;
+		case 22:
+			err = mkfs_parse_manifest_cfg(optarg);
+			if (err)
+				return err;
+			break;
 		case 516:
 			params->ovlfs_strip = !optarg || !strcmp(optarg, "1");
 			break;
@@ -1585,9 +1636,14 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 		err = mkfs_parse_sources(argc, argv, optind);
 		if (err)
 			return err;
-	} else if (source_mode != EROFS_MKFS_SOURCE_TAR) {
+	} else if (source_mode != EROFS_MKFS_SOURCE_TAR &&
+		   source_mode != EROFS_MKFS_SOURCE_MANIFEST) {
 		erofs_err("missing argument: SOURCE(s)");
 		return -EINVAL;
+	} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
+		cfg.c_src_path = strdup("-");
+		if (!cfg.c_src_path)
+			return -ENOMEM;
 	} else {
 		int dupfd;
 
@@ -2032,6 +2088,9 @@ int main(int argc, char **argv)
 	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		while (!(err = tarerofs_parse_tar(&importer, &erofstar)))
 			;
+	} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
+		erofs_err("manifest input support is not implemented yet");
+		err = -EOPNOTSUPP;
 	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
 		err = erofs_mkfs_rebuild_load_trees(root);
 #ifdef S3EROFS_ENABLED
Signature · 1 line
2.51.0
author
#2 lore raw

RFC PATCH 2/5 erofs-utils: lib: add manifest loader interface

In reply to adigitX
---
 include/erofs/manifest.h | 26 ++++++++++++++++++++++++++
 lib/Makefile.am          |  3 ++-
 lib/manifest.c           | 16 ++++++++++++++++
 mkfs/main.c              | 17 +++++++----------
 4 files changed, 51 insertions(+), 11 deletions(-)
 create mode 100644 include/erofs/manifest.h
 create mode 100644 lib/manifest.c
include/erofs/manifest.h new file +26 -0
new file mode 100644
index 0000000..c8a04ec
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_MANIFEST_H
+#define __EROFS_MANIFEST_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "importer.h"
+
+enum erofs_manifest_format {
+	EROFS_MANIFEST_FORMAT_AUTO,
+	EROFS_MANIFEST_FORMAT_COMPOSEFS,
+	EROFS_MANIFEST_FORMAT_PROTO,
+};
+
+int erofs_manifest_load(struct erofs_importer *im,
+			enum erofs_manifest_format format,
+			const char *source);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/Makefile.am +2 -1
index 77f6fd8..42c56dc 100644
@@ -16,6 +16,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/internal.h \
       $(top_srcdir)/include/erofs/io.h \
       $(top_srcdir)/include/erofs/list.h \
+      $(top_srcdir)/include/erofs/manifest.h \
       $(top_srcdir)/include/erofs/print.h \
       $(top_srcdir)/include/erofs/bitops.h \
       $(top_srcdir)/include/erofs/tar.h \
@@ -40,7 +41,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
-		      block_list.c rebuild.c diskbuf.c bitops.c dedupe_ext.c \
+		      block_list.c rebuild.c diskbuf.c manifest.c bitops.c dedupe_ext.c \
 		      vmdk.c metabox.c global.c importer.c base64.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
lib/manifest.c new file +16 -0
new file mode 100644
index 0000000..d7bbd5d
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
+#include <errno.h>
+#include "erofs/manifest.h"
+#include "erofs/print.h"
+
+int erofs_manifest_load(struct erofs_importer *im,
+			enum erofs_manifest_format format,
+			const char *source)
+{
+	if (!im || !im->root || !source)
+		return -EINVAL;
+
+	(void)format;
+	erofs_err("manifest input support is not implemented yet");
+	return -EOPNOTSUPP;
+}
mkfs/main.c +7 -10
index 9641f2f..cb2cdca 100644
@@ -16,6 +16,7 @@
 #include "erofs/config.h"
 #include "erofs/print.h"
 #include "erofs/importer.h"
+#include "erofs/manifest.h"
 #include "erofs/diskbuf.h"
 #include "erofs/inode.h"
 #include "erofs/tar.h"
@@ -326,11 +327,7 @@ static enum {
 	EROFS_MKFS_SOURCE_REBUILD,
 } source_mode;
 
-static enum {
-	EROFS_MKFS_MANIFEST_AUTO,
-	EROFS_MKFS_MANIFEST_COMPOSEFS,
-	EROFS_MKFS_MANIFEST_PROTO,
-} manifest_format;
+static enum erofs_manifest_format manifest_format;
 
 static unsigned int rebuild_src_count;
 static LIST_HEAD(rebuild_src_list);
@@ -349,18 +346,18 @@ static int mkfs_parse_manifest_cfg(const char *arg)
 	}
 
 	source_mode = EROFS_MKFS_SOURCE_MANIFEST;
-	manifest_format = EROFS_MKFS_MANIFEST_AUTO;
+	manifest_format = EROFS_MANIFEST_FORMAT_AUTO;
 
 	if (!arg || !*arg)
 		return 0;
 	if (!strcmp(arg, "auto"))
 		return 0;
 	if (!strcmp(arg, "composefs")) {
-		manifest_format = EROFS_MKFS_MANIFEST_COMPOSEFS;
+		manifest_format = EROFS_MANIFEST_FORMAT_COMPOSEFS;
 		return 0;
 	}
 	if (!strcmp(arg, "proto")) {
-		manifest_format = EROFS_MKFS_MANIFEST_PROTO;
+		manifest_format = EROFS_MANIFEST_FORMAT_PROTO;
 		return 0;
 	}
 
@@ -2089,8 +2086,8 @@ int main(int argc, char **argv)
 		while (!(err = tarerofs_parse_tar(&importer, &erofstar)))
 			;
 	} else if (source_mode == EROFS_MKFS_SOURCE_MANIFEST) {
-		erofs_err("manifest input support is not implemented yet");
-		err = -EOPNOTSUPP;
+		err = erofs_manifest_load(&importer, manifest_format,
+					  cfg.c_src_path);
 	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
 		err = erofs_mkfs_rebuild_load_trees(root);
 #ifdef S3EROFS_ENABLED
Signature · 1 line
2.51.0
author
#3 lore raw

RFC PATCH 3/5 erofs-utils: lib: implement composefs manifest subset

In reply to adigitX
---
 lib/manifest.c | 715 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 712 insertions(+), 3 deletions(-)
lib/manifest.c +712 -3
index d7bbd5d..ed37f94 100644
@@ -1,16 +1,725 @@
 // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
+#define _GNU_SOURCE
 #include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "erofs/diskbuf.h"
+#include "erofs/hashmap.h"
+#include "erofs/inode.h"
 #include "erofs/manifest.h"
 #include "erofs/print.h"
+#include "liberofs_rebuild.h"
+
+#define EROFS_MANIFEST_COMPOSEFS_NR_FIELDS	11
+
+struct erofs_manifest_path {
+	struct hashmap_entry ent;
+	char path[];
+};
+
+struct erofs_manifest_ctx {
+	struct erofs_importer *im;
+	struct erofs_inode *root;
+	struct hashmap explicit_paths;
+};
+
+static int erofs_manifest_path_cmp(const void *a, const void *b,
+				   const void *keydata)
+{
+	const struct erofs_manifest_path *ea = a;
+	const struct erofs_manifest_path *eb = b;
+	const char *path = keydata ? keydata : eb->path;
+
+	return strcmp(ea->path, path);
+}
+
+static void erofs_manifest_ctx_init(struct erofs_manifest_ctx *ctx,
+				    struct erofs_importer *im)
+{
+	*ctx = (struct erofs_manifest_ctx) {
+		.im = im,
+		.root = im->root,
+	};
+	hashmap_init(&ctx->explicit_paths, erofs_manifest_path_cmp, 0);
+}
+
+static void erofs_manifest_ctx_exit(struct erofs_manifest_ctx *ctx)
+{
+	struct hashmap_iter iter;
+	struct erofs_manifest_path *path;
+
+	while ((path = hashmap_iter_first(&ctx->explicit_paths, &iter)) != NULL) {
+		hashmap_remove(&ctx->explicit_paths, &path->ent);
+		free(path);
+	}
+	(void)hashmap_free(&ctx->explicit_paths);
+}
+
+static bool erofs_manifest_has_explicit_path(struct erofs_manifest_ctx *ctx,
+					     const char *path)
+{
+	return hashmap_get_from_hash(&ctx->explicit_paths, strhash(path), path);
+}
+
+static int erofs_manifest_add_explicit_path(struct erofs_manifest_ctx *ctx,
+					    const char *path)
+{
+	struct erofs_manifest_path *entry;
+
+	entry = malloc(sizeof(*entry) + strlen(path) + 1);
+	if (!entry)
+		return -ENOMEM;
+
+	hashmap_entry_init(&entry->ent, strhash(path));
+	strcpy(entry->path, path);
+	hashmap_add(&ctx->explicit_paths, &entry->ent);
+	return 0;
+}
+
+static int erofs_manifest_open_source(const char *source, FILE **fp)
+{
+	if (!strcmp(source, "-")) {
+		*fp = stdin;
+		return 0;
+	}
+
+	*fp = fopen(source, "r");
+	if (!*fp)
+		return -errno;
+	return 0;
+}
+
+static void erofs_manifest_close_source(const char *source, FILE *fp)
+{
+	if (fp && strcmp(source, "-"))
+		fclose(fp);
+}
+
+static void erofs_manifest_trim_newline(char *line)
+{
+	size_t len = strlen(line);
+
+	while (len && (line[len - 1] == '\n' || line[len - 1] == '\r'))
+		line[--len] = '\0';
+}
+
+static int erofs_manifest_parse_uint(const char *value, int base, u64 *out)
+{
+	char *end;
+	unsigned long long n;
+
+	if (!value || !*value)
+		return -EINVAL;
+
+	errno = 0;
+	n = strtoull(value, &end, base);
+	if (errno || *end != '\0')
+		return -EINVAL;
+	*out = n;
+	return 0;
+}
+
+static int erofs_manifest_parse_mtime(const char *value, u64 *sec, u32 *nsec)
+{
+	char *end;
+	unsigned long long seconds, nanoseconds;
+
+	if (!value || !*value || !strcmp(value, "-"))
+		return -EINVAL;
+
+	errno = 0;
+	seconds = strtoull(value, &end, 10);
+	if (errno || *end != '.')
+		return -EINVAL;
+
+	errno = 0;
+	nanoseconds = strtoull(end + 1, &end, 10);
+	if (errno || *end != '\0' || nanoseconds >= 1000000000ULL)
+		return -EINVAL;
+
+	*sec = seconds;
+	*nsec = nanoseconds;
+	return 0;
+}
+
+static int erofs_manifest_hex2nibble(int c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
+	if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+	return -EINVAL;
+}
+
+static int erofs_manifest_unescape(const char *value, bool optional, char **out)
+{
+	size_t len, i, j;
+	char *decoded;
+
+	if (optional && !strcmp(value, "-")) {
+		*out = NULL;
+		return 0;
+	}
+
+	len = strlen(value);
+	decoded = malloc(len + 1);
+	if (!decoded)
+		return -ENOMEM;
+
+	for (i = 0, j = 0; i < len; ++i) {
+		int c = value[i];
+
+		if (c != '\\') {
+			decoded[j++] = c;
+			continue;
+		}
+
+		if (++i >= len) {
+			free(decoded);
+			return -EINVAL;
+		}
+
+		switch (value[i]) {
+		case '\\':
+			decoded[j++] = '\\';
+			break;
+		case 'n':
+			decoded[j++] = '\n';
+			break;
+		case 'r':
+			decoded[j++] = '\r';
+			break;
+		case 't':
+			decoded[j++] = '\t';
+			break;
+		case 'x': {
+			int hi, lo;
+
+			if (i + 2 >= len) {
+				free(decoded);
+				return -EINVAL;
+			}
+			hi = erofs_manifest_hex2nibble(value[i + 1]);
+			lo = erofs_manifest_hex2nibble(value[i + 2]);
+			if (hi < 0 || lo < 0) {
+				free(decoded);
+				return -EINVAL;
+			}
+			c = (hi << 4) | lo;
+			if (!c) {
+				free(decoded);
+				return -EINVAL;
+			}
+			decoded[j++] = c;
+			i += 2;
+			break;
+		}
+		default:
+			free(decoded);
+			return -EINVAL;
+		}
+	}
+	decoded[j] = '\0';
+	*out = decoded;
+	return 0;
+}
+
+static int erofs_manifest_next_field(char **cursor, char **field)
+{
+	char *start, *end;
+
+	start = *cursor;
+	if (!start || !*start)
+		return -EINVAL;
+
+	end = strchr(start, ' ');
+	if (end) {
+		*end = '\0';
+		*cursor = end + 1;
+		if (!**cursor)
+			*cursor = NULL;
+	} else {
+		*cursor = NULL;
+	}
+
+	if (!*start)
+		return -EINVAL;
+	*field = start;
+	return 0;
+}
+
+static int erofs_manifest_apply_inode(struct erofs_manifest_ctx *ctx,
+				      struct erofs_inode *inode,
+				      const char *path,
+				      umode_t mode, u32 uid, u32 gid,
+				      u64 mtime, u32 mtime_nsec,
+				      dev_t rdev, erofs_off_t size)
+{
+	struct stat st = { 0 };
+	int err;
+
+	st.st_mode = mode;
+	st.st_uid = uid;
+	st.st_gid = gid;
+	st.st_mtime = mtime;
+	ST_MTIM_NSEC_SET(&st, mtime_nsec);
+	st.st_rdev = rdev;
+	st.st_size = size;
+
+	err = __erofs_fill_inode(ctx->im, inode, &st, path);
+	if (err)
+		return err;
+
+	inode->i_mode = mode;
+	inode->i_size = S_ISDIR(mode) || S_ISCHR(mode) || S_ISBLK(mode) ||
+			S_ISFIFO(mode) ? 0 : size;
+	inode->u.i_rdev = (S_ISCHR(mode) || S_ISBLK(mode)) ?
+		erofs_new_encode_dev(rdev) : 0;
+	if (!inode->i_srcpath) {
+		inode->i_srcpath = strdup(path);
+		if (!inode->i_srcpath)
+			return -ENOMEM;
+	}
+	if (!S_ISDIR(mode))
+		inode->i_nlink = 1;
+	return 0;
+}
+
+static int erofs_manifest_stage_file(struct erofs_inode *inode,
+				     const char *payload)
+{
+	char buf[32768];
+	struct stat st;
+	int srcfd = -1, dstfd, err;
+	u64 off, left;
+
+	srcfd = open(payload, O_RDONLY);
+	if (srcfd < 0)
+		return -errno;
+	if (fstat(srcfd, &st)) {
+		err = -errno;
+		goto out;
+	}
+	if (!S_ISREG(st.st_mode) || st.st_size != inode->i_size) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (!inode->i_diskbuf) {
+		inode->i_diskbuf = calloc(1, sizeof(*inode->i_diskbuf));
+		if (!inode->i_diskbuf) {
+			err = -ENOMEM;
+			goto out;
+		}
+	} else {
+		erofs_diskbuf_close(inode->i_diskbuf);
+	}
+
+	dstfd = erofs_diskbuf_reserve(inode->i_diskbuf, 0, &off);
+	if (dstfd < 0) {
+		err = dstfd;
+		goto out;
+	}
+
+	left = inode->i_size;
+	while (left) {
+		ssize_t nread;
+
+		nread = read(srcfd, buf, min_t(u64, sizeof(buf), left));
+		if (nread < 0) {
+			err = -errno;
+			goto out;
+		}
+		if (!nread) {
+			err = -EIO;
+			goto out;
+		}
+		if (pwrite(dstfd, buf, nread, off) != nread) {
+			err = -errno ? -errno : -EIO;
+			goto out;
+		}
+		left -= nread;
+		off += nread;
+	}
+
+	erofs_diskbuf_commit(inode->i_diskbuf, inode->i_size);
+	inode->datasource = EROFS_INODE_DATA_SOURCE_DISKBUF;
+	err = 0;
+out:
+	if (srcfd >= 0)
+		close(srcfd);
+	return err;
+}
+
+static int erofs_manifest_lookup_dentry(struct erofs_manifest_ctx *ctx,
+					const char *path,
+					struct erofs_dentry **out)
+{
+	struct erofs_dentry *d;
+	char *scratch;
+	bool ignored;
+
+	scratch = strdup(path);
+	if (!scratch)
+		return -ENOMEM;
+
+	d = erofs_rebuild_get_dentry(ctx->root, scratch, false,
+				     &ignored, &ignored, false);
+	free(scratch);
+	if (IS_ERR(d))
+		return PTR_ERR(d);
+	*out = d;
+	return 0;
+}
+
+static int erofs_manifest_prepare_dentry(struct erofs_manifest_ctx *ctx,
+					 const char *path, umode_t mode,
+					 struct erofs_dentry **out_dentry,
+					 struct erofs_inode **out_inode,
+					 bool *out_root)
+{
+	struct erofs_dentry *d;
+	struct erofs_inode *inode;
+	char *scratch;
+	bool ignored;
+	int err;
+
+	*out_root = false;
+	if (erofs_manifest_has_explicit_path(ctx, path))
+		return -EEXIST;
+
+	if (!strcmp(path, "/")) {
+		if (!S_ISDIR(mode))
+			return -EINVAL;
+		*out_inode = ctx->root;
+		*out_dentry = NULL;
+		*out_root = true;
+		return 0;
+	}
+
+	scratch = strdup(path);
+	if (!scratch)
+		return -ENOMEM;
+
+	d = erofs_rebuild_get_dentry(ctx->root, scratch, false,
+				     &ignored, &ignored, false);
+	free(scratch);
+	if (IS_ERR(d))
+		return PTR_ERR(d);
+	if (!d)
+		return -EINVAL;
+
+	if (d->type == EROFS_FT_UNKNOWN) {
+		inode = erofs_new_inode(ctx->root->sbi);
+		if (IS_ERR(inode))
+			return PTR_ERR(inode);
+
+		inode->i_parent = d->inode;
+		d->inode = inode;
+		d->type = erofs_mode_to_ftype(mode);
+	} else {
+		inode = d->inode;
+		if (!S_ISDIR(inode->i_mode) || !S_ISDIR(mode))
+			return -EEXIST;
+	}
+
+	err = erofs_manifest_add_explicit_path(ctx, path);
+	if (err)
+		return err;
+
+	*out_inode = inode;
+	*out_dentry = d;
+	return 0;
+}
+
+static int erofs_manifest_apply_composefs_line(struct erofs_manifest_ctx *ctx,
+					       char *line, unsigned int lineno)
+{
+	char *cursor = line, *field[EROFS_MANIFEST_COMPOSEFS_NR_FIELDS];
+	char *path = NULL, *payload = NULL, *content = NULL, *digest = NULL;
+	struct erofs_dentry *dentry = NULL, *target = NULL;
+	struct erofs_inode *inode, *target_inode;
+	umode_t mode;
+	u64 size, mode_raw, uid, gid, rdev_raw;
+	u64 mtime;
+	u32 mtime_nsec;
+	bool hardlink, is_root;
+	int err, i;
+
+	for (i = 0; i < EROFS_MANIFEST_COMPOSEFS_NR_FIELDS; ++i) {
+		err = erofs_manifest_next_field(&cursor, &field[i]);
+		if (err) {
+			erofs_err("manifest:%u: expected %u composefs fields",
+				  lineno, EROFS_MANIFEST_COMPOSEFS_NR_FIELDS);
+			return err;
+		}
+	}
+	if (cursor && *cursor) {
+		erofs_err("manifest:%u: xattrs are not supported yet", lineno);
+		return -EOPNOTSUPP;
+	}
+
+	err = erofs_manifest_unescape(field[0], false, &path);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs path", lineno);
+		goto out;
+	}
+	if (path[0] != '/') {
+		err = -EINVAL;
+		erofs_err("manifest:%u: composefs path must be absolute: %s",
+			  lineno, path);
+		goto out;
+	}
+
+	err = erofs_manifest_parse_uint(field[1], 10, &size);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs size", lineno);
+		goto out;
+	}
+
+	hardlink = field[2][0] == '@';
+	err = erofs_manifest_parse_uint(field[2] + hardlink, 8, &mode_raw);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs mode", lineno);
+		goto out;
+	}
+	mode = mode_raw;
+
+	err = erofs_manifest_parse_uint(field[4], 10, &uid);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs uid", lineno);
+		goto out;
+	}
+
+	err = erofs_manifest_parse_uint(field[5], 10, &gid);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs gid", lineno);
+		goto out;
+	}
+
+	if (!strcmp(field[6], "-")) {
+		rdev_raw = 0;
+	} else {
+		err = erofs_manifest_parse_uint(field[6], 10, &rdev_raw);
+		if (err) {
+			erofs_err("manifest:%u: invalid composefs rdev", lineno);
+			goto out;
+		}
+	}
+
+	err = erofs_manifest_parse_mtime(field[7], &mtime, &mtime_nsec);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs mtime", lineno);
+		goto out;
+	}
+
+	err = erofs_manifest_unescape(field[8], true, &payload);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs payload", lineno);
+		goto out;
+	}
+	err = erofs_manifest_unescape(field[9], true, &content);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs content", lineno);
+		goto out;
+	}
+	err = erofs_manifest_unescape(field[10], true, &digest);
+	if (err) {
+		erofs_err("manifest:%u: invalid composefs digest", lineno);
+		goto out;
+	}
+
+	if (hardlink) {
+		if (content || digest) {
+			err = -EOPNOTSUPP;
+			erofs_err("manifest:%u: hardlinks only support PATH and PAYLOAD in v1",
+				  lineno);
+			goto out;
+		}
+		if (!payload || payload[0] != '/') {
+			err = -EINVAL;
+			erofs_err("manifest:%u: hardlink target must be absolute",
+				  lineno);
+			goto out;
+		}
+		err = erofs_manifest_prepare_dentry(ctx, path, S_IFREG | 0644,
+						    &dentry, &inode, &is_root);
+		if (err)
+			goto out;
+		if (is_root) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		err = erofs_manifest_lookup_dentry(ctx, payload, &target);
+		if (err)
+			goto out;
+		if (!target || target->type == EROFS_FT_UNKNOWN) {
+			err = -ENOENT;
+			goto out;
+		}
+
+		target_inode = target->inode;
+		if (S_ISDIR(target_inode->i_mode)) {
+			err = -EISDIR;
+			goto out;
+		}
+
+		erofs_iput(inode);
+		dentry->inode = erofs_igrab(target_inode);
+		dentry->type = erofs_mode_to_ftype(target_inode->i_mode);
+		++target_inode->i_nlink;
+		err = 0;
+		goto out;
+	}
+
+	err = erofs_manifest_prepare_dentry(ctx, path, mode, &dentry, &inode,
+					    &is_root);
+	if (err)
+		goto out;
+
+	if (content) {
+		err = -EOPNOTSUPP;
+		erofs_err("manifest:%u: inline composefs content is not supported yet",
+			  lineno);
+		goto out;
+	}
+	if (digest) {
+		err = -EOPNOTSUPP;
+		erofs_err("manifest:%u: composefs digests are not supported yet",
+			  lineno);
+		goto out;
+	}
+
+	switch (mode & S_IFMT) {
+	case S_IFDIR:
+		if (payload) {
+			err = -EINVAL;
+			goto out;
+		}
+		err = erofs_manifest_apply_inode(ctx, inode, path, mode,
+						 uid, gid, mtime, mtime_nsec,
+						 0, 0);
+		break;
+	case S_IFREG:
+		err = erofs_manifest_apply_inode(ctx, inode, path, mode,
+						 uid, gid, mtime, mtime_nsec,
+						 0, size);
+		if (err)
+			break;
+		if (payload) {
+			err = erofs_manifest_stage_file(inode, payload);
+		} else if (size) {
+			err = -EINVAL;
+		}
+		break;
+	case S_IFLNK:
+		if (!payload || size != strlen(payload)) {
+			err = -EINVAL;
+			goto out;
+		}
+		err = erofs_manifest_apply_inode(ctx, inode, path, mode,
+						 uid, gid, mtime, mtime_nsec,
+						 0, strlen(payload));
+		if (err)
+			break;
+		inode->i_link = strdup(payload);
+		if (!inode->i_link)
+			err = -ENOMEM;
+		break;
+	case S_IFCHR:
+	case S_IFBLK:
+		if (payload) {
+			err = -EINVAL;
+			goto out;
+		}
+		err = erofs_manifest_apply_inode(ctx, inode, path, mode,
+						 uid, gid, mtime, mtime_nsec,
+						 (dev_t)rdev_raw, 0);
+		break;
+	case S_IFIFO:
+		if (payload) {
+			err = -EINVAL;
+			goto out;
+		}
+		err = erofs_manifest_apply_inode(ctx, inode, path, mode,
+						 uid, gid, mtime, mtime_nsec,
+						 0, 0);
+		break;
+	default:
+		err = -EOPNOTSUPP;
+		break;
+	}
+out:
+	free(path);
+	free(payload);
+	free(content);
+	free(digest);
+	return err;
+}
+
+static int erofs_manifest_load_composefs(struct erofs_manifest_ctx *ctx,
+					 const char *source)
+{
+	FILE *fp;
+	char *line = NULL;
+	size_t cap = 0;
+	unsigned int lineno = 0;
+	int err;
+
+	err = erofs_manifest_open_source(source, &fp);
+	if (err)
+		return err;
+
+	while (getline(&line, &cap, fp) >= 0) {
+		++lineno;
+		erofs_manifest_trim_newline(line);
+		if (!line[0])
+			continue;
+
+		err = erofs_manifest_apply_composefs_line(ctx, line, lineno);
+		if (err)
+			goto out;
+	}
+
+	err = ferror(fp) ? -EIO : 0;
+out:
+	free(line);
+	erofs_manifest_close_source(source, fp);
+	return err;
+}
 
 int erofs_manifest_load(struct erofs_importer *im,
 			enum erofs_manifest_format format,
 			const char *source)
 {
+	struct erofs_manifest_ctx ctx;
+	int err;
+
 	if (!im || !im->root || !source)
 		return -EINVAL;
 
-	(void)format;
-	erofs_err("manifest input support is not implemented yet");
-	return -EOPNOTSUPP;
+	erofs_manifest_ctx_init(&ctx, im);
+	switch (format) {
+	case EROFS_MANIFEST_FORMAT_AUTO:
+	case EROFS_MANIFEST_FORMAT_COMPOSEFS:
+		err = erofs_manifest_load_composefs(&ctx, source);
+		break;
+	case EROFS_MANIFEST_FORMAT_PROTO:
+		erofs_err("proto manifest input support is not implemented yet");
+		err = -EOPNOTSUPP;
+		break;
+	default:
+		err = -EINVAL;
+		break;
+	}
+	erofs_manifest_ctx_exit(&ctx);
+	return err;
 }
Signature · 1 line
2.51.0
author
#4 lore raw

RFC PATCH 4/5 erofs-utils: lib: implement proto manifest subset

In reply to adigitX
---
 lib/manifest.c | 388 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 362 insertions(+), 26 deletions(-)
lib/manifest.c +362 -26
index ed37f94..a376a98 100644
@@ -8,6 +8,10 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <config.h>
+#if defined(HAVE_SYS_SYSMACROS_H)
+#include <sys/sysmacros.h>
+#endif
 #include "erofs/diskbuf.h"
 #include "erofs/hashmap.h"
 #include "erofs/inode.h"
@@ -16,6 +20,7 @@
 #include "liberofs_rebuild.h"
 
 #define EROFS_MANIFEST_COMPOSEFS_NR_FIELDS	11
+#define EROFS_MANIFEST_PROTO_HEADER		"%erofs.proto.v1"
 
 struct erofs_manifest_path {
 	struct hashmap_entry ent;
@@ -236,7 +241,12 @@ static int erofs_manifest_next_field(char **cursor, char **field)
 	char *start, *end;
 
 	start = *cursor;
-	if (!start || !*start)
+	if (!start)
+		return -EINVAL;
+
+	while (*start == ' ')
+		++start;
+	if (!*start)
 		return -EINVAL;
 
 	end = strchr(start, ' ');
@@ -255,6 +265,18 @@ static int erofs_manifest_next_field(char **cursor, char **field)
 	return 0;
 }
 
+static int erofs_manifest_stat_regular(const char *path, erofs_off_t *size)
+{
+	struct stat st;
+
+	if (stat(path, &st))
+		return -errno;
+	if (!S_ISREG(st.st_mode))
+		return -EINVAL;
+	*size = st.st_size;
+	return 0;
+}
+
 static int erofs_manifest_apply_inode(struct erofs_manifest_ctx *ctx,
 				      struct erofs_inode *inode,
 				      const char *path,
@@ -399,7 +421,8 @@ static int erofs_manifest_prepare_dentry(struct erofs_manifest_ctx *ctx,
 		if (!S_ISDIR(mode))
 			return -EINVAL;
 		*out_inode = ctx->root;
-		*out_dentry = NULL;
+		if (out_dentry)
+			*out_dentry = NULL;
 		*out_root = true;
 		return 0;
 	}
@@ -435,7 +458,8 @@ static int erofs_manifest_prepare_dentry(struct erofs_manifest_ctx *ctx,
 		return err;
 
 	*out_inode = inode;
-	*out_dentry = d;
+	if (out_dentry)
+		*out_dentry = d;
 	return 0;
 }
 
@@ -666,33 +690,357 @@ out:
 }
 
 static int erofs_manifest_load_composefs(struct erofs_manifest_ctx *ctx,
-					 const char *source)
+					 FILE *fp, char **line, size_t *cap,
+					 unsigned int lineno, bool has_line)
+{
+	int err;
+
+	if (has_line && (*line)[0]) {
+		err = erofs_manifest_apply_composefs_line(ctx, *line, lineno);
+		if (err)
+			return err;
+	}
+
+	while (getline(line, cap, fp) >= 0) {
+		++lineno;
+		erofs_manifest_trim_newline(*line);
+		if (!(*line)[0])
+			continue;
+
+		err = erofs_manifest_apply_composefs_line(ctx, *line, lineno);
+		if (err)
+			return err;
+	}
+	return ferror(fp) ? -EIO : 0;
+}
+
+static int erofs_manifest_apply_proto_hardlink(struct erofs_manifest_ctx *ctx,
+					       const char *path,
+					       const char *target_path,
+					       umode_t mode)
+{
+	struct erofs_dentry *dentry = NULL, *target = NULL;
+	struct erofs_inode *inode, *target_inode;
+	bool is_root;
+	int err;
+
+	err = erofs_manifest_prepare_dentry(ctx, path, mode, &dentry, &inode,
+					    &is_root);
+	if (err)
+		return err;
+	if (is_root)
+		return -EINVAL;
+
+	err = erofs_manifest_lookup_dentry(ctx, target_path, &target);
+	if (err)
+		return err;
+	if (!target || target->type == EROFS_FT_UNKNOWN)
+		return -ENOENT;
+
+	target_inode = target->inode;
+	if (S_ISDIR(target_inode->i_mode))
+		return -EISDIR;
+
+	erofs_iput(inode);
+	dentry->inode = erofs_igrab(target_inode);
+	dentry->type = erofs_mode_to_ftype(target_inode->i_mode);
+	++target_inode->i_nlink;
+	return 0;
+}
+
+static int erofs_manifest_apply_proto_line(struct erofs_manifest_ctx *ctx,
+					   char *line, unsigned int lineno)
+{
+	char *cursor = line, *field, *path = NULL, *payload = NULL;
+	struct erofs_inode *inode;
+	umode_t mode;
+	erofs_off_t size = 0;
+	u64 mode_raw, uid, gid, major_id, minor_id;
+	u64 mtime = ctx->root->i_mtime;
+	u32 mtime_nsec = ctx->root->i_mtime_nsec;
+	dev_t rdev = 0;
+	char kind;
+	bool is_root;
+	int err;
+
+	err = erofs_manifest_next_field(&cursor, &field);
+	if (err) {
+		erofs_err("manifest:%u: missing proto path", lineno);
+		return err;
+	}
+	err = erofs_manifest_unescape(field, false, &path);
+	if (err) {
+		erofs_err("manifest:%u: invalid proto path", lineno);
+		goto out;
+	}
+	if (path[0] != '/') {
+		err = -EINVAL;
+		erofs_err("manifest:%u: proto path must be absolute: %s",
+			  lineno, path);
+		goto out;
+	}
+
+	err = erofs_manifest_next_field(&cursor, &field);
+	if (err || strlen(field) != 1) {
+		err = -EINVAL;
+		erofs_err("manifest:%u: invalid proto type", lineno);
+		goto out;
+	}
+	kind = field[0];
+
+	err = erofs_manifest_next_field(&cursor, &field);
+	if (err || erofs_manifest_parse_uint(field, 8, &mode_raw)) {
+		err = -EINVAL;
+		erofs_err("manifest:%u: invalid proto mode", lineno);
+		goto out;
+	}
+
+	err = erofs_manifest_next_field(&cursor, &field);
+	if (err || erofs_manifest_parse_uint(field, 10, &uid)) {
+		err = -EINVAL;
+		erofs_err("manifest:%u: invalid proto uid", lineno);
+		goto out;
+	}
+
+	err = erofs_manifest_next_field(&cursor, &field);
+	if (err || erofs_manifest_parse_uint(field, 10, &gid)) {
+		err = -EINVAL;
+		erofs_err("manifest:%u: invalid proto gid", lineno);
+		goto out;
+	}
+
+	switch (kind) {
+	case 'd':
+		mode = S_IFDIR | (mode_raw & 07777);
+		break;
+	case 'f':
+		mode = S_IFREG | (mode_raw & 07777);
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err || erofs_manifest_unescape(field, false, &payload)) {
+			err = -EINVAL;
+			erofs_err("manifest:%u: invalid proto file payload", lineno);
+			goto out;
+		}
+		err = erofs_manifest_stat_regular(payload, &size);
+		if (err)
+			goto out;
+		break;
+	case 'l':
+		mode = S_IFLNK | (mode_raw & 07777);
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err || erofs_manifest_unescape(field, false, &payload)) {
+			err = -EINVAL;
+			erofs_err("manifest:%u: invalid proto symlink target",
+				  lineno);
+			goto out;
+		}
+		size = strlen(payload);
+		break;
+	case 'L':
+		mode = S_IFREG | (mode_raw & 07777);
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err || erofs_manifest_unescape(field, false, &payload)) {
+			err = -EINVAL;
+			erofs_err("manifest:%u: invalid proto hardlink target",
+				  lineno);
+			goto out;
+		}
+		if (payload[0] != '/') {
+			err = -EINVAL;
+			erofs_err("manifest:%u: hardlink target must be absolute",
+				  lineno);
+			goto out;
+		}
+		break;
+	case 'p':
+		mode = S_IFIFO | (mode_raw & 07777);
+		break;
+	case 'c':
+	case 'b':
+		mode = (kind == 'c' ? S_IFCHR : S_IFBLK) | (mode_raw & 07777);
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err || erofs_manifest_parse_uint(field, 10, &major_id)) {
+			err = -EINVAL;
+			erofs_err("manifest:%u: invalid proto device major", lineno);
+			goto out;
+		}
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err || erofs_manifest_parse_uint(field, 10, &minor_id)) {
+			err = -EINVAL;
+			erofs_err("manifest:%u: invalid proto device minor", lineno);
+			goto out;
+		}
+		rdev = makedev(major_id, minor_id);
+		break;
+	default:
+		err = -EOPNOTSUPP;
+		erofs_err("manifest:%u: unsupported proto type %c", lineno, kind);
+		goto out;
+	}
+
+	while (cursor && *cursor) {
+		err = erofs_manifest_next_field(&cursor, &field);
+		if (err)
+			goto out;
+		if (!strncmp(field, "mtime:", 6)) {
+			err = erofs_manifest_parse_mtime(field + 6, &mtime,
+							 &mtime_nsec);
+			if (err) {
+				erofs_err("manifest:%u: invalid proto mtime",
+					  lineno);
+				goto out;
+			}
+			continue;
+		}
+
+		err = -EINVAL;
+		erofs_err("manifest:%u: unsupported proto attribute %s",
+			  lineno, field);
+		goto out;
+	}
+
+	if (kind == 'L') {
+		err = erofs_manifest_apply_proto_hardlink(ctx, path, payload,
+							  mode);
+		goto out;
+	}
+
+	err = erofs_manifest_prepare_dentry(ctx, path, mode, NULL, &inode,
+					    &is_root);
+	if (err)
+		goto out;
+
+	switch (mode & S_IFMT) {
+	case S_IFDIR:
+	case S_IFREG:
+	case S_IFLNK:
+	case S_IFCHR:
+	case S_IFBLK:
+	case S_IFIFO:
+		break;
+	default:
+		err = -EOPNOTSUPP;
+		goto out;
+	}
+
+	err = erofs_manifest_apply_inode(ctx, inode, path, mode, uid, gid,
+					 mtime, mtime_nsec, rdev, size);
+	if (err)
+		goto out;
+
+	if (S_ISREG(mode) && payload) {
+		err = erofs_manifest_stage_file(inode, payload);
+	} else if (S_ISLNK(mode)) {
+		inode->i_link = strdup(payload);
+		if (!inode->i_link)
+			err = -ENOMEM;
+	}
+out:
+	free(path);
+	free(payload);
+	return err;
+}
+
+static int erofs_manifest_load_proto(struct erofs_manifest_ctx *ctx, FILE *fp,
+				     char **line, size_t *cap,
+				     unsigned int lineno, bool has_line)
+{
+	bool seen_header = false;
+	int err;
+
+	if (has_line && (*line)[0]) {
+		if (strcmp(*line, EROFS_MANIFEST_PROTO_HEADER)) {
+			erofs_err("manifest:%u: expected %s", lineno,
+				  EROFS_MANIFEST_PROTO_HEADER);
+			return -EINVAL;
+		}
+		seen_header = true;
+	}
+
+	while (getline(line, cap, fp) >= 0) {
+		++lineno;
+		erofs_manifest_trim_newline(*line);
+		if (!(*line)[0])
+			continue;
+		if (!seen_header) {
+			if (strcmp(*line, EROFS_MANIFEST_PROTO_HEADER)) {
+				erofs_err("manifest:%u: expected %s", lineno,
+					  EROFS_MANIFEST_PROTO_HEADER);
+				return -EINVAL;
+			}
+			seen_header = true;
+			continue;
+		}
+
+		if ((err = erofs_manifest_apply_proto_line(ctx, *line, lineno)))
+			return err;
+	}
+
+	if (!seen_header) {
+		erofs_err("manifest: missing %s header",
+			  EROFS_MANIFEST_PROTO_HEADER);
+		return -EINVAL;
+	}
+
+	return ferror(fp) ? -EIO : 0;
+}
+
+static int erofs_manifest_load_source(struct erofs_manifest_ctx *ctx,
+				      enum erofs_manifest_format format,
+				      const char *source)
 {
 	FILE *fp;
 	char *line = NULL;
 	size_t cap = 0;
 	unsigned int lineno = 0;
+	bool has_line = false;
 	int err;
 
 	err = erofs_manifest_open_source(source, &fp);
 	if (err)
 		return err;
 
-	while (getline(&line, &cap, fp) >= 0) {
-		++lineno;
-		erofs_manifest_trim_newline(line);
-		if (!line[0])
-			continue;
+	if (format == EROFS_MANIFEST_FORMAT_AUTO) {
+		while (getline(&line, &cap, fp) >= 0) {
+			++lineno;
+			erofs_manifest_trim_newline(line);
+			if (!line[0])
+				continue;
+
+			format = !strcmp(line, EROFS_MANIFEST_PROTO_HEADER) ?
+				EROFS_MANIFEST_FORMAT_PROTO :
+				EROFS_MANIFEST_FORMAT_COMPOSEFS;
+			has_line = true;
+			break;
+		}
 
-		err = erofs_manifest_apply_composefs_line(ctx, line, lineno);
-		if (err)
+		if (ferror(fp)) {
+			err = -EIO;
+			goto out;
+		}
+		if (format == EROFS_MANIFEST_FORMAT_AUTO) {
+			err = 0;
 			goto out;
+		}
+	}
+
+	switch (format) {
+	case EROFS_MANIFEST_FORMAT_COMPOSEFS:
+		err = erofs_manifest_load_composefs(ctx, fp, &line, &cap,
+						    lineno, has_line);
+		break;
+	case EROFS_MANIFEST_FORMAT_PROTO:
+		err = erofs_manifest_load_proto(ctx, fp, &line, &cap,
+						lineno, has_line);
+		break;
+	default:
+		err = -EINVAL;
+		break;
 	}
 
-	err = ferror(fp) ? -EIO : 0;
 out:
-	free(line);
 	erofs_manifest_close_source(source, fp);
+	free(line);
 	return err;
 }
 
@@ -707,19 +1055,7 @@ int erofs_manifest_load(struct erofs_importer *im,
 		return -EINVAL;
 
 	erofs_manifest_ctx_init(&ctx, im);
-	switch (format) {
-	case EROFS_MANIFEST_FORMAT_AUTO:
-	case EROFS_MANIFEST_FORMAT_COMPOSEFS:
-		err = erofs_manifest_load_composefs(&ctx, source);
-		break;
-	case EROFS_MANIFEST_FORMAT_PROTO:
-		erofs_err("proto manifest input support is not implemented yet");
-		err = -EOPNOTSUPP;
-		break;
-	default:
-		err = -EINVAL;
-		break;
-	}
+	err = erofs_manifest_load_source(&ctx, format, source);
 	erofs_manifest_ctx_exit(&ctx);
 	return err;
 }
Signature · 1 line
2.51.0
author
#5 lore raw

RFC PATCH 5/5 erofs-utils: mkfs: enable diskbuf staging for manifest inputs

In reply to adigitX
---
 mkfs/main.c | 1 +
 1 file changed, 1 insertion(+)
mkfs/main.c +1 -0
index cb2cdca..3fef5f6 100644
@@ -1986,6 +1986,7 @@ int main(int argc, char **argv)
 		erofs_uuid_generate(g_sbi.uuid);
 
 	if ((source_mode == EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) ||
+	    (source_mode == EROFS_MKFS_SOURCE_MANIFEST) ||
 	    (source_mode == EROFS_MKFS_SOURCE_S3) ||
 	    (source_mode == EROFS_MKFS_SOURCE_OCI)) {
 		err = erofs_diskbuf_init(1);
Signature · 1 line
2.51.0