[lxc-devel] [lxd/master] shared/util: ParseByteSizeString() deal with bytes

brauner on Github lxc-bot at linuxcontainers.org
Sat Jan 14 13:31:17 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 502 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170114/70ea170e/attachment.bin>
-------------- next part --------------
From ab394c4baf29b653022d9060b88f9b61def3f0db Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 14 Jan 2017 14:28:03 +0100
Subject: [PATCH] shared/util: ParseByteSizeString() deal with bytes

When we are passed in a number with a suffix, assume it is already in bytes.

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

diff --git a/shared/util.go b/shared/util.go
index 351bbd6..94c5a2a 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -19,6 +19,7 @@ import (
 	"regexp"
 	"strconv"
 	"strings"
+	"unicode"
 )
 
 const SnapshotDelimiter = "/"
@@ -621,6 +622,18 @@ func ParseByteSizeString(input string) (int64, error) {
 		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 {
 		return -1, fmt.Errorf("Invalid value: %s", input)
 	}


More information about the lxc-devel mailing list