| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using Ryujinx.HLE.HOS;
- using Ryujinx.HLE.HOS.Services.FspSrv;
- using Ryujinx.HLE.Utilities;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using static Ryujinx.HLE.HOS.ErrorCode;
- namespace Ryujinx.HLE.FileSystem
- {
- class FileSystemProvider : IFileSystemProvider
- {
- private readonly string _basePath;
- private readonly string _rootPath;
- public FileSystemProvider(string basePath, string rootPath)
- {
- _basePath = basePath;
- _rootPath = rootPath;
- CheckIfDescendentOfRootPath(basePath);
- }
- public long CreateDirectory(string name)
- {
- CheckIfDescendentOfRootPath(name);
- if (Directory.Exists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- Directory.CreateDirectory(name);
- return 0;
- }
- public long CreateFile(string name, long size)
- {
- CheckIfDescendentOfRootPath(name);
- if (File.Exists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- using (FileStream newFile = File.Create(name))
- {
- newFile.SetLength(size);
- }
- return 0;
- }
- public long DeleteDirectory(string name, bool recursive)
- {
- CheckIfDescendentOfRootPath(name);
- string dirName = name;
- if (!Directory.Exists(dirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- Directory.Delete(dirName, recursive);
- return 0;
- }
- public long DeleteFile(string name)
- {
- CheckIfDescendentOfRootPath(name);
- if (!File.Exists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- else
- {
- File.Delete(name);
- }
- return 0;
- }
- public DirectoryEntry[] GetDirectories(string path)
- {
- CheckIfDescendentOfRootPath(path);
- List<DirectoryEntry> entries = new List<DirectoryEntry>();
- foreach(string directory in Directory.EnumerateDirectories(path))
- {
- DirectoryEntry directoryEntry = new DirectoryEntry(directory, DirectoryEntryType.Directory);
- entries.Add(directoryEntry);
- }
- return entries.ToArray();
- }
- public DirectoryEntry[] GetEntries(string path)
- {
- CheckIfDescendentOfRootPath(path);
- if (Directory.Exists(path))
- {
- List<DirectoryEntry> entries = new List<DirectoryEntry>();
- foreach (string directory in Directory.EnumerateDirectories(path))
- {
- DirectoryEntry directoryEntry = new DirectoryEntry(directory, DirectoryEntryType.Directory);
- entries.Add(directoryEntry);
- }
- foreach (string file in Directory.EnumerateFiles(path))
- {
- FileInfo fileInfo = new FileInfo(file);
- DirectoryEntry directoryEntry = new DirectoryEntry(file, DirectoryEntryType.File, fileInfo.Length);
- entries.Add(directoryEntry);
- }
- return entries.ToArray();
- }
- return null;
- }
- public DirectoryEntry[] GetFiles(string path)
- {
- CheckIfDescendentOfRootPath(path);
- List<DirectoryEntry> entries = new List<DirectoryEntry>();
- foreach (string file in Directory.EnumerateFiles(path))
- {
- FileInfo fileInfo = new FileInfo(file);
- DirectoryEntry directoryEntry = new DirectoryEntry(file, DirectoryEntryType.File, fileInfo.Length);
- entries.Add(directoryEntry);
- }
- return entries.ToArray();
- }
- public long GetFreeSpace(ServiceCtx context)
- {
- return context.Device.FileSystem.GetDrive().AvailableFreeSpace;
- }
- public string GetFullPath(string name)
- {
- if (name.StartsWith("//"))
- {
- name = name.Substring(2);
- }
- else if (name.StartsWith('/'))
- {
- name = name.Substring(1);
- }
- else
- {
- return null;
- }
- string fullPath = Path.Combine(_basePath, name);
- CheckIfDescendentOfRootPath(fullPath);
- return fullPath;
- }
- public long GetTotalSpace(ServiceCtx context)
- {
- return context.Device.FileSystem.GetDrive().TotalSize;
- }
- public bool DirectoryExists(string name)
- {
- CheckIfDescendentOfRootPath(name);
- return Directory.Exists(name);
- }
- public bool FileExists(string name)
- {
- CheckIfDescendentOfRootPath(name);
- return File.Exists(name);
- }
- public long OpenDirectory(string name, int filterFlags, out IDirectory directoryInterface)
- {
- CheckIfDescendentOfRootPath(name);
- if (Directory.Exists(name))
- {
- directoryInterface = new IDirectory(name, filterFlags, this);
- return 0;
- }
- directoryInterface = null;
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- public long OpenFile(string name, out IFile fileInterface)
- {
- CheckIfDescendentOfRootPath(name);
- if (File.Exists(name))
- {
- FileStream stream = new FileStream(name, FileMode.Open);
- fileInterface = new IFile(stream, name);
- return 0;
- }
- fileInterface = null;
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- public long RenameDirectory(string oldName, string newName)
- {
- CheckIfDescendentOfRootPath(oldName);
- CheckIfDescendentOfRootPath(newName);
- if (Directory.Exists(oldName))
- {
- Directory.Move(oldName, newName);
- }
- else
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- return 0;
- }
- public long RenameFile(string oldName, string newName)
- {
- CheckIfDescendentOfRootPath(oldName);
- CheckIfDescendentOfRootPath(newName);
- if (File.Exists(oldName))
- {
- File.Move(oldName, newName);
- }
- else
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- return 0;
- }
- public void CheckIfDescendentOfRootPath(string path)
- {
- DirectoryInfo pathInfo = new DirectoryInfo(path);
- DirectoryInfo rootInfo = new DirectoryInfo(_rootPath);
- while (pathInfo.Parent != null)
- {
- if (pathInfo.Parent.FullName == rootInfo.FullName)
- {
- return;
- }
- else
- {
- pathInfo = pathInfo.Parent;
- }
- }
- throw new InvalidOperationException($"Path {path} is not a child directory of {_rootPath}");
- }
- public FileTimestamp GetFileTimeStampRaw(string name)
- {
- CheckIfDescendentOfRootPath(name);
- DateTime creationDateTime = DateTime.UnixEpoch;
- DateTime modifiedDateTime = DateTime.UnixEpoch;
- DateTime lastAccessDateTime = DateTime.UnixEpoch;
- if (File.Exists(name))
- {
- creationDateTime = File.GetCreationTime(name);
- modifiedDateTime = File.GetLastWriteTime(name);
- lastAccessDateTime = File.GetLastAccessTime(name);
- }
- else if (Directory.Exists(name))
- {
- creationDateTime = Directory.GetCreationTime(name);
- modifiedDateTime = Directory.GetLastWriteTime(name);
- lastAccessDateTime = Directory.GetLastAccessTime(name);
- }
- return new FileTimestamp
- {
- CreationDateTime = creationDateTime,
- ModifiedDateTime = modifiedDateTime,
- LastAccessDateTime = lastAccessDateTime
- };
- }
- }
- }
|