[PATCH v3 2/2] erofs-utils: lib: Add API to get on disk size of an inode
Kelvin Zhang
zhangkelvin at google.com
Wed Dec 22 12:54:19 AEDT 2021
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>
---
dump/main.c | 4 ++--
include/erofs/internal.h | 23 +++++++++++++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/dump/main.c b/dump/main.c
index 71b44b4..4c2c513 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -175,7 +175,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;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
return err;
}
- err = erofs_get_occupied_size(&inode, &occupied_size);
+ err = erofsdump_get_occupied_size(&inode, &occupied_size);
if (err) {
erofs_err("get file size failed\n");
return err;
diff --git a/include/erofs/internal.h b/include/erofs/internal.h
index 947304f..c64cf36 100644
--- a/include/erofs/internal.h
+++ b/include/erofs/internal.h
@@ -19,6 +19,7 @@ typedef unsigned short umode_t;
#define __packed __attribute__((__packed__))
+#include "erofs/print.h"
#include "erofs_fs.h"
#include <fcntl.h>
@@ -320,6 +321,28 @@ 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:
+ erofs_err("unknown datalayout");
+ return -1;
+ }
+ return 0;
+}
+
/* zmap.c */
int z_erofs_fill_inode(struct erofs_inode *vi);
int z_erofs_map_blocks_iter(struct erofs_inode *vi,
--
2.34.1.448.ga2b2bfdf31-goog
More information about the Linux-erofs
mailing list