diff options
| author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
|---|---|---|
| committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
| commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
| tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy | |
| parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) | |
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy')
5 files changed, 586 insertions, 0 deletions
diff --git a/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs new file mode 100644 index 00000000..ba924db8 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs @@ -0,0 +1,161 @@ +using LibHac; +using LibHac.Common; +using LibHac.Common.Keys; +using LibHac.Fs; +using LibHac.FsSrv.Impl; +using LibHac.FsSrv.Sf; +using LibHac.FsSystem; +using LibHac.Spl; +using LibHac.Tools.Es; +using LibHac.Tools.Fs; +using LibHac.Tools.FsSystem; +using LibHac.Tools.FsSystem.NcaUtils; +using System; +using System.IO; +using System.Runtime.InteropServices; +using Path = System.IO.Path; + +namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy +{ + static class FileSystemProxyHelper + { + public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem) + { + openedFileSystem = null; + + try + { + LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open); + using SharedRef<LibHac.Fs.Fsa.IFileSystem> nsp = new(new PartitionFileSystem(storage)); + + ImportTitleKeysFromNsp(nsp.Get, context.Device.System.KeySet); + + using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref nsp.Ref, true); + + openedFileSystem = new IFileSystem(ref adapter.Ref); + } + catch (HorizonResultException ex) + { + return (ResultCode)ex.ResultValue.Value; + } + + return ResultCode.Success; + } + + public static ResultCode OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage, out IFileSystem openedFileSystem) + { + openedFileSystem = null; + + try + { + Nca nca = new Nca(context.Device.System.KeySet, ncaStorage); + + if (!nca.SectionExists(NcaSectionType.Data)) + { + return ResultCode.PartitionNotFound; + } + + LibHac.Fs.Fsa.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel); + using var sharedFs = new SharedRef<LibHac.Fs.Fsa.IFileSystem>(fileSystem); + + using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref sharedFs.Ref, true); + + openedFileSystem = new IFileSystem(ref adapter.Ref); + } + catch (HorizonResultException ex) + { + return (ResultCode)ex.ResultValue.Value; + } + + return ResultCode.Success; + } + + public static ResultCode OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath, out IFileSystem openedFileSystem) + { + openedFileSystem = null; + + DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent; + + while (string.IsNullOrWhiteSpace(archivePath.Extension)) + { + archivePath = archivePath.Parent; + } + + if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName)) + { + FileStream pfsFile = new FileStream( + archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar), + FileMode.Open, + FileAccess.Read); + + try + { + PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage()); + + ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet); + + string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\'); + + using var ncaFile = new UniqueRef<LibHac.Fs.Fsa.IFile>(); + + Result result = nsp.OpenFile(ref ncaFile.Ref, filename.ToU8Span(), OpenMode.Read); + if (result.IsFailure()) + { + return (ResultCode)result.Value; + } + + return OpenNcaFs(context, fullPath, ncaFile.Release().AsStorage(), out openedFileSystem); + } + catch (HorizonResultException ex) + { + return (ResultCode)ex.ResultValue.Value; + } + } + + return ResultCode.PathDoesNotExist; + } + + public static void ImportTitleKeysFromNsp(LibHac.Fs.Fsa.IFileSystem nsp, KeySet keySet) + { + foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik")) + { + using var ticketFile = new UniqueRef<LibHac.Fs.Fsa.IFile>(); + + Result result = nsp.OpenFile(ref ticketFile.Ref, ticketEntry.FullPath.ToU8Span(), OpenMode.Read); + + if (result.IsSuccess()) + { + Ticket ticket = new Ticket(ticketFile.Get.AsStream()); + var titleKey = ticket.GetTitleKey(keySet); + + if (titleKey != null) + { + keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(titleKey)); + } + } + } + } + + public static ref readonly FspPath GetFspPath(ServiceCtx context, int index = 0) + { + ulong position = context.Request.PtrBuff[index].Position; + ulong size = context.Request.PtrBuff[index].Size; + + ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size); + ReadOnlySpan<FspPath> fspBuffer = MemoryMarshal.Cast<byte, FspPath>(buffer); + + return ref fspBuffer[0]; + } + + public static ref readonly LibHac.FsSrv.Sf.Path GetSfPath(ServiceCtx context, int index = 0) + { + ulong position = context.Request.PtrBuff[index].Position; + ulong size = context.Request.PtrBuff[index].Size; + + ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size); + ReadOnlySpan<LibHac.FsSrv.Sf.Path> pathBuffer = MemoryMarshal.Cast<byte, LibHac.FsSrv.Sf.Path>(buffer); + + return ref pathBuffer[0]; + } + } +} diff --git a/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs new file mode 100644 index 00000000..b9759449 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs @@ -0,0 +1,52 @@ +using LibHac; +using LibHac.Common; +using LibHac.Sf; + +namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy +{ + class IDirectory : DisposableIpcService + { + private SharedRef<LibHac.FsSrv.Sf.IDirectory> _baseDirectory; + + public IDirectory(ref SharedRef<LibHac.FsSrv.Sf.IDirectory> directory) + { + _baseDirectory = SharedRef<LibHac.FsSrv.Sf.IDirectory>.CreateMove(ref directory); + } + + [CommandCmif(0)] + // Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries) + public ResultCode Read(ServiceCtx context) + { + ulong bufferAddress = context.Request.ReceiveBuff[0].Position; + ulong bufferLen = context.Request.ReceiveBuff[0].Size; + + using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true)) + { + Result result = _baseDirectory.Get.Read(out long entriesRead, new OutBuffer(region.Memory.Span)); + + context.ResponseData.Write(entriesRead); + + return (ResultCode)result.Value; + } + } + + [CommandCmif(1)] + // GetEntryCount() -> u64 + public ResultCode GetEntryCount(ServiceCtx context) + { + Result result = _baseDirectory.Get.GetEntryCount(out long entryCount); + + context.ResponseData.Write(entryCount); + + return (ResultCode)result.Value; + } + + protected override void Dispose(bool isDisposing) + { + if (isDisposing) + { + _baseDirectory.Destroy(); + } + } + } +} diff --git a/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs new file mode 100644 index 00000000..4bc58ae5 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs @@ -0,0 +1,95 @@ +using LibHac; +using LibHac.Common; +using LibHac.Fs; +using LibHac.Sf; +using Ryujinx.Common; + +namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy +{ + class IFile : DisposableIpcService + { + private SharedRef<LibHac.FsSrv.Sf.IFile> _baseFile; + + public IFile(ref SharedRef<LibHac.FsSrv.Sf.IFile> baseFile) + { + _baseFile = SharedRef<LibHac.FsSrv.Sf.IFile>.CreateMove(ref baseFile); + } + + [CommandCmif(0)] + // Read(u32 readOption, u64 offset, u64 size) -> (u64 out_size, buffer<u8, 0x46, 0> out_buf) + public ResultCode Read(ServiceCtx context) + { + ulong bufferAddress = context.Request.ReceiveBuff[0].Position; + ulong bufferLen = context.Request.ReceiveBuff[0].Size; + + ReadOption readOption = context.RequestData.ReadStruct<ReadOption>(); + context.RequestData.BaseStream.Position += 4; + + long offset = context.RequestData.ReadInt64(); + long size = context.RequestData.ReadInt64(); + + using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true)) + { + Result result = _baseFile.Get.Read(out long bytesRead, offset, new OutBuffer(region.Memory.Span), size, readOption); + + context.ResponseData.Write(bytesRead); + + return (ResultCode)result.Value; + } + } + + [CommandCmif(1)] + // Write(u32 writeOption, u64 offset, u64 size, buffer<u8, 0x45, 0>) + public ResultCode Write(ServiceCtx context) + { + ulong position = context.Request.SendBuff[0].Position; + + WriteOption writeOption = context.RequestData.ReadStruct<WriteOption>(); + context.RequestData.BaseStream.Position += 4; + + long offset = context.RequestData.ReadInt64(); + long size = context.RequestData.ReadInt64(); + + byte[] data = new byte[context.Request.SendBuff[0].Size]; + + context.Memory.Read(position, data); + + return (ResultCode)_baseFile.Get.Write(offset, new InBuffer(data), size, writeOption).Value; + } + + [CommandCmif(2)] + // Flush() + public ResultCode Flush(ServiceCtx context) + { + return (ResultCode)_baseFile.Get.Flush().Value; + } + + [CommandCmif(3)] + // SetSize(u64 size) + public ResultCode SetSize(ServiceCtx context) + { + long size = context.RequestData.ReadInt64(); + + return (ResultCode)_baseFile.Get.SetSize(size).Value; + } + + [CommandCmif(4)] + // GetSize() -> u64 fileSize + public ResultCode GetSize(ServiceCtx context) + { + Result result = _baseFile.Get.GetSize(out long size); + + context.ResponseData.Write(size); + + return (ResultCode)result.Value; + } + + protected override void Dispose(bool isDisposing) + { + if (isDisposing) + { + _baseFile.Destroy(); + } + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs new file mode 100644 index 00000000..9effa79d --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs @@ -0,0 +1,213 @@ +using LibHac; +using LibHac.Common; +using LibHac.Fs; +using Path = LibHac.FsSrv.Sf.Path; + +namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy +{ + class IFileSystem : DisposableIpcService + { + private SharedRef<LibHac.FsSrv.Sf.IFileSystem> _fileSystem; + + public IFileSystem(ref SharedRef<LibHac.FsSrv.Sf.IFileSystem> provider) + { + _fileSystem = SharedRef<LibHac.FsSrv.Sf.IFileSystem>.CreateMove(ref provider); + } + + public SharedRef<LibHac.FsSrv.Sf.IFileSystem> GetBaseFileSystem() + { + return SharedRef<LibHac.FsSrv.Sf.IFileSystem>.CreateCopy(in _fileSystem); + } + + [CommandCmif(0)] + // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode CreateFile(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + int createOption = context.RequestData.ReadInt32(); + context.RequestData.BaseStream.Position += 4; + + long size = context.RequestData.ReadInt64(); + + return (ResultCode)_fileSystem.Get.CreateFile(in name, size, createOption).Value; + } + + [CommandCmif(1)] + // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode DeleteFile(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + return (ResultCode)_fileSystem.Get.DeleteFile(in name).Value; + } + + [CommandCmif(2)] + // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode CreateDirectory(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + return (ResultCode)_fileSystem.Get.CreateDirectory(in name).Value; + } + + [CommandCmif(3)] + // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode DeleteDirectory(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + return (ResultCode)_fileSystem.Get.DeleteDirectory(in name).Value; + } + + [CommandCmif(4)] + // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode DeleteDirectoryRecursively(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + return (ResultCode)_fileSystem.Get.DeleteDirectoryRecursively(in name).Value; + } + + [CommandCmif(5)] + // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath) + public ResultCode RenameFile(ServiceCtx context) + { + ref readonly Path currentName = ref FileSystemProxyHelper.GetSfPath(context, index: 0); + ref readonly Path newName = ref FileSystemProxyHelper.GetSfPath(context, index: 1); + + return (ResultCode)_fileSystem.Get.RenameFile(in currentName, in newName).Value; + } + + [CommandCmif(6)] + // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath) + public ResultCode RenameDirectory(ServiceCtx context) + { + ref readonly Path currentName = ref FileSystemProxyHelper.GetSfPath(context, index: 0); + ref readonly Path newName = ref FileSystemProxyHelper.GetSfPath(context, index: 1); + + return (ResultCode)_fileSystem.Get.RenameDirectory(in currentName, in newName).Value; + } + + [CommandCmif(7)] + // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType + public ResultCode GetEntryType(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + Result result = _fileSystem.Get.GetEntryType(out uint entryType, in name); + + context.ResponseData.Write((int)entryType); + + return (ResultCode)result.Value; + } + + [CommandCmif(8)] + // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file + public ResultCode OpenFile(ServiceCtx context) + { + uint mode = context.RequestData.ReadUInt32(); + + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + using var file = new SharedRef<LibHac.FsSrv.Sf.IFile>(); + + Result result = _fileSystem.Get.OpenFile(ref file.Ref, in name, mode); + + if (result.IsSuccess()) + { + IFile fileInterface = new IFile(ref file.Ref); + + MakeObject(context, fileInterface); + } + + return (ResultCode)result.Value; + } + + [CommandCmif(9)] + // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory + public ResultCode OpenDirectory(ServiceCtx context) + { + uint mode = context.RequestData.ReadUInt32(); + + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + using var dir = new SharedRef<LibHac.FsSrv.Sf.IDirectory>(); + + Result result = _fileSystem.Get.OpenDirectory(ref dir.Ref, name, mode); + + if (result.IsSuccess()) + { + IDirectory dirInterface = new IDirectory(ref dir.Ref); + + MakeObject(context, dirInterface); + } + + return (ResultCode)result.Value; + } + + [CommandCmif(10)] + // Commit() + public ResultCode Commit(ServiceCtx context) + { + return (ResultCode)_fileSystem.Get.Commit().Value; + } + + [CommandCmif(11)] + // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace + public ResultCode GetFreeSpaceSize(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + Result result = _fileSystem.Get.GetFreeSpaceSize(out long size, in name); + + context.ResponseData.Write(size); + + return (ResultCode)result.Value; + } + + [CommandCmif(12)] + // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize + public ResultCode GetTotalSpaceSize(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + Result result = _fileSystem.Get.GetTotalSpaceSize(out long size, in name); + + context.ResponseData.Write(size); + + return (ResultCode)result.Value; + } + + [CommandCmif(13)] + // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path) + public ResultCode CleanDirectoryRecursively(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + return (ResultCode)_fileSystem.Get.CleanDirectoryRecursively(in name).Value; + } + + [CommandCmif(14)] + // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp + public ResultCode GetFileTimeStampRaw(ServiceCtx context) + { + ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context); + + Result result = _fileSystem.Get.GetFileTimeStampRaw(out FileTimeStampRaw timestamp, in name); + + context.ResponseData.Write(timestamp.Created); + context.ResponseData.Write(timestamp.Modified); + context.ResponseData.Write(timestamp.Accessed); + context.ResponseData.Write(1L); // Is valid? + + return (ResultCode)result.Value; + } + + protected override void Dispose(bool isDisposing) + { + if (isDisposing) + { + _fileSystem.Destroy(); + } + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs new file mode 100644 index 00000000..54c7b800 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs @@ -0,0 +1,65 @@ +using LibHac; +using LibHac.Common; +using LibHac.Sf; +using Ryujinx.HLE.HOS.Ipc; + +namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy +{ + class IStorage : DisposableIpcService + { + private SharedRef<LibHac.FsSrv.Sf.IStorage> _baseStorage; + + public IStorage(ref SharedRef<LibHac.FsSrv.Sf.IStorage> baseStorage) + { + _baseStorage = SharedRef<LibHac.FsSrv.Sf.IStorage>.CreateMove(ref baseStorage); + } + + [CommandCmif(0)] + // Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer + public ResultCode Read(ServiceCtx context) + { + ulong offset = context.RequestData.ReadUInt64(); + ulong size = context.RequestData.ReadUInt64(); + + if (context.Request.ReceiveBuff.Count > 0) + { + ulong bufferAddress = context.Request.ReceiveBuff[0].Position; + ulong bufferLen = context.Request.ReceiveBuff[0].Size; + + // Use smaller length to avoid overflows. + if (size > bufferLen) + { + size = bufferLen; + } + + using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true)) + { + Result result = _baseStorage.Get.Read((long)offset, new OutBuffer(region.Memory.Span), (long)size); + + return (ResultCode)result.Value; + } + } + + return ResultCode.Success; + } + + [CommandCmif(4)] + // GetSize() -> u64 size + public ResultCode GetSize(ServiceCtx context) + { + Result result = _baseStorage.Get.GetSize(out long size); + + context.ResponseData.Write(size); + + return (ResultCode)result.Value; + } + + protected override void Dispose(bool isDisposing) + { + if (isDisposing) + { + _baseStorage.Destroy(); + } + } + } +}
\ No newline at end of file |
