[Libguestfs] [PATCH nbdinfo v2 2/3] info: Add human size to ordinary output

Laszlo Ersek lersek at redhat.com
Mon Sep 20 16:27:18 UTC 2021


On 09/20/21 13:04, Richard W.M. Jones wrote:
> For example:
> 
>   $ nbdkit null 1G --run 'nbdinfo "$uri"'
>   protocol: newstyle-fixed without TLS
>   export="":
>   	export-size: 1073741824 (1G)
> 
> If the value cannot be abbreviated then the output doesn't include the
> part in parentheses:
> 
>   $ nbdkit null 1023 --run 'nbdinfo "$uri"'
>   protocol: newstyle-fixed without TLS
>   export="":
>   	export-size: 1023
> 
> The human-readable output changes, but callers shouldn't be parsing it
> (use --size and/or --json instead).
> ---
>  info/nbdinfo.pod |  9 +++++----
>  info/show.c      | 13 +++++++++++--
>  2 files changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/info/nbdinfo.pod b/info/nbdinfo.pod
> index fbd0ef20..f64fe213 100644
> --- a/info/nbdinfo.pod
> +++ b/info/nbdinfo.pod
> @@ -29,7 +29,7 @@ L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md>):
>   $ nbdinfo nbd://localhost
>   protocol: newstyle-fixed without TLS
>   export="":
> -         export-size: 1048576
> +         export-size: 1048576 (1M)
>           content: data
>           uri: nbd://localhost:10809/
>           is_rotational: false
> @@ -78,7 +78,8 @@ the I<--json> parameter:
>         "block_size_minimum": 1,
>         "block_size_preferred": 4096,
>         "block_size_maximum": 33554432,
> -       "export-size": 2125119488
> +       "export-size": 2125119488,
> +       "export-size-str": "2075312K"
>       }
>     ]
>   }
> @@ -222,11 +223,11 @@ For example:
>   $ nbdkit file dir=. --run 'nbdinfo --list "$uri"'
>   protocol: newstyle-fixed without TLS
>   export="Fedora-Workstation-Live-x86_64-29-1.2.iso":
> -     export-size: 1931476992
> +     export-size: 1931476992 (1842M)
>       uri: nbd://localhost:10809/Fedora-Workstation-Live-x86_64-29-1.2.iso
>       [...]
>   export="debian-10.4.0-amd64-DVD-1.iso":
> -     export-size: 3955556352
> +     export-size: 3955556352 (3862848K)
>       uri: nbd://localhost:10809/debian-10.4.0-amd64-DVD-1.iso
>       [...]
>  
> diff --git a/info/show.c b/info/show.c
> index 893178cb..ff241a83 100644
> --- a/info/show.c
> +++ b/info/show.c
> @@ -28,6 +28,7 @@
>  
>  #include <libnbd.h>
>  
> +#include "human-size.h"
>  #include "vector.h"
>  
>  #include "nbdinfo.h"
> @@ -45,6 +46,8 @@ show_one_export (struct nbd_handle *nbd, const char *desc,
>                   bool first, bool last)
>  {
>    int64_t i, size;
> +  char size_str[HUMAN_SIZE_LONGEST];
> +  bool human_size_flag;
>    char *export_name = NULL;
>    char *export_desc = NULL;
>    char *content = NULL;
> @@ -75,6 +78,8 @@ show_one_export (struct nbd_handle *nbd, const char *desc,
>      exit (EXIT_FAILURE);
>    }
>  
> +  human_size (size_str, size, &human_size_flag);
> +
>    uri = nbd_get_uri (nbd);
>  
>    /* Prefer the server's version of the name, if available */
> @@ -116,7 +121,10 @@ show_one_export (struct nbd_handle *nbd, const char *desc,
>      fprintf (fp, ":\n");
>      if (desc && *desc)
>        fprintf (fp, "\tdescription: %s\n", desc);
> -    fprintf (fp, "\texport-size: %" PRIi64 "\n", size);
> +    if (human_size_flag)
> +      fprintf (fp, "\texport-size: %" PRIi64 " (%s)\n", size, size_str);
> +    else
> +      fprintf (fp, "\texport-size: %" PRIi64 "\n", size);
>      if (content)
>        fprintf (fp, "\tcontent: %s\n", content);
>      if (uri)
> @@ -239,7 +247,8 @@ show_one_export (struct nbd_handle *nbd, const char *desc,
>                 block_maximum);
>  
>      /* Put this one at the end because of the stupid comma thing in JSON. */
> -    fprintf (fp, "\t\"export-size\": %" PRIi64 "\n", size);
> +    fprintf (fp, "\t\"export-size\": %" PRIi64 ",\n", size);
> +    fprintf (fp, "\t\"export-size-str\": \"%s\"\n", size_str);
>  
>      if (last)
>        fprintf (fp, "\t} ]\n");
> 

Assuming "size" (of type int64_t) is never negative here:

Acked-by: Laszlo Ersek <lersek at redhat.com>

Thanks
Laszlo




More information about the Libguestfs mailing list