From d0e608775eeb0584d263cb26d490a469395511a3 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 30 Jan 2021 01:54:43 -0700 Subject: [PATCH] Flesh out FileSystemProxy functions a bit more including 11.0.0 ones --- src/LibHac/FsSrv/AccessLogService.cs | 10 +++ src/LibHac/FsSrv/AccessLogServiceImpl.cs | 10 +++ src/LibHac/FsSrv/BaseFileSystemService.cs | 83 ++++++++++++++++--- src/LibHac/FsSrv/BaseFileSystemServiceImpl.cs | 3 +- src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs | 6 ++ src/LibHac/FsSrv/FileSystemProxyImpl.cs | 78 ++++++++++++++--- src/LibHac/FsSrv/Impl/AccessControl.cs | 14 +++- src/LibHac/FsSrv/SaveDataFileSystemService.cs | 34 ++++++++ 8 files changed, 214 insertions(+), 24 deletions(-) diff --git a/src/LibHac/FsSrv/AccessLogService.cs b/src/LibHac/FsSrv/AccessLogService.cs index 8f2bf4e5..33b67002 100644 --- a/src/LibHac/FsSrv/AccessLogService.cs +++ b/src/LibHac/FsSrv/AccessLogService.cs @@ -42,12 +42,22 @@ namespace LibHac.FsSrv return _serviceImpl.OutputAccessLogToSdCard(textBuffer.Buffer, programInfo.ProgramIdValue, _processId); } + public Result OutputApplicationInfoAccessLog(in ApplicationInfo applicationInfo) + { + throw new NotImplementedException(); + } + public Result OutputMultiProgramTagAccessLog() { _serviceImpl.OutputAccessLogToSdCard(MultiProgramTag, _processId).IgnoreResult(); return Result.Success; } + public Result FlushAccessLogOnSdCard() + { + throw new NotImplementedException(); + } + private Result GetProgramInfo(out ProgramInfo programInfo) { return _serviceImpl.GetProgramInfo(out programInfo, _processId); diff --git a/src/LibHac/FsSrv/AccessLogServiceImpl.cs b/src/LibHac/FsSrv/AccessLogServiceImpl.cs index 378610fd..b9274035 100644 --- a/src/LibHac/FsSrv/AccessLogServiceImpl.cs +++ b/src/LibHac/FsSrv/AccessLogServiceImpl.cs @@ -48,6 +48,16 @@ namespace LibHac.FsSrv throw new NotImplementedException(); } + public Result FlushAccessLogSdCardWriter() + { + throw new NotImplementedException(); + } + + public Result FinalizeAccessLogSdCardWriter() + { + throw new NotImplementedException(); + } + internal Result GetProgramInfo(out ProgramInfo programInfo, ulong processId) { return _config.ProgramRegistry.GetProgramInfo(out programInfo, processId); diff --git a/src/LibHac/FsSrv/BaseFileSystemService.cs b/src/LibHac/FsSrv/BaseFileSystemService.cs index 4d6e25c3..43371e76 100644 --- a/src/LibHac/FsSrv/BaseFileSystemService.cs +++ b/src/LibHac/FsSrv/BaseFileSystemService.cs @@ -1,4 +1,5 @@ -using LibHac.Fs; +using System; +using LibHac.Fs; using LibHac.FsSrv.Impl; using LibHac.FsSrv.Sf; using LibHac.Sf; @@ -18,6 +19,68 @@ namespace LibHac.FsSrv _processId = processId; } + private Result GetProgramInfo(out ProgramInfo programInfo) + { + return GetProgramInfo(out programInfo, _processId); + } + + private Result GetProgramInfo(out ProgramInfo programInfo, ulong processId) + { + return _serviceImpl.GetProgramInfo(out programInfo, processId); + } + + private Result CheckCapabilityById(BaseFileSystemId id, ulong processId) + { + Result rc = GetProgramInfo(out ProgramInfo programInfo, processId); + if (rc.IsFailure()) return rc; + + if (id == BaseFileSystemId.TemporaryDirectory) + { + Accessibility accessibility = + programInfo.AccessControl.GetAccessibilityFor(AccessibilityType.MountTemporaryDirectory); + + if (!accessibility.CanRead || !accessibility.CanWrite) + return ResultFs.PermissionDenied.Log(); + } + else + { + Accessibility accessibility = + programInfo.AccessControl.GetAccessibilityFor(AccessibilityType.MountAllBaseFileSystem); + + if (!accessibility.CanRead || !accessibility.CanWrite) + return ResultFs.PermissionDenied.Log(); + } + + return Result.Success; + } + + public Result OpenBaseFileSystem(out ReferenceCountedDisposable fileSystem, + BaseFileSystemId fileSystemId) + { + fileSystem = default; + + Result rc = CheckCapabilityById(fileSystemId, _processId); + if (rc.IsFailure()) return rc; + + ReferenceCountedDisposable fs = null; + + try + { + // Open the file system + rc = _serviceImpl.OpenBaseFileSystem(out fs, fileSystemId); + if (rc.IsFailure()) return rc; + + // Create an SF adapter for the file system + fileSystem = FileSystemInterfaceAdapter.CreateShared(ref fs); + + return Result.Success; + } + finally + { + fs?.Dispose(); + } + } + public Result OpenBisFileSystem(out ReferenceCountedDisposable fileSystem, in FspPath rootPath, BisPartitionId partitionId) { @@ -71,6 +134,11 @@ namespace LibHac.FsSrv } } + public Result SetBisRootForHost(BisPartitionId partitionId, in FspPath path) + { + throw new NotImplementedException(); + } + public Result CreatePaddingFile(long size) { // File size must be non-negative @@ -201,14 +269,14 @@ namespace LibHac.FsSrv return ResultFs.PermissionDenied.Log(); // Get the base file system ID - int id; + BaseFileSystemId fileSystemId; switch (directoryId) { case ImageDirectoryId.Nand: - id = 0; + fileSystemId = BaseFileSystemId.ImageDirectoryNand; break; case ImageDirectoryId.SdCard: - id = 1; + fileSystemId = BaseFileSystemId.ImageDirectorySdCard; break; default: return ResultFs.InvalidArgument.Log(); @@ -217,7 +285,7 @@ namespace LibHac.FsSrv try { - rc = _serviceImpl.OpenBaseFileSystem(out fs, id); + rc = _serviceImpl.OpenBaseFileSystem(out fs, fileSystemId); if (rc.IsFailure()) return rc; // Create an SF adapter for the file system @@ -248,10 +316,5 @@ namespace LibHac.FsSrv bisWiper = new ReferenceCountedDisposable(wiper); return Result.Success; } - - private Result GetProgramInfo(out ProgramInfo programInfo) - { - return _serviceImpl.GetProgramInfo(out programInfo, _processId); - } } } diff --git a/src/LibHac/FsSrv/BaseFileSystemServiceImpl.cs b/src/LibHac/FsSrv/BaseFileSystemServiceImpl.cs index 2d28c9ce..c93db88c 100644 --- a/src/LibHac/FsSrv/BaseFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/BaseFileSystemServiceImpl.cs @@ -35,7 +35,8 @@ namespace LibHac.FsSrv public ProgramRegistryImpl ProgramRegistry; } - public Result OpenBaseFileSystem(out ReferenceCountedDisposable fileSystem, int fileSystemId) + public Result OpenBaseFileSystem(out ReferenceCountedDisposable fileSystem, + BaseFileSystemId fileSystemId) { throw new NotImplementedException(); } diff --git a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs index ea962aa5..2713afbf 100644 --- a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs @@ -23,6 +23,12 @@ namespace LibHac.FsSrv ProgramRegistry = new ProgramRegistryImpl(Config.ProgramRegistryService); } + public Result OpenCloudBackupWorkStorageFileSystem(out ReferenceCountedDisposable fileSystem, + CloudBackupWorkStorageId storageId) + { + throw new NotImplementedException(); + } + public Result OpenCustomStorageFileSystem(out ReferenceCountedDisposable fileSystem, CustomStorageId storageId) { diff --git a/src/LibHac/FsSrv/FileSystemProxyImpl.cs b/src/LibHac/FsSrv/FileSystemProxyImpl.cs index 9e49936f..a7488705 100644 --- a/src/LibHac/FsSrv/FileSystemProxyImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyImpl.cs @@ -443,7 +443,7 @@ namespace LibHac.FsSrv public Result OpenBaseFileSystem(out ReferenceCountedDisposable fileSystem, BaseFileSystemId fileSystemId) { - throw new NotImplementedException(); + return GetBaseFileSystemService().OpenBaseFileSystem(out fileSystem, fileSystemId); } public Result OpenBisFileSystem(out ReferenceCountedDisposable fileSystem, in FspPath rootPath, @@ -697,31 +697,56 @@ namespace LibHac.FsSrv public Result OpenSaveDataTransferManager(out ReferenceCountedDisposable manager) { - throw new NotImplementedException(); + manager = default; + + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OpenSaveDataTransferManager(out manager); } public Result OpenSaveDataTransferManagerVersion2( out ReferenceCountedDisposable manager) { - throw new NotImplementedException(); + manager = default; + + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OpenSaveDataTransferManagerVersion2(out manager); } public Result OpenSaveDataTransferManagerForSaveDataRepair( out ReferenceCountedDisposable manager) { - throw new NotImplementedException(); + manager = default; + + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OpenSaveDataTransferManagerForSaveDataRepair(out manager); } public Result OpenSaveDataTransferManagerForRepair( out ReferenceCountedDisposable manager) { - throw new NotImplementedException(); + manager = default; + + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OpenSaveDataTransferManagerForRepair(out manager); } public Result OpenSaveDataTransferProhibiter( out ReferenceCountedDisposable prohibiter, Ncm.ApplicationId applicationId) { - throw new NotImplementedException(); + prohibiter = default; + + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OpenSaveDataTransferProhibiter(out prohibiter, applicationId); } public Result ListAccessibleSaveDataOwnerId(out int readCount, OutBuffer idBuffer, ProgramId programId, @@ -787,7 +812,35 @@ namespace LibHac.FsSrv public Result OpenCloudBackupWorkStorageFileSystem(out ReferenceCountedDisposable fileSystem, CloudBackupWorkStorageId storageId) { - throw new NotImplementedException(); + fileSystem = default; + var storageFlag = StorageType.NonGameCard; + using var scopedLayoutType = new ScopedStorageLayoutTypeSetter(storageFlag); + + Result rc = GetProgramInfo(out ProgramInfo programInfo); + if (rc.IsFailure()) return rc; + + Accessibility accessibility = + programInfo.AccessControl.GetAccessibilityFor(AccessibilityType.MountCloudBackupWorkStorage); + + if (!accessibility.CanRead || !accessibility.CanWrite) + return ResultFs.PermissionDenied.Log(); + + ReferenceCountedDisposable tempFs = null; + try + { + rc = FsProxyCore.OpenCloudBackupWorkStorageFileSystem(out tempFs, storageId); + if (rc.IsFailure()) return rc; + + tempFs = StorageLayoutTypeSetFileSystem.CreateShared(ref tempFs, storageFlag); + tempFs = AsynchronousAccessFileSystem.CreateShared(ref tempFs); + fileSystem = FileSystemInterfaceAdapter.CreateShared(ref tempFs); + + return Result.Success; + } + finally + { + tempFs?.Dispose(); + } } public Result OpenCustomStorageFileSystem(out ReferenceCountedDisposable fileSystem, CustomStorageId storageId) @@ -933,7 +986,7 @@ namespace LibHac.FsSrv public Result SetBisRootForHost(BisPartitionId partitionId, in FspPath path) { - throw new NotImplementedException(); + return GetBaseFileSystemService().SetBisRootForHost(partitionId, in path); } public Result VerifySaveDataFileSystemBySaveDataSpaceId(SaveDataSpaceId spaceId, ulong saveDataId, @@ -1015,12 +1068,12 @@ namespace LibHac.FsSrv public Result OutputApplicationInfoAccessLog(in ApplicationInfo applicationInfo) { - throw new NotImplementedException(); + return GetAccessLogService().OutputApplicationInfoAccessLog(in applicationInfo); } public Result FlushAccessLogOnSdCard() { - throw new NotImplementedException(); + return GetAccessLogService().FlushAccessLogOnSdCard(); } public Result RegisterUpdatePartition() @@ -1055,7 +1108,10 @@ namespace LibHac.FsSrv public Result OverrideSaveDataTransferTokenSignVerificationKey(InBuffer key) { - throw new NotImplementedException(); + Result rc = GetSaveDataFileSystemService(out SaveDataFileSystemService saveFsService); + if (rc.IsFailure()) return rc; + + return saveFsService.OverrideSaveDataTransferTokenSignVerificationKey(key); } public Result SetSdCardAccessibility(bool isAccessible) diff --git a/src/LibHac/FsSrv/Impl/AccessControl.cs b/src/LibHac/FsSrv/Impl/AccessControl.cs index 9d526d83..4e166f06 100644 --- a/src/LibHac/FsSrv/Impl/AccessControl.cs +++ b/src/LibHac/FsSrv/Impl/AccessControl.cs @@ -396,7 +396,7 @@ namespace LibHac.FsSrv.Impl return new Accessibility(accessBits.CanMountContentStorageRead(), accessBits.CanMountContentStorageWrite()); case AccessibilityType.MountImageAndVideoStorage: return new Accessibility(accessBits.CanMountImageAndVideoStorageRead(), accessBits.CanMountImageAndVideoStorageWrite()); - case AccessibilityType.MountCloudBackupWorkStorageRead: + case AccessibilityType.MountCloudBackupWorkStorage: return new Accessibility(accessBits.CanMountCloudBackupWorkStorageRead(), accessBits.CanMountCloudBackupWorkStorageWrite()); case AccessibilityType.MountCustomStorage: return new Accessibility(accessBits.CanMountCustomStorage0Read(), accessBits.CanMountCustomStorage0Write()); @@ -468,6 +468,10 @@ namespace LibHac.FsSrv.Impl return new Accessibility(accessBits.CanMountRegisteredUpdatePartitionRead() && FsServer.IsDebugMode, false); case AccessibilityType.MountSaveDataInternalStorage: return new Accessibility(accessBits.CanOpenSaveDataInternalStorageRead(), accessBits.CanOpenSaveDataInternalStorageWrite()); + case AccessibilityType.MountTemporaryDirectory: + return new Accessibility(accessBits.CanMountTemporaryDirectoryRead(), accessBits.CanMountTemporaryDirectoryWrite()); + case AccessibilityType.MountAllBaseFileSystem: + return new Accessibility(accessBits.CanMountAllBaseFileSystemRead(), accessBits.CanMountAllBaseFileSystemWrite()); case AccessibilityType.NotMount: return new Accessibility(false, false); default: @@ -611,6 +615,8 @@ namespace LibHac.FsSrv.Impl public bool CanInvalidateBisCache() => Has(Bits.BisAllRaw); public bool CanIsAccessFailureDetected() => Has(Bits.AccessFailureResolution); public bool CanListAccessibleSaveDataOwnerId() => Has(Bits.SaveDataTransferVersion2 | Bits.SaveDataTransfer | Bits.CreateSaveData); + public bool CanMountAllBaseFileSystemRead() => Has(Bits.None); + public bool CanMountAllBaseFileSystemWrite() => Has(Bits.None); public bool CanMountApplicationPackageRead() => Has(Bits.ContentManager | Bits.ApplicationInfo); public bool CanMountBisCalibrationFileRead() => Has(Bits.BisAllRaw | Bits.Calibration); public bool CanMountBisCalibrationFileWrite() => Has(Bits.BisAllRaw | Bits.Calibration); @@ -654,6 +660,8 @@ namespace LibHac.FsSrv.Impl public bool CanMountSystemDataPrivateRead() => Has(Bits.SystemData | Bits.SystemSaveData); public bool CanMountSystemSaveDataRead() => Has(Bits.SaveDataBackUp | Bits.SystemSaveData); public bool CanMountSystemSaveDataWrite() => Has(Bits.SaveDataBackUp | Bits.SystemSaveData); + public bool CanMountTemporaryDirectoryRead() => Has(Bits.Debug); + public bool CanMountTemporaryDirectoryWrite() => Has(Bits.Debug); public bool CanNotifySystemDataUpdateEvent() => Has(Bits.SystemUpdate); public bool CanOpenAccessFailureDetectionEventNotifier() => Has(Bits.AccessFailureResolution); public bool CanOpenBisPartitionBootConfigAndPackage2Part1Read() => Has(Bits.SystemUpdate | Bits.BisAllRaw); @@ -850,7 +858,7 @@ namespace LibHac.FsSrv.Impl MountSaveDataStorage, MountContentStorage, MountImageAndVideoStorage, - MountCloudBackupWorkStorageRead, + MountCloudBackupWorkStorage, MountCustomStorage, MountBisCalibrationFile, MountBisSafeMode, @@ -886,6 +894,8 @@ namespace LibHac.FsSrv.Impl MountHost, MountRegisteredUpdatePartition, MountSaveDataInternalStorage, + MountTemporaryDirectory, + MountAllBaseFileSystem, NotMount } } diff --git a/src/LibHac/FsSrv/SaveDataFileSystemService.cs b/src/LibHac/FsSrv/SaveDataFileSystemService.cs index e334fb32..bdd801ef 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemService.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemService.cs @@ -1573,6 +1573,35 @@ namespace LibHac.FsSrv throw new NotImplementedException(); } + public Result OpenSaveDataTransferManager(out ReferenceCountedDisposable manager) + { + throw new NotImplementedException(); + } + + public Result OpenSaveDataTransferManagerVersion2( + out ReferenceCountedDisposable manager) + { + throw new NotImplementedException(); + } + + public Result OpenSaveDataTransferManagerForSaveDataRepair( + out ReferenceCountedDisposable manager) + { + throw new NotImplementedException(); + } + + public Result OpenSaveDataTransferManagerForRepair( + out ReferenceCountedDisposable manager) + { + throw new NotImplementedException(); + } + + public Result OpenSaveDataTransferProhibiter( + out ReferenceCountedDisposable prohibiter, Ncm.ApplicationId applicationId) + { + throw new NotImplementedException(); + } + public Result OpenSaveDataMover(out ReferenceCountedDisposable saveMover, SaveDataSpaceId sourceSpaceId, SaveDataSpaceId destinationSpaceId, NativeHandle workBufferHandle, ulong workBufferSize) @@ -1814,6 +1843,11 @@ namespace LibHac.FsSrv } } + public Result OverrideSaveDataTransferTokenSignVerificationKey(InBuffer key) + { + throw new NotImplementedException(); + } + public Result SetSdCardAccessibility(bool isAccessible) { Result rc = GetProgramInfo(out ProgramInfo programInfo);