Skip to contentSkip to conversation

erofs-utils: mkfs: combine erofs_{mkfs,rebuild}_handle_directory

PATCH New Gao Xiang 1 participant 2 messages Sep 25, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: mkfs: combine erofs_{mkfs,rebuild}_handle_directory

- Remove redundant logic;

- In preparation for incremental builds on local paths.

---
 lib/inode.c | 91 ++++++++++++++++++++++++++---------------------------
 1 file changed, 44 insertions(+), 47 deletions(-)
lib/inode.c +44 -47
index e57a0db..2918791 100644
@@ -1608,26 +1608,29 @@ static void erofs_mkfs_flushjobs(struct erofs_sb_info *sbi)
 }
 #endif
 
-static int erofs_mkfs_handle_directory(struct erofs_importer *im, struct erofs_inode *dir)
+static int erofs_mkfs_import_localdir(struct erofs_importer *im, struct erofs_inode *dir,
+				      u64 *nr_subdirs, unsigned int *i_nlink)
 {
+	unsigned int __nlink;
+	u64 __nr_subdirs;
 	DIR *_dir;
-	struct dirent *dp;
-	struct erofs_dentry *d;
-	unsigned int nr_subdirs, i_nlink;
 	int ret;
 
 	_dir = opendir(dir->i_srcpath);
 	if (!_dir) {
+		ret = -errno;
 		erofs_err("failed to opendir at %s: %s",
-			  dir->i_srcpath, erofs_strerror(-errno));
-		return -errno;
+			  dir->i_srcpath, erofs_strerror(ret));
+		return ret;
 	}
 
-	nr_subdirs = 0;
-	i_nlink = 0;
+	__nr_subdirs = *nr_subdirs;
+	__nlink = *i_nlink;
 	while (1) {
-		char buf[PATH_MAX];
 		struct erofs_inode *inode;
+		struct erofs_dentry *d;
+		char buf[PATH_MAX];
+		struct dirent *dp;
 
 		/*
 		 * set errno to 0 before calling readdir() in order to
@@ -1636,16 +1639,14 @@ static int erofs_mkfs_handle_directory(struct erofs_importer *im, struct erofs_i
 		errno = 0;
 		dp = readdir(_dir);
 		if (!dp) {
-			if (!errno)
-				break;
 			ret = -errno;
+			if (!ret)
+				break;
 			goto err_closedir;
 		}
 
-		if (is_dot_dotdot(dp->d_name)) {
-			++i_nlink;
+		if (is_dot_dotdot(dp->d_name))
 			continue;
-		}
 
 		/* skip if it's a exclude file */
 		if (erofs_is_exclude_path(dir->i_srcpath, dp->d_name))
@@ -1668,32 +1669,15 @@ static int erofs_mkfs_handle_directory(struct erofs_importer *im, struct erofs_i
 		}
 		d->inode = inode;
 		d->type = erofs_mode_to_ftype(inode->i_mode);
-		i_nlink += S_ISDIR(inode->i_mode);
+		__nlink += S_ISDIR(inode->i_mode);
 		erofs_dbg("file %s added (type %u)", buf, d->type);
-		nr_subdirs++;
+		__nr_subdirs++;
 	}
 	closedir(_dir);
 
-	ret = erofs_prepare_dir_file(im, dir, nr_subdirs); /* sort subdirs */
-	if (ret)
-		return ret;
-
-	/*
-	 * if there're too many subdirs as compact form, set nlink=1
-	 * rather than upgrade to use extented form instead if possible.
-	 */
-	if (i_nlink > USHRT_MAX &&
-	    dir->inode_isize == sizeof(struct erofs_inode_compact)) {
-		if (dir->dot_omitted)
-			dir->inode_isize = sizeof(struct erofs_inode_extended);
-		else
-			dir->i_nlink = 1;
-	} else {
-		dir->i_nlink = i_nlink;
-	}
-
-	return erofs_mkfs_go(im, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
-
+	*nr_subdirs = __nr_subdirs;
+	*i_nlink = __nlink;
+	return 0;
 err_closedir:
 	closedir(_dir);
 	return ret;
@@ -1728,13 +1712,15 @@ static void erofs_dentry_kill(struct erofs_dentry *d)
 	free(d);
 }
 
