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

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