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

Refactor repeated invalid character checks in Path.GetFullPath Unix and Windows #56568

Merged
merged 5 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
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
45 changes: 27 additions & 18 deletions src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,7 @@ public static string GetFullPath(string path)
if (path.Contains('\0'))
throw new ArgumentException(SR.Argument_InvalidPathChars, nameof(path));

// Expand with current directory if necessary
if (!IsPathRooted(path))
{
path = Combine(Interop.Sys.GetCwd(), path);
}

// We would ideally use realpath to do this, but it resolves symlinks, requires that the file actually exist,
// and turns it into a full path, which we only want if fullCheck is true.
string collapsedString = PathInternal.RemoveRelativeSegments(path, PathInternal.GetRootLength(path));

Debug.Assert(collapsedString.Length < path.Length || collapsedString.ToString() == path,
"Either we've removed characters, or the string should be unmodified from the input path.");

string result = collapsedString.Length == 0 ? PathInternal.DirectorySeparatorCharAsString : collapsedString;

return result;
return GetFullPathInternal(path);
}

public static string GetFullPath(string path, string basePath)
Expand All @@ -58,9 +43,33 @@ public static string GetFullPath(string path, string basePath)
throw new ArgumentException(SR.Argument_InvalidPathChars);

if (IsPathFullyQualified(path))
return GetFullPath(path);
return GetFullPathInternal(path);

return GetFullPathInternal(CombineInternal(basePath, path));
}

return GetFullPath(CombineInternal(basePath, path));
// Gets the full path without argument validation
private static string GetFullPathInternal(string path)
{
jkotas marked this conversation as resolved.
Show resolved Hide resolved
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(!path.Contains('\0'));

// Expand with current directory if necessary
if (!IsPathRooted(path))
{
path = Combine(Interop.Sys.GetCwd(), path);
}

// We would ideally use realpath to do this, but it resolves symlinks, requires that the file actually exist,
// and turns it into a full path, which we only want if fullCheck is true.
string collapsedString = PathInternal.RemoveRelativeSegments(path, PathInternal.GetRootLength(path));

Debug.Assert(collapsedString.Length < path.Length || collapsedString.ToString() == path,
"Either we've removed characters, or the string should be unmodified from the input path.");

string result = collapsedString.Length == 0 ? PathInternal.DirectorySeparatorCharAsString : collapsedString;

return result;
}

private static string RemoveLongPathPrefix(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static string GetFullPath(string path)
if (path.Contains('\0'))
throw new ArgumentException(SR.Argument_InvalidPathChars, nameof(path));

return GetFullyQualifiedPath(path);
return GetFullPathInternal(path);
}

public static string GetFullPath(string path, string basePath)
Expand All @@ -68,7 +68,7 @@ public static string GetFullPath(string path, string basePath)
throw new ArgumentException(SR.Argument_InvalidPathChars);

if (IsPathFullyQualified(path))
return GetFullyQualifiedPath(path);
return GetFullPathInternal(path);

if (PathInternal.IsEffectivelyEmpty(path.AsSpan()))
return basePath;
Expand Down Expand Up @@ -120,11 +120,15 @@ public static string GetFullPath(string path, string basePath)

return PathInternal.IsDevice(combinedPath.AsSpan())
? PathInternal.RemoveRelativeSegments(combinedPath, PathInternal.GetRootLength(combinedPath.AsSpan()))
: GetFullyQualifiedPath(combinedPath);
: GetFullPathInternal(combinedPath);
}

internal static string GetFullyQualifiedPath(string path)
// Gets the full path without argument validation
private static string GetFullPathInternal(string path)
{
jkotas marked this conversation as resolved.
Show resolved Hide resolved
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(!path.Contains('\0'));

if (PathInternal.IsExtended(path.AsSpan()))
{
// \\?\ paths are considered normalized by definition. Windows doesn't normalize \\?\
Expand Down