| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- using ChocolArm64.Memory;
- using Ryujinx.Core.OsHle.Ipc;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using static Ryujinx.Core.OsHle.Objects.ErrorCode;
- using static Ryujinx.Core.OsHle.Objects.ObjHelper;
- namespace Ryujinx.Core.OsHle.Objects.FspSrv
- {
- class IFileSystem : IIpcInterface
- {
- private Dictionary<int, ServiceProcessRequest> m_Commands;
- public IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
- private HashSet<string> OpenPaths;
- private string Path;
- public IFileSystem(string Path)
- {
- m_Commands = new Dictionary<int, ServiceProcessRequest>()
- {
- { 0, CreateFile },
- { 1, DeleteFile },
- { 2, CreateDirectory },
- { 3, DeleteDirectory },
- { 4, DeleteDirectoryRecursively },
- { 5, RenameFile },
- { 6, RenameDirectory },
- { 7, GetEntryType },
- { 8, OpenFile },
- { 9, OpenDirectory },
- { 10, Commit },
- { 11, GetFreeSpaceSize },
- { 12, GetTotalSpaceSize },
- //{ 13, CleanDirectoryRecursively },
- //{ 14, GetFileTimeStampRaw }
- };
- OpenPaths = new HashSet<string>();
- this.Path = Path;
- }
- public long CreateFile(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- long Mode = Context.RequestData.ReadInt64();
- int Size = Context.RequestData.ReadInt32();
- string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
- 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))
- {
- NewFile.SetLength(Size);
- }
- return 0;
- }
- public long DeleteFile(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (!File.Exists(FileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(FileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- File.Delete(FileName);
- return 0;
- }
- public long CreateDirectory(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (DirName == null)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (Directory.Exists(DirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(DirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- Directory.CreateDirectory(DirName);
- return 0;
- }
- public long DeleteDirectory(ServiceCtx Context)
- {
- return DeleteDirectory(Context, false);
- }
- public long DeleteDirectoryRecursively(ServiceCtx Context)
- {
- return DeleteDirectory(Context, true);
- }
- private long DeleteDirectory(ServiceCtx Context, bool Recursive)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (!Directory.Exists(DirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(DirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- Directory.Delete(DirName, Recursive);
- return 0;
- }
- public long RenameFile(ServiceCtx Context)
- {
- long OldPosition = Context.Request.PtrBuff[0].Position;
- long NewPosition = Context.Request.PtrBuff[0].Position;
- string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
- string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
- string OldFileName = Context.Ns.VFs.GetFullPath(Path, OldName);
- string NewFileName = Context.Ns.VFs.GetFullPath(Path, NewName);
- if (!File.Exists(OldFileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (File.Exists(NewFileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(OldFileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- File.Move(OldFileName, NewFileName);
- return 0;
- }
- public long RenameDirectory(ServiceCtx Context)
- {
- long OldPosition = Context.Request.PtrBuff[0].Position;
- long NewPosition = Context.Request.PtrBuff[0].Position;
- string OldName = AMemoryHelper.ReadAsciiString(Context.Memory, OldPosition);
- string NewName = AMemoryHelper.ReadAsciiString(Context.Memory, NewPosition);
- string OldDirName = Context.Ns.VFs.GetFullPath(Path, OldName);
- string NewDirName = Context.Ns.VFs.GetFullPath(Path, NewName);
- if (!Directory.Exists(OldDirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- 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;
- }
- public long GetEntryType(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (File.Exists(FileName))
- {
- Context.ResponseData.Write(1);
- }
- else if (Directory.Exists(FileName))
- {
- Context.ResponseData.Write(0);
- }
- else
- {
- Context.ResponseData.Write(0);
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- return 0;
- }
- public long OpenFile(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- int FilterFlags = Context.RequestData.ReadInt32();
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string FileName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (!File.Exists(FileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(FileName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- FileStream Stream = new FileStream(FileName, FileMode.Open);
- IFile FileInterface = new IFile(Stream, FileName);
- FileInterface.Disposed += RemoveFileInUse;
- lock (OpenPaths)
- {
- OpenPaths.Add(FileName);
- }
- MakeObject(Context, FileInterface);
- return 0;
- }
- public long OpenDirectory(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- int FilterFlags = Context.RequestData.ReadInt32();
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- string DirName = Context.Ns.VFs.GetFullPath(Path, Name);
- if (!Directory.Exists(DirName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- 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;
- }
- public long Commit(ServiceCtx Context)
- {
- return 0;
- }
- public long GetFreeSpaceSize(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- Context.ResponseData.Write(Context.Ns.VFs.GetDrive().AvailableFreeSpace);
- return 0;
- }
- public long GetTotalSpaceSize(ServiceCtx Context)
- {
- long Position = Context.Request.PtrBuff[0].Position;
- string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
- 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)
- {
- FileInterface.Disposed -= RemoveFileInUse;
- OpenPaths.Remove(FileInterface.HostPath);
- }
- }
- private void RemoveDirectoryInUse(object sender, EventArgs e)
- {
- IDirectory DirInterface = (IDirectory)sender;
- lock (OpenPaths)
- {
- DirInterface.Disposed -= RemoveDirectoryInUse;
- OpenPaths.Remove(DirInterface.HostPath);
- }
- }
- }
- }
|