Skip to content

Commit

Permalink
Switch from PInvoke nuget dependency to CsWin32
Browse files Browse the repository at this point in the history
Fixes #615
  • Loading branch information
AArnott committed Jun 12, 2021
1 parent b7707b3 commit 69c33ed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using static PInvoke.Kernel32;
using FileShare = PInvoke.Kernel32.FileShare;
using Microsoft.Windows.Sdk;

namespace Nerdbank.GitVersioning.ManagedGit
{
Expand All @@ -24,7 +23,7 @@ internal static bool TryOpen(string path, out FileStream? stream)
{
if (IsWindows)
{
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);
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);

if (!handle.IsInvalid)
{
Expand Down Expand Up @@ -62,12 +61,15 @@ internal static unsafe bool TryOpen(ReadOnlySpan<char> path, [NotNullWhen(true)]
{
if (IsWindows)
{
var handle = CreateFile(path, ACCESS_MASK.GenericRight.GENERIC_READ, FileShare.FILE_SHARE_READ, null, CreationDisposition.OPEN_EXISTING, CreateFileFlags.FILE_ATTRIBUTE_NORMAL, SafeObjectHandle.Null);
HANDLE handle;
fixed (char* pPath = &path[0])
{
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);
}

if (!handle.IsInvalid)
if (!handle.Equals(Constants.INVALID_HANDLE_VALUE))
{
var fileHandle = new SafeFileHandle(handle.DangerousGetHandle(), ownsHandle: true);
handle.SetHandleAsInvalid();
var fileHandle = new SafeFileHandle(handle, ownsHandle: true);
stream = new FileStream(fileHandle, System.IO.FileAccess.Read);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/NerdBank.GitVersioning/NativeMethods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json"
}
2 changes: 2 additions & 0 deletions src/NerdBank.GitVersioning/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CreateFile
INVALID_HANDLE_VALUE
2 changes: 1 addition & 1 deletion src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<PackageReference Include="DotNetMDDocs" Version="0.111.0" PrivateAssets="all" Condition=" '$(GenerateMarkdownApiDocs)' == 'true' " />
<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0096" PrivateAssets="none" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.422-beta" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
<PackageReference Include="Validation" Version="2.5.5-beta" />
<PackageReference Include="Nerdbank.GitVersioning.LKG" Version="3.4.173-alpha" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared\**\*.cs" LinkBase="Shared" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj
<file src="$BaseOutputPath$net461\NerdBank.GitVersioning.dll" target="build\MSBuildFull\NerdBank.GitVersioning.dll" />
<file src="$BaseOutputPath$net461\Nerdbank.GitVersioning.Tasks.dll" target="build\MSBuildFull\Nerdbank.GitVersioning.Tasks.dll" />
<file src="$BaseOutputPath$net461\Newtonsoft.Json.dll" target="build\MSBuildFull\Newtonsoft.Json.dll" />
<file src="$BaseOutputPath$net461\PInvoke.Kernel32.dll" target="build\MSBuildFull\PInvoke.Kernel32.dll" />
<file src="$BaseOutputPath$net461\System.Buffers.dll" target="build\MSBuildFull\System.Buffers.dll" />
<file src="$BaseOutputPath$net461\System.Memory.dll" target="build\MSBuildFull\System.Memory.dll" />
<file src="$BaseOutputPath$net461\System.Numerics.Vectors.dll" target="build\MSBuildFull\System.Numerics.Vectors.dll" />
Expand All @@ -49,7 +48,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj
<file src="$BaseOutputPath$netcoreapp2.1\System.Text.Json.dll" target="build\MSBuildCore\System.Text.Json.dll" />
<file src="$BaseOutputPath$netcoreapp2.1\Validation.dll" target="build\MSBuildCore\Validation.dll" />
<file src="$BaseOutputPath$netcoreapp2.1\System.Runtime.CompilerServices.Unsafe.dll" target="build\MSBuildCore\System.Runtime.CompilerServices.Unsafe.dll" />
<file src="$BaseOutputPath$netcoreapp2.1\PInvoke.Kernel32.dll" target="build\MSBuildCore\PInvoke.Kernel32.dll" />

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

0 comments on commit 69c33ed

Please sign in to comment.