[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [lvm-devel] [PATCH 2/3] differentiate between S.I. and binary units in output



On 28.09.2009 18:44, Alasdair G Kergon wrote:
So please would you:
   1) Make this change conditional on a runtime check of
cmd->si_unit_consistency;
Attached, hope it's bug free, somehow all things went crazy and I got very busy this week but don't couldn't wait any longer to get this merged.

   2) Remove the 500-byte sector offering.
Figured, you already did.

The new-style output can be the default from 2.02.54 onwards - Fedora
13, RHEL6 etc.
We still need to tweak the documentation at places, which I can do as soon as the patches are for the actual code handling are applied.

People who find problems with their scripts after an upgrade can easily
reinstate the old inconsistent output by using an lvm.conf setting.
Yep, win win. Good solution in the end anyway although it requires the LVM2 development team to keep "old" code around, which I prefer not to if I can avoid it. ;)

--
Mierswa, Daniel

If you still don't like it, that's ok: that's why I'm boss. I simply know better than you do.
               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22
>From bed43da9ca7f95a9051feeaf7e83b75d706bf703 Mon Sep 17 00:00:00 2001
From: Daniel Mierswa <impulze impulze org>
Date: Sat, 26 Sep 2009 07:09:18 +0200
Subject: [PATCH] differentiate between S.I. and binary units in output

Current behaviour would print identical strings for S.I. and binary
based units, say KB or Megabyte, also K/M/G in short notation.
This can be confusing at times, because the manual for lvs/pvs/vgs etc.
all state that you pass lowercase suffixes for binary units and uppercase
suffixes for S.I. units.
This patch attempts to differentiate between those units in the output
and print a proper string accordingly.
For binary based units:
 Long output strings (unused) will be Kibibyte, Mebibyte, etc.
 Short output strings will be KiB, MiB, etc.
 Unit output strings will be lower-cased: k, m, g, etc.
 Sectors determined by division with 512 will be suffixed with s.
For S.I. based units:
 Long output strings (unused) will be Kilobyte, Megabyte, etc.
 Short output strings will be kB, MB, GB, etc.
 Unit output strings will be upper-cased: K, M, G, etc.
 Sectors determined by division with 500 will be suffixed with S.

This will break every script that relies on the output of commands,
which would print "KB", "MB", etc.
This behaviour is configurable with the si_unit_consistency global
configuration option.

Signed-off-by: Daniel Mierswa <impulze impulze org>
---
 lib/display/display.c |   65 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/lib/display/display.c b/lib/display/display.c
index 74e376b..1d25b3d 100644
--- a/lib/display/display.c
+++ b/lib/display/display.c
@@ -154,16 +154,32 @@ static const char *_display_size(const struct cmd_context *cmd,
 	int s;
 	int suffix = 1, precision;
 	uint64_t byte = UINT64_C(0);
-	uint64_t units = UINT64_C(1024);
 	char *size_buf = NULL;
 	const char * const size_str[][3] = {
-		{" Exabyte", " EB", "E"},
+		/* old strings */
+		{" Exabyte",  " EB", "E"},  /* I: 0 */
 		{" Petabyte", " PB", "P"},
 		{" Terabyte", " TB", "T"},
 		{" Gigabyte", " GB", "G"},
 		{" Megabyte", " MB", "M"},
 		{" Kilobyte", " KB", "K"},
-		{"", "", ""},
+
+		/* new strings, note: Kilo = k */
+		{" Exbibyte", " EiB", "e"}, /* I: 6 */
+		{" Pebibyte", " PiB", "p"},
+		{" Tebibyte", " TiB", "t"},
+		{" Gibibyte", " GiB", "g"},
+		{" Mebibyte", " MiB", "m"},
+		{" Kibibyte", " KiB", "k"},
+
+		{" Exabyte",  " EB", "E"},  /* I: 12 */
+		{" Petabyte", " PB", "P"},
+		{" Terabyte", " TB", "T"},
+		{" Gigabyte", " GB", "G"},
+		{" Megabyte", " MB", "M"},
+		{" Kilobyte", " kB", "K"},
+
+		{"", "", ""},               /* I: 18 */
 		{" Byte    ", " B ", "B"},
 		{" Units   ", " Un", "U"},
 		{" Sectors ", " Se", "S"},
@@ -177,10 +193,22 @@ static const char *_display_size(const struct cmd_context *cmd,
 
 	suffix = cmd->current_settings.suffix;
 
-	for (s = 0; s < 10; s++)
-		if (toupper((int) cmd->current_settings.unit_type) ==
-		    *size_str[s][2])
-			break;
+	if (cmd->si_unit_consistency) {
+		for (s = 6; s < 18; s++)
+			if (cmd->current_settings.unit_type == *size_str[s][2])
+				break;
+	} else {
+		for (s = 0; s < 6; s++)
+			if (toupper((int) cmd->current_settings.unit_type) == *size_str[s][2])
+				break;
+	}
+
+	/* check if we have an sSbBuU or empty unit type, but only if no previous unit type
+	 * was found (i.e. s is either 6 or 18) */
+	if ((cmd->si_unit_consistency && s == 18) || (!cmd->si_unit_consistency && s == 6))
+		for (s = 18; s < 22; s++)
+			if (toupper((int) cmd->current_settings.unit_type) == *size_str[s][2])
+				break;
 
 	if (size == UINT64_C(0)) {
 		sprintf(size_buf, "0%s", suffix ? size_str[s][sl] : "");
@@ -189,18 +217,27 @@ static const char *_display_size(const struct cmd_context *cmd,
 
 	size *= UINT64_C(512);
 
-	if (s < 10)
+	if (s < 22)
 		byte = cmd->current_settings.unit_factor;
 	else {
+		int s2; /* iterate through "KMGTPE" */
+		uint64_t units = UINT64_C(1024);
 		suffix = 1;
-		if (cmd->current_settings.unit_type == 'H')
+		s = 0;
+
+		if (cmd->current_settings.unit_type == 'H') {
+			if (cmd->si_unit_consistency)
+				s = 12;
+				
 			units = UINT64_C(1000);
-		else
-			units = UINT64_C(1024);
+		} else {
+			if (cmd->si_unit_consistency)
+				s = 6;
+		}
+
 		byte = units * units * units * units * units * units;
-		s = 0;
-		while (size_str[s] && size < byte)
-			s++, byte /= units;
+		for (s2 = 0; s2 < 6 && size < byte; s2++, s++)
+			byte /= units;
 	}
 
 	/* FIXME Make precision configurable */
-- 
1.6.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]