[PATCH v4] erofs-utils: lib: Add API to get on disk size of an inode

Gao Xiang xiang at kernel.org
Wed Dec 22 13:08:22 AEDT 2021


From: Kelvin Zhang <zhangkelvin at google.com>

Marginally improve code re-use. It's quite common for users to query for
compressed size of an inode.

Signed-off-by: Kelvin Zhang <zhangkelvin at google.com>
Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
Hi Kelvin,

I've removed the print message of API, and return -ENOTSUP instead.
Please check out if it's fine with you.

Thanks,
Gao Xiang

 dump/main.c              |  6 +++---
 include/erofs/internal.h | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/dump/main.c b/dump/main.c
index 97c8750d40f1..9d05d89b4436 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -176,7 +176,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int erofsdump_get_occupied_size(struct erofs_inode *inode,
 		erofs_off_t *size)
 {
 	*size = 0;
@@ -194,7 +194,7 @@ static int erofs_get_occupied_size(struct erofs_inode *inode,
 		break;
 	default:
 		erofs_err("unknown datalayout");
-		return -1;
+		return -ENOTSUP;
 	}
 	return 0;
 }
@@ -270,7 +270,7 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 	stats.files++;
 	stats.file_category_stat[erofs_mode_to_ftype(vi.i_mode)]++;
 
-	err = erofs_get_occupied_size(&vi, &occupied_size);
+	err = erofsdump_get_occupied_size(&vi, &occupied_size);
 	if (err) {
 		erofs_err("get file size failed");
 		return err;
diff --git a/include/erofs/internal.h b/include/erofs/internal.h
index d2adf57d8ae2..2c7b611e811f 100644
--- a/include/erofs/internal.h
+++ b/include/erofs/internal.h
@@ -320,6 +320,27 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+
+static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
+					  erofs_off_t *size)
+{
+	*size = 0;
+	switch (inode->datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+	case EROFS_INODE_CHUNK_BASED:
+		*size = inode->i_size;
+		break;
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		*size = inode->u.i_blocks * EROFS_BLKSIZ;
+		break;
+	default:
+		return -ENOTSUP;
+	}
+	return 0;
+}
+
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
-- 
2.20.1



More information about the Linux-erofs mailing list