Rearrange some FS client functions

This commit is contained in:
Alex Barney 2019-10-17 15:52:29 -05:00
parent ee9bee3efb
commit fef6d19900
12 changed files with 208 additions and 182 deletions

View file

@ -153,7 +153,7 @@ namespace LibHac.Fs.Accessors
{
if (MountNameGenerator == null) return ResultFs.PreconditionViolation;
return MountNameGenerator.Generate(nameBuffer);
return MountNameGenerator.GenerateCommonMountName(nameBuffer);
}
internal void NotifyCloseFile(FileAccessor file)

View file

@ -1,60 +1,9 @@
using System;
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs
{
public static class GameCard
{
public static Result OpenGameCardPartition(this FileSystemClient fs, out IStorage storage,
GameCardHandle handle, GameCardPartitionRaw partitionType)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.OpenGameCardStorage(out storage, handle, partitionType);
}
public static Result MountGameCardPartition(this FileSystemClient fs, U8Span mountName, GameCardHandle handle,
GameCardPartition partitionId)
{
Result rc = MountHelpers.CheckMountNameAcceptingReservedMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
rc = fsProxy.OpenGameCardFileSystem(out IFileSystem cardFs, handle, partitionId);
if (rc.IsFailure()) return rc;
var mountNameGenerator = new GameCardCommonMountNameGenerator(handle, partitionId);
return fs.Register(mountName, cardFs, mountNameGenerator);
}
public static Result GetGameCardHandle(this FileSystemClient fs, out GameCardHandle handle)
{
handle = default;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
Result rc = fsProxy.OpenDeviceOperator(out IDeviceOperator deviceOperator);
if (rc.IsFailure()) return rc;
return deviceOperator.GetGameCardHandle(out handle);
}
public static bool IsGameCardInserted(this FileSystemClient fs)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
Result rc = fsProxy.OpenDeviceOperator(out IDeviceOperator deviceOperator);
if (rc.IsFailure()) throw new LibHacException("Abort");
rc = deviceOperator.IsGameCardInserted(out bool isInserted);
if (rc.IsFailure()) throw new LibHacException("Abort");
return isInserted;
}
public static long GetGameCardSizeBytes(GameCardSize size)
{
switch (size)
@ -74,40 +23,6 @@ namespace LibHac.Fs
{
return (long)page << 9;
}
private class GameCardCommonMountNameGenerator : ICommonMountNameGenerator
{
private GameCardHandle Handle { get; }
private GameCardPartition PartitionId { get; }
public GameCardCommonMountNameGenerator(GameCardHandle handle, GameCardPartition partitionId)
{
Handle = handle;
PartitionId = partitionId;
}
public Result Generate(Span<byte> nameBuffer)
{
char letter = GetPartitionMountLetter(PartitionId);
string mountName = $"@Gc{letter}{Handle.Value:x8}";
new U8Span(mountName).Value.CopyTo(nameBuffer);
return Result.Success;
}
private static char GetPartitionMountLetter(GameCardPartition partition)
{
switch (partition)
{
case GameCardPartition.Update: return 'U';
case GameCardPartition.Normal: return 'N';
case GameCardPartition.Secure: return 'S';
default:
throw new ArgumentOutOfRangeException(nameof(partition), partition, null);
}
}
}
}
public enum GameCardSize

View file

@ -4,6 +4,6 @@ namespace LibHac.Fs
{
public interface ICommonMountNameGenerator
{
Result Generate(Span<byte> nameBuffer);
Result GenerateCommonMountName(Span<byte> nameBuffer);
}
}

View file

