FileSystemProvider.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. return entries.ToArray();
  95. }
  96. return null;
  97. }
  98. public DirectoryEntry[] GetFiles(string path)
  99. {
  100. CheckIfDescendentOfRootPath(path);
  101. List<DirectoryEntry> entries = new List<DirectoryEntry>();
  102. foreach (string file in Directory.EnumerateFiles(path))
  103. {
  104. FileInfo fileInfo = new FileInfo(file);
  105. DirectoryEntry directoryEntry = new DirectoryEntry(file, DirectoryEntryType.File, fileInfo.Length);
  106. entries.Add(directoryEntry);
  107. }
  108. return entries.ToArray();
  109. }
  110. public long GetFreeSpace(ServiceCtx context)
  111. {
  112. return context.Device.FileSystem.GetDrive().AvailableFreeSpace;
  113. }
  114. public string GetFullPath(string name)
  115. {
  116. if (name.StartsWith("//"))
  117. {
  118. name = name.Substring(2);
  119. }
  120. else if (name.StartsWith('/'))
  121. {
  122. name = name.Substring(1);
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. string fullPath = Path.Combine(_basePath, name);
  129. CheckIfDescendentOfRootPath(fullPath);
  130. return fullPath;
  131. }
  132. public long GetTotalSpace(ServiceCtx context)
  133. {
  134. return context.Device.FileSystem.GetDrive().TotalSize;
  135. }
  136. public bool DirectoryExists(string name)
  137. {
  138. CheckIfDescendentOfRootPath(name);
  139. return Directory.Exists(name);
  140. }
  141. public bool FileExists(string name)
  142. {
  143. CheckIfDescendentOfRootPath(name);
  144. return File.Exists(name);
  145. }
  146. public long OpenDirectory(string name, int filterFlags, out IDirectory directoryInterface)
  147. {
  148. CheckIfDescendentOfRootPath(name);
  149. if (Directory.Exists(name))
  150. {
  151. directoryInterface = new IDirectory(name, filterFlags, this);
  152. return 0;
  153. }
  154. directoryInterface = null;
  155. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  156. }
  157. public long OpenFile(string name, out IFile fileInterface)
  158. {
  159. CheckIfDescendentOfRootPath(name);
  160. if (File.Exists(name))
  161. {
  162. FileStream stream = new FileStream(name, FileMode.Open);
  163. fileInterface = new IFile(stream, name);
  164. return 0;
  165. }
  166. fileInterface = null;
  167. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  168. }
  169. public long RenameDirectory(string oldName, string newName)
  170. {
  171. CheckIfDescendentOfRootPath(oldName);
  172. CheckIfDescendentOfRootPath(newName);
  173. if (Directory.Exists(oldName))
  174. {
  175. Directory.Move(oldName, newName);
  176. }
  177. else
  178. {
  179. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  180. }
  181. return 0;
  182. }
  183. public long RenameFile(string oldName, string newName)
  184. {
  185. CheckIfDescendentOfRootPath(oldName);
  186. CheckIfDescendentOfRootPath(newName);
  187. if (File.Exists(oldName))
  188. {
  189. File.Move(oldName, newName);
  190. }
  191. else
  192. {
  193. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  194. }
  195. return 0;
  196. }
  197. public void CheckIfDescendentOfRootPath(string path)
  198. {
  199. DirectoryInfo pathInfo = new DirectoryInfo(path);
  200. DirectoryInfo rootInfo = new DirectoryInfo(_rootPath);
  201. while (pathInfo.Parent != null)
  202. {
  203. if (pathInfo.Parent.FullName == rootInfo.FullName)
  204. {
  205. return;
  206. }
  207. else
  208. {
  209. pathInfo = pathInfo.Parent;
  210. }
  211. }
  212. throw new InvalidOperationException($"Path {path} is not a child directory of {_rootPath}");
  213. }
  214. }
  215. }