IFileSystem.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using Path = LibHac.FsSrv.Sf.Path;
  5. namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
  6. {
  7. class IFileSystem : DisposableIpcService
  8. {
  9. private SharedRef<LibHac.FsSrv.Sf.IFileSystem> _fileSystem;
  10. public IFileSystem(ref SharedRef<LibHac.FsSrv.Sf.IFileSystem> provider)
  11. {
  12. _fileSystem = SharedRef<LibHac.FsSrv.Sf.IFileSystem>.CreateMove(ref provider);
  13. }
  14. public SharedRef<LibHac.FsSrv.Sf.IFileSystem> GetBaseFileSystem()
  15. {
  16. return SharedRef<LibHac.FsSrv.Sf.IFileSystem>.CreateCopy(in _fileSystem);
  17. }
  18. [CommandHipc(0)]
  19. // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
  20. public ResultCode CreateFile(ServiceCtx context)
  21. {
  22. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  23. int createOption = context.RequestData.ReadInt32();
  24. context.RequestData.BaseStream.Position += 4;
  25. long size = context.RequestData.ReadInt64();
  26. return (ResultCode)_fileSystem.Get.CreateFile(in name, size, createOption).Value;
  27. }
  28. [CommandHipc(1)]
  29. // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
  30. public ResultCode DeleteFile(ServiceCtx context)
  31. {
  32. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  33. return (ResultCode)_fileSystem.Get.DeleteFile(in name).Value;
  34. }
  35. [CommandHipc(2)]
  36. // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  37. public ResultCode CreateDirectory(ServiceCtx context)
  38. {
  39. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  40. return (ResultCode)_fileSystem.Get.CreateDirectory(in name).Value;
  41. }
  42. [CommandHipc(3)]
  43. // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  44. public ResultCode DeleteDirectory(ServiceCtx context)
  45. {
  46. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  47. return (ResultCode)_fileSystem.Get.DeleteDirectory(in name).Value;
  48. }
  49. [CommandHipc(4)]
  50. // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  51. public ResultCode DeleteDirectoryRecursively(ServiceCtx context)
  52. {
  53. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  54. return (ResultCode)_fileSystem.Get.DeleteDirectoryRecursively(in name).Value;
  55. }
  56. [CommandHipc(5)]
  57. // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  58. public ResultCode RenameFile(ServiceCtx context)
  59. {
  60. ref readonly Path currentName = ref FileSystemProxyHelper.GetSfPath(context, index: 0);
  61. ref readonly Path newName = ref FileSystemProxyHelper.GetSfPath(context, index: 1);
  62. return (ResultCode)_fileSystem.Get.RenameFile(in currentName, in newName).Value;
  63. }
  64. [CommandHipc(6)]
  65. // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  66. public ResultCode RenameDirectory(ServiceCtx context)
  67. {
  68. ref readonly Path currentName = ref FileSystemProxyHelper.GetSfPath(context, index: 0);
  69. ref readonly Path newName = ref FileSystemProxyHelper.GetSfPath(context, index: 1);
  70. return (ResultCode)_fileSystem.Get.RenameDirectory(in currentName, in newName).Value;
  71. }
  72. [CommandHipc(7)]
  73. // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
  74. public ResultCode GetEntryType(ServiceCtx context)
  75. {
  76. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  77. Result result = _fileSystem.Get.GetEntryType(out uint entryType, in name);
  78. context.ResponseData.Write((int)entryType);
  79. return (ResultCode)result.Value;
  80. }
  81. [CommandHipc(8)]
  82. // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
  83. public ResultCode OpenFile(ServiceCtx context)
  84. {
  85. uint mode = context.RequestData.ReadUInt32();
  86. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  87. using var file = new SharedRef<LibHac.FsSrv.Sf.IFile>();
  88. Result result = _fileSystem.Get.OpenFile(ref file.Ref(), in name, mode);
  89. if (result.IsSuccess())
  90. {
  91. IFile fileInterface = new IFile(ref file.Ref());
  92. MakeObject(context, fileInterface);
  93. }
  94. return (ResultCode)result.Value;
  95. }
  96. [CommandHipc(9)]
  97. // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
  98. public ResultCode OpenDirectory(ServiceCtx context)
  99. {
  100. uint mode = context.RequestData.ReadUInt32();
  101. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  102. using var dir = new SharedRef<LibHac.FsSrv.Sf.IDirectory>();
  103. Result result = _fileSystem.Get.OpenDirectory(ref dir.Ref(), name, mode);
  104. if (result.IsSuccess())
  105. {
  106. IDirectory dirInterface = new IDirectory(ref dir.Ref());
  107. MakeObject(context, dirInterface);
  108. }
  109. return (ResultCode)result.Value;
  110. }
  111. [CommandHipc(10)]
  112. // Commit()
  113. public ResultCode Commit(ServiceCtx context)
  114. {
  115. return (ResultCode)_fileSystem.Get.Commit().Value;
  116. }
  117. [CommandHipc(11)]
  118. // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
  119. public ResultCode GetFreeSpaceSize(ServiceCtx context)
  120. {
  121. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  122. Result result = _fileSystem.Get.GetFreeSpaceSize(out long size, in name);
  123. context.ResponseData.Write(size);
  124. return (ResultCode)result.Value;
  125. }
  126. [CommandHipc(12)]
  127. // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
  128. public ResultCode GetTotalSpaceSize(ServiceCtx context)
  129. {
  130. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  131. Result result = _fileSystem.Get.GetTotalSpaceSize(out long size, in name);
  132. context.ResponseData.Write(size);
  133. return (ResultCode)result.Value;
  134. }
  135. [CommandHipc(13)]
  136. // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  137. public ResultCode CleanDirectoryRecursively(ServiceCtx context)
  138. {
  139. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  140. return (ResultCode)_fileSystem.Get.CleanDirectoryRecursively(in name).Value;
  141. }
  142. [CommandHipc(14)]
  143. // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
  144. public ResultCode GetFileTimeStampRaw(ServiceCtx context)
  145. {
  146. ref readonly Path name = ref FileSystemProxyHelper.GetSfPath(context);
  147. Result result = _fileSystem.Get.GetFileTimeStampRaw(out FileTimeStampRaw timestamp, in name);
  148. context.ResponseData.Write(timestamp.Created);
  149. context.ResponseData.Write(timestamp.Modified);
  150. context.ResponseData.Write(timestamp.Accessed);
  151. byte[] data = new byte[8];
  152. // is valid?
  153. data[0] = 1;
  154. context.ResponseData.Write(data);
  155. return (ResultCode)result.Value;
  156. }
  157. protected override void Dispose(bool isDisposing)
  158. {
  159. if (isDisposing)
  160. {
  161. _fileSystem.Destroy();
  162. }
  163. }
  164. }
  165. }