cluster: STABLE3 - GFS2: Add human readable output to gfs2_tool df

Bob Peterson rpeterso@fedoraproject.org
Fri Jan 23 20:07:00 GMT 2009


Gitweb:        http://git.fedorahosted.org/git/cluster.git?p=cluster.git;a=commitdiff;h=c0a2d5b73035d5d37aa179956cff877c90a5bf4e
Commit:        c0a2d5b73035d5d37aa179956cff877c90a5bf4e
Parent:        6834a0bf614b62e2006c0aa4c333954279dfefa8
Author:        Bob Peterson <rpeterso@redhat.com>
AuthorDate:    Fri Jan 23 13:57:58 2009 -0600
Committer:     Bob Peterson <rpeterso@redhat.com>
CommitterDate: Fri Jan 23 14:07:19 2009 -0600

GFS2: Add human readable output to gfs2_tool df

bz #480833

Like gfs_tool, I added two new parameters to gfs2_tool's
df option.  With -k, the values are printed in kilobytes.
With -H, the values are printed in human-readable format
much like df -h.
---
 gfs2/tool/df.c        |   74 +++++++++++++++++++++++++++++++++++++++++++------
 gfs2/tool/gfs2_tool.h |    6 +++-
 gfs2/tool/main.c      |   12 +++++++-
 3 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/gfs2/tool/df.c b/gfs2/tool/df.c
index 8174a92..cd3bfaa 100644
--- a/gfs2/tool/df.c
+++ b/gfs2/tool/df.c
@@ -19,6 +19,53 @@
 #include "libgfs2.h"
 
 /**
+ * anthropomorphize - make a uint64_t number more human
+ */
+static const char *anthropomorphize(unsigned long long inhuman_value)
+{
+	const char *symbols = " KMGTPE";
+	int i;
+	unsigned long long val = inhuman_value;
+	static char out_val[32];
+
+	memset(out_val, 0, sizeof(out_val));
+	for (i = 0; i < 6 && val > 1024; i++)
+		val /= 1024;
+	sprintf(out_val, "%llu%c", val, symbols[i]);
+	return out_val;
+}
+
+/**
+ * printit - parse out and print values according to the output type
+ */
+static void printit(unsigned long long block_size, const char *label,
+		    unsigned long long total, unsigned long long used,
+		    unsigned long long free, unsigned int percentage)
+{
+	switch (output_type) {
+	case OUTPUT_BLOCKS:
+		printf("  %-15s%-15llu%-15llu%-15llu%u%%\n",
+		       label, total, used, free, percentage);
+		break;
+	case OUTPUT_K:
+		printf("  %-15s%-15llu%-15llu%-15llu%u%%\n",
+		       label, (total * block_size) / 1024,
+		       (used * block_size) / 1024, (free * block_size) / 1024,
+		       percentage);
+		break;
+	case OUTPUT_HUMAN:
+		/* Need to do three separate printfs here because function
+		   anthropomorphize re-uses the same static space. */
+		printf("  %-15s%-15s", label,
+		       anthropomorphize(total * block_size));
+		printf("%-15s", anthropomorphize(used * block_size));
+		printf("%-15s%u%%\n", anthropomorphize(free * block_size),
+		       percentage);
+		break;
+	}
+}
+
+/**
  * do_df_one - print out information about one filesystem
  * @path: the path to the filesystem
  *
@@ -113,23 +160,32 @@ do_df_one(char *path)
 	cleanup_metafs(&sbd);
 
 	printf("\n");
-	printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total", "Used", "Free", "use%");
+	switch (output_type) {
+	case OUTPUT_BLOCKS:
+		printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total Blocks",
+		       "Used Blocks", "Free Blocks", "use%");
+		break;
+	case OUTPUT_K:
+		printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total K",
+		       "Used K", "Free K", "use%");
+		break;
+	case OUTPUT_HUMAN:
+		printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total",
+		       "Used", "Free", "use%");
+		break;
+	}
 	printf("  ------------------------------------------------------------------------\n");
 
 	percentage = sc.sc_total ?
 		(100.0 * (sc.sc_total - sc.sc_free)) / sc.sc_total + 0.5 : 0;
-	printf("  %-15s%-15llu%-15llu%-15llu%u%%\n", "data",
-	       (unsigned long long)sc.sc_total,
-	       (unsigned long long)sc.sc_total - sc.sc_free,
-	       (unsigned long long)sc.sc_free, percentage);
+	printit(sbd.sd_sb.sb_bsize, "data", sc.sc_total,
+		sc.sc_total - sc.sc_free, sc.sc_free, percentage);
 
 	percentage = (sc.sc_dinodes + sc.sc_free) ?
 		(100.0 * sc.sc_dinodes / (sc.sc_dinodes + sc.sc_free)) + 0.5 :
 		0;
-	printf("  %-15s%-15llu%-15llu%-15llu%u%%\n", "inodes",
-	       (unsigned long long)sc.sc_dinodes + sc.sc_free,
-	       (unsigned long long)sc.sc_dinodes,
-	       (unsigned long long)sc.sc_free, percentage);
+	printit(sbd.sd_sb.sb_bsize, "inodes", sc.sc_dinodes + sc.sc_free,
+		sc.sc_dinodes, sc.sc_free, percentage);
 }
 
 
diff --git a/gfs2/tool/gfs2_tool.h b/gfs2/tool/gfs2_tool.h
index b0c7e9a..fa43eb1 100644
--- a/gfs2/tool/gfs2_tool.h
+++ b/gfs2/tool/gfs2_tool.h
@@ -2,6 +2,10 @@
 #define __GFS2_TOOL_DOT_H__
 
 
+#define OUTPUT_BLOCKS 0
+#define OUTPUT_K      1
+#define OUTPUT_HUMAN  2
+
 extern char *prog_name;
 extern char *action;
 extern int override;
@@ -9,7 +13,7 @@ extern int expert;
 extern int debug;
 extern int continuous;
 extern int interval;
-
+extern int output_type;
 
 /* From counters.c */
 
diff --git a/gfs2/tool/main.c b/gfs2/tool/main.c
index d4434f8..b80ca9d 100644
--- a/gfs2/tool/main.c
+++ b/gfs2/tool/main.c
@@ -24,6 +24,7 @@ int expert = FALSE;
 int debug = FALSE;
 int continuous = FALSE;
 int interval = 1;
+int output_type = OUTPUT_BLOCKS;
 
 static const char *usage[] = {
 	"Clear a flag on a inode\n",
@@ -122,8 +123,9 @@ decode_arguments(int argc, char *argv[])
 	int cont = TRUE;
 	int optchar;
 
+	output_type = OUTPUT_BLOCKS;
 	while (cont) {
-		optchar = getopt(argc, argv, "cDhi:OVX");
+		optchar = getopt(argc, argv, "cDhHki:OVX");
 
 		switch (optchar) {
 		case 'c':
@@ -138,10 +140,18 @@ decode_arguments(int argc, char *argv[])
 			print_usage();
 			exit(EXIT_SUCCESS);
 
+		case 'H':
+			output_type = OUTPUT_HUMAN;
+			break;
+
 		case 'i':
 			sscanf(optarg, "%u", &interval);
 			break;
 
+		case 'k':
+			output_type = OUTPUT_K;
+			break;
+
 		case 'O':
 			override = TRUE;
 			break;



More information about the Cluster-cvs mailing list