1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-26 22:02:15 +00:00

fs.mitm: Use std::map for RomFS building

This commit is contained in:
Michael Scire 2018-06-28 22:40:18 -06:00
parent fb0c1b1ac7
commit c536665fe9
2 changed files with 71 additions and 115 deletions

View file

@ -10,6 +10,8 @@ void RomFSBuildContext::VisitDirectory(FsFileSystem *filesys, RomFSBuildDirector
FsDir dir; FsDir dir;
Result rc; Result rc;
std::vector<RomFSBuildDirectoryContext *> child_dirs;
/* Open the current parent directory. */ /* Open the current parent directory. */
if (R_FAILED((rc = Utils::OpenRomFSDir(filesys, this->title_id, parent->path, &dir)))) { if (R_FAILED((rc = Utils::OpenRomFSDir(filesys, this->title_id, parent->path, &dir)))) {
fatalSimple(rc); fatalSimple(rc);
@ -33,6 +35,8 @@ void RomFSBuildContext::VisitDirectory(FsFileSystem *filesys, RomFSBuildDirector
if (!this->AddDirectory(parent, child, NULL)) { if (!this->AddDirectory(parent, child, NULL)) {
delete child->path; delete child->path;
delete child; delete child;
} else {
child_dirs.push_back(child);
} }
} else if (this->dir_entry.type == ENTRYTYPE_FILE) { } else if (this->dir_entry.type == ENTRYTYPE_FILE) {
RomFSBuildFileContext *child = new RomFSBuildFileContext({0}); RomFSBuildFileContext *child = new RomFSBuildFileContext({0});
@ -61,10 +65,8 @@ void RomFSBuildContext::VisitDirectory(FsFileSystem *filesys, RomFSBuildDirector
} }
fsDirClose(&dir); fsDirClose(&dir);
RomFSBuildDirectoryContext *cur_child = parent->child; for (auto &child : child_dirs) {
while (cur_child != NULL) { this->VisitDirectory(filesys, child);
this->VisitDirectory(filesys, cur_child);
cur_child = cur_child->sibling;
} }
} }
@ -180,56 +182,21 @@ void RomFSBuildContext::MergeRomStorage(IROStorage *storage, RomFSDataSource sou
} }
bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildDirectoryContext *dir_ctx, RomFSBuildDirectoryContext **out_dir_ctx) { bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildDirectoryContext *dir_ctx, RomFSBuildDirectoryContext **out_dir_ctx) {
/* Ordered insertion on child. */ /* Check whether it's already in the known directories. */
int cmp_val; auto existing = this->directories.find(dir_ctx->path);
if (parent_dir_ctx->child == NULL || (cmp_val = strcmp(dir_ctx->path, parent_dir_ctx->child->path)) < 0) { if (existing != this->directories.end()) {
dir_ctx->sibling = parent_dir_ctx->child;
parent_dir_ctx->child = dir_ctx;
} else if (cmp_val == 0) {
/* This directory already exists! */
if (out_dir_ctx) { if (out_dir_ctx) {
*out_dir_ctx = parent_dir_ctx->child; *out_dir_ctx = existing->second;
}
return false;
} else {
RomFSBuildDirectoryContext *child, *prev;
prev = parent_dir_ctx->child;
child = prev->sibling;
while (child != NULL) {
cmp_val = strcmp(dir_ctx->path, child->path);
if (cmp_val < 0) {
break;
} else if (cmp_val == 0) {
/* This directory already exists! */
if (out_dir_ctx) {
*out_dir_ctx = child;
} }
return false; return false;
} }
prev = child;
child = child->sibling;
}
prev->sibling = dir_ctx; /* Add a new directory. */
dir_ctx->sibling = child;
}
/* If we got this far, we're definitely adding a new directory. */
this->num_dirs++; this->num_dirs++;
this->dir_table_size += sizeof(RomFSDirectoryEntry) + ((dir_ctx->path_len - dir_ctx->cur_path_ofs + 3) & ~3); this->dir_table_size += 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});
/* Ordered insertion on next */
RomFSBuildDirectoryContext *cur = parent_dir_ctx->next, *prev = parent_dir_ctx;
while (cur != NULL) {
if (strcmp(dir_ctx->path, cur->path) < 0) {
break;
}
prev = cur;
cur = cur->next;
}
prev->next = dir_ctx;
dir_ctx->next = cur;
if (out_dir_ctx) { if (out_dir_ctx) {
*out_dir_ctx = dir_ctx; *out_dir_ctx = dir_ctx;
} }
@ -237,55 +204,18 @@ bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx,
} }
bool RomFSBuildContext::AddFile(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildFileContext *file_ctx) { bool RomFSBuildContext::AddFile(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildFileContext *file_ctx) {
/* Ordered insertion on sibling */ /* Check whether it's already in the known files. */
int cmp_val; auto existing = this->files.find(file_ctx->path);
if (parent_dir_ctx->file == NULL || (cmp_val = strcmp(file_ctx->path, parent_dir_ctx->file->path)) < 0) { if (existing != this->files.end()) {
file_ctx->sibling = parent_dir_ctx->file;
parent_dir_ctx->file = file_ctx;
} else if (cmp_val == 0) {
/* This file already exists! */
return false;
} else {
RomFSBuildFileContext *child, *prev;
prev = parent_dir_ctx->file;
child = prev->sibling;
while (child != NULL) {
cmp_val = strcmp(file_ctx->path, child->path);
if (cmp_val < 0) {
break;
} else if (cmp_val == 0) {
/* This directory already exists! */
return false; return false;
} }
prev = child;
child = child->sibling;
}
prev->sibling = file_ctx; /* Add a new file. */
file_ctx->sibling = child;
}
/* If we got this far, we're definitely adding a new file. */
this->num_files++; this->num_files++;
this->file_table_size += sizeof(RomFSFileEntry) + ((file_ctx->path_len - file_ctx->cur_path_ofs + 3) & ~3); this->file_table_size += 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});
/* Ordered insertion on next */
if (this->files == NULL || strcmp(file_ctx->path, this->files->path) < 0) {
file_ctx->next = this->files;
this->files = file_ctx;
} else {
RomFSBuildFileContext *cur = this->files->next, *prev = this->files;
while (cur != NULL) {
if (strcmp(file_ctx->path, cur->path) < 0) {
break;
}
prev = cur;
cur = cur->next;
}
prev->next = file_ctx;
file_ctx->next = cur;
}
return true; return true;
} }
@ -319,10 +249,10 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
out_infos->emplace_back(0, sizeof(*header), header, RomFSDataSource::Memory); out_infos->emplace_back(0, sizeof(*header), header, RomFSDataSource::Memory);
/* Determine file offsets. */ /* Determine file offsets. */
cur_file = this->files;
entry_offset = 0; entry_offset = 0;
RomFSBuildFileContext *prev_file = NULL; RomFSBuildFileContext *prev_file = NULL;
while (cur_file != NULL) { for (const auto &it : this->files) {
cur_file = it.second;
this->file_partition_size = (this->file_partition_size + 0xFULL) & ~0xFULL; this->file_partition_size = (this->file_partition_size + 0xFULL) & ~0xFULL;
/* Check for extra padding in the original romfs source and preserve it, to help ourselves later. */ /* Check for extra padding in the original romfs source and preserve it, to help ourselves later. */
if (prev_file && prev_file->source == cur_file->source && (prev_file->source == RomFSDataSource::BaseRomFS || prev_file->source == RomFSDataSource::FileRomFS)) { if (prev_file && prev_file->source == cur_file->source && (prev_file->source == RomFSDataSource::BaseRomFS || prev_file->source == RomFSDataSource::FileRomFS)) {
@ -339,23 +269,45 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
this->file_partition_size += cur_file->size; this->file_partition_size += cur_file->size;
cur_file->entry_offset = entry_offset; cur_file->entry_offset = entry_offset;
entry_offset += sizeof(RomFSFileEntry) + ((cur_file->path_len - cur_file->cur_path_ofs + 3) & ~3); entry_offset += sizeof(RomFSFileEntry) + ((cur_file->path_len - cur_file->cur_path_ofs + 3) & ~3);
/* Assign deferred parent/sibling ownership. */
if (cur_file->parent->file == NULL) {
cur_file->parent->file = cur_file;
} else {
RomFSBuildFileContext *temp = cur_file->parent->file;
while (temp->sibling != NULL) {
temp = temp->sibling;
}
temp->sibling = cur_file;
}
prev_file = cur_file; prev_file = cur_file;
cur_file = cur_file->next;
} }
/* Determine directory offsets. */ /* Determine directory offsets. */
cur_dir = this->root;
entry_offset = 0; entry_offset = 0;
while (cur_dir != NULL) { for (const auto &it : this->directories) {
cur_dir->entry_offset = entry_offset; cur_dir = it.second;
entry_offset += sizeof(RomFSDirectoryEntry) + ((cur_dir->path_len - cur_dir->cur_path_ofs + 3) & ~3); entry_offset += sizeof(RomFSDirectoryEntry) + ((cur_dir->path_len - cur_dir->cur_path_ofs + 3) & ~3);
cur_dir = cur_dir->next;
/* Assign deferred parent/sibling ownership. */
if (cur_dir != this->root) {
if (cur_dir->parent->child == NULL) {
cur_dir->parent->child = cur_dir;
} else {
RomFSBuildDirectoryContext *temp = cur_dir->parent->child;
while (temp->sibling != NULL) {
temp = temp->sibling;
}
temp->sibling = cur_dir;
}
}
} }
/* Populate file tables. */ /* Populate file tables. */
cur_file = this->files; for (const auto &it : this->files) {
while (cur_file != NULL) { cur_file = it.second;
RomFSFileEntry *cur_entry = romfs_get_fentry(file_table, cur_file->entry_offset); RomFSFileEntry *cur_entry = romfs_get_fentry(file_table, cur_file->entry_offset);
cur_entry->parent = cur_file->parent->entry_offset; cur_entry->parent = cur_file->parent->entry_offset;
@ -368,12 +320,10 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
cur_entry->hash = file_hash_table[hash % file_hash_table_entry_count]; 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); 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); memcpy(cur_entry->name, cur_file->path + cur_file->cur_path_ofs, name_size);
switch (cur_file->source) { switch (cur_file->source) {
case RomFSDataSource::BaseRomFS: case RomFSDataSource::BaseRomFS:
case RomFSDataSource::FileRomFS: case RomFSDataSource::FileRomFS:
@ -395,15 +345,14 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
fatalSimple(0xF601); fatalSimple(0xF601);
} }
RomFSBuildFileContext *temp = cur_file; delete cur_file->path;
cur_file = cur_file->next; delete cur_file;
delete temp->path;
delete temp;
} }
this->files.clear();
/* Populate dir tables. */ /* Populate dir tables. */
cur_dir = this->root; for (const auto &it : this->directories) {
while (cur_dir != NULL) { cur_dir = it.second;
RomFSDirectoryEntry *cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset); RomFSDirectoryEntry *cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset);
cur_entry->parent = cur_dir == this->root ? 0 : cur_dir->parent->entry_offset; cur_entry->parent = cur_dir == this->root ? 0 : cur_dir->parent->entry_offset;
cur_entry->sibling = (cur_dir->sibling == NULL) ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset; cur_entry->sibling = (cur_dir->sibling == NULL) ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset;
@ -419,11 +368,11 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
memset(cur_entry->name, 0, (cur_entry->name_size + 3) & ~3); 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); memcpy(cur_entry->name, cur_dir->path + cur_dir->cur_path_ofs, name_size);
RomFSBuildDirectoryContext *temp = cur_dir; delete cur_dir->path;
cur_dir = cur_dir->next; delete cur_dir;
delete temp->path;
delete temp;
} }
this->root = NULL;
this->directories.clear();
/* Set header fields. */ /* Set header fields. */
header->header_size = sizeof(*header); header->header_size = sizeof(*header);

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <variant> #include <map>
#include "fsmitm_romstorage.hpp" #include "fsmitm_romstorage.hpp"
@ -137,6 +137,13 @@ static_assert(sizeof(RomFSFileEntry) == 0x20, "Incorrect RomFSFileEntry definiti
struct RomFSBuildFileContext; struct RomFSBuildFileContext;
/* 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 RomFSBuildDirectoryContext { struct RomFSBuildDirectoryContext {
char *path; char *path;
u32 cur_path_ofs; u32 cur_path_ofs;
@ -146,7 +153,6 @@ struct RomFSBuildDirectoryContext {
RomFSBuildDirectoryContext *child = NULL; RomFSBuildDirectoryContext *child = NULL;
RomFSBuildDirectoryContext *sibling = NULL; RomFSBuildDirectoryContext *sibling = NULL;
RomFSBuildFileContext *file = NULL; RomFSBuildFileContext *file = NULL;
RomFSBuildDirectoryContext *next = NULL;
}; };
struct RomFSBuildFileContext { struct RomFSBuildFileContext {
@ -158,7 +164,6 @@ struct RomFSBuildFileContext {
u64 size = 0; u64 size = 0;
RomFSBuildDirectoryContext *parent = NULL; RomFSBuildDirectoryContext *parent = NULL;
RomFSBuildFileContext *sibling = NULL; RomFSBuildFileContext *sibling = NULL;
RomFSBuildFileContext *next = NULL;
RomFSDataSource source{0}; RomFSDataSource source{0};
u64 orig_offset = 0; u64 orig_offset = 0;
}; };
@ -167,7 +172,8 @@ class RomFSBuildContext {
private: private:
u64 title_id; u64 title_id;
RomFSBuildDirectoryContext *root; RomFSBuildDirectoryContext *root;
RomFSBuildFileContext *files; std::map<char *, RomFSBuildDirectoryContext *, build_ctx_cmp> directories;
std::map<char *, RomFSBuildFileContext *, build_ctx_cmp> files;
u64 num_dirs; u64 num_dirs;
u64 num_files; u64 num_files;
u64 dir_table_size; u64 dir_table_size;
@ -185,10 +191,11 @@ class RomFSBuildContext {
bool AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildDirectoryContext *dir_ctx, RomFSBuildDirectoryContext **out_dir_ctx); bool AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildDirectoryContext *dir_ctx, RomFSBuildDirectoryContext **out_dir_ctx);
bool AddFile(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildFileContext *file_ctx); bool AddFile(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildFileContext *file_ctx);
public: public:
RomFSBuildContext(u64 tid) : title_id(tid), root(NULL), files(NULL), num_dirs(0), num_files(0), dir_table_size(0), file_table_size(0), dir_hash_table_size(0), file_hash_table_size(0), file_partition_size(0) { RomFSBuildContext(u64 tid) : title_id(tid), num_dirs(0), num_files(0), dir_table_size(0), file_table_size(0), dir_hash_table_size(0), file_hash_table_size(0), file_partition_size(0) {
this->root = new RomFSBuildDirectoryContext({0}); this->root = new RomFSBuildDirectoryContext({0});
this->root->path = new char[1]; this->root->path = new char[1];
this->root->path[0] = '\x00'; this->root->path[0] = '\x00';
this->directories.insert({this->root->path, this->root});
this->num_dirs = 1; this->num_dirs = 1;
this->dir_table_size = 0x18; this->dir_table_size = 0x18;
} }