Blame SOURCES/0027-v2v-windows-Refactor-uninstallation_commands-functio.patch

7ed5e3
From 9c81b523857e057b8361cbbcc4647ed02b572ca0 Mon Sep 17 00:00:00 2001
7ed5e3
From: "Richard W.M. Jones" <rjones@redhat.com>
7ed5e3
Date: Tue, 19 Jan 2021 11:38:46 +0000
7ed5e3
Subject: [PATCH] v2v: windows: Refactor uninstallation_commands function.
7ed5e3
7ed5e3
Simplify and shorten this function:
7ed5e3
7ed5e3
 - Remove unnecessary use of Not_found exception and generally
7ed5e3
   simplify flow control.
7ed5e3
7ed5e3
 - Use sprintf.
7ed5e3
7ed5e3
This shouldn't change what the function does.
7ed5e3
7ed5e3
(cherry picked from commit d48530209a79725f26d6e25101bed6f228f45e8d)
7ed5e3
---
7ed5e3
 v2v/convert_windows.ml | 89 ++++++++++++++++++------------------------
7ed5e3
 1 file changed, 37 insertions(+), 52 deletions(-)
7ed5e3
7ed5e3
diff --git a/v2v/convert_windows.ml b/v2v/convert_windows.ml
7ed5e3
index f2f7b95c..84db742f 100644
7ed5e3
--- a/v2v/convert_windows.ml
7ed5e3
+++ b/v2v/convert_windows.ml
7ed5e3
@@ -135,56 +135,41 @@ let convert (g : G.guestfs) inspect _ output rcaps static_ips =
7ed5e3
   (* Locate and retrieve all the uninstallation commands for installed
7ed5e3
    * applications.
7ed5e3
    *)
7ed5e3
-  let uninstallation_commands pretty_name matchfn extra_uninstall_string =
7ed5e3
-    let uninsts = ref [] in
7ed5e3
+  let uninstallation_commands pretty_name matchfn extra_uninstall_params =
7ed5e3
+    let path = ["Microsoft"; "Windows"; "CurrentVersion"; "Uninstall"] in
7ed5e3
+    let uninstval = "UninstallString" in
7ed5e3
+    let ret = ref [] in
7ed5e3
 
