2018-01-17 03:20:12 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
#include <boost/container/flat_map.hpp>
|
2018-02-19 05:32:00 +00:00
|
|
|
#include "common/file_util.h"
|
2018-01-17 03:20:12 +00:00
|
|
|
#include "core/file_sys/filesystem.h"
|
2018-07-08 03:24:51 +00:00
|
|
|
#include "core/file_sys/savedata_factory.h"
|
|
|
|
#include "core/file_sys/sdmc_factory.h"
|
2018-01-17 03:20:12 +00:00
|
|
|
#include "core/hle/service/filesystem/filesystem.h"
|
|
|
|
#include "core/hle/service/filesystem/fsp_srv.h"
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::FileSystem {
|
2018-01-17 03:20:12 +00: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-08 03:24:51 +00:00
|
|
|
static boost::container::flat_map<Type, std::unique_ptr<FileSys::FileSystemFactory>> filesystem_map;
|
2018-01-17 03:20:12 +00:00
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
ResultCode RegisterFileSystem(std::unique_ptr<FileSys::FileSystemFactory>&& factory, Type type) {
|
2018-01-17 03:20:12 +00:00
|
|
|
auto result = filesystem_map.emplace(type, std::move(factory));
|
|
|
|
|
|
|
|
bool inserted = result.second;
|
|
|
|
ASSERT_MSG(inserted, "Tried to register more than one system with same id code");
|
|
|
|
|
|
|
|
auto& filesystem = result.first->second;
|
2018-07-02 16:20:50 +00:00
|
|
|
LOG_DEBUG(Service_FS, "Registered file system {} with id code 0x{:08X}", filesystem->GetName(),
|
2018-07-08 03:24:51 +00:00
|
|
|
static_cast<u32>(type));
|
2018-01-17 03:20:12 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
ResultVal<std::unique_ptr<FileSys::FileSystemBackend>> OpenFileSystem(Type type,
|
|
|
|
FileSys::Path& path) {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_TRACE(Service_FS, "Opening FileSystem with type={}", static_cast<u32>(type));
|
2018-01-17 03:20:12 +00:00
|
|
|
|
|
|
|
auto itr = filesystem_map.find(type);
|
|
|
|
if (itr == filesystem_map.end()) {
|
|
|
|
// TODO(bunnei): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
return itr->second->Open(path);
|
2018-01-17 03:20:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 18:03:58 +00:00
|
|
|
ResultCode FormatFileSystem(Type type) {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_TRACE(Service_FS, "Formatting FileSystem with type={}", static_cast<u32>(type));
|
2018-03-04 18:03:58 +00:00
|
|
|
|
|
|
|
auto itr = filesystem_map.find(type);
|
|
|
|
if (itr == filesystem_map.end()) {
|
|
|
|
// TODO(bunnei): Find a better error code for this
|
|
|
|
return ResultCode(-1);
|
|
|
|
}
|
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
FileSys::Path unused;
|
|
|
|
return itr->second->Format(unused);
|
2018-03-04 18:03:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 05:32:00 +00:00
|
|
|
void RegisterFileSystems() {
|
2018-01-17 03:20:12 +00:00
|
|
|
filesystem_map.clear();
|
2018-02-19 05:32:00 +00:00
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
std::string nand_directory = FileUtil::GetUserPath(D_NAND_IDX);
|
2018-03-20 02:17:15 +00:00
|
|
|
std::string sd_directory = FileUtil::GetUserPath(D_SDMC_IDX);
|
2018-02-19 05:32:00 +00:00
|
|
|
|
2018-07-08 03:24:51 +00:00
|
|
|
auto savedata = std::make_unique<FileSys::SaveData_Factory>(std::move(nand_directory));
|
|
|
|
RegisterFileSystem(std::move(savedata), Type::SaveData);
|
|
|
|
|
|
|
|
auto sdcard = std::make_unique<FileSys::SDMC_Factory>(std::move(sd_directory));
|
|
|
|
RegisterFileSystem(std::move(sdcard), Type::SDMC);
|
2018-01-17 03:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
2018-02-19 05:32:00 +00:00
|
|
|
RegisterFileSystems();
|
2018-01-17 03:20:12 +00:00
|
|
|
std::make_shared<FSP_SRV>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
} // namespace Service::FileSystem
|