-static int erofs_rebuild_handle_directory(struct erofs_importer *im,
-					  struct erofs_inode *dir,
-					  bool incremental)
+static int erofs_mkfs_handle_directory(struct erofs_importer *im,
+				       struct erofs_inode *dir,
+				       bool rebuild,
+				       bool incremental)
 {
 	struct erofs_sb_info *sbi = im->sbi;
 	struct erofs_dentry *d, *n;
-	unsigned int nr_subdirs, i_nlink;
+	unsigned int i_nlink;
+	u64 nr_subdirs;
 	bool delwht = im->params->ovlfs_strip && dir->whiteouts;
 	int ret;
 
@@ -1755,6 +1741,14 @@ static int erofs_rebuild_handle_directory(struct erofs_importer *im,
 		i_nlink += (d->type == EROFS_FT_DIR);
 		++nr_subdirs;
 	}
+
+	if (!rebuild) {
+		ret = erofs_mkfs_import_localdir(im, dir,
+						 &nr_subdirs, &i_nlink);
+		if (ret)
+			return ret;
+	}
+
 	DBG_BUGON(nr_subdirs + 2 < i_nlink);
 	ret = erofs_prepare_dir_file(im, dir, nr_subdirs);
 	if (ret)
@@ -1763,15 +1757,18 @@ static int erofs_rebuild_handle_directory(struct erofs_importer *im,
 	if (IS_ROOT(dir) && incremental)
 		dir->datalayout = EROFS_INODE_FLAT_PLAIN;
 
+	dir->i_nlink = i_nlink;
 	/*
 	 * if there're too many subdirs as compact form, set nlink=1
-	 * rather than upgrade to use extented form instead.
+	 * rather than upgrade to use extented form instead if possible.
 	 */
 	if (i_nlink > USHRT_MAX &&
-	    dir->inode_isize == sizeof(struct erofs_inode_compact))
-		dir->i_nlink = 1;
-	else
-		dir->i_nlink = i_nlink;
+	    dir->inode_isize == sizeof(struct erofs_inode_compact)) {
+		if (dir->dot_omitted)
+			dir->inode_isize = sizeof(struct erofs_inode_extended);
+		else
+			dir->i_nlink = 1;
+	}
 
 	return erofs_mkfs_go(im, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
 }
@@ -1814,7 +1811,7 @@ static int erofs_mkfs_handle_inode(struct erofs_importer *im,
 		}
 		ret = erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
 	} else {
-		ret = erofs_mkfs_handle_directory(im, inode);
+		ret = erofs_mkfs_handle_directory(im, inode, false, false);
 	}
 	erofs_info("file /%s dumped (mode %05o)", relpath, inode->i_mode);
 	return ret;
@@ -1880,7 +1877,7 @@ static int erofs_rebuild_handle_inode(struct erofs_importer *im,
 		}
 		ret = erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
 	} else {
-		ret = erofs_rebuild_handle_directory(im, inode, incremental);
+		ret = erofs_mkfs_handle_directory(im, inode, true, incremental);
 	}
 	erofs_info("file %s dumped (mode %05o)", erofs_fspath(inode->i_srcpath),
 		   inode->i_mode);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: mkfs: combine erofs_{mkfs,rebuild}_handle_inode

- Remove redundant logic;

- In preparation for incremental builds on local paths.

---
 lib/inode.c | 107 +++++++++++++++++++++-------------------------------
 1 file changed, 42 insertions(+), 65 deletions(-)
lib/inode.c +42 -65
index 2918791..6148b86 100644
@@ -1323,6 +1323,7 @@ static struct erofs_inode *erofs_iget_from_local(struct erofs_importer *im,
 		erofs_iput(inode);
 		return ERR_PTR(ret);
 	}
+	inode->datasource = EROFS_INODE_DATA_SOURCE_LOCALPATH;
 	return inode;
 }
 
