[PATCH 3/7] erofs-utils: lib: adapt importer for filesystem tree dumping

Gao Xiang hsiangkao at linux.alibaba.com
Tue Aug 19 00:47:37 AEST 2025


Convert erofs_mkfs_build_tree_from_path() and erofs_rebuild_dump_tree()
to erofs_importer_load_tree().

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 include/erofs/importer.h |  2 ++
 include/erofs/inode.h    |  7 ++--
 lib/inode.c              | 76 ++++++++++++++--------------------------
 mkfs/main.c              | 16 +++++----
 4 files changed, 43 insertions(+), 58 deletions(-)

diff --git a/include/erofs/importer.h b/include/erofs/importer.h
index 7c29e03..541a86d 100644
--- a/include/erofs/importer.h
+++ b/include/erofs/importer.h
@@ -13,11 +13,13 @@ extern "C"
 #include "internal.h"
 
 struct erofs_importer_params {
+	char *source;
 };
 
 struct erofs_importer {
 	struct erofs_importer_params *params;
 	struct erofs_sb_info *sbi;
+	struct erofs_inode *root;
 };
 
 void erofs_importer_preset(struct erofs_importer_params *params);
diff --git a/include/erofs/inode.h b/include/erofs/inode.h
index ce86f59..9d05aaa 100644
--- a/include/erofs/inode.h
+++ b/include/erofs/inode.h
@@ -23,6 +23,8 @@ static inline struct erofs_inode *erofs_igrab(struct erofs_inode *inode)
 	return inode;
 }
 
+struct erofs_importer;
+
 u32 erofs_new_encode_dev(dev_t dev);
 unsigned char erofs_mode_to_ftype(umode_t mode);
 umode_t erofs_ftype_to_mode(unsigned int ftype, unsigned int perm);
@@ -38,12 +40,11 @@ struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
 				   const char *name);
 int erofs_allocate_inode_bh_data(struct erofs_inode *inode, erofs_blk_t nblocks);
 bool erofs_dentry_is_wht(struct erofs_sb_info *sbi, struct erofs_dentry *d);
-int erofs_rebuild_dump_tree(struct erofs_inode *dir, bool incremental);
 int __erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 		       const char *path);
 struct erofs_inode *erofs_new_inode(struct erofs_sb_info *sbi);
-struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
-						    const char *path);
+int erofs_importer_load_tree(struct erofs_importer *im, bool rebuild,
+			     bool incremental);
 struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
 						     int fd, const char *name);
 int erofs_fixup_root_inode(struct erofs_inode *root);
diff --git a/lib/inode.c b/lib/inode.c
index 3d64dcb..2599772 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -27,6 +27,7 @@
 #include "erofs/compress_hints.h"
 #include "erofs/blobchunk.h"
 #include "erofs/fragments.h"
+#include "erofs/importer.h"
 #include "liberofs_private.h"
 #include "liberofs_metabox.h"
 
@@ -1275,9 +1276,8 @@ struct erofs_inode *erofs_new_inode(struct erofs_sb_info *sbi)
 	return inode;
 }
 
-/* get the inode from the source path */
-static struct erofs_inode *erofs_iget_from_srcpath(struct erofs_sb_info *sbi,
-						   const char *path)
+static struct erofs_inode *erofs_iget_from_local(struct erofs_sb_info *sbi,
+						 const char *path)
 {
 	struct stat st;
 	struct erofs_inode *inode;
@@ -1644,7 +1644,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)
 		if (ret < 0 || ret >= PATH_MAX)
 			goto err_closedir;
 
