[Libguestfs] [PATCH nbdkit 2/5] Use .get_ready method in miscellaneous plugins and filters.

Richard W.M. Jones rjones at redhat.com
Tue Feb 25 10:30:53 UTC 2020


For plugins/filters where .config_complete was "overloaded" as a place
to finish configuration and do some set up before we start serving,
use the new .get_ready callback.
---
 plugins/floppy/floppy.c             |  9 ++++++++-
 plugins/floppy/virtual-floppy.c     |  8 ++++----
 plugins/iso/iso.c                   | 10 +++++++---
 plugins/linuxdisk/linuxdisk.c       |  9 ++++++++-
 plugins/partitioning/partitioning.c |  9 ++++++++-
 plugins/streaming/streaming.c       |  7 +++++++
 plugins/vddk/vddk.c                 | 14 ++++++++++----
 filters/log/log.c                   | 14 +++++++++++---
 filters/rate/rate.c                 |  4 ++--
 filters/stats/stats.c               | 11 +++++++++--
 10 files changed, 74 insertions(+), 21 deletions(-)

diff --git a/plugins/floppy/floppy.c b/plugins/floppy/floppy.c
index 41a23644..1c2f645e 100644
--- a/plugins/floppy/floppy.c
+++ b/plugins/floppy/floppy.c
@@ -99,13 +99,19 @@ floppy_config_complete (void)
     return -1;
   }
 
-  return create_virtual_floppy (dir, label, &floppy);
+  return 0;
 }
 
 #define floppy_config_help \
   "dir=<DIRECTORY>     (required) The directory to serve.\n" \
   "label=<LABEL>                  The volume label." \
 
