[Sheepdog] [PATCH, RFC] collie: add -r/--raw option for machine-readable output

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Jun 16 18:56:31 CEST 2011


Hi Chris,

Nice work again!

At Thu, 16 Jun 2011 14:00:37 +0100,
Chris Webb wrote:
> 
> This option modifies the info and list displays to make them easier to parse
> reliably from C and shell scripts. The fields are separated with a single
> space character, column headers are omitted, and all sizes are displayed in
> raw bytes rather than using units of MB, GB, TB, etc.
> 
> Signed-off-by: Chris Webb <chris at arachsys.com>
> ---
>  collie/collie.c |   63 +++++++++++++++++++++++++++++++++++++-----------------
>  1 files changed, 43 insertions(+), 20 deletions(-)
> 
> diff --git a/collie/collie.c b/collie/collie.c
> index 44eacd3..6262243 100644
> --- a/collie/collie.c
> +++ b/collie/collie.c
> @@ -35,6 +35,7 @@ static char program_name[] = "collie";
>  static const char *sdhost = "localhost";
>  static int sdport = SD_LISTEN_PORT;
>  static int highlight = 1;
> +static int raw_output = 0;
>  
>  #define TEXT_NORMAL "\033[0m"
>  #define TEXT_BOLD   "\033[1m"
> @@ -42,9 +43,10 @@ static int highlight = 1;
>  #define COMMON_LONG_OPTIONS				\
>  	{"address", required_argument, NULL, 'a'},	\
>  	{"port", required_argument, NULL, 'p'},		\
> +	{"raw", no_argument, NULL, 'r'},		\
>  	{"help", no_argument, NULL, 'h'},		\
>  
> -#define COMMON_SHORT_OPTIONS "a:p:h"
> +#define COMMON_SHORT_OPTIONS "a:p:hr"
>  
>  static void usage(int status)
>  {
> @@ -62,6 +64,8 @@ Command syntax:\n\
>  Common parameters:\n\
>    -a, --address           specify the daemon address (default: localhost)\n\
>    -p, --port              specify the daemon port\n\
> +  -r, --raw               raw output mode: omit headers, separate fields with\n\
> +                          single spaces and print all sizes in raw bytes\n\
>    -h, --help              display this help and exit\n\
>  ");
>  	}
> @@ -84,12 +88,17 @@ static char *size_to_str(uint64_t _size, char *str, int str_size)
>  {
>  	const char *units[] = {"MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
>  	int i = 0;
> -	double size = (double)_size;
> +	double size;
>  
> +	if (raw_output) {
> +		snprintf(str, str_size, "%" PRIu64, _size);
> +		return str;
> +	}
> +
> +	size = (double)_size;
>  	size /= 1024 * 1024;
>  	while (i < ARRAY_SIZE(units) && size >= 1024) {
>  		i++;
> -
>  		size /= 1024;
>  	}
>  
> @@ -387,8 +396,9 @@ static void print_vdi_list(uint32_t vid, char *name, char *tag, uint32_t snapid,
>  	size_to_str(cow_objs * SD_DATA_OBJ_SIZE, cow_objs_str, sizeof(cow_objs_str));
>  
>  	if (!data || strcmp(name, data) == 0) {
> -		printf("%c %-8s %5d %7s %7s %7s %s  %7" PRIx32 "\n",
> -		       is_current(i) ? ' ' : 's', name, snapid,
> +		printf(raw_output ? "%c %s %d %s %s %s %s %" PRIx32 "\n"
> +				  : "%c %-8s %5d %7s %7s %7s %s  %7" PRIx32 "\n",
> +		       is_current(i) ? (raw_output ? '=' : ' ') : 's', name, snapid,
>  		       vdi_size_str, my_objs_str, cow_objs_str, dbuf, vid);

A vdi name can contain spaces, so shouldn't we espace them?

Thanks,

Kazutaka



More information about the sheepdog mailing list