Mykola Ivanets
2020-Feb-11 21:41 UTC
[Libguestfs] [common PATCH] options: add '--blocksize' option for C-based tools
From: Nikolay Ivanets <stenavin@gmail.com> This patch adds '--blocksize' command line option parsing and handling for guestfish and other C-based tools which share the same code. --- options/options.c | 13 ++++- options/options.h | 125 +++++++++++++++++++++++++++------------------- 2 files changed, 86 insertions(+), 52 deletions(-) diff --git a/options/options.c b/options/options.c index fe63da9..63221ea 100644 --- a/options/options.c +++ b/options/options.c @@ -49,7 +49,8 @@ * Handle the guestfish I<-a> option on the command line. */ void -option_a (const char *arg, const char *format, struct drv **drvsp) +option_a (const char *arg, const char *format, int blocksize, + struct drv **drvsp) { struct uri uri; struct drv *drv; @@ -69,6 +70,7 @@ option_a (const char *arg, const char *format, struct drv **drvsp) drv->type = drv_a; drv->a.filename = uri.path; drv->a.format = format; + drv->a.blocksize = blocksize; free (uri.protocol); } @@ -82,6 +84,7 @@ option_a (const char *arg, const char *format, struct drv **drvsp) drv->uri.password = uri.password; drv->uri.format = format; drv->uri.orig_uri = arg; + drv->uri.blocksize = blocksize; } drv->next = *drvsp; @@ -137,6 +140,10 @@ add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index) ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_DISCARD_BITMASK; ad_optargs.discard = drv->a.discard; } + if (drv->a.blocksize) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK; + ad_optargs.blocksize = drv->a.blocksize; + } r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs); if (r == -1) @@ -170,6 +177,10 @@ add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index) ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_SECRET_BITMASK; ad_optargs.secret = drv->uri.password; } + if (drv->uri.blocksize) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK; + ad_optargs.blocksize = drv->uri.blocksize; + } r = guestfs_add_drive_opts_argv (g, drv->uri.path, &ad_optargs); if (r == -1) diff --git a/options/options.h b/options/options.h index 9b78302..9f3a1e7 100644 --- a/options/options.h +++ b/options/options.h @@ -65,6 +65,7 @@ struct drv { const char *format; /* format (NULL == autodetect) */ const char *cachemode;/* cachemode (NULL == default) */ const char *discard; /* discard (NULL == disable) */ + int blocksize; /* blocksize (0 == default) */ } a; struct { char *path; /* disk path */ @@ -74,6 +75,7 @@ struct drv { char *password; /* password - can be NULL */ const char *format; /* format (NULL == autodetect) */ const char *orig_uri; /* original URI (for error messages etc.) */ + int blocksize; /* blocksize (0 == default) */ } uri; struct { char *guest; /* guest name */ @@ -156,7 +158,7 @@ extern struct key_store *key_store_import_key (struct key_store *ks, const struc extern void free_key_store (struct key_store *ks); /* in options.c */ -extern void option_a (const char *arg, const char *format, struct drv **drvsp); +extern void option_a (const char *arg, const char *format, int blocksize, struct drv **drvsp); extern void option_d (const char *arg, struct drv **drvsp); extern char add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index); #define add_drives(drv) add_drives_handle (g, drv, 0) @@ -164,76 +166,87 @@ extern void mount_mps (struct mp *mp); extern void free_drives (struct drv *drv); extern void free_mps (struct mp *mp); -#define OPTION_a \ - do { \ - option_a (optarg, format, &drvs); \ - format_consumed = true; \ +#define OPTION_a \ + do { \ + option_a (optarg, format, blocksize, &drvs); \ + format_consumed = true; \ + blocksize_consumed = true; \ } while (0) -#define OPTION_A \ - do { \ - option_a (optarg, format, &drvs2); \ - format_consumed = true; \ +#define OPTION_A \ + do { \ + option_a (optarg, format, blocksize, &drvs2); \ + format_consumed = true; \ + blocksize_consumed = true; \ } while (0) -#define OPTION_c \ +#define OPTION_c \ libvirt_uri = optarg -#define OPTION_d \ +#define OPTION_d \ option_d (optarg, &drvs) -#define OPTION_D \ +#define OPTION_D \ option_d (optarg, &drvs2) -#define OPTION_format \ - do { \ - if (!optarg || STREQ (optarg, "")) \ - format = NULL; \ - else \ - format = optarg; \ - format_consumed = false; \ +#define OPTION_format \ + do { \ + if (!optarg || STREQ (optarg, "")) \ + format = NULL; \ + else \ + format = optarg; \ + format_consumed = false; \ } while (0) -#define OPTION_i \ +#define OPTION_blocksize \ + do { \ + if (!optarg || STREQ (optarg, "")) \ + blocksize = 0; \ + else if (sscanf (optarg, "%d", &blocksize) != 1) \ + error (EXIT_FAILURE, 0, _("--blocksize option is not numeric")); \ + blocksize_consumed = false; \ + } while (0) + +#define OPTION_i \ inspector = 1 -#define OPTION_m \ - mp = malloc (sizeof (struct mp)); \ - if (!mp) \ - error (EXIT_FAILURE, errno, "malloc"); \ - mp->fstype = NULL; \ - mp->options = NULL; \ - mp->mountpoint = (char *) "/"; \ - p = strchr (optarg, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->mountpoint = p; \ - p = strchr (p, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->options = p; \ - p = strchr (p, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->fstype = p; \ - } \ - } \ - } \ - mp->device = optarg; \ - mp->next = mps; \ +#define OPTION_m \ + mp = malloc (sizeof (struct mp)); \ + if (!mp) \ + error (EXIT_FAILURE, errno, "malloc"); \ + mp->fstype = NULL; \ + mp->options = NULL; \ + mp->mountpoint = (char *) "/"; \ + p = strchr (optarg, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->mountpoint = p; \ + p = strchr (p, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->options = p; \ + p = strchr (p, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->fstype = p; \ + } \ + } \ + } \ + mp->device = optarg; \ + mp->next = mps; \ mps = mp -#define OPTION_n \ +#define OPTION_n \ guestfs_set_autosync (g, 0) -#define OPTION_r \ +#define OPTION_r \ read_only = 1 -#define OPTION_v \ - verbose++; \ +#define OPTION_v \ + verbose++; \ guestfs_set_verbose (g, verbose) #define OPTION_V \ @@ -267,4 +280,14 @@ extern void free_mps (struct mp *mp); } \ } while (0) +#define CHECK_OPTION_blocksize_consumed \ + do { \ + if (!blocksize_consumed) { \ + fprintf (stderr, \ + _("%s: --blocksize parameter must appear before -a parameter\n"), \ + getprogname ()); \ + exit (EXIT_FAILURE); \ + } \ + } while (0) + #endif /* OPTIONS_H */ -- 2.17.2
Richard W.M. Jones
2020-Feb-13 09:42 UTC
Re: [Libguestfs] [common PATCH] options: add '--blocksize' option for C-based tools
On Tue, Feb 11, 2020 at 11:41:20PM +0200, Mykola Ivanets wrote:> diff --git a/options/options.h b/options/options.h > index 9b78302..9f3a1e7 100644 > --- a/options/options.h > +++ b/options/options.h > @@ -65,6 +65,7 @@ struct drv { > const char *format; /* format (NULL == autodetect) */ > const char *cachemode;/* cachemode (NULL == default) */ > const char *discard; /* discard (NULL == disable) */ > + int blocksize; /* blocksize (0 == default) */ > } a; > struct { > char *path; /* disk path */ > @@ -74,6 +75,7 @@ struct drv { > char *password; /* password - can be NULL */ > const char *format; /* format (NULL == autodetect) */ > const char *orig_uri; /* original URI (for error messages etc.) */ > + int blocksize; /* blocksize (0 == default) */Trailing whitespace on this line.> -#define OPTION_c \ > +#define OPTION_c \ > libvirt_uri = optarg > > -#define OPTION_d \ > +#define OPTION_d \ > option_d (optarg, &drvs) > > -#define OPTION_D \ > +#define OPTION_D \ > option_d (optarg, &drvs2) > > -#define OPTION_format \ > - do { \ > - if (!optarg || STREQ (optarg, "")) \ > - format = NULL; \ > - else \ > - format = optarg; \ > - format_consumed = false; \ > +#define OPTION_format \ > + do { \ > + if (!optarg || STREQ (optarg, "")) \ > + format = NULL; \ > + else \ > + format = optarg; \ > + format_consumed = false; \ > } while (0)There are loads of whitespace-change-only hunks in this patch. Are they necessary? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Apparently Analagous Threads
- [common PATCH v2 1/1] options: add '--blocksize' option for C-based tools
- [common PATCH v3 0/1] options: add '--blocksize' option for C-based tools
- [PATCH] common/options: Change drv struct to store drive index instead of device name.
- [common PATCH v2 0/1] options: add '--blocksize' option for C-based tools
- [common PATCH v4 0/1] options: add '--blocksize' option for C-based tools