fsmitm: Cleanup and modernize fsmitm port

This commit is contained in:
Zach Hilman 2018-09-23 21:50:16 -04:00
parent ba0873d33c
commit b3c2ec362b
22 changed files with 379 additions and 379 deletions

View file

@ -33,7 +33,7 @@ add_library(core STATIC
file_sys/directory.h file_sys/directory.h
file_sys/errors.h file_sys/errors.h
file_sys/fsmitm_romfsbuild.cpp file_sys/fsmitm_romfsbuild.cpp
file_sys/fsmitm_romfsbuild.hpp file_sys/fsmitm_romfsbuild.h
file_sys/mode.h file_sys/mode.h
file_sys/nca_metadata.cpp file_sys/nca_metadata.cpp
file_sys/nca_metadata.h file_sys/nca_metadata.h

View file

@ -2,9 +2,9 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <fmt/format.h>
#include "core/file_sys/bis_factory.h" #include "core/file_sys/bis_factory.h"
#include "core/file_sys/registered_cache.h" #include "core/file_sys/registered_cache.h"
#include "fmt/format.h"
namespace FileSys { namespace FileSys {

View file

@ -17,7 +17,7 @@ class RegisteredCache;
/// registered caches. /// registered caches.
class BISFactory { class BISFactory {
public: public:
BISFactory(VirtualDir nand_root, VirtualDir load_root); explicit BISFactory(VirtualDir nand_root, VirtualDir load_root);
~BISFactory(); ~BISFactory();
std::shared_ptr<RegisteredCache> GetSystemNANDContents() const; std::shared_ptr<RegisteredCache> GetSystemNANDContents() const;

View file

@ -24,9 +24,9 @@
#include <cstring> #include <cstring>
#include "common/assert.h" #include "common/assert.h"
#include "fsmitm_romfsbuild.hpp" #include "core/file_sys/fsmitm_romfsbuild.h"
#include "vfs.h" #include "core/file_sys/vfs.h"
#include "vfs_vector.h" #include "core/file_sys/vfs_vector.h"
namespace FileSys { namespace FileSys {
@ -35,7 +35,7 @@ constexpr u64 FS_MAX_PATH = 0x301;
constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF; constexpr u32 ROMFS_ENTRY_EMPTY = 0xFFFFFFFF;
constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200; constexpr u32 ROMFS_FILEPARTITION_OFS = 0x200;
/* Types for building a RomFS. */ // Types for building a RomFS.
struct RomFSHeader { struct RomFSHeader {
u64 header_size; u64 header_size;
u64 dir_hash_table_ofs; u64 dir_hash_table_ofs;
@ -57,7 +57,6 @@ struct RomFSDirectoryEntry {
u32 file; u32 file;
u32 hash; u32 hash;
u32 name_size; u32 name_size;
char name[];
}; };
static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size."); static_assert(sizeof(RomFSDirectoryEntry) == 0x18, "RomFSDirectoryEntry has incorrect size.");
@ -68,37 +67,63 @@ struct RomFSFileEntry {
u64 size; u64 size;
u32 hash; u32 hash;
u32 name_size; u32 name_size;
char name[];
}; };
static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size."); static_assert(sizeof(RomFSFileEntry) == 0x20, "RomFSFileEntry has incorrect size.");
struct RomFSBuildFileContext; struct RomFSBuildFileContext;
struct RomFSBuildDirectoryContext { struct RomFSBuildDirectoryContext {
char* path; std::string path = "";
u32 cur_path_ofs; u32 cur_path_ofs = 0;
u32 path_len; u32 path_len = 0;
u32 entry_offset = 0; u32 entry_offset = 0;
RomFSBuildDirectoryContext* parent = nullptr; std::shared_ptr<RomFSBuildDirectoryContext> parent;
RomFSBuildDirectoryContext* child = nullptr; std::shared_ptr<RomFSBuildDirectoryContext> child;
RomFSBuildDirectoryContext* sibling = nullptr; std::shared_ptr<RomFSBuildDirectoryContext> sibling;
RomFSBuildFileContext* file = nullptr; std::shared_ptr<RomFSBuildFileContext> file;
}; };
struct RomFSBuildFileContext { struct RomFSBuildFileContext {
char* path; std::string path = "";
u32 cur_path_ofs; u32 cur_path_ofs = 0;
u32 path_len; u32 path_len = 0;
u32 entry_offset = 0; u32 entry_offset = 0;
u64 offset = 0; u64 offset = 0;
u64 size = 0; u64 size = 0;
RomFSBuildDirectoryContext* parent = nullptr; std::shared_ptr<RomFSBuildDirectoryContext> parent;
RomFSBuildFileContext* sibling = nullptr; std::shared_ptr<RomFSBuildFileContext> sibling;
VirtualFile source = nullptr; VirtualFile source = nullptr;
RomFSBuildFileContext() : path(""), cur_path_ofs(0), path_len(0) {}
}; };
void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirectoryContext* parent) { static u32 romfs_calc_path_hash(u32 parent, std::string path, u32 start, size_t path_len) {
std::vector<RomFSBuildDirectoryContext*> child_dirs; u32 hash = parent ^ 123456789;
for (u32 i = 0; i < path_len; i++) {
hash = (hash >> 5) | (hash << 27);
hash ^= path[start + i];
}
return hash;
}
static u32 romfs_get_hash_table_count(u32 num_entries) {
if (num_entries < 3) {
return 3;
} else if (num_entries < 19) {
return num_entries | 1;
}
u32 count = num_entries;
while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
count++;
}
return count;
}
void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs,
std::shared_ptr<RomFSBuildDirectoryContext> parent) {
std::vector<std::shared_ptr<RomFSBuildDirectoryContext>> child_dirs;
VirtualDir dir; VirtualDir dir;
@ -111,41 +136,33 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirector
for (const auto& kv : entries) { for (const auto& kv : entries) {
if (kv.second == VfsEntryType::Directory) { if (kv.second == VfsEntryType::Directory) {
RomFSBuildDirectoryContext* child = new RomFSBuildDirectoryContext({0}); const auto child = std::make_shared<RomFSBuildDirectoryContext>();
/* Set child's path. */ // Set child's path.
child->cur_path_ofs = parent->path_len + 1; child->cur_path_ofs = parent->path_len + 1;
child->path_len = child->cur_path_ofs + kv.first.size(); child->path_len = child->cur_path_ofs + kv.first.size();
child->path = new char[child->path_len + 1]; child->path = parent->path + "/" + kv.first;
strcpy(child->path, parent->path);
ASSERT(child->path_len < FS_MAX_PATH);
strcat(child->path + parent->path_len, "/");
strcat(child->path + parent->path_len, kv.first.c_str());
if (!this->AddDirectory(parent, child, nullptr)) { // Sanity check on path_len
delete child->path; ASSERT(child->path_len < FS_MAX_PATH);
delete child;
} else { if (AddDirectory(parent, child)) {
child_dirs.push_back(child); child_dirs.push_back(child);
} }
} else { } else {
RomFSBuildFileContext* child = new RomFSBuildFileContext({0}); const auto child = std::make_shared<RomFSBuildFileContext>();
/* Set child's path. */ // Set child's path.
child->cur_path_ofs = parent->path_len + 1; child->cur_path_ofs = parent->path_len + 1;
child->path_len = child->cur_path_ofs + kv.first.size(); child->path_len = child->cur_path_ofs + kv.first.size();
child->path = new char[child->path_len + 1]; child->path = parent->path + "/" + kv.first;
strcpy(child->path, parent->path);
// Sanity check on path_len
ASSERT(child->path_len < FS_MAX_PATH); ASSERT(child->path_len < FS_MAX_PATH);
strcat(child->path + parent->path_len, "/");
strcat(child->path + parent->path_len, kv.first.c_str());
child->source = root_romfs->GetFileRelative(child->path); child->source = root_romfs->GetFileRelative(child->path);
child->size = child->source->GetSize(); child->size = child->source->GetSize();
if (!this->AddFile(parent, child)) { AddFile(parent, child);
delete child->path;
delete child;
}
} }
} }
@ -154,213 +171,200 @@ void RomFSBuildContext::VisitDirectory(VirtualDir root_romfs, RomFSBuildDirector
} }
} }
bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext* parent_dir_ctx, bool RomFSBuildContext::AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
RomFSBuildDirectoryContext* dir_ctx, std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx) {
RomFSBuildDirectoryContext** out_dir_ctx) { // Check whether it's already in the known directories.
/* Check whether it's already in the known directories. */ const auto existing = directories.find(dir_ctx->path);
auto existing = this->directories.find(dir_ctx->path); if (existing != directories.end())
if (existing != this->directories.end()) {
if (out_dir_ctx) {
*out_dir_ctx = existing->second;
}
return false; return false;
}
/* Add a new directory. */ // Add a new directory.
this->num_dirs++; num_dirs++;
this->dir_table_size += dir_table_size +=
sizeof(RomFSDirectoryEntry) + ((dir_ctx->path_len - dir_ctx->cur_path_ofs + 3) & ~3); sizeof(RomFSDirectoryEntry) + ((dir_ctx->path_len - dir_ctx->cur_path_ofs + 3) & ~3);
dir_ctx->parent = parent_dir_ctx; dir_ctx->parent = parent_dir_ctx;
this->directories.insert({dir_ctx->path, dir_ctx}); directories.emplace(dir_ctx->path, dir_ctx);
if (out_dir_ctx) {
*out_dir_ctx = dir_ctx;
}
return true; return true;
} }
bool RomFSBuildContext::AddFile(RomFSBuildDirectoryContext* parent_dir_ctx, bool RomFSBuildContext::AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
RomFSBuildFileContext* file_ctx) { std::shared_ptr<RomFSBuildFileContext> file_ctx) {
/* Check whether it's already in the known files. */ // Check whether it's already in the known files.
auto existing = this->files.find(file_ctx->path); const auto existing = files.find(file_ctx->path);
if (existing != this->files.end()) { if (existing != files.end()) {
return false; return false;
} }
/* Add a new file. */ // Add a new file.
this->num_files++; num_files++;
this->file_table_size += file_table_size +=
sizeof(RomFSFileEntry) + ((file_ctx->path_len - file_ctx->cur_path_ofs + 3) & ~3); sizeof(RomFSFileEntry) + ((file_ctx->path_len - file_ctx->cur_path_ofs + 3) & ~3);
file_ctx->parent = parent_dir_ctx; file_ctx->parent = parent_dir_ctx;
this->files.insert({file_ctx->path, file_ctx}); files.emplace(file_ctx->path, file_ctx);
return true; return true;
} }
RomFSBuildContext::RomFSBuildContext(VirtualDir base_) : base(std::move(base_)) { RomFSBuildContext::RomFSBuildContext(VirtualDir base_) : base(std::move(base_)) {
this->root = new RomFSBuildDirectoryContext({0}); root = std::make_shared<RomFSBuildDirectoryContext>();
this->root->path = new char[1]; root->path = "\0";
this->root->path[0] = '\x00'; directories.emplace(root->path, root);
this->directories.insert({this->root->path, this->root}); num_dirs = 1;
this->num_dirs = 1; dir_table_size = 0x18;
this->dir_table_size = 0x18;
VisitDirectory(base, this->root); VisitDirectory(base, root);
} }
RomFSBuildContext::~RomFSBuildContext() = default;
std::map<u64, VirtualFile> RomFSBuildContext::Build() { std::map<u64, VirtualFile> RomFSBuildContext::Build() {
std::map<u64, VirtualFile> out; const auto dir_hash_table_entry_count = romfs_get_hash_table_count(num_dirs);
RomFSBuildFileContext* cur_file; const auto file_hash_table_entry_count = romfs_get_hash_table_count(num_files);
RomFSBuildDirectoryContext* cur_dir; dir_hash_table_size = 4 * dir_hash_table_entry_count;
file_hash_table_size = 4 * file_hash_table_entry_count;
const auto dir_hash_table_entry_count = romfs_get_hash_table_count(this->num_dirs); // Assign metadata pointers
const auto file_hash_table_entry_count = romfs_get_hash_table_count(this->num_files); RomFSHeader header{};
this->dir_hash_table_size = 4 * dir_hash_table_entry_count;
this->file_hash_table_size = 4 * file_hash_table_entry_count;
/* Assign metadata pointers */ std::vector<u32> dir_hash_table(dir_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
RomFSHeader* header = new RomFSHeader({0}); std::vector<u32> file_hash_table(file_hash_table_entry_count, ROMFS_ENTRY_EMPTY);
auto metadata = new u8[this->dir_hash_table_size + this->dir_table_size +
this->file_hash_table_size + this->file_table_size];
auto dir_hash_table = reinterpret_cast<u32*>(metadata);
const auto dir_table = reinterpret_cast<RomFSDirectoryEntry*>(
reinterpret_cast<uintptr_t>(dir_hash_table) + this->dir_hash_table_size);
auto file_hash_table =
reinterpret_cast<u32*>(reinterpret_cast<uintptr_t>(dir_table) + this->dir_table_size);
const auto file_table = reinterpret_cast<RomFSFileEntry*>(
reinterpret_cast<uintptr_t>(file_hash_table) + this->file_hash_table_size);
/* Clear out hash tables. */ std::vector<u8> dir_table(dir_table_size);
std::vector<u8> file_table(file_table_size);
// Clear out hash tables.
for (u32 i = 0; i < dir_hash_table_entry_count; i++) for (u32 i = 0; i < dir_hash_table_entry_count; i++)
dir_hash_table[i] = ROMFS_ENTRY_EMPTY; dir_hash_table[i] = ROMFS_ENTRY_EMPTY;
for (u32 i = 0; i < file_hash_table_entry_count; i++) for (u32 i = 0; i < file_hash_table_entry_count; i++)
file_hash_table[i] = ROMFS_ENTRY_EMPTY; file_hash_table[i] = ROMFS_ENTRY_EMPTY;
out.clear(); std::shared_ptr<RomFSBuildFileContext> cur_file;
/* Determine file offsets. */ // Determine file offsets.
u32 entry_offset = 0; u32 entry_offset = 0;
RomFSBuildFileContext* prev_file = nullptr; std::shared_ptr<RomFSBuildFileContext> prev_file = nullptr;
for (const auto& it : this->files) { for (const auto& it : files) {
cur_file = it.second; cur_file = it.second;
this->file_partition_size = (this->file_partition_size + 0xFULL) & ~0xFULL; file_partition_size = (file_partition_size + 0xFULL) & ~0xFULL;
cur_file->offset = this->file_partition_size; cur_file->offset = file_partition_size;
this->file_partition_size += cur_file->size; file_partition_size += cur_file->size;
cur_file->entry_offset = entry_offset; cur_file->entry_offset = entry_offset;
entry_offset += entry_offset +=
sizeof(RomFSFileEntry) + ((cur_file->path_len - cur_file->cur_path_ofs + 3) & ~3); sizeof(RomFSFileEntry) + ((cur_file->path_len - cur_file->cur_path_ofs + 3) & ~3);
prev_file = cur_file; prev_file = cur_file;
} }
/* Assign deferred parent/sibling ownership. */ // Assign deferred parent/sibling ownership.
for (auto it = this->files.rbegin(); it != this->files.rend(); it++) { for (auto it = files.rbegin(); it != files.rend(); ++it) {
cur_file = it->second; cur_file = it->second;
cur_file->sibling = cur_file->parent->file; cur_file->sibling = cur_file->parent->file;
cur_file->parent->file = cur_file; cur_file->parent->file = cur_file;
} }
/* Determine directory offsets. */ std::shared_ptr<RomFSBuildDirectoryContext> cur_dir;
// Determine directory offsets.
entry_offset = 0; entry_offset = 0;
for (const auto& it : this->directories) { for (const auto& it : directories) {
cur_dir = it.second; cur_dir = it.second;
cur_dir->entry_offset = entry_offset; cur_dir->entry_offset = entry_offset;
entry_offset += entry_offset +=
sizeof(RomFSDirectoryEntry) + ((cur_dir->path_len - cur_dir->cur_path_ofs + 3) & ~3); sizeof(RomFSDirectoryEntry) + ((cur_dir->path_len - cur_dir->cur_path_ofs + 3) & ~3);
} }
/* Assign deferred parent/sibling ownership. */ // Assign deferred parent/sibling ownership.
for (auto it = this->directories.rbegin(); it->second != this->root; it++) { for (auto it = directories.rbegin(); it->second != root; ++it) {
cur_dir = it->second; cur_dir = it->second;
cur_dir->sibling = cur_dir->parent->child; cur_dir->sibling = cur_dir->parent->child;
cur_dir->parent->child = cur_dir; cur_dir->parent->child = cur_dir;
} }
/* Populate file tables. */ std::map<u64, VirtualFile> out;
for (const auto& it : this->files) {
cur_file = it.second;
RomFSFileEntry* cur_entry = romfs_get_fentry(file_table, cur_file->entry_offset);
cur_entry->parent = cur_file->parent->entry_offset; // Populate file tables.
cur_entry->sibling = for (const auto& it : files) {
(cur_file->sibling == nullptr) ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset; cur_file = it.second;
cur_entry->offset = cur_file->offset; RomFSFileEntry cur_entry{};
cur_entry->size = cur_file->size;
cur_entry.parent = cur_file->parent->entry_offset;
cur_entry.sibling =
cur_file->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset;
cur_entry.offset = cur_file->offset;
cur_entry.size = cur_file->size;
const auto name_size = cur_file->path_len - cur_file->cur_path_ofs; const auto name_size = cur_file->path_len - cur_file->cur_path_ofs;
const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, const auto hash = romfs_calc_path_hash(cur_file->parent->entry_offset, cur_file->path,
reinterpret_cast<unsigned char*>(cur_file->path) + cur_file->cur_path_ofs, name_size);
cur_file->cur_path_ofs, cur_entry.hash = file_hash_table[hash % file_hash_table_entry_count];
0, name_size);
cur_entry->hash = file_hash_table[hash % file_hash_table_entry_count];
file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset; file_hash_table[hash % file_hash_table_entry_count] = cur_file->entry_offset;
cur_entry->name_size = name_size; cur_entry.name_size = name_size;
memset(cur_entry->name, 0, (cur_entry->name_size + 3) & ~3);
memcpy(cur_entry->name, cur_file->path + cur_file->cur_path_ofs, name_size);
out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source); out.emplace(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
std::memcpy(file_table.data() + cur_file->entry_offset, &cur_entry, sizeof(RomFSFileEntry));
std::memset(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry), 0,
(cur_entry.name_size + 3) & ~3);
std::memcpy(file_table.data() + cur_file->entry_offset + sizeof(RomFSFileEntry),
cur_file->path.data() + cur_file->cur_path_ofs, name_size);
} }
/* Populate dir tables. */ // Populate dir tables.
for (const auto& it : this->directories) { for (const auto& it : directories) {
cur_dir = it.second; cur_dir = it.second;
RomFSDirectoryEntry* cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset); RomFSDirectoryEntry cur_entry{};
cur_entry->parent = cur_dir == this->root ? 0 : cur_dir->parent->entry_offset;
cur_entry->sibling =
(cur_dir->sibling == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
cur_entry->child =
(cur_dir->child == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
cur_entry->file =
(cur_dir->file == nullptr) ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
u32 name_size = cur_dir->path_len - cur_dir->cur_path_ofs; cur_entry.parent = cur_dir == root ? 0 : cur_dir->parent->entry_offset;
u32 hash = romfs_calc_path_hash( cur_entry.sibling =
cur_dir == this->root ? 0 : cur_dir->parent->entry_offset, cur_dir->sibling == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
reinterpret_cast<unsigned char*>(cur_dir->path) + cur_dir->cur_path_ofs, 0, name_size); cur_entry.child =
cur_entry->hash = dir_hash_table[hash % dir_hash_table_entry_count]; cur_dir->child == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset;
cur_entry.file = cur_dir->file == nullptr ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset;
const auto name_size = cur_dir->path_len - cur_dir->cur_path_ofs;
const auto hash = romfs_calc_path_hash(cur_dir == root ? 0 : cur_dir->parent->entry_offset,
cur_dir->path, cur_dir->cur_path_ofs, name_size);
cur_entry.hash = dir_hash_table[hash % dir_hash_table_entry_count];
dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset; dir_hash_table[hash % dir_hash_table_entry_count] = cur_dir->entry_offset;
cur_entry->name_size = name_size; cur_entry.name_size = name_size;
memset(cur_entry->name, 0, (cur_entry->name_size + 3) & ~3);
memcpy(cur_entry->name, cur_dir->path + cur_dir->cur_path_ofs, name_size); std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
sizeof(RomFSDirectoryEntry));
std::memcpy(dir_table.data() + cur_dir->entry_offset, &cur_entry,
sizeof(RomFSDirectoryEntry));
std::memset(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry), 0,
(cur_entry.name_size + 3) & ~3);
std::memcpy(dir_table.data() + cur_dir->entry_offset + sizeof(RomFSDirectoryEntry),
cur_dir->path.data() + cur_dir->cur_path_ofs, name_size);
} }
/* Delete directories. */ // Set header fields.
for (const auto& it : this->directories) { header.header_size = sizeof(RomFSHeader);
cur_dir = it.second; header.file_hash_table_size = file_hash_table_size;
delete cur_dir->path; header.file_table_size = file_table_size;
delete cur_dir; header.dir_hash_table_size = dir_hash_table_size;
} header.dir_table_size = dir_table_size;
this->root = nullptr; header.file_partition_ofs = ROMFS_FILEPARTITION_OFS;
this->directories.clear(); header.dir_hash_table_ofs = (header.file_partition_ofs + file_partition_size + 3ULL) & ~3ULL;
header.dir_table_ofs = header.dir_hash_table_ofs + header.dir_hash_table_size;
/* Delete files. */ header.file_hash_table_ofs = header.dir_table_ofs + header.dir_table_size;
for (const auto& it : this->files) { header.file_table_ofs = header.file_hash_table_ofs + header.file_hash_table_size;
cur_file = it.second;
delete cur_file->path;
delete cur_file;
}
this->files.clear();
/* Set header fields. */
header->header_size = sizeof(*header);
header->file_hash_table_size = this->file_hash_table_size;
header->file_table_size = this->file_table_size;
header->dir_hash_table_size = this->dir_hash_table_size;
header->dir_table_size = this->dir_table_size;
header->file_partition_ofs = ROMFS_FILEPARTITION_OFS;
header->dir_hash_table_ofs =
(header->file_partition_ofs + this->file_partition_size + 3ULL) & ~3ULL;
header->dir_table_ofs = header->dir_hash_table_ofs + header->dir_hash_table_size;
header->file_hash_table_ofs = header->dir_table_ofs + header->dir_table_size;
header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size;
std::vector<u8> header_data(sizeof(RomFSHeader)); std::vector<u8> header_data(sizeof(RomFSHeader));
std::memcpy(header_data.data(), header, header_data.size()); std::memcpy(header_data.data(), &header, header_data.size());
out.emplace(0, std::make_shared<VectorVfsFile>(header_data)); out.emplace(0, std::make_shared<VectorVfsFile>(header_data));
std::vector<u8> meta_out(this->file_hash_table_size + this->file_table_size + std::vector<u8> metadata(file_hash_table_size + file_table_size + dir_hash_table_size +
this->dir_hash_table_size + this->dir_table_size); dir_table_size);
std::memcpy(meta_out.data(), metadata, meta_out.size()); auto index = 0;
out.emplace(header->dir_hash_table_ofs, std::make_shared<VectorVfsFile>(meta_out)); std::memcpy(metadata.data(), dir_hash_table.data(), dir_hash_table.size() * sizeof(u32));
index += dir_hash_table.size() * sizeof(u32);
std::memcpy(metadata.data() + index, dir_table.data(), dir_table.size());
index += dir_table.size();
std::memcpy(metadata.data() + index, file_hash_table.data(),
file_hash_table.size() * sizeof(u32));
index += file_hash_table.size() * sizeof(u32);
std::memcpy(metadata.data() + index, file_table.data(), file_table.size());
out.emplace(header.dir_hash_table_ofs, std::make_shared<VectorVfsFile>(metadata));
return out; return out;
} }

