Re: [PATCH v2 1/2] common/rc: add _parse_size_string

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



On Thu, 2022-06-16 at 11:25 -0400, Gabriel Krisman Bertazi wrote:
> An Long <lan@xxxxxxxx> 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()
-- 
An Long <lan@xxxxxxxx>
SUSE QE LSG, QE 2, Beijing




[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