Blob Blame History Raw
From 851928b06c6b61dca845c971e80e8e0a85ef9482 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Tue, 1 Sep 2015 14:56:10 +0100
Subject: [PATCH] v2v: Fix handling of extra slashes in dcPath calculation
 (RHBZ#1258342).

This updates commit 20f1eb400b13be8733b6586769c4845b99a70722.

Thanks: Tingting Zheng
(cherry picked from commit 51bc573d0c4e78104a682e7c42d63d701aedd093)
---
 v2v/input_libvirt_vcenter_https.ml | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/v2v/input_libvirt_vcenter_https.ml b/v2v/input_libvirt_vcenter_https.ml
index 295dded..dd02feb 100644
--- a/v2v/input_libvirt_vcenter_https.ml
+++ b/v2v/input_libvirt_vcenter_https.ml
@@ -170,6 +170,8 @@ and run_curl_get_lines curl_args =
   Unix.unlink config_file;
   lines
 
+let multiple_slash = Str.regexp "/+"
+
 (* Helper function to extract the dcPath from a URI. *)
 let get_dcPath uri scheme =
   let default_dc = "ha-datacenter" in
@@ -183,6 +185,10 @@ let get_dcPath uri scheme =
       (* vCenter: URIs are *usually* '/Folder/Datacenter/esxi' so we can
        * just chop off the first '/' and final '/esxi' to get the dcPath.
        *
+       * The libvirt driver allows things like '/DC///esxi////' so we also
+       * have to handle trailing slashes and collapse multiple slashes into
+       * single (RHBZ#1258342).
+       *
        * However if there is a cluster involved then the URI may be
        * /Folder/Datacenter/Cluster/esxi but dcPath=Folder/Datacenter/Cluster
        * won't work.  In this case the user has to adjust the path to
@@ -190,12 +196,21 @@ let get_dcPath uri scheme =
        * should be a way to ask the libvirt vpx driver for the correct
        * path, but there isn't. XXX  See also RHBZ#1256823.
        *)
-      let path =                (* chop off the first '/' *)
+      (* Collapse multiple slashes to single slash. *)
+      let path = Str.global_replace multiple_slash "/" path in
+      (* Chop off the first and trailing '/' (if found). *)
+      let path =
         let len = String.length path in
         if len > 0 && path.[0] = '/' then
           String.sub path 1 (len-1)
         else path in
-      let len =                 (* chop off the final element (ESXi hostname) *)
+      let path =
+        let len = String.length path in
+        if len > 0 && path.[len-1] = '/' then
+          String.sub path 0 (len-1)
+        else path in
+      (* Chop off the final element (ESXi hostname). *)
+      let len =
         try String.rindex path '/' with Not_found -> String.length path in
       String.sub path 0 len
     );
-- 
1.8.3.1