IFileSystem.cs 8.8 KB

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