| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- using LibHac.Fs;
- using Ryujinx.HLE.HOS.Ipc;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Ryujinx.Common.Logging;
- using static Ryujinx.HLE.HOS.ErrorCode;
- using static Ryujinx.HLE.Utilities.StringUtils;
- namespace Ryujinx.HLE.HOS.Services.FspSrv
- {
- class IFileSystem : IpcService
- {
- private Dictionary<int, ServiceProcessRequest> _commands;
- public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
- private HashSet<string> _openPaths;
- private LibHac.Fs.IFileSystem _provider;
- public IFileSystem(LibHac.Fs.IFileSystem provider)
- {
- _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>();
- _provider = provider;
- }
- // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
- public long CreateFile(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
- context.RequestData.BaseStream.Position += 4;
- long size = context.RequestData.ReadInt64();
- if (name == null)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (_provider.FileExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.CreateFile(name, size, createOption);
- }
- catch (DirectoryNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
- public long DeleteFile(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (!_provider.FileExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.DeleteFile(name);
- }
- catch (FileNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
- public long CreateDirectory(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (name == null)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (_provider.DirectoryExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.CreateDirectory(name);
- }
- catch (DirectoryNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
- public long DeleteDirectory(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (!_provider.DirectoryExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.DeleteDirectory(name);
- }
- catch (DirectoryNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
- public long DeleteDirectoryRecursively(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (!_provider.DirectoryExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.DeleteDirectoryRecursively(name);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
- public long RenameFile(ServiceCtx context)
- {
- string oldName = ReadUtf8String(context, 0);
- string newName = ReadUtf8String(context, 1);
- if (_provider.FileExists(oldName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (_provider.FileExists(newName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(oldName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.RenameFile(oldName, newName);
- }
- catch (FileNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {oldName} or {newName}");
- throw;
- }
- return 0;
- }
- // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
- public long RenameDirectory(ServiceCtx context)
- {
- string oldName = ReadUtf8String(context, 0);
- string newName = ReadUtf8String(context, 1);
- if (!_provider.DirectoryExists(oldName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (!_provider.DirectoryExists(newName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
- }
- if (IsPathAlreadyInUse(oldName))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.RenameFile(oldName, newName);
- }
- catch (DirectoryNotFoundException)
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {oldName} or {newName}");
- throw;
- }
- return 0;
- }
- // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
- public long GetEntryType(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- try
- {
- LibHac.Fs.DirectoryEntryType entryType = _provider.GetEntryType(name);
- context.ResponseData.Write((int)entryType);
- }
- catch (FileNotFoundException)
- {
- context.ResponseData.Write(0);
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- return 0;
- }
- // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
- public long OpenFile(ServiceCtx context)
- {
- OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
- string name = ReadUtf8String(context);
- if (!_provider.FileExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- IFile fileInterface;
- try
- {
- LibHac.Fs.IFile file = _provider.OpenFile(name, mode);
- fileInterface = new IFile(file, name);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- fileInterface.Disposed += RemoveFileInUse;
- lock (_openPaths)
- {
- _openPaths.Add(fileInterface.Path);
- }
- MakeObject(context, fileInterface);
- return 0;
- }
- // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
- public long OpenDirectory(ServiceCtx context)
- {
- OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
- string name = ReadUtf8String(context);
- if (!_provider.DirectoryExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- IDirectory dirInterface;
- try
- {
- LibHac.Fs.IDirectory dir = _provider.OpenDirectory(name, mode);
- dirInterface = new IDirectory(dir);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- dirInterface.Disposed += RemoveDirectoryInUse;
- lock (_openPaths)
- {
- _openPaths.Add(dirInterface.Path);
- }
- MakeObject(context, dirInterface);
- return 0;
- }
- // Commit()
- public long Commit(ServiceCtx context)
- {
- _provider.Commit();
- return 0;
- }
- // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
- public long GetFreeSpaceSize(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- context.ResponseData.Write(_provider.GetFreeSpaceSize(name));
- return 0;
- }
- // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
- public long GetTotalSpaceSize(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- context.ResponseData.Write(_provider.GetTotalSpaceSize(name));
- return 0;
- }
- // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
- public long CleanDirectoryRecursively(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (!_provider.DirectoryExists(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- if (IsPathAlreadyInUse(name))
- {
- return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
- }
- try
- {
- _provider.CleanDirectoryRecursively(name);
- }
- catch (UnauthorizedAccessException)
- {
- Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
- throw;
- }
- return 0;
- }
- // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
- public long GetFileTimeStampRaw(ServiceCtx context)
- {
- string name = ReadUtf8String(context);
- if (_provider.FileExists(name) || _provider.DirectoryExists(name))
- {
- FileTimeStampRaw timestamp = _provider.GetFileTimeStampRaw(name);
- context.ResponseData.Write(timestamp.Created);
- context.ResponseData.Write(timestamp.Modified);
- context.ResponseData.Write(timestamp.Accessed);
- byte[] data = new byte[8];
- // is valid?
- data[0] = 1;
- context.ResponseData.Write(data);
- return 0;
- }
- return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
- }
- 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.Path);
- }
- }
- private void RemoveDirectoryInUse(object sender, EventArgs e)
- {
- IDirectory dirInterface = (IDirectory)sender;
- lock (_openPaths)
- {
- dirInterface.Disposed -= RemoveDirectoryInUse;
- _openPaths.Remove(dirInterface.Path);
- }
- }
- }
- }
|