From fef6d199005c7b89e14345b7d9a312ae3e7247cb Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 17 Oct 2019 15:52:29 -0500 Subject: [PATCH] Rearrange some FS client functions --- src/LibHac/Fs/Accessors/FileSystemAccessor.cs | 2 +- src/LibHac/Fs/GameCard.cs | 85 ----------------- src/LibHac/Fs/ICommonMountNameGenerator.cs | 2 +- src/LibHac/Fs/{ => Shim}/ContentStorage.cs | 4 +- src/LibHac/Fs/{ => Shim}/CustomStorage.cs | 2 +- src/LibHac/Fs/Shim/GameCard.cs | 92 +++++++++++++++++++ .../Fs/{ExternalKeys.cs => Shim/RightsId.cs} | 15 +-- src/LibHac/Fs/Shim/SaveData.cs | 66 +++++++++++++ .../SaveDataManagement.cs} | 88 +----------------- src/LibHac/Fs/Shim/SystemSaveData.cs | 31 +++++++ src/LibHac/FsService/FileSystemProxyCore.cs | 2 + src/LibHac/FsService/SaveDataIndexer.cs | 1 + 12 files changed, 208 insertions(+), 182 deletions(-) rename src/LibHac/Fs/{ => Shim}/ContentStorage.cs (96%) rename src/LibHac/Fs/{ => Shim}/CustomStorage.cs (97%) create mode 100644 src/LibHac/Fs/Shim/GameCard.cs rename src/LibHac/Fs/{ExternalKeys.cs => Shim/RightsId.cs} (84%) create mode 100644 src/LibHac/Fs/Shim/SaveData.cs rename src/LibHac/Fs/{SaveData.cs => Shim/SaveDataManagement.cs} (56%) create mode 100644 src/LibHac/Fs/Shim/SystemSaveData.cs diff --git a/src/LibHac/Fs/Accessors/FileSystemAccessor.cs b/src/LibHac/Fs/Accessors/FileSystemAccessor.cs index e6202b58..1cd96262 100644 --- a/src/LibHac/Fs/Accessors/FileSystemAccessor.cs +++ b/src/LibHac/Fs/Accessors/FileSystemAccessor.cs @@ -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) diff --git a/src/LibHac/Fs/GameCard.cs b/src/LibHac/Fs/GameCard.cs index 7b2058b7..e8d42a37 100644 --- a/src/LibHac/Fs/GameCard.cs +++ b/src/LibHac/Fs/GameCard.cs @@ -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 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 diff --git a/src/LibHac/Fs/ICommonMountNameGenerator.cs b/src/LibHac/Fs/ICommonMountNameGenerator.cs index 53838931..32211de5 100644 --- a/src/LibHac/Fs/ICommonMountNameGenerator.cs +++ b/src/LibHac/Fs/ICommonMountNameGenerator.cs @@ -4,6 +4,6 @@ namespace LibHac.Fs { public interface ICommonMountNameGenerator { - Result Generate(Span nameBuffer); + Result GenerateCommonMountName(Span nameBuffer); } } \ No newline at end of file diff --git a/src/LibHac/Fs/ContentStorage.cs b/src/LibHac/Fs/Shim/ContentStorage.cs similarity index 96% rename from src/LibHac/Fs/ContentStorage.cs rename to src/LibHac/Fs/Shim/ContentStorage.cs index c74d2763..1c6e0097 100644 --- a/src/LibHac/Fs/ContentStorage.cs +++ b/src/LibHac/Fs/Shim/ContentStorage.cs @@ -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 nameBuffer) + public Result GenerateCommonMountName(Span nameBuffer) { U8String mountName = GetContentStorageMountName(StorageId); diff --git a/src/LibHac/Fs/CustomStorage.cs b/src/LibHac/Fs/Shim/CustomStorage.cs similarity index 97% rename from src/LibHac/Fs/CustomStorage.cs rename to src/LibHac/Fs/Shim/CustomStorage.cs index f97727b1..a2482873 100644 --- a/src/LibHac/Fs/CustomStorage.cs +++ b/src/LibHac/Fs/Shim/CustomStorage.cs @@ -2,7 +2,7 @@ using LibHac.Common; using LibHac.FsService; -namespace LibHac.Fs +namespace LibHac.Fs.Shim { public static class CustomStorage { diff --git a/src/LibHac/Fs/Shim/GameCard.cs b/src/LibHac/Fs/Shim/GameCard.cs new file mode 100644 index 00000000..fe374f6a --- /dev/null +++ b/src/LibHac/Fs/Shim/GameCard.cs @@ -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 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); + } + } + } + } +} diff --git a/src/LibHac/Fs/ExternalKeys.cs b/src/LibHac/Fs/Shim/RightsId.cs similarity index 84% rename from src/LibHac/Fs/ExternalKeys.cs rename to src/LibHac/Fs/Shim/RightsId.cs index e0638600..dd3d1728 100644 --- a/src/LibHac/Fs/ExternalKeys.cs +++ b/src/LibHac/Fs/Shim/RightsId.cs @@ -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(); diff --git a/src/LibHac/Fs/Shim/SaveData.cs b/src/LibHac/Fs/Shim/SaveData.cs new file mode 100644 index 00000000..1ed6e25e --- /dev/null +++ b/src/LibHac/Fs/Shim/SaveData.cs @@ -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); + } + } +} diff --git a/src/LibHac/Fs/SaveData.cs b/src/LibHac/Fs/Shim/SaveDataManagement.cs similarity index 56% rename from src/LibHac/Fs/SaveData.cs rename to src/LibHac/Fs/Shim/SaveDataManagement.cs index a612c824..409a28be 100644 --- a/src/LibHac/Fs/SaveData.cs +++ b/src/LibHac/Fs/Shim/SaveDataManagement.cs @@ -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) { diff --git a/src/LibHac/Fs/Shim/SystemSaveData.cs b/src/LibHac/Fs/Shim/SystemSaveData.cs new file mode 100644 index 00000000..3bb55ef0 --- /dev/null +++ b/src/LibHac/Fs/Shim/SystemSaveData.cs @@ -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); + } + } +} diff --git a/src/LibHac/FsService/FileSystemProxyCore.cs b/src/LibHac/FsService/FileSystemProxyCore.cs index c53b32da..c1ff51d8 100644 --- a/src/LibHac/FsService/FileSystemProxyCore.cs +++ b/src/LibHac/FsService/FileSystemProxyCore.cs @@ -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 { diff --git a/src/LibHac/FsService/SaveDataIndexer.cs b/src/LibHac/FsService/SaveDataIndexer.cs index 9b513938..032554c6 100644 --- a/src/LibHac/FsService/SaveDataIndexer.cs +++ b/src/LibHac/FsService/SaveDataIndexer.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using LibHac.Common; using LibHac.Fs; +using LibHac.Fs.Shim; using LibHac.Kvdb; namespace LibHac.FsService