IFileSystem.cs 11 KB

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