fs: fixup all OperateRange implementations

This commit is contained in:
Michael Scire 2022-03-28 00:54:10 -07:00
parent 4ad8dad416
commit 0fbf007bcf
11 changed files with 60 additions and 41 deletions

View file

@ -145,22 +145,25 @@ namespace ams::fssystem {
template<typename BasePointer>
Result AesCtrStorage<BasePointer>::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
/* Handle the zero size case. */
if (size == 0) {
if (op_id == fs::OperationId::QueryRange) {
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
/* If operation isn't invalidate, special case. */
if (op_id != fs::OperationId::Invalidate) {
/* Handle the zero-size case. */
if (size == 0) {
if (op_id == fs::OperationId::QueryRange) {
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(dst_size == sizeof(fs::QueryRangeInfo), fs::ResultInvalidSize());
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Clear();
reinterpret_cast<fs::QueryRangeInfo *>(dst)->Clear();
}
R_SUCCEED();
}
R_SUCCEED();
/* Ensure alignment. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidArgument());
}
/* Ensure alignment. */
R_UNLESS(util::IsAligned(offset, BlockSize), fs::ResultInvalidArgument());
R_UNLESS(util::IsAligned(size, BlockSize), fs::ResultInvalidArgument());
switch (op_id) {
case fs::OperationId::QueryRange:
{

View file

@ -722,10 +722,7 @@ namespace ams::fssystem {
/* Invalidate caches, if we should. */
if (op_id == fs::OperationId::Invalidate) {
SharedCache cache(this);
while (cache.AcquireNextOverlappedCache(offset, size)) {
cache.Invalidate();
}
this->InvalidateCaches();
}
R_RETURN(m_base_storage.OperateRange(dst, dst_size, op_id, offset, size, src, src_size));

View file

@ -122,6 +122,11 @@ namespace ams::fssystem {
}
Result IndirectStorage::OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
/* Validate pre-conditions. */
AMS_ASSERT(offset >= 0);
AMS_ASSERT(size >= 0);
AMS_ASSERT(this->IsInitialized());
switch (op_id) {
case fs::OperationId::Invalidate:
{

View file

@ -90,24 +90,33 @@ namespace ams::fssystem {
virtual Result DoOperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override final {
/* Validate preconditions for operation. */
s64 operate_offset;
s64 operate_size;
switch (op_id) {
case fs::OperationId::Invalidate:
R_UNLESS((m_mode & fs::OpenMode_Read) != 0, fs::ResultReadNotPermitted());
R_UNLESS((m_mode & fs::OpenMode_Write) == 0, fs::ResultUnsupportedOperateRangeForPartitionFile());
/* Set offset/size. */
operate_offset = 0;
operate_size = std::numeric_limits<s64>::max();
break;
case fs::OperationId::QueryRange:
/* Validate offset and size. */
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
R_UNLESS(offset <= static_cast<s64>(m_partition_entry->size), fs::ResultOutOfRange());
R_UNLESS(static_cast<s64>(offset + size) <= static_cast<s64>(m_partition_entry->size), fs::ResultInvalidSize());
R_UNLESS(static_cast<s64>(offset + size) >= offset, fs::ResultInvalidSize());
/* Set offset/size. */
operate_offset = m_parent->m_meta_data_size + m_partition_entry->offset + offset;
operate_size = size;
break;
default:
R_THROW(fs::ResultUnsupportedOperateRangeForPartitionFile());
}
/* Validate offset and size. */
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
R_UNLESS(offset <= static_cast<s64>(m_partition_entry->size), fs::ResultOutOfRange());
R_UNLESS(static_cast<s64>(offset + size) <= static_cast<s64>(m_partition_entry->size), fs::ResultInvalidSize());
R_UNLESS(static_cast<s64>(offset + size) >= offset, fs::ResultInvalidSize());
R_RETURN(m_parent->m_base_storage->OperateRange(dst, dst_size, op_id, m_parent->m_meta_data_size + m_partition_entry->offset + offset, size, src, src_size));
R_RETURN(m_parent->m_base_storage->OperateRange(dst, dst_size, op_id, operate_offset, operate_size, src, src_size));
}
public:
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {

View file

@ -77,21 +77,24 @@ namespace ams::fssystem {
virtual Result DoOperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override {
switch (op_id) {
case fs::OperationId::Invalidate:
{
R_RETURN(buffers::DoContinuouslyUntilBufferIsAllocated([&]() -> Result {
R_RETURN(m_parent->GetBaseStorage()->OperateRange(fs::OperationId::Invalidate, 0, std::numeric_limits<s64>::max()));
}, AMS_CURRENT_FUNCTION_NAME));
}
case fs::OperationId::QueryRange:
{
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
R_UNLESS(this->GetSize() >= 0, fs::ResultOutOfRange());
R_UNLESS(offset >= 0, fs::ResultInvalidOffset());
R_UNLESS(this->GetSize() >= offset, fs::ResultOutOfRange());
auto operate_size = size;
if (offset + operate_size > this->GetSize() || offset + operate_size < offset) {
operate_size = this->GetSize() - offset;
}
R_TRY(buffers::DoContinuouslyUntilBufferIsAllocated([&]() -> Result {
R_TRY(m_parent->GetBaseStorage()->OperateRange(dst, dst_size, op_id, m_start + offset, operate_size, src, src_size));
R_SUCCEED();
R_RETURN(buffers::DoContinuouslyUntilBufferIsAllocated([&]() -> Result {
R_RETURN(m_parent->GetBaseStorage()->OperateRange(dst, dst_size, op_id, m_start + offset, operate_size, src, src_size));
}, AMS_CURRENT_FUNCTION_NAME));
R_SUCCEED();
}
default:
R_THROW(fs::ResultUnsupportedOperateRangeForRomFsFile());