FileSystemProvider.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using Ryujinx.HLE.HOS;
  2. using Ryujinx.HLE.HOS.Services.FspSrv;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using static Ryujinx.HLE.HOS.ErrorCode;
  7. namespace Ryujinx.HLE.FileSystem
  8. {
  9. class FileSystemProvider : IFileSystemProvider
  10. {
  11. private readonly string BasePath;
  12. private readonly string RootPath;
  13. public FileSystemProvider(string BasePath, string RootPath)
  14. {
  15. this.BasePath = BasePath;
  16. this.RootPath = RootPath;
  17. CheckIfDecendentOfRootPath(BasePath);
  18. }
  19. public long CreateDirectory(string Name)
  20. {
  21. CheckIfDecendentOfRootPath(Name);
  22. if (Directory.Exists(Name))
  23. {
  24. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  25. }
  26. Directory.CreateDirectory(Name);
  27. return 0;
  28. }
  29. public long CreateFile(string Name, long Size)
  30. {
  31. CheckIfDecendentOfRootPath(Name);
  32. if (File.Exists(Name))
  33. {
  34. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  35. }
  36. using (FileStream NewFile = File.Create(Name))
  37. {
  38. NewFile.SetLength(Size);
  39. }
  40. return 0;
  41. }
  42. public long DeleteDirectory(string Name, bool Recursive)
  43. {
  44. CheckIfDecendentOfRootPath(Name);
  45. string DirName = Name;
  46. if (!Directory.Exists(DirName))
  47. {
  48. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  49. }
  50. Directory.Delete(DirName, Recursive);
  51. return 0;
  52. }
  53. public long DeleteFile(string Name)
  54. {
  55. CheckIfDecendentOfRootPath(Name);
  56. if (!File.Exists(Name))
  57. {
  58. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  59. }
  60. else
  61. {
  62. File.Delete(Name);
  63. }
  64. return 0;
  65. }
  66. public DirectoryEntry[] GetDirectories(string Path)
  67. {
  68. CheckIfDecendentOfRootPath(Path);
  69. List<DirectoryEntry> Entries = new List<DirectoryEntry>();
  70. foreach(string Directory in Directory.EnumerateDirectories(Path))
  71. {
  72. DirectoryEntry DirectoryEntry = new DirectoryEntry(Directory, DirectoryEntryType.Directory);
  73. Entries.Add(DirectoryEntry);
  74. }
  75. return Entries.ToArray();
  76. }
  77. public DirectoryEntry[] GetEntries(string Path)
  78. {
  79. CheckIfDecendentOfRootPath(Path);
  80. if (Directory.Exists(Path))
  81. {
  82. List<DirectoryEntry> Entries = new List<DirectoryEntry>();
  83. foreach (string Directory in Directory.EnumerateDirectories(Path))
  84. {
  85. DirectoryEntry DirectoryEntry = new DirectoryEntry(Directory, DirectoryEntryType.Directory);
  86. Entries.Add(DirectoryEntry);
  87. }
  88. foreach (string File in Directory.EnumerateFiles(Path))
  89. {
  90. FileInfo FileInfo = new FileInfo(File);
  91. DirectoryEntry DirectoryEntry = new DirectoryEntry(File, DirectoryEntryType.File, FileInfo.Length);
  92. Entries.Add(DirectoryEntry);
  93. }
  94. }
  95. return null;
  96. }
  97. public DirectoryEntry[] GetFiles(string Path)
  98. {
  99. CheckIfDecendentOfRootPath(Path);
  100. List<DirectoryEntry> Entries = new List<DirectoryEntry>();
  101. foreach (string File in Directory.EnumerateFiles(Path))
  102. {
  103. FileInfo FileInfo = new FileInfo(File);
  104. DirectoryEntry DirectoryEntry = new DirectoryEntry(File, DirectoryEntryType.File, FileInfo.Length);
  105. Entries.Add(DirectoryEntry);
  106. }
  107. return Entries.ToArray();
  108. }
  109. public long GetFreeSpace(ServiceCtx Context)
  110. {
  111. return Context.Device.FileSystem.GetDrive().AvailableFreeSpace;
  112. }
  113. public string GetFullPath(string Name)
  114. {
  115. if (Name.StartsWith("//"))
  116. {
  117. Name = Name.Substring(2);
  118. }
  119. else if (Name.StartsWith('/'))
  120. {
  121. Name = Name.Substring(1);
  122. }
  123. else
  124. {
  125. return null;
  126. }
  127. string FullPath = Path.Combine(BasePath, Name);
  128. CheckIfDecendentOfRootPath(FullPath);
  129. return FullPath;
  130. }
  131. public long GetTotalSpace(ServiceCtx Context)
  132. {
  133. return Context.Device.FileSystem.GetDrive().TotalSize;
  134. }
  135. public bool DirectoryExists(string Name)
  136. {
  137. CheckIfDecendentOfRootPath(Name);
  138. return Directory.Exists(Name);
  139. }
  140. public bool FileExists(string Name)
  141. {
  142. CheckIfDecendentOfRootPath(Name);
  143. return File.Exists(Name);
  144. }
  145. public long OpenDirectory(string Name, int FilterFlags, out IDirectory DirectoryInterface)
  146. {
  147. CheckIfDecendentOfRootPath(Name);
  148. if (Directory.Exists(Name))
  149. {
  150. DirectoryInterface = new IDirectory(Name, FilterFlags, this);
  151. return 0;
  152. }
  153. DirectoryInterface = null;
  154. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  155. }
  156. public long OpenFile(string Name, out IFile FileInterface)
  157. {
  158. CheckIfDecendentOfRootPath(Name);
  159. if (File.Exists(Name))
  160. {
  161. FileStream Stream = new FileStream(Name, FileMode.Open);
  162. FileInterface = new IFile(Stream, Name);
  163. return 0;
  164. }
  165. FileInterface = null;
  166. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  167. }
  168. public long RenameDirectory(string OldName, string NewName)
  169. {
  170. CheckIfDecendentOfRootPath(OldName);
  171. CheckIfDecendentOfRootPath(NewName);
  172. if (Directory.Exists(OldName))
  173. {
  174. Directory.Move(OldName, NewName);
  175. }
  176. else
  177. {
  178. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  179. }
  180. return 0;
  181. }
  182. public long RenameFile(string OldName, string NewName)
  183. {
  184. CheckIfDecendentOfRootPath(OldName);
  185. CheckIfDecendentOfRootPath(NewName);
  186. if (File.Exists(OldName))
  187. {
  188. File.Move(OldName, NewName);
  189. }
  190. else
  191. {
  192. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  193. }
  194. return 0;
  195. }
  196. public void CheckIfDecendentOfRootPath(string Path)
  197. {
  198. DirectoryInfo PathInfo = new DirectoryInfo(Path);
  199. DirectoryInfo RootInfo = new DirectoryInfo(RootPath);
  200. while (PathInfo.Parent != null)
  201. {
  202. if (PathInfo.Parent.FullName == RootInfo.FullName)
  203. {
  204. return;
  205. }
  206. else
  207. {
  208. PathInfo = PathInfo.Parent;
  209. }
  210. }
  211. throw new InvalidOperationException($"Path {Path} is not a child directory of {RootPath}");
  212. }
  213. }
  214. }