[Libguestfs] [PATCH nbdinfo v2 1/3] common/utils: Add function to convert sizes to human-readable

Daniel P. Berrangé berrange at redhat.com
Mon Sep 20 16:56:06 UTC 2021


On Mon, Sep 20, 2021 at 05:39:59PM +0100, Richard W.M. Jones wrote:
> On Mon, Sep 20, 2021 at 06:25:34PM +0200, Laszlo Ersek wrote:
> > On 09/20/21 13:04, Richard W.M. Jones wrote:
> > > For example 1024 is returned as "1K".
> > > 
> > > This does not attempt to handle decimals or SI units.  If the number
> > > isn't some multiple of a power of 1024 then it is returned as bytes (a
> > > flag is available to indicate this).
> > > 
> > > I looked at both the gnulib and qemu versions of this function.  The
> > > gnulib version is not under a license which is compatible with libnbd
> > > and is also really complicated, although it does handle fractions and
> > > SI units.  The qemu version is essentially just frexp + sprintf and
> > > doesn't attempt to convert to the human-readable version reversibly.
> > > ---
> > >  .gitignore                     |  1 +
> > >  common/utils/Makefile.am       | 10 +++-
> > >  common/utils/human-size.c      | 54 +++++++++++++++++++++
> > >  common/utils/human-size.h      | 49 +++++++++++++++++++
> > >  common/utils/test-human-size.c | 89 ++++++++++++++++++++++++++++++++++
> > >  5 files changed, 201 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/.gitignore b/.gitignore
> > > index 2aa1fd99..5fc59677 100644
> > > --- a/.gitignore
> > > +++ b/.gitignore
> > > @@ -31,6 +31,7 @@ Makefile.in
> > >  /bash/nbdcopy
> > >  /bash/nbdfuse
> > >  /bash/nbdinfo
> > > +/common/utils/test-human-size
> > >  /common/utils/test-vector
> > >  /compile
> > >  /config.cache
> > > diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
> > > index 1ca4a370..b273ada1 100644
> > > --- a/common/utils/Makefile.am
> > > +++ b/common/utils/Makefile.am
> > > @@ -34,6 +34,8 @@ include $(top_srcdir)/common-rules.mk
> > >  noinst_LTLIBRARIES = libutils.la
> > >  
> > >  libutils_la_SOURCES = \
> > > +	human-size.c \
> > > +	human-size.h \
> > >  	vector.c \
> > >  	vector.h \
> > >  	version.c \
> > > @@ -50,8 +52,12 @@ libutils_la_LIBADD = \
> > >  
> > >  # Unit tests.
> > >  
> > > -TESTS = test-vector
> > > -check_PROGRAMS = test-vector
> > > +TESTS = test-human-size test-vector
> > > +check_PROGRAMS = test-human-size test-vector
> > > +
> > > +test_human_size_SOURCES = test-human-size.c human-size.c human-size.h
> > > +test_human_size_CPPFLAGS = -I$(srcdir)
> > > +test_human_size_CFLAGS = $(WARNINGS_CFLAGS)
> > >  
> > >  test_vector_SOURCES = test-vector.c vector.c vector.h
> > >  test_vector_CPPFLAGS = -I$(srcdir)
> > > diff --git a/common/utils/human-size.c b/common/utils/human-size.c
> > > new file mode 100644
> > > index 00000000..772f2489
> > > --- /dev/null
> > > +++ b/common/utils/human-size.c
> > > @@ -0,0 +1,54 @@
> > > +/* nbd client library in userspace
> > > + * Copyright (C) 2020-2021 Red Hat Inc.
> > > + *
> > > + * This library is free software; you can redistribute it and/or
> > > + * modify it under the terms of the GNU Lesser General Public
> > > + * License as published by the Free Software Foundation; either
> > > + * version 2 of the License, or (at your option) any later version.
> > > + *
> > > + * This library is distributed in the hope that it will be useful,
> > > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> > > + * Lesser General Public License for more details.
> > > + *
> > > + * You should have received a copy of the GNU Lesser General Public
> > > + * License along with this library; if not, write to the Free Software
> > > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> > > + */
> > 
> > (1) General question: should we adopt "SPDX-License-Identifier"s? They
> > are more succinct.
> 
> I'd usually follow what qemu or libvirt are doing, and as far as I can
> see they are not using these.

It is a non-trivial undertaking for any established project with
multiple copyright holders.

The license header is generally not something you are permitted to
change generally unless you are the copyright holder, or have the
copy holders' agreement.  Despite this, we can see the kernel did
such a switch, replacing license headers with SPDX tags. My
understanding though, is that there was work done behind scenes
with legal input before they actually merged the patches, as a
means to justify this change.

On any newly written project I'd certainly use SPDX, but IMHO
for existing projects it isn't viable unless we see clear legal
advice explaining a process to follow that makes it acceptable
to replace license text with SPDX.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the Libguestfs mailing list