Skip to content

Commit

Permalink
Initialize WOW64 in TouchFile custom action
Browse files Browse the repository at this point in the history
Fixes 8638
  • Loading branch information
robmen committed Jul 17, 2024
1 parent bb975d3 commit 8fb5d57
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ext/Util/ca/TouchFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static HRESULT SetExistingFileModifiedTime(
)
{
HRESULT hr = S_OK;
#ifndef _WIN64
BOOL fReenableFileSystemRedirection = FALSE;

if (f64Bit)
Expand All @@ -32,14 +33,20 @@ static HRESULT SetExistingFileModifiedTime(

fReenableFileSystemRedirection = TRUE;
}
#else
UNREFERENCED_PARAMETER(wzId);
UNREFERENCED_PARAMETER(f64Bit);
#endif

hr = FileSetTime(wzPath, NULL, NULL, pftModified);

#ifndef _WIN64
LExit:
if (fReenableFileSystemRedirection)
{
WcaRevertWow64FSRedirection();
}
#endif

return hr;
}
Expand Down Expand Up @@ -83,6 +90,7 @@ static BOOL TryGetExistingFileModifiedTime(
)
{
HRESULT hr = S_OK;
#ifndef _WIN64
BOOL fReenableFileSystemRedirection = FALSE;

if (f64Bit)
Expand All @@ -92,6 +100,10 @@ static BOOL TryGetExistingFileModifiedTime(

fReenableFileSystemRedirection = TRUE;
}
#else
UNREFERENCED_PARAMETER(wzId);
UNREFERENCED_PARAMETER(f64Bit);
#endif

hr = FileGetTime(wzPath, NULL, NULL, pftModified);
if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr)
Expand All @@ -104,11 +116,13 @@ static BOOL TryGetExistingFileModifiedTime(
WcaLog(LOGMSG_STANDARD, "Cannot access modified timestamp for file: '%ls' due to error: 0x%x. Continuing with out rollback for: %ls", wzPath, hr, wzId);
}

#ifndef _WIN64
LExit:
if (fReenableFileSystemRedirection)
{
WcaRevertWow64FSRedirection();
}
#endif

return SUCCEEDED(hr);
}
Expand Down Expand Up @@ -217,9 +231,17 @@ extern "C" UINT WINAPI WixTouchFileDuringInstall(
hr = WcaInitialize(hInstall, "WixTouchFileDuringInstall");
ExitOnFailure(hr, "Failed to initialize WixTouchFileDuringInstall.");

#ifndef _WIN64
WcaInitializeWow64();
#endif

hr = ProcessTouchFileTable(TRUE);

LExit:
#ifndef _WIN64
WcaFinalizeWow64();
#endif

DWORD er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
Expand All @@ -236,9 +258,17 @@ extern "C" UINT WINAPI WixTouchFileDuringUninstall(
hr = WcaInitialize(hInstall, "WixTouchFileDuringUninstall");
ExitOnFailure(hr, "Failed to initialize WixTouchFileDuringUninstall.");

#ifndef _WIN64
WcaInitializeWow64();
#endif

hr = ProcessTouchFileTable(FALSE);

LExit:
#ifndef _WIN64
WcaFinalizeWow64();
#endif

DWORD er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
Expand All @@ -261,6 +291,10 @@ extern "C" UINT WINAPI WixExecuteTouchFile(
hr = WcaInitialize(hInstall, "WixExecuteTouchFile");
ExitOnFailure(hr, "Failed to initialize WixExecuteTouchFile.");

#ifndef _WIN64
WcaInitializeWow64();
#endif

hr = WcaGetProperty(L"CustomActionData", &sczData);
ExitOnFailure(hr, "Failed to get custom action data for WixExecuteTouchFile.");

Expand Down Expand Up @@ -299,6 +333,10 @@ extern "C" UINT WINAPI WixExecuteTouchFile(
}

LExit:
#ifndef _WIN64
WcaFinalizeWow64();
#endif

ReleaseStr(sczPath);
ReleaseStr(sczId);
ReleaseStr(sczData);
Expand Down
8 changes: 8 additions & 0 deletions src/test/msi/TestData/TouchFileTests/TouchFile/Package.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util">
<Package Name="~TouchFile" Version="1.0.0" Manufacturer="Example Corporation" UpgradeCode="047730a5-30fe-4a62-a520-da9381b8226a">
<Component>
<File Source="$(sys.SOURCEFILEPATH)" />
<util:TouchFile Path="[LocalAppDataFolder]touch-file-test.txt" />
</Component>
</Package>
</Wix>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. -->
<Project Sdk="WixToolset.Sdk">
<ItemGroup>
<PackageReference Include="WixToolset.Util.wixext" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions src/test/msi/WixToolsetTest.MsiE2E/TouchFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolsetTest.MsiE2E;

using System;
using System.IO;
using WixTestTools;
using Xunit;
using Xunit.Abstractions;

public class TouchFileTests : MsiE2ETests
{
public TouchFileTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[RuntimeFact]
public void CanValidateTouchFile()
{
var touchFileTestPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "touch-file-test.txt");

try
{
var touchFileTime = new DateTime(2004, 4, 5, 0, 0, 0, DateTimeKind.Utc);

File.WriteAllText(touchFileTestPath, "This file exists to test CanValidateTouchFile()");
File.SetCreationTimeUtc(touchFileTestPath, touchFileTime);
File.SetLastAccessTimeUtc(touchFileTestPath, touchFileTime);
File.SetLastWriteTimeUtc(touchFileTestPath, touchFileTime);

var product = this.CreatePackageInstaller("TouchFile");

var justBeforeInstall = DateTime.UtcNow;
product.InstallProduct(MSIExec.MSIExecReturnCode.SUCCESS);

var touchFile = new FileInfo(touchFileTestPath);
Assert.Equal(touchFileTime, touchFile.CreationTimeUtc);
Assert.Equal(touchFileTime, touchFile.LastAccessTimeUtc);
Assert.True(touchFile.LastWriteTimeUtc >= justBeforeInstall, $"Touch file {touchFileTestPath} last write time: {touchFile.LastWriteTimeUtc} of file should have been updated to at least: {justBeforeInstall}");

product.UninstallProduct(MSIExec.MSIExecReturnCode.SUCCESS);
}
finally
{
File.Delete(touchFileTestPath);
}
}
}

0 comments on commit 8fb5d57

Please sign in to comment.