diff --git a/libraries/libstratosphere/include/stratosphere/fs.hpp b/libraries/libstratosphere/include/stratosphere/fs.hpp index 4078aaf48..f9df48d3e 100644 --- a/libraries/libstratosphere/include/stratosphere/fs.hpp +++ b/libraries/libstratosphere/include/stratosphere/fs.hpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/libraries/libstratosphere/include/stratosphere/fs/fs_file_handle_storage.hpp b/libraries/libstratosphere/include/stratosphere/fs/fs_file_handle_storage.hpp deleted file mode 100644 index 86e7a5246..000000000 --- a/libraries/libstratosphere/include/stratosphere/fs/fs_file_handle_storage.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2018-2020 Adubbz, 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 . - */ -#pragma once -#include "fs_common.hpp" -#include "fs_istorage.hpp" - -namespace ams::fs { - - class FileHandleStorage : public IStorage { - private: - static constexpr s64 InvalidSize = -1; - private: - FileHandle handle; - bool close_file; - s64 size; - os::Mutex mutex; - public: - FileHandleStorage(FileHandle handle) : FileHandleStorage(handle, false) { /* ... */ } - - FileHandleStorage(FileHandle handle, bool close_file) : handle(handle), close_file(close_file), size(InvalidSize) { - /* ... */ - } - - virtual ~FileHandleStorage() { - if (this->close_file) { - CloseFile(this->handle); - } - } - protected: - Result UpdateSize(); - public: - virtual Result Read(s64 offset, void *buffer, size_t size) override; - virtual Result Write(s64 offset, const void *buffer, size_t size) override; - virtual Result Flush() override; - virtual Result GetSize(s64 *out_size) override; - virtual Result SetSize(s64 size) override; - virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override; - }; - -} diff --git a/libraries/libstratosphere/include/stratosphere/fs/fs_file_storage.hpp b/libraries/libstratosphere/include/stratosphere/fs/fs_file_storage.hpp index 950f6d1df..2ef9cdf11 100644 --- a/libraries/libstratosphere/include/stratosphere/fs/fs_file_storage.hpp +++ b/libraries/libstratosphere/include/stratosphere/fs/fs_file_storage.hpp @@ -14,13 +14,14 @@ * along with this program. If not, see . */ #pragma once -#include "fs_common.hpp" -#include "fs_istorage.hpp" -#include "fsa/fs_ifile.hpp" +#include +#include +#include +#include namespace ams::fs { - class FileStorage : public IStorage { + class FileStorage : public IStorage, public impl::Newable { private: static constexpr s64 InvalidSize = -1; private: @@ -53,4 +54,32 @@ namespace ams::fs { virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override; }; + class FileHandleStorage : public IStorage, public impl::Newable { + private: + static constexpr s64 InvalidSize = -1; + private: + FileHandle handle; + bool close_file; + s64 size; + os::Mutex mutex; + public: + constexpr explicit FileHandleStorage(FileHandle handle, bool close_file) : handle(handle), close_file(close_file), size(InvalidSize), mutex() { /* ... */ } + constexpr explicit FileHandleStorage(FileHandle handle) : FileHandleStorage(handle, false) { /* ... */ } + + virtual ~FileHandleStorage() override { + if (this->close_file) { + CloseFile(this->handle); + } + } + protected: + Result UpdateSize(); + public: + virtual Result Read(s64 offset, void *buffer, size_t size) override; + virtual Result Write(s64 offset, const void *buffer, size_t size) override; + virtual Result Flush() override; + virtual Result GetSize(s64 *out_size) override; + virtual Result SetSize(s64 size) override; + virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override; + }; + } diff --git a/libraries/libstratosphere/source/fs/fs_file_handle_storage.cpp b/libraries/libstratosphere/source/fs/fs_file_handle_storage.cpp deleted file mode 100644 index 02bdc7abe..000000000 --- a/libraries/libstratosphere/source/fs/fs_file_handle_storage.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2018-2020 Adubbz, 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 . - */ -#include - -namespace ams::fs { - - Result FileHandleStorage::UpdateSize() { - R_SUCCEED_IF(this->size != InvalidSize); - return GetFileSize(std::addressof(this->size), this->handle); - } - - Result FileHandleStorage::Read(s64 offset, void *buffer, size_t size) { - /* Lock the mutex. */ - std::scoped_lock lk(this->mutex); - - /* Immediately succeed if there's nothing to read. */ - R_SUCCEED_IF(size == 0); - - /* Validate buffer. */ - R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); - - /* Ensure our size is valid. */ - R_TRY(this->UpdateSize()); - - /* Ensure our access is valid. */ - R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange()); - - return ReadFile(this->handle, offset, buffer, size, fs::ReadOption()); - } - - Result FileHandleStorage::Write(s64 offset, const void *buffer, size_t size) { - /* Lock the mutex. */ - std::scoped_lock lk(this->mutex); - - /* Immediately succeed if there's nothing to write. */ - R_SUCCEED_IF(size == 0); - - /* Validate buffer. */ - R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); - - /* Ensure our size is valid. */ - R_TRY(this->UpdateSize()); - - /* Ensure our access is valid. */ - R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange()); - - return WriteFile(this->handle, offset, buffer, size, fs::WriteOption()); - } - - Result FileHandleStorage::Flush() { - return FlushFile(this->handle); - } - - Result FileHandleStorage::GetSize(s64 *out_size) { - R_TRY(this->UpdateSize()); - *out_size = this->size; - return ResultSuccess(); - } - - Result FileHandleStorage::SetSize(s64 size) { - this->size = InvalidSize; - return SetFileSize(this->handle, size); - } - - Result FileHandleStorage::OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) { - switch (op_id) { - case OperationId::QueryRange: - /* Validate buffer and size. */ - R_UNLESS(dst != nullptr, fs::ResultNullptrArgument()); - R_UNLESS(dst_size == sizeof(QueryRangeInfo), fs::ResultInvalidSize()); - - return QueryRange(static_cast(dst), this->handle, offset, size); - default: - return fs::ResultUnsupportedOperationInFileStorageB(); - } - } - -} diff --git a/libraries/libstratosphere/source/fs/fs_file_storage.cpp b/libraries/libstratosphere/source/fs/fs_file_storage.cpp index 88105baab..f7ee4c14c 100644 --- a/libraries/libstratosphere/source/fs/fs_file_storage.cpp +++ b/libraries/libstratosphere/source/fs/fs_file_storage.cpp @@ -90,4 +90,75 @@ namespace ams::fs { } } + Result FileHandleStorage::UpdateSize() { + R_SUCCEED_IF(this->size != InvalidSize); + return GetFileSize(std::addressof(this->size), this->handle); + } + + Result FileHandleStorage::Read(s64 offset, void *buffer, size_t size) { + /* Lock the mutex. */ + std::scoped_lock lk(this->mutex); + + /* Immediately succeed if there's nothing to read. */ + R_SUCCEED_IF(size == 0); + + /* Validate buffer. */ + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + + /* Ensure our size is valid. */ + R_TRY(this->UpdateSize()); + + /* Ensure our access is valid. */ + R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange()); + + return ReadFile(this->handle, offset, buffer, size, fs::ReadOption()); + } + + Result FileHandleStorage::Write(s64 offset, const void *buffer, size_t size) { + /* Lock the mutex. */ + std::scoped_lock lk(this->mutex); + + /* Immediately succeed if there's nothing to write. */ + R_SUCCEED_IF(size == 0); + + /* Validate buffer. */ + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + + /* Ensure our size is valid. */ + R_TRY(this->UpdateSize()); + + /* Ensure our access is valid. */ + R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange()); + + return WriteFile(this->handle, offset, buffer, size, fs::WriteOption()); + } + + Result FileHandleStorage::Flush() { + return FlushFile(this->handle); + } + + Result FileHandleStorage::GetSize(s64 *out_size) { + R_TRY(this->UpdateSize()); + *out_size = this->size; + return ResultSuccess(); + } + + Result FileHandleStorage::SetSize(s64 size) { + this->size = InvalidSize; + return SetFileSize(this->handle, size); + } + + Result FileHandleStorage::OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) { + switch (op_id) { + case OperationId::QueryRange: + /* Validate buffer and size. */ + R_UNLESS(dst != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(dst_size == sizeof(QueryRangeInfo), fs::ResultInvalidSize()); + + return QueryRange(static_cast(dst), this->handle, offset, size); + default: + return fs::ResultUnsupportedOperationInFileStorageB(); + } + } + }