From f7957b4cc8310d90d75ec69cba5337f6df1a88eb Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 14 Mar 2020 23:27:49 -0700 Subject: [PATCH] Remove some old code --- src/LibHac/FsSystem/PathTools.cs | 194 --------------------------- tests/LibHac.Tests/PathToolsTests.cs | 104 -------------- 2 files changed, 298 deletions(-) diff --git a/src/LibHac/FsSystem/PathTools.cs b/src/LibHac/FsSystem/PathTools.cs index 55aac6da..3e4ad0af 100644 --- a/src/LibHac/FsSystem/PathTools.cs +++ b/src/LibHac/FsSystem/PathTools.cs @@ -497,200 +497,6 @@ namespace LibHac.FsSystem private static bool IsValidMountNameChar(byte c) => IsValidMountNameChar((char)c); - private static Result GetPathRoot(out ReadOnlySpan afterRootPath, Span pathRootBuffer, out int outRootLength, ReadOnlySpan path) - { - outRootLength = 0; - afterRootPath = path; - - if (path.Length == 0) return Result.Success; - - int mountNameStart; - if (IsDirectorySeparator(path[0])) - { - mountNameStart = 1; - } - else - { - mountNameStart = 0; - } - - int rootLength = 0; - - for (int i = mountNameStart; i < mountNameStart + MountNameLengthMax; i++) - { - if (i >= path.Length || path[i] == 0) break; - - // Set the length to 0 if there's no mount name - if (IsDirectorySeparator(path[i])) - { - outRootLength = 0; - return Result.Success; - } - - if (path[i] == MountSeparator) - { - rootLength = i + 1; - break; - } - } - - if (mountNameStart >= rootLength - 1 || path[rootLength - 1] != MountSeparator) - { - return ResultFs.InvalidPathFormat.Log(); - } - - if (mountNameStart < rootLength) - { - for (int i = mountNameStart; i < rootLength; i++) - { - if (path[i] == '.') - { - return ResultFs.InvalidCharacter.Log(); - } - } - } - - if (!pathRootBuffer.IsEmpty) - { - if (rootLength > pathRootBuffer.Length) - { - return ResultFs.TooLongPath.Log(); - } - - path.Slice(0, rootLength).CopyTo(pathRootBuffer); - } - - afterRootPath = path.Slice(rootLength); - outRootLength = rootLength; - return Result.Success; - } - - public static Result Normalize(Span normalizedPath, out int normalizedLength, ReadOnlySpan path, bool hasMountName) - { - normalizedLength = 0; - - int rootLength = 0; - ReadOnlySpan mainPath = path; - - if (hasMountName) - { - Result pathRootRc = GetPathRoot(out mainPath, normalizedPath, out rootLength, path); - if (pathRootRc.IsFailure()) return pathRootRc; - } - - var sb = new PathBuilder(normalizedPath.Slice(rootLength)); - - var state = NormalizeState.Initial; - - for (int i = 0; i < mainPath.Length; i++) - { - Result rc = Result.Success; - byte c = mainPath[i]; - - // Read input strings as null-terminated - if (c == 0) break; - - switch (state) - { - case NormalizeState.Initial when IsDirectorySeparator(c): - state = NormalizeState.Delimiter; - break; - - case NormalizeState.Initial: - return ResultFs.InvalidPathFormat.Log(); - - case NormalizeState.Normal when IsDirectorySeparator(c): - state = NormalizeState.Delimiter; - break; - - case NormalizeState.Normal: - rc = sb.Append(c); - break; - - case NormalizeState.Delimiter when IsDirectorySeparator(c): - break; - - case NormalizeState.Delimiter when c == '.': - state = NormalizeState.Dot; - rc = sb.AppendWithPrecedingSeparator(c); - break; - - case NormalizeState.Delimiter: - state = NormalizeState.Normal; - rc = sb.AppendWithPrecedingSeparator(c); - break; - - case NormalizeState.Dot when IsDirectorySeparator(c): - state = NormalizeState.Delimiter; - rc = sb.GoUpLevels(1); - break; - - case NormalizeState.Dot when c == '.': - state = NormalizeState.DoubleDot; - rc = sb.Append(c); - break; - - case NormalizeState.Dot: - state = NormalizeState.Normal; - rc = sb.Append(c); - break; - - case NormalizeState.DoubleDot when IsDirectorySeparator(c): - state = NormalizeState.Delimiter; - rc = sb.GoUpLevels(2); - break; - - case NormalizeState.DoubleDot: - state = NormalizeState.Normal; - break; - } - - if (rc.IsFailure()) - { - if (ResultFs.TooLongPath.Includes(rc)) - { - // Make sure pending delimiters are added to the string if possible - if (state == NormalizeState.Delimiter) - { - sb.Append((byte)DirectorySeparator); - } - } - - normalizedLength = sb.Length; - sb.Terminate(); - return rc; - } - } - - Result finalRc = Result.Success; - - switch (state) - { - case NormalizeState.Dot: - state = NormalizeState.Delimiter; - finalRc = sb.GoUpLevels(1); - break; - - case NormalizeState.DoubleDot: - state = NormalizeState.Delimiter; - finalRc = sb.GoUpLevels(2); - break; - } - - // Add the pending delimiter if the path is empty - // or if the path has only a mount name with no trailing delimiter - if (state == NormalizeState.Delimiter && sb.Length == 0 || - rootLength > 0 && sb.Length == 0) - { - finalRc = sb.Append((byte)'/'); - } - - normalizedLength = sb.Length; - sb.Terminate(); - - return finalRc; - } - private enum NormalizeState { Initial, diff --git a/tests/LibHac.Tests/PathToolsTests.cs b/tests/LibHac.Tests/PathToolsTests.cs index 403a79ea..3929e9cf 100644 --- a/tests/LibHac.Tests/PathToolsTests.cs +++ b/tests/LibHac.Tests/PathToolsTests.cs @@ -172,110 +172,6 @@ namespace LibHac.Tests return paths.Select(x => new object[] { x }).ToArray(); } - public static object[][] NormalizedPathTestItemsU8NoMountName = - { - new object[] {"/", "/", Result.Success}, - new object[] {"/.", "/", Result.Success}, - new object[] {"/..", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"/abc", "/abc", Result.Success}, - new object[] {"/a/..", "/", Result.Success}, - new object[] {"/a/b/c", "/a/b/c", Result.Success}, - new object[] {"/a/b/../c", "/a/c", Result.Success}, - new object[] {"/a/b/c/..", "/a/b", Result.Success}, - new object[] {"/a/b/c/.", "/a/b/c", Result.Success}, - new object[] {"/a/../../..", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"/a/../../../a/b/c", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"//a/b//.//c", "/a/b/c", Result.Success}, - new object[] {"/../a/b/c/.", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"/./aaa/bbb/ccc/.", "/aaa/bbb/ccc", Result.Success}, - - new object[] {"/a/b/c/", "/a/b/c", Result.Success}, - new object[] {"/aa/./bb/../cc/", "/aa/cc", Result.Success}, - new object[] {"/./b/../c/", "/c", Result.Success}, - new object[] {"/a/../../../", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"//a/b//.//c/", "/a/b/c", Result.Success}, - new object[] {"/tmp/../", "/", Result.Success}, - new object[] {"abc", "", ResultFs.InvalidPathFormat.Value } - }; - - public static object[][] NormalizedPathTestItemsU8MountName = - { - new object[] {"mount:/a/b/../c", "mount:/a/c", Result.Success}, - new object[] {"a:/a/b/c", "a:/a/b/c", Result.Success}, - new object[] {"mount:/a/b/../c", "mount:/a/c", Result.Success}, - new object[] {"mount:", "mount:/", Result.Success}, - new object[] {"abc:/a/../../../a/b/c", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"abc:/./b/../c/", "abc:/c", Result.Success}, - new object[] {"abc:/.", "abc:/", Result.Success}, - new object[] {"abc:/..", "", ResultFs.DirectoryUnobtainable.Value}, - new object[] {"abc:/", "abc:/", Result.Success}, - new object[] {"abc://a/b//.//c", "abc:/a/b/c", Result.Success}, - new object[] {"abc:/././/././a/b//.//c", "abc:/a/b/c", Result.Success}, - new object[] {"mount:/d./aa", "mount:/d./aa", Result.Success}, - new object[] {"mount:/d/..", "mount:/", Result.Success} - }; - - [Theory] - [MemberData(nameof(NormalizedPathTestItemsU8NoMountName))] - public static void NormalizePathU8NoMountName(string path, string expected, Result expectedResult) - { - var u8Path = path.ToU8String(); - Span buffer = stackalloc byte[0x301]; - - Result rc = PathTools.Normalize(buffer, out _, u8Path, false); - - string actual = StringUtils.Utf8ZToString(buffer); - - Assert.Equal(expectedResult, rc); - if (expectedResult == Result.Success) - { - Assert.Equal(expected, actual); - } - } - - [Theory] - [MemberData(nameof(NormalizedPathTestItemsU8MountName))] - public static void NormalizePathU8MountName(string path, string expected, Result expectedResult) - { - var u8Path = path.ToU8String(); - Span buffer = stackalloc byte[0x301]; - - Result rc = PathTools.Normalize(buffer, out _, u8Path, true); - - string actual = StringUtils.Utf8ZToString(buffer); - - Assert.Equal(expectedResult, rc); - if (expectedResult == Result.Success) - { - Assert.Equal(expected, actual); - } - } - - public static object[][] NormalizedPathTestItemsU8TooShort = - { - new object[] {"/a/b/c", "", 0}, - new object[] {"/a/b/c", "/a/", 4}, - new object[] {"/a/b/c", "/a/b", 5}, - new object[] {"/a/b/c", "/a/b/", 6} - }; - - [Theory] - [MemberData(nameof(NormalizedPathTestItemsU8TooShort))] - public static void NormalizePathU8TooShortDest(string path, string expected, int destSize) - { - var u8Path = path.ToU8String(); - - Span buffer = stackalloc byte[destSize]; - - Result rc = PathTools.Normalize(buffer, out int normalizedLength, u8Path, false); - - string actual = StringUtils.Utf8ZToString(buffer); - - Assert.Equal(ResultFs.TooLongPath.Value, rc); - Assert.Equal(Math.Max(0, destSize - 1), normalizedLength); - Assert.Equal(expected, actual); - } - public static object[][] GetFileNameTestItems = { new object[] {"/a/bb/ccc", "ccc"},