2020-03-08 09:06:23 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-03-08 09:06:23 +01: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 04:21:08 +02:00
|
|
|
#include <stratosphere/fs/impl/fs_newable.hpp>
|
|
|
|
#include <stratosphere/fs/fs_istorage.hpp>
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
2022-03-24 16:43:40 +01:00
|
|
|
/* ACCURATE_TO_VERSION: Unknown */
|
2020-03-08 09:06:23 +01:00
|
|
|
class SubStorage : public ::ams::fs::IStorage, public ::ams::fs::impl::Newable {
|
|
|
|
private:
|
2021-10-10 09:14:06 +02:00
|
|
|
std::shared_ptr<IStorage> m_shared_base_storage;
|
|
|
|
fs::IStorage *m_base_storage;
|
|
|
|
s64 m_offset;
|
|
|
|
s64 m_size;
|
|
|
|
bool m_resizable;
|
2020-03-08 09:06:23 +01:00
|
|
|
private:
|
|
|
|
constexpr bool IsValid() const {
|
2021-10-10 09:14:06 +02:00
|
|
|
return m_base_storage != nullptr;
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
public:
|
2021-10-10 09:14:06 +02:00
|
|
|
SubStorage() : m_shared_base_storage(), m_base_storage(nullptr), m_offset(0), m_size(0), m_resizable(false) { /* ... */ }
|
2020-03-08 09:06:23 +01:00
|
|
|
|
2022-02-21 23:56:10 +01:00
|
|
|
SubStorage(const SubStorage &rhs) : m_shared_base_storage(rhs.m_shared_base_storage), m_base_storage(rhs.m_base_storage), m_offset(rhs.m_offset), m_size(rhs.m_size), m_resizable(rhs.m_resizable) { /* ... */}
|
2020-03-08 09:06:23 +01:00
|
|
|
SubStorage &operator=(const SubStorage &rhs) {
|
|
|
|
if (this != std::addressof(rhs)) {
|
2022-02-21 23:56:10 +01:00
|
|
|
m_shared_base_storage = rhs.m_shared_base_storage;
|
|
|
|
m_base_storage = rhs.m_base_storage;
|
|
|
|
m_offset = rhs.m_offset;
|
|
|
|
m_size = rhs.m_size;
|
|
|
|
m_resizable = rhs.m_resizable;
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
SubStorage(IStorage *storage, s64 o, s64 sz) : m_shared_base_storage(), m_base_storage(storage), m_offset(o), m_size(sz), m_resizable(false) {
|
2020-03-08 09:06:23 +01:00
|
|
|
AMS_ABORT_UNLESS(this->IsValid());
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ABORT_UNLESS(m_offset >= 0);
|
|
|
|
AMS_ABORT_UNLESS(m_size >= 0);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
SubStorage(std::shared_ptr<IStorage> storage, s64 o, s64 sz) : m_shared_base_storage(storage), m_base_storage(storage.get()), m_offset(o), m_size(sz), m_resizable(false) {
|
2020-03-08 09:06:23 +01:00
|
|
|
AMS_ABORT_UNLESS(this->IsValid());
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ABORT_UNLESS(m_offset >= 0);
|
|
|
|
AMS_ABORT_UNLESS(m_size >= 0);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 23:56:10 +01:00
|
|
|
SubStorage(SubStorage *sub, s64 o, s64 sz) : m_shared_base_storage(sub->m_shared_base_storage), m_base_storage(sub->m_base_storage), m_offset(o + sub->m_offset), m_size(sz), m_resizable(false) {
|
2020-03-08 09:06:23 +01:00
|
|
|
AMS_ABORT_UNLESS(this->IsValid());
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ABORT_UNLESS(m_offset >= 0);
|
|
|
|
AMS_ABORT_UNLESS(m_size >= 0);
|
|
|
|
AMS_ABORT_UNLESS(sub->m_size >= o + sz);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 07:42:32 +01:00
|
|
|
ALWAYS_INLINE ::ams::fs::IStorage *operator->() {
|
|
|
|
return this;
|
|
|
|
}
|
2020-03-08 09:06:23 +01:00
|
|
|
public:
|
|
|
|
void SetResizable(bool rsz) {
|
2021-10-10 09:14:06 +02:00
|
|
|
m_resizable = rsz;
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual Result Read(s64 offset, void *buffer, size_t size) override {
|
|
|
|
/* Ensure we're initialized. */
|
2022-03-26 07:29:36 +01:00
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
/* Succeed immediately on zero-sized operation. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Validate arguments and read. */
|
2022-03-26 07:29:36 +01:00
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_TRY(IStorage::CheckAccessRange(offset, size, m_size));
|
2021-10-10 09:14:06 +02:00
|
|
|
return m_base_storage->Read(m_offset + offset, buffer, size);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result Write(s64 offset, const void *buffer, size_t size) override{
|
|
|
|
/* Ensure we're initialized. */
|
2022-03-26 07:29:36 +01:00
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
/* Succeed immediately on zero-sized operation. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
|
|
|
|
|
|
|
/* Validate arguments and write. */
|
2022-03-26 07:29:36 +01:00
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_TRY(IStorage::CheckAccessRange(offset, size, m_size));
|
2021-10-10 09:14:06 +02:00
|
|
|
return m_base_storage->Write(m_offset + offset, buffer, size);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result Flush() override {
|
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
2021-10-10 09:14:06 +02:00
|
|
|
return m_base_storage->Flush();
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result SetSize(s64 size) override {
|
|
|
|
/* Ensure we're initialized and validate arguments. */
|
2021-10-10 09:14:06 +02:00
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
2022-03-13 10:32:34 +01:00
|
|
|
R_UNLESS(m_resizable, fs::ResultUnsupportedSetSizeForNotResizableSubStorage());
|
2022-03-26 07:29:36 +01:00
|
|
|
|
|
|
|
R_TRY(IStorage::CheckOffsetAndSize(m_offset, size));
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
/* Ensure that we're allowed to set size. */
|
|
|
|
s64 cur_size;
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(m_base_storage->GetSize(std::addressof(cur_size)));
|
2022-03-13 10:32:34 +01:00
|
|
|
R_UNLESS(cur_size == m_offset + m_size, fs::ResultUnsupportedSetSizeForResizableSubStorage());
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
/* Set the size. */
|
2021-10-10 09:14:06 +02:00
|
|
|
R_TRY(m_base_storage->SetSize(m_offset + size));
|
2020-03-08 09:06:23 +01:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
m_size = size;
|
2020-03-08 09:06:23 +01:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetSize(s64 *out) override {
|
|
|
|
/* Ensure we're initialized. */
|
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
*out = m_size;
|
2020-03-08 09:06:23 +01:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override {
|
|
|
|
/* Ensure we're initialized. */
|
2022-03-13 10:32:34 +01:00
|
|
|
R_UNLESS(this->IsValid(), fs::ResultNotInitialized());
|
2020-03-08 09:06:23 +01:00
|
|
|
|
2022-03-26 07:29:36 +01:00
|
|
|
/* If we're not invalidating, sanity check arguments. */
|
|
|
|
if (op_id != fs::OperationId::Invalidate) {
|
|
|
|
/* Succeed immediately on zero-sized operation other than invalidate. */
|
|
|
|
R_SUCCEED_IF(size == 0);
|
|
|
|
|
|
|
|
/* Check access extents. */
|
|
|
|
R_TRY(IStorage::CheckOffsetAndSize(offset, size));
|
|
|
|
}
|
2020-03-08 09:06:23 +01:00
|
|
|
|
2022-03-26 07:29:36 +01:00
|
|
|
/* Perform the operation. */
|
|
|
|
R_RETURN(m_base_storage->OperateRange(dst, dst_size, op_id, m_offset + offset, size, src, src_size));
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
using IStorage::OperateRange;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|