From a94bb81c54b30598e4cb2c999facb75a172b303c Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 6 Feb 2021 22:04:00 -0700 Subject: [PATCH] Add FsSrv.Impl.FileSystemProxyServiceObject --- build/CodeGen/results.csv | 1 + src/LibHac/Fs/ResultFs.cs | 2 + .../Fs/Shim/FileSystemProxyServiceObject.cs | 21 ++--- src/LibHac/FsSrv/FileSystemServer.cs | 27 ++---- .../Impl/FileSystemProxyServiceObject.cs | 88 +++++++++++++++++++ 5 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 src/LibHac/FsSrv/Impl/FileSystemProxyServiceObject.cs diff --git a/build/CodeGen/results.csv b/build/CodeGen/results.csv index aa53fcbf..d1924714 100644 --- a/build/CodeGen/results.csv +++ b/build/CodeGen/results.csv @@ -661,6 +661,7 @@ Module,DescriptionStart,DescriptionEnd,Flags,Namespace,Name,Summary 2,6400,6449,,,PermissionDenied, +2,6450,,,,PortAcceptableCountLimited, 2,6452,,,,ExternalKeyAlreadyRegistered, 2,6454,,,,NeedFlush, 2,6455,,,,FileNotClosed, diff --git a/src/LibHac/Fs/ResultFs.cs b/src/LibHac/Fs/ResultFs.cs index 9dda2eae..947d1dbf 100644 --- a/src/LibHac/Fs/ResultFs.cs +++ b/src/LibHac/Fs/ResultFs.cs @@ -1182,6 +1182,8 @@ namespace LibHac.Fs /// Error code: 2002-6400; Range: 6400-6449; Inner value: 0x320002 public static Result.Base PermissionDenied { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new Result.Base(ModuleFs, 6400, 6449); } + /// Error code: 2002-6450; Inner value: 0x326402 + public static Result.Base PortAcceptableCountLimited => new Result.Base(ModuleFs, 6450); /// Error code: 2002-6452; Inner value: 0x326802 public static Result.Base ExternalKeyAlreadyRegistered => new Result.Base(ModuleFs, 6452); /// Error code: 2002-6454; Inner value: 0x326c02 diff --git a/src/LibHac/Fs/Shim/FileSystemProxyServiceObject.cs b/src/LibHac/Fs/Shim/FileSystemProxyServiceObject.cs index a5241506..ac71b78f 100644 --- a/src/LibHac/Fs/Shim/FileSystemProxyServiceObject.cs +++ b/src/LibHac/Fs/Shim/FileSystemProxyServiceObject.cs @@ -54,15 +54,15 @@ namespace LibHac.Fs.Shim throw new InvalidOperationException("Client was not initialized with a server object."); } - Result rc = fs.Hos.Sm.GetService(out IFileSystemProxy fsProxy, "fsp-srv"); + Result rc = fs.Hos.Sm.GetService(out ReferenceCountedDisposable fsProxy, "fsp-srv"); if (rc.IsFailure()) { throw new HorizonResultException(rc, "Failed to get file system proxy service object."); } - fsProxy.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); - return new ReferenceCountedDisposable(fsProxy); + fsProxy.Target.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); + return fsProxy; } public static ReferenceCountedDisposable GetFileSystemProxyForLoaderServiceObject( @@ -88,15 +88,16 @@ namespace LibHac.Fs.Shim throw new InvalidOperationException("Client was not initialized with a server object."); } - Result rc = fs.Hos.Sm.GetService(out IFileSystemProxyForLoader fsProxy, "fsp-ldr"); + Result rc = fs.Hos.Sm.GetService(out ReferenceCountedDisposable fsProxy, + "fsp-ldr"); if (rc.IsFailure()) { throw new HorizonResultException(rc, "Failed to get file system proxy service object."); } - fsProxy.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); - return new ReferenceCountedDisposable(fsProxy); + fsProxy.Target.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); + return fsProxy; } public static ReferenceCountedDisposable GetProgramRegistryServiceObject( @@ -122,19 +123,19 @@ namespace LibHac.Fs.Shim throw new InvalidOperationException("Client was not initialized with a server object."); } - Result rc = fs.Hos.Sm.GetService(out IProgramRegistry registry, "fsp-pr"); + Result rc = fs.Hos.Sm.GetService(out ReferenceCountedDisposable registry, "fsp-pr"); if (rc.IsFailure()) { throw new HorizonResultException(rc, "Failed to get registry service object."); } - registry.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); - return new ReferenceCountedDisposable(registry); + registry.Target.SetCurrentProcess(fs.Hos.Os.GetCurrentProcessId().Value).IgnoreResult(); + return registry; } /// - /// Sets a service object to use for direct function calls + /// Sets an service object to use for direct function calls /// instead of going over IPC. If using a DFC service object, this function should be /// called before calling . /// diff --git a/src/LibHac/FsSrv/FileSystemServer.cs b/src/LibHac/FsSrv/FileSystemServer.cs index 4d650948..c8ee9b00 100644 --- a/src/LibHac/FsSrv/FileSystemServer.cs +++ b/src/LibHac/FsSrv/FileSystemServer.cs @@ -41,9 +41,11 @@ namespace LibHac.FsSrv FileSystemProxyConfiguration fspConfig = InitializeFileSystemProxyConfiguration(config); this.InitializeFileSystemProxy(fspConfig); - FileSystemProxyImpl fsProxy = GetFileSystemProxyServiceObject(); + using ReferenceCountedDisposable fsProxy = Impl.GetFileSystemProxyServiceObject(); ulong processId = Hos.Os.GetCurrentProcessId().Value; - fsProxy.SetCurrentProcess(processId).IgnoreResult(); + fsProxy.Target.SetCurrentProcess(processId).IgnoreResult(); + + Hos.Fs.InitializeDfcFileSystemProxyServiceObject(fsProxy); var saveService = new SaveDataFileSystemService(fspConfig.SaveDataFileSystemService, processId); @@ -164,21 +166,6 @@ namespace LibHac.FsSrv return fspConfig; } - private FileSystemProxyImpl GetFileSystemProxyServiceObject() - { - return new FileSystemProxyImpl(this); - } - - private FileSystemProxyImpl GetFileSystemProxyForLoaderServiceObject() - { - return new FileSystemProxyImpl(this); - } - - private ProgramRegistryImpl GetProgramRegistryServiceObject() - { - return new ProgramRegistryImpl(this); - } - private class FileSystemProxyService : IServiceObject { private readonly FileSystemServer _server; @@ -190,7 +177,7 @@ namespace LibHac.FsSrv public Result GetServiceObject(out object serviceObject) { - serviceObject = _server.GetFileSystemProxyServiceObject(); + serviceObject = _server.Impl.GetFileSystemProxyServiceObject(); return Result.Success; } } @@ -206,7 +193,7 @@ namespace LibHac.FsSrv public Result GetServiceObject(out object serviceObject) { - serviceObject = _server.GetFileSystemProxyForLoaderServiceObject(); + serviceObject = _server.Impl.GetFileSystemProxyForLoaderServiceObject(); return Result.Success; } } @@ -222,7 +209,7 @@ namespace LibHac.FsSrv public Result GetServiceObject(out object serviceObject) { - serviceObject = _server.GetProgramRegistryServiceObject(); + serviceObject = _server.Impl.GetProgramRegistryServiceObject(); return Result.Success; } } diff --git a/src/LibHac/FsSrv/Impl/FileSystemProxyServiceObject.cs b/src/LibHac/FsSrv/Impl/FileSystemProxyServiceObject.cs new file mode 100644 index 00000000..5d9a5bd7 --- /dev/null +++ b/src/LibHac/FsSrv/Impl/FileSystemProxyServiceObject.cs @@ -0,0 +1,88 @@ +using System.Runtime.CompilerServices; +using LibHac.Fs; +using LibHac.FsSrv.Sf; +using LibHac.Ncm; +using LibHac.Sf; + +namespace LibHac.FsSrv.Impl +{ + public static class FileSystemProxyServiceObject + { + public static ReferenceCountedDisposable GetFileSystemProxyServiceObject( + this FileSystemServerImpl fsSrv) + { + return new ReferenceCountedDisposable(new FileSystemProxyImpl(fsSrv.FsSrv)); + } + + public static ReferenceCountedDisposable GetFileSystemProxyForLoaderServiceObject( + this FileSystemServerImpl fsSrv) + { + return new ReferenceCountedDisposable(new FileSystemProxyImpl(fsSrv.FsSrv)); + } + + public static ReferenceCountedDisposable + GetInvalidFileSystemProxyForLoaderServiceObject(this FileSystemServerImpl fsSrv) + { + return new ReferenceCountedDisposable(new InvalidFileSystemProxyImplForLoader()); + } + + public static ReferenceCountedDisposable GetProgramRegistryServiceObject( + this FileSystemServerImpl fsSrv) + { + return new ReferenceCountedDisposable(new ProgramRegistryImpl(fsSrv.FsSrv)); + } + + public static ReferenceCountedDisposable GetInvalidProgramRegistryServiceObject( + this FileSystemServerImpl fsSrv) + { + return new ReferenceCountedDisposable(new InvalidProgramRegistryImpl()); + } + + private class InvalidFileSystemProxyImplForLoader : IFileSystemProxyForLoader + { + public void Dispose() { } + + public Result IsArchivedProgram(out bool isArchived, ulong processId) + { + Unsafe.SkipInit(out isArchived); + + return ResultFs.PortAcceptableCountLimited.Log(); + } + + public Result OpenCodeFileSystem(out ReferenceCountedDisposable fileSystem, + out CodeVerificationData verificationData, in FspPath path, ProgramId programId) + { + fileSystem = default; + Unsafe.SkipInit(out verificationData); + + return ResultFs.PortAcceptableCountLimited.Log(); + } + + public Result SetCurrentProcess(ulong processId) + { + return ResultFs.PortAcceptableCountLimited.Log(); + } + } + + private class InvalidProgramRegistryImpl : IProgramRegistry + { + public void Dispose() { } + + public Result RegisterProgram(ulong processId, ProgramId programId, StorageId storageId, + InBuffer accessControlData, InBuffer accessControlDescriptor) + { + return ResultFs.PortAcceptableCountLimited.Log(); + } + + public Result SetCurrentProcess(ulong processId) + { + return ResultFs.PortAcceptableCountLimited.Log(); + } + + public Result UnregisterProgram(ulong processId) + { + return ResultFs.PortAcceptableCountLimited.Log(); + } + } + } +}