diff mbox series

[v2,1/2] common/rc: add _parse_size_string

Message ID 20220616043845.14320-2-lan@suse.com (mailing list archive)
State New, archived
Headers show
Series Fix input value to _scratch_mkfs_sized | expand

Commit Message

An Long June 16, 2022, 4:38 a.m. UTC
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>
---
V1 -> V2:
 - Rename _parse_size_from_string to _parse_size_string
 - Remove unnecessary '$' sign
---
 common/rc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

Comments

Gabriel Krisman Bertazi June 16, 2022, 3:25 p.m. UTC | #1
An Long <lan@suse.com> writes:

> +        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
> +}

Hi An,

Coreutils has numfmt(1) to do this kind of conversion.  I wonder if we
could use it here, unless it is not available all the platforms that
matters for xfstests, though:

$ echo 1K | numfmt --from=iec
1024

> +
>  # Create fs of certain size on scratch device
>  # _scratch_mkfs_sized <size in bytes> [optional blocksize]
>  _scratch_mkfs_sized()
An Long June 17, 2022, 6:45 a.m. UTC | #2
On Thu, 2022-06-16 at 11:25 -0400, Gabriel Krisman Bertazi wrote:
> An Long <lan@suse.com> writes:
> 
> > +        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
> > +}
> 
> Hi An,
> 
> Coreutils has numfmt(1) to do this kind of conversion.  I wonder if
> we
> could use it here, unless it is not available all the platforms that
> matters for xfstests, though:
> 
> $ echo 1K | numfmt --from=iec
> 1024
> 

Hi Gabriel,

Using numfmt should reduce the code. But it brings new problems:

1) numfmt doesn't support lowercase
echo 4k | numfmt --from=iec
numfmt: invalid suffix in input: '4k'

2) This cannot clearly point out the error

3) Value range is limited
echo 16E | numfmt --from=iec
numfmt: value too large to be printed: '1.84467e+19' (consider using --
to)

4) Added system dependencies

More code is required to solve above problems, and not elegant.

> > +
> >  # Create fs of certain size on scratch device
> >  # _scratch_mkfs_sized <size in bytes> [optional blocksize]
> >  _scratch_mkfs_sized()
diff mbox series

Patch

diff --git a/common/rc b/common/rc
index 3c072c16..09ffafa4 100644
--- a/common/rc
+++ b/common/rc
@@ -1028,6 +1028,54 @@  _check_minimal_fs_size()
 	fi
 }
 
+# Convert size value to bytes
+# _parse_size_string <size>
+_parse_size_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()