IFileSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using Ryujinx.Core.OsHle.Ipc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using static Ryujinx.Core.OsHle.ErrorCode;
  7. using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
  8. namespace Ryujinx.Core.OsHle.IpcServices.FspSrv
  9. {
  10. class IFileSystem : IIpcService
  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 = ReadUtf8String(Context);
  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 = ReadUtf8String(Context);
  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 = ReadUtf8String(Context);
  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 = ReadUtf8String(Context);
  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. string OldName = ReadUtf8String(Context, 0);
  127. string NewName = ReadUtf8String(Context, 1);
  128. string OldFileName = Context.Ns.VFs.GetFullPath(Path, OldName);
  129. string NewFileName = Context.Ns.VFs.GetFullPath(Path, NewName);
  130. if (!File.Exists(OldFileName))
  131. {
  132. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  133. }
  134. if (File.Exists(NewFileName))
  135. {
  136. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  137. }
  138. if (IsPathAlreadyInUse(OldFileName))
  139. {
  140. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  141. }
  142. File.Move(OldFileName, NewFileName);
  143. return 0;
  144. }
  145. public long RenameDirectory(ServiceCtx Context)
  146. {
  147. string OldName = ReadUtf8String(Context, 0);
  148. string NewName = ReadUtf8String(Context, 1);
  149. string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
  150. string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);
  151. if (!Directory.Exists(OldDirName))
  152. {
  153. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  154. }
  155. if (Directory.Exists(NewDirName))
  156. {
  157. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  158. }
  159. if (IsPathAlreadyInUse(OldDirName))
  160. {
  161. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  162. }
  163. Directory.Move(OldDirName, NewDirName);
  164. return 0;
  165. }
  166. public long GetEntryType(ServiceCtx Context)
  167. {
  168. long Position = Context.Request.PtrBuff[0].Position;
  169. string Name = ReadUtf8String(Context);
  170. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  171. if (File.Exists(FileName))
  172. {
  173. Context.ResponseData.Write(1);
  174. }
  175. else if (Directory.Exists(FileName))
  176. {
  177. Context.ResponseData.Write(0);
  178. }
  179. else
  180. {
  181. Context.ResponseData.Write(0);
  182. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  183. }
  184. return 0;
  185. }
  186. public long OpenFile(ServiceCtx Context)
  187. {
  188. long Position = Context.Request.PtrBuff[0].Position;
  189. int FilterFlags = Context.RequestData.ReadInt32();
  190. string Name = ReadUtf8String(Context);
  191. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  192. if (!File.Exists(FileName))
  193. {
  194. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  195. }
  196. if (IsPathAlreadyInUse(FileName))
  197. {
  198. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  199. }
  200. FileStream Stream = new FileStream(FileName, FileMode.Open);
  201. IFile FileInterface = new IFile(Stream, FileName);
  202. FileInterface.Disposed += RemoveFileInUse;
  203. lock (OpenPaths)
  204. {
  205. OpenPaths.Add(FileName);
  206. }
  207. MakeObject(Context, FileInterface);
  208. return 0;
  209. }
  210. public long OpenDirectory(ServiceCtx Context)
  211. {
  212. long Position = Context.Request.PtrBuff[0].Position;
  213. int FilterFlags = Context.RequestData.ReadInt32();
  214. string Name = ReadUtf8String(Context);
  215. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  216. if (!Directory.Exists(DirName))
  217. {
  218. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  219. }
  220. if (IsPathAlreadyInUse(DirName))
  221. {
  222. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
  223. }
  224. IDirectory DirInterface = new IDirectory(DirName, FilterFlags);
  225. DirInterface.Disposed += RemoveDirectoryInUse;
  226. lock (OpenPaths)
  227. {
  228. OpenPaths.Add(DirName);
  229. }
  230. MakeObject(Context, DirInterface);
  231. return 0;
  232. }
  233. public long Commit(ServiceCtx Context)
  234. {
  235. return 0;
  236. }
  237. public long GetFreeSpaceSize(ServiceCtx Context)
  238. {
  239. long Position = Context.Request.PtrBuff[0].Position;
  240. string Name = ReadUtf8String(Context);
  241. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
  242. return 0;
  243. }
  244. public long GetTotalSpaceSize(ServiceCtx Context)
  245. {
  246. long Position = Context.Request.PtrBuff[0].Position;
  247. string Name = ReadUtf8String(Context);
  248. Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);
  249. return 0;
  250. }
  251. private bool IsPathAlreadyInUse(string Path)
  252. {
  253. lock (OpenPaths)
  254. {
  255. return OpenPaths.Contains(Path);
  256. }
  257. }
  258. private void RemoveFileInUse(object sender, EventArgs e)
  259. {
  260. IFile FileInterface = (IFile)sender;
  261. lock (OpenPaths)
  262. {
  263. FileInterface.Disposed -= RemoveFileInUse;
  264. OpenPaths.Remove(FileInterface.HostPath);
  265. }
  266. }
  267. private void RemoveDirectoryInUse(object sender, EventArgs e)
  268. {
  269. IDirectory DirInterface = (IDirectory)sender;
  270. lock (OpenPaths)
  271. {
  272. DirInterface.Disposed -= RemoveDirectoryInUse;
  273. OpenPaths.Remove(DirInterface.HostPath);
  274. }
  275. }
  276. private string ReadUtf8String(ServiceCtx Context, int Index = 0)
  277. {
  278. long Position = Context.Request.PtrBuff[Index].Position;
  279. long Size = Context.Request.PtrBuff[Index].Size;
  280. using (MemoryStream MS = new MemoryStream())
  281. {
  282. while (Size-- > 0)
  283. {
  284. byte Value = Context.Memory.ReadByte(Position++);
  285. if (Value == 0)
  286. {
  287. break;
  288. }
  289. MS.WriteByte(Value);
  290. }
  291. return Encoding.UTF8.GetString(MS.ToArray());
  292. }
  293. }
  294. }
  295. }