Remove some old code

This commit is contained in:
Alex Barney 2020-03-14 23:27:49 -07:00
parent 0c6447daac
commit f7957b4cc8
2 changed files with 0 additions and 298 deletions

View file

@ -497,200 +497,6 @@ namespace LibHac.FsSystem
private static bool IsValidMountNameChar(byte c) => IsValidMountNameChar((char)c);
private static Result GetPathRoot(out ReadOnlySpan<byte> afterRootPath, Span<byte> pathRootBuffer, out int outRootLength, ReadOnlySpan<byte> 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<byte> normalizedPath, out int normalizedLength, ReadOnlySpan<byte> path, bool hasMountName)
{
normalizedLength = 0;
int rootLength = 0;
ReadOnlySpan<byte> 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,

View file

@ -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<byte> 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<byte> 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<byte> 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"},