supermin/0001-utils-import-parse_size-from-libguestfs.patch
Richard W.M. Jones 22a6b3ebe7 Pull in all upstream patches since 5.1.13.
- Choose providers better (RHBZ#1266918).
- Use autopatch.
2015-10-13 16:28:41 +01:00

54 lines
1.6 KiB
Diff

From b6cf15d8f38270d6d6c9df2c8589829c9a3ad176 Mon Sep 17 00:00:00 2001
From: Pino Toscano <ptoscano@redhat.com>
Date: Mon, 13 Jul 2015 17:32:33 +0200
Subject: [PATCH 1/7] utils: import parse_size from libguestfs
---
src/utils.ml | 21 +++++++++++++++++++++
src/utils.mli | 3 +++
2 files changed, 24 insertions(+)
diff --git a/src/utils.ml b/src/utils.ml
index 3e81c21..7ae24bd 100644
--- a/src/utils.ml
+++ b/src/utils.ml
@@ -204,3 +204,24 @@ let compare_architecture a1 a2 =
exit 1
in
compare (index_of_architecture a1) (index_of_architecture a2)
+
+(* Parse a size field, eg. "10G". *)
+let parse_size =
+ let const_re = Str.regexp "^\\([.0-9]+\\)\\([bKMG]\\)$" in
+ fun field ->
+ let matches rex = Str.string_match rex field 0 in
+ let sub i = Str.matched_group i field in
+ let size_scaled f = function
+ | "b" -> Int64.of_float f
+ | "K" -> Int64.of_float (f *. 1024.)
+ | "M" -> Int64.of_float (f *. 1024. *. 1024.)
+ | "G" -> Int64.of_float (f *. 1024. *. 1024. *. 1024.)
+ | _ -> assert false
+ in
+
+ if matches const_re then (
+ size_scaled (float_of_string (sub 1)) (sub 2)
+ ) else (
+ eprintf "supermin: cannot parse size field '%s'\n" field;
+ exit 1
+ )
diff --git a/src/utils.mli b/src/utils.mli
index 5de940d..7896e34 100644
--- a/src/utils.mli
+++ b/src/utils.mli
@@ -90,3 +90,6 @@ val compare_version : string -> string -> int
val compare_architecture : string -> string -> int
(** Compare two architecture strings. *)
+
+val parse_size : string -> int64
+(** Parse a size field, eg. [10G] *)
--
2.5.0