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

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