IFileSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using Ryujinx.HLE.FileSystem;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using static Ryujinx.HLE.HOS.ErrorCode;
  7. using static Ryujinx.HLE.Utilities.StringUtils;
  8. namespace Ryujinx.HLE.HOS.Services.FspSrv
  9. {
  10. class IFileSystem : IpcService
  11. {
  12. private Dictionary<int, ServiceProcessRequest> _commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  14. private HashSet<string> _openPaths;
  15. private string _path;
  16. private IFileSystemProvider _provider;
  17. public IFileSystem(string path, IFileSystemProvider provider)
  18. {
  19. _commands = new Dictionary<int, ServiceProcessRequest>
  20. {
  21. { 0, CreateFile },
  22. { 1, DeleteFile },
  23. { 2, CreateDirectory },
  24. { 3, DeleteDirectory },
  25. { 4, DeleteDirectoryRecursively },
  26. { 5, RenameFile },
  27. { 6, RenameDirectory },
  28. { 7, GetEntryType },
  29. { 8, OpenFile },
  30. { 9, OpenDirectory },
  31. { 10, Commit },
  32. { 11, GetFreeSpaceSize },
  33. { 12, GetTotalSpaceSize },
  34. { 13, CleanDirectoryRecursively },
  35. { 14, GetFileTimeStampRaw }
  36. };
  37. _openPaths = new HashSet<string>();
  38. _path = path;
  39. _provider = provider;
  40. }
  41. // CreateFile(u32 mode, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
  42. public long CreateFile(ServiceCtx context)
  43. {
  44. string name = ReadUtf8String(context);
  45. long mode = context.RequestData.ReadInt64();
  46. int size = context.RequestData.ReadInt32();
  47. string fileName = _provider.GetFullPath(name);
  48. if (fileName == null)
  49. {
  50. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  51. }
  52. if (_provider.FileExists(fileName))
  53. {
  54. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  55. }
  56. if (IsPathAlreadyInUse(fileName))
  57. {
  58. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  59. }
  60. return _provider.CreateFile(fileName, size);
  61. }
  62. // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
  63. public long DeleteFile(ServiceCtx context)
  64. {
  65. string name = ReadUtf8String(context);
  66. string fileName = _provider.GetFullPath(name);
  67. if (!_provider.FileExists(fileName))
  68. {
  69. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  70. }
  71. if (IsPathAlreadyInUse(fileName))
  72. {
  73. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  74. }
  75. return _provider.DeleteFile(fileName);
  76. }
  77. // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  78. public long CreateDirectory(ServiceCtx context)
  79. {
  80. string name = ReadUtf8String(context);
  81. string dirName = _provider.GetFullPath(name);
  82. if (dirName == null)
  83. {
  84. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  85. }
  86. if (_provider.DirectoryExists(dirName))
  87. {
  88. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  89. }
  90. if (IsPathAlreadyInUse(dirName))
  91. {
  92. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  93. }
  94. _provider.CreateDirectory(dirName);
  95. return 0;
  96. }
  97. // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  98. public long DeleteDirectory(ServiceCtx context)
  99. {
  100. return DeleteDirectory(context, false);
  101. }
  102. // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  103. public long DeleteDirectoryRecursively(ServiceCtx context)
  104. {
  105. return DeleteDirectory(context, true);
  106. }
  107. private long DeleteDirectory(ServiceCtx context, bool recursive)
  108. {
  109. string name = ReadUtf8String(context);
  110. string dirName = _provider.GetFullPath(name);
  111. if (!Directory.Exists(dirName))
  112. {
  113. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  114. }
  115. if (IsPathAlreadyInUse(dirName))
  116. {
  117. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  118. }
  119. _provider.DeleteDirectory(dirName, recursive);
  120. return 0;
  121. }
  122. // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  123. public long RenameFile(ServiceCtx context)
  124. {
  125. string oldName = ReadUtf8String(context, 0);
  126. string newName = ReadUtf8String(context, 1);
  127. string oldFileName = _provider.GetFullPath(oldName);
  128. string newFileName = _provider.GetFullPath(newName);
  129. if (_provider.FileExists(oldFileName))
  130. {
  131. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  132. }
  133. if (_provider.FileExists(newFileName))
  134. {
  135. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  136. }
  137. if (IsPathAlreadyInUse(oldFileName))
  138. {
  139. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  140. }
  141. return _provider.RenameFile(oldFileName, newFileName);
  142. }
  143. // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  144. public long RenameDirectory(ServiceCtx context)
  145. {
  146. string oldName = ReadUtf8String(context, 0);
  147. string newName = ReadUtf8String(context, 1);
  148. string oldDirName = _provider.GetFullPath(oldName);
  149. string newDirName = _provider.GetFullPath(newName);
  150. if (!_provider.DirectoryExists(oldDirName))
  151. {
  152. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  153. }
  154. if (!_provider.DirectoryExists(newDirName))
  155. {
  156. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  157. }
  158. if (IsPathAlreadyInUse(oldDirName))
  159. {
  160. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  161. }
  162. return _provider.RenameDirectory(oldDirName, newDirName);
  163. }
  164. // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
  165. public long GetEntryType(ServiceCtx context)
  166. {
  167. string name = ReadUtf8String(context);
  168. string fileName = _provider.GetFullPath(name);
  169. if (_provider.FileExists(fileName))
  170. {
  171. context.ResponseData.Write(1);
  172. }
  173. else if (_provider.DirectoryExists(fileName))
  174. {
  175. context.ResponseData.Write(0);
  176. }
  177. else
  178. {
  179. context.ResponseData.Write(0);
  180. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  181. }
  182. return 0;
  183. }
  184. // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
  185. public long OpenFile(ServiceCtx context)
  186. {
  187. int filterFlags = context.RequestData.ReadInt32();
  188. string name = ReadUtf8String(context);
  189. string fileName = _provider.GetFullPath(name);
  190. if (!_provider.FileExists(fileName))
  191. {
  192. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  193. }
  194. if (IsPathAlreadyInUse(fileName))
  195. {
  196. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  197. }
  198. long error = _provider.OpenFile(fileName, out IFile fileInterface);
  199. if (error == 0)
  200. {
  201. fileInterface.Disposed += RemoveFileInUse;
  202. lock (_openPaths)
  203. {
  204. _openPaths.Add(fileName);
  205. }
  206. MakeObject(context, fileInterface);
  207. return 0;
  208. }
  209. return error;
  210. }
  211. // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
  212. public long OpenDirectory(ServiceCtx context)
  213. {
  214. int filterFlags = context.RequestData.ReadInt32();
  215. string name = ReadUtf8String(context);
  216. string dirName = _provider.GetFullPath(name);
  217. if (!_provider.DirectoryExists(dirName))
  218. {
  219. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  220. }
  221. if (IsPathAlreadyInUse(dirName))
  222. {
  223. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  224. }
  225. long error = _provider.OpenDirectory(dirName, filterFlags, out IDirectory dirInterface);
  226. if (error == 0)
  227. {
  228. dirInterface.Disposed += RemoveDirectoryInUse;
  229. lock (_openPaths)
  230. {
  231. _openPaths.Add(dirName);
  232. }
  233. MakeObject(context, dirInterface);
  234. }
  235. return error;
  236. }
  237. // Commit()
  238. public long Commit(ServiceCtx context)
  239. {
  240. return 0;
  241. }
  242. // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
  243. public long GetFreeSpaceSize(ServiceCtx context)
  244. {
  245. string name = ReadUtf8String(context);
  246. context.ResponseData.Write(_provider.GetFreeSpace(context));
  247. return 0;
  248. }
  249. // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
  250. public long GetTotalSpaceSize(ServiceCtx context)
  251. {
  252. string name = ReadUtf8String(context);
  253. context.ResponseData.Write(_provider.GetFreeSpace(context));
  254. return 0;
  255. }
  256. // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  257. public long CleanDirectoryRecursively(ServiceCtx context)
  258. {
  259. string name = ReadUtf8String(context);
  260. string dirName = _provider.GetFullPath(name);
  261. if (!_provider.DirectoryExists(dirName))
  262. {
  263. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  264. }
  265. if (IsPathAlreadyInUse(dirName))
  266. {
  267. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  268. }
  269. foreach (DirectoryEntry entry in _provider.GetEntries(dirName))
  270. {
  271. if (_provider.DirectoryExists(entry.Path))
  272. {
  273. _provider.DeleteDirectory(entry.Path, true);
  274. }
  275. else if (_provider.FileExists(entry.Path))
  276. {
  277. _provider.DeleteFile(entry.Path);
  278. }
  279. }
  280. return 0;
  281. }
  282. // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
  283. public long GetFileTimeStampRaw(ServiceCtx context)
  284. {
  285. string name = ReadUtf8String(context);
  286. string path = _provider.GetFullPath(name);
  287. if (_provider.FileExists(path) || _provider.DirectoryExists(path))
  288. {
  289. FileTimestamp timestamp = _provider.GetFileTimeStampRaw(path);
  290. context.ResponseData.Write(new DateTimeOffset(timestamp.CreationDateTime).ToUnixTimeSeconds());
  291. context.ResponseData.Write(new DateTimeOffset(timestamp.ModifiedDateTime).ToUnixTimeSeconds());
  292. context.ResponseData.Write(new DateTimeOffset(timestamp.LastAccessDateTime).ToUnixTimeSeconds());
  293. byte[] data = new byte[8];
  294. // is valid?
  295. data[0] = 1;
  296. context.ResponseData.Write(data);
  297. return 0;
  298. }
  299. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  300. }
  301. private bool IsPathAlreadyInUse(string path)
  302. {
  303. lock (_openPaths)
  304. {
  305. return _openPaths.Contains(path);
  306. }
  307. }
  308. private void RemoveFileInUse(object sender, EventArgs e)
  309. {
  310. IFile fileInterface = (IFile)sender;
  311. lock (_openPaths)
  312. {
  313. fileInterface.Disposed -= RemoveFileInUse;
  314. _openPaths.Remove(fileInterface.HostPath);
  315. }
  316. }
  317. private void RemoveDirectoryInUse(object sender, EventArgs e)
  318. {
  319. IDirectory dirInterface = (IDirectory)sender;
  320. lock (_openPaths)
  321. {
  322. dirInterface.Disposed -= RemoveDirectoryInUse;
  323. _openPaths.Remove(dirInterface.DirectoryPath);
  324. }
  325. }
  326. }
  327. }