From c29fed67c18829cb28caad82c806462777603020 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 4 Nov 2020 14:33:10 -0700 Subject: [PATCH] Add TimeService --- .../FsSrv/FileSystemProxyConfiguration.cs | 1 + src/LibHac/FsSrv/FileSystemProxyImpl.cs | 19 +++-- src/LibHac/FsSrv/FileSystemServer.cs | 6 ++ src/LibHac/FsSrv/IFileSystemProxy.cs | 2 +- src/LibHac/FsSrv/TimeService.cs | 77 +++++++++++++++++++ 5 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/LibHac/FsSrv/TimeService.cs diff --git a/src/LibHac/FsSrv/FileSystemProxyConfiguration.cs b/src/LibHac/FsSrv/FileSystemProxyConfiguration.cs index d203475c..73dac42c 100644 --- a/src/LibHac/FsSrv/FileSystemProxyConfiguration.cs +++ b/src/LibHac/FsSrv/FileSystemProxyConfiguration.cs @@ -8,6 +8,7 @@ namespace LibHac.FsSrv public BaseFileSystemServiceImpl BaseFileSystemService { get; set; } public NcaFileSystemServiceImpl NcaFileSystemService { get; set; } public SaveDataFileSystemServiceImpl SaveDataFileSystemService { get; set; } + public TimeServiceImpl TimeService { get; set; } public ProgramRegistryServiceImpl ProgramRegistryService { get; set; } public AccessLogServiceImpl AccessLogService { get; set; } } diff --git a/src/LibHac/FsSrv/FileSystemProxyImpl.cs b/src/LibHac/FsSrv/FileSystemProxyImpl.cs index a81df2ca..2dfe1b74 100644 --- a/src/LibHac/FsSrv/FileSystemProxyImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyImpl.cs @@ -25,11 +25,6 @@ namespace LibHac.FsSrv CurrentProcess = ulong.MaxValue; } - private ProgramIndexRegistryService GetProgramIndexRegistryService() - { - return new ProgramIndexRegistryService(FsProxyCore.Config.ProgramRegistryService, CurrentProcess); - } - public Result OpenFileSystemWithId(out ReferenceCountedDisposable fileSystem, in FspPath path, ulong id, FileSystemProxyType fsType) { @@ -746,9 +741,9 @@ namespace LibHac.FsSrv return saveFsService.QuerySaveDataTotalSize(out totalSize, dataSize, journalSize); } - public Result SetCurrentPosixTimeWithTimeDifference(long time, int difference) + public Result SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference) { - throw new NotImplementedException(); + return GetTimeService().SetCurrentPosixTimeWithTimeDifference(currentTime, timeDifference); } public Result GetRightsId(out RightsId rightsId, ProgramId programId, StorageId storageId) @@ -1005,6 +1000,16 @@ namespace LibHac.FsSrv return Result.Success; } + private TimeService GetTimeService() + { + return new TimeService(FsProxyCore.Config.TimeService, CurrentProcess); + } + + private ProgramIndexRegistryService GetProgramIndexRegistryService() + { + return new ProgramIndexRegistryService(FsProxyCore.Config.ProgramRegistryService, CurrentProcess); + } + private AccessLogService GetAccessLogService() { return new AccessLogService(FsProxyCore.Config.AccessLogService, CurrentProcess); diff --git a/src/LibHac/FsSrv/FileSystemServer.cs b/src/LibHac/FsSrv/FileSystemServer.cs index 6bf841ea..8bab0b16 100644 --- a/src/LibHac/FsSrv/FileSystemServer.cs +++ b/src/LibHac/FsSrv/FileSystemServer.cs @@ -96,6 +96,11 @@ namespace LibHac.FsSrv var programRegistryService = new ProgramRegistryServiceImpl(this); var programRegistry = new ProgramRegistryImpl(programRegistryService); + var timeServiceConfig = new TimeServiceImpl.Configuration(); + timeServiceConfig.HorizonClient = Hos; + timeServiceConfig.ProgramRegistry = programRegistry; + var timeService = new TimeServiceImpl(in timeServiceConfig); + var baseFsServiceConfig = new BaseFileSystemServiceImpl.Configuration(); baseFsServiceConfig.BisFileSystemCreator = config.FsCreators.BuiltInStorageFileSystemCreator; baseFsServiceConfig.GameCardFileSystemCreator = config.FsCreators.GameCardFileSystemCreator; @@ -148,6 +153,7 @@ namespace LibHac.FsSrv BaseFileSystemService = baseFsService, NcaFileSystemService = ncaFsService, SaveDataFileSystemService = saveFsService, + TimeService = timeService, ProgramRegistryService = programRegistryService, AccessLogService = accessLogService }; diff --git a/src/LibHac/FsSrv/IFileSystemProxy.cs b/src/LibHac/FsSrv/IFileSystemProxy.cs index 56b72f81..935eb73d 100644 --- a/src/LibHac/FsSrv/IFileSystemProxy.cs +++ b/src/LibHac/FsSrv/IFileSystemProxy.cs @@ -86,7 +86,7 @@ namespace LibHac.FsSrv Result UnregisterAllExternalKey(); Result GetRightsIdByPath(out RightsId rightsId, in FspPath path); Result GetRightsIdAndKeyGenerationByPath(out RightsId rightsId, out byte keyGeneration, in FspPath path); - Result SetCurrentPosixTimeWithTimeDifference(long time, int difference); + Result SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference); Result GetFreeSpaceSizeForSaveData(out long freeSpaceSize, SaveDataSpaceId spaceId); Result VerifySaveDataFileSystemBySaveDataSpaceId(SaveDataSpaceId spaceId, ulong saveDataId, OutBuffer readBuffer); Result CorruptSaveDataFileSystemBySaveDataSpaceId(SaveDataSpaceId spaceId, ulong saveDataId); diff --git a/src/LibHac/FsSrv/TimeService.cs b/src/LibHac/FsSrv/TimeService.cs new file mode 100644 index 00000000..4776b25e --- /dev/null +++ b/src/LibHac/FsSrv/TimeService.cs @@ -0,0 +1,77 @@ +using System; +using LibHac.Fs; +using LibHac.FsSrv.Impl; + +namespace LibHac.FsSrv +{ + public readonly struct TimeService + { + private readonly TimeServiceImpl _serviceImpl; + private readonly ulong _processId; + + public TimeService(TimeServiceImpl serviceImpl, ulong processId) + { + _serviceImpl = serviceImpl; + _processId = processId; + } + + public Result SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference) + { + Result rc = GetProgramInfo(out ProgramInfo programInfo); + if (rc.IsFailure()) return rc; + + if (!programInfo.AccessControl.CanCall(OperationType.SetCurrentPosixTime)) + return ResultFs.PermissionDenied.Log(); + + return _serviceImpl.SetCurrentPosixTimeWithTimeDifference(currentTime, timeDifference); + } + + private Result GetProgramInfo(out ProgramInfo programInfo) + { + return _serviceImpl.GetProgramInfo(out programInfo, _processId); + } + } + + public class TimeServiceImpl + { + private Configuration _config; + private long _baseTime; + private int _timeDifference; + private object _lockObject; + + public TimeServiceImpl(in Configuration configuration) + { + _config = configuration; + _baseTime = 0; + _timeDifference = 0; + _lockObject = new object(); + } + + // The entire Configuration struct is a LibHac addition to avoid using global state + public struct Configuration + { + public HorizonClient HorizonClient; + public ProgramRegistryImpl ProgramRegistry; + } + + public Result GetCurrentPosixTime(out long time) + { + throw new NotImplementedException(); + } + + public Result GetCurrentPosixTimeWithTimeDifference(out long currentTime, out int timeDifference) + { + throw new NotImplementedException(); + } + + public Result SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference) + { + throw new NotImplementedException(); + } + + internal Result GetProgramInfo(out ProgramInfo programInfo, ulong processId) + { + return _config.ProgramRegistry.GetProgramInfo(out programInfo, processId); + } + } +}