2018-02-04 15:08:20 -08:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-20 12:09:23 -08:00
|
|
|
using Ryujinx.Core.OsHle.Ipc;
|
2018-02-21 13:56:52 -08:00
|
|
|
using System;
|
2018-02-09 16:14:55 -08:00
|
|
|
using System.Collections.Generic;
|
2018-02-04 15:08:20 -08:00
|
|
|
using System.IO;
|
2018-03-02 21:24:04 -08:00
|
|
|
using System.Text;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-24 20:34:16 -08:00
|
|
|
using static Ryujinx.Core.OsHle.IpcServices.ErrorCode;
|
|
|
|
using static Ryujinx.Core.OsHle.IpcServices.ObjHelper;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-24 20:34:16 -08:00
|
|
|
namespace Ryujinx.Core.OsHle.IpcServices.FspSrv
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-24 20:34:16 -08:00
|
|
|
class IFileSystem : IIpcService
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-09 16:14:55 -08:00
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
private HashSet<string> OpenPaths;
|
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
private string Path;
|
|
|
|
|
|
|
|
public IFileSystem(string Path)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-09 16:14:55 -08:00
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
{ 0, CreateFile },
|
|
|
|
{ 1, DeleteFile },
|
|
|
|
{ 2, CreateDirectory },
|
|
|
|
{ 3, DeleteDirectory },
|
2018-02-20 03:03:04 -08:00
|
|
|
{ 4, DeleteDirectoryRecursively },
|
2018-02-21 13:56:52 -08:00
|
|
|
{ 5, RenameFile },
|
|
|
|
{ 6, RenameDirectory },
|
|
|
|
{ 7, GetEntryType },
|
|
|
|
{ 8, OpenFile },
|
|
|
|
{ 9, OpenDirectory },
|
|
|
|
{ 10, Commit },
|
|
|
|
{ 11, GetFreeSpaceSize },
|
|
|
|
{ 12, GetTotalSpaceSize },
|
|
|
|
//{ 13, CleanDirectoryRecursively },
|
|
|
|
//{ 14, GetFileTimeStampRaw }
|
2018-02-09 16:14:55 -08:00
|
|
|
};
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
OpenPaths = new HashSet<string>();
|
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
this.Path = Path;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
public long CreateFile(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
long Mode = Context.RequestData.ReadInt64();
|
|
|
|
int Size = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (FileName == null)
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (File.Exists(FileName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPathAlreadyInUse(FileName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
using (FileStream NewFile = File.Create(FileName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
|
|
|
NewFile.SetLength(Size);
|
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long DeleteFile(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (!File.Exists(FileName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPathAlreadyInUse(FileName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
File.Delete(FileName);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long CreateDirectory(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
|
|
|
|
|
|
|
|
if (DirName == null)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (Directory.Exists(DirName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (IsPathAlreadyInUse(DirName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
Directory.CreateDirectory(DirName);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long DeleteDirectory(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
return DeleteDirectory(Context, false);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long DeleteDirectoryRecursively(ServiceCtx Context)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return DeleteDirectory(Context, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private long DeleteDirectory(ServiceCtx Context, bool Recursive)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
|
|
|
|
|
|
|
|
if (!Directory.Exists(DirName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPathAlreadyInUse(DirName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
Directory.Delete(DirName, Recursive);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long RenameFile(ServiceCtx Context)
|
|
|
|
{
|
2018-03-03 09:04:58 -08:00
|
|
|
string OldName = ReadUtf8String(Context, 0);
|
|
|
|
string NewName = ReadUtf8String(Context, 1);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
string OldFileName = Context.Ns.VFs.GetFullPath(Path, OldName);
|
|
|
|
string NewFileName = Context.Ns.VFs.GetFullPath(Path, NewName);
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (!File.Exists(OldFileName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (File.Exists(NewFileName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (IsPathAlreadyInUse(OldFileName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
File.Move(OldFileName, NewFileName);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long RenameDirectory(ServiceCtx Context)
|
|
|
|
{
|
2018-03-03 09:04:58 -08:00
|
|
|
string OldName = ReadUtf8String(Context, 0);
|
|
|
|
string NewName = ReadUtf8String(Context, 1);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
|
|
|
|
string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (!Directory.Exists(OldDirName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (Directory.Exists(NewDirName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPathAlreadyInUse(OldDirName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory.Move(OldDirName, NewDirName);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
public long GetEntryType(ServiceCtx Context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (File.Exists(FileName))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
Context.ResponseData.Write(1);
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
else if (Directory.Exists(FileName))
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Context.ResponseData.Write(0);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
public long OpenFile(ServiceCtx Context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
int FilterFlags = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (!File.Exists(FileName))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (IsPathAlreadyInUse(FileName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
FileStream Stream = new FileStream(FileName, FileMode.Open);
|
|
|
|
|
2018-02-23 16:59:38 -08:00
|
|
|
IFile FileInterface = new IFile(Stream, FileName);
|
|
|
|
|
|
|
|
FileInterface.Disposed += RemoveFileInUse;
|
|
|
|
|
|
|
|
lock (OpenPaths)
|
|
|
|
{
|
|
|
|
OpenPaths.Add(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeObject(Context, FileInterface);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-20 03:03:04 -08:00
|
|
|
public long OpenDirectory(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
|
|
|
|
|
|
|
int FilterFlags = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
|
|
|
string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (!Directory.Exists(DirName))
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-02-21 13:56:52 -08:00
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 13:56:52 -08:00
|
|
|
if (IsPathAlreadyInUse(DirName))
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDirectory DirInterface = new IDirectory(DirName, FilterFlags);
|
|
|
|
|
|
|
|
DirInterface.Disposed += RemoveDirectoryInUse;
|
|
|
|
|
|
|
|
lock (OpenPaths)
|
|
|
|
{
|
|
|
|
OpenPaths.Add(DirName);
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeObject(Context, DirInterface);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-09 16:14:55 -08:00
|
|
|
public long Commit(ServiceCtx Context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
public long GetFreeSpaceSize(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetTotalSpaceSize(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long Position = Context.Request.PtrBuff[0].Position;
|
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
string Name = ReadUtf8String(Context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
Context.ResponseData.Write(Context.Ns.VFs.GetDrive().TotalSize);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsPathAlreadyInUse(string Path)
|
|
|
|
{
|
|
|
|
lock (OpenPaths)
|
|
|
|
{
|
|
|
|
return OpenPaths.Contains(Path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveFileInUse(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
IFile FileInterface = (IFile)sender;
|
|
|
|
|
|
|
|
lock (OpenPaths)
|
|
|
|
{
|
2018-02-23 16:59:38 -08:00
|
|
|
FileInterface.Disposed -= RemoveFileInUse;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
OpenPaths.Remove(FileInterface.HostPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveDirectoryInUse(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
IDirectory DirInterface = (IDirectory)sender;
|
|
|
|
|
|
|
|
lock (OpenPaths)
|
|
|
|
{
|
|
|
|
DirInterface.Disposed -= RemoveDirectoryInUse;
|
|
|
|
|
|
|
|
OpenPaths.Remove(DirInterface.HostPath);
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:24:04 -08:00
|
|
|
|
2018-03-03 09:04:58 -08:00
|
|
|
private string ReadUtf8String(ServiceCtx Context, int Index = 0)
|
2018-03-02 21:24:04 -08:00
|
|
|
{
|
2018-03-03 09:04:58 -08:00
|
|
|
long Position = Context.Request.PtrBuff[Index].Position;
|
|
|
|
long Size = Context.Request.PtrBuff[Index].Size;
|
|
|
|
|
2018-03-02 21:24:04 -08:00
|
|
|
using (MemoryStream MS = new MemoryStream())
|
|
|
|
{
|
2018-03-03 09:04:58 -08:00
|
|
|
while (Size-- > 0)
|
2018-03-02 21:24:04 -08:00
|
|
|
{
|
2018-03-03 09:04:58 -08:00
|
|
|
byte Value = Context.Memory.ReadByte(Position++);
|
2018-03-02 21:24:04 -08:00
|
|
|
|
|
|
|
if (Value == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MS.WriteByte(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Encoding.UTF8.GetString(MS.ToArray());
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
}
|