mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-22 20:06:40 +00:00
fs.mitm: require less contiguous memory to build romfs
This commit is contained in:
parent
d0404f3cc9
commit
5a15d62b8b
3 changed files with 124 additions and 74 deletions
|
@ -213,4 +213,29 @@ namespace ams::mitm::fs {
|
|||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result CreateAndOpenAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, size_t size) {
|
||||
R_TRY(EnsureSdInitialized());
|
||||
|
||||
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
||||
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), program_id, path);
|
||||
|
||||
/* Unconditionally create. */
|
||||
/* Don't check error, as a failure here should be okay. */
|
||||
FsFile f;
|
||||
fsFsCreateFile(&g_sd_filesystem, fixed_path, size, 0);
|
||||
|
||||
/* Try to open. */
|
||||
R_TRY(fsFsOpenFile(&g_sd_filesystem, fixed_path, OpenMode_ReadWrite, &f));
|
||||
auto file_guard = SCOPE_GUARD { fsFileClose(&f); };
|
||||
|
||||
/* Try to set the size. */
|
||||
R_TRY(fsFileSetSize(&f, static_cast<s64>(size)));
|
||||
|
||||
/* Set output. */
|
||||
file_guard.Cancel();
|
||||
*out = f;
|
||||
return ResultSuccess();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -44,5 +44,6 @@ namespace ams::mitm::fs {
|
|||
bool HasSdRomfsContent(ncm::ProgramId program_id);
|
||||
|
||||
Result SaveAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, void *data, size_t size);
|
||||
Result CreateAndOpenAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, size_t size);
|
||||
|
||||
}
|
||||
|
|
|
@ -271,9 +271,10 @@ namespace ams::mitm::fs {
|
|||
AMS_ASSERT(header.header_size == sizeof(Header));
|
||||
|
||||
/* Read tables. */
|
||||
auto tables = std::unique_ptr<u8[]>(new u8[header.dir_table_size + header.file_table_size]);
|
||||
void *dir_table = tables.get();
|
||||
void *file_table = tables.get() + header.dir_table_size;
|
||||
void *tables = std::malloc(header.dir_table_size + header.file_table_size);
|
||||
ON_SCOPE_EXIT { std::free(tables); };
|
||||
void *dir_table = tables;
|
||||
void *file_table = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(tables) + header.dir_table_size);
|
||||
R_ASSERT(storage->Read(header.dir_table_ofs, dir_table, size_t(header.dir_table_size)));
|
||||
R_ASSERT(storage->Read(header.file_table_ofs, file_table, size_t(header.file_table_size)));
|
||||
|
||||
|
@ -300,17 +301,13 @@ namespace ams::mitm::fs {
|
|||
Header *header = reinterpret_cast<Header *>(std::malloc(sizeof(Header)));
|
||||
std::memset(header, 0x00, sizeof(*header));
|
||||
|
||||
/* Open metadata file. */
|
||||
const size_t metadata_size = this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size;
|
||||
std::unique_ptr<u8[]> metadata(new u8[metadata_size]);
|
||||
u32 *dir_hash_table = reinterpret_cast<u32 *>(metadata.get());
|
||||
DirectoryEntry *dir_table = reinterpret_cast<DirectoryEntry *>(reinterpret_cast<uintptr_t>(dir_hash_table) + this->dir_hash_table_size);
|
||||
u32 *file_hash_table = reinterpret_cast<u32 *>(reinterpret_cast<uintptr_t>(dir_table) + this->dir_table_size);
|
||||
FileEntry *file_table = reinterpret_cast<FileEntry *>(reinterpret_cast<uintptr_t>(file_hash_table) + this->file_hash_table_size);
|
||||
FsFile metadata_file;
|
||||
R_ASSERT(mitm::fs::CreateAndOpenAtmosphereSdFile(&metadata_file, this->program_id, "romfs_metadata.bin", metadata_size));
|
||||
|
||||
/* Clear hash tables. */
|
||||
/* Ensure later hash tables will have correct defaults. */
|
||||
static_assert(EmptyEntry == 0xFFFFFFFF);
|
||||
std::memset(dir_hash_table, 0xFF, this->dir_hash_table_size);
|
||||
std::memset(file_hash_table, 0xFF, this->file_hash_table_size);
|
||||
|
||||
/* Emplace metadata source info. */
|
||||
out_infos->emplace_back(0, sizeof(*header), DataSourceType::Memory, header);
|
||||
|
@ -374,6 +371,15 @@ namespace ams::mitm::fs {
|
|||
}
|
||||
|
||||
/* Populate file tables. */
|
||||
{
|
||||
void *ft_buf = std::malloc(this->file_table_size);
|
||||
void *fht_buf = std::malloc(this->file_hash_table_size);
|
||||
ON_SCOPE_EXIT { std::free(fht_buf); std::free(ft_buf); };
|
||||
|
||||
u32 *file_hash_table = reinterpret_cast<u32 *>(fht_buf);
|
||||
FileEntry *file_table = reinterpret_cast<FileEntry *>(ft_buf);
|
||||
std::memset(file_hash_table, 0xFF, this->file_hash_table_size);
|
||||
|
||||
for (const auto &it : this->files) {
|
||||
BuildFileContext *cur_file = it.get();
|
||||
FileEntry *cur_entry = GetFileEntry(file_table, cur_file->entry_offset);
|
||||
|
@ -424,7 +430,21 @@ namespace ams::mitm::fs {
|
|||
}
|
||||
}
|
||||
|
||||
/* Write to file. */
|
||||
R_ASSERT(fsFileWrite(&metadata_file, this->dir_hash_table_size + this->dir_table_size, file_hash_table, this->file_hash_table_size, FsWriteOption_None));
|
||||
R_ASSERT(fsFileWrite(&metadata_file, this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size, file_table, this->file_table_size, FsWriteOption_None));
|
||||
}
|
||||
|
||||
/* Populate directory tables. */
|
||||
{
|
||||
void *dt_buf = std::malloc(this->dir_table_size);
|
||||
void *dht_buf = std::malloc(this->dir_hash_table_size);
|
||||
ON_SCOPE_EXIT { std::free(dht_buf); std::free(dt_buf); };
|
||||
|
||||
u32 *dir_hash_table = reinterpret_cast<u32 *>(dht_buf);
|
||||
DirectoryEntry *dir_table = reinterpret_cast<DirectoryEntry *>(dt_buf);
|
||||
std::memset(dir_hash_table, 0xFF, this->dir_hash_table_size);
|
||||
|
||||
for (const auto &it : this->directories) {
|
||||
BuildDirectoryContext *cur_dir = it.get();
|
||||
DirectoryEntry *cur_entry = GetDirectoryEntry(dir_table, cur_dir->entry_offset);
|
||||
|
@ -451,6 +471,11 @@ namespace ams::mitm::fs {
|
|||
}
|
||||
}
|
||||
|
||||
/* Write to file. */
|
||||
R_ASSERT(fsFileWrite(&metadata_file, 0, dir_hash_table, this->dir_hash_table_size, FsWriteOption_None));
|
||||
R_ASSERT(fsFileWrite(&metadata_file, this->dir_hash_table_size, dir_table, this->dir_table_size, FsWriteOption_None));
|
||||
}
|
||||
|
||||
/* Delete maps. */
|
||||
this->root = nullptr;
|
||||
this->directories.clear();
|
||||
|
@ -470,8 +495,7 @@ namespace ams::mitm::fs {
|
|||
|
||||
/* Save metadata to the SD card, to save on memory space. */
|
||||
{
|
||||
FsFile metadata_file;
|
||||
R_ASSERT(mitm::fs::SaveAtmosphereSdFile(&metadata_file, this->program_id, "romfs_metadata.bin", metadata.get(), metadata_size));
|
||||
R_ASSERT(fsFileFlush(&metadata_file));
|
||||
out_infos->emplace_back(header->dir_hash_table_ofs, metadata_size, DataSourceType::Metadata, new RemoteFile(metadata_file));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue