Skip to content

Commit 69c33ed

Browse files
committed
Switch from PInvoke nuget dependency to CsWin32
Fixes #615
1 parent b7707b3 commit 69c33ed

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
using System.IO;
66
using System.Runtime.InteropServices;
77
using Microsoft.Win32.SafeHandles;
8-
using static PInvoke.Kernel32;
9-
using FileShare = PInvoke.Kernel32.FileShare;
8+
using Microsoft.Windows.Sdk;
109

1110
namespace Nerdbank.GitVersioning.ManagedGit
1211
{
@@ -24,7 +23,7 @@ internal static bool TryOpen(string path, out FileStream? stream)
2423
{
2524
if (IsWindows)
2625
{
27-
var handle = CreateFile(path, ACCESS_MASK.GenericRight.GENERIC_READ, FileShare.FILE_SHARE_READ, (SECURITY_ATTRIBUTES?)null, CreationDisposition.OPEN_EXISTING, CreateFileFlags.FILE_ATTRIBUTE_NORMAL, SafeObjectHandle.Null);
26+
var handle = PInvoke.CreateFile(path, FILE_ACCESS_FLAGS.FILE_GENERIC_READ, FILE_SHARE_FLAGS.FILE_SHARE_READ, lpSecurityAttributes: null, FILE_CREATE_FLAGS.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, null);
2827

2928
if (!handle.IsInvalid)
3029
{
@@ -62,12 +61,15 @@ internal static unsafe bool TryOpen(ReadOnlySpan<char> path, [NotNullWhen(true)]
6261
{
6362
if (IsWindows)
6463
{
65-
var handle = CreateFile(path, ACCESS_MASK.GenericRight.GENERIC_READ, FileShare.FILE_SHARE_READ, null, CreationDisposition.OPEN_EXISTING, CreateFileFlags.FILE_ATTRIBUTE_NORMAL, SafeObjectHandle.Null);
64+
HANDLE handle;
65+
fixed (char* pPath = &path[0])
66+
{
67+
handle = PInvoke.CreateFile(pPath, FILE_ACCESS_FLAGS.FILE_GENERIC_READ, FILE_SHARE_FLAGS.FILE_SHARE_READ, null, FILE_CREATE_FLAGS.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, default);
68+
}
6669

67-
if (!handle.IsInvalid)
70+
if (!handle.Equals(Constants.INVALID_HANDLE_VALUE))
6871
{
69-
var fileHandle = new SafeFileHandle(handle.DangerousGetHandle(), ownsHandle: true);
70-
handle.SetHandleAsInvalid();
72+
var fileHandle = new SafeFileHandle(handle, ownsHandle: true);
7173
stream = new FileStream(fileHandle, System.IO.FileAccess.Read);
7274
return true;
7375
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$schema": "https://aka.ms/CsWin32.schema.json"
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CreateFile
2+
INVALID_HANDLE_VALUE

src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<PackageReference Include="DotNetMDDocs" Version="0.111.0" PrivateAssets="all" Condition=" '$(GenerateMarkdownApiDocs)' == 'true' " />
1313
<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0096" PrivateAssets="none" />
1414
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="2.1.0" />
15+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.422-beta" PrivateAssets="all" />
1516
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
1617
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
1718
<PackageReference Include="Validation" Version="2.5.5-beta" />
1819
<PackageReference Include="Nerdbank.GitVersioning.LKG" Version="3.4.173-alpha" />
1920
<PackageReference Include="System.Text.Json" Version="4.7.2" />
20-
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
2121
</ItemGroup>
2222
<ItemGroup>
2323
<Compile Include="..\Shared\**\*.cs" LinkBase="Shared" />

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj
2323
<file src="$BaseOutputPath$net461\NerdBank.GitVersioning.dll" target="build\MSBuildFull\NerdBank.GitVersioning.dll" />
2424
<file src="$BaseOutputPath$net461\Nerdbank.GitVersioning.Tasks.dll" target="build\MSBuildFull\Nerdbank.GitVersioning.Tasks.dll" />
2525
<file src="$BaseOutputPath$net461\Newtonsoft.Json.dll" target="build\MSBuildFull\Newtonsoft.Json.dll" />
26-
<file src="$BaseOutputPath$net461\PInvoke.Kernel32.dll" target="build\MSBuildFull\PInvoke.Kernel32.dll" />
2726
<file src="$BaseOutputPath$net461\System.Buffers.dll" target="build\MSBuildFull\System.Buffers.dll" />
2827
<file src="$BaseOutputPath$net461\System.Memory.dll" target="build\MSBuildFull\System.Memory.dll" />
2928
<file src="$BaseOutputPath$net461\System.Numerics.Vectors.dll" target="build\MSBuildFull\System.Numerics.Vectors.dll" />
@@ -49,7 +48,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj
4948
<file src="$BaseOutputPath$netcoreapp2.1\System.Text.Json.dll" target="build\MSBuildCore\System.Text.Json.dll" />
5049
<file src="$BaseOutputPath$netcoreapp2.1\Validation.dll" target="build\MSBuildCore\Validation.dll" />
5150
<file src="$BaseOutputPath$netcoreapp2.1\System.Runtime.CompilerServices.Unsafe.dll" target="build\MSBuildCore\System.Runtime.CompilerServices.Unsafe.dll" />
52-
<file src="$BaseOutputPath$netcoreapp2.1\PInvoke.Kernel32.dll" target="build\MSBuildCore\PInvoke.Kernel32.dll" />
5351

5452
<file src="build\Nerdbank.GitVersioning.targets" target="build\Nerdbank.GitVersioning$LKGSuffix$.targets" />
5553
<file src="build\Nerdbank.GitVersioning.Common.targets" target="build\Nerdbank.GitVersioning.Common.targets" />

0 commit comments

Comments
 (0)