fs/archive: wrap states into archive manager
This commit is contained in:
@ -8,9 +8,7 @@
|
||||
#include <memory>
|
||||
#include <system_error>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include "common/assert.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/file_util.h"
|
||||
@ -32,27 +30,13 @@
|
||||
|
||||
namespace Service::FS {
|
||||
|
||||
using FileSys::ArchiveBackend;
|
||||
using FileSys::ArchiveFactory;
|
||||
|
||||
/**
|
||||
* Map of registered archives, identified by id code. Once an archive is registered here, it is
|
||||
* never removed until UnregisterArchiveTypes is called.
|
||||
*/
|
||||
static boost::container::flat_map<ArchiveIdCode, std::unique_ptr<ArchiveFactory>> id_code_map;
|
||||
|
||||
/**
|
||||
* Map of active archive handles. Values are pointers to the archives in `idcode_map`.
|
||||
*/
|
||||
static std::unordered_map<ArchiveHandle, std::unique_ptr<ArchiveBackend>> handle_map;
|
||||
static ArchiveHandle next_handle;
|
||||
|
||||
static ArchiveBackend* GetArchive(ArchiveHandle handle) {
|
||||
ArchiveBackend* ArchiveManager::GetArchive(ArchiveHandle handle) {
|
||||
auto itr = handle_map.find(handle);
|
||||
return (itr == handle_map.end()) ? nullptr : itr->second.get();
|
||||
}
|
||||
|
||||
ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path) {
|
||||
ResultVal<ArchiveHandle> ArchiveManager::OpenArchive(ArchiveIdCode id_code,
|
||||
FileSys::Path& archive_path) {
|
||||
LOG_TRACE(Service_FS, "Opening archive with id code 0x{:08X}", static_cast<u32>(id_code));
|
||||
|
||||
auto itr = id_code_map.find(id_code);
|
||||
@ -70,7 +54,7 @@ ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archi
|
||||
return MakeResult<ArchiveHandle>(next_handle++);
|
||||
}
|
||||
|
||||
ResultCode CloseArchive(ArchiveHandle handle) {
|
||||
ResultCode ArchiveManager::CloseArchive(ArchiveHandle handle) {
|
||||
if (handle_map.erase(handle) == 0)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
else
|
||||
@ -79,8 +63,8 @@ ResultCode CloseArchive(ArchiveHandle handle) {
|
||||
|
||||
// TODO(yuriks): This might be what the fs:REG service is for. See the Register/Unregister calls in
|
||||
// http://3dbrew.org/wiki/Filesystem_services#ProgramRegistry_service_.22fs:REG.22
|
||||
ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
|
||||
ArchiveIdCode id_code) {
|
||||
ResultCode ArchiveManager::RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
|
||||
ArchiveIdCode id_code) {
|
||||
auto result = id_code_map.emplace(id_code, std::move(factory));
|
||||
|
||||
bool inserted = result.second;
|
||||
@ -92,9 +76,9 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path,
|
||||
const FileSys::Mode mode) {
|
||||
ResultVal<std::shared_ptr<File>> ArchiveManager::OpenFileFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path,
|
||||
const FileSys::Mode mode) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -107,7 +91,8 @@ ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handl
|
||||
return MakeResult<std::shared_ptr<File>>(std::move(file));
|
||||
}
|
||||
|
||||
ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
|
||||
ResultCode ArchiveManager::DeleteFileFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -115,10 +100,10 @@ ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Pa
|
||||
return archive->DeleteFile(path);
|
||||
}
|
||||
|
||||
ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
const FileSys::Path& src_path,
|
||||
ArchiveHandle dest_archive_handle,
|
||||
const FileSys::Path& dest_path) {
|
||||
ResultCode ArchiveManager::RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
const FileSys::Path& src_path,
|
||||
ArchiveHandle dest_archive_handle,
|
||||
const FileSys::Path& dest_path) {
|
||||
ArchiveBackend* src_archive = GetArchive(src_archive_handle);
|
||||
ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
|
||||
if (src_archive == nullptr || dest_archive == nullptr)
|
||||
@ -132,7 +117,8 @@ ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
}
|
||||
}
|
||||
|
||||
ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
|
||||
ResultCode ArchiveManager::DeleteDirectoryFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -140,8 +126,8 @@ ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSy
|
||||
return archive->DeleteDirectory(path);
|
||||
}
|
||||
|
||||
ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ResultCode ArchiveManager::DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -149,8 +135,8 @@ ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
|
||||
return archive->DeleteDirectoryRecursively(path);
|
||||
}
|
||||
|
||||
ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
|
||||
u64 file_size) {
|
||||
ResultCode ArchiveManager::CreateFileInArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path, u64 file_size) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -158,7 +144,8 @@ ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path
|
||||
return archive->CreateFile(path, file_size);
|
||||
}
|
||||
|
||||
ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
|
||||
ResultCode ArchiveManager::CreateDirectoryFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -166,10 +153,10 @@ ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSy
|
||||
return archive->CreateDirectory(path);
|
||||
}
|
||||
|
||||
ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
const FileSys::Path& src_path,
|
||||
ArchiveHandle dest_archive_handle,
|
||||
const FileSys::Path& dest_path) {
|
||||
ResultCode ArchiveManager::RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
const FileSys::Path& src_path,
|
||||
ArchiveHandle dest_archive_handle,
|
||||
const FileSys::Path& dest_path) {
|
||||
ArchiveBackend* src_archive = GetArchive(src_archive_handle);
|
||||
ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
|
||||
if (src_archive == nullptr || dest_archive == nullptr)
|
||||
@ -183,8 +170,8 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
|
||||
}
|
||||
}
|
||||
|
||||
ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
|
||||
const FileSys::Path& path) {
|
||||
ResultVal<std::shared_ptr<Directory>> ArchiveManager::OpenDirectoryFromArchive(
|
||||
ArchiveHandle archive_handle, const FileSys::Path& path) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
@ -197,15 +184,16 @@ ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle arc
|
||||
return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
|
||||
}
|
||||
|
||||
ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {
|
||||
ResultVal<u64> ArchiveManager::GetFreeBytesInArchive(ArchiveHandle archive_handle) {
|
||||
ArchiveBackend* archive = GetArchive(archive_handle);
|
||||
if (archive == nullptr)
|
||||
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
|
||||
return MakeResult<u64>(archive->GetFreeBytes());
|
||||
}
|
||||
|
||||
ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo& format_info,
|
||||
const FileSys::Path& path) {
|
||||
ResultCode ArchiveManager::FormatArchive(ArchiveIdCode id_code,
|
||||
const FileSys::ArchiveFormatInfo& format_info,
|
||||
const FileSys::Path& path) {
|
||||
auto archive_itr = id_code_map.find(id_code);
|
||||
if (archive_itr == id_code_map.end()) {
|
||||
return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
|
||||
@ -214,8 +202,8 @@ ResultCode FormatArchive(ArchiveIdCode id_code, const FileSys::ArchiveFormatInfo
|
||||
return archive_itr->second->Format(path, format_info);
|
||||
}
|
||||
|
||||
ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code,
|
||||
FileSys::Path& archive_path) {
|
||||
ResultVal<FileSys::ArchiveFormatInfo> ArchiveManager::GetArchiveFormatInfo(
|
||||
ArchiveIdCode id_code, FileSys::Path& archive_path) {
|
||||
auto archive = id_code_map.find(id_code);
|
||||
if (archive == id_code_map.end()) {
|
||||
return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
|
||||
@ -224,9 +212,9 @@ ResultVal<FileSys::ArchiveFormatInfo> GetArchiveFormatInfo(ArchiveIdCode id_code
|
||||
return archive->second->GetFormatInfo(archive_path);
|
||||
}
|
||||
|
||||
ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low,
|
||||
const std::vector<u8>& smdh_icon,
|
||||
const FileSys::ArchiveFormatInfo& format_info) {
|
||||
ResultCode ArchiveManager::CreateExtSaveData(MediaType media_type, u32 high, u32 low,
|
||||
const std::vector<u8>& smdh_icon,
|
||||
const FileSys::ArchiveFormatInfo& format_info) {
|
||||
// Construct the binary path to the archive first
|
||||
FileSys::Path path =
|
||||
FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
|
||||
@ -248,7 +236,7 @@ ResultCode CreateExtSaveData(MediaType media_type, u32 high, u32 low,
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
|
||||
ResultCode ArchiveManager::DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
|
||||
// Construct the binary path to the archive first
|
||||
FileSys::Path path =
|
||||
FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
|
||||
@ -272,7 +260,7 @@ ResultCode DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ResultCode DeleteSystemSaveData(u32 high, u32 low) {
|
||||
ResultCode ArchiveManager::DeleteSystemSaveData(u32 high, u32 low) {
|
||||
// Construct the binary path to the archive first
|
||||
FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
|
||||
|
||||
@ -284,7 +272,7 @@ ResultCode DeleteSystemSaveData(u32 high, u32 low) {
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ResultCode CreateSystemSaveData(u32 high, u32 low) {
|
||||
ResultCode ArchiveManager::CreateSystemSaveData(u32 high, u32 low) {
|
||||
// Construct the binary path to the archive first
|
||||
FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
|
||||
|
||||
@ -296,7 +284,7 @@ ResultCode CreateSystemSaveData(u32 high, u32 low) {
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
void RegisterArchiveTypes() {
|
||||
void ArchiveManager::RegisterArchiveTypes() {
|
||||
// TODO(Subv): Add the other archive types (see here for the known types:
|
||||
// http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes).
|
||||
|
||||
@ -348,7 +336,7 @@ void RegisterArchiveTypes() {
|
||||
RegisterArchiveType(std::move(selfncch_factory), ArchiveIdCode::SelfNCCH);
|
||||
}
|
||||
|
||||
void RegisterSelfNCCH(Loader::AppLoader& app_loader) {
|
||||
void ArchiveManager::RegisterSelfNCCH(Loader::AppLoader& app_loader) {
|
||||
auto itr = id_code_map.find(ArchiveIdCode::SelfNCCH);
|
||||
if (itr == id_code_map.end()) {
|
||||
LOG_ERROR(Service_FS,
|
||||
@ -360,20 +348,8 @@ void RegisterSelfNCCH(Loader::AppLoader& app_loader) {
|
||||
factory->Register(app_loader);
|
||||
}
|
||||
|
||||
void UnregisterArchiveTypes() {
|
||||
id_code_map.clear();
|
||||
}
|
||||
|
||||
/// Initialize archives
|
||||
void ArchiveInit() {
|
||||
next_handle = 1;
|
||||
ArchiveManager::ArchiveManager() {
|
||||
RegisterArchiveTypes();
|
||||
}
|
||||
|
||||
/// Shutdown archives
|
||||
void ArchiveShutdown() {
|
||||
handle_map.clear();
|
||||
UnregisterArchiveTypes();
|
||||
}
|
||||
|
||||
} // namespace Service::FS
|
||||
|
Reference in New Issue
Block a user