Re: [PATCH 1/2] common/rc: add _parse_size_from_string

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



On Tue, Jun 14, 2022 at 01:08:42PM +0800, An Long wrote:
> 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@xxxxxxxx>
> ---
>  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()

I think this name is better to be shorter, as it might be a lower level common
function. How about _convert_num() or _cvtnum()?

> +{
> +        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))
                   ^       ^
I think above two '$' isn't necessary, although it doesn't make something
wrong if keep it. Same as all above (($mult * 1024)) .

Thanks,
Zorro
                                      ^

> +        echo $size
> +}
> +
>  # Create fs of certain size on scratch device
>  # _scratch_mkfs_sized <size in bytes> [optional blocksize]
>  _scratch_mkfs_sized()
> -- 
> 2.35.3
> 




[Index of Archives]     [Linux Filesystems Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux