2020-03-09 10:10:12 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-03-09 10:10:12 +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
|
|
|
|
#include <stratosphere/fs/fs_common.hpp>
|
|
|
|
#include <stratosphere/fs/impl/fs_newable.hpp>
|
|
|
|
#include <stratosphere/fs/fsa/fs_ifile.hpp>
|
|
|
|
#include <stratosphere/fs/fsa/fs_idirectory.hpp>
|
|
|
|
#include <stratosphere/fs/fsa/fs_ifilesystem.hpp>
|
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
2022-03-24 15:43:40 +00:00
|
|
|
/* ACCURATE_TO_VERSION: Unknown */
|
|
|
|
|
2020-03-09 10:10:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ReadOnlyFile : public fsa::IFile, public impl::Newable {
|
|
|
|
NON_COPYABLE(ReadOnlyFile);
|
|
|
|
NON_MOVEABLE(ReadOnlyFile);
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
std::unique_ptr<fsa::IFile> m_base_file;
|
2020-03-09 10:10:12 +00:00
|
|
|
public:
|
2022-03-06 20:08:20 +00:00
|
|
|
explicit ReadOnlyFile(std::unique_ptr<fsa::IFile> &&f) : m_base_file(std::move(f)) { AMS_ASSERT(m_base_file != nullptr); }
|
2020-03-09 10:10:12 +00:00
|
|
|
virtual ~ReadOnlyFile() { /* ... */ }
|
|
|
|
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) override final {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_file->Read(out, offset, buffer, size, option));
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetSize(s64 *out) override final {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_file->GetSize(out));
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoFlush() override final {
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoWrite(s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) override final {
|
2022-03-06 20:08:20 +00:00
|
|
|
bool need_append;
|
|
|
|
R_TRY(this->DryWrite(std::addressof(need_append), offset, size, option, fs::OpenMode_Read));
|
|
|
|
|
|
|
|
AMS_ASSERT(!need_append);
|
|
|
|
|
|
|
|
AMS_UNUSED(buffer);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFile());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoSetSize(s64 size) override final {
|
2022-03-06 20:08:20 +00:00
|
|
|
R_TRY(this->DrySetSize(size, fs::OpenMode_Read));
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFile());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
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 {
|
2020-03-09 10:10:12 +00:00
|
|
|
switch (op_id) {
|
2021-04-30 03:09:45 +00:00
|
|
|
case OperationId::Invalidate:
|
2020-03-09 10:10:12 +00:00
|
|
|
case OperationId::QueryRange:
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_file->OperateRange(dst, dst_size, op_id, offset, size, src, src_size));
|
2020-03-09 10:10:12 +00:00
|
|
|
default:
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedOperateRangeForReadOnlyFile());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_base_file->GetDomainObjectId();
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ReadOnlyFileSystemTemplate : public fsa::IFileSystem, public impl::Newable {
|
|
|
|
NON_COPYABLE(ReadOnlyFileSystemTemplate);
|
|
|
|
NON_MOVEABLE(ReadOnlyFileSystemTemplate);
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
T m_base_fs;
|
2020-03-09 10:10:12 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
explicit ReadOnlyFileSystemTemplate(T &&fs) : m_base_fs(std::move(fs)) { /* ... */ }
|
2020-03-09 10:10:12 +00:00
|
|
|
virtual ~ReadOnlyFileSystemTemplate() { /* ... */ }
|
|
|
|
private:
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoOpenFile(std::unique_ptr<fsa::IFile> *out_file, const fs::Path &path, OpenMode mode) override final {
|
2020-03-09 10:10:12 +00:00
|
|
|
/* Only allow opening files with mode = read. */
|
2020-04-04 09:37:21 +00:00
|
|
|
R_UNLESS((mode & fs::OpenMode_All) == fs::OpenMode_Read, fs::ResultInvalidOpenMode());
|
2020-03-09 10:10:12 +00:00
|
|
|
|
|
|
|
std::unique_ptr<fsa::IFile> base_file;
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(m_base_fs->OpenFile(std::addressof(base_file), path, mode));
|
2020-03-09 10:10:12 +00:00
|
|
|
|
|
|
|
auto read_only_file = std::make_unique<ReadOnlyFile>(std::move(base_file));
|
2022-03-13 09:51:42 +00:00
|
|
|
R_UNLESS(read_only_file != nullptr, fs::ResultAllocationMemoryFailedInReadOnlyFileSystemA());
|
2020-03-09 10:10:12 +00:00
|
|
|
|
|
|
|
*out_file = std::move(read_only_file);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoOpenDirectory(std::unique_ptr<fsa::IDirectory> *out_dir, const fs::Path &path, OpenDirectoryMode mode) override final {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_fs->OpenDirectory(out_dir, path, mode));
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoGetEntryType(DirectoryEntryType *out, const fs::Path &path) override final {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(m_base_fs->GetEntryType(out, path));
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCommit() override final {
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoCreateFile(const fs::Path &path, s64 size, int flags) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path, size, flags);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoDeleteFile(const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoCreateDirectory(const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoDeleteDirectory(const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoDeleteDirectoryRecursively(const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoRenameFile(const fs::Path &old_path, const fs::Path &new_path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(old_path, new_path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoRenameDirectory(const fs::Path &old_path, const fs::Path &new_path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(old_path, new_path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoCleanDirectoryRecursively(const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(path);
|
2022-03-26 07:14:36 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedWriteForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoGetFreeSpaceSize(s64 *out, const fs::Path &path) override final {
|
2022-03-28 07:54:10 +00:00
|
|
|
R_RETURN(m_base_fs->GetFreeSpaceSize(out, path));
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual Result DoGetTotalSpaceSize(s64 *out, const fs::Path &path) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(out, path);
|
2022-03-28 07:54:10 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedGetTotalSpaceSizeForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCommitProvisionally(s64 counter) override final {
|
2021-10-06 22:20:48 +00:00
|
|
|
AMS_UNUSED(counter);
|
2022-03-28 07:54:10 +00:00
|
|
|
R_THROW(fs::ResultUnsupportedCommitProvisionallyForReadOnlyFileSystem());
|
2020-03-09 10:10:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using ReadOnlyFileSystem = ReadOnlyFileSystemTemplate<std::unique_ptr<::ams::fs::fsa::IFileSystem>>;
|
|
|
|
using ReadOnlyFileSystemShared = ReadOnlyFileSystemTemplate<std::shared_ptr<::ams::fs::fsa::IFileSystem>>;
|
|
|
|
|
|
|
|
}
|