IFileSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. IFile FileInterface = new IFile(Stream, FileName);
  206. FileInterface.Disposed += RemoveFileInUse;
  207. lock (OpenPaths)
  208. {
  209. OpenPaths.Add(FileName);
  210. }
  211. MakeObject(Context, FileInterface);
  212. return 0;
  213. }
  214. public long OpenDirectory(ServiceCtx Context)
  215. {
  216. long Position = Context.Request.PtrBuff[0].Position;
  217. int FilterFlags = Context.RequestData.ReadInt32();
  218. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  219. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  220. if (!Directory.Exists(DirName))
  221. {
  222. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  223. }
  224. if (IsPathAlreadyInUse(DirName))
  225. {
  226. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  227. }
  228. IDirectory DirInterface = new IDirectory(DirName, FilterFlags);
  229. DirInterface.Disposed += RemoveDirectoryInUse;
  230. lock (OpenPaths)
  231. {
  232. OpenPaths.Add(DirName);
  233. }
  234. MakeObject(Context, DirInterface);
  235. return 0;
  236. }
  237. public long Commit(ServiceCtx Context)
  238. {
  239. return 0;
  240. }
  241. public long GetFreeSpaceSize(ServiceCtx Context)
  242. {
  243. long Position = Context.Request.PtrBuff[0].Position;
  244. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  245. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
  246. return 0;
  247. }
  248. public long GetTotalSpaceSize(ServiceCtx Context)
  249. {
  250. long Position = Context.Request.PtrBuff[0].Position;
  251. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  252. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);
  253. return 0;
  254. }
  255. private bool IsPathAlreadyInUse(string Path)
  256. {
  257. lock (OpenPaths)
  258. {
  259. return OpenPaths.Contains(Path);
  260. }
  261. }
  262. private void RemoveFileInUse(object sender, EventArgs e)
  263. {
  264. IFile FileInterface = (IFile)sender;
  265. lock (OpenPaths)
  266. {
  267. FileInterface.Disposed -= RemoveFileInUse;
  268. OpenPaths.Remove(FileInterface.HostPath);
  269. }
  270. }
  271. private void RemoveDirectoryInUse(object sender, EventArgs e)
  272. {
  273. IDirectory DirInterface = (IDirectory)sender;
  274. lock (OpenPaths)
  275. {
  276. DirInterface.Disposed -= RemoveDirectoryInUse;
  277. OpenPaths.Remove(DirInterface.HostPath);
  278. }
  279. }
  280. }
  281. }