2018-11-18 19:37:41 +00:00
|
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.FspSrv;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.FileSystem
|
|
|
|
|
{
|
|
|
|
|
class FileSystemProvider : IFileSystemProvider
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
private readonly string BasePath;
|
|
|
|
|
private readonly string RootPath;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public FileSystemProvider(string BasePath, string RootPath)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
this.BasePath = BasePath;
|
|
|
|
|
this.RootPath = RootPath;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(BasePath);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long CreateDirectory(string Name)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Directory.Exists(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Directory.CreateDirectory(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long CreateFile(string Name, long Size)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (File.Exists(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
using (FileStream NewFile = File.Create(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
NewFile.SetLength(Size);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long DeleteDirectory(string Name, bool Recursive)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
string DirName = Name;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (!Directory.Exists(DirName))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Directory.Delete(DirName, Recursive);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long DeleteFile(string Name)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (!File.Exists(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
File.Delete(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public DirectoryEntry[] GetDirectories(string Path)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Path);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
List<DirectoryEntry> Entries = new List<DirectoryEntry>();
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
foreach(string Directory in Directory.EnumerateDirectories(Path))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
DirectoryEntry DirectoryEntry = new DirectoryEntry(Directory, DirectoryEntryType.Directory);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Entries.Add(DirectoryEntry);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Entries.ToArray();
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public DirectoryEntry[] GetEntries(string Path)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Path);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Directory.Exists(Path))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
List<DirectoryEntry> Entries = new List<DirectoryEntry>();
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
foreach (string Directory in Directory.EnumerateDirectories(Path))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
DirectoryEntry DirectoryEntry = new DirectoryEntry(Directory, DirectoryEntryType.Directory);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Entries.Add(DirectoryEntry);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
foreach (string File in Directory.EnumerateFiles(Path))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FileInfo FileInfo = new FileInfo(File);
|
|
|
|
|
DirectoryEntry DirectoryEntry = new DirectoryEntry(File, DirectoryEntryType.File, FileInfo.Length);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Entries.Add(DirectoryEntry);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public DirectoryEntry[] GetFiles(string Path)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Path);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
List<DirectoryEntry> Entries = new List<DirectoryEntry>();
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
foreach (string File in Directory.EnumerateFiles(Path))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FileInfo FileInfo = new FileInfo(File);
|
|
|
|
|
DirectoryEntry DirectoryEntry = new DirectoryEntry(File, DirectoryEntryType.File, FileInfo.Length);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Entries.Add(DirectoryEntry);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Entries.ToArray();
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long GetFreeSpace(ServiceCtx Context)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Context.Device.FileSystem.GetDrive().AvailableFreeSpace;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public string GetFullPath(string Name)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Name.StartsWith("//"))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Name = Name.Substring(2);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
|
else if (Name.StartsWith('/'))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Name = Name.Substring(1);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
string FullPath = Path.Combine(BasePath, Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(FullPath);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return FullPath;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long GetTotalSpace(ServiceCtx Context)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Context.Device.FileSystem.GetDrive().TotalSize;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public bool DirectoryExists(string Name)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Directory.Exists(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public bool FileExists(string Name)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return File.Exists(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long OpenDirectory(string Name, int FilterFlags, out IDirectory DirectoryInterface)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Directory.Exists(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
DirectoryInterface = new IDirectory(Name, FilterFlags, this);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
DirectoryInterface = null;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long OpenFile(string Name, out IFile FileInterface)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (File.Exists(Name))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FileStream Stream = new FileStream(Name, FileMode.Open);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FileInterface = new IFile(Stream, Name);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
FileInterface = null;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long RenameDirectory(string OldName, string NewName)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(OldName);
|
|
|
|
|
CheckIfDescendentOfRootPath(NewName);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Directory.Exists(OldName))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Directory.Move(OldName, NewName);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public long RenameFile(string OldName, string NewName)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
CheckIfDescendentOfRootPath(OldName);
|
|
|
|
|
CheckIfDescendentOfRootPath(NewName);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (File.Exists(OldName))
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
File.Move(OldName, NewName);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public void CheckIfDescendentOfRootPath(string Path)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
DirectoryInfo PathInfo = new DirectoryInfo(Path);
|
|
|
|
|
DirectoryInfo RootInfo = new DirectoryInfo(RootPath);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
while (PathInfo.Parent != null)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (PathInfo.Parent.FullName == RootInfo.FullName)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
PathInfo = PathInfo.Parent;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
throw new InvalidOperationException($"Path {Path} is not a child directory of {RootPath}");
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|