IFileSystem.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using LibHac;
  2. using LibHac.Fs;
  3. using static Ryujinx.HLE.Utilities.StringUtils;
  4. namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
  5. {
  6. class IFileSystem : IpcService
  7. {
  8. private LibHac.Fs.IFileSystem _fileSystem;
  9. public IFileSystem(LibHac.Fs.IFileSystem provider)
  10. {
  11. _fileSystem = provider;
  12. }
  13. [Command(0)]
  14. // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
  15. public ResultCode CreateFile(ServiceCtx context)
  16. {
  17. string name = ReadUtf8String(context);
  18. CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
  19. context.RequestData.BaseStream.Position += 4;
  20. long size = context.RequestData.ReadInt64();
  21. try
  22. {
  23. _fileSystem.CreateFile(name, size, createOption);
  24. }
  25. catch (HorizonResultException ex)
  26. {
  27. return (ResultCode)ex.ResultValue.Value;
  28. }
  29. return ResultCode.Success;
  30. }
  31. [Command(1)]
  32. // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
  33. public ResultCode DeleteFile(ServiceCtx context)
  34. {
  35. string name = ReadUtf8String(context);
  36. try
  37. {
  38. _fileSystem.DeleteFile(name);
  39. }
  40. catch (HorizonResultException ex)
  41. {
  42. return (ResultCode)ex.ResultValue.Value;
  43. }
  44. return ResultCode.Success;
  45. }
  46. [Command(2)]
  47. // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  48. public ResultCode CreateDirectory(ServiceCtx context)
  49. {
  50. string name = ReadUtf8String(context);
  51. try
  52. {
  53. _fileSystem.CreateDirectory(name);
  54. }
  55. catch (HorizonResultException ex)
  56. {
  57. return (ResultCode)ex.ResultValue.Value;
  58. }
  59. return ResultCode.Success;
  60. }
  61. [Command(3)]
  62. // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  63. public ResultCode DeleteDirectory(ServiceCtx context)
  64. {
  65. string name = ReadUtf8String(context);
  66. try
  67. {
  68. _fileSystem.DeleteDirectory(name);
  69. }
  70. catch (HorizonResultException ex)
  71. {
  72. return (ResultCode)ex.ResultValue.Value;
  73. }
  74. return ResultCode.Success;
  75. }
  76. [Command(4)]
  77. // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  78. public ResultCode DeleteDirectoryRecursively(ServiceCtx context)
  79. {
  80. string name = ReadUtf8String(context);
  81. try
  82. {
  83. _fileSystem.DeleteDirectoryRecursively(name);
  84. }
  85. catch (HorizonResultException ex)
  86. {
  87. return (ResultCode)ex.ResultValue.Value;
  88. }
  89. return ResultCode.Success;
  90. }
  91. [Command(5)]
  92. // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  93. public ResultCode RenameFile(ServiceCtx context)
  94. {
  95. string oldName = ReadUtf8String(context, 0);
  96. string newName = ReadUtf8String(context, 1);
  97. try
  98. {
  99. _fileSystem.RenameFile(oldName, newName);
  100. }
  101. catch (HorizonResultException ex)
  102. {
  103. return (ResultCode)ex.ResultValue.Value;
  104. }
  105. return ResultCode.Success;
  106. }
  107. [Command(6)]
  108. // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  109. public ResultCode RenameDirectory(ServiceCtx context)
  110. {
  111. string oldName = ReadUtf8String(context, 0);
  112. string newName = ReadUtf8String(context, 1);
  113. try
  114. {
  115. _fileSystem.RenameDirectory(oldName, newName);
  116. }
  117. catch (HorizonResultException ex)
  118. {
  119. return (ResultCode)ex.ResultValue.Value;
  120. }
  121. return ResultCode.Success;
  122. }
  123. [Command(7)]
  124. // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
  125. public ResultCode GetEntryType(ServiceCtx context)
  126. {
  127. string name = ReadUtf8String(context);
  128. try
  129. {
  130. DirectoryEntryType entryType = _fileSystem.GetEntryType(name);
  131. if (entryType == DirectoryEntryType.Directory || entryType == DirectoryEntryType.File)
  132. {
  133. context.ResponseData.Write((int)entryType);
  134. }
  135. else
  136. {
  137. return ResultCode.PathDoesNotExist;
  138. }
  139. }
  140. catch (HorizonResultException ex)
  141. {
  142. return (ResultCode)ex.ResultValue.Value;
  143. }
  144. return ResultCode.Success;
  145. }
  146. [Command(8)]
  147. // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
  148. public ResultCode OpenFile(ServiceCtx context)
  149. {
  150. OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
  151. string name = ReadUtf8String(context);
  152. try
  153. {
  154. LibHac.Fs.IFile file = _fileSystem.OpenFile(name, mode);
  155. IFile fileInterface = new IFile(file);
  156. MakeObject(context, fileInterface);
  157. }
  158. catch (HorizonResultException ex)
  159. {
  160. return (ResultCode)ex.ResultValue.Value;
  161. }
  162. return ResultCode.Success;
  163. }
  164. [Command(9)]
  165. // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
  166. public ResultCode OpenDirectory(ServiceCtx context)
  167. {
  168. OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
  169. string name = ReadUtf8String(context);
  170. try
  171. {
  172. LibHac.Fs.IDirectory dir = _fileSystem.OpenDirectory(name, mode);
  173. IDirectory dirInterface = new IDirectory(dir);
  174. MakeObject(context, dirInterface);
  175. }
  176. catch (HorizonResultException ex)
  177. {
  178. return (ResultCode)ex.ResultValue.Value;
  179. }
  180. return ResultCode.Success;
  181. }
  182. [Command(10)]
  183. // Commit()
  184. public ResultCode Commit(ServiceCtx context)
  185. {
  186. try
  187. {
  188. _fileSystem.Commit();
  189. }
  190. catch (HorizonResultException ex)
  191. {
  192. return (ResultCode)ex.ResultValue.Value;
  193. }
  194. return ResultCode.Success;
  195. }
  196. [Command(11)]
  197. // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
  198. public ResultCode GetFreeSpaceSize(ServiceCtx context)
  199. {
  200. string name = ReadUtf8String(context);
  201. try
  202. {
  203. context.ResponseData.Write(_fileSystem.GetFreeSpaceSize(name));
  204. }
  205. catch (HorizonResultException ex)
  206. {
  207. return (ResultCode)ex.ResultValue.Value;
  208. }
  209. return ResultCode.Success;
  210. }
  211. [Command(12)]
  212. // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
  213. public ResultCode GetTotalSpaceSize(ServiceCtx context)
  214. {
  215. string name = ReadUtf8String(context);
  216. try
  217. {
  218. context.ResponseData.Write(_fileSystem.GetTotalSpaceSize(name));
  219. }
  220. catch (HorizonResultException ex)
  221. {
  222. return (ResultCode)ex.ResultValue.Value;
  223. }
  224. return ResultCode.Success;
  225. }
  226. [Command(13)]
  227. // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  228. public ResultCode CleanDirectoryRecursively(ServiceCtx context)
  229. {
  230. string name = ReadUtf8String(context);
  231. try
  232. {
  233. _fileSystem.CleanDirectoryRecursively(name);
  234. }
  235. catch (HorizonResultException ex)
  236. {
  237. return (ResultCode)ex.ResultValue.Value;
  238. }
  239. return ResultCode.Success;
  240. }
  241. [Command(14)]
  242. // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
  243. public ResultCode GetFileTimeStampRaw(ServiceCtx context)
  244. {
  245. string name = ReadUtf8String(context);
  246. try
  247. {
  248. FileTimeStampRaw timestamp = _fileSystem.GetFileTimeStampRaw(name);
  249. context.ResponseData.Write(timestamp.Created);
  250. context.ResponseData.Write(timestamp.Modified);
  251. context.ResponseData.Write(timestamp.Accessed);
  252. byte[] data = new byte[8];
  253. // is valid?
  254. data[0] = 1;
  255. context.ResponseData.Write(data);
  256. }
  257. catch (HorizonResultException ex)
  258. {
  259. return (ResultCode)ex.ResultValue.Value;
  260. }
  261. return ResultCode.Success;
  262. }
  263. }
  264. }