@ -2,7 +2,7 @@
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs
namespace LibHac.Fs.Shim
{
public static class ContentStorage
{
@ -54,7 +54,7 @@ namespace LibHac.Fs
StorageId = storageId;
}
public Result Generate(Span<byte> nameBuffer)
public Result GenerateCommonMountName(Span<byte> nameBuffer)
{
U8String mountName = GetContentStorageMountName(StorageId);

View file

@ -2,7 +2,7 @@
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs
namespace LibHac.Fs.Shim
{
public static class CustomStorage
{

View file

@ -0,0 +1,92 @@
using System;
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs.Shim
{
public static class GameCard
{
public static Result GetGameCardHandle(this FileSystemClient fs, out GameCardHandle handle)
{
handle = default;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
Result rc = fsProxy.OpenDeviceOperator(out IDeviceOperator deviceOperator);
if (rc.IsFailure()) return rc;
return deviceOperator.GetGameCardHandle(out handle);
}
public static bool IsGameCardInserted(this FileSystemClient fs)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
Result rc = fsProxy.OpenDeviceOperator(out IDeviceOperator deviceOperator);
if (rc.IsFailure()) throw new LibHacException("Abort");
rc = deviceOperator.IsGameCardInserted(out bool isInserted);
if (rc.IsFailure()) throw new LibHacException("Abort");
return isInserted;
}
public static Result OpenGameCardPartition(this FileSystemClient fs, out IStorage storage,
GameCardHandle handle, GameCardPartitionRaw partitionType)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.OpenGameCardStorage(out storage, handle, partitionType);
}
public static Result MountGameCardPartition(this FileSystemClient fs, U8Span mountName, GameCardHandle handle,
GameCardPartition partitionId)
{
Result rc = MountHelpers.CheckMountNameAcceptingReservedMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
rc = fsProxy.OpenGameCardFileSystem(out IFileSystem cardFs, handle, partitionId);
if (rc.IsFailure()) return rc;
var mountNameGenerator = new GameCardCommonMountNameGenerator(handle, partitionId);
return fs.Register(mountName, cardFs, mountNameGenerator);
}
private class GameCardCommonMountNameGenerator : ICommonMountNameGenerator
{
private GameCardHandle Handle { get; }
private GameCardPartition PartitionId { get; }
public GameCardCommonMountNameGenerator(GameCardHandle handle, GameCardPartition partitionId)
{
Handle = handle;
PartitionId = partitionId;
}
public Result GenerateCommonMountName(Span<byte> nameBuffer)
{
char letter = GetPartitionMountLetter(PartitionId);
string mountName = $"@Gc{letter}{Handle.Value:x8}";
new U8Span(mountName).Value.CopyTo(nameBuffer);
return Result.Success;
}
private static char GetPartitionMountLetter(GameCardPartition partition)
{
switch (partition)
{
case GameCardPartition.Update: return 'U';
case GameCardPartition.Normal: return 'N';
case GameCardPartition.Secure: return 'S';
default:
throw new ArgumentOutOfRangeException(nameof(partition), partition, null);
}
}
}
}
}

View file

@ -3,12 +3,13 @@ using LibHac.FsService;
using LibHac.FsSystem;
using LibHac.Ncm;
using LibHac.Spl;
using FsRightsId = LibHac.Fs.RightsId;
namespace LibHac.Fs
namespace LibHac.Fs.Shim
{
public static class ExternalKeys
public static class RightsId
{
public static Result GetRightsId(this FileSystemClient fs, out RightsId rightsId, TitleId programId,
public static Result GetRightsId(this FileSystemClient fs, out FsRightsId rightsId, TitleId programId,
StorageId storageId)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
@ -16,7 +17,7 @@ namespace LibHac.Fs
return fsProxy.GetRightsId(out rightsId, programId, storageId);
}
public static Result GetRightsId(this FileSystemClient fs, out RightsId rightsId, U8Span path)
public static Result GetRightsId(this FileSystemClient fs, out FsRightsId rightsId, U8Span path)
{
rightsId = default;
@ -28,7 +29,7 @@ namespace LibHac.Fs
return fsProxy.GetRightsIdByPath(out rightsId, ref fsPath);
}
public static Result GetRightsId(this FileSystemClient fs, out RightsId rightsId, out byte keyGeneration, U8Span path)
public static Result GetRightsId(this FileSystemClient fs, out FsRightsId rightsId, out byte keyGeneration, U8Span path)
{
rightsId = default;
keyGeneration = default;
@ -41,14 +42,14 @@ namespace LibHac.Fs
return fsProxy.GetRightsIdAndKeyGenerationByPath(out rightsId, out keyGeneration, ref fsPath);
}
public static Result RegisterExternalKey(this FileSystemClient fs, ref RightsId rightsId, ref AccessKey key)
public static Result RegisterExternalKey(this FileSystemClient fs, ref FsRightsId rightsId, ref AccessKey key)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.RegisterExternalKey(ref rightsId, ref key);
}
public static Result UnregisterExternalKey(this FileSystemClient fs, ref RightsId rightsId)
public static Result UnregisterExternalKey(this FileSystemClient fs, ref FsRightsId rightsId)
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();

View file

@ -0,0 +1,66 @@
using System;
using LibHac.Common;
using LibHac.FsService;
using LibHac.FsSystem.Save;
using LibHac.Ncm;
namespace LibHac.Fs.Shim
{
public static class SaveData
{
public static Result MountSaveData(this FileSystemClient fs, U8Span mountName, TitleId titleId, UserId userId)
{
Result rc;
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Application))
{
TimeSpan startTime = fs.Time.GetCurrent();
rc = MountSaveDataImpl(fs, mountName, SaveDataSpaceId.User, titleId, userId, SaveDataType.SaveData, false, 0);
TimeSpan endTime = fs.Time.GetCurrent();
fs.OutputAccessLog(rc, startTime, endTime, $", name: \"{mountName.ToString()}\", applicationid: 0x{titleId}, userid: {userId}");
}
else
{
rc = MountSaveDataImpl(fs, mountName, SaveDataSpaceId.User, titleId, userId, SaveDataType.SaveData, false, 0);
}
if (rc.IsSuccess() && fs.IsEnabledAccessLog(LocalAccessLogMode.Application))
{
fs.EnableFileSystemAccessorAccessLog(mountName);
}
return rc;
}
private static Result MountSaveDataImpl(this FileSystemClient fs, U8Span mountName, SaveDataSpaceId spaceId,
TitleId titleId, UserId userId, SaveDataType type, bool openReadOnly, short index)
{
Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
SaveDataAttribute attribute = default;
attribute.TitleId = titleId;
attribute.UserId = userId;
attribute.Type = type;
attribute.Index = index;
IFileSystem saveFs;
if (openReadOnly)
{
rc = fsProxy.OpenReadOnlySaveDataFileSystem(out saveFs, spaceId, ref attribute);
}
else
{
rc = fsProxy.OpenSaveDataFileSystem(out saveFs, spaceId, ref attribute);
}
if (rc.IsFailure()) return rc;
return fs.Register(mountName, saveFs);
}
}
}

View file

@ -1,91 +1,9 @@
using System;
using LibHac.Common;
using LibHac.FsService;
using LibHac.FsSystem.Save;
using LibHac.Ncm;
using LibHac.FsService;
namespace LibHac.Fs
namespace LibHac.Fs.Shim
{
public static class SaveData
public static class SaveDataManagement
{
public static Result MountSaveData(this FileSystemClient fs, U8Span mountName, TitleId titleId, UserId userId)
{
Result rc;
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Application))
{
TimeSpan startTime = fs.Time.GetCurrent();
rc = MountSaveDataImpl(fs, mountName, SaveDataSpaceId.User, titleId, userId, SaveDataType.SaveData, false, 0);
TimeSpan endTime = fs.Time.GetCurrent();
fs.OutputAccessLog(rc, startTime, endTime, $", name: \"{mountName.ToString()}\", applicationid: 0x{titleId}, userid: {userId}");
}
else
{
rc = MountSaveDataImpl(fs, mountName, SaveDataSpaceId.User, titleId, userId, SaveDataType.SaveData, false, 0);
}
if (rc.IsSuccess() && fs.IsEnabledAccessLog(LocalAccessLogMode.Application))
{
fs.EnableFileSystemAccessorAccessLog(mountName);
}
return rc;
}
private static Result MountSaveDataImpl(this FileSystemClient fs, U8Span mountName, SaveDataSpaceId spaceId,
TitleId titleId, UserId userId, SaveDataType type, bool openReadOnly, short index)
{
Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
SaveDataAttribute attribute = default;
attribute.TitleId = titleId;
attribute.UserId = userId;
attribute.Type = type;
attribute.Index = index;
IFileSystem saveFs;
if (openReadOnly)
{
rc = fsProxy.OpenReadOnlySaveDataFileSystem(out saveFs, spaceId, ref attribute);
}
else
{
rc = fsProxy.OpenSaveDataFileSystem(out saveFs, spaceId, ref attribute);
}
if (rc.IsFailure()) return rc;
return fs.Register(mountName, saveFs);
}
public static Result MountSystemSaveData(this FileSystemClient fs, U8Span mountName, SaveDataSpaceId spaceId, ulong saveDataId)
{
return MountSystemSaveData(fs, mountName, spaceId, saveDataId, UserId.Zero);
}
public static Result MountSystemSaveData(this FileSystemClient fs, U8Span mountName,
SaveDataSpaceId spaceId, ulong saveDataId, UserId userId)
{
Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
SaveDataAttribute attribute = default;
attribute.UserId = userId;
attribute.SaveDataId = saveDataId;
rc = fsProxy.OpenSaveDataFileSystemBySystemSaveDataId(out IFileSystem fileSystem, spaceId, ref attribute);
if (rc.IsFailure()) return rc;
return fs.Register(mountName, fileSystem);
}
public static Result CreateSystemSaveData(this FileSystemClient fs, SaveDataSpaceId spaceId,
ulong saveDataId, UserId userId, ulong ownerId, long size, long journalSize, uint flags)
{

View file

@ -0,0 +1,31 @@
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs.Shim
{
public static class SystemSaveData
{
public static Result MountSystemSaveData(this FileSystemClient fs, U8Span mountName, SaveDataSpaceId spaceId, ulong saveDataId)
{
return MountSystemSaveData(fs, mountName, spaceId, saveDataId, UserId.Zero);
}
public static Result MountSystemSaveData(this FileSystemClient fs, U8Span mountName,
SaveDataSpaceId spaceId, ulong saveDataId, UserId userId)
{
Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc;
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
SaveDataAttribute attribute = default;
attribute.UserId = userId;
attribute.SaveDataId = saveDataId;
rc = fsProxy.OpenSaveDataFileSystemBySystemSaveDataId(out IFileSystem fileSystem, spaceId, ref attribute);
if (rc.IsFailure()) return rc;
return fs.Register(mountName, fileSystem);
}
}
}

View file

@ -1,9 +1,11 @@
using System;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.FsSystem;
using LibHac.FsSystem.Save;
using LibHac.FsService.Creators;
using LibHac.Spl;
using RightsId = LibHac.Fs.RightsId;
namespace LibHac.FsService
{

View file

@ -1,6 +1,7 @@
using System.Diagnostics;
using LibHac.Common;
using LibHac.Fs;
using LibHac.Fs.Shim;
using LibHac.Kvdb;
namespace LibHac.FsService