From: Li Guifu <blucerlee@gmail.com>
To: linux-erofs@lists.ozlabs.org
Subject: [PATCH v1] erofs-utils:code clean of write file
Date: Wed, 18 Dec 2019 23:52:37 +0800 [thread overview]
Message-ID: <20191218155237.2222-1-blucerlee@gmail.com> (raw)
From: Li Guifu <bluce.liguifu@huawei.com>
Make a code clean at function erofs_write_file() which
has multi jump.
Signed-off-by: Li Guifu <blucerlee@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
next reply other threads:[~2019-12-18 15:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-18 15:52 Li Guifu [this message]
2019-12-19 18:19 ` [PATCH v1] erofs-utils:code clean of write file Gao Xiang via Linux-erofs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191218155237.2222-1-blucerlee@gmail.com \
--to=blucerlee@gmail.com \
--cc=linux-erofs@lists.ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.