From 872975b0ab7208c60c1a46be38e58642f146d9e6 Mon Sep 17 00:00:00 2001 From: bernard Date: Sun, 31 Dec 2017 10:46:24 +0800 Subject: [PATCH] [DFS] add df cmd in msh and provide better display. re-write for #1138 --- components/dfs/src/dfs_fs.c | 16 ++++++++++++++-- components/finsh/msh_cmd.c | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index f6c210ddb0..c1f31a2f8b 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -540,9 +540,13 @@ FINSH_FUNCTION_EXPORT(mkfs, make a file system); int df(const char *path) { int result; + int minor = 0; long long cap; struct statfs buffer; + int unit_index = 0; + char *unit_str[] = {"KB", "MB", "GB"}; + result = dfs_statfs(path ? path : NULL, &buffer); if (result != 0) { @@ -551,8 +555,16 @@ int df(const char *path) } cap = buffer.f_bsize * buffer.f_bfree / 1024; - rt_kprintf("disk free: %d KB [ %d block, %d bytes per block ]\n", - (unsigned long)cap, buffer.f_bfree, buffer.f_bsize); + for (unit_index = 0; unit_index < 3; unit_index ++) + { + if (cap < 1024) break; + + minor = (cap % 1024) * 10 / 1024; /* only one decimal point */ + cap = cap / 1024; + } + + rt_kprintf("disk free: %d.%d %s [ %d block, %d bytes per block ]\n", + (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize); return 0; } FINSH_FUNCTION_EXPORT(df, get disk free); diff --git a/components/finsh/msh_cmd.c b/components/finsh/msh_cmd.c index b5881d897a..f184f1f33d 100644 --- a/components/finsh/msh_cmd.c +++ b/components/finsh/msh_cmd.c @@ -255,6 +255,29 @@ int cmd_mkfs(int argc, char **argv) } FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system); +extern int df(const char *path); +int cmd_df(int argc, char** argv) +{ + if (argc != 2) + { + df("/"); + } + else + { + if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) + { + rt_kprintf("df [path]\n"); + } + else + { + df(argv[1]); + } + } + + return 0; +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_df, __cmd_df, disk free); + int cmd_echo(int argc, char** argv) { if (argc == 2)