Update DirectoryPathParser, PathUtility and WindowsPath for 13.1.0

This commit is contained in:
Alex Barney 2022-01-05 00:21:45 -07:00
parent 61b29e57a3
commit 1f8277202f
5 changed files with 41 additions and 25 deletions

View file

@ -3,8 +3,13 @@ using LibHac.Common;
using LibHac.Diag;
using static LibHac.Fs.StringTraits;
namespace LibHac.Fs.Common;
// ReSharper disable once CheckNamespace
namespace LibHac.Fs;
/// <summary>
/// Iterates through each directory in a path beginning with the root directory.
/// </summary>
/// <remarks>Based on FS 13.1.0 (nnSdk 13.4.0)</remarks>
[NonCopyableDisposable]
public ref struct DirectoryPathParser
{
@ -15,11 +20,26 @@ public ref struct DirectoryPathParser
// Todo: Make private so we can use the GetCurrentPath method once lifetime tracking is better
public Path CurrentPath;
public DirectoryPathParser()
{
_buffer = Span<byte>.Empty;
_replacedChar = 0;
_position = 0;
CurrentPath = new Path();
}
public void Dispose()
{
CurrentPath.Dispose();
}
/// <summary>
/// Initializes this <see cref="DirectoryPathParser"/> with a new <see cref="Path"/>. The <see cref="Path"/>
/// should not be a fixed path that was just initialized with <see cref="PathFunctions.SetUpFixedPath"/>
/// because we need it to have an allocated write buffer.
/// </summary>
/// <param name="path">The <see cref="Path"/> to iterate. Must have an allocated write buffer.</param>
/// <returns>The <see cref="Result"/> of the operation.</returns>
public Result Initialize(ref Path path)
{
Span<byte> pathBuffer = path.GetWriteBufferLength() != 0 ? path.GetWriteBuffer() : Span<byte>.Empty;

View file

@ -1,6 +1,7 @@
using System;
using LibHac.Common;
using LibHac.Diag;
using LibHac.Fs.Impl;
using LibHac.FsSrv.Sf;
using LibHac.Util;
using static LibHac.Fs.StringTraits;
@ -11,7 +12,7 @@ namespace LibHac.Fs;
/// <summary>
/// Contains various utility functions for working with paths.
/// </summary>
/// <remarks>Based on FS 12.1.0 (nnSdk 12.3.1)</remarks>
/// <remarks>Based on FS 13.1.0 (nnSdk 13.4.0)</remarks>
public static class PathUtility
{
public static void Replace(Span<byte> buffer, byte currentChar, byte newChar)
@ -46,16 +47,6 @@ public static class PathUtility
(path.Length < 3 || path[2] == NullTerminator || path[2] == DirectorySeparator);
}
public static bool IsSeparator(byte c)
{
return c == DirectorySeparator;
}
public static bool IsNul(byte c)
{
return c == NullTerminator;
}
public static Result ConvertToFspPath(out FspPath fspPath, ReadOnlySpan<byte> path)
{
UnsafeHelpers.SkipParamInit(out fspPath);
@ -65,7 +56,7 @@ public static class PathUtility
if (length >= PathTool.EntryNameLengthMax + 1)
return ResultFs.TooLongPath.Log();
Result rc = PathFormatter.SkipMountName(out ReadOnlySpan<byte> pathWithoutMountName, out _,
Result rc = PathFormatter.SkipMountName(out ReadOnlySpan<byte> pathWithoutMountName, out int skipLength,
new U8Span(path));
if (rc.IsFailure()) return rc;
@ -73,10 +64,16 @@ public static class PathUtility
{
Replace(SpanHelpers.AsByteSpan(ref fspPath).Slice(0, 0x300), AltDirectorySeparator, DirectorySeparator);
}
else if (fspPath.Str[0] == DirectorySeparator && fspPath.Str[1] == DirectorySeparator)
else
{
SpanHelpers.AsByteSpan(ref fspPath)[0] = AltDirectorySeparator;
SpanHelpers.AsByteSpan(ref fspPath)[1] = AltDirectorySeparator;
bool isHostOrNoMountName = skipLength == 0 || StringUtils.Compare(path, CommonMountNames.HostRootFileSystemMountName,
CommonMountNames.HostRootFileSystemMountName.Length) == 0;
if (isHostOrNoMountName && WindowsPath.IsUncPath(path.Slice(skipLength), true, false))
{
SpanHelpers.AsByteSpan(ref fspPath)[skipLength] = AltDirectorySeparator;
SpanHelpers.AsByteSpan(ref fspPath)[skipLength + 1] = AltDirectorySeparator;
}
}
return Result.Success;

View file

@ -10,7 +10,7 @@ namespace LibHac.Fs;
/// <summary>
/// Contains functions for working with Windows paths.
/// </summary>
/// <remarks>Based on FS 12.1.0 (nnSdk 12.3.1)</remarks>
/// <remarks>Based on FS 13.1.0 (nnSdk 13.4.0)</remarks>
public static class WindowsPath
{
private const int WindowsDriveLength = 2;

View file

@ -22,7 +22,7 @@ public static class MountUtility
if (WindowsPath.IsWindowsDrive(path) || WindowsPath.IsUncPath(path))
{
StringUtils.Copy(mountName.Name, CommonPaths.HostRootFileSystemMountName);
mountName.Name[PathTool.MountNameLengthMax] = StringTraits.NullTerminator;
mountName.Name[PathTool.MountNameLengthMax] = NullTerminator;
subPath = path;
return Result.Success;
@ -30,7 +30,7 @@ public static class MountUtility
for (int i = 0; i <= maxMountLen; i++)
{
if (path[i] == StringTraits.DriveSeparator)
if (path[i] == DriveSeparator)
{
mountLen = i;
break;
@ -53,7 +53,7 @@ public static class MountUtility
return ResultFs.InvalidPathFormat.Log();
path.Value.Slice(0, mountLen).CopyTo(mountName.Name);
mountName.Name[mountLen] = StringTraits.NullTerminator;
mountName.Name[mountLen] = NullTerminator;
subPath = subPathTemp;
return Result.Success;

View file

@ -1,7 +1,6 @@
using System;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Common;
using LibHac.Fs.Fsa;
using LibHac.Os;