From: Alexandre Oliva <oliva@gnu.org>
To: linux-btrfs@vger.kernel.org
Subject: btrfs: add -k option to filesystem df
Date: Sat, 30 Aug 2014 15:27:00 -0300 [thread overview]
Message-ID: <or61h94yvf.fsf@free.home> (raw)
Introduce support for df to print sizes in KiB, easy to extend to other
bases.
The man page is also updated and fixed in that it made it seem like
multiple paths were accepted.
Signed-off-by: Alexandre Oliva <oliva@gnu.org>
---
Documentation/btrfs-filesystem.txt | 4 +++-
cmds-filesystem.c | 26 +++++++++++++++++++++++---
utils.c | 29 +++++++++++++++++++++++++++--
utils.h | 1 +
4 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/Documentation/btrfs-filesystem.txt b/Documentation/btrfs-filesystem.txt
index c9c0b00..70ba4b8 100644
--- a/Documentation/btrfs-filesystem.txt
+++ b/Documentation/btrfs-filesystem.txt
@@ -17,8 +17,10 @@ resizing, defragment.
SUBCOMMAND
----------
-*df* <path> [<path>...]::
+*df* [--kbytes] <path>::
Show space usage information for a mount point.
++
+If '-k' or '--kbytes' is passed, sizes will be printed in KiB.
*show* [--mounted|--all-devices|<path>|<uuid>|<device>|<label>]::
Show the btrfs filesystem with some additional info.
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 7e8ca95..737fcf3 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -113,8 +113,9 @@ static const char * const filesystem_cmd_group_usage[] = {
};
static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
+ "btrfs filesystem df [-k] <path>",
"Show space usage information for a mount point",
+ "-k|--kbytes show disk spaces in KB",
NULL
};
@@ -226,10 +227,29 @@ static int cmd_df(int argc, char **argv)
char *path;
DIR *dirstream = NULL;
- if (check_argc_exact(argc, 2))
+ while (1) {
+ int long_index;
+ static struct option long_options[] = {
+ { "kbytes", no_argument, NULL, 'k'},
+ { NULL, no_argument, NULL, 0 },
+ };
+ int c = getopt_long(argc, argv, "k", long_options,
+ &long_index);
+ if (c < 0)
+ break;
+ switch (c) {
+ case 'k':
+ pretty_size_force_base (1024);
+ break;
+ default:
+ usage(cmd_df_usage);
+ }
+ }
+
+ if (check_argc_max(argc, optind + 1))
usage(cmd_df_usage);
- path = argv[1];
+ path = argv[optind];
fd = open_file_or_dir(path, &dirstream);
if (fd < 0) {
diff --git a/utils.c b/utils.c
index 6c09366..f760d1b 100644
--- a/utils.c
+++ b/utils.c
@@ -1377,19 +1377,43 @@ out:
}
static char *size_strs[] = { "", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
+u64 forced_base = 0;
+int pretty_size_force_base(u64 base)
+{
+ u64 check = 1;
+ while (check < base)
+ check *= 1024;
+ if (check != base && base)
+ return -1;
+ forced_base = base;
+ return 0;
+}
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
{
int num_divs = 0;
+ u64 last_size = size;
float fraction;
if (str_bytes == 0)
return 0;
- if( size < 1024 ){
+ if( forced_base ){
+ u64 base = forced_base;
+ while (base > 1) {
+ base /= 1024;
+ last_size = size;
+ size /= 1024;
+ num_divs++;
+ }
+ if (num_divs < 2)
+ return snprintf(str, str_bytes, "%llu%s",
+ (unsigned long long)size,
+ size_strs[num_divs]);
+ goto check;
+ } else if( size < 1024 ){
fraction = size;
num_divs = 0;
} else {
- u64 last_size = size;
num_divs = 0;
while(size >= 1024){
last_size = size;
@@ -1397,6 +1421,7 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
num_divs ++;
}
+ check:
if (num_divs >= ARRAY_SIZE(size_strs)) {
str[0] = '\0';
return -1;
diff --git a/utils.h b/utils.h
index fd25126..bbcb042 100644
--- a/utils.h
+++ b/utils.h
@@ -71,6 +71,7 @@ int check_mounted_where(int fd, const char *file, char *where, int size,
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
int super_offset);
+int pretty_size_force_base(u64 base);
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
#define pretty_size(size) \
({ \
--
Alexandre Oliva, freedom fighter http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/ FSF Latin America board member
Free Software Evangelist|Red Hat Brasil GNU Toolchain Engineer
next reply other threads:[~2014-08-30 18:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-30 18:27 Alexandre Oliva [this message]
2014-08-31 2:17 ` btrfs: add -k option to filesystem df Shriramana Sharma
[not found] ` <CAH-HCWX49Rw6psFtbUP+ohWMQVBLuOYLwBz0q46hvrGytJzhCA@mail.gmail.com>
2014-08-31 18:07 ` Alexandre Oliva
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=or61h94yvf.fsf@free.home \
--to=oliva@gnu.org \
--cc=linux-btrfs@vger.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).