View file

@ -0,0 +1,70 @@
/*
* Copyright (c) 2018 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Adapted by DarkLordZach for use/interaction with yuzu
*
* Modifications Copyright 2018 yuzu emulator team
* Licensed under GPLv2 or any later version
* Refer to the license.txt file included.
*/
#pragma once
#include <map>
#include <memory>
#include <string>
#include <boost/detail/container_fwd.hpp>
#include "common/common_types.h"
#include "core/file_sys/vfs.h"
namespace FileSys {
struct RomFSBuildDirectoryContext;
struct RomFSBuildFileContext;
struct RomFSDirectoryEntry;
struct RomFSFileEntry;
class RomFSBuildContext {
public:
explicit RomFSBuildContext(VirtualDir base);
~RomFSBuildContext();
// This finalizes the context.
std::map<u64, VirtualFile> Build();
private:
VirtualDir base;
std::shared_ptr<RomFSBuildDirectoryContext> root;
std::map<std::string, std::shared_ptr<RomFSBuildDirectoryContext>, std::less<>> directories;
std::map<std::string, std::shared_ptr<RomFSBuildFileContext>, std::less<>> files;
u64 num_dirs = 0;
u64 num_files = 0;
u64 dir_table_size = 0;
u64 file_table_size = 0;
u64 dir_hash_table_size = 0;
u64 file_hash_table_size = 0;
u64 file_partition_size = 0;
void VisitDirectory(VirtualDir filesys, std::shared_ptr<RomFSBuildDirectoryContext> parent);
bool AddDirectory(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
std::shared_ptr<RomFSBuildDirectoryContext> dir_ctx);
bool AddFile(std::shared_ptr<RomFSBuildDirectoryContext> parent_dir_ctx,
std::shared_ptr<RomFSBuildFileContext> file_ctx);
};
} // namespace FileSys

