From: Guo Xuenan <guoxuenan@huawei.com>
To: <xiang@kernel.org>, <linux-erofs@lists.ozlabs.org>
Cc: daeho43@gmail.com, mpiglet@outlook.com
Subject: [PATCH 2/3] erofs-utils: dump: add -i options to dump file information of specific inode number
Date: Wed, 27 Oct 2021 20:03:50 +0800 [thread overview]
Message-ID: <20211027120351.2885966-2-guoxuenan@huawei.com> (raw)
In-Reply-To: <20211027120351.2885966-1-guoxuenan@huawei.com>
From: Wang Qi <mpiglet@outlook.com>
Signed-off-by: Guo Xuenan <guoxuenan@huawei.com>
Signed-off-by: Wang Qi <mpiglet@outlook.com>
---
dump/main.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 134 insertions(+), 1 deletion(-)
diff --git a/dump/main.c b/dump/main.c
index 98ee68b..8a31345 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -17,8 +17,10 @@
struct erofsdump_cfg {
unsigned int totalshow;
+ bool show_inode;
bool show_superblock;
bool show_statistics;
+ u64 ino;
};
static struct erofsdump_cfg dumpcfg;
@@ -82,6 +84,9 @@ static struct erofsdump_feature feature_lists[] = {
};
static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid);
+static inline int erofs_checkdirent(struct erofs_dirent *de,
+ struct erofs_dirent *last_de,
+ u32 maxsize, const char *dname);
static void usage(void)
{
@@ -89,6 +94,7 @@ static void usage(void)
"Dump erofs layout from IMAGE, and [options] are:\n"
" -S show statistic information of the image\n"
" -V print the version number of dump.erofs and exit.\n"
+ " -i # show the target inode info of nid #\n"
" -s show information about superblock\n"
" --help display this help and exit.\n",
stderr);
@@ -102,10 +108,17 @@ static void erofsdump_print_version(void)
static int erofsdump_parse_options_cfg(int argc, char **argv)
{
int opt;
+ u64 i;
- while ((opt = getopt_long(argc, argv, "SVs",
+ while ((opt = getopt_long(argc, argv, "SVi:s",
long_options, NULL)) != -1) {
switch (opt) {
+ case 'i':
+ i = atoll(optarg);
+ dumpcfg.show_inode = true;
+ dumpcfg.ino = i;
+ ++dumpcfg.totalshow;
+ break;
case 's':
dumpcfg.show_superblock = true;
++dumpcfg.totalshow;
@@ -331,6 +344,123 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
return 0;
}
+static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
+ erofs_nid_t target, char *path, unsigned int pos)
+{
+ int err;
+ struct erofs_inode inode = {.nid = nid};
+ erofs_off_t offset;
+ char buf[EROFS_BLKSIZ];
+
+ path[pos++] = '/';
+ if (target == sbi.root_nid)
+ return 0;
+
+ err = erofs_read_inode_from_disk(&inode);
+ if (err) {
+ erofs_err("read inode %lu failed", nid);
+ return err;
+ }
+
+ offset = 0;
+ while (offset < inode.i_size) {
+ erofs_off_t maxsize = min_t(erofs_off_t,
+ inode.i_size - offset, EROFS_BLKSIZ);
+ struct erofs_dirent *de = (void *)buf;
+ struct erofs_dirent *end;
+ unsigned int nameoff;
+
+ err = erofs_pread(&inode, buf, maxsize, offset);
+ if (err)
+ return err;
+
+ nameoff = le16_to_cpu(de->nameoff);
+ end = (void *)buf + nameoff;
+ while (de < end) {
+ const char *dname;
+ int len;
+
+ nameoff = le16_to_cpu(de->nameoff);
+ dname = (char *)buf + nameoff;
+ len = erofs_checkdirent(de, end, maxsize, dname);
+ if (len < 0)
+ return len;
+
+ if (de->nid == target) {
+ memcpy(path + pos, dname, len);
+ return 0;
+ }
+
+ if (de->file_type == EROFS_FT_DIR &&
+ de->nid != parent_nid &&
+ de->nid != nid) {
+ memcpy(path + pos, dname, len);
+ err = erofs_get_pathname(de->nid, nid,
+ target, path, pos + len);
+ if (!err)
+ return 0;
+ memset(path + pos, 0, len);
+ }
+ ++de;
+ }
+ offset += maxsize;
+ }
+ return -1;
+}
+
+static void dumpfs_print_inode(void)
+{
+ int err;
+ erofs_off_t size;
+ u16 access_mode;
+ time_t t;
+ erofs_nid_t nid = dumpcfg.ino;
+ struct erofs_inode inode = {.nid = nid};
+ char path[PATH_MAX + 1] = {0};
+ char access_mode_str[] = "rwxrwxrwx";
+ char timebuf[128] = {0};
+
+ err = erofs_read_inode_from_disk(&inode);
+ if (err) {
+ erofs_err("read inode %lu from disk failed", nid);
+ return;
+ }
+
+ err = erofs_get_occupied_size(&inode, &size);
+ if (err) {
+ erofs_err("get file size failed\n");
+ return;
+ }
+
+ err = erofs_get_pathname(sbi.root_nid, sbi.root_nid, nid, path, 0);
+ if (err < 0) {
+ fprintf(stderr, "File: Path not found.\n");
+ return;
+ }
+
+ fprintf(stdout, "File : %s\n", path);
+ fprintf(stdout, "Inode: %lu ", inode.nid);
+ fprintf(stdout, "Links: %u ", inode.i_nlink);
+ fprintf(stdout, "Layout: %d\n", inode.datalayout);
+ fprintf(stdout, "File size: %lu ", inode.i_size);
+ fprintf(stdout, "On-disk size: %lu ", size);
+ fprintf(stdout, "Compress rate: %.2f%%\n",
+ (double)(100 * size) / (double)(inode.i_size));
+ fprintf(stdout, "Inode size: %d ", inode.inode_isize);
+ fprintf(stdout, "Extent size: %u ", inode.extent_isize);
+ fprintf(stdout, "Xattr size: %u\n", inode.xattr_isize);
+ access_mode = inode.i_mode & 0777;
+ t = inode.i_ctime;
+ strftime(timebuf, sizeof(timebuf),
+ "%Y-%m-%d %H:%M:%S", localtime(&t));
+ for (int i = 8; i >= 0; i--)
+ if (((access_mode >> i) & 1) == 0)
+ access_mode_str[8 - i] = '-';
+ fprintf(stdout, "Uid: %u Gid: %u ", inode.i_uid, inode.i_gid);
+ fprintf(stdout, "Access: %04o/%s\n", access_mode, access_mode_str);
+ fprintf(stdout, "Birth : %s\n", timebuf);
+}
+
static void erofsdump_print_chart_row(char *col1, unsigned int col2,
double col3, char *col4)
{
@@ -512,6 +642,9 @@ int main(int argc, char **argv)
if (dumpcfg.show_statistics)
erofsdump_print_statistic();
+ if (dumpcfg.show_inode)
+ dumpfs_print_inode();
+
exit:
erofs_exit_configure();
return err;
--
2.31.1
next prev parent reply other threads:[~2021-10-27 11:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-27 12:03 [PATCH 1/3] erofs-utils: dump: add -S options for collecting filesystem statistics Guo Xuenan
2021-10-27 12:03 ` Guo Xuenan [this message]
2021-10-27 12:23 ` [PATCH 2/3] erofs-utils: dump: add -i options to dump file information of specific inode number Gao Xiang
2021-10-27 12:03 ` [PATCH 3/3] erofs-utils: dump: add -e option to dump the inode extent info Guo Xuenan
2021-10-27 12:29 ` Gao Xiang
2021-10-27 12:17 ` [PATCH 1/3] erofs-utils: dump: add -S options for collecting filesystem statistics Gao Xiang
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=20211027120351.2885966-2-guoxuenan@huawei.com \
--to=guoxuenan@huawei.com \
--cc=daeho43@gmail.com \
--cc=linux-erofs@lists.ozlabs.org \
--cc=mpiglet@outlook.com \
--cc=xiang@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).