2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01: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
|
2020-07-08 01:07:23 +01:00
|
|
|
#include <stratosphere/fs/fs_common.hpp>
|
|
|
|
#include <stratosphere/fs/fs_file.hpp>
|
|
|
|
#include <stratosphere/fs/fs_filesystem.hpp>
|
|
|
|
#include <stratosphere/fs/fs_operate_range.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::fs::fsa {
|
|
|
|
|
|
|
|
class IFile {
|
|
|
|
public:
|
|
|
|
virtual ~IFile() { /* ... */ }
|
|
|
|
|
|
|
|
Result Read(size_t *out, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) {
|
2022-03-06 20:08:20 +00:00
|
|
|
/* Check that we have an output pointer. */
|
2019-12-09 11:57:37 +00:00
|
|
|
R_UNLESS(out != nullptr, fs::ResultNullptrArgument());
|
2022-03-06 20:08:20 +00:00
|
|
|
|
|
|
|
/* If we have nothing to read, just succeed. */
|
2019-12-09 11:57:37 +00:00
|
|
|
if (size == 0) {
|
|
|
|
*out = 0;
|
2022-03-06 20:08:20 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
|
|
|
|
/* Check that the read is valid. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
|
|
|
|
R_UNLESS(util::IsIntValueRepresentable<s64>(size), fs::ResultOutOfRange());
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
const s64 signed_size = static_cast<s64>(size);
|
|
|
|
R_UNLESS((std::numeric_limits<s64>::max() - offset) >= signed_size, fs::ResultOutOfRange());
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
/* Do the read. */
|
|
|
|
R_RETURN(this->DoRead(out, offset, buffer, size, option));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
ALWAYS_INLINE Result Read(size_t *out, s64 offset, void *buffer, size_t size) { R_RETURN(this->Read(out, offset, buffer, size, ReadOption::None)); }
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
Result GetSize(s64 *out) {
|
|
|
|
R_UNLESS(out != nullptr, fs::ResultNullptrArgument());
|
2022-03-06 20:08:20 +00:00
|
|
|
R_RETURN(this->DoGetSize(out));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Flush() {
|
2022-03-06 20:08:20 +00:00
|
|
|
R_RETURN(this->DoFlush());
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Write(s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) {
|
2022-03-06 20:08:20 +00:00
|
|
|
/* Handle the zero-size case. */
|
2019-12-09 11:57:37 +00:00
|
|
|
if (size == 0) {
|
|
|
|
if (option.HasFlushFlag()) {
|
|
|
|
R_TRY(this->Flush());
|
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
|
|
|
|
/* Check the write is valid. */
|
|
|
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
|
|
|
|
R_UNLESS(util::IsIntValueRepresentable<s64>(size), fs::ResultOutOfRange());
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
const s64 signed_size = static_cast<s64>(size);
|
|
|
|
R_UNLESS((std::numeric_limits<s64>::max() - offset) >= signed_size, fs::ResultOutOfRange());
|
2022-03-06 20:08:20 +00:00
|
|
|
|
|
|
|
R_RETURN(this->DoWrite(offset, buffer, size, option));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetSize(s64 size) {
|
|
|
|
R_UNLESS(size >= 0, fs::ResultOutOfRange());
|
2022-03-06 20:08:20 +00:00
|
|
|
R_RETURN(this->DoSetSize(size));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
|
2022-03-06 20:08:20 +00:00
|
|
|
R_RETURN(this->DoOperateRange(dst, dst_size, op_id, offset, size, src, src_size));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OperateRange(fs::OperationId op_id, s64 offset, s64 size) {
|
2022-03-06 20:08:20 +00:00
|
|
|
R_RETURN(this->DoOperateRange(nullptr, 0, op_id, offset, size, nullptr, 0));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
/* TODO: This is a hack to allow the mitm API to work. Find a better way? */
|
|
|
|
virtual sf::cmif::DomainObjectId GetDomainObjectId() const = 0;
|
|
|
|
protected:
|
2020-02-26 00:44:36 +00:00
|
|
|
Result DryRead(size_t *out, s64 offset, size_t size, const fs::ReadOption &option, OpenMode open_mode) {
|
2021-10-07 01:58:42 +01:00
|
|
|
AMS_UNUSED(option);
|
|
|
|
|
2020-02-26 00:44:36 +00:00
|
|
|
/* Check that we can read. */
|
2020-03-27 10:40:52 +00:00
|
|
|
R_UNLESS((open_mode & OpenMode_Read) != 0, fs::ResultReadNotPermitted());
|
2020-02-26 00:44:36 +00:00
|
|
|
|
|
|
|
/* Get the file size, and validate our offset. */
|
|
|
|
s64 file_size = 0;
|
2022-03-06 20:08:20 +00:00
|
|
|
R_TRY(this->DoGetSize(std::addressof(file_size)));
|
2020-02-26 00:44:36 +00:00
|
|
|
R_UNLESS(offset <= file_size, fs::ResultOutOfRange());
|
|
|
|
|
|
|
|
*out = static_cast<size_t>(std::min(file_size - offset, static_cast<s64>(size)));
|
2022-03-06 20:08:20 +00:00
|
|
|
R_SUCCEED();
|
2020-02-26 00:44:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
Result DrySetSize(s64 size, fs::OpenMode open_mode) {
|
2021-10-07 01:58:42 +01:00
|
|
|
AMS_UNUSED(size);
|
2020-03-08 08:06:23 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
/* Check that we can write. */
|
|
|
|
R_UNLESS((open_mode & OpenMode_Write) != 0, fs::ResultWriteNotPermitted());
|
|
|
|
R_SUCCEED();
|
2020-02-26 00:44:36 +00:00
|
|
|
}
|
2020-03-27 10:40:52 +00:00
|
|
|
|
|
|
|
Result DryWrite(bool *out_append, s64 offset, size_t size, const fs::WriteOption &option, fs::OpenMode open_mode) {
|
2021-10-07 01:58:42 +01:00
|
|
|
AMS_UNUSED(option);
|
|
|
|
|
2020-03-27 10:40:52 +00:00
|
|
|
/* Check that we can write. */
|
|
|
|
R_UNLESS((open_mode & OpenMode_Write) != 0, fs::ResultWriteNotPermitted());
|
|
|
|
|
|
|
|
/* Get the file size. */
|
|
|
|
s64 file_size = 0;
|
2022-03-06 20:08:20 +00:00
|
|
|
R_TRY(this->DoGetSize(&file_size));
|
2020-03-27 10:40:52 +00:00
|
|
|
|
|
|
|
/* Determine if we need to append. */
|
2022-03-06 20:08:20 +00:00
|
|
|
*out_append = false;
|
|
|
|
if (file_size < offset + static_cast<s64>(size)) {
|
2020-03-27 10:40:52 +00:00
|
|
|
R_UNLESS((open_mode & OpenMode_AllowAppend) != 0, fs::ResultFileExtensionWithoutOpenModeAllowAppend());
|
|
|
|
*out_append = true;
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
R_SUCCEED();
|
2020-03-27 10:40:52 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoRead(size_t *out, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) = 0;
|
|
|
|
virtual Result DoGetSize(s64 *out) = 0;
|
|
|
|
virtual Result DoFlush() = 0;
|
|
|
|
virtual Result DoWrite(s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) = 0;
|
|
|
|
virtual Result DoSetSize(s64 size) = 0;
|
|
|
|
virtual Result DoOperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|