Re: [Libguestfs] [PATCH libnbd 2/5] common/include: Import the human_size_parse (nbdkit_parse_size) function

2023-09-04 Thread Laszlo Ersek
On 9/3/23 17:23, Richard W.M. Jones wrote:
> This function is copied from nbdkit commit XXX [fill in after nbdkit
> change is upstream] XXX

Right, if you end up updating the nbdkit patch, please refresh this one.

Reviewed-by: Laszlo Ersek 

Laszlo

___
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs



[Libguestfs] [PATCH libnbd 2/5] common/include: Import the human_size_parse (nbdkit_parse_size) function

2023-09-03 Thread Richard W.M. Jones
This function is copied from nbdkit commit XXX [fill in after nbdkit
change is upstream] XXX
---
 .gitignore   |   1 +
 common/include/Makefile.am   |   6 ++
 common/include/human-size.h  | 137 +++
 common/include/test-human-size.c | 133 ++
 4 files changed, 277 insertions(+)

diff --git a/.gitignore b/.gitignore
index 866d745a46..bbe52c94dc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -38,6 +38,7 @@ Makefile.in
 /common/include/test-ascii-ctype
 /common/include/test-byte-swapping
 /common/include/test-checked-overflow
+/common/include/test-human-size
 /common/include/test-isaligned
 /common/include/test-ispowerof2
 /common/include/test-iszero
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index 1acc9fa413..f32cdcf85a 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -24,6 +24,7 @@ EXTRA_DIST = \
byte-swapping.h \
checked-overflow.h \
compiler-macros.h \
+   human-size.h \
isaligned.h \
ispowerof2.h \
iszero.h \
@@ -40,6 +41,7 @@ TESTS = \
test-ascii-ctype \
test-byte-swapping \
test-checked-overflow \
+   test-human-size \
test-isaligned \
test-ispowerof2 \
test-iszero \
@@ -63,6 +65,10 @@ test_checked_overflow_SOURCES = test-checked-overflow.c 
checked-overflow.h
 test_checked_overflow_CPPFLAGS = -I$(srcdir)
 test_checked_overflow_CFLAGS = $(WARNINGS_CFLAGS)
 
+test_human_size_SOURCES = test-human-size.c human-size.h
+test_human_size_CPPFLAGS = -I$(srcdir)
+test_human_size_CFLAGS = $(WARNINGS_CFLAGS)
+
 test_isaligned_SOURCES = test-isaligned.c isaligned.h
 test_isaligned_CPPFLAGS = -I$(srcdir)
 test_isaligned_CFLAGS = $(WARNINGS_CFLAGS)
diff --git a/common/include/human-size.h b/common/include/human-size.h
new file mode 100644
index 00..788dbd7ba5
--- /dev/null
+++ b/common/include/human-size.h
@@ -0,0 +1,137 @@
+/* nbdkit
+ * Copyright Red Hat
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_HUMAN_SIZE_H
+#define NBDKIT_HUMAN_SIZE_H
+
+#include 
+#include 
+#include 
+
+/* Attempt to parse a string with a possible scaling suffix, such as
+ * "2M".  Disk sizes cannot usefully exceed off_t (which is signed)
+ * and cannot be negative.
+ *
+ * On error, returns -1 and sets *error and *pstr.  You can form a
+ * final error message by appending ": ".
+ */
+static inline int64_t
+human_size_parse (const char *str,
+  const char **error, const char **pstr)
+{
+  int64_t size;
+  char *end;
+  uint64_t scale = 1;
+
+  /* XXX Should we also parse things like '1.5M'? */
+  /* XXX Should we allow hex? If so, hex cannot use scaling suffixes,
+   * because some of them are valid hex digits.
+   */
+  errno = 0;
+  size = strtoimax (str, , 10);
+  if (str == end) {
+*error = "could not parse size string";
+*pstr = str;
+return -1;
+  }
+  if (size < 0) {
+*error = "size cannot be negative";
+*pstr = str;
+return -1;
+  }
+  if (errno) {
+*error = "size exceeds maximum value";
+*pstr = str;
+return -1;
+  }
+
+  switch (*end) {
+/* No suffix */
+  case '\0':
+end--; /* Safe, since we already filtered out empty string */
+break;
+
+/* Powers of 1024 */
+  case 'e': case 'E':
+scale *= 1024;
+/* fallthru */
+  case 'p': case 'P':
+scale *= 1024;
+/* fallthru */
+  case 't': case 'T':
+scale *= 1024;
+/* fallthru */
+  case 'g': case 'G':
+scale