[Libguestfs] [PATCH 1/2] mllib: add normalize_arch helper

Pino Toscano ptoscano at redhat.com
Tue Aug 11 13:45:11 UTC 2015


Small helper to normalize an architecture string, so it is easier to
compare them and check for compatibilities.

Make use of it in guest_arch_compatible, simplifying it.
---
 mllib/common_utils.ml  | 12 ++++++++++--
 mllib/common_utils.mli |  5 +++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index f9e8996..ca6d470 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -730,13 +730,21 @@ let rec mkdir_p path permissions =
     mkdir_p (Filename.dirname path) permissions;
     Unix.mkdir path permissions
 
+let normalize_arch = function
+  | "i386" | "i486" | "i586" | "i686" -> "i386"
+  | "amd64" | "x86_64" -> "x86_64"
+  | "powerpc" | "ppc" -> "ppc"
+  | arch -> arch
+
 (* Are guest arch and host_cpu compatible, in terms of being able
  * to run commands in the libguestfs appliance?
  *)
 let guest_arch_compatible guest_arch =
-  match Config.host_cpu, guest_arch with
+  let own = normalize_arch Config.host_cpu in
+  let guest_arch = normalize_arch guest_arch in
+  match own, guest_arch with
   | x, y when x = y -> true
-  | "x86_64", ("i386"|"i486"|"i586"|"i686") -> true
+  | "x86_64", "i386" -> true
   | _ -> false
 
 (** Return the last part of a string, after the specified separator. *)
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 16834f7..ac232af 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -175,6 +175,11 @@ val qemu_input_filename : string -> string
 val mkdir_p : string -> int -> unit
 (** Creates a directory, and its parents if missing. *)
 
+val normalize_arch : string -> string
+(** Normalize the architecture name, i.e. maps it into a defined
+    identifier for it -- e.g. i386, i486, i586, and i686 are
+    normalized as i386. *)
+
 val guest_arch_compatible : string -> bool
 (** Are guest arch and host_cpu compatible, in terms of being able
     to run commands in the libguestfs appliance? *)
-- 
2.1.0




More information about the Libguestfs mailing list