Skip to content

Commit

Permalink
Move more code to PathHelpers
Browse files Browse the repository at this point in the history
Also fix bug where IsPathTooLong would say "no" for paths exactly
260 characters long (260 *is* too long).
  • Loading branch information
rpaquay committed Dec 26, 2017
1 parent 95f61b1 commit 38d38b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/Core/Files/FullPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Generic;
using System.IO;

namespace VsChromium.Core.Files {
/// <summary>
Expand Down Expand Up @@ -45,7 +44,7 @@ public FullPath Parent {
/// <summary>
/// Return the file name part of this full path.
/// </summary>
public string FileName { get { return Path.GetFileName(_path); } }
public string FileName { get { return PathHelpers.GetFileName(_path); } }

/// <summary>
/// Returns a full path instance as the combination of this full path with
Expand Down Expand Up @@ -113,7 +112,7 @@ public bool StartsWith(FullPath x) {
/// one of its component, i.e. parts between directory separator.
/// </summary>
public bool HasComponent(string component) {
foreach (string currentComponent in _path.Split(Path.DirectorySeparatorChar)) {
foreach (string currentComponent in PathHelpers.SplitPath(_path)) {
if (currentComponent.Equals(component, StringComparison.CurrentCultureIgnoreCase))
return true;
}
Expand All @@ -124,7 +123,7 @@ public bool HasComponent(string component) {
/// Returns the enumeration of the parent full path of this full path.
/// </summary>
public IEnumerable<FullPath> EnumerateParents() {
for (var parent = this.Parent; parent != default(FullPath); parent = parent.Parent) {
for (var parent = Parent; parent != default(FullPath); parent = parent.Parent) {
yield return parent;
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/Core/Files/PathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class PathHelpers {
private static readonly string DirectorySeparatorString = new string(Path.DirectorySeparatorChar, 1);
private static readonly char[] DirectorySeparatorArray = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
private static readonly string NetworkSharePrefix = new string(Path.DirectorySeparatorChar, 2);
public static char DirectorySeparatorChar = Path.DirectorySeparatorChar;

/// <summary>
/// Combines two paths into a single path. More efficient than <see
Expand Down Expand Up @@ -174,7 +175,7 @@ public static bool IsPathTooLong(string parentPath, string relativePath) {
if (string.IsNullOrEmpty(relativePath))
return IsPathTooLong(parentPath);

return parentPath.Length + 1 + relativePath.Length > MaxPath;
return (parentPath.Length + 1 + relativePath.Length) >= MaxPath;
}

/// <summary>
Expand All @@ -194,6 +195,20 @@ public static bool IsValidBclPath(string path) {
return false;
}
}

public static string GetFileName(string path) {
if (string.IsNullOrEmpty(path)) {
return path;
}
int length = path.Length;
int index = length;
while (--index >= 0) {
char ch = path[index];
if (ch == Path.DirectorySeparatorChar || ch == Path.AltDirectorySeparatorChar || ch == Path.VolumeSeparatorChar)
return path.Substring(index + 1, length - index - 1);
}
return path;
}
}

/// <summary>
Expand Down
13 changes: 6 additions & 7 deletions src/Core/Files/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

using System;
using System.IO;

namespace VsChromium.Core.Files {
/// <summary>
Expand Down Expand Up @@ -62,7 +61,7 @@ private static string ExtractFileName(string relativePath) {
throw new ArgumentNullException("relativePath");
if (PathHelpers.IsFileName(relativePath))
return relativePath;
return Path.GetFileName(relativePath);
return PathHelpers.GetFileName(relativePath);
}

/// <summary>
Expand Down Expand Up @@ -105,28 +104,28 @@ public RelativePath Parent {
}

public override string ToString() {
return this.Value;
return Value;
}

/// <summary>
/// Returns a new relative path instance by appending <paramref name="name"/> to this instance.
/// </summary>
public RelativePath CreateChild(string name) {
return new RelativePath(PathHelpers.CombinePaths(this.Value, name), name);
return new RelativePath(PathHelpers.CombinePaths(Value, name), name);
}

#region Comparison/Equality plumbing

public int CompareTo(RelativePath other) {
return SystemPathComparer.Instance.StringComparer.Compare(this.Value, other.Value);
return SystemPathComparer.Instance.StringComparer.Compare(Value, other.Value);
}

public bool Equals(RelativePath other) {
return SystemPathComparer.Instance.StringComparer.Equals(this.Value, other.Value);
return SystemPathComparer.Instance.StringComparer.Equals(Value, other.Value);
}

public override int GetHashCode() {
return SystemPathComparer.Instance.StringComparer.GetHashCode(this.Value);
return SystemPathComparer.Instance.StringComparer.GetHashCode(Value);
}

public override bool Equals(object other) {
Expand Down
1 change: 0 additions & 1 deletion src/Server/FileSystem/FileSystemNameFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using VsChromium.Core.Files;
using VsChromium.Core.Ipc.TypedMessages;
Expand Down

0 comments on commit 38d38b2

Please sign in to comment.