View file

@ -1,106 +0,0 @@
/*
* Copyright (c) 2018 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Adapted by DarkLordZach for use/interaction with yuzu
*
* Modifications Copyright 2018 yuzu emulator team
* Licensed under GPLv2 or any later version
* Refer to the license.txt file included.
*/
#pragma once
#include <map>
#include <boost/detail/container_fwd.hpp>
#include "common/common_types.h"
#include "vfs.h"
namespace FileSys {
/* Used as comparator for std::map<char *, RomFSBuild*Context> */
struct build_ctx_cmp {
bool operator()(const char* a, const char* b) const {
return strcmp(a, b) < 0;
}
};
struct RomFSDirectoryEntry;
struct RomFSFileEntry;
struct RomFSBuildDirectoryContext;
struct RomFSBuildFileContext;
class RomFSBuildContext {
private:
VirtualDir base;
RomFSBuildDirectoryContext* root;
std::map<char*, RomFSBuildDirectoryContext*, build_ctx_cmp> directories;
std::map<char*, RomFSBuildFileContext*, build_ctx_cmp> files;
u64 num_dirs = 0;
u64 num_files = 0;
u64 dir_table_size = 0;
u64 file_table_size = 0;
u64 dir_hash_table_size = 0;
u64 file_hash_table_size = 0;
u64 file_partition_size = 0;
void VisitDirectory(VirtualDir filesys, RomFSBuildDirectoryContext* parent);
bool AddDirectory(RomFSBuildDirectoryContext* parent_dir_ctx,
RomFSBuildDirectoryContext* dir_ctx,
RomFSBuildDirectoryContext** out_dir_ctx);
bool AddFile(RomFSBuildDirectoryContext* parent_dir_ctx, RomFSBuildFileContext* file_ctx);
public:
explicit RomFSBuildContext(VirtualDir base);
/* This finalizes the context. */
std::map<u64, VirtualFile> Build();
};
static inline RomFSDirectoryEntry* romfs_get_direntry(void* directories, uint32_t offset) {
return (RomFSDirectoryEntry*)((uintptr_t)directories + offset);
}
static inline RomFSFileEntry* romfs_get_fentry(void* files, uint32_t offset) {
return (RomFSFileEntry*)((uintptr_t)files + offset);
}
static inline uint32_t romfs_calc_path_hash(uint32_t parent, const unsigned char* path,
uint32_t start, size_t path_len) {
uint32_t hash = parent ^ 123456789;
for (uint32_t i = 0; i < path_len; i++) {
hash = (hash >> 5) | (hash << 27);
hash ^= path[start + i];
}
return hash;
}
static inline uint32_t romfs_get_hash_table_count(uint32_t num_entries) {
if (num_entries < 3) {
return 3;
} else if (num_entries < 19) {
return num_entries | 1;
}
uint32_t count = num_entries;
while (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ||
count % 11 == 0 || count % 13 == 0 || count % 17 == 0) {
count++;
}
return count;
}
} // namespace FileSys

