Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax LinkTarget so it always returns null when steps on an error #55668

Merged
merged 1 commit into from
Jul 15, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -427,34 +427,33 @@ internal static void CreateSymbolicLink(string path, string pathToTarget, bool i
{
string? targetPath = returnFinalTarget ?
GetFinalLinkTarget(linkPath, isDirectory) :
GetImmediateLinkTarget(linkPath, isDirectory, throwOnUnreachable: true, returnFullPath: true);
GetImmediateLinkTarget(linkPath, isDirectory, throwOnError: true, returnFullPath: true);

return targetPath == null ? null :
isDirectory ? new DirectoryInfo(targetPath) : new FileInfo(targetPath);
}

internal static string? GetLinkTarget(string linkPath, bool isDirectory)
=> GetImmediateLinkTarget(linkPath, isDirectory, throwOnUnreachable: false, returnFullPath: false);
=> GetImmediateLinkTarget(linkPath, isDirectory, throwOnError: false, returnFullPath: false);

/// <summary>
/// Gets reparse point information associated to <paramref name="linkPath"/>.
/// </summary>
/// <returns>The immediate link target, absolute or relative or null if the file is not a supported link.</returns>
internal static unsafe string? GetImmediateLinkTarget(string linkPath, bool isDirectory, bool throwOnUnreachable, bool returnFullPath)
internal static unsafe string? GetImmediateLinkTarget(string linkPath, bool isDirectory, bool throwOnError, bool returnFullPath)
{
using SafeFileHandle handle = OpenSafeFileHandle(linkPath,
Interop.Kernel32.FileOperations.FILE_FLAG_BACKUP_SEMANTICS |
Interop.Kernel32.FileOperations.FILE_FLAG_OPEN_REPARSE_POINT);

if (handle.IsInvalid)
{
int error = Marshal.GetLastWin32Error();

if (!throwOnUnreachable && IsPathUnreachableError(error))
if (!throwOnError)
{
return null;
}

int error = Marshal.GetLastWin32Error();
// File not found doesn't make much sense coming from a directory.
if (isDirectory && error == Interop.Errors.ERROR_FILE_NOT_FOUND)
{
Expand All @@ -479,6 +478,11 @@ internal static void CreateSymbolicLink(string path, string pathToTarget, bool i

if (!success)
{
if (!throwOnError)
{
return null;
}

int error = Marshal.GetLastWin32Error();
// The file or directory is not a reparse point.
if (error == Interop.Errors.ERROR_NOT_A_REPARSE_POINT)
Expand Down Expand Up @@ -600,13 +604,15 @@ uint GetFinalPathNameByHandle(SafeFileHandle handle, char[] buffer)
{
// Since all these paths will be passed to CreateFile, which takes a string anyway, it is pointless to use span.
// I am not sure if it's possible to change CreateFile's param to ROS<char> and avoid all these allocations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also follow-up to address this comment? If we can save allocations by marshaling a span instead of creating a string to then pass down, we should. You can't today pass a span directly to a DllImport method, but you can manually pin it and pass in the pointer, which is exactly what the runtime does with string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address this in a separate PR in order to get this in before the preview7 snap, thanks.

string? current = GetImmediateLinkTarget(linkPath, isDirectory, throwOnUnreachable: false, returnFullPath: true);

// We don't throw on error since we already did all the proper validations before.
string? current = GetImmediateLinkTarget(linkPath, isDirectory, throwOnError: false, returnFullPath: true);
string? prev = null;

while (current != null)
{
prev = current;
current = GetImmediateLinkTarget(current, isDirectory, throwOnUnreachable: false, returnFullPath: true);
current = GetImmediateLinkTarget(current, isDirectory, throwOnError: false, returnFullPath: true);
}

return prev;
Expand Down