[Libguestfs] [PATCH nbdkit 2/5] error: Use new nbdkit_parse_probability

Richard W.M. Jones rjones at redhat.com
Tue May 16 12:12:16 UTC 2023


This replaces the existing ad hoc parsing with the new public function.
---
 filters/error/nbdkit-error-filter.pod | 12 +++++++-----
 filters/error/error.c                 | 14 +-------------
 2 files changed, 8 insertions(+), 18 deletions(-)

diff --git a/filters/error/nbdkit-error-filter.pod b/filters/error/nbdkit-error-filter.pod
index bedd97924..49f21d10a 100644
--- a/filters/error/nbdkit-error-filter.pod
+++ b/filters/error/nbdkit-error-filter.pod
@@ -6,7 +6,7 @@ nbdkit-error-filter - inject errors for testing clients
 
  nbdkit --filter=error PLUGIN
      [error=EPERM|EIO|ENOMEM|EINVAL|ENOSPC|ESHUTDOWN]
-     [error-rate=10%|0.1]
+     [error-rate=10%|0.1|1:10]
      [error-file=/tmp/inject]
      [error-pread=...] [error-pread-rate=...] [error-pread-file=...]
      [error-pwrite=...] [error-pwrite-rate=...] [error-pwrite-file=...]
@@ -66,11 +66,13 @@ This parameter is optional and the default is C<EIO>
 
 =item B<error-rate=>0..1
 
+=item B<error-rate=>NB<:>M
+
 The rate of injected errors per NBD request.  This can be expressed as
-either a percentage between C<0%> and C<100%> or as a probability
-between C<0> and C<1>.  If C<0%> or C<0> is used then no errors are
-ever injected, and if C<100%> or C<1> is used then all requests return
-errors.
+a percentage between C<0%> and C<100%>, or as a probability between
+C<0> and C<1>, or as a ratio like C<1:10>.  If C<0%> or C<0> is used
+then no errors are ever injected, and if C<100%> or C<1> is used then
+all requests return errors.
 
 This parameter is optional and the default is C<0%>.
 B<Unless you set this, the filter will do nothing.>
diff --git a/filters/error/error.c b/filters/error/error.c
index ac4da8c16..08e19e3bd 100644
--- a/filters/error/error.c
+++ b/filters/error/error.c
@@ -130,21 +130,9 @@ static int
 parse_error_rate (const char *key, const char *value, double *retp)
 {
   double d;
-  int n;
 
-  if (sscanf (value, "%lg%n", &d, &n) == 1) {
-    if (strcmp (&value[n], "%") == 0) /* percentage */
-      d /= 100.0;
-    else if (strcmp (&value[n], "") == 0) /* probability */
-      ;
-    else
-      goto bad_parse;
-  }
-  else {
-  bad_parse:
-    nbdkit_error ("%s: could not parse rate '%s'", key, value);
+  if (nbdkit_parse_probability (key, value, &d) == -1)
     return -1;
-  }
   if (d < 0 || d > 1) {
     nbdkit_error ("%s: rate out of range: '%s' parsed as %g", key, value, d);
     return -1;
-- 
2.39.2



More information about the Libguestfs mailing list