Add non-system BCAT mount shim

This commit is contained in:
Alex Barney 2020-04-04 14:19:36 -07:00
parent 7bcb09b714
commit 12775895eb
4 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,56 @@
using System;
using LibHac.Common;
using LibHac.FsService;
using LibHac.Ncm;
namespace LibHac.Fs.Shim
{
public static class BcatSaveData
{
public static Result MountBcatSaveData(this FileSystemClient fs, U8Span mountName, TitleId applicationId)
{
Result rc;
if (fs.IsEnabledAccessLog(AccessLogTarget.System))
{
TimeSpan startTime = fs.Time.GetCurrent();
rc = MountBcatSaveDataImpl(fs, mountName, applicationId);
TimeSpan endTime = fs.Time.GetCurrent();
string logMessage = $", name: \"{mountName.ToString()}\", applicationid: 0x{applicationId}\"";
fs.OutputAccessLog(rc, startTime, endTime, logMessage);
}
else
{
rc = MountBcatSaveDataImpl(fs, mountName, applicationId);
}
if (rc.IsFailure()) return rc;
if (fs.IsEnabledAccessLog(AccessLogTarget.System))
{
fs.EnableFileSystemAccessorAccessLog(mountName);
}
return Result.Success;
}
private static Result MountBcatSaveDataImpl(FileSystemClient fs, U8Span mountName, TitleId applicationId)
{
Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
SaveDataAttribute attribute = default;
attribute.TitleId = applicationId;
attribute.Type = SaveDataType.Bcat;
rc = fsProxy.OpenSaveDataFileSystem(out IFileSystem fileSystem, SaveDataSpaceId.User, ref attribute);
if (rc.IsFailure()) return rc;
return fs.Register(mountName, fileSystem);
}
}
}

View File

@ -0,0 +1,51 @@
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.Ncm;
using Xunit;
namespace LibHac.Tests.Fs.FileSystemClientTests.ShimTests
{
public class BcatSaveData
{
[Fact]
public void MountBcatSaveData_SaveDoesNotExist_ReturnsTargetNotFound()
{
var applicationId = new TitleId(1);
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
Assert.Result(ResultFs.TargetNotFound, fs.MountBcatSaveData("bcat_test".ToU8Span(), applicationId));
}
[Fact]
public void MountBcatSaveData_SaveExists_ReturnsSuccess()
{
var applicationId = new TitleId(1);
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
Assert.Success(fs.CreateBcatSaveData(applicationId, 0x400000));
Assert.Success(fs.MountBcatSaveData("bcat_test".ToU8Span(), applicationId));
}
[Fact]
public void MountBcatSaveData_WrittenDataPersists()
{
var applicationId = new TitleId(1);
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
Assert.Success(fs.CreateBcatSaveData(applicationId, 0x400000));
Assert.Success(fs.MountBcatSaveData("bcat_test".ToU8Span(), applicationId));
// Check that the path doesn't exist
Assert.Result(ResultFs.PathNotFound, fs.GetEntryType(out _, "bcat_test:/file".ToU8Span()));
fs.CreateFile("bcat_test:/file".ToU8Span(), 0);
fs.Commit("bcat_test".ToU8Span());
fs.Unmount("bcat_test".ToU8Span());
Assert.Success(fs.MountBcatSaveData("bcat_test".ToU8Span(), applicationId));
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "bcat_test:/file".ToU8Span()));
Assert.Equal(DirectoryEntryType.File, type);
}
}
}

View File

@ -36,6 +36,7 @@ namespace LibHac.Tests.Fs.FileSystemClientTests.ShimTests
Assert.Success(fs.GetEntryType(out DirectoryEntryType type, "cache:/file".ToU8Span()));
Assert.Equal(DirectoryEntryType.File, type);
}
[Fact]
public void MountCacheStorage_SdCardIsPreferredOverBis()
{

View File

@ -84,5 +84,33 @@ namespace LibHac.Tests.Fs.FileSystemClientTests.ShimTests
Assert.Equal(expectedIndexes, actualIndexes);
}
[Fact]
public void CreateBcatSaveData_DoesNotExist_SaveIsCreated()
{
var applicationId = new TitleId(1);
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
Assert.Success(fs.CreateBcatSaveData(applicationId, 0x400000));
fs.OpenSaveDataIterator(out SaveDataIterator iterator, SaveDataSpaceId.User);
var info = new SaveDataInfo[2];
iterator.ReadSaveDataInfo(out long entriesRead, info);
Assert.Equal(1, entriesRead);
Assert.Equal(applicationId, info[0].TitleId);
Assert.Equal(SaveDataType.Bcat, info[0].Type);
}
[Fact]
public void CreateBcatSaveData_AlreadyExists_ReturnsPathAlreadyExists()
{
var applicationId = new TitleId(1);
FileSystemClient fs = FileSystemServerFactory.CreateClient(true);
Assert.Success(fs.CreateBcatSaveData(applicationId, 0x400000));
Assert.Result(ResultFs.PathAlreadyExists, fs.CreateBcatSaveData(applicationId, 0x400000));
}
}
}