-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: David Cantu <dacantu@microsoft.com> Co-authored-by: carlossanlop <carlossanlop@users.noreply.github.com> Co-authored-by: carlossanlop <Carlos Sanchez carlossanlop@users.noreply.github.com>
- Loading branch information
1 parent
f1bf5ee
commit dcce0f5
Showing
50 changed files
with
1,883 additions
and
174 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/libraries/Common/src/Interop/Unix/Interop.DefaultPathBufferSize.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
internal static partial class Interop | ||
{ | ||
// Unix max paths are typically 1K or 4K UTF-8 bytes, 256 should handle the majority of paths | ||
// without putting too much pressure on the stack. | ||
internal const int DefaultPathBufferSize = 256; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/libraries/Common/src/Interop/Unix/System.Native/Interop.SymLink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_SymLink", SetLastError = true)] | ||
internal static extern int SymLink(string target, string linkPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateSymbolicLink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Kernel32 | ||
{ | ||
/// <summary> | ||
/// The link target is a directory. | ||
/// </summary> | ||
internal const int SYMBOLIC_LINK_FLAG_DIRECTORY = 0x1; | ||
|
||
/// <summary> | ||
/// Allows creation of symbolic links from a process that is not elevated. Requires Windows 10 Insiders build 14972 or later. | ||
/// Developer Mode must first be enabled on the machine before this option will function. | ||
/// </summary> | ||
internal const int SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 0x2; | ||
|
||
[DllImport(Libraries.Kernel32, EntryPoint = "CreateSymbolicLinkW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)] | ||
private static extern bool CreateSymbolicLinkPrivate(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); | ||
|
||
/// <summary> | ||
/// Creates a symbolic link. | ||
/// </summary> | ||
/// <param name="symlinkFileName">The symbolic link to be created.</param> | ||
/// <param name="targetFileName">The name of the target for the symbolic link to be created. | ||
/// If it has a device name associated with it, the link is treated as an absolute link; otherwise, the link is treated as a relative link.</param> | ||
/// <param name="isDirectory"><see langword="true" /> if the link target is a directory; <see langword="false" /> otherwise.</param> | ||
internal static void CreateSymbolicLink(string symlinkFileName, string targetFileName, bool isDirectory) | ||
{ | ||
string originalPath = symlinkFileName; | ||
symlinkFileName = PathInternal.EnsureExtendedPrefixIfNeeded(symlinkFileName); | ||
targetFileName = PathInternal.EnsureExtendedPrefixIfNeeded(targetFileName); | ||
|
||
int flags = 0; | ||
|
||
bool isAtLeastWin10Build14972 = | ||
Environment.OSVersion.Version.Major == 10 && Environment.OSVersion.Version.Build >= 14972 || | ||
Environment.OSVersion.Version.Major >= 11; | ||
|
||
if (isAtLeastWin10Build14972) | ||
{ | ||
flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; | ||
} | ||
|
||
if (isDirectory) | ||
{ | ||
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; | ||
} | ||
|
||
bool success = CreateSymbolicLinkPrivate(symlinkFileName, targetFileName, flags); | ||
|
||
int error; | ||
if (!success) | ||
{ | ||
throw Win32Marshal.GetExceptionForLastWin32Error(originalPath); | ||
} | ||
// In older versions we need to check GetLastWin32Error regardless of the return value of CreateSymbolicLink, | ||
// e.g: if the user doesn't have enough privileges to create a symlink the method returns success which we can consider as a silent failure. | ||
else if (!isAtLeastWin10Build14972 && (error = Marshal.GetLastWin32Error()) != 0) | ||
{ | ||
throw Win32Marshal.GetExceptionForWin32Error(error, originalPath); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.DeviceIoControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Kernel32 | ||
{ | ||
// https://docs.microsoft.com/windows/win32/api/winioctl/ni-winioctl-fsctl_get_reparse_point | ||
internal const int FSCTL_GET_REPARSE_POINT = 0x000900a8; | ||
|
||
[DllImport(Libraries.Kernel32, EntryPoint = "DeviceIoControl", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)] | ||
internal static extern bool DeviceIoControl( | ||
SafeHandle hDevice, | ||
uint dwIoControlCode, | ||
IntPtr lpInBuffer, | ||
uint nInBufferSize, | ||
byte[] lpOutBuffer, | ||
uint nOutBufferSize, | ||
out uint lpBytesReturned, | ||
IntPtr lpOverlapped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetFinalPathNameByHandle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Kernel32 | ||
{ | ||
internal const uint FILE_NAME_NORMALIZED = 0x0; | ||
|
||
// https://docs.microsoft.com/windows/desktop/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew (kernel32) | ||
[DllImport(Libraries.Kernel32, EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)] | ||
internal static unsafe extern uint GetFinalPathNameByHandle( | ||
SafeFileHandle hFile, | ||
char* lpszFilePath, | ||
uint cchFilePath, | ||
uint dwFlags); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.REPARSE_DATA_BUFFER.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Kernel32 | ||
{ | ||
// https://docs.microsoft.com/windows-hardware/drivers/ifs/fsctl-get-reparse-point | ||
internal const int MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024; | ||
|
||
internal const uint SYMLINK_FLAG_RELATIVE = 1; | ||
|
||
// https://msdn.microsoft.com/library/windows/hardware/ff552012.aspx | ||
// We don't need all the struct fields; omitting the rest. | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal unsafe struct REPARSE_DATA_BUFFER | ||
{ | ||
internal uint ReparseTag; | ||
internal ushort ReparseDataLength; | ||
internal ushort Reserved; | ||
internal SymbolicLinkReparseBuffer ReparseBufferSymbolicLink; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct SymbolicLinkReparseBuffer | ||
{ | ||
internal ushort SubstituteNameOffset; | ||
internal ushort SubstituteNameLength; | ||
internal ushort PrintNameOffset; | ||
internal ushort PrintNameLength; | ||
internal uint Flags; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.