2019-05-31 17:31:10 -07:00
|
|
|
using LibHac.Fs;
|
2018-08-16 16:47:36 -07:00
|
|
|
using Ryujinx.HLE.HOS.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;
|
2019-05-31 17:31:10 -07:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-16 16:47:36 -07:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-11-18 11:37:41 -08:00
|
|
|
using static Ryujinx.HLE.Utilities.StringUtils;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-08-16 16:47:36 -07:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.FspSrv
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-03-19 11:58:46 -07:00
|
|
|
class IFileSystem : IpcService
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-09 16:14:55 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private HashSet<string> _openPaths;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
private LibHac.Fs.IFileSystem _provider;
|
2018-11-18 11:37:41 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
public IFileSystem(LibHac.Fs.IFileSystem provider)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-09 16:14:55 -08:00
|
|
|
{
|
2018-04-24 11:57:39 -07:00
|
|
|
{ 0, CreateFile },
|
|
|
|
{ 1, DeleteFile },
|
|
|
|
{ 2, CreateDirectory },
|
|
|
|
{ 3, DeleteDirectory },
|
|
|
|
{ 4, DeleteDirectoryRecursively },
|
|
|
|
{ 5, RenameFile },
|
|
|
|
{ 6, RenameDirectory },
|
|
|
|
{ 7, GetEntryType },
|
|
|
|
{ 8, OpenFile },
|
|
|
|
{ 9, OpenDirectory },
|
2018-02-21 13:56:52 -08:00
|
|
|
{ 10, Commit },
|
|
|
|
{ 11, GetFreeSpaceSize },
|
|
|
|
{ 12, GetTotalSpaceSize },
|
2019-02-13 16:44:39 -08:00
|
|
|
{ 13, CleanDirectoryRecursively },
|
|
|
|
{ 14, GetFileTimeStampRaw }
|
2018-02-09 16:14:55 -08:00
|
|
|
};
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_openPaths = new HashSet<string>();
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
_provider = provider;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
// CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long CreateFile(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-06-06 15:01:44 -07:00
|
|
|
CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
|
2019-05-31 17:31:10 -07:00
|
|
|
context.RequestData.BaseStream.Position += 4;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
long size = context.RequestData.ReadInt64();
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (name == null)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (_provider.FileExists(name))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
|
|
|
{
|
2019-06-06 15:01:44 -07:00
|
|
|
_provider.CreateFile(name, size, createOption);
|
2019-05-31 17:31:10 -07:00
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long DeleteFile(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.FileExists(name))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
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;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long CreateDirectory(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (name == 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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (_provider.DirectoryExists(name))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_provider.CreateDirectory(name);
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long DeleteDirectory(ServiceCtx context)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
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;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long DeleteDirectoryRecursively(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.DirectoryExists(name))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_provider.DeleteDirectoryRecursively(name);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long RenameFile(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string oldName = ReadUtf8String(context, 0);
|
|
|
|
string newName = ReadUtf8String(context, 1);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (_provider.FileExists(oldName))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (_provider.FileExists(newName))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(oldName))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
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;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long RenameDirectory(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string oldName = ReadUtf8String(context, 0);
|
|
|
|
string newName = ReadUtf8String(context, 1);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.DirectoryExists(oldName))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.DirectoryExists(newName))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyExists);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(oldName))
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
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;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
|
2018-12-06 03:16:24 -08:00
|
|
|
public long GetEntryType(ServiceCtx context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2019-07-01 19:39:22 -07:00
|
|
|
DirectoryEntryType entryType = _provider.GetEntryType(name);
|
2019-05-31 17:31:10 -07:00
|
|
|
|
|
|
|
context.ResponseData.Write((int)entryType);
|
2018-02-21 13:56:52 -08:00
|
|
|
}
|
2019-05-31 17:31:10 -07:00
|
|
|
catch (FileNotFoundException)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
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-11-18 11:37:41 -08:00
|
|
|
// OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
|
2018-12-06 03:16:24 -08:00
|
|
|
public long OpenFile(ServiceCtx context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2019-06-06 15:01:44 -07:00
|
|
|
OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.FileExists(name))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
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
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
IFile fileInterface;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
|
|
|
{
|
2019-06-06 15:01:44 -07:00
|
|
|
LibHac.Fs.IFile file = _provider.OpenFile(name, mode);
|
2018-02-23 16:59:38 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
fileInterface = new IFile(file, name);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
2018-02-23 16:59:38 -08:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
2018-02-23 16:59:38 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
throw;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
fileInterface.Disposed += RemoveFileInUse;
|
2018-11-18 11:37:41 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
lock (_openPaths)
|
|
|
|
{
|
|
|
|
_openPaths.Add(fileInterface.Path);
|
2018-11-18 11:37:41 -08:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
MakeObject(context, fileInterface);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-20 03:03:04 -08:00
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
|
2018-12-06 03:16:24 -08:00
|
|
|
public long OpenDirectory(ServiceCtx context)
|
2018-02-20 03:03:04 -08:00
|
|
|
{
|
2019-06-06 15:01:44 -07:00
|
|
|
OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-20 03:03:04 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.DirectoryExists(name))
|
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
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
2018-11-18 11:37:41 -08:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
IDirectory dirInterface;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-06-06 15:01:44 -07:00
|
|
|
LibHac.Fs.IDirectory dir = _provider.OpenDirectory(name, mode);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
dirInterface = new IDirectory(dir);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
dirInterface.Disposed += RemoveDirectoryInUse;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
lock (_openPaths)
|
|
|
|
{
|
|
|
|
_openPaths.Add(dirInterface.Path);
|
2018-11-18 11:37:41 -08:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
MakeObject(context, dirInterface);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// Commit()
|
2018-12-06 03:16:24 -08:00
|
|
|
public long Commit(ServiceCtx context)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
_provider.Commit();
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
|
2018-12-06 03:16:24 -08:00
|
|
|
public long GetFreeSpaceSize(ServiceCtx context)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
context.ResponseData.Write(_provider.GetFreeSpaceSize(name));
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
|
2018-12-06 03:16:24 -08:00
|
|
|
public long GetTotalSpaceSize(ServiceCtx context)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
context.ResponseData.Write(_provider.GetTotalSpaceSize(name));
|
2018-02-21 13:56:52 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:37:41 -08:00
|
|
|
// CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
|
2018-12-06 03:16:24 -08:00
|
|
|
public long CleanDirectoryRecursively(ServiceCtx context)
|
2018-07-18 12:05:17 -07:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
string name = ReadUtf8String(context);
|
2018-07-18 12:05:17 -07:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (!_provider.DirectoryExists(name))
|
2018-07-18 12:05:17 -07:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (IsPathAlreadyInUse(name))
|
2018-07-18 12:05:17 -07:00
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathAlreadyInUse);
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
try
|
2018-07-18 12:05:17 -07:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
_provider.CleanDirectoryRecursively(name);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
Logger.PrintError(LogClass.ServiceFs, $"Unable to access {name}");
|
|
|
|
|
|
|
|
throw;
|
2018-07-18 12:05:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:44:39 -08:00
|
|
|
// GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
|
|
|
|
public long GetFileTimeStampRaw(ServiceCtx context)
|
|
|
|
{
|
|
|
|
string name = ReadUtf8String(context);
|
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
if (_provider.FileExists(name) || _provider.DirectoryExists(name))
|
2019-02-13 16:44:39 -08:00
|
|
|
{
|
2019-05-31 17:31:10 -07:00
|
|
|
FileTimeStampRaw timestamp = _provider.GetFileTimeStampRaw(name);
|
2019-02-13 16:44:39 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
context.ResponseData.Write(timestamp.Created);
|
|
|
|
context.ResponseData.Write(timestamp.Modified);
|
|
|
|
context.ResponseData.Write(timestamp.Accessed);
|
2019-02-13 16:44:39 -08:00
|
|
|
|
|
|
|
byte[] data = new byte[8];
|
|
|
|
|
|
|
|
// is valid?
|
|
|
|
data[0] = 1;
|
|
|
|
|
|
|
|
context.ResponseData.Write(data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
private bool IsPathAlreadyInUse(string path)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_openPaths)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
return _openPaths.Contains(path);
|
2018-02-21 13:56:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveFileInUse(object sender, EventArgs e)
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
IFile fileInterface = (IFile)sender;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_openPaths)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
fileInterface.Disposed -= RemoveFileInUse;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
_openPaths.Remove(fileInterface.Path);
|
2018-02-21 13:56:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveDirectoryInUse(object sender, EventArgs e)
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
IDirectory dirInterface = (IDirectory)sender;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
lock (_openPaths)
|
2018-02-21 13:56:52 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
dirInterface.Disposed -= RemoveDirectoryInUse;
|
2018-02-21 13:56:52 -08:00
|
|
|
|
2019-05-31 17:31:10 -07:00
|
|
|
_openPaths.Remove(dirInterface.Path);
|
2018-03-02 21:24:04 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
}
|