IFileSystem.cs 11 KB

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