7ed5e3
-    Registry.with_hive_readonly g inspect.i_windows_software_hive
7ed5e3
-      (fun reg ->
7ed5e3
-       try
7ed5e3
-         let path = ["Microsoft"; "Windows"; "CurrentVersion"; "Uninstall"] in
7ed5e3
-         let node =
7ed5e3
-           match Registry.get_node reg path with
7ed5e3
-           | None -> raise Not_found
7ed5e3
-           | Some node -> node in
7ed5e3
-         let uninstnodes = g#hivex_node_children node in
7ed5e3
-
7ed5e3
-         Array.iter (
7ed5e3
-           fun { G.hivex_node_h = uninstnode } ->
7ed5e3
-             try
7ed5e3
+    Registry.with_hive_readonly g inspect.i_windows_software_hive (
7ed5e3
+      fun reg ->
7ed5e3
+        match Registry.get_node reg path with
7ed5e3
+        | None -> ()
7ed5e3
+        | Some node ->
7ed5e3
+           let uninstnodes = g#hivex_node_children node in
7ed5e3
+           Array.iter (
7ed5e3
+             fun { G.hivex_node_h = uninstnode } ->
7ed5e3
                let valueh = g#hivex_node_get_value uninstnode "DisplayName" in
7ed5e3
-               if valueh = 0L then
7ed5e3
-                 raise Not_found;
7ed5e3
-
7ed5e3
-               let dispname = g#hivex_value_string valueh in
7ed5e3
-               if not (matchfn dispname) then
7ed5e3
-                 raise Not_found;
7ed5e3
-
7ed5e3
-               let uninstval = "UninstallString" in
7ed5e3
-               let valueh = g#hivex_node_get_value uninstnode uninstval in
7ed5e3
-               if valueh = 0L then (
7ed5e3
-                 let name = g#hivex_node_name uninstnode in
7ed5e3
-                 warning (f_"cannot uninstall %s: registry key ‘HKLM\\SOFTWARE\\%s\\%s’ with DisplayName ‘%s’ doesn't contain value ‘%s’")
7ed5e3
-                    pretty_name (String.concat "\\" path) name dispname uninstval;
7ed5e3
-                 raise Not_found
7ed5e3
-               );
7ed5e3
-
7ed5e3
-               let uninst = (g#hivex_value_string valueh) ^
7ed5e3
-                     " /quiet /norestart /l*v+ \"%~dpn0.log\"" ^
7ed5e3
-                     " REBOOT=ReallySuppress REMOVE=ALL" in
7ed5e3
-               let uninst =
7ed5e3
-                 match extra_uninstall_string with
7ed5e3
-                 | None -> uninst
7ed5e3
-                 | Some s -> uninst ^ " " ^ s in
7ed5e3
-
7ed5e3
-               List.push_front uninst uninsts
7ed5e3
-             with
7ed5e3
-               Not_found -> ()
7ed5e3
-         ) uninstnodes
7ed5e3
-       with
7ed5e3
-         Not_found -> ()
7ed5e3
-      );
7ed5e3
-
7ed5e3
-    !uninsts
7ed5e3
+               if valueh <> 0L then (
7ed5e3
+                 let dispname = g#hivex_value_string valueh in
7ed5e3
+                 if matchfn dispname then (
7ed5e3
+                   let valueh = g#hivex_node_get_value uninstnode uninstval in
7ed5e3
+                   if valueh <> 0L then (
7ed5e3
+                     let reg_cmd = g#hivex_value_string valueh in
7ed5e3
+                     let cmd =
7ed5e3
+                       sprintf "%s /quiet /norestart /l*v+ \"%%~dpn0.log\" REBOOT=ReallySuppress REMOVE=ALL %s"
7ed5e3
+                         reg_cmd extra_uninstall_params in
7ed5e3
+                     List.push_front cmd ret
7ed5e3
+                   )
7ed5e3
+                   else
7ed5e3
+                     let name = g#hivex_node_name uninstnode in
7ed5e3
+                     warning (f_"cannot uninstall %s: registry key ‘HKLM\\SOFTWARE\\%s\\%s’ with DisplayName ‘%s’ doesn't contain value ‘%s’")
7ed5e3
+                       pretty_name (String.concat "\\" path) name
7ed5e3
+                       dispname uninstval
7ed5e3
+                 )
7ed5e3
+               )
7ed5e3
+             ) uninstnodes
7ed5e3
+    ) (* with_hive_readonly *);
7ed5e3
+    !ret
7ed5e3
   in
7ed5e3
 
7ed5e3
   (* Locate and retrieve all uninstallation commands for Parallels Tools. *)
7ed5e3
@@ -196,16 +181,16 @@ let convert (g : G.guestfs) inspect _ output rcaps static_ips =
7ed5e3
     (* Without these custom Parallels-specific MSI properties the
7ed5e3
      * uninstaller still shows a no-way-out reboot dialog.
7ed5e3
      *)
7ed5e3
-    let extra_uninstall_string =
7ed5e3
-      Some "PREVENT_REBOOT=Yes LAUNCHED_BY_SETUP_EXE=Yes" in
7ed5e3
-    uninstallation_commands "Parallels Tools" matchfn extra_uninstall_string in
7ed5e3
+    let extra_uninstall_params =
7ed5e3
+      "PREVENT_REBOOT=Yes LAUNCHED_BY_SETUP_EXE=Yes" in
7ed5e3
+    uninstallation_commands "Parallels Tools" matchfn extra_uninstall_params in
7ed5e3
 
7ed5e3
   (* Locate and retrieve all uninstallation commands for VMware Tools. *)
7ed5e3
   let vmwaretools_uninst =
7ed5e3
     let matchfn s =
7ed5e3
       String.find s "VMware Tools" != -1
7ed5e3
     in
7ed5e3
-    uninstallation_commands "VMware Tools" matchfn None in
7ed5e3
+    uninstallation_commands "VMware Tools" matchfn "" in
7ed5e3
 
7ed5e3
   (*----------------------------------------------------------------------*)
7ed5e3
   (* Perform the conversion of the Windows guest. *)
7ed5e3
-- 
7ed5e3
2.18.4
7ed5e3