View file

@ -68,6 +68,42 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const {
return exefs; return exefs;
} }
static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) {
const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id);
if (type == ContentRecordType::Program && load_dir != nullptr && load_dir->GetSize() > 0) {
auto extracted = ExtractRomFS(romfs);
if (extracted != nullptr) {
auto patch_dirs = load_dir->GetSubdirectories();
std::sort(patch_dirs.begin(), patch_dirs.end(),
[](const VirtualDir& l, const VirtualDir& r) {
return l->GetName() < r->GetName();
});
std::vector<VirtualDir> layers;
layers.reserve(patch_dirs.size() + 1);
for (const auto& subdir : patch_dirs) {
auto romfs_dir = subdir->GetSubdirectory("romfs");
if (romfs_dir != nullptr)
layers.push_back(std::move(romfs_dir));
}
layers.push_back(std::move(extracted));
const auto layered = LayerDirectories(layers);
if (layered != nullptr) {
auto packed = CreateRomFS(layered);
if (packed != nullptr) {
LOG_INFO(Loader, " RomFS: LayeredFS patches applied successfully");
romfs = std::move(packed);
}
}
}
}
}
VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
ContentRecordType type) const { ContentRecordType type) const {
LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id, LOG_INFO(Loader, "Patching RomFS for title_id={:016X}, type={:02X}", title_id,
@ -92,39 +128,7 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset,
} }
// LayeredFS // LayeredFS
const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); ApplyLayeredFS(romfs, title_id, type);
if (type == ContentRecordType::Program && load_dir != nullptr && load_dir->GetSize() > 0) {
const auto extracted = ExtractRomFS(romfs);
if (extracted != nullptr) {
auto patch_dirs = load_dir->GetSubdirectories();
std::sort(patch_dirs.begin(), patch_dirs.end(),
[](const VirtualDir& l, const VirtualDir& r) {
return l->GetName() < r->GetName();
});
std::vector<VirtualDir> layers;
layers.reserve(patch_dirs.size() + 1);
for (const auto& subdir : patch_dirs) {
const auto romfs_dir = subdir->GetSubdirectory("romfs");
if (romfs_dir != nullptr)
layers.push_back(romfs_dir);
}
layers.push_back(extracted);
const auto layered = LayerDirectories(layers);
if (layered != nullptr) {
const auto packed = CreateRomFS(layered);
if (packed != nullptr) {
LOG_INFO(Loader, " RomFS: LayeredFS patches applied successfully");
romfs = packed;
}
}
}
}
return romfs; return romfs;
} }
@ -153,7 +157,7 @@ std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const {
const auto lfs_dir = Service::FileSystem::GetModificationLoadRoot(title_id); const auto lfs_dir = Service::FileSystem::GetModificationLoadRoot(title_id);
if (lfs_dir != nullptr && lfs_dir->GetSize() > 0) if (lfs_dir != nullptr && lfs_dir->GetSize() > 0)
out[PatchType::LayeredFS] = ""; out.insert_or_assign(PatchType::LayeredFS, "");
return out; return out;
} }

