Skip to content

Commit

Permalink
Fix finding a component with --component-code
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbar committed Jun 13, 2024
1 parent 89e0e42 commit cc2a876
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
version:
description: 'Build & package version'
required: true
default: 0.1.4
default: 0.1.5
type: string
jobs:
Build:
Expand Down
81 changes: 51 additions & 30 deletions MsiZapEx/ComponentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

namespace MsiZapEx
{
Expand Down Expand Up @@ -40,43 +41,31 @@ internal ProductKeyPath(Guid productCode, string keyPath, bool exists)
public List<ProductKeyPath> ProductsKeyPath { get; } = new List<ProductKeyPath>();
private static List<ComponentInfo> _components = new List<ComponentInfo>();

private static ManualResetEventSlim _componentsLock = new ManualResetEventSlim(false);
internal static List<ComponentInfo> GetAllComponents()
{
if (_components.Count > 0)
if (_componentsLock.IsSet)
{
return _components;
}

using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
try
{
using (RegistryKey k = hklm.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{ProductInfo.LocalSystemSID}\Components", false))
_componentsLock.Set();
if (_components.Count > 0)
{
if (k == null)
{
throw new FileNotFoundException();
}
return _components;
}

string[] componentCodes = k.GetSubKeyNames();
foreach (string c in componentCodes)
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (RegistryKey k = hklm.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{ProductInfo.LocalSystemSID}\Components", false))
{
// Ignore default value
if (string.IsNullOrWhiteSpace(c) || c.Equals("@") || !Guid.TryParse(c, out Guid id) || id.Equals(Guid.Empty))
if (k == null)
{
continue;
throw new FileNotFoundException();
}

Guid componentCode = GuidEx.MsiObfuscate(c);
ComponentInfo ci = _components.FirstOrDefault(cc => cc.MachineScope && cc.ComponentCode.Equals(componentCode));
if (ci == null)
{
ci = new ComponentInfo(c, true);
}
}
}
using (RegistryKey k = hklm.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{ProductInfo.CurrentUserSID}\Components", false))
{
if (k != null)
{
string[] componentCodes = k.GetSubKeyNames();
foreach (string c in componentCodes)
{
Expand All @@ -87,15 +76,41 @@ internal static List<ComponentInfo> GetAllComponents()
}

Guid componentCode = GuidEx.MsiObfuscate(c);
ComponentInfo ci = _components.FirstOrDefault(cc => !cc.MachineScope && cc.ComponentCode.Equals(componentCode));
ComponentInfo ci = _components.FirstOrDefault(cc => cc.MachineScope && cc.ComponentCode.Equals(componentCode));
if (ci == null)
{
ci = new ComponentInfo(c, false);
ci = new ComponentInfo(c, true);
}
}
}
using (RegistryKey k = hklm.OpenSubKey($@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{ProductInfo.CurrentUserSID}\Components", false))
{
if (k != null)
{
string[] componentCodes = k.GetSubKeyNames();
foreach (string c in componentCodes)
{
// Ignore default value
if (string.IsNullOrWhiteSpace(c) || c.Equals("@") || !Guid.TryParse(c, out Guid id) || id.Equals(Guid.Empty))
{
continue;
}

Guid componentCode = GuidEx.MsiObfuscate(c);
ComponentInfo ci = _components.FirstOrDefault(cc => !cc.MachineScope && cc.ComponentCode.Equals(componentCode));
if (ci == null)
{
ci = new ComponentInfo(c, false);
}
}
}
}
}
}
finally
{
_componentsLock.Reset();
}
return _components;
}

Expand Down Expand Up @@ -131,13 +146,18 @@ public ComponentInfo(Guid componentCode, bool? machineScope = null)

internal ComponentInfo(string obfuscatedGuid, bool? machineScope = null)
{
Guid componentCode = GuidEx.MsiObfuscate(obfuscatedGuid);
this.MachineScope = machineScope ?? ResolveScope(componentCode);
ComponentInfo ci = _components.FirstOrDefault(c => c.MachineScope == machineScope && c.ComponentCode.Equals(componentCode));
GetAllComponents();

ComponentCode = GuidEx.MsiObfuscate(obfuscatedGuid);
if (machineScope == null)
{
machineScope = ResolveScope(ComponentCode);
}
this.MachineScope = machineScope.Value;
ComponentInfo ci = _components.FirstOrDefault(c => c.MachineScope == machineScope && c.ComponentCode.Equals(ComponentCode));
if (ci != null)
{
this.Status = ci.Status;
this.ComponentCode = ci.ComponentCode;
this.ProductsKeyPath.AddRange(ci.ProductsKeyPath);
return;
}
Expand Down Expand Up @@ -183,6 +203,7 @@ internal void PrintProducts()
if (ProductsKeyPath.Count == 0)
{
Console.WriteLine($"Component '{ComponentCode}' is not related to any product");
return;
}

Console.WriteLine($"Component '{ComponentCode}' belongs to {ProductsKeyPath.Count} products");
Expand Down
1 change: 1 addition & 0 deletions MsiZapEx/ProductInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public void PrintState()
if (Settings.Instance.Verbose)
{
Console.WriteLine($"\tFeatures: {Features.Aggregate((a, c) => $"{a}, {c}")}");
Console.WriteLine($"\tLocalPackage: {LocalPackage}");
}
}

Expand Down

0 comments on commit cc2a876

Please sign in to comment.