Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 120 additions & 183 deletions .github/skills/cswin32-com/SKILL.md

Large diffs are not rendered by default.

61 changes: 35 additions & 26 deletions src/Build.UnitTests/BackEnd/TargetUpToDateChecker_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.Win32.SafeHandles;
using Xunit;
#if FEATURE_WINDOWSINTEROP
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Storage.FileSystem;
#endif

Expand Down Expand Up @@ -1005,26 +1007,36 @@ public void NewSymlinkNewDestinationIsNotUpToDate()
expectedOutOfDate: true);
}

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
[SupportedOSPlatform("windows6.1")]
private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, UInt32 dwFlags);
private static unsafe bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, uint dwFlags)
{
fixed (char* pSym = lpSymlinkFileName)
{
fixed (char* pTarget = lpTargetFileName)
{
return PInvoke.CreateSymbolicLink(new PCWSTR(pSym), new PCWSTR(pTarget), (SYMBOLIC_LINK_FLAGS)dwFlags) != 0;
}
}
}

[DllImport("kernel32.dll", SetLastError = true)]
[SupportedOSPlatform("windows6.1")]
private static extern bool SetFileTime(SafeFileHandle hFile, ref long creationTime,
ref long lastAccessTime, ref long lastWriteTime);
private static unsafe SafeFileHandle CreateFileForSymlink(string path)
{
HANDLE h;
fixed (char* pPath = path)
{
h = PInvoke.CreateFile(
new PCWSTR(pPath),
(uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | 0x100 /* FILE_WRITE_ATTRIBUTES */,
FILE_SHARE_MODE.FILE_SHARE_READ,
lpSecurityAttributes: null,
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL | FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_OPEN_REPARSE_POINT,
hTemplateFile: default);
}

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateFileW")]
[SupportedOSPlatform("windows6.1")]
private static extern SafeFileHandle CreateFileForSymlink(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
return new SafeFileHandle((IntPtr)h.Value, ownsHandle: true);
}

[SupportedOSPlatform("windows6.1")]
private void SimpleSymlinkInputCheck(DateTime symlinkWriteTime, DateTime targetWriteTime,
Expand Down Expand Up @@ -1052,24 +1064,21 @@ private void SimpleSymlinkInputCheck(DateTime symlinkWriteTime, DateTime targetW

// File.SetLastWriteTime on the symlink sets the target write time,
// so set the symlink's write time the hard way
using (SafeFileHandle handle = CreateFileForSymlink(
inputSymlink,
(uint)FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | 0x100 /* FILE_WRITE_ATTRIBUTES */,
(uint)FILE_SHARE_MODE.FILE_SHARE_READ,
IntPtr.Zero,
(uint)FILE_CREATION_DISPOSITION.OPEN_EXISTING,
(uint)(FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL | FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_OPEN_REPARSE_POINT),
IntPtr.Zero))
using (SafeFileHandle handle = CreateFileForSymlink(inputSymlink))
{
if (handle.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

long symlinkWriteTimeTicks = symlinkWriteTime.ToFileTimeUtc();
System.Runtime.InteropServices.ComTypes.FILETIME ft = new()
{
dwLowDateTime = (int)(symlinkWriteTimeTicks & 0xFFFFFFFF),
dwHighDateTime = (int)(symlinkWriteTimeTicks >> 32),
};

if (!SetFileTime(handle, ref symlinkWriteTimeTicks, ref symlinkWriteTimeTicks,
ref symlinkWriteTimeTicks))
if (!PInvoke.SetFileTime((HANDLE)handle.DangerousGetHandle(), ft, ft, ft))
{
Comment thread
JeremyKuhne marked this conversation as resolved.
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<NoWarn>$(NoWarn);MSB3270;CS0436</NoWarn>

<AppConfig>..\Shared\UnitTests\App.config</AppConfig>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
229 changes: 0 additions & 229 deletions src/Build/BackEnd/Node/NativeMethods.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Build/Microsoft.Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and '$(DotNetBuildSourceOnly)' != 'true'">
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" PrivateAssets="all" />
Comment thread
JeremyKuhne marked this conversation as resolved.
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System.IO.Compression" />
<PackageReference Include="System.Memory" />
Expand Down
Loading
Loading