IFileSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using LibHac.Fs;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Ryujinx.Common.Logging;
  7. using static Ryujinx.HLE.HOS.ErrorCode;
  8. using static Ryujinx.HLE.Utilities.StringUtils;
  9. namespace Ryujinx.HLE.HOS.Services.FspSrv
  10. {
  11. class IFileSystem : IpcService
  12. {
  13. private Dictionary<int, ServiceProcessRequest> _commands;
  14. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  15. private HashSet<string> _openPaths;
  16. private LibHac.Fs.IFileSystem _provider;
  17. public IFileSystem(LibHac.Fs.IFileSystem 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. _provider = provider;
  39. }
  40. // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
  41. public long CreateFile(ServiceCtx context)
  42. {
  43. string name = ReadUtf8String(context);
  44. CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
  45. context.RequestData.BaseStream.Position += 4;
  46. long size = context.RequestData.ReadInt64();
  47. if (name == null)
  48. {
  49. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  50. }
  51. if (_provider.FileExists(name))
  52. {
  53. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  54. }
  55. if (IsPathAlreadyInUse(name))
  56. {
  57. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  58. }
  59. try
  60. {
  61. _provider.CreateFile(name, size, createOption);
  62. }
  63. catch (DirectoryNotFoundException)
  64. {
  65. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  66. }
  67. catch (UnauthorizedAccessException)
  68. {
  69. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  70. throw;
  71. }
  72. return 0;
  73. }
  74. // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
  75. public long DeleteFile(ServiceCtx context)
  76. {
  77. string name = ReadUtf8String(context);
  78. if (!_provider.FileExists(name))
  79. {
  80. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  81. }
  82. if (IsPathAlreadyInUse(name))
  83. {
  84. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  85. }
  86. try
  87. {
  88. _provider.DeleteFile(name);
  89. }
  90. catch (FileNotFoundException)
  91. {
  92. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  93. }
  94. catch (UnauthorizedAccessException)
  95. {
  96. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  97. throw;
  98. }
  99. return 0;
  100. }
  101. // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  102. public long CreateDirectory(ServiceCtx context)
  103. {
  104. string name = ReadUtf8String(context);
  105. if (name == null)
  106. {
  107. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  108. }
  109. if (_provider.DirectoryExists(name))
  110. {
  111. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  112. }
  113. if (IsPathAlreadyInUse(name))
  114. {
  115. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  116. }
  117. try
  118. {
  119. _provider.CreateDirectory(name);
  120. }
  121. catch (DirectoryNotFoundException)
  122. {
  123. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  124. }
  125. catch (UnauthorizedAccessException)
  126. {
  127. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  128. throw;
  129. }
  130. return 0;
  131. }
  132. // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
  133. public long DeleteDirectory(ServiceCtx context)
  134. {
  135. string name = ReadUtf8String(context);
  136. if (!_provider.DirectoryExists(name))
  137. {
  138. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  139. }
  140. if (IsPathAlreadyInUse(name))
  141. {
  142. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  143. }
  144. try
  145. {
  146. _provider.DeleteDirectory(name);
  147. }
  148. catch (DirectoryNotFoundException)
  149. {
  150. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  151. }
  152. catch (UnauthorizedAccessException)
  153. {
  154. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  155. throw;
  156. }
  157. return 0;
  158. }
  159. // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  160. public long DeleteDirectoryRecursively(ServiceCtx context)
  161. {
  162. string name = ReadUtf8String(context);
  163. if (!_provider.DirectoryExists(name))
  164. {
  165. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  166. }
  167. if (IsPathAlreadyInUse(name))
  168. {
  169. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  170. }
  171. try
  172. {
  173. _provider.DeleteDirectoryRecursively(name);
  174. }
  175. catch (UnauthorizedAccessException)
  176. {
  177. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  178. throw;
  179. }
  180. return 0;
  181. }
  182. // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  183. public long RenameFile(ServiceCtx context)
  184. {
  185. string oldName = ReadUtf8String(context, 0);
  186. string newName = ReadUtf8String(context, 1);
  187. if (_provider.FileExists(oldName))
  188. {
  189. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  190. }
  191. if (_provider.FileExists(newName))
  192. {
  193. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  194. }
  195. if (IsPathAlreadyInUse(oldName))
  196. {
  197. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  198. }
  199. try
  200. {
  201. _provider.RenameFile(oldName, newName);
  202. }
  203. catch (FileNotFoundException)
  204. {
  205. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  206. }
  207. catch (UnauthorizedAccessException)
  208. {
  209. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {oldName} or {newName}");
  210. throw;
  211. }
  212. return 0;
  213. }
  214. // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
  215. public long RenameDirectory(ServiceCtx context)
  216. {
  217. string oldName = ReadUtf8String(context, 0);
  218. string newName = ReadUtf8String(context, 1);
  219. if (!_provider.DirectoryExists(oldName))
  220. {
  221. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  222. }
  223. if (!_provider.DirectoryExists(newName))
  224. {
  225. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  226. }
  227. if (IsPathAlreadyInUse(oldName))
  228. {
  229. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  230. }
  231. try
  232. {
  233. _provider.RenameFile(oldName, newName);
  234. }
  235. catch (DirectoryNotFoundException)
  236. {
  237. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  238. }
  239. catch (UnauthorizedAccessException)
  240. {
  241. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {oldName} or {newName}");
  242. throw;
  243. }
  244. return 0;
  245. }
  246. // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
  247. public long GetEntryType(ServiceCtx context)
  248. {
  249. string name = ReadUtf8String(context);
  250. try
  251. {
  252. LibHac.Fs.DirectoryEntryType entryType = _provider.GetEntryType(name);
  253. context.ResponseData.Write((int)entryType);
  254. }
  255. catch (FileNotFoundException)
  256. {
  257. context.ResponseData.Write(0);
  258. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  259. }
  260. return 0;
  261. }
  262. // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
  263. public long OpenFile(ServiceCtx context)
  264. {
  265. OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
  266. string name = ReadUtf8String(context);
  267. if (!_provider.FileExists(name))
  268. {
  269. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  270. }
  271. if (IsPathAlreadyInUse(name))
  272. {
  273. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  274. }
  275. IFile fileInterface;
  276. try
  277. {
  278. LibHac.Fs.IFile file = _provider.OpenFile(name, mode);
  279. fileInterface = new IFile(file, name);
  280. }
  281. catch (UnauthorizedAccessException)
  282. {
  283. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  284. throw;
  285. }
  286. fileInterface.Disposed += RemoveFileInUse;
  287. lock (_openPaths)
  288. {
  289. _openPaths.Add(fileInterface.Path);
  290. }
  291. MakeObject(context, fileInterface);
  292. return 0;
  293. }
  294. // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
  295. public long OpenDirectory(ServiceCtx context)
  296. {
  297. OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
  298. string name = ReadUtf8String(context);
  299. if (!_provider.DirectoryExists(name))
  300. {
  301. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  302. }
  303. if (IsPathAlreadyInUse(name))
  304. {
  305. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  306. }
  307. IDirectory dirInterface;
  308. try
  309. {
  310. LibHac.Fs.IDirectory dir = _provider.OpenDirectory(name, mode);
  311. dirInterface = new IDirectory(dir);
  312. }
  313. catch (UnauthorizedAccessException)
  314. {
  315. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  316. throw;
  317. }
  318. dirInterface.Disposed += RemoveDirectoryInUse;
  319. lock (_openPaths)
  320. {
  321. _openPaths.Add(dirInterface.Path);
  322. }
  323. MakeObject(context, dirInterface);
  324. return 0;
  325. }
  326. // Commit()
  327. public long Commit(ServiceCtx context)
  328. {
  329. _provider.Commit();
  330. return 0;
  331. }
  332. // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
  333. public long GetFreeSpaceSize(ServiceCtx context)
  334. {
  335. string name = ReadUtf8String(context);
  336. context.ResponseData.Write(_provider.GetFreeSpaceSize(name));
  337. return 0;
  338. }
  339. // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
  340. public long GetTotalSpaceSize(ServiceCtx context)
  341. {
  342. string name = ReadUtf8String(context);
  343. context.ResponseData.Write(_provider.GetTotalSpaceSize(name));
  344. return 0;
  345. }
  346. // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
  347. public long CleanDirectoryRecursively(ServiceCtx context)
  348. {
  349. string name = ReadUtf8String(context);
  350. if (!_provider.DirectoryExists(name))
  351. {
  352. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  353. }
  354. if (IsPathAlreadyInUse(name))
  355. {
  356. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  357. }
  358. try
  359. {
  360. _provider.CleanDirectoryRecursively(name);
  361. }
  362. catch (UnauthorizedAccessException)
  363. {
  364. Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
  365. throw;
  366. }
  367. return 0;
  368. }
  369. // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
  370. public long GetFileTimeStampRaw(ServiceCtx context)
  371. {
  372. string name = ReadUtf8String(context);
  373. if (_provider.FileExists(name) || _provider.DirectoryExists(name))
  374. {
  375. FileTimeStampRaw timestamp = _provider.GetFileTimeStampRaw(name);
  376. context.ResponseData.Write(timestamp.Created);
  377. context.ResponseData.Write(timestamp.Modified);
  378. context.ResponseData.Write(timestamp.Accessed);
  379. byte[] data = new byte[8];
  380. // is valid?
  381. data[0] = 1;
  382. context.ResponseData.Write(data);
  383. return 0;
  384. }
  385. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  386. }
  387. private bool IsPathAlreadyInUse(string path)
  388. {
  389. lock (_openPaths)
  390. {
  391. return _openPaths.Contains(path);
  392. }
  393. }
  394. private void RemoveFileInUse(object sender, EventArgs e)
  395. {
  396. IFile fileInterface = (IFile)sender;
  397. lock (_openPaths)
  398. {
  399. fileInterface.Disposed -= RemoveFileInUse;
  400. _openPaths.Remove(fileInterface.Path);
  401. }
  402. }
  403. private void RemoveDirectoryInUse(object sender, EventArgs e)
  404. {
  405. IDirectory dirInterface = (IDirectory)sender;
  406. lock (_openPaths)
  407. {
  408. dirInterface.Disposed -= RemoveDirectoryInUse;
  409. _openPaths.Remove(dirInterface.Path);
  410. }
  411. }
  412. }
  413. }