2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2021-10-01 02:21:08 +00:00
|
|
|
#include <stratosphere/fs/fs_common.hpp>
|
|
|
|
#include <stratosphere/fs/fs_file.hpp>
|
|
|
|
#include <stratosphere/fs/fs_operate_range.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
2022-03-26 06:29:36 +00:00
|
|
|
/* ACCURATE_TO_VERSION: 14.3.0.0 */
|
2019-12-09 11:57:37 +00:00
|
|
|
class IStorage {
|
|
|
|
public:
|
|
|
|
virtual ~IStorage() { /* ... */ }
|
|
|
|
|
|
|
|
virtual Result Read(s64 offset, void *buffer, size_t size) = 0;
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result Write(s64 offset, const void *buffer, size_t size) = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
virtual Result Flush() = 0;
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result SetSize(s64 size) = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
virtual Result GetSize(s64 *out) = 0;
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
virtual Result OperateRange(OperationId op_id, s64 offset, s64 size) {
|
2022-03-26 06:29:36 +00:00
|
|
|
R_RETURN(this->OperateRange(nullptr, 0, op_id, offset, size, nullptr, 0));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
public:
|
2022-03-26 06:29:36 +00:00
|
|
|
static inline Result CheckAccessRange(s64 offset, s64 size, s64 total_size) {
|
|
|
|
R_UNLESS(offset >= 0, fs::ResultInvalidOffset());
|
|
|
|
R_UNLESS(size >= 0, fs::ResultInvalidSize());
|
|
|
|
R_UNLESS(util::CanAddWithoutOverflow<s64>(offset, size), fs::ResultOutOfRange());
|
|
|
|
R_UNLESS(offset + size <= total_size, fs::ResultOutOfRange());
|
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 06:29:36 +00:00
|
|
|
static ALWAYS_INLINE Result CheckAccessRange(s64 offset, size_t size, s64 total_size) {
|
|
|
|
R_RETURN(CheckAccessRange(offset, static_cast<s64>(size), total_size));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 06:29:36 +00:00
|
|
|
static inline Result CheckOffsetAndSize(s64 offset, s64 size) {
|
|
|
|
R_UNLESS(offset >= 0, fs::ResultInvalidOffset());
|
|
|
|
R_UNLESS(size >= 0, fs::ResultInvalidSize());
|
|
|
|
R_UNLESS(util::CanAddWithoutOverflow<s64>(offset, size), fs::ResultOutOfRange());
|
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 06:29:36 +00:00
|
|
|
static ALWAYS_INLINE Result CheckOffsetAndSize(s64 offset, size_t size) {
|
|
|
|
R_RETURN(CheckOffsetAndSize(offset, static_cast<s64>(size)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Result CheckOffsetAndSizeWithResult(s64 offset, s64 size, Result fail_result) {
|
|
|
|
R_TRY_CATCH(CheckOffsetAndSize(offset, size)) {
|
|
|
|
R_CONVERT_ALL(fail_result);
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
R_SUCCEED();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALWAYS_INLINE Result CheckOffsetAndSizeWithResult(s64 offset, size_t size, Result fail_result) {
|
|
|
|
R_RETURN(CheckOffsetAndSizeWithResult(offset, static_cast<s64>(size), fail_result));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadOnlyStorageAdapter : public IStorage {
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
std::shared_ptr<IStorage> m_shared_storage;
|
|
|
|
std::unique_ptr<IStorage> m_unique_storage;
|
|
|
|
IStorage *m_storage;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2022-03-06 20:08:20 +00:00
|
|
|
explicit ReadOnlyStorageAdapter(IStorage *s) : m_unique_storage(s) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_storage = m_unique_storage.get();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
explicit ReadOnlyStorageAdapter(std::shared_ptr<IStorage> s) : m_shared_storage(s) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_storage = m_shared_storage.get();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
explicit ReadOnlyStorageAdapter(std::unique_ptr<IStorage> s) : m_unique_storage(std::move(s)) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_storage = m_unique_storage.get();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ReadOnlyStorageAdapter() { /* ... */ }
|
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result Read(s64 offset, void *buffer, size_t size) override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_storage->Read(offset, buffer, size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result Flush() override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_storage->Flush();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual Result GetSize(s64 *out) override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_storage->GetSize(out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2020-03-08 08:06:23 +00:00
|
|
|
|
|
|
|
virtual Result Write(s64 offset, const void *buffer, size_t size) override {
|
|
|
|
/* TODO: Better result? Is it possible to get a more specific one? */
|
2021-10-07 00:58:42 +00:00
|
|
|
AMS_UNUSED(offset, buffer, size);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedOperation());
|
2020-03-08 08:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result SetSize(s64 size) override {
|
|
|
|
/* TODO: Better result? Is it possible to get a more specific one? */
|
2021-10-07 00:58:42 +00:00
|
|
|
AMS_UNUSED(size);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedOperation());
|
2020-03-08 08:06:23 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
2022-03-24 23:21:03 +00:00
|
|
|
template<typename T>
|
|
|
|
concept PointerToStorage = ::ams::util::RawOrSmartPointerTo<T, ::ams::fs::IStorage>;
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|