public inbox for fstests@vger.kernel.org
 help / color / mirror / Atom feed
From: An Long <lan@suse.com>
To: fstests@vger.kernel.org
Cc: An Long <lan@suse.com>
Subject: [PATCH 1/2] common/rc: add _parse_size_from_string
Date: Tue, 14 Jun 2022 13:08:42 +0800	[thread overview]
Message-ID: <20220614050843.11802-1-lan@suse.com> (raw)

Add a helper to convert size value to bytes. This is used to handle
value as bytes, such as 4k to 4096.

Signed-off-by: An Long <lan@suse.com>
---
 common/rc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/common/rc b/common/rc
index 3c072c16..22050bc2 100644
--- a/common/rc
+++ b/common/rc
@@ -1028,6 +1028,54 @@ _check_minimal_fs_size()
 	fi
 }
 
+# Convert size value to bytes
+# _parse_size_from_string <size>
+_parse_size_from_string()
+{
+        local str=$1
+        local mult=1
+        local size
+        local endchar
+
+        if [[ $str =~ ^[0-9]+[a-zA-Z]$ ]] ; then
+                size=${str:: -1}
+                endchar=${str: -1}
+                case $endchar in
+                e|E)
+                                mult=$(($mult * 1024))
+                                ;&
+                p|P)
+                                mult=$(($mult * 1024))
+                                ;&
+                t|T)
+                                mult=$(($mult * 1024))
+                                ;&
+                g|G)
+                                mult=$(($mult * 1024))
+                                ;&
+                m|M)
+                                mult=$(($mult * 1024))
+                                ;&
+                k|K)
+                                mult=$(($mult * 1024))
+                                ;&
+                b|B)
+                                ;;
+                *)
+                                echo "unknown size descriptor $endchar"
+                                exit 1
+                esac
+        elif [[ $str =~ ^[0-9]+$ ]] ; then
+                size=$str
+        else
+                echo "size value $str is invalid"
+                exit 1
+        fi
+
+        size=$(($size * $mult))
+        echo $size
+}
+
 # Create fs of certain size on scratch device
 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
 _scratch_mkfs_sized()
-- 
2.35.3


             reply	other threads:[~2022-06-14  5:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-14  5:08 An Long [this message]
2022-06-14  5:08 ` [PATCH 2/2] common/rc: fix input value to _scratch_mkfs_sized An Long
2022-06-15  4:15   ` Zorro Lang
2022-06-16  4:30     ` Long An
2022-06-15  6:05 ` [PATCH 1/2] common/rc: add _parse_size_from_string Zorro Lang
2022-06-16  4:37   ` Long An

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=20220614050843.11802-1-lan@suse.com \
    --to=lan@suse.com \
    --cc=fstests@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