Skip to content

Commit

Permalink
Core - PathCheck rewrite to match Chromium FilePath::IsAbsolute
Browse files Browse the repository at this point in the history
Resovles #3102
  • Loading branch information
amaitland committed May 3, 2020
1 parent e1401e7 commit 98b7abe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
11 changes: 6 additions & 5 deletions CefSharp.Test/Framework/PathCheckFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ public void IsPathAbsoluteValid()
Assert.True(PathCheck.IsAbsolute(@"C:/foo1"));
Assert.True(PathCheck.IsAbsolute(@"c:\"));
Assert.True(PathCheck.IsAbsolute(@"C:/foo2"));
Assert.True(PathCheck.IsAbsolute(@"C:\Users\appveyor\AppData\Local\CefSharp\Tests\Cache"));

}

[Fact]
public void IsPathAbsoluteInValid()
{
Assert.False(PathCheck.IsAbsolute(@"\"));
Assert.False(PathCheck.IsAbsolute(@"/"));
Assert.False(PathCheck.IsAbsolute(@"C:foo.txt"));
Assert.False(PathCheck.IsAbsolute(@"C:"));
Assert.False(PathCheck.IsAbsolute(@"."));
Assert.False(PathCheck.IsAbsolute(@".."));
Expand All @@ -39,10 +40,10 @@ public void IsPathAbsoluteInValid()
[Fact]
public void AssertPathAbsoluteInValid()
{
Assert.Throws<Exception>(() => PathCheck.IsAbsolute(@"\"));
Assert.Throws<Exception>(() => PathCheck.IsAbsolute(@"c:foo"));
Assert.Throws<Exception>(() => PathCheck.IsAbsolute(@"cache"));
Assert.Throws<Exception>(() => PathCheck.IsAbsolute(@"locales\"));
Assert.Throws<Exception>(() => PathCheck.AssertAbsolute(@"\", "Path"));
Assert.Throws<Exception>(() => PathCheck.AssertAbsolute(@"c:foo", "Path"));
Assert.Throws<Exception>(() => PathCheck.AssertAbsolute(@"cache", "Path"));
Assert.Throws<Exception>(() => PathCheck.AssertAbsolute(@"locales\", "Path"));
}
}
}
54 changes: 37 additions & 17 deletions CefSharp/Internals/PathCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace CefSharp.Internals
{
Expand All @@ -13,6 +17,9 @@ namespace CefSharp.Internals
//TODO: Better name
public static class PathCheck
{
internal const char DirectorySeparatorChar = '\\';
internal const char AltDirectorySeparatorChar = '/';

/// <summary>
/// Allow user to disable the assert.
/// As a temporary measure we'll allow users to disable the assert
Expand All @@ -21,6 +28,25 @@ public static class PathCheck
[Obsolete("This will be removed after further testing.")]
public static bool EnableAssert = true;

internal static int FindDriveLetter(string path)
{
if (path.Length >= 2 && path[1] == ':' &&
((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z')))
{
return 1;
}
return -1;
}

/// <summary>
/// True if the given character is a directory separator.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsDirectorySeparator(char c)
{
return c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;
}

/// <summary>
/// Throw exception if the path provided is non-asbolute
/// CEF now explicitly requires absolute paths
Expand All @@ -36,36 +62,30 @@ public static void AssertAbsolute(string path, string settingName)
if (!string.IsNullOrEmpty(path) && EnableAssert)
{
//IsPathRooted will return true for paths that start with a single slash, e.g. \programfiles
if (IsAbsolute(path))
if (!IsAbsolute(path))
{
throw new Exception(settingName + " now requires an absolute path, the path provided is non-absolute. You can use System.IO.Path.GetFullPath to obtain an absolute path. Current value:" + path);
}
}
}

/// <summary>
/// Throw exception if the path provided is non-asbolute
/// CEF now explicitly requires absolute paths
/// https://bitbucket.org/chromiumembedded/cef/issues/2916/not-persisting-in-local-stoage-when-using
/// Empty paths are ignored
/// Valid path is absolute, based on Chromium implementation.
/// </summary>
/// <param name="path">path</param>
public static bool IsAbsolute(string path)
{
const string directorySeperator = "\\";

//We don't have access to Path.IsPathFullyQualified as it's not in .Net 4.5.2 so we have to implement our own check
//If this isn't sufficent then we can import the code for .Net Core
//https://github.com/dotnet/runtime/blob/4f9ae42d861fcb4be2fcd5d3d55d5f227d30e723/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs#L290

//IsPathRooted will return true for paths that start with a single slash, e.g. \programfiles
//Based on https://stackoverflow.com/a/35046453/4583726
if (!Path.IsPathRooted(path) || Path.GetPathRoot(path).Equals(directorySeperator, StringComparison.Ordinal))
//Based on Chromium FilePath::IsAbolsute
//https://source.chromium.org/chromium/chromium/src/+/master:base/files/file_path.cc;drc=1c097f5f790782b2ad0b897cd9e2921ce9713585;l=97?q=IsAbsolute&ss=chromium&originalUrl=https:%2F%2Fcs.chromium.org%2F
var pos = FindDriveLetter(path);
if (pos != -1)
{
return false;
// Look for a separator right after the drive specification.
return path.Length > (pos + 1) && IsDirectorySeparator(path[pos + 1]);
}

return true;
// Look for a pair of leading separators.
return path.Length > 1 && IsDirectorySeparator(path[0]) && IsDirectorySeparator(path[1]);
}
}
}

0 comments on commit 98b7abe

Please sign in to comment.