[Libguestfs] [PATCH 1/2] mllib: add simple qemu filename sanitizing function

Pino Toscano ptoscano at redhat.com
Fri Jan 16 18:33:19 UTC 2015


It mimics a bit in OCaml the logic used in
commit a95214b1985e694946e3426120a6fdc13a3f081f (for the main library)
and commit 588af1953e5f7ab74009b9175cc5d3efb8bb651a (in disk-create),
so in OCaml tools direct calls to qemu/qemu-img with filenames provided
by the user can properly handle filenames with e.g. ':'.
---
 mllib/common_utils.ml  | 13 +++++++++++++
 mllib/common_utils.mli |  8 ++++++++
 2 files changed, 21 insertions(+)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index b143710..4d3bf95 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -603,3 +603,16 @@ let is_directory path =
 let absolute_path path =
   if not (Filename.is_relative path) then path
   else Sys.getcwd () // path
+
+(* Sanitizes a filename for passing it safely to qemu/qemu-img.
+ *
+ * If the filename is something like "file:foo" then qemu-img will
+ * try to interpret that as "foo" in the file:/// protocol.  To
+ * avoid that, if the path is relative prefix it with "./" since
+ * qemu-img won't try to interpret such a path.
+ *)
+let qemu_input_filename filename =
+  if String.length filename > 0 && filename.[0] <> '/' then
+    "./" ^ filename
+  else
+    filename
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 2103ff1..e59d802 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -131,3 +131,11 @@ val is_directory : string -> bool
 
 val absolute_path : string -> string
 (** Convert any path to an absolute path. *)
+
+val qemu_input_filename : string -> string
+(** Sanitizes a filename for passing it safely to qemu/qemu-img.
+
+    If the filename is something like "file:foo" then qemu-img will
+    try to interpret that as "foo" in the file:/// protocol.  To
+    avoid that, if the path is relative prefix it with "./" since
+    qemu-img won't try to interpret such a path. *)
-- 
1.9.3




More information about the Libguestfs mailing list