[PATCH v1] erofs-utils:code clean of write file

Li Guifu blucerlee at gmail.com
Thu Dec 19 02:52:37 AEDT 2019


From: Li Guifu <bluce.liguifu at huawei.com>

Make a code clean at function erofs_write_file() which
has multi jump.

Signed-off-by: Li Guifu <blucerlee at gmail.com>
---
 lib/inode.c | 63 ++++++++++++++++++++++++++---------------------------
 1 file changed, 31 insertions(+), 32 deletions(-)

diff --git a/lib/inode.c b/lib/inode.c
index 0e19b11..052315a 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -302,22 +302,10 @@ static bool erofs_file_is_compressible(struct erofs_inode *inode)
 	return true;
 }
 
-int erofs_write_file(struct erofs_inode *inode)
+int erofs_write_file_by_fd(int fd, struct erofs_inode *inode)
 {
+	int ret;
 	unsigned int nblocks, i;
-	int ret, fd;
-
-	if (!inode->i_size) {
-		inode->datalayout = EROFS_INODE_FLAT_PLAIN;
-		return 0;
-	}
-
-	if (cfg.c_compr_alg_master && erofs_file_is_compressible(inode)) {
-		ret = erofs_write_compressed_file(inode);
-
-		if (!ret || ret != -ENOSPC)
-			return ret;
-	}
 
 	/* fallback to all data uncompressed */
 	inode->datalayout = EROFS_INODE_FLAT_INLINE;
@@ -327,47 +315,58 @@ int erofs_write_file(struct erofs_inode *inode)
 	if (ret)
 		return ret;
 
-	fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
-	if (fd < 0)
-		return -errno;
-
 	for (i = 0; i < nblocks; ++i) {
 		char buf[EROFS_BLKSIZ];
 
 		ret = read(fd, buf, EROFS_BLKSIZ);
-		if (ret != EROFS_BLKSIZ) {
-			if (ret < 0)
-				goto fail;
-			close(fd);
-			return -EAGAIN;
-		}
+		if (ret != EROFS_BLKSIZ)
+			return -errno;
 
 		ret = blk_write(buf, inode->u.i_blkaddr + i, 1);
 		if (ret)
-			goto fail;
+			return ret;
 	}
 
 	/* read the tail-end data */
 	inode->idata_size = inode->i_size % EROFS_BLKSIZ;
 	if (inode->idata_size) {
 		inode->idata = malloc(inode->idata_size);
-		if (!inode->idata) {
-			close(fd);
+		if (!inode->idata)
 			return -ENOMEM;
-		}
 
 		ret = read(fd, inode->idata, inode->idata_size);
 		if (ret < inode->idata_size) {
 			free(inode->idata);
 			inode->idata = NULL;
-			close(fd);
 			return -EIO;
 		}
 	}
-	close(fd);
+
 	return 0;
-fail:
-	ret = -errno;
+}
+
+int erofs_write_file(struct erofs_inode *inode)
+{
+	int ret, fd;
+
+	if (!inode->i_size) {
+		inode->datalayout = EROFS_INODE_FLAT_PLAIN;
+		return 0;
+	}
+
+	if (cfg.c_compr_alg_master && erofs_file_is_compressible(inode)) {
+		ret = erofs_write_compressed_file(inode);
+
+		if (!ret || ret != -ENOSPC)
+			return ret;
+	}
+
+	fd = open(inode->i_srcpath, O_RDONLY | O_BINARY);
+	if (fd < 0)
+		return -errno;
+
+	ret = erofs_write_file_by_fd(fd, inode);
+
 	close(fd);
 	return ret;
 }
-- 
2.17.1



More information about the Linux-erofs mailing list