2018-01-16 22:20:12 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-19 10:34:09 -04:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
#include "common/assert.h"
|
2018-02-19 00:32:00 -05:00
|
|
|
#include "common/file_util.h"
|
2018-07-18 21:07:11 -04:00
|
|
|
#include "core/core.h"
|
2018-07-17 15:42:15 -04:00
|
|
|
#include "core/file_sys/errors.h"
|
2018-07-07 23:24:51 -04:00
|
|
|
#include "core/file_sys/savedata_factory.h"
|
|
|
|
#include "core/file_sys/sdmc_factory.h"
|
2018-07-18 21:07:11 -04:00
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
#include "core/file_sys/vfs_offset.h"
|
|
|
|
#include "core/file_sys/vfs_real.h"
|
2018-01-16 22:20:12 -05:00
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
|
|
|
#include "core/hle/service/filesystem/fsp_srv.h"
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::FileSystem {
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
// Size of emulated sd card free space, reported in bytes.
|
|
|
|
// Just using 32GB because thats reasonable
|
|
|
|
// TODO(DarkLordZach): Eventually make this configurable in settings.
|
|
|
|
constexpr u64 EMULATED_SD_REPORTED_SIZE = 32000000000;
|
|
|
|
|
|
|
|
static FileSys::VirtualDir GetDirectoryRelativeWrapped(FileSys::VirtualDir base,
|
|
|
|
const std::string& dir_name) {
|
2018-07-19 10:32:21 -04:00
|
|
|
if (dir_name.empty() || dir_name == "." || dir_name == "/" || dir_name == "\\")
|
2018-07-18 21:07:11 -04:00
|
|
|
return base;
|
|
|
|
|
|
|
|
return base->GetDirectoryRelative(dir_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
VfsDirectoryServiceWrapper::VfsDirectoryServiceWrapper(FileSys::VirtualDir backing_)
|
2018-07-19 10:34:09 -04:00
|
|
|
: backing(std::move(backing_)) {}
|
2018-07-18 21:07:11 -04:00
|
|
|
|
|
|
|
std::string VfsDirectoryServiceWrapper::GetName() const {
|
|
|
|
return backing->GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::CreateFile(const std::string& path, u64 size) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
auto file = dir->CreateFile(FileUtil::GetFilename(path));
|
|
|
|
if (file == nullptr) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
if (!file->Resize(size)) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::DeleteFile(const std::string& path) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
if (path == "/" || path == "\\") {
|
|
|
|
// TODO(DarkLordZach): Why do games call this and what should it do? Works as is but...
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
if (dir->GetFile(FileUtil::GetFilename(path)) == nullptr)
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
if (!backing->DeleteFile(FileUtil::GetFilename(path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::CreateDirectory(const std::string& path) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
if (dir == nullptr && FileUtil::GetFilename(FileUtil::GetParentPath(path)).empty())
|
|
|
|
dir = backing;
|
|
|
|
auto new_dir = dir->CreateSubdirectory(FileUtil::GetFilename(path));
|
|
|
|
if (new_dir == nullptr) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::DeleteDirectory(const std::string& path) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
if (!dir->DeleteSubdirectory(FileUtil::GetFilename(path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::DeleteDirectoryRecursively(const std::string& path) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
if (!dir->DeleteSubdirectoryRecursive(FileUtil::GetFilename(path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::RenameFile(const std::string& src_path,
|
|
|
|
const std::string& dest_path) const {
|
|
|
|
auto src = backing->GetFileRelative(src_path);
|
|
|
|
if (FileUtil::GetParentPath(src_path) == FileUtil::GetParentPath(dest_path)) {
|
|
|
|
// Use more-optimized vfs implementation rename.
|
|
|
|
if (src == nullptr)
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
if (!src->Rename(FileUtil::GetFilename(dest_path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move by hand -- TODO(DarkLordZach): Optimize
|
|
|
|
auto c_res = CreateFile(dest_path, src->GetSize());
|
|
|
|
if (c_res != RESULT_SUCCESS)
|
|
|
|
return c_res;
|
|
|
|
|
|
|
|
auto dest = backing->GetFileRelative(dest_path);
|
|
|
|
ASSERT_MSG(dest != nullptr, "Newly created file with success cannot be found.");
|
|
|
|
|
|
|
|
ASSERT_MSG(dest->WriteBytes(src->ReadAllBytes()) == src->GetSize(),
|
|
|
|
"Could not write all of the bytes but everything else has succeded.");
|
|
|
|
|
|
|
|
if (!src->GetContainingDirectory()->DeleteFile(FileUtil::GetFilename(src_path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode VfsDirectoryServiceWrapper::RenameDirectory(const std::string& src_path,
|
|
|
|
const std::string& dest_path) const {
|
|
|
|
auto src = GetDirectoryRelativeWrapped(backing, src_path);
|
|
|
|
if (FileUtil::GetParentPath(src_path) == FileUtil::GetParentPath(dest_path)) {
|
|
|
|
// Use more-optimized vfs implementation rename.
|
|
|
|
if (src == nullptr)
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
if (!src->Rename(FileUtil::GetFilename(dest_path))) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(DarkLordZach): Implement renaming across the tree (move).
|
|
|
|
ASSERT_MSG(false,
|
|
|
|
"Could not rename directory with path \"{}\" to new path \"{}\" because parent dirs "
|
|
|
|
"don't match -- UNIMPLEMENTED",
|
|
|
|
src_path, dest_path);
|
|
|
|
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal<FileSys::VirtualFile> VfsDirectoryServiceWrapper::OpenFile(const std::string& path,
|
|
|
|
FileSys::Mode mode) const {
|
|
|
|
auto npath = path;
|
|
|
|
while (npath.size() > 0 && (npath[0] == '/' || npath[0] == '\\'))
|
|
|
|
npath = npath.substr(1);
|
|
|
|
auto file = backing->GetFileRelative(npath);
|
|
|
|
if (file == nullptr)
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
|
|
|
|
if (mode == FileSys::Mode::Append) {
|
|
|
|
return MakeResult<FileSys::VirtualFile>(
|
|
|
|
std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeResult<FileSys::VirtualFile>(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const std::string& path) {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, path);
|
|
|
|
if (dir == nullptr) {
|
|
|
|
// TODO(DarkLordZach): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
return MakeResult(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 VfsDirectoryServiceWrapper::GetFreeSpaceSize() const {
|
|
|
|
if (backing->IsWritable())
|
|
|
|
return EMULATED_SD_REPORTED_SIZE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
|
|
|
|
const std::string& path) const {
|
|
|
|
auto dir = GetDirectoryRelativeWrapped(backing, FileUtil::GetParentPath(path));
|
|
|
|
if (dir == nullptr)
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
auto filename = FileUtil::GetFilename(path);
|
2018-07-19 14:11:09 -04:00
|
|
|
// TODO(Subv): Some games use the '/' path, find out what this means.
|
|
|
|
if (filename.empty())
|
|
|
|
return MakeResult(FileSys::EntryType::Directory);
|
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
if (dir->GetFile(filename) != nullptr)
|
|
|
|
return MakeResult(FileSys::EntryType::File);
|
|
|
|
if (dir->GetSubdirectory(filename) != nullptr)
|
|
|
|
return MakeResult(FileSys::EntryType::Directory);
|
|
|
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:20:12 -05:00
|
|
|
/**
|
|
|
|
* Map of registered file systems, identified by type. Once an file system is registered here, it
|
|
|
|
* is never removed until UnregisterFileSystems is called.
|
|
|
|
*/
|
2018-07-17 15:42:15 -04:00
|
|
|
static std::unique_ptr<FileSys::RomFSFactory> romfs_factory;
|
|
|
|
static std::unique_ptr<FileSys::SaveDataFactory> save_data_factory;
|
|
|
|
static std::unique_ptr<FileSys::SDMCFactory> sdmc_factory;
|
|
|
|
|
|
|
|
ResultCode RegisterRomFS(std::unique_ptr<FileSys::RomFSFactory>&& factory) {
|
|
|
|
ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second RomFS");
|
|
|
|
romfs_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered RomFS");
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultCode RegisterSaveData(std::unique_ptr<FileSys::SaveDataFactory>&& factory) {
|
|
|
|
ASSERT_MSG(romfs_factory == nullptr, "Tried to register a second save data");
|
|
|
|
save_data_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered save data");
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
ResultCode RegisterSDMC(std::unique_ptr<FileSys::SDMCFactory>&& factory) {
|
|
|
|
ASSERT_MSG(sdmc_factory == nullptr, "Tried to register a second SDMC");
|
|
|
|
sdmc_factory = std::move(factory);
|
|
|
|
LOG_DEBUG(Service_FS, "Registered SDMC");
|
2018-01-16 22:20:12 -05:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
ResultVal<FileSys::VirtualFile> OpenRomFS(u64 title_id) {
|
2018-07-17 15:42:15 -04:00
|
|
|
LOG_TRACE(Service_FS, "Opening RomFS for title_id={:016X}", title_id);
|
2018-01-16 22:20:12 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
if (romfs_factory == nullptr) {
|
2018-01-16 22:20:12 -05:00
|
|
|
// TODO(bunnei): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
return romfs_factory->Open(title_id);
|
2018-01-16 22:20:12 -05:00
|
|
|
}
|
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
ResultVal<FileSys::VirtualDir> OpenSaveData(FileSys::SaveDataSpaceId space,
|
|
|
|
FileSys::SaveDataDescriptor save_struct) {
|
2018-07-17 15:42:15 -04:00
|
|
|
LOG_TRACE(Service_FS, "Opening Save Data for space_id={:01X}, save_struct={}",
|
2018-07-18 21:07:11 -04:00
|
|
|
static_cast<u8>(space), save_struct.DebugInfo());
|
2018-03-04 13:03:58 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
if (save_data_factory == nullptr) {
|
2018-07-18 21:07:11 -04:00
|
|
|
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::TitleNotFound);
|
2018-07-17 15:42:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return save_data_factory->Open(space, save_struct);
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
ResultVal<FileSys::VirtualDir> OpenSDMC() {
|
2018-07-17 15:42:15 -04:00
|
|
|
LOG_TRACE(Service_FS, "Opening SDMC");
|
|
|
|
|
|
|
|
if (sdmc_factory == nullptr) {
|
|
|
|
return ResultCode(ErrorModule::FS, FileSys::ErrCodes::SdCardNotFound);
|
2018-03-04 13:03:58 -05:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
return sdmc_factory->Open();
|
2018-03-04 13:03:58 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 00:32:00 -05:00
|
|
|
void RegisterFileSystems() {
|
2018-07-17 15:42:15 -04:00
|
|
|
romfs_factory = nullptr;
|
|
|
|
save_data_factory = nullptr;
|
|
|
|
sdmc_factory = nullptr;
|
2018-02-19 00:32:00 -05:00
|
|
|
|
2018-07-18 21:07:11 -04:00
|
|
|
auto nand_directory = std::make_shared<FileSys::RealVfsDirectory>(
|
2018-07-21 15:52:42 -04:00
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::NANDDir), FileSys::Mode::Write);
|
2018-07-18 21:07:11 -04:00
|
|
|
auto sd_directory = std::make_shared<FileSys::RealVfsDirectory>(
|
2018-07-21 15:52:42 -04:00
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir), FileSys::Mode::Write);
|
2018-02-19 00:32:00 -05:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
auto savedata = std::make_unique<FileSys::SaveDataFactory>(std::move(nand_directory));
|
|
|
|
save_data_factory = std::move(savedata);
|
2018-07-07 23:24:51 -04:00
|
|
|
|
2018-07-17 15:42:15 -04:00
|
|
|
auto sdcard = std::make_unique<FileSys::SDMCFactory>(std::move(sd_directory));
|
|
|
|
sdmc_factory = std::move(sdcard);
|
2018-01-16 22:20:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
2018-02-19 00:32:00 -05:00
|
|
|
RegisterFileSystems();
|
2018-01-16 22:20:12 -05:00
|
|
|
std::make_shared<FSP_SRV>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::FileSystem
|