+static int
+floppy_get_ready (void)
+{
+  return create_virtual_floppy (dir, label, &floppy);
+}
+
 static void *
 floppy_open (int readonly)
 {
@@ -204,6 +210,7 @@ static struct nbdkit_plugin plugin = {
   .config_complete   = floppy_config_complete,
   .config_help       = floppy_config_help,
   .magic_config_key  = "dir",
+  .get_ready         = floppy_get_ready,
   .open              = floppy_open,
   .get_size          = floppy_get_size,
   .can_multi_conn    = floppy_can_multi_conn,
diff --git a/plugins/floppy/virtual-floppy.c b/plugins/floppy/virtual-floppy.c
index b5c29a46..fc0cafa8 100644
--- a/plugins/floppy/virtual-floppy.c
+++ b/plugins/floppy/virtual-floppy.c
@@ -256,10 +256,10 @@ visit (const char *dir, struct virtual_floppy *floppy)
   floppy->nr_dirs++;
   memset (&floppy->dirs[di], 0, sizeof (struct dir));
 
-  /* Because this is called from config_complete, before nbdkit
-   * daemonizes or starts any threads, it's safe to use chdir here and
-   * greatly simplifies the code.  However we must chdir back to the
-   * original directory at the end.
+  /* Because this is called from get_ready, before nbdkit daemonizes
+   * or starts any threads, it's safe to use chdir here and greatly
+   * simplifies the code.  However we must chdir back to the original
+   * directory at the end.
    */
   origdir = get_current_dir_name ();
   if (origdir == NULL) {
diff --git a/plugins/iso/iso.c b/plugins/iso/iso.c
index 5634bac9..92554ace 100644
--- a/plugins/iso/iso.c
+++ b/plugins/iso/iso.c
@@ -176,9 +176,6 @@ iso_config_complete (void)
     return -1;
   }
 
-  if (make_iso () == -1)
-    return -1;
-
   return 0;
 }
 
@@ -187,6 +184,12 @@ iso_config_complete (void)
   "params='<PARAMS>'              Extra parameters to pass.\n" \
   "prog=<ISOPROG>                 The program used to make ISOs." \
 
+static int
+iso_get_ready (void)
+{
+  return make_iso ();
+}
+
 static void *
 iso_open (int readonly)
 {
@@ -254,6 +257,7 @@ static struct nbdkit_plugin plugin = {
   .config_complete   = iso_config_complete,
   .config_help       = iso_config_help,
   .magic_config_key  = "dir",
+  .get_ready         = iso_get_ready,
   .open              = iso_open,
   .get_size          = iso_get_size,
   .can_multi_conn    = iso_can_multi_conn,
diff --git a/plugins/linuxdisk/linuxdisk.c b/plugins/linuxdisk/linuxdisk.c
index 99dbc996..d7b52242 100644
--- a/plugins/linuxdisk/linuxdisk.c
+++ b/plugins/linuxdisk/linuxdisk.c
@@ -128,7 +128,7 @@ linuxdisk_config_complete (void)
     return -1;
   }
 
-  return create_virtual_disk (&disk);
+  return 0;
 }
 
 #define linuxdisk_config_help \
@@ -137,6 +137,12 @@ linuxdisk_config_complete (void)
   "type=ext2|ext3|ext4         The filesystem type.\n" \
   "size=[+]<SIZE>              The virtual filesystem size."
 
+static int
+linuxdisk_get_ready (void)
+{
+  return create_virtual_disk (&disk);
+}
+
 static void *
 linuxdisk_open (int readonly)
 {
@@ -226,6 +232,7 @@ static struct nbdkit_plugin plugin = {
   .config_complete   = linuxdisk_config_complete,
   .config_help       = linuxdisk_config_help,
   .magic_config_key  = "dir",
+  .get_ready         = linuxdisk_get_ready,
   .open              = linuxdisk_open,
   .get_size          = linuxdisk_get_size,
   .can_multi_conn    = linuxdisk_can_multi_conn,
diff --git a/plugins/partitioning/partitioning.c b/plugins/partitioning/partitioning.c
index 6e426b93..865acd28 100644
--- a/plugins/partitioning/partitioning.c
+++ b/plugins/partitioning/partitioning.c
@@ -269,13 +269,19 @@ partitioning_config_complete (void)
     return -1;
   }
 
-  return create_virtual_disk_layout ();
+  return 0;
 }
 
 #define partitioning_config_help \
   "file=<FILENAME>  (required) File(s) containing partitions\n" \
   "partition-type=mbr|gpt      Partition type"
 
+static int
+partitioning_get_ready (void)
+{
+  return create_virtual_disk_layout ();
+}
+
 /* Create the per-connection handle. */
 static void *
 partitioning_open (int readonly)
@@ -433,6 +439,7 @@ static struct nbdkit_plugin plugin = {
   .config_complete   = partitioning_config_complete,
   .config_help       = partitioning_config_help,
   .magic_config_key = "file",
+  .get_ready         = partitioning_get_ready,
   .open              = partitioning_open,
   .get_size          = partitioning_get_size,
   .can_multi_conn    = partitioning_can_multi_conn,
diff --git a/plugins/streaming/streaming.c b/plugins/streaming/streaming.c
index b2359540..71365c58 100644
--- a/plugins/streaming/streaming.c
+++ b/plugins/streaming/streaming.c
@@ -90,6 +90,12 @@ streaming_config_complete (void)
     return -1;
   }
 
+  return 0;
+}
+
+static int
+streaming_get_ready (void)
+{
   /* Open the file blindly.  If this fails with ENOENT then we create a
    * FIFO and try again.
    */
@@ -248,6 +254,7 @@ static struct nbdkit_plugin plugin = {
   .config            = streaming_config,
   .config_complete   = streaming_config_complete,
   .config_help       = streaming_config_help,
+  .get_ready         = streaming_get_ready,
   .open              = streaming_open,
   .close             = streaming_close,
   .get_size          = streaming_get_size,
diff --git a/plugins/vddk/vddk.c b/plugins/vddk/vddk.c
index 533c5605..7788a31b 100644
--- a/plugins/vddk/vddk.c
+++ b/plugins/vddk/vddk.c
@@ -60,8 +60,7 @@ int vddk_debug_extents;
 int vddk_debug_datapath = 1;
 
 /* For each VDDK API define a static global variable.  These globals
- * are initialized when the plugin is loaded (by
- * vddk_config_complete).
+ * are initialized when the plugin is loaded (by vddk_get_ready).
  */
 #define STUB(fn,ret,args) static ret (*fn) args
 #define OPTIONAL_STUB(fn,ret,args) static ret (*fn) args
@@ -432,8 +431,6 @@ load_library (void)
 static int
 vddk_config_complete (void)
 {
-  VixError err;
-
   if (filename == NULL) {
     nbdkit_error ("you must supply the file=<FILENAME> parameter "
                   "after the plugin name on the command line");
@@ -506,6 +503,14 @@ vddk_config_complete (void)
     }
   }
 
+  return 0;
+}
+
+static int
+vddk_get_ready (void)
+{
+  VixError err;
+
   load_library ();
 
   /* Initialize VDDK library. */
@@ -1017,6 +1022,7 @@ static struct nbdkit_plugin plugin = {
   .config_help       = vddk_config_help,
   .magic_config_key  = "file",
   .dump_plugin       = vddk_dump_plugin,
+  .get_ready         = vddk_get_ready,
   .open              = vddk_open,
   .close             = vddk_close,
   .get_size          = vddk_get_size,
diff --git a/filters/log/log.c b/filters/log/log.c
index 7eb608c5..76d139eb 100644
--- a/filters/log/log.c
+++ b/filters/log/log.c
@@ -86,16 +86,23 @@ log_config (nbdkit_next_config *next, void *nxdata,
   return next (nxdata, key, value);
 }
 
-/* Open the logfile. */
 static int
 log_config_complete (nbdkit_next_config_complete *next, void *nxdata)
 {
-  int fd;
-
   if (!logfilename) {
     nbdkit_error ("missing logfile= parameter for the log filter");
     return -1;
   }
+
+  return next (nxdata);
+}
+
+/* Open the logfile. */
+static int
+log_get_ready (nbdkit_next_get_ready *next, void *nxdata)
+{
+  int fd;
+
   /* Using fopen("ae"/"we") would be more convenient, but as Haiku
    * still lacks that, use this instead. Atomicity is not essential
    * here since .config completes before threads that might fork, if
@@ -444,6 +451,7 @@ static struct nbdkit_filter filter = {
   .config_complete   = log_config_complete,
   .config_help       = log_config_help,
   .unload            = log_unload,
+  .get_ready         = log_get_ready,
   .open              = log_open,
   .close             = log_close,
   .prepare           = log_prepare,
diff --git a/filters/rate/rate.c b/filters/rate/rate.c
index 2b105f91..f71e9cf3 100644
--- a/filters/rate/rate.c
+++ b/filters/rate/rate.c
@@ -145,7 +145,7 @@ rate_config (nbdkit_next_config *next, void *nxdata,
 }
 
 static int
-rate_config_complete (nbdkit_next_config_complete *next, void *nxdata)
+rate_get_ready (nbdkit_next_get_ready *next, void *nxdata)
 {
   /* Initialize the global buckets. */
   bucket_init (&read_bucket, rate, BUCKET_CAPACITY);
@@ -312,8 +312,8 @@ static struct nbdkit_filter filter = {
   .longname          = "nbdkit rate filter",
   .unload            = rate_unload,
   .config            = rate_config,
-  .config_complete   = rate_config_complete,
   .config_help       = rate_config_help,
+  .get_ready         = rate_get_ready,
   .open              = rate_open,
   .close             = rate_close,
   .pread             = rate_pread,
diff --git a/filters/stats/stats.c b/filters/stats/stats.c
index 0759cebe..05832561 100644
--- a/filters/stats/stats.c
+++ b/filters/stats/stats.c
@@ -201,13 +201,19 @@ stats_config (nbdkit_next_config *next, void *nxdata,
 static int
 stats_config_complete (nbdkit_next_config_complete *next, void *nxdata)
 {
-  int fd;
-
   if (filename == NULL) {
     nbdkit_error ("stats filter requires statsfile parameter");
     return -1;
   }
 
+  return next (nxdata);
+}
+
+static int
+stats_get_ready (nbdkit_next_get_ready *next, void *nxdata)
+{
+  int fd;
+
   /* Using fopen("ae"/"we") would be more convenient, but as Haiku
    * still lacks that, use this instead. Atomicity is not essential
    * here since .config completes before threads that might fork, if
@@ -372,6 +378,7 @@ static struct nbdkit_filter filter = {
   .config            = stats_config,
   .config_complete   = stats_config_complete,
   .config_help       = stats_config_help,
+  .get_ready         = stats_get_ready,
   .pread             = stats_pread,
   .pwrite            = stats_pwrite,
   .trim              = stats_trim,
-- 
2.25.0




More information about the Libguestfs mailing list