View file

@ -18,6 +18,10 @@
#include "core/loader/loader.h" #include "core/loader/loader.h"
namespace FileSys { namespace FileSys {
// The size of blocks to use when vfs raw copying into nand.
constexpr size_t VFS_RC_LARGE_COPY_BLOCK = 0x400000;
std::string RegisteredCacheEntry::DebugInfo() const { std::string RegisteredCacheEntry::DebugInfo() const {
return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type)); return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type));
} }
@ -121,7 +125,7 @@ VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
if (concat.empty()) if (concat.empty())
return nullptr; return nullptr;
file = FileSys::ConcatenateFiles(concat); file = FileSys::ConcatenateFiles(concat, concat.front()->GetName());
} }
return file; return file;
@ -480,7 +484,8 @@ InstallResult RegisteredCache::RawInstallNCA(std::shared_ptr<NCA> nca, const Vfs
auto out = dir->CreateFileRelative(path); auto out = dir->CreateFileRelative(path);
if (out == nullptr) if (out == nullptr)
return InstallResult::ErrorCopyFailed; return InstallResult::ErrorCopyFailed;
return copy(in, out, 0x400000) ? InstallResult::Success : InstallResult::ErrorCopyFailed; return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
: InstallResult::ErrorCopyFailed;
} }
bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) { bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {

View file

@ -4,7 +4,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/file_sys/fsmitm_romfsbuild.hpp" #include "core/file_sys/fsmitm_romfsbuild.h"
#include "core/file_sys/romfs.h" #include "core/file_sys/romfs.h"
#include "core/file_sys/vfs.h" #include "core/file_sys/vfs.h"
#include "core/file_sys/vfs_concat.h" #include "core/file_sys/vfs_concat.h"
@ -100,7 +100,7 @@ void ProcessDirectory(VirtualFile file, std::size_t dir_offset, std::size_t file
} }
} }
VirtualDir ExtractRomFS(VirtualFile file, bool traverse_into_data) { VirtualDir ExtractRomFS(VirtualFile file, RomFSExtractionType type) {
RomFSHeader header{}; RomFSHeader header{};
if (file->ReadObject(&header) != sizeof(RomFSHeader)) if (file->ReadObject(&header) != sizeof(RomFSHeader))
return nullptr; return nullptr;
@ -119,8 +119,9 @@ VirtualDir ExtractRomFS(VirtualFile file, bool traverse_into_data) {
VirtualDir out = std::move(root); VirtualDir out = std::move(root);
while (out->GetSubdirectories().size() == 1 && out->GetFiles().size() == 0) { while (out->GetSubdirectories().size() == 1 && out->GetFiles().empty()) {
if (out->GetSubdirectories().front()->GetName() == "data" && !traverse_into_data) if (out->GetSubdirectories().front()->GetName() == "data" &&
type == RomFSExtractionType::Truncated)
break; break;
out = out->GetSubdirectories().front(); out = out->GetSubdirectories().front();
} }

View file

@ -32,9 +32,15 @@ struct IVFCHeader {
}; };
static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size."); static_assert(sizeof(IVFCHeader) == 0xE0, "IVFCHeader has incorrect size.");
enum class RomFSExtractionType {
Full, // Includes data directory
Truncated, // Traverses into data directory
};
// Converts a RomFS binary blob to VFS Filesystem // Converts a RomFS binary blob to VFS Filesystem
// Returns nullptr on failure // Returns nullptr on failure
VirtualDir ExtractRomFS(VirtualFile file, bool traverse_into_data = true); VirtualDir ExtractRomFS(VirtualFile file,
RomFSExtractionType type = RomFSExtractionType::Truncated);
// Converts a VFS filesystem into a RomFS binary // Converts a VFS filesystem into a RomFS binary
// Returns nullptr on failure // Returns nullptr on failure

View file

@ -399,8 +399,8 @@ bool VfsDirectory::Copy(std::string_view src, std::string_view dest) {
return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize(); return f2->WriteBytes(f1->ReadAllBytes()) == f1->GetSize();
} }
std::map<std::string, VfsEntryType> VfsDirectory::GetEntries() const { std::map<std::string, VfsEntryType, std::less<>> VfsDirectory::GetEntries() const {
std::map<std::string, VfsEntryType> out; std::map<std::string, VfsEntryType, std::less<>> out;
for (const auto& dir : GetSubdirectories()) for (const auto& dir : GetSubdirectories())
out.emplace(dir->GetName(), VfsEntryType::Directory); out.emplace(dir->GetName(), VfsEntryType::Directory);
for (const auto& file : GetFiles()) for (const auto& file : GetFiles())

View file

@ -268,7 +268,7 @@ public:
// Gets all of the entries directly in the directory (files and dirs), returning a map between // Gets all of the entries directly in the directory (files and dirs), returning a map between
// item name -> type. // item name -> type.
virtual std::map<std::string, VfsEntryType> GetEntries() const; virtual std::map<std::string, VfsEntryType, std::less<>> GetEntries() const;
// Interprets the file with name file instead as a directory of type directory. // Interprets the file with name file instead as a directory of type directory.
// The directory must have a constructor that takes a single argument of type // The directory must have a constructor that takes a single argument of type
@ -323,6 +323,9 @@ bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block
// directory of src/dest. // directory of src/dest.
bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, size_t block_size = 0x1000); bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, size_t block_size = 0x1000);
// A method that performs a similar function to VfsRawCopy above, but instead copies entire
// directories. It suffers the same performance penalties as above and an implementation-specific
// Copy should always be preferred.
bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, size_t block_size = 0x1000); bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, size_t block_size = 0x1000);
// Checks if the directory at path relative to rel exists. If it does, returns that. If it does not // Checks if the directory at path relative to rel exists. If it does, returns that. If it does not

