IFileSystem.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using ChocolArm64.Memory;
  2. using Ryujinx.OsHle.Ipc;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using static Ryujinx.OsHle.Objects.ObjHelper;
  6. namespace Ryujinx.OsHle.Objects.FspSrv
  7. {
  8. class IFileSystem : IIpcInterface
  9. {
  10. private Dictionary<int, ServiceProcessRequest> m_Commands;
  11. public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  12. private string Path;
  13. public IFileSystem(string Path)
  14. {
  15. //TODO: implement.
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 0, CreateFile },
  19. { 1, DeleteFile },
  20. { 2, CreateDirectory },
  21. { 3, DeleteDirectory },
  22. { 4, DeleteDirectoryRecursively },
  23. { 5, RenameFile },
  24. { 6, RenameDirectory },
  25. { 7, GetEntryType },
  26. { 8, OpenFile },
  27. { 9, OpenDirectory },
  28. { 10, Commit },
  29. //{ 11, GetFreeSpaceSize },
  30. //{ 12, GetTotalSpaceSize },
  31. //{ 13, CleanDirectoryRecursively },
  32. //{ 14, GetFileTimeStampRaw }
  33. };
  34. this.Path = Path;
  35. }
  36. public long CreateFile(ServiceCtx Context)
  37. {
  38. long Position = Context.Request.PtrBuff[0].Position;
  39. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  40. ulong Mode = Context.RequestData.ReadUInt64();
  41. uint Size = Context.RequestData.ReadUInt32();
  42. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  43. if (FileName != null)
  44. {
  45. FileStream NewFile = File.Create(FileName);
  46. NewFile.SetLength(Size);
  47. NewFile.Close();
  48. return 0;
  49. }
  50. //TODO: Correct error code.
  51. return -1;
  52. }
  53. public long DeleteFile(ServiceCtx Context)
  54. {
  55. long Position = Context.Request.PtrBuff[0].Position;
  56. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  57. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  58. if (FileName != null)
  59. {
  60. File.Delete(FileName);
  61. return 0;
  62. }
  63. //TODO: Correct error code.
  64. return -1;
  65. }
  66. public long CreateDirectory(ServiceCtx Context)
  67. {
  68. long Position = Context.Request.PtrBuff[0].Position;
  69. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  70. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  71. if (FileName != null)
  72. {
  73. Directory.CreateDirectory(FileName);
  74. return 0;
  75. }
  76. //TODO: Correct error code.
  77. return -1;
  78. }
  79. public long DeleteDirectory(ServiceCtx Context)
  80. {
  81. long Position = Context.Request.PtrBuff[0].Position;
  82. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  83. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  84. if (FileName != null)
  85. {
  86. Directory.Delete(FileName);
  87. return 0;
  88. }
  89. // TODO: Correct error code.
  90. return -1;
  91. }
  92. public long DeleteDirectoryRecursively(ServiceCtx Context)
  93. {
  94. long Position = Context.Request.PtrBuff[0].Position;
  95. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  96. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  97. if (FileName != null)
  98. {
  99. Directory.Delete(FileName, true); // recursive = true
  100. return 0;
  101. }
  102. // TODO: Correct error code.
  103. return -1;
  104. }
  105. public long RenameFile(ServiceCtx Context)
  106. {
  107. long OldPosition = Context.Request.PtrBuff[0].Position;
  108. long NewPosition = Context.Request.PtrBuff[0].Position;
  109. string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
  110. string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
  111. string OldFileName = Context.Ns.VFs.GetFullPath(Path, OldName);
  112. string NewFileName = Context.Ns.VFs.GetFullPath(Path, NewName);
  113. if (OldFileName != null && NewFileName != null)
  114. {
  115. File.Move(OldFileName, NewFileName);
  116. return 0;
  117. }
  118. // TODO: Correct error code.
  119. return -1;
  120. }
  121. public long RenameDirectory(ServiceCtx Context)
  122. {
  123. long OldPosition = Context.Request.PtrBuff[0].Position;
  124. long NewPosition = Context.Request.PtrBuff[0].Position;
  125. string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
  126. string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
  127. string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
  128. string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);
  129. if (OldDirName != null && NewDirName != null)
  130. {
  131. Directory.Move(OldDirName, NewDirName);
  132. return 0;
  133. }
  134. // TODO: Correct error code.
  135. return -1;
  136. }
  137. public long GetEntryType(ServiceCtx Context)
  138. {
  139. long Position = Context.Request.PtrBuff[0].Position;
  140. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  141. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  142. if (FileName == null)
  143. {
  144. //TODO: Correct error code.
  145. return -1;
  146. }
  147. bool IsFile = File.Exists(FileName);
  148. Context.ResponseData.Write(IsFile ? 1 : 0);
  149. return 0;
  150. }
  151. public long OpenFile(ServiceCtx Context)
  152. {
  153. long Position = Context.Request.PtrBuff[0].Position;
  154. int FilterFlags = Context.RequestData.ReadInt32();
  155. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  156. string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
  157. if (FileName == null)
  158. {
  159. //TODO: Correct error code.
  160. return -1;
  161. }
  162. if (File.Exists(FileName))
  163. {
  164. FileStream Stream = new FileStream(FileName, FileMode.OpenOrCreate);
  165. MakeObject(Context, new IFile(Stream));
  166. return 0;
  167. }
  168. //TODO: Correct error code.
  169. return -1;
  170. }
  171. public long OpenDirectory(ServiceCtx Context)
  172. {
  173. long Position = Context.Request.PtrBuff[0].Position;
  174. int FilterFlags = Context.RequestData.ReadInt32();
  175. string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
  176. string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
  177. if(DirName != null)
  178. {
  179. if (Directory.Exists(DirName))
  180. {
  181. MakeObject(Context, new IDirectory(DirName, FilterFlags));
  182. return 0;
  183. }
  184. else
  185. {
  186. // TODO: correct error code.
  187. return -1;
  188. }
  189. }
  190. // TODO: Correct error code.
  191. return -1;
  192. }
  193. public long Commit(ServiceCtx Context)
  194. {
  195. return 0;
  196. }
  197. }
  198. }