From 69c33ed0be29de36e9fbd3188b55d0cd165d578a Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sat, 12 Jun 2021 16:20:35 -0600 Subject: [PATCH 1/3] Switch from PInvoke nuget dependency to CsWin32 Fixes #615 --- .../ManagedGit/FileHelpers.cs | 16 +++++++++------- src/NerdBank.GitVersioning/NativeMethods.json | 3 +++ src/NerdBank.GitVersioning/NativeMethods.txt | 2 ++ .../NerdBank.GitVersioning.csproj | 2 +- .../Nerdbank.GitVersioning.nuspec | 2 -- 5 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 src/NerdBank.GitVersioning/NativeMethods.json create mode 100644 src/NerdBank.GitVersioning/NativeMethods.txt diff --git a/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs b/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs index 623539a1..eb250db9 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs @@ -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 { @@ -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) { @@ -62,12 +61,15 @@ internal static unsafe bool TryOpen(ReadOnlySpan 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; } diff --git a/src/NerdBank.GitVersioning/NativeMethods.json b/src/NerdBank.GitVersioning/NativeMethods.json new file mode 100644 index 00000000..69d2bcc5 --- /dev/null +++ b/src/NerdBank.GitVersioning/NativeMethods.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://aka.ms/CsWin32.schema.json" +} \ No newline at end of file diff --git a/src/NerdBank.GitVersioning/NativeMethods.txt b/src/NerdBank.GitVersioning/NativeMethods.txt new file mode 100644 index 00000000..885be08e --- /dev/null +++ b/src/NerdBank.GitVersioning/NativeMethods.txt @@ -0,0 +1,2 @@ +CreateFile +INVALID_HANDLE_VALUE diff --git a/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj b/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj index afeb8233..b196afbc 100644 --- a/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj +++ b/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj @@ -12,12 +12,12 @@ + - diff --git a/src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec b/src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec index 30e035fc..48fc3455 100644 --- a/src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec +++ b/src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.nuspec @@ -23,7 +23,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj - @@ -49,7 +48,6 @@ IMPORTANT: The 3.x release may produce a different version height than prior maj - From 546a678f93bd67af3589ae0e6a1d2c2ccadc488e Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sun, 13 Jun 2021 02:56:04 -0600 Subject: [PATCH 2/3] Update CsWin32 to fix `dotnet build` --- azure-pipelines.yml | 18 +++++++++--------- .../ManagedGit/FileHelpers.cs | 8 +++++--- .../NerdBank.GitVersioning.csproj | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 10d8b58c..14753b51 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -58,13 +58,13 @@ stages: displayName: Configure git commit author for testing - task: UseDotNet@2 - displayName: Install .NET Core SDK 5.0.202 + displayName: Install .NET Core 5.0.202 SDK inputs: packageType: sdk version: 5.0.202 - task: UseDotNet@2 - displayName: Install .NET Core 3.1 + displayName: Install .NET Core 3.1 runtime inputs: packageType: runtime version: 3.1.x @@ -296,17 +296,17 @@ stages: vmImage: $(imageName) steps: - task: UseDotNet@2 - displayName: Install .NET Core SDK 2.1.811 + displayName: Install .NET Core 2.1 runtime inputs: - packageType: sdk - version: 2.1.811 + packageType: runtime + version: 2.1.x - task: UseDotNet@2 - displayName: Install .NET Core SDK 3.1.100 + displayName: Install .NET Core 3.1 runtime inputs: - packageType: sdk - version: 3.1.100 + packageType: runtime + version: 3.1.x - task: UseDotNet@2 - displayName: Install .NET Core SDK 5.0.202 + displayName: Install .NET Core 5.0.202 SDK inputs: packageType: sdk version: 5.0.202 diff --git a/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs b/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs index eb250db9..e1d9713b 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/FileHelpers.cs @@ -5,7 +5,9 @@ using System.IO; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; -using Microsoft.Windows.Sdk; +using Windows.Win32; +using Windows.Win32.Storage.FileSystem; +using Windows.Win32.System.SystemServices; namespace Nerdbank.GitVersioning.ManagedGit { @@ -23,7 +25,7 @@ internal static bool TryOpen(string path, out FileStream? stream) { if (IsWindows) { - 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); + var handle = PInvoke.CreateFile(path, FILE_ACCESS_FLAGS.FILE_GENERIC_READ, FILE_SHARE_MODE.FILE_SHARE_READ, lpSecurityAttributes: null, FILE_CREATION_DISPOSITION.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, null); if (!handle.IsInvalid) { @@ -64,7 +66,7 @@ internal static unsafe bool TryOpen(ReadOnlySpan path, [NotNullWhen(true)] 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); + handle = PInvoke.CreateFile(pPath, FILE_ACCESS_FLAGS.FILE_GENERIC_READ, FILE_SHARE_MODE.FILE_SHARE_READ, null, FILE_CREATION_DISPOSITION.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, default); } if (!handle.Equals(Constants.INVALID_HANDLE_VALUE)) diff --git a/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj b/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj index b196afbc..d6dfbf49 100644 --- a/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj +++ b/src/NerdBank.GitVersioning/NerdBank.GitVersioning.csproj @@ -12,7 +12,7 @@ - + From 8cfe442dbc59172078156bbde21fdd00bfdbcda8 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sun, 13 Jun 2021 06:55:01 -0600 Subject: [PATCH 3/3] Remove dotnet/pinvoke from 3rd party licenses file --- 3rdPartyNotices.txt | 25 ------------------------- src/Nerdbank.GitVersioning.sln | 5 +++-- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/3rdPartyNotices.txt b/3rdPartyNotices.txt index f96a675c..3a888faa 100644 --- a/3rdPartyNotices.txt +++ b/3rdPartyNotices.txt @@ -127,31 +127,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -dotnet/PInvoke (https://github.com/dotnet/pinvoke) -============== - -MIT License - -Copyright (c) .NET Foundation and Contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - Cake Contrib (https://github.com/cake-contrib/graphics) ============ diff --git a/src/Nerdbank.GitVersioning.sln b/src/Nerdbank.GitVersioning.sln index 66c96ce6..43ffb2c7 100644 --- a/src/Nerdbank.GitVersioning.sln +++ b/src/Nerdbank.GitVersioning.sln @@ -1,12 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28404.58 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31411.2 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4BD1A7CD-6F52-4F5A-825B-50E4D8C3ECFF}" ProjectSection(SolutionItems) = preProject ..\.editorconfig = ..\.editorconfig ..\.gitignore = ..\.gitignore + ..\3rdPartyNotices.txt = ..\3rdPartyNotices.txt ..\azure-pipelines.yml = ..\azure-pipelines.yml ..\build.ps1 = ..\build.ps1 Directory.Build.props = Directory.Build.props