View file

@ -10,8 +10,9 @@
namespace FileSys { namespace FileSys {
bool VerifyConcatenationMap(std::map<u64, VirtualFile> map) { static bool VerifyConcatenationMapContinuity(const std::map<u64, VirtualFile>& map) {
for (auto iter = map.begin(); iter != --map.end();) { const auto last_valid = --map.end();
for (auto iter = map.begin(); iter != last_valid;) {
const auto old = iter++; const auto old = iter++;
if (old->first + old->second->GetSize() != iter->first) { if (old->first + old->second->GetSize() != iter->first) {
return false; return false;
@ -41,9 +42,11 @@ ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::s
ConcatenatedVfsFile::ConcatenatedVfsFile(std::map<u64, VirtualFile> files_, std::string name) ConcatenatedVfsFile::ConcatenatedVfsFile(std::map<u64, VirtualFile> files_, std::string name)
: files(std::move(files_)), name(std::move(name)) { : files(std::move(files_)), name(std::move(name)) {
ASSERT(VerifyConcatenationMap(files)); ASSERT(VerifyConcatenationMapContinuity(files));
} }
ConcatenatedVfsFile::~ConcatenatedVfsFile() = default;
std::string ConcatenatedVfsFile::GetName() const { std::string ConcatenatedVfsFile::GetName() const {
if (files.empty()) if (files.empty())
return ""; return "";
@ -77,25 +80,25 @@ bool ConcatenatedVfsFile::IsReadable() const {
} }
std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const { std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
std::pair<u64, VirtualFile> entry = *files.rbegin(); auto entry = --files.end();
for (auto iter = files.begin(); iter != files.end(); ++iter) { for (auto iter = files.begin(); iter != files.end(); ++iter) {
if (iter->first > offset) { if (iter->first > offset) {
entry = *--iter; entry = --iter;
break; break;
} }
} }
if (entry.first + entry.second->GetSize() <= offset) if (entry->first + entry->second->GetSize() <= offset)
return 0; return 0;
const auto read_in = const auto read_in =
std::min(entry.first + entry.second->GetSize() - offset, entry.second->GetSize()); std::min<u64>(entry->first + entry->second->GetSize() - offset, entry->second->GetSize());
if (length > read_in) { if (length > read_in) {
return entry.second->Read(data, read_in, offset - entry.first) + return entry->second->Read(data, read_in, offset - entry->first) +
Read(data + read_in, length - read_in, offset + read_in); Read(data + read_in, length - read_in, offset + read_in);
} }
return entry.second->Read(data, std::min(read_in, length), offset - entry.first); return entry->second->Read(data, std::min<u64>(read_in, length), offset - entry->first);
} }
std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) { std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {

View file

@ -13,35 +13,6 @@
namespace FileSys { namespace FileSys {
class ConcatenatedVfsFile;
// Wrapper function to allow for more efficient handling of files.size() == 0, 1 cases.
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name = "");
// Convenience function that turns a map of offsets to files into a concatenated file, filling gaps
// with template parameter.
template <u8 filler_byte>
VirtualFile ConcatenateFiles(std::map<u64, VirtualFile> files, std::string name = "") {
if (files.empty())
return nullptr;
if (files.size() == 1)
return files.begin()->second;
for (auto iter = files.begin(); iter != --files.end();) {
const auto old = iter++;
if (old->first + old->second->GetSize() != iter->first) {
files.emplace(old->first + old->second->GetSize(),
std::make_shared<StaticVfsFile<filler_byte>>(iter->first - old->first -
old->second->GetSize()));
}
}
if (files.begin()->first != 0)
files.emplace(0, std::make_shared<StaticVfsFile<filler_byte>>(files.begin()->first));
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
}
// Class that wraps multiple vfs files and concatenates them, making reads seamless. Currently // Class that wraps multiple vfs files and concatenates them, making reads seamless. Currently
// read-only. // read-only.
class ConcatenatedVfsFile : public VfsFile { class ConcatenatedVfsFile : public VfsFile {
@ -72,4 +43,33 @@ private:
std::string name; std::string name;
}; };
// Wrapper function to allow for more efficient handling of files.size() == 0, 1 cases.
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name);
// Convenience function that turns a map of offsets to files into a concatenated file, filling gaps
// with template parameter.
template <u8 filler_byte>
VirtualFile ConcatenateFiles(std::map<u64, VirtualFile> files, std::string name) {
if (files.empty())
return nullptr;
if (files.size() == 1)
return files.begin()->second;
const auto last_valid = --files.end();
for (auto iter = files.begin(); iter != last_valid;) {
const auto old = iter++;
if (old->first + old->second->GetSize() != iter->first) {
files.emplace(old->first + old->second->GetSize(),
std::make_shared<StaticVfsFile<filler_byte>>(iter->first - old->first -
old->second->GetSize()));
}
}
// Ensure the map starts at offset 0 (start of file), otherwise pad to fill.
if (files.begin()->first != 0)
files.emplace(0, std::make_shared<StaticVfsFile<filler_byte>>(files.begin()->first));
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
}
} // namespace FileSys } // namespace FileSys

View file

@ -20,10 +20,11 @@ VirtualDir LayerDirectories(std::vector<VirtualDir> dirs, std::string name) {
LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name) LayeredVfsDirectory::LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name)
: dirs(std::move(dirs)), name(std::move(name)) {} : dirs(std::move(dirs)), name(std::move(name)) {}
LayeredVfsDirectory::~LayeredVfsDirectory() = default;
std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFileRelative(std::string_view path) const { std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFileRelative(std::string_view path) const {
VirtualFile file;
for (const auto& layer : dirs) { for (const auto& layer : dirs) {
file = layer->GetFileRelative(path); const auto file = layer->GetFileRelative(path);
if (file != nullptr) if (file != nullptr)
return file; return file;
} }
@ -35,12 +36,12 @@ std::shared_ptr<VfsDirectory> LayeredVfsDirectory::GetDirectoryRelative(
std::string_view path) const { std::string_view path) const {
std::vector<VirtualDir> out; std::vector<VirtualDir> out;
for (const auto& layer : dirs) { for (const auto& layer : dirs) {
const auto dir = layer->GetDirectoryRelative(path); auto dir = layer->GetDirectoryRelative(path);
if (dir != nullptr) if (dir != nullptr)
out.push_back(dir); out.push_back(std::move(dir));
} }
return LayerDirectories(out); return LayerDirectories(std::move(out));
} }
std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFile(std::string_view name) const { std::shared_ptr<VfsFile> LayeredVfsDirectory::GetFile(std::string_view name) const {
@ -61,10 +62,11 @@ std::vector<std::shared_ptr<VfsFile>> LayeredVfsDirectory::GetFiles() const {
for (const auto& file : layer->GetFiles()) { for (const auto& file : layer->GetFiles()) {
if (std::find_if(out.begin(), out.end(), [&file](const VirtualFile& comp) { if (std::find_if(out.begin(), out.end(), [&file](const VirtualFile& comp) {
return comp->GetName() == file->GetName(); return comp->GetName() == file->GetName();
}) == out.end()) }) == out.end()) {
out.push_back(file); out.push_back(file);
} }
} }
}
return out; return out;
} }
@ -79,6 +81,7 @@ std::vector<std::shared_ptr<VfsDirectory>> LayeredVfsDirectory::GetSubdirectorie
} }
std::vector<VirtualDir> out; std::vector<VirtualDir> out;
out.reserve(names.size());
for (const auto& subdir : names) for (const auto& subdir : names)
out.push_back(GetSubdirectory(subdir)); out.push_back(GetSubdirectory(subdir));

View file

@ -21,6 +21,8 @@ class LayeredVfsDirectory : public VfsDirectory {
LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name); LayeredVfsDirectory(std::vector<VirtualDir> dirs, std::string name);
public: public:
~LayeredVfsDirectory() override;
std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override; std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override;
std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override; std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override;
std::shared_ptr<VfsFile> GetFile(std::string_view name) const override; std::shared_ptr<VfsFile> GetFile(std::string_view name) const override;

View file

@ -413,11 +413,11 @@ std::string RealVfsDirectory::GetFullPath() const {
return out; return out;
} }
std::map<std::string, VfsEntryType> RealVfsDirectory::GetEntries() const { std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
if (perms == Mode::Append) if (perms == Mode::Append)
return {}; return {};
std::map<std::string, VfsEntryType> out; std::map<std::string, VfsEntryType, std::less<>> out;
FileUtil::ForeachDirectoryEntry( FileUtil::ForeachDirectoryEntry(
nullptr, path, nullptr, path,
[&out](u64* entries_out, const std::string& directory, const std::string& filename) { [&out](u64* entries_out, const std::string& directory, const std::string& filename) {

View file

@ -98,7 +98,7 @@ public:
bool DeleteFile(std::string_view name) override; bool DeleteFile(std::string_view name) override;
bool Rename(std::string_view name) override; bool Rename(std::string_view name) override;
std::string GetFullPath() const override; std::string GetFullPath() const override;
std::map<std::string, VfsEntryType> GetEntries() const override; std::map<std::string, VfsEntryType, std::less<>> GetEntries() const override;
protected: protected:
bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override;

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <algorithm>
#include <memory> #include <memory>
#include <string_view> #include <string_view>
@ -15,7 +16,7 @@ template <u8 value>
class StaticVfsFile : public VfsFile { class StaticVfsFile : public VfsFile {
public: public:
explicit StaticVfsFile(size_t size = 0, std::string name = "", VirtualDir parent = nullptr) explicit StaticVfsFile(size_t size = 0, std::string name = "", VirtualDir parent = nullptr)
: size(size), name(name), parent(parent) {} : size(size), name(std::move(name)), parent(std::move(parent)) {}
std::string GetName() const override { std::string GetName() const override {
return name; return name;

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm> #include <algorithm>
#include <cstring>
#include <utility> #include <utility>
#include "core/file_sys/vfs_vector.h" #include "core/file_sys/vfs_vector.h"
@ -10,6 +11,8 @@ namespace FileSys {
VectorVfsFile::VectorVfsFile(std::vector<u8> initial_data, std::string name, VirtualDir parent) VectorVfsFile::VectorVfsFile(std::vector<u8> initial_data, std::string name, VirtualDir parent)
: data(std::move(initial_data)), name(std::move(name)), parent(std::move(parent)) {} : data(std::move(initial_data)), name(std::move(name)), parent(std::move(parent)) {}
VectorVfsFile::~VectorVfsFile() = default;
std::string VectorVfsFile::GetName() const { std::string VectorVfsFile::GetName() const {
return name; return name;
} }
@ -20,7 +23,7 @@ size_t VectorVfsFile::GetSize() const {
bool VectorVfsFile::Resize(size_t new_size) { bool VectorVfsFile::Resize(size_t new_size) {
data.resize(new_size); data.resize(new_size);
return data.size() == new_size; return true;
} }
std::shared_ptr<VfsDirectory> VectorVfsFile::GetContainingDirectory() const { std::shared_ptr<VfsDirectory> VectorVfsFile::GetContainingDirectory() const {
@ -55,7 +58,7 @@ bool VectorVfsFile::Rename(std::string_view name_) {
} }
void VectorVfsFile::Assign(std::vector<u8> new_data) { void VectorVfsFile::Assign(std::vector<u8> new_data) {
data = new_data; data = std::move(new_data);
} }
VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_, VectorVfsDirectory::VectorVfsDirectory(std::vector<VirtualFile> files_,

View file

@ -13,6 +13,7 @@ class VectorVfsFile : public VfsFile {
public: public:
explicit VectorVfsFile(std::vector<u8> initial_data = {}, std::string name = "", explicit VectorVfsFile(std::vector<u8> initial_data = {}, std::string name = "",
VirtualDir parent = nullptr); VirtualDir parent = nullptr);
~VectorVfsFile() override;
std::string GetName() const override; std::string GetName() const override;
size_t GetSize() const override; size_t GetSize() const override;

View file

@ -784,7 +784,7 @@ void GMainWindow::OnGameListDumpRomFS(u64 program_id, const std::string& game_pa
? FileSys::PatchManager(program_id).PatchRomFS(file, loader->ReadRomFSIVFCOffset()) ? FileSys::PatchManager(program_id).PatchRomFS(file, loader->ReadRomFSIVFCOffset())
: file; : file;
const auto extracted = FileSys::ExtractRomFS(romfs, false); const auto extracted = FileSys::ExtractRomFS(romfs, FileSys::RomFSExtractionType::Full);
if (extracted == nullptr) { if (extracted == nullptr) {
failed(); failed();
return; return;