IFileSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Ipc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using static Ryujinx.Core.OsHle.Objects.ErrorCode;
  7. using static Ryujinx.Core.OsHle.Objects.ObjHelper;
  8. namespace Ryujinx.Core.OsHle.Objects.FspSrv
  9. {
  10. class IFileSystem : IIpcInterface
  11. {
  12. private Dictionary<int, ServiceProcessRequest> m_Commands;
  13. public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. private HashSet<string> OpenPaths;
  15. private string Path;
  16. public IFileSystem(string Path)
  17. {
  18. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  19. {
  20. { 0, CreateFile },
  21. { 1, DeleteFile },
  22. { 2, CreateDirectory },
  23. { 3, DeleteDirectory },
  24. { 4, DeleteDirectoryRecursively },
  25. { 5, RenameFile },
  26. { 6, RenameDirectory },
  27. { 7, GetEntryType },
  28. { 8, OpenFile },
  29. { 9, OpenDirectory },
  30. { 10, Commit },
  31. { 11, GetFreeSpaceSize },
  32. { 12, GetTotalSpaceSize },
  33. //{ 13, CleanDirectoryRecursively },
  34. //{ 14, GetFileTimeStampRaw }
  35. };
  36. OpenPaths = new HashSet<string>();
  37. this.Path = Path;
  38. }
  39. public long CreateFile(ServiceCtx Context)
  40. {
  41. long Position = Context.Request.PtrBuff[0].Position;
  42. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  43. long Mode = Context.RequestData.ReadInt64();
  44. int Size = Context.RequestData.ReadInt32();
  45. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  46. if (FileName == null)
  47. {
  48. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  49. }
  50. if (File.Exists(FileName))
  51. {
  52. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  53. }
  54. if (IsPathAlreadyInUse(FileName))
  55. {
  56. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  57. }
  58. using (FileStream NewFile = File.Create(FileName))
  59. {
  60. NewFile.SetLength(Size);
  61. }
  62. return 0;
  63. }
  64. public long DeleteFile(ServiceCtx Context)
  65. {
  66. long Position = Context.Request.PtrBuff[0].Position;
  67. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  68. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  69. if (!File.Exists(FileName))
  70. {
  71. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  72. }
  73. if (IsPathAlreadyInUse(FileName))
  74. {
  75. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  76. }
  77. File.Delete(FileName);
  78. return 0;
  79. }
  80. public long CreateDirectory(ServiceCtx Context)
  81. {
  82. long Position = Context.Request.PtrBuff[0].Position;
  83. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  84. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  85. if (DirName == null)
  86. {
  87. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  88. }
  89. if (Directory.Exists(DirName))
  90. {
  91. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  92. }
  93. if (IsPathAlreadyInUse(DirName))
  94. {
  95. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  96. }
  97. Directory.CreateDirectory(DirName);
  98. return 0;
  99. }
  100. public long DeleteDirectory(ServiceCtx Context)
  101. {
  102. return DeleteDirectory(Context, false);
  103. }
  104. public long DeleteDirectoryRecursively(ServiceCtx Context)
  105. {
  106. return DeleteDirectory(Context, true);
  107. }
  108. private long DeleteDirectory(ServiceCtx Context, bool Recursive)
  109. {
  110. long Position = Context.Request.PtrBuff[0].Position;
  111. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  112. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  113. if (!Directory.Exists(DirName))
  114. {
  115. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  116. }
  117. if (IsPathAlreadyInUse(DirName))
  118. {
  119. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  120. }
  121. Directory.Delete(DirName, Recursive);
  122. return 0;
  123. }
  124. public long RenameFile(ServiceCtx Context)
  125. {
  126. long OldPosition = Context.Request.PtrBuff[0].Position;
  127. long NewPosition = Context.Request.PtrBuff[0].Position;
  128. string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
  129. string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
  130. string OldFileName = Context.Ns.VFs.GetFullPath(Path, OldName);
  131. string NewFileName = Context.Ns.VFs.GetFullPath(Path, NewName);
  132. if (!File.Exists(OldFileName))
  133. {
  134. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  135. }
  136. if (File.Exists(NewFileName))
  137. {
  138. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  139. }
  140. if (IsPathAlreadyInUse(OldFileName))
  141. {
  142. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  143. }
  144. File.Move(OldFileName, NewFileName);
  145. return 0;
  146. }
  147. public long RenameDirectory(ServiceCtx Context)
  148. {
  149. long OldPosition = Context.Request.PtrBuff[0].Position;
  150. long NewPosition = Context.Request.PtrBuff[0].Position;
  151. string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
  152. string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
  153. string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
  154. string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);
  155. if (!Directory.Exists(OldDirName))
  156. {
  157. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  158. }
  159. if (Directory.Exists(NewDirName))
  160. {
  161. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  162. }
  163. if (IsPathAlreadyInUse(OldDirName))
  164. {
  165. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  166. }
  167. Directory.Move(OldDirName, NewDirName);
  168. return 0;
  169. }
  170. public long GetEntryType(ServiceCtx Context)
  171. {
  172. long Position = Context.Request.PtrBuff[0].Position;
  173. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  174. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  175. if (File.Exists(FileName))
  176. {
  177. Context.ResponseData.Write(1);
  178. }
  179. else if (Directory.Exists(FileName))
  180. {
  181. Context.ResponseData.Write(0);
  182. }
  183. else
  184. {
  185. Context.ResponseData.Write(0);
  186. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  187. }
  188. return 0;
  189. }
  190. public long OpenFile(ServiceCtx Context)
  191. {
  192. long Position = Context.Request.PtrBuff[0].Position;
  193. int FilterFlags = Context.RequestData.ReadInt32();
  194. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  195. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  196. if (!File.Exists(FileName))
  197. {
  198. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  199. }
  200. if (IsPathAlreadyInUse(FileName))
  201. {
  202. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  203. }
  204. FileStream Stream = new FileStream(FileName, FileMode.Open);
  205. MakeObject(Context, new IFile(Stream, FileName));
  206. return 0;
  207. }
  208. public long OpenDirectory(ServiceCtx Context)
  209. {
  210. long Position = Context.Request.PtrBuff[0].Position;
  211. int FilterFlags = Context.RequestData.ReadInt32();
  212. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  213. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  214. if (!Directory.Exists(DirName))
  215. {
  216. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  217. }
  218. if (IsPathAlreadyInUse(DirName))
  219. {
  220. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  221. }
  222. IDirectory DirInterface = new IDirectory(DirName, FilterFlags);
  223. DirInterface.Disposed += RemoveDirectoryInUse;
  224. lock (OpenPaths)
  225. {
  226. OpenPaths.Add(DirName);
  227. }
  228. MakeObject(Context, DirInterface);
  229. return 0;
  230. }
  231. public long Commit(ServiceCtx Context)
  232. {
  233. return 0;
  234. }
  235. public long GetFreeSpaceSize(ServiceCtx Context)
  236. {
  237. long Position = Context.Request.PtrBuff[0].Position;
  238. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  239. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
  240. return 0;
  241. }
  242. public long GetTotalSpaceSize(ServiceCtx Context)
  243. {
  244. long Position = Context.Request.PtrBuff[0].Position;
  245. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  246. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);
  247. return 0;
  248. }
  249. private bool IsPathAlreadyInUse(string Path)
  250. {
  251. lock (OpenPaths)
  252. {
  253. return OpenPaths.Contains(Path);
  254. }
  255. }
  256. private void RemoveFileInUse(object sender, EventArgs e)
  257. {
  258. IFile FileInterface = (IFile)sender;
  259. lock (OpenPaths)
  260. {
  261. FileInterface.Disposed -= RemoveDirectoryInUse;
  262. OpenPaths.Remove(FileInterface.HostPath);
  263. }
  264. }
  265. private void RemoveDirectoryInUse(object sender, EventArgs e)
  266. {
  267. IDirectory DirInterface = (IDirectory)sender;
  268. lock (OpenPaths)
  269. {
  270. DirInterface.Disposed -= RemoveDirectoryInUse;
  271. OpenPaths.Remove(DirInterface.HostPath);
  272. }
  273. }
  274. }
  275. }