Update SaveDataFileSystemServiceImpl to use Path

This commit is contained in:
Alex Barney 2021-07-29 11:36:17 -07:00
parent 2f58e2fd5a
commit 6db134cae4

View file

@ -1,5 +1,4 @@
using System.IO;
using System.Runtime.CompilerServices;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Fsa;
@ -32,29 +31,36 @@ namespace LibHac.FsSystem.Save
FileTable = new HierarchicalSaveFileTable(dirTableStorage, fileTableStorage);
}
protected override Result DoCreateDirectory(in Path path)
private Result CheckIfNormalized(in Path path)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = PathNormalizer12.IsNormalized(out bool isNormalized, out _, path.GetString());
if (rc.IsFailure()) return rc;
FileTable.AddDirectory(normalizedPath);
if (!isNormalized)
return ResultFs.NotNormalized.Log();
return Result.Success;
}
protected override Result DoCreateDirectory(in Path path)
{
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
FileTable.AddDirectory(new U8Span(path.GetString()));
return Result.Success;
}
protected override Result DoCreateFile(in Path path, long size, CreateFileOptions option)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
if (size == 0)
{
var emptyFileEntry = new SaveFileInfo { StartBlock = int.MinValue, Length = size };
FileTable.AddFile(normalizedPath, ref emptyFileEntry);
FileTable.AddFile(new U8Span(path.GetString()), ref emptyFileEntry);
return Result.Success;
}
@ -69,34 +75,30 @@ namespace LibHac.FsSystem.Save
var fileEntry = new SaveFileInfo { StartBlock = startBlock, Length = size };
FileTable.AddFile(normalizedPath, ref fileEntry);
FileTable.AddFile(new U8Span(path.GetString()), ref fileEntry);
return Result.Success;
}
protected override Result DoDeleteDirectory(in Path path)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
FileTable.DeleteDirectory(normalizedPath);
FileTable.DeleteDirectory(new U8Span(path.GetString()));
return Result.Success;
}
protected override Result DoDeleteDirectoryRecursively(in Path path)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
rc = CleanDirectoryRecursively(normalizedPath);
rc = CleanDirectoryRecursively(in path);
if (rc.IsFailure()) return rc;
rc = DeleteDirectory(normalizedPath);
rc = DeleteDirectory(in path);
if (rc.IsFailure()) return rc;
return Result.Success;
@ -104,24 +106,20 @@ namespace LibHac.FsSystem.Save
protected override Result DoCleanDirectoryRecursively(in Path path)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
FileSystemExtensions.CleanDirectoryRecursivelyGeneric(this, normalizedPath.ToString());
FileSystemExtensions.CleanDirectoryRecursivelyGeneric(this, new U8Span(path.GetString()).ToString());
return Result.Success;
}
protected override Result DoDeleteFile(in Path path)
{
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
if (!FileTable.TryOpenFile(normalizedPath, out SaveFileInfo fileInfo))
if (!FileTable.TryOpenFile(new U8Span(path.GetString()), out SaveFileInfo fileInfo))
{
return ResultFs.PathNotFound.Log();
}
@ -131,7 +129,7 @@ namespace LibHac.FsSystem.Save
AllocationTable.Free(fileInfo.StartBlock);
}
FileTable.DeleteFile(normalizedPath);
FileTable.DeleteFile(new U8Span(path.GetString()));
return Result.Success;
}
@ -140,12 +138,10 @@ namespace LibHac.FsSystem.Save
{
UnsafeHelpers.SkipParamInit(out directory);
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
if (!FileTable.TryOpenDirectory(normalizedPath, out SaveFindPosition position))
if (!FileTable.TryOpenDirectory(new U8Span(path.GetString()), out SaveFindPosition position))
{
return ResultFs.PathNotFound.Log();
}
@ -159,49 +155,41 @@ namespace LibHac.FsSystem.Save
{
UnsafeHelpers.SkipParamInit(out file);
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
if (!FileTable.TryOpenFile(normalizedPath, out SaveFileInfo fileInfo))
if (!FileTable.TryOpenFile(new U8Span(path.GetString()), out SaveFileInfo fileInfo))
{
return ResultFs.PathNotFound.Log();
}
AllocationTableStorage storage = OpenFatStorage(fileInfo.StartBlock);
file = new SaveDataFile(storage, normalizedPath, FileTable, fileInfo.Length, mode);
file = new SaveDataFile(storage, new U8Span(path.GetString()), FileTable, fileInfo.Length, mode);
return Result.Success;
}
protected override Result DoRenameDirectory(in Path currentPath, in Path newPath)
{
Unsafe.SkipInit(out FsPath normalizedCurrentPath);
Unsafe.SkipInit(out FsPath normalizedNewPath);
Result rc = PathNormalizer.Normalize(normalizedCurrentPath.Str, out _, currentPath, false, false);
Result rc = CheckIfNormalized(in currentPath);
if (rc.IsFailure()) return rc;
rc = PathNormalizer.Normalize(normalizedNewPath.Str, out _, newPath, false, false);
rc = CheckIfNormalized(in newPath);
if (rc.IsFailure()) return rc;
return FileTable.RenameDirectory(normalizedCurrentPath, normalizedNewPath);
return FileTable.RenameDirectory(new U8Span(currentPath.GetString()), new U8Span(newPath.GetString()));
}
protected override Result DoRenameFile(in Path currentPath, in Path newPath)
{
Unsafe.SkipInit(out FsPath normalizedCurrentPath);
Unsafe.SkipInit(out FsPath normalizedNewPath);
Result rc = PathNormalizer.Normalize(normalizedCurrentPath.Str, out _, currentPath, false, false);
Result rc = CheckIfNormalized(in currentPath);
if (rc.IsFailure()) return rc;
rc = PathNormalizer.Normalize(normalizedNewPath.Str, out _, newPath, false, false);
rc = CheckIfNormalized(in newPath);
if (rc.IsFailure()) return rc;
FileTable.RenameFile(normalizedCurrentPath, normalizedNewPath);
FileTable.RenameFile(new U8Span(currentPath.GetString()), new U8Span(newPath.GetString()));
return Result.Success;
}
@ -210,18 +198,16 @@ namespace LibHac.FsSystem.Save
{
UnsafeHelpers.SkipParamInit(out entryType);
Unsafe.SkipInit(out FsPath normalizedPath);
Result rc = PathNormalizer.Normalize(normalizedPath.Str, out _, path, false, false);
Result rc = CheckIfNormalized(in path);
if (rc.IsFailure()) return rc;
if (FileTable.TryOpenFile(normalizedPath, out SaveFileInfo _))
if (FileTable.TryOpenFile(new U8Span(path.GetString()), out SaveFileInfo _))
{
entryType = DirectoryEntryType.File;
return Result.Success;
}
if (FileTable.TryOpenDirectory(normalizedPath, out SaveFindPosition _))
if (FileTable.TryOpenDirectory(new U8Span(path.GetString()), out SaveFindPosition _))
{
entryType = DirectoryEntryType.Directory;
return Result.Success;