[Libguestfs] [common PATCH] options: add '--blocksize' option for C-based tools

Mykola Ivanets stenavin at gmail.com
Tue Feb 11 21:41:20 UTC 2020


From: Nikolay Ivanets <stenavin at 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





More information about the Libguestfs mailing list