fs/system: deduplicate RomFs code

This commit is contained in:
Michael Scire 2020-12-04 22:08:33 -08:00
parent c45088d1cd
commit 73167448cc
16 changed files with 226 additions and 1723 deletions

View file

@ -17,23 +17,22 @@
namespace ams::fs {
s64 HierarchicalRomFileTable::QueryDirectoryEntryStorageSize(u32 count) {
const size_t real_count = count + ReservedDirectoryCount;
return DirectoryEntryMapTable::QueryKeyValueStorageSize(real_count) + real_count * (RomPathTool::MaxPathLength + 1) * sizeof(RomPathChar);
}
s64 HierarchicalRomFileTable::QueryDirectoryEntryBucketStorageSize(s64 count) {
return DirectoryEntryMapTable::QueryBucketStorageSize(count);
}
s64 HierarchicalRomFileTable::QueryFileEntryStorageSize(u32 count) {
return FileEntryMapTable::QueryKeyValueStorageSize(count) + count * (RomPathTool::MaxPathLength + 1) * sizeof(RomPathChar);
size_t HierarchicalRomFileTable::QueryDirectoryEntrySize(size_t aux_size) {
return DirectoryEntryMapTable::QueryEntrySize(aux_size);
}
s64 HierarchicalRomFileTable::QueryFileEntryBucketStorageSize(s64 count) {
return FileEntryMapTable::QueryBucketStorageSize(count);
}
size_t HierarchicalRomFileTable::QueryFileEntrySize(size_t aux_size) {
return FileEntryMapTable::QueryEntrySize(aux_size);
}
Result HierarchicalRomFileTable::Format(SubStorage dir_bucket, SubStorage file_bucket) {
s64 dir_bucket_size;
R_TRY(dir_bucket.GetSize(std::addressof(dir_bucket_size)));
@ -69,7 +68,7 @@ namespace ams::fs {
Position root_pos = RootPosition;
EntryKey root_key = {};
root_key.key.parent = root_pos;
RomPathTool::InitializeRomEntryName(std::addressof(root_key.name));
RomPathTool::InitEntryName(std::addressof(root_key.name));
RomDirectoryEntry root_entry = {
.next = InvalidPosition,
.dir = InvalidPosition,
@ -99,7 +98,7 @@ namespace ams::fs {
R_CONVERT(fs::ResultDbmKeyFull, fs::ResultDbmDirectoryEntryFull())
} R_END_TRY_CATCH;
*out = ConvertToDirectoryId(new_pos);
*out = PositionToDirectoryId(new_pos);
if (parent_entry.dir == InvalidPosition) {
parent_entry.dir = new_pos;
@ -146,7 +145,7 @@ namespace ams::fs {
R_CONVERT(fs::ResultDbmKeyFull, fs::ResultDbmFileEntryFull())
} R_END_TRY_CATCH;
*out = ConvertToFileId(new_pos);
*out = PositionToFileId(new_pos);
if (parent_entry.file == InvalidPosition) {
parent_entry.file = new_pos;
@ -185,7 +184,7 @@ namespace ams::fs {
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(pos), std::addressof(entry), key));
*out = ConvertToDirectoryId(pos);
*out = PositionToDirectoryId(pos);
return ResultSuccess();
}
@ -201,7 +200,7 @@ namespace ams::fs {
RomFileEntry entry = {};
R_TRY(this->GetFileEntry(std::addressof(pos), std::addressof(entry), key));
*out = ConvertToFileId(pos);
*out = PositionToFileId(pos);
return ResultSuccess();
}
@ -353,7 +352,7 @@ namespace ams::fs {
R_TRY(this->GetDirectoryEntry(std::addressof(dir_pos), std::addressof(dir_entry), dir_key));
Position parent_pos = dir_pos;
while (!parser->IsFinished()) {
while (!parser->IsParseFinished()) {
EntryKey old_key = dir_key;
R_TRY(parser->GetNextDirectoryName(std::addressof(dir_key.name)));
@ -492,7 +491,7 @@ namespace ams::fs {
Result HierarchicalRomFileTable::GetDirectoryEntry(RomDirectoryEntry *out_entry, RomDirectoryId id) {
AMS_ASSERT(out_entry != nullptr);
Position pos = ConvertToPosition(id);
Position pos = DirectoryIdToPosition(id);
RomEntryKey key = {};
const Result dir_res = this->dir_table.GetByPosition(std::addressof(key), out_entry, pos);
@ -524,7 +523,7 @@ namespace ams::fs {
Result HierarchicalRomFileTable::GetFileEntry(RomFileEntry *out_entry, RomFileId id) {
AMS_ASSERT(out_entry != nullptr);
Position pos = ConvertToPosition(id);
Position pos = FileIdToPosition(id);
RomEntryKey key = {};
const Result file_res = this->file_table.GetByPosition(std::addressof(key), out_entry, pos);

View file

@ -28,9 +28,8 @@ namespace ams::fs::RomPathTool {
this->prev_path_start = path;
this->prev_path_end = path;
this->next_path = path + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
for (this->next_path = path + 1; IsSeparator(this->next_path[0]); ++this->next_path) {
/* ... */
}
return ResultSuccess();
@ -43,12 +42,13 @@ namespace ams::fs::RomPathTool {
this->finished = false;
}
bool PathParser::IsFinished() const {
bool PathParser::IsParseFinished() const {
return this->finished;
}
bool PathParser::IsDirectoryPath() const {
AMS_ASSERT(this->next_path != nullptr);
if (IsNullTerminator(this->next_path[0]) && IsSeparator(this->next_path[-1])) {
return true;
}
@ -57,11 +57,47 @@ namespace ams::fs::RomPathTool {
return true;
}
if (IsParentDirectory(this->next_path)) {
return true;
return IsParentDirectory(this->next_path);
}
Result PathParser::GetNextDirectoryName(RomEntryName *out) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
/* Set the current path to output. */
out->length = this->prev_path_end - this->prev_path_start;
out->path = this->prev_path_start;
/* Parse the next path. */
this->prev_path_start = this->next_path;
const RomPathChar *cur = this->next_path;
for (size_t name_len = 0; true; name_len++) {
if (IsSeparator(cur[name_len])) {
R_UNLESS(name_len < MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
this->prev_path_end = cur + name_len;
this->next_path = this->prev_path_end + 1;
while (IsSeparator(this->next_path[0])) {
++this->next_path;
}
if (IsNullTerminator(this->next_path[0])) {
this->finished = true;
}
break;
}
if (IsNullTerminator(cur[name_len])) {
this->finished = true;
this->next_path = cur + name_len;
this->prev_path_end = cur + name_len;
break;
}
}
return false;
return ResultSuccess();
}
Result PathParser::GetAsDirectoryName(RomEntryName *out) const {
@ -92,45 +128,6 @@ namespace ams::fs::RomPathTool {
return ResultSuccess();
}
Result PathParser::GetNextDirectoryName(RomEntryName *out) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
/* Set the current path to output. */
out->length = this->prev_path_end - this->prev_path_start;
out->path = this->prev_path_start;
/* Parse the next path. */
this->prev_path_start = this->next_path;
const RomPathChar *cur = this->next_path;
for (size_t name_len = 0; true; name_len++) {
if (IsSeparator(cur[name_len])) {
R_UNLESS(name_len < MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
this->prev_path_end = cur + name_len;
this->next_path = this->prev_path_end + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
}
if (IsNullTerminator(this->next_path[0])) {
this->finished = true;
}
break;
}
if (IsNullTerminator(cur[name_len])) {
this->finished = true;
this->prev_path_end = this->next_path = cur + name_len;
break;
}
}
return ResultSuccess();
}
Result GetParentDirectoryName(RomEntryName *out, const RomEntryName &cur, const RomPathChar *p) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(p != nullptr);
@ -140,7 +137,7 @@ namespace ams::fs::RomPathTool {
s32 depth = 1;
if (IsParentDirectory(cur)) {
depth++;
++depth;
}
if (cur.path > p) {
@ -149,7 +146,7 @@ namespace ams::fs::RomPathTool {
while (head >= p) {
if (IsSeparator(*head)) {
if (IsCurrentDirectory(head + 1, len)) {
depth++;
++depth;
}
if (IsParentDirectory(head + 1, len)) {
@ -162,16 +159,16 @@ namespace ams::fs::RomPathTool {
}
while (IsSeparator(*head)) {
head--;
--head;
}
end = head;
len = 0;
depth--;
--depth;
}
len++;
head--;
++len;
--head;
}
R_UNLESS(depth == 0, fs::ResultDirectoryUnobtainable());
@ -182,10 +179,10 @@ namespace ams::fs::RomPathTool {
}
if (end <= p) {
out->path = p;
out->path = p;
out->length = 0;
} else {
out->path = start;
out->path = start;
out->length = end - start + 1;
}

View file

@ -316,7 +316,7 @@ namespace ams::fs {
out_entries[i].type = fs::DirectoryEntryType_File;
RomFsFileSystem::RomFileTable::FileInfo file_info;
R_TRY(this->parent->GetRomFileTable()->OpenFile(std::addressof(file_info), this->parent->GetRomFileTable()->ConvertToFileId(file_pos)));
R_TRY(this->parent->GetRomFileTable()->OpenFile(std::addressof(file_info), this->parent->GetRomFileTable()->PositionToFileId(file_pos)));
out_entries[i].file_size = file_info.size.Get();
}

View file

@ -1,189 +0,0 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#include <stratosphere.hpp>
namespace ams::fssystem::RomPathTool {
Result PathParser::Initialize(const RomPathChar *path) {
AMS_ASSERT(path != nullptr);
/* Require paths start with a separator, and skip repeated separators. */
R_UNLESS(IsSeparator(path[0]), fs::ResultDbmInvalidPathFormat());
while (IsSeparator(path[1])) {
path++;
}
this->prev_path_start = path;
this->prev_path_end = path;
this->next_path = path + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
}
return ResultSuccess();
}
void PathParser::Finalize() {
/* ... */
}
bool PathParser::IsFinished() const {
return this->finished;
}
bool PathParser::IsDirectoryPath() const {
AMS_ASSERT(this->next_path != nullptr);
if (IsNullTerminator(this->next_path[0]) && IsSeparator(this->next_path[-1])) {
return true;
}
if (IsCurrentDirectory(this->next_path)) {
return true;
}
if (IsParentDirectory(this->next_path)) {
return true;
}
return false;
}
Result PathParser::GetAsDirectoryName(RomEntryName *out) const {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
const size_t len = this->prev_path_end - this->prev_path_start;
R_UNLESS(len <= MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
out->length = len;
out->path = this->prev_path_start;
return ResultSuccess();
}
Result PathParser::GetAsFileName(RomEntryName *out) const {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
const size_t len = this->prev_path_end - this->prev_path_start;
R_UNLESS(len <= MaxPathLength, fs::ResultDbmFileNameTooLong());
out->length = len;
out->path = this->prev_path_start;
return ResultSuccess();
}
Result PathParser::GetNextDirectoryName(RomEntryName *out) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
/* Set the current path to output. */
out->length = this->prev_path_end - this->prev_path_start;
out->path = this->prev_path_start;
/* Parse the next path. */
this->prev_path_start = this->next_path;
const RomPathChar *cur = this->next_path;
for (size_t name_len = 0; true; name_len++) {
if (IsSeparator(cur[name_len])) {
R_UNLESS(name_len < MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
this->prev_path_end = cur + name_len;
this->next_path = this->prev_path_end + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
}
if (IsNullTerminator(this->next_path[0])) {
this->finished = true;
}
break;
}
if (IsNullTerminator(cur[name_len])) {
this->finished = true;
this->prev_path_end = this->next_path = cur + name_len;
break;
}
}
return ResultSuccess();
}
Result GetParentDirectoryName(RomEntryName *out, const RomEntryName &cur, const RomPathChar *p) {
const RomPathChar *start = cur.path;
const RomPathChar *end = cur.path + cur.length - 1;
s32 depth = 1;
if (IsParentDirectory(cur)) {
depth++;
}
if (cur.path > p) {
size_t len = 0;
const RomPathChar *head = cur.path - 1;
while (head >= p) {
if (IsSeparator(*head)) {
if (IsCurrentDirectory(head + 1, len)) {
depth++;
}
if (IsParentDirectory(head + 1, len)) {
depth += 2;
}
if (depth == 0) {
start = head + 1;
break;
}
while (IsSeparator(*head)) {
head--;
}
end = head;
len = 0;
depth--;
}
len++;
head--;
}
R_UNLESS(depth == 0, fs::ResultDbmInvalidPathFormat());
if (head == p) {
start = p + 1;
}
}
if (end <= p) {
out->path = p;
out->length = 0;
} else {
out->path = start;
out->length = end - start + 1;
}
return ResultSuccess();
}
}

View file

@ -19,7 +19,7 @@ namespace ams::fssystem {
namespace {
constexpr size_t CalculateRequiredWorkingMemorySize(const RomFileSystemInformation &header) {
constexpr size_t CalculateRequiredWorkingMemorySize(const fs::RomFileSystemInformation &header) {
return header.directory_bucket_size + header.directory_entry_size + header.file_bucket_size + header.file_entry_size;
}
@ -114,7 +114,7 @@ namespace ams::fssystem {
public:
virtual Result ReadImpl(s64 *out_count, fs::DirectoryEntry *out_entries, s64 max_entries) {
R_TRY(buffers::DoContinuouslyUntilBufferIsAllocated([=, this]() -> Result {
return this->ReadImpl(out_count, std::addressof(this->current_find), out_entries, max_entries);
return this->ReadInternal(out_count, std::addressof(this->current_find), out_entries, max_entries);
}, AMS_CURRENT_FUNCTION_NAME));
return ResultSuccess();
}
@ -123,20 +123,20 @@ namespace ams::fssystem {
FindPosition find = this->first_find;
R_TRY(buffers::DoContinuouslyUntilBufferIsAllocated([&]() -> Result {
R_TRY(this->ReadImpl(out, std::addressof(find), nullptr, 0));
R_TRY(this->ReadInternal(out, std::addressof(find), nullptr, 0));
return ResultSuccess();
}, AMS_CURRENT_FUNCTION_NAME));
return ResultSuccess();
}
private:
Result ReadImpl(s64 *out_count, FindPosition *find, fs::DirectoryEntry *out_entries, s64 max_entries) {
Result ReadInternal(s64 *out_count, FindPosition *find, fs::DirectoryEntry *out_entries, s64 max_entries) {
constexpr size_t NameBufferSize = fs::EntryNameLengthMax + 1;
RomPathChar name[NameBufferSize];
fs::RomPathChar name[NameBufferSize];
s32 i = 0;
if (this->mode & fs::OpenDirectoryMode_Directory) {
while (i < max_entries || out_entries == nullptr) {
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextDirectory(name, find)) {
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextDirectory(name, find, NameBufferSize)) {
R_CATCH(fs::ResultDbmFindFinished) { break; }
} R_END_TRY_CATCH;
@ -156,7 +156,7 @@ namespace ams::fssystem {
while (i < max_entries || out_entries == nullptr) {
auto file_pos = find->next_file;
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextFile(name, find)) {
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextFile(name, find, NameBufferSize)) {
R_CATCH(fs::ResultDbmFindFinished) { break; }
} R_END_TRY_CATCH;
@ -167,7 +167,7 @@ namespace ams::fssystem {
out_entries[i].type = fs::DirectoryEntryType_File;
RomFsFileSystem::RomFileTable::FileInfo file_info;
R_TRY(this->parent->GetRomFileTable()->OpenFile(std::addressof(file_info), this->parent->GetRomFileTable()->ConvertToFileId(file_pos)));
R_TRY(this->parent->GetRomFileTable()->OpenFile(std::addressof(file_info), this->parent->GetRomFileTable()->PositionToFileId(file_pos)));
out_entries[i].file_size = file_info.size.Get();
}
@ -204,7 +204,7 @@ namespace ams::fssystem {
}
Result RomFsFileSystem::GetRequiredWorkingMemorySize(size_t *out, fs::IStorage *storage) {
RomFileSystemInformation header;
fs::RomFileSystemInformation header;
R_TRY(buffers::DoContinuouslyUntilBufferIsAllocated([&]() -> Result {
R_TRY(storage->Read(0, std::addressof(header), sizeof(header)));
@ -224,7 +224,7 @@ namespace ams::fssystem {
buffers::EnableBlockingBufferManagerAllocation();
/* Read the header. */
RomFileSystemInformation header;
fs::RomFileSystemInformation header;
R_TRY(base->Read(0, std::addressof(header), sizeof(header)));
/* Set up our storages. */
@ -261,10 +261,10 @@ namespace ams::fssystem {
R_UNLESS(this->file_entry_storage != nullptr, fs::ResultAllocationFailureInRomFsFileSystemB());
/* Initialize the rom table. */
R_TRY(this->rom_file_table.Initialize(this->dir_bucket_storage.get(), 0, static_cast<u32>(header.directory_bucket_size),
this->dir_entry_storage.get(), 0, static_cast<u32>(header.directory_entry_size),
this->file_bucket_storage.get(), 0, static_cast<u32>(header.file_bucket_size),
this->file_entry_storage.get(), 0, static_cast<u32>(header.file_entry_size)));
R_TRY(this->rom_file_table.Initialize(fs::SubStorage(this->dir_bucket_storage.get(), 0, static_cast<u32>(header.directory_bucket_size)),
fs::SubStorage(this->dir_entry_storage.get(), 0, static_cast<u32>(header.directory_entry_size)),
fs::SubStorage(this->file_bucket_storage.get(), 0, static_cast<u32>(header.file_bucket_size)),
fs::SubStorage(this->file_entry_storage.get(), 0, static_cast<u32>(header.file_entry_size))));
/* Set members. */
this->entry_size = header.body_offset;
@ -326,7 +326,7 @@ namespace ams::fssystem {
Result RomFsFileSystem::GetEntryTypeImpl(fs::DirectoryEntryType *out, const char *path) {
R_TRY(buffers::DoContinuouslyUntilBufferIsAllocated([=, this]() -> Result {
RomDirectoryInfo dir_info;
fs::RomDirectoryInfo dir_info;
R_TRY_CATCH(this->rom_file_table.GetDirectoryInformation(std::addressof(dir_info), path)) {
R_CONVERT(fs::ResultDbmNotFound, fs::ResultPathNotFound())