IFileSystem.cs 9.6 KB

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