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/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
|
|
|
Result FileStorage::UpdateSize() {
|
2021-10-10 07:14:06 +00:00
|
|
|
R_SUCCEED_IF(m_size != InvalidSize);
|
|
|
|
return m_base_file->GetSize(std::addressof(m_size));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::Read(s64 offset, void *buffer, size_t size) {
|
|
|
|
/* Immediately succeed if there's nothing to read. */
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(size == 0);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Validate buffer. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
|
|
|
|
/* Ensure our size is valid. */
|
|
|
|
R_TRY(this->UpdateSize());
|
|
|
|
|
|
|
|
/* Ensure our access is valid. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(IStorage::CheckAccessRange(offset, size, m_size), fs::ResultOutOfRange());
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
size_t read_size;
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_base_file->Read(std::addressof(read_size), offset, buffer, size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::Write(s64 offset, const void *buffer, size_t size) {
|
|
|
|
/* Immediately succeed if there's nothing to write. */
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(size == 0);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Validate buffer. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
|
|
|
|
/* Ensure our size is valid. */
|
|
|
|
R_TRY(this->UpdateSize());
|
|
|
|
|
|
|
|
/* Ensure our access is valid. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(IStorage::CheckAccessRange(offset, size, m_size), fs::ResultOutOfRange());
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_base_file->Write(offset, buffer, size, fs::WriteOption());
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::Flush() {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_base_file->Flush();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::GetSize(s64 *out_size) {
|
|
|
|
R_TRY(this->UpdateSize());
|
2021-10-10 07:14:06 +00:00
|
|
|
*out_size = m_size;
|
2019-12-09 11:57:37 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::SetSize(s64 size) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_size = InvalidSize;
|
|
|
|
return m_base_file->SetSize(size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileStorage::OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
|
|
|
|
switch (op_id) {
|
2021-04-30 03:09:45 +00:00
|
|
|
case OperationId::Invalidate:
|
2019-12-09 11:57:37 +00:00
|
|
|
case OperationId::QueryRange:
|
|
|
|
if (size == 0) {
|
|
|
|
if (op_id == OperationId::QueryRange) {
|
|
|
|
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(dst_size == sizeof(QueryRangeInfo), fs::ResultInvalidSize());
|
|
|
|
reinterpret_cast<QueryRangeInfo *>(dst)->Clear();
|
|
|
|
}
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
R_TRY(this->UpdateSize());
|
2020-12-05 11:15:48 +00:00
|
|
|
R_UNLESS(IStorage::CheckOffsetAndSize(offset, size), fs::ResultOutOfRange());
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_base_file->OperateRange(dst, dst_size, op_id, offset, size, src, src_size);
|
2019-12-09 11:57:37 +00:00
|
|
|
default:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::ResultUnsupportedOperationInFileStorageA();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
Result FileStorageBasedFileSystem::Initialize(std::shared_ptr<fs::fsa::IFileSystem> base_file_system, const fs::Path &path, fs::OpenMode mode) {
|
2020-05-11 22:04:51 +00:00
|
|
|
/* Open the file. */
|
|
|
|
std::unique_ptr<fs::fsa::IFile> base_file;
|
|
|
|
R_TRY(base_file_system->OpenFile(std::addressof(base_file), path, mode));
|
|
|
|
|
|
|
|
/* Set the file. */
|
|
|
|
this->SetFile(std::move(base_file));
|
2021-10-10 07:14:06 +00:00
|
|
|
m_base_file_system = std::move(base_file_system);
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:45:02 +00:00
|
|
|
Result FileHandleStorage::UpdateSize() {
|
2021-10-10 07:14:06 +00:00
|
|
|
R_SUCCEED_IF(m_size != InvalidSize);
|
|
|
|
return GetFileSize(std::addressof(m_size), m_handle);
|
2020-03-27 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::Read(s64 offset, void *buffer, size_t size) {
|
|
|
|
/* Lock the mutex. */
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_mutex);
|
2020-03-27 18:45:02 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(IStorage::CheckAccessRange(offset, size, m_size), fs::ResultOutOfRange());
|
2020-03-27 18:45:02 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
return ReadFile(m_handle, offset, buffer, size, fs::ReadOption());
|
2020-03-27 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::Write(s64 offset, const void *buffer, size_t size) {
|
|
|
|
/* Lock the mutex. */
|
2021-10-10 07:14:06 +00:00
|
|
|
std::scoped_lock lk(m_mutex);
|
2020-03-27 18:45:02 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(IStorage::CheckAccessRange(offset, size, m_size), fs::ResultOutOfRange());
|
2020-03-27 18:45:02 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
return WriteFile(m_handle, offset, buffer, size, fs::WriteOption());
|
2020-03-27 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::Flush() {
|
2021-10-10 07:14:06 +00:00
|
|
|
return FlushFile(m_handle);
|
2020-03-27 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::GetSize(s64 *out_size) {
|
|
|
|
R_TRY(this->UpdateSize());
|
2021-10-10 07:14:06 +00:00
|
|
|
*out_size = m_size;
|
2020-03-27 18:45:02 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::SetSize(s64 size) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_size = InvalidSize;
|
|
|
|
return SetFileSize(m_handle, size);
|
2020-03-27 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result FileHandleStorage::OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(src, src_size);
|
|
|
|
|
2020-03-27 18:45:02 +00:00
|
|
|
switch (op_id) {
|
|
|
|
case OperationId::QueryRange:
|
|
|
|
/* Validate buffer and size. */
|
|
|
|
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(dst_size == sizeof(QueryRangeInfo), fs::ResultInvalidSize());
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
return QueryRange(static_cast<QueryRangeInfo *>(dst), m_handle, offset, size);
|
2020-03-27 18:45:02 +00:00
|
|
|
default:
|
|
|
|
return fs::ResultUnsupportedOperationInFileStorageB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|