fssystem: cleanup PartitionFileSystemCore

This commit is contained in:
Michael Scire 2020-03-27 02:03:31 -07:00
parent 98087ef346
commit 15229b61a4
2 changed files with 146 additions and 140 deletions

View file

@ -24,6 +24,7 @@ namespace ams::fssystem {
template<typename MetaType>
class PartitionFileSystemCore : public fs::impl::Newable, public fs::fsa::IFileSystem {
NON_COPYABLE(PartitionFileSystemCore);
NON_MOVEABLE(PartitionFileSystemCore);
private:
class PartitionFile;
class PartitionDirectory;
@ -40,7 +41,7 @@ namespace ams::fssystem {
PartitionFileSystemCore();
virtual ~PartitionFileSystemCore() override;
Result Initialize(std::unique_ptr<MetaType> &meta_data, std::shared_ptr<fs::IStorage> base_storage);
Result Initialize(std::unique_ptr<MetaType> &&meta_data, std::shared_ptr<fs::IStorage> base_storage);
Result Initialize(MetaType *meta_data, std::shared_ptr<fs::IStorage> base_storage);
Result Initialize(fs::IStorage *base_storage);
Result Initialize(std::shared_ptr<fs::IStorage> base_storage);

View file

@ -17,8 +17,8 @@
namespace ams::fssystem {
namespace {
namespace {
class PartitionFileSystemDefaultAllocator : public MemoryResource {
private:
virtual void *AllocateImpl(size_t size, size_t alignment) override {
@ -42,12 +42,10 @@ namespace ams::fssystem {
class PartitionFileSystemCore<MetaType>::PartitionFile : public fs::fsa::IFile, public fs::impl::Newable {
private:
const typename MetaType::PartitionEntry *partition_entry;
PartitionFileSystemCore<MetaType> *parent;
fs::OpenMode mode;
const PartitionFileSystemCore<MetaType> *parent;
const fs::OpenMode mode;
public:
PartitionFile(PartitionFileSystemCore<MetaType> *parent, const typename MetaType::PartitionEntry *partition_entry, fs::OpenMode mode) : partition_entry(partition_entry), parent(parent), mode(mode) { /* ... */ }
virtual ~PartitionFile() { /* ... */ }
private:
virtual Result ReadImpl(size_t *out, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) override final;
@ -58,7 +56,7 @@ namespace ams::fssystem {
virtual Result FlushImpl() override final {
/* Nothing to do if writing disallowed. */
R_SUCCEED_IF(!(this->mode & fs::OpenMode_Write));
R_SUCCEED_IF((this->mode & fs::OpenMode_Write) == 0);
/* Flush base storage. */
return this->parent->base_storage->Flush();
@ -98,7 +96,7 @@ namespace ams::fssystem {
default:
return fs::ResultUnsupportedOperationInPartitionFileB();
}
/* Validate offset and size. */
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
R_UNLESS(offset <= static_cast<s64>(this->partition_entry->size), fs::ResultOutOfRange());
@ -107,7 +105,6 @@ namespace ams::fssystem {
return this->parent->base_storage->OperateRange(dst, dst_size, op_id, this->parent->meta_data_size + this->partition_entry->offset + offset, size, src, src_size);
}
public:
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
/* TODO: How should this be handled? */
@ -133,32 +130,30 @@ namespace ams::fssystem {
class PartitionFileSystemCore<MetaType>::PartitionDirectory : public fs::fsa::IDirectory, public fs::impl::Newable {
private:
u32 cur_index;
PartitionFileSystemCore<MetaType> *parent;
fs::OpenDirectoryMode mode;
const PartitionFileSystemCore<MetaType> *parent;
const fs::OpenDirectoryMode mode;
public:
PartitionDirectory(PartitionFileSystemCore<MetaType> *parent, fs::OpenDirectoryMode mode) : cur_index(0), parent(parent), mode(mode) { /* ... */ }
virtual ~PartitionDirectory() { /* ... */ }
public:
virtual Result ReadImpl(s64 *out_count, fs::DirectoryEntry *out_entries, s64 max_entries) override final {
/* There are no subdirectories. */
if (!(this->mode & fs::OpenDirectoryMode_File)) {
if ((this->mode & fs::OpenDirectoryMode_File) == 0) {
*out_count = 0;
return ResultSuccess();
}
/* Calculate number of entries. */
const s32 entry_count = std::min(max_entries, static_cast<s64>(this->parent->meta_data->GetEntryCount() - this->cur_index));
const s64 entry_count = std::min(max_entries, static_cast<s64>(this->parent->meta_data->GetEntryCount() - this->cur_index));
/* Populate output directory entries. */
for (s32 i = 0; i < entry_count; i++, this->cur_index++) {
for (auto i = 0; i < entry_count; i++, this->cur_index++) {
fs::DirectoryEntry &dir_entry = out_entries[i];
/* Setup the output directory entry. */
dir_entry.type = fs::DirectoryEntryType_File;
dir_entry.file_size = this->parent->meta_data->GetEntry(this->cur_index)->size;
std::strncpy(dir_entry.name, this->parent->meta_data->GetEntryName(this->cur_index), fs::EntryNameLengthMax);
dir_entry.name[fs::EntryNameLengthMax] = StringTraits::NullTerminator;
std::strncpy(dir_entry.name, this->parent->meta_data->GetEntryName(this->cur_index), sizeof(dir_entry.name) - 1);
dir_entry.name[sizeof(dir_entry.name) - 1] = StringTraits::NullTerminator;
}
*out_count = entry_count;
@ -183,8 +178,132 @@ namespace ams::fssystem {
};
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::CleanDirectoryRecursivelyImpl(const char *path) {
return fs::ResultUnsupportedOperationInPartitionFileSystemA();
PartitionFileSystemCore<MetaType>::PartitionFileSystemCore() : initialized(false) {
/* ... */
}
template <typename MetaType>
PartitionFileSystemCore<MetaType>::~PartitionFileSystemCore() {
/* ... */
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(fs::IStorage *base_storage, MemoryResource *allocator) {
/* Validate preconditions. */
R_UNLESS(!this->initialized, fs::ResultPreconditionViolation());
/* Allocate meta data. */
this->unique_meta_data = std::make_unique<MetaType>();
R_UNLESS(this->unique_meta_data != nullptr, fs::ResultAllocationFailureInPartitionFileSystemA());
/* Initialize meta data. */
R_TRY(this->unique_meta_data->Initialize(base_storage, allocator));
/* Initialize members. */
this->meta_data = this->unique_meta_data.get();
this->base_storage = base_storage;
this->meta_data_size = this->meta_data->GetMetaDataSize();
this->initialized = true;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::unique_ptr<MetaType> &&meta_data, std::shared_ptr<fs::IStorage> base_storage) {
this->unique_meta_data = std::move(meta_data);
return this->Initialize(this->unique_meta_data.get(), base_storage);
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(MetaType *meta_data, std::shared_ptr<fs::IStorage> base_storage) {
/* Validate preconditions. */
R_UNLESS(!this->initialized, fs::ResultPreconditionViolation());
/* Initialize members. */
this->shared_storage = std::move(base_storage);
this->base_storage = this->shared_storage.get();
this->meta_data = meta_data;
this->meta_data_size = this->meta_data->GetMetaDataSize();
this->initialized = true;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(fs::IStorage *base_storage) {
return this->Initialize(base_storage, std::addressof(g_partition_filesystem_default_allocator));
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::shared_ptr<fs::IStorage> base_storage) {
this->shared_storage = std::move(base_storage);
return this->Initialize(this->shared_storage.get());
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::shared_ptr<fs::IStorage> base_storage, MemoryResource *allocator) {
this->shared_storage = std::move(base_storage);
return this->Initialize(this->shared_storage.get(), allocator);
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::GetFileBaseOffset(s64 *out_offset, const char *path) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
/* Obtain and validate the entry index. */
const s32 entry_index = this->meta_data->GetEntryIndex(path + 1);
R_UNLESS(entry_index >= 0, fs::ResultPathNotFound());
/* Output offset. */
*out_offset = this->meta_data_size + this->meta_data->GetEntry(entry_index)->offset;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::GetEntryTypeImpl(fs::DirectoryEntryType *out, const char *path) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
R_UNLESS(PathTool::IsSeparator(path[0]), fs::ResultInvalidPathFormat());
/* Check if the path is for a directory. */
if (std::strncmp(path, PathTool::RootPath, sizeof(PathTool::RootPath)) == 0) {
*out = fs::DirectoryEntryType_Directory;
return ResultSuccess();
}
/* Ensure that path is for a file. */
R_UNLESS(this->meta_data->GetEntryIndex(path + 1) >= 0, fs::ResultPathNotFound());
*out = fs::DirectoryEntryType_File;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::OpenFileImpl(std::unique_ptr<fs::fsa::IFile> *out_file, const char *path, fs::OpenMode mode) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
/* Obtain and validate the entry index. */
const s32 entry_index = this->meta_data->GetEntryIndex(path + 1);
R_UNLESS(entry_index >= 0, fs::ResultPathNotFound());
/* Create and output the file directory. */
std::unique_ptr file = std::make_unique<PartitionFile>(this, this->meta_data->GetEntry(entry_index), mode);
R_UNLESS(file != nullptr, fs::ResultAllocationFailureInPartitionFileSystemB());
*out_file = std::move(file);
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::OpenDirectoryImpl(std::unique_ptr<fs::fsa::IDirectory> *out_dir, const char *path, fs::OpenDirectoryMode mode) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
R_UNLESS(std::strncmp(path, PathTool::RootPath, sizeof(PathTool::RootPath)) == 0, fs::ResultPathNotFound());
/* Create and output the partition directory. */
std::unique_ptr directory = std::make_unique<PartitionDirectory>(this, mode);
R_UNLESS(directory != nullptr, fs::ResultAllocationFailureInPartitionFileSystemC());
*out_dir = std::move(directory);
return ResultSuccess();
}
template <typename MetaType>
@ -193,8 +312,8 @@ namespace ams::fssystem {
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::CommitProvisionallyImpl(s64 counter) {
return fs::ResultUnsupportedOperationInPartitionFileSystemB();
Result PartitionFileSystemCore<MetaType>::CleanDirectoryRecursivelyImpl(const char *path) {
return fs::ResultUnsupportedOperationInPartitionFileSystemA();
}
template <typename MetaType>
@ -222,54 +341,6 @@ namespace ams::fssystem {
return fs::ResultUnsupportedOperationInPartitionFileSystemA();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::GetEntryTypeImpl(fs::DirectoryEntryType *out, const char *path) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
R_UNLESS(PathTool::IsSeparator(path[0]), fs::ResultInvalidPathFormat());
/* Check if the path is for a directory. */
if (std::strncmp(path, PathTool::RootPath, sizeof(PathTool::RootPath)) == 0) {
*out = fs::DirectoryEntryType_Directory;
return ResultSuccess();
}
/* Ensure that path is for a file. */
R_UNLESS(this->meta_data->GetEntryIndex(path + 1) >= 0, fs::ResultPathNotFound());
*out = fs::DirectoryEntryType_File;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::OpenDirectoryImpl(std::unique_ptr<fs::fsa::IDirectory> *out_dir, const char *path, fs::OpenDirectoryMode mode) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
R_UNLESS(std::strncmp(path, PathTool::RootPath, sizeof(PathTool::RootPath)) == 0, fs::ResultPathNotFound());
/* Create and output the partition directory. */
std::unique_ptr directory = std::make_unique<PartitionDirectory>(this, mode);
R_UNLESS(directory != nullptr, fs::ResultAllocationFailureInPartitionFileSystemA());
*out_dir = std::move(directory);
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::OpenFileImpl(std::unique_ptr<fs::fsa::IFile> *out_file, const char *path, fs::OpenMode mode) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
/* Obtain and validate the entry index. */
const s32 entry_index = this->meta_data->GetEntryIndex(path + 1);
R_UNLESS(entry_index >= 0, fs::ResultPathNotFound());
/* Create and output the file directory. */
std::unique_ptr file = std::make_unique<PartitionFile>(this, this->meta_data->GetEntry(entry_index), mode);
R_UNLESS(file != nullptr, fs::ResultAllocationFailureInPartitionFileSystemB());
*out_file = std::move(file);
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::RenameDirectoryImpl(const char *old_path, const char *new_path) {
return fs::ResultUnsupportedOperationInPartitionFileSystemA();
@ -281,74 +352,8 @@ namespace ams::fssystem {
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::GetFileBaseOffset(s64 *out_offset, const char *path) {
/* Validate preconditions. */
R_UNLESS(this->initialized, fs::ResultPreconditionViolation());
/* Obtain and validate the entry index. */
const s32 entry_index = this->meta_data->GetEntryIndex(path + 1);
R_UNLESS(entry_index >= 0, fs::ResultPathNotFound());
/* Output offset. */
*out_offset = this->meta_data_size + this->meta_data->GetEntry(entry_index)->offset;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(fs::IStorage *base_storage, MemoryResource *allocator) {
/* Validate preconditions. */
R_UNLESS(!this->initialized, fs::ResultPreconditionViolation());
/* Allocate meta data. */
this->unique_meta_data = std::make_unique<MetaType>();
R_UNLESS(this->unique_meta_data != nullptr, fs::ResultAllocationFailureInPartitionFileSystemA());
/* Initialize meta data. */
R_TRY(this->unique_meta_data->Initialize(base_storage, allocator));
/* Initialize members. */
this->meta_data = this->unique_meta_data.get();
this->base_storage = base_storage;
this->meta_data_size = this->meta_data->GetMetaDataSize();
this->initialized = true;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::unique_ptr<MetaType> &meta_data, std::shared_ptr<fs::IStorage> base_storage) {
this->unique_meta_data = std::move(meta_data);
return this->Initialize(this->unique_meta_data.get(), base_storage);
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(MetaType *meta_data, std::shared_ptr<fs::IStorage> base_storage) {
/* Validate preconditions. */
R_UNLESS(!this->initialized, fs::ResultPreconditionViolation());
/* Initialize members. */
this->shared_storage = base_storage;
this->base_storage = this->shared_storage.get();
this->meta_data = meta_data;
this->meta_data_size = this->meta_data->GetMetaDataSize();
this->initialized = true;
return ResultSuccess();
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(fs::IStorage *base_storage) {
return this->Initialize(base_storage, std::addressof(g_partition_filesystem_default_allocator));
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::shared_ptr<fs::IStorage> base_storage) {
this->shared_storage = base_storage;
return this->Initialize(this->shared_storage.get());
}
template <typename MetaType>
Result PartitionFileSystemCore<MetaType>::Initialize(std::shared_ptr<fs::IStorage> base_storage, MemoryResource *allocator) {
this->shared_storage = base_storage;
return this->Initialize(this->shared_storage.get(), allocator);
Result PartitionFileSystemCore<MetaType>::CommitProvisionallyImpl(s64 counter) {
return fs::ResultUnsupportedOperationInPartitionFileSystemB();
}
template class PartitionFileSystemCore<PartitionFileSystemMeta>;