IFileSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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> m_Commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. private HashSet<string> OpenPaths;
  15. private string Path;
  16. private IFileSystemProvider Provider;
  17. public IFileSystem(string Path, IFileSystemProvider Provider)
  18. {
  19. m_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. this.Path = Path;
  39. this.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. private bool IsPathAlreadyInUse(string Path)
  283. {
  284. lock (OpenPaths)
  285. {
  286. return OpenPaths.Contains(Path);
  287. }
  288. }
  289. private void RemoveFileInUse(object sender, EventArgs e)
  290. {
  291. IFile FileInterface = (IFile)sender;
  292. lock (OpenPaths)
  293. {
  294. FileInterface.Disposed -= RemoveFileInUse;
  295. OpenPaths.Remove(FileInterface.HostPath);
  296. }
  297. }
  298. private void RemoveDirectoryInUse(object sender, EventArgs e)
  299. {
  300. IDirectory DirInterface = (IDirectory)sender;
  301. lock (OpenPaths)
  302. {
  303. DirInterface.Disposed -= RemoveDirectoryInUse;
  304. OpenPaths.Remove(DirInterface.DirectoryPath);
  305. }
  306. }
  307. }
  308. }