@@ -1773,58 +1774,48 @@ static int erofs_mkfs_handle_directory(struct erofs_importer *im,
 	return erofs_mkfs_go(im, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
 }
 
-static int erofs_mkfs_handle_inode(struct erofs_importer *im,
-				   struct erofs_inode *inode)
+static int erofs_mkfs_begin_nondirectory(struct erofs_importer *im,
+					 struct erofs_inode *inode)
 {
-	const char *relpath = erofs_fspath(inode->i_srcpath);
-	char *trimmed;
-	int ret;
-
-	trimmed = erofs_trim_for_progressinfo(relpath[0] ? relpath : "/",
-					      sizeof("Processing  ...") - 1);
-	erofs_update_progressinfo("Processing %s ...", trimmed);
-	free(trimmed);
-
-	ret = erofs_scan_file_xattrs(inode);
-	if (ret < 0)
-		return ret;
+	struct erofs_mkfs_job_ndir_ctx ctx =
+		{ .inode = inode, .fd = -1 };
 
-	ret = erofs_prepare_xattr_ibody(inode, false);
-	if (ret < 0)
-		return ret;
-
-	if (!S_ISDIR(inode->i_mode)) {
-		struct erofs_mkfs_job_ndir_ctx ctx = { .inode = inode, .fd = -1 };
-
-		if (!S_ISLNK(inode->i_mode) && inode->i_size) {
+	if (S_ISREG(inode->i_mode) && inode->i_size) {
+		switch (inode->datasource) {
+		case EROFS_INODE_DATA_SOURCE_DISKBUF:
+			ctx.fd = erofs_diskbuf_getfd(inode->i_diskbuf, &ctx.fpos);
+			if (ctx.fd < 0)
+				return ctx.fd;
+			break;
+		case EROFS_INODE_DATA_SOURCE_LOCALPATH:
 			ctx.fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
 			if (ctx.fd < 0)
 				return -errno;
-
-			if (cfg.c_compr_opts[0].alg &&
-			    erofs_file_is_compressible(im, inode)) {
-				ctx.ictx = erofs_begin_compressed_file(im,
-							inode, ctx.fd, 0);
-				if (IS_ERR(ctx.ictx))
-					return PTR_ERR(ctx.ictx);
-			}
+			__erofs_fallthrough;
+		default:
+			break;
+		}
+		if (cfg.c_compr_opts[0].alg &&
+		    erofs_file_is_compressible(im, inode)) {
+			ctx.ictx = erofs_begin_compressed_file(im, inode,
+							ctx.fd, ctx.fpos);
+			if (IS_ERR(ctx.ictx))
+				return PTR_ERR(ctx.ictx);
 		}
-		ret = erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
-	} else {
-		ret = erofs_mkfs_handle_directory(im, inode, false, false);
 	}
-	erofs_info("file /%s dumped (mode %05o)", relpath, inode->i_mode);
-	return ret;
+	return erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
 }
 
-static int erofs_rebuild_handle_inode(struct erofs_importer *im,
-				    struct erofs_inode *inode, bool incremental)
+static int erofs_mkfs_handle_inode(struct erofs_importer *im,
+				   struct erofs_inode *inode,
+				   bool rebuild, bool incremental)
 {
+	const char *relpath = erofs_fspath(inode->i_srcpath);
 	const struct erofs_importer_params *params = im->params;
 	char *trimmed;
 	int ret;
 
-	trimmed = erofs_trim_for_progressinfo(erofs_fspath(inode->i_srcpath),
+	trimmed = erofs_trim_for_progressinfo(*relpath ? relpath : "/",
 					      sizeof("Processing  ...") - 1);
 	erofs_update_progressinfo("Processing %s ...", trimmed);
 	free(trimmed);
@@ -1847,6 +1838,12 @@ static int erofs_rebuild_handle_inode(struct erofs_importer *im,
 			return ret;
 	}
 
+	if (!rebuild) {
+		ret = erofs_scan_file_xattrs(inode);
+		if (ret < 0)
+			return ret;
+	}
+
 	/* strip all unnecessary overlayfs xattrs when ovlfs_strip is enabled */
 	if (params->ovlfs_strip)
 		erofs_clear_opaque_xattr(inode);
@@ -1858,28 +1855,12 @@ static int erofs_rebuild_handle_inode(struct erofs_importer *im,
 		return ret;
 
 	if (!S_ISDIR(inode->i_mode)) {
-		struct erofs_mkfs_job_ndir_ctx ctx =
-			{ .inode = inode, .fd = -1 };
-
-		if (S_ISREG(inode->i_mode) && inode->i_size &&
-		    inode->datasource == EROFS_INODE_DATA_SOURCE_DISKBUF) {
-			ctx.fd = erofs_diskbuf_getfd(inode->i_diskbuf, &ctx.fpos);
-			if (ctx.fd < 0)
-				return ret;
-
-			if (cfg.c_compr_opts[0].alg &&
-			    erofs_file_is_compressible(im, inode)) {
-				ctx.ictx = erofs_begin_compressed_file(im, inode,
-							ctx.fd, ctx.fpos);
-				if (IS_ERR(ctx.ictx))
-					return PTR_ERR(ctx.ictx);
-			}
-		}
-		ret = erofs_mkfs_go(im, EROFS_MKFS_JOB_NDIR, &ctx, sizeof(ctx));
+		ret = erofs_mkfs_begin_nondirectory(im, inode);
 	} else {
-		ret = erofs_mkfs_handle_directory(im, inode, true, incremental);
+		ret = erofs_mkfs_handle_directory(im, inode,
+						  rebuild, incremental);
 	}
-	erofs_info("file %s dumped (mode %05o)", erofs_fspath(inode->i_srcpath),
+	erofs_info("file %s dumped (mode %05o)", *relpath ? relpath : "/",
 		   inode->i_mode);
 	return ret;
 }
@@ -1921,8 +1902,7 @@ static int erofs_mkfs_dump_tree(struct erofs_importer *im, bool rebuild,
 		root->xattr_isize = cfg.c_root_xattr_isize;
 	}
 
-	err = !rebuild ? erofs_mkfs_handle_inode(im, root) :
-			erofs_rebuild_handle_inode(im, root, incremental);
+	err = erofs_mkfs_handle_inode(im, root, rebuild, incremental);
 	if (err)
 		return err;
 
@@ -1952,11 +1932,8 @@ static int erofs_mkfs_dump_tree(struct erofs_importer *im, bool rebuild,
 					  erofs_parent_inode(inode) != dir);
 				erofs_mark_parent_inode(inode, dir);
 
-				if (!rebuild)
-					err = erofs_mkfs_handle_inode(im, inode);
-				else
-					err = erofs_rebuild_handle_inode(im,
-							inode, incremental);
+				err = erofs_mkfs_handle_inode(im, inode,
+							rebuild, incremental);
 				if (err)
 					break;
 				if (S_ISDIR(inode->i_mode)) {
Signature · 1 line
2.43.5