[lxc-devel] [lxd/master] more byte parsing

brauner on Github lxc-bot at linuxcontainers.org
Sun Jan 15 01:28:14 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170115/bde09da8/attachment.bin>
-------------- next part --------------
From 471dd6305ec64e7834b55ee76da626c11576a9ee Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sun, 15 Jan 2017 02:25:13 +0100
Subject: [PATCH 1/2] shared/util: add Int64InSlice()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util.go | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/shared/util.go b/shared/util.go
index 94c5a2a..f6600bb 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -395,6 +395,15 @@ func IntInSlice(key int, list []int) bool {
 	return false
 }
 
+func Int64InSlice(key int64, list []int64) bool {
+	for _, entry := range list {
+		if entry == key {
+			return true
+		}
+	}
+	return false
+}
+
 func IsTrue(value string) bool {
 	if StringInSlice(strings.ToLower(value), []string{"true", "1", "yes", "on"}) {
 		return true

From 4699780def6d67e8f37b524a399fc8cc913a4af6 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sun, 15 Jan 2017 02:25:34 +0100
Subject: [PATCH 2/2] shared/util: improve byte parsing

- GetByteSizeString() --> switch to standardized "B" suffix
- ParseByteSizeString() --> handle "B" suffix and legacy " bytes" suffix

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util.go | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index f6600bb..a736b6e 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -627,31 +627,32 @@ func ParseMetadata(metadata interface{}) (map[string]interface{}, error) {
 // Parse a size string in bytes (e.g. 200kB or 5GB) into the number of bytes it
 // represents. Supports suffixes up to EB. "" == 0.
 func ParseByteSizeString(input string) (int64, error) {
+	suffixLen := 2
 	if input == "" {
 		return 0, nil
 	}
 
-	// COMMENT(brauner): In case the last character is a number we assume
-	// that we are passed a simple integer which we interpret as being in
-	// bytes. So we parse it directly and return.
 	if unicode.IsNumber(rune(input[len(input)-1])) {
-		valueInt, err := strconv.ParseInt(input, 10, 64)
-		if err != nil {
-			return -1, fmt.Errorf("Invalid integer: %s", input)
-		}
-
-		return valueInt, nil
-	}
-
-	if len(input) < 3 {
+		// COMMENT(brauner): No suffix --> bytes.
+		suffixLen = 0
+	} else if (input[len(input)-1] == 'B') && unicode.IsNumber(rune(input[len(input)-2])) {
+		suffixLen = 1
+	} else if strings.HasSuffix(input, " bytes") {
+		// COMMENT(brauner): Backward compatible behaviour in case we
+		// talk to a LXD that still uses GetByteSizeString() that
+		// returns "n bytes".
+		suffixLen = 6
+	}
+
+	if len(input) < 3 && suffixLen == 2 {
 		return -1, fmt.Errorf("Invalid value: %s", input)
 	}
 
 	// Extract the suffix
-	suffix := input[len(input)-2:]
+	suffix := input[len(input)-suffixLen:]
 
 	// Extract the value
-	value := input[0 : len(input)-2]
+	value := input[0 : len(input)-suffixLen]
 	valueInt, err := strconv.ParseInt(value, 10, 64)
 	if err != nil {
 		return -1, fmt.Errorf("Invalid integer: %s", input)
@@ -661,6 +662,10 @@ func ParseByteSizeString(input string) (int64, error) {
 		return -1, fmt.Errorf("Invalid value: %d", valueInt)
 	}
 
+	if suffixLen < 2 || suffixLen == 6 {
+		return valueInt, nil
+	}
+
 	// Figure out the multiplicator
 	multiplicator := int64(0)
 	switch suffix {
@@ -732,7 +737,7 @@ func ParseBitSizeString(input string) (int64, error) {
 
 func GetByteSizeString(input int64, precision uint) string {
 	if input < 1024 {
-		return fmt.Sprintf("%d bytes", input)
+		return fmt.Sprintf("%dB", input)
 	}
 
 	value := float64(input)


More information about the lxc-devel mailing list