FileSystemProvider.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Ryujinx.HLE.HOS;
  2. using Ryujinx.HLE.HOS.Services.FspSrv;
  3. using Ryujinx.HLE.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using static Ryujinx.HLE.HOS.ErrorCode;
  8. namespace Ryujinx.HLE.FileSystem
  9. {
  10. class FileSystemProvider : IFileSystemProvider
  11. {
  12. private readonly string _basePath;
  13. private readonly string _rootPath;
  14. public FileSystemProvider(string basePath, string rootPath)
  15. {
  16. _basePath = basePath;
  17. _rootPath = rootPath;
  18. CheckIfDescendentOfRootPath(basePath);
  19. }
  20. public long CreateDirectory(string name)
  21. {
  22. CheckIfDescendentOfRootPath(name);
  23. if (Directory.Exists(name))
  24. {
  25. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  26. }
  27. Directory.CreateDirectory(name);
  28. return 0;
  29. }
  30. public long CreateFile(string name, long size)
  31. {
  32. CheckIfDescendentOfRootPath(name);
  33. if (File.Exists(name))
  34. {
  35. return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
  36. }
  37. using (FileStream newFile = File.Create(name))
  38. {
  39. newFile.SetLength(size);
  40. }
  41. return 0;
  42. }
  43. public long DeleteDirectory(string name, bool recursive)
  44. {
  45. CheckIfDescendentOfRootPath(name);
  46. string dirName = name;
  47. if (!Directory.Exists(dirName))
  48. {
  49. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  50. }
  51. Directory.Delete(dirName, recursive);
  52. return 0;
  53. }
  54. public long DeleteFile(string name)
  55. {
  56. CheckIfDescendentOfRootPath(name);
  57. if (!File.Exists(name))
  58. {
  59. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  60. }
  61. else
  62. {
  63. File.Delete(name);
  64. }
  65. return 0;
  66. }
  67. public DirectoryEntry[] GetDirectories(string path)
  68. {
  69. CheckIfDescendentOfRootPath(path);
  70. List<DirectoryEntry> entries = new List<DirectoryEntry>();
  71. foreach(string directory in Directory.EnumerateDirectories(path))
  72. {
  73. DirectoryEntry directoryEntry = new DirectoryEntry(directory, DirectoryEntryType.Directory);
  74. entries.Add(directoryEntry);
  75. }
  76. return entries.ToArray();
  77. }
  78. public DirectoryEntry[] GetEntries(string path)
  79. {
  80. CheckIfDescendentOfRootPath(path);
  81. if (Directory.Exists(path))
  82. {
  83. List<DirectoryEntry> entries = new List<DirectoryEntry>();
  84. foreach (string directory in Directory.EnumerateDirectories(path))
  85. {
  86. DirectoryEntry directoryEntry = new DirectoryEntry(directory, DirectoryEntryType.Directory);
  87. entries.Add(directoryEntry);
  88. }
  89. foreach (string file in Directory.EnumerateFiles(path))
  90. {
  91. FileInfo fileInfo = new FileInfo(file);
  92. DirectoryEntry directoryEntry = new DirectoryEntry(file, DirectoryEntryType.File, fileInfo.Length);
  93. entries.Add(directoryEntry);
  94. }
  95. return entries.ToArray();
  96. }
  97. return null;
  98. }
  99. public DirectoryEntry[] GetFiles(string path)
  100. {
  101. CheckIfDescendentOfRootPath(path);
  102. List<DirectoryEntry> entries = new List<DirectoryEntry>();
  103. foreach (string file in Directory.EnumerateFiles(path))
  104. {
  105. FileInfo fileInfo = new FileInfo(file);
  106. DirectoryEntry directoryEntry = new DirectoryEntry(file, DirectoryEntryType.File, fileInfo.Length);
  107. entries.Add(directoryEntry);
  108. }
  109. return entries.ToArray();
  110. }
  111. public long GetFreeSpace(ServiceCtx context)
  112. {
  113. return context.Device.FileSystem.GetDrive().AvailableFreeSpace;
  114. }
  115. public string GetFullPath(string name)
  116. {
  117. if (name.StartsWith("//"))
  118. {
  119. name = name.Substring(2);
  120. }
  121. else if (name.StartsWith('/'))
  122. {
  123. name = name.Substring(1);
  124. }
  125. else
  126. {
  127. return null;
  128. }
  129. string fullPath = Path.Combine(_basePath, name);
  130. CheckIfDescendentOfRootPath(fullPath);
  131. return fullPath;
  132. }
  133. public long GetTotalSpace(ServiceCtx context)
  134. {
  135. return context.Device.FileSystem.GetDrive().TotalSize;
  136. }
  137. public bool DirectoryExists(string name)
  138. {
  139. CheckIfDescendentOfRootPath(name);
  140. return Directory.Exists(name);
  141. }
  142. public bool FileExists(string name)
  143. {
  144. CheckIfDescendentOfRootPath(name);
  145. return File.Exists(name);
  146. }
  147. public long OpenDirectory(string name, int filterFlags, out IDirectory directoryInterface)
  148. {
  149. CheckIfDescendentOfRootPath(name);
  150. if (Directory.Exists(name))
  151. {
  152. directoryInterface = new IDirectory(name, filterFlags, this);
  153. return 0;
  154. }
  155. directoryInterface = null;
  156. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  157. }
  158. public long OpenFile(string name, out IFile fileInterface)
  159. {
  160. CheckIfDescendentOfRootPath(name);
  161. if (File.Exists(name))
  162. {
  163. FileStream stream = new FileStream(name, FileMode.Open);
  164. fileInterface = new IFile(stream, name);
  165. return 0;
  166. }
  167. fileInterface = null;
  168. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  169. }
  170. public long RenameDirectory(string oldName, string newName)
  171. {
  172. CheckIfDescendentOfRootPath(oldName);
  173. CheckIfDescendentOfRootPath(newName);
  174. if (Directory.Exists(oldName))
  175. {
  176. Directory.Move(oldName, newName);
  177. }
  178. else
  179. {
  180. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  181. }
  182. return 0;
  183. }
  184. public long RenameFile(string oldName, string newName)
  185. {
  186. CheckIfDescendentOfRootPath(oldName);
  187. CheckIfDescendentOfRootPath(newName);
  188. if (File.Exists(oldName))
  189. {
  190. File.Move(oldName, newName);
  191. }
  192. else
  193. {
  194. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  195. }
  196. return 0;
  197. }
  198. public void CheckIfDescendentOfRootPath(string path)
  199. {
  200. DirectoryInfo pathInfo = new DirectoryInfo(path);
  201. DirectoryInfo rootInfo = new DirectoryInfo(_rootPath);
  202. while (pathInfo.Parent != null)
  203. {
  204. if (pathInfo.Parent.FullName == rootInfo.FullName)
  205. {
  206. return;
  207. }
  208. else
  209. {
  210. pathInfo = pathInfo.Parent;
  211. }
  212. }
  213. throw new InvalidOperationException($"Path {path} is not a child directory of {_rootPath}");
  214. }
  215. public FileTimestamp GetFileTimeStampRaw(string name)
  216. {
  217. CheckIfDescendentOfRootPath(name);
  218. DateTime creationDateTime = DateTime.UnixEpoch;
  219. DateTime modifiedDateTime = DateTime.UnixEpoch;
  220. DateTime lastAccessDateTime = DateTime.UnixEpoch;
  221. if (File.Exists(name))
  222. {
  223. creationDateTime = File.GetCreationTime(name);
  224. modifiedDateTime = File.GetLastWriteTime(name);
  225. lastAccessDateTime = File.GetLastAccessTime(name);
  226. }
  227. else if (Directory.Exists(name))
  228. {
  229. creationDateTime = Directory.GetCreationTime(name);
  230. modifiedDateTime = Directory.GetLastWriteTime(name);
  231. lastAccessDateTime = Directory.GetLastAccessTime(name);
  232. }
  233. return new FileTimestamp
  234. {
  235. CreationDateTime = creationDateTime,
  236. ModifiedDateTime = modifiedDateTime,
  237. LastAccessDateTime = lastAccessDateTime
  238. };
  239. }
  240. }
  241. }