-		inode = erofs_iget_from_srcpath(sbi, buf);
+		inode = erofs_iget_from_local(sbi, buf);
 		if (IS_ERR(inode)) {
 			ret = PTR_ERR(inode);
 			goto err_closedir;
@@ -1880,9 +1880,10 @@ static void erofs_mark_parent_inode(struct erofs_inode *inode,
 	inode->i_parent = (void *)((unsigned long)dir | 1);
 }
 
-static int erofs_mkfs_dump_tree(struct erofs_inode *root, bool rebuild,
+static int erofs_mkfs_dump_tree(struct erofs_importer *im, bool rebuild,
 				bool incremental)
 {
+	struct erofs_inode *root = im->root;
 	struct erofs_sb_info *sbi = root->sbi;
 	struct erofs_inode *dumpdir = erofs_igrab(root);
 	int err, err2;
@@ -1964,12 +1965,8 @@ static int erofs_mkfs_dump_tree(struct erofs_inode *root, bool rebuild,
 }
 
 struct erofs_mkfs_buildtree_ctx {
-	struct erofs_sb_info *sbi;
-	union {
-		const char *path;
-		struct erofs_inode *root;
-	} u;
-	bool incremental;
+	struct erofs_importer *im;
+	bool rebuild, incremental;
 };
 #ifndef EROFS_MT_ENABLED
 #define __erofs_mkfs_build_tree erofs_mkfs_build_tree
@@ -1977,26 +1974,22 @@ struct erofs_mkfs_buildtree_ctx {
 
 static int __erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx)
 {
-	bool from_path = !!ctx->sbi;
-	struct erofs_inode *root;
-	int err;
+	struct erofs_importer *im = ctx->im;
 
-	if (from_path) {
-		root = erofs_iget_from_srcpath(ctx->sbi, ctx->u.path);
-		if (IS_ERR(root))
-			return PTR_ERR(root);
-	} else {
-		root = ctx->u.root;
-	}
+	if (!ctx->rebuild) {
+		struct erofs_importer_params *params = im->params;
+		struct stat st;
+		int err;
 
-	err = erofs_mkfs_dump_tree(root, !from_path, ctx->incremental);
-	if (err) {
-		if (from_path)
-			erofs_iput(root);
-		return err;
+		err = lstat(params->source, &st);
+		if (err)
+			return -errno;
+
+		err = erofs_fill_inode(im->root, &st, params->source);
+		if (err)
+			return err;
 	}
-	ctx->u.root = root;
-	return 0;
+	return erofs_mkfs_dump_tree(im, ctx->rebuild, ctx->incremental);
 }
 
 #ifdef EROFS_MT_ENABLED
@@ -2024,7 +2017,8 @@ static int erofs_get_fdlimit(void)
 
 static int erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx)
 {
-	struct erofs_sb_info *sbi = ctx->sbi ? ctx->sbi : ctx->u.root->sbi;
+	struct erofs_importer *im = ctx->im;
+	struct erofs_sb_info *sbi = im->sbi;
 	struct erofs_mkfs_dfops *q;
 	int err, err2;
 	void *retval;
@@ -2078,28 +2072,12 @@ fail:
 }
 #endif
 
-struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
-						    const char *path)
-{
-	struct erofs_mkfs_buildtree_ctx ctx = {
-		.sbi = sbi,
-		.u.path = path,
-	};
-	int err;
-
-	if (!sbi)
-		return ERR_PTR(-EINVAL);
-	err = erofs_mkfs_build_tree(&ctx);
-	if (err)
-		return ERR_PTR(err);
-	return ctx.u.root;
-}
-
-int erofs_rebuild_dump_tree(struct erofs_inode *root, bool incremental)
+int erofs_importer_load_tree(struct erofs_importer *im, bool rebuild,
+			     bool incremental)
 {
 	return erofs_mkfs_build_tree(&((struct erofs_mkfs_buildtree_ctx) {
-		.sbi = NULL,
-		.u.root = root,
+		.im = im,
+		.rebuild = rebuild,
 		.incremental = incremental,
 	}));
 }
diff --git a/mkfs/main.c b/mkfs/main.c
index 8b18b35..9039710 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1542,6 +1542,7 @@ int main(int argc, char **argv)
 #endif
 	erofs_show_config();
 
+	importer_params.source = cfg.c_src_path;
 	err = erofs_importer_init(&importer);
 	if (err)
 		goto exit;
@@ -1694,12 +1695,12 @@ int main(int argc, char **argv)
 		if (cfg.c_extra_ea_name_prefixes)
 			erofs_xattr_flush_name_prefixes(&g_sbi);
 
-		root = erofs_mkfs_build_tree_from_path(&g_sbi, cfg.c_src_path);
+		root = erofs_new_inode(&g_sbi);
 		if (IS_ERR(root)) {
 			err = PTR_ERR(root);
-			root = NULL;
 			goto exit;
 		}
+		incremental_mode = false;
 	} else {
 		root = erofs_rebuild_make_root(&g_sbi);
 		if (IS_ERR(root)) {
@@ -1740,12 +1741,15 @@ int main(int argc, char **argv)
 				goto exit;
 #endif
 		}
-
-		err = erofs_rebuild_dump_tree(root, incremental_mode);
-		if (err)
-			goto exit;
 	}
 
+	importer.root = root;
+	err = erofs_importer_load_tree(&importer,
+				       source_mode != EROFS_MKFS_SOURCE_LOCALDIR,
+				       incremental_mode);
+	if (err)
+		goto exit;
+
 	if (tar_index_512b) {
 		if (!g_sbi.extra_devices) {
 			DBG_BUGON(1);
-- 
2.43.5



More information about the Linux-erofs mailing list