Skip to content

Commit

Permalink
Share InvalidPathChars between PathUtility and RelativePath. (dotnet#…
Browse files Browse the repository at this point in the history
…2613)

This is necessary to avoid genarting filenames that are treated as
invalid by RelativePath on Linux.
  • Loading branch information
surban authored and superyyrrzz committed May 3, 2018
1 parent 6c3392f commit e9d6a77
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Microsoft.DocAsCode.Common/Path/PathUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ namespace Microsoft.DocAsCode.Common
public static class PathUtility
{
private static readonly Regex UriWithProtocol = new Regex(@"^\w{2,}\:", RegexOptions.Compiled);
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
private static readonly char[] InvalidPathChars = Path.GetInvalidPathChars();

private static readonly char[] AdditionalInvalidChars = ":*".ToArray();
public static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars().Concat(AdditionalInvalidChars).ToArray();
public static readonly char[] InvalidPathChars = Path.GetInvalidPathChars().Concat(AdditionalInvalidChars).ToArray();
private static readonly string InvalidFileNameCharsRegexString = $"[{Regex.Escape(new string(InvalidFileNameChars))}]";

// refers to http://index/?query=urlencode&rightProject=System&file=%5BRepoRoot%5D%5CNDP%5Cfx%5Csrc%5Cnet%5CSystem%5CNet%5Cwebclient.cs&rightSymbol=fptyy6owkva8
Expand Down
7 changes: 3 additions & 4 deletions src/Microsoft.DocAsCode.Common/Path/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ public sealed class RelativePath : IEquatable<RelativePath>
private const string ParentDirectory = "../";
public const char WorkingFolderChar = '~';
public const string WorkingFolderString = "~";
public static readonly char[] InvalidChars = Path.GetInvalidPathChars().Concat(":*").ToArray();
public static readonly string NormalizedWorkingFolder = "~/";
public static readonly string AltWorkingFolder = "~\\";
public static readonly RelativePath Empty = new RelativePath(false, 0, new string[] { string.Empty });
public static readonly RelativePath WorkingFolder = new RelativePath(true, 0, new string[] { string.Empty });
public static readonly char[] InvalidPartChars = InvalidChars.Concat(@"\/?").ToArray();
public static readonly char[] InvalidPartChars = PathUtility.InvalidPathChars.Concat(@"\/?").ToArray();
private static readonly string[] EncodedInvalidPartChars = Array.ConvertAll(InvalidPartChars, ch => Uri.EscapeDataString(ch.ToString()));
private static readonly char[] UnsafeInvalidPartChars = { '/' };
private static readonly string[] EncodedUnsafeInvalidPartChars = Array.ConvertAll(UnsafeInvalidPartChars, ch => Uri.EscapeDataString(ch.ToString()));
Expand Down Expand Up @@ -58,7 +57,7 @@ public static bool IsRelativePath(string path)
path.Length > 0 &&
path[0] != '/' &&
path[0] != '\\' &&
path.IndexOfAny(InvalidChars) == -1;
path.IndexOfAny(PathUtility.InvalidPathChars) == -1;
}

public static RelativePath Parse(string path) => TryParseCore(path, true);
Expand Down Expand Up @@ -373,7 +372,7 @@ private static RelativePath TryParseCore(string path, bool throwOnError)
{
return Empty;
}
if (path.IndexOfAny(InvalidChars) != -1)
if (path.IndexOfAny(PathUtility.InvalidPathChars) != -1)
{
if (throwOnError)
{
Expand Down

0 comments on commit e9d6a77

Please sign in to comment.