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. _basePath = basePath;
  16. _rootPath = rootPath;
  17. CheckIfDescendentOfRootPath(basePath);
  18. }
  19. public long CreateDirectory(string name)
  20. {
  21. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(name);
  138. return Directory.Exists(name);
  139. }
  140. public bool FileExists(string name)
  141. {
  142. CheckIfDescendentOfRootPath(name);
  143. return File.Exists(name);
  144. }
  145. public long OpenDirectory(string name, int filterFlags, out IDirectory directoryInterface)
  146. {
  147. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(oldName);
  171. CheckIfDescendentOfRootPath(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. CheckIfDescendentOfRootPath(oldName);
  185. CheckIfDescendentOfRootPath(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 CheckIfDescendentOfRootPath(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. }