diff --git a/include/stratosphere.hpp b/include/stratosphere.hpp index 0a136366..19ba7185 100644 --- a/include/stratosphere.hpp +++ b/include/stratosphere.hpp @@ -48,3 +48,6 @@ #include "stratosphere/sm.hpp" #include "stratosphere/spl.hpp" #include "stratosphere/updater.hpp" + +/* Include FS last. */ +#include "stratosphere/fs.hpp" diff --git a/include/stratosphere/fs.hpp b/include/stratosphere/fs.hpp new file mode 100644 index 00000000..d1347e3a --- /dev/null +++ b/include/stratosphere/fs.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ + +#pragma once +#include "fs/fs_common.hpp" +#include "fs/fsa/fs_ifile.hpp" +#include "fs/fsa/fs_idirectory.hpp" +#include "fs/fsa/fs_ifilesystem.hpp" +#include "fs/fs_remote_filesystem.hpp" +#include "fs/fs_istorage.hpp" +#include "fs/fs_remote_storage.hpp" +#include "fs/fs_query_range.hpp" diff --git a/include/stratosphere/fs/fs_common.hpp b/include/stratosphere/fs/fs_common.hpp new file mode 100644 index 00000000..cbeaeede --- /dev/null +++ b/include/stratosphere/fs/fs_common.hpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include +#include "../os.hpp" +#include "../ncm.hpp" +#include "../sf.hpp" diff --git a/include/stratosphere/fs/fs_directory.hpp b/include/stratosphere/fs/fs_directory.hpp new file mode 100644 index 00000000..01c8eb2d --- /dev/null +++ b/include/stratosphere/fs/fs_directory.hpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" + +namespace ams::fs { + + using DirectoryEntry = ::FsDirectoryEntry; + +} diff --git a/include/stratosphere/fs/fs_file.hpp b/include/stratosphere/fs/fs_file.hpp new file mode 100644 index 00000000..f7872f51 --- /dev/null +++ b/include/stratosphere/fs/fs_file.hpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" + +namespace ams::fs { + + struct ReadOption { + u32 value; + + static const ReadOption None; + }; + + inline constexpr const ReadOption ReadOption::None = {FsReadOption_None}; + + inline constexpr bool operator==(const ReadOption &lhs, const ReadOption &rhs) { + return lhs.value == rhs.value; + } + + inline constexpr bool operator!=(const ReadOption &lhs, const ReadOption &rhs) { + return !(lhs == rhs); + } + + static_assert(std::is_pod::value && sizeof(ReadOption) == sizeof(u32)); + + struct WriteOption { + u32 value; + + constexpr inline bool HasFlushFlag() const { + return this->value & FsWriteOption_Flush; + } + + static const WriteOption None; + static const WriteOption Flush; + }; + + inline constexpr const WriteOption WriteOption::None = {FsWriteOption_None}; + inline constexpr const WriteOption WriteOption::Flush = {FsWriteOption_Flush}; + + inline constexpr bool operator==(const WriteOption &lhs, const WriteOption &rhs) { + return lhs.value == rhs.value; + } + + inline constexpr bool operator!=(const WriteOption &lhs, const WriteOption &rhs) { + return !(lhs == rhs); + } + + static_assert(std::is_pod::value && sizeof(WriteOption) == sizeof(u32)); + +} diff --git a/include/stratosphere/fs/fs_filesystem.hpp b/include/stratosphere/fs/fs_filesystem.hpp new file mode 100644 index 00000000..86581f3d --- /dev/null +++ b/include/stratosphere/fs/fs_filesystem.hpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" + +namespace ams::fs { + + enum OpenMode { + OpenMode_Read = ::FsOpenMode_Read, + OpenMode_Write = ::FsOpenMode_Write, + OpenMode_Append = ::FsOpenMode_Append, + + OpenMode_ReadWrite = (OpenMode_Read | OpenMode_Write), + OpenMode_All = (OpenMode_ReadWrite | OpenMode_Append), + }; + + enum OpenDirectoryMode { + OpenDirectoryMode_Directory = ::FsDirOpenMode_ReadDirs, + OpenDirectoryMode_File = ::FsDirOpenMode_ReadFiles, + + OpenDirectoryMode_All = (OpenDirectoryMode_Directory | OpenDirectoryMode_File), + + /* TODO: Separate enum, like N? */ + OpenDirectoryMode_NotRequireFileSize = ::FsDirOpenMode_NoFileSize, + }; + + enum DirectoryEntryType { + DirectoryEntryType_Directory = ::FsDirEntryType_Dir, + DirectoryEntryType_File = ::FsDirEntryType_File, + }; + + enum CreateOption { + CreateOption_BigFile = ::FsCreateOption_BigFile, + }; + + using FileTimeStampRaw = ::FsTimeStampRaw; + +} diff --git a/include/stratosphere/fs/fs_istorage.hpp b/include/stratosphere/fs/fs_istorage.hpp new file mode 100644 index 00000000..8a462582 --- /dev/null +++ b/include/stratosphere/fs/fs_istorage.hpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" +#include "fs_file.hpp" +#include "fs_operate_range.hpp" + +namespace ams::fs { + + class IStorage { + public: + virtual ~IStorage() { /* ... */ } + + virtual Result Read(s64 offset, void *buffer, size_t size) = 0; + + virtual Result Write(s64 offset, const void *buffer, size_t size) { + return fs::ResultUnsupportedOperation(); + } + + virtual Result Flush() = 0; + + virtual Result SetSize(s64 size) { + return fs::ResultUnsupportedOperation(); + } + + virtual Result GetSize(s64 *out) = 0; + + virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) { + return fs::ResultUnsupportedOperation(); + } + + virtual Result OperateRange(OperationId op_id, s64 offset, s64 size) { + return this->OperateRange(nullptr, 0, op_id, offset, size, nullptr, 0); + } + public: + static inline bool IsRangeValid(s64 offset, s64 size, s64 total_size) { + return offset >= 0 && + size >= 0 && + size <= total_size && + offset <= (total_size - size); + } + + static inline bool IsRangeValid(s64 offset, size_t size, s64 total_size) { + return IsRangeValid(offset, static_cast(size), total_size); + } + + static inline bool IsOffsetAndSizeValid(s64 offset, s64 size) { + return offset >= 0 && + size >= 0 && + offset <= (offset + size); + } + + static inline bool IsOffsetAndSizeValid(s64 offset, size_t size) { + return IsOffsetAndSizeValid(offset, static_cast(size)); + } + }; + + class ReadOnlyStorageAdapter : public IStorage { + private: + std::shared_ptr shared_storage; + std::unique_ptr unique_storage; + IStorage *storage; + public: + ReadOnlyStorageAdapter(IStorage *s) : unique_storage(s) { + this->storage = this->unique_storage.get(); + } + ReadOnlyStorageAdapter(std::shared_ptr s) : shared_storage(s) { + this->storage = this->shared_storage.get(); + } + ReadOnlyStorageAdapter(std::unique_ptr s) : unique_storage(std::move(s)) { + this->storage = this->unique_storage.get(); + } + + virtual ~ReadOnlyStorageAdapter() { /* ... */ } + public: + virtual Result Read(s64 offset, void *buffer, size_t size) { + return this->storage->Read(offset, buffer, size); + } + + virtual Result Flush() { + return this->storage->Flush(); + } + + virtual Result GetSize(s64 *out) { + return this->storage->GetSize(out); + } + + virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override { + return this->storage->OperateRange(dst, dst_size, op_id, offset, size, src, src_size); + } + }; + +} diff --git a/include/stratosphere/fs/fs_operate_range.hpp b/include/stratosphere/fs/fs_operate_range.hpp new file mode 100644 index 00000000..3896da2e --- /dev/null +++ b/include/stratosphere/fs/fs_operate_range.hpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" + +namespace ams::fs { + + enum class OperationId : u64 { + Clear = ::FsOperationId_Clear, + ClearSignature = ::FsOperationId_ClearSignature, + InvalidateCache = ::FsOperationId_InvalidateCache, + QueryRange = ::FsOperationId_QueryRange, + }; + +} diff --git a/include/stratosphere/fs/fs_query_range.hpp b/include/stratosphere/fs/fs_query_range.hpp new file mode 100644 index 00000000..9bab0e67 --- /dev/null +++ b/include/stratosphere/fs/fs_query_range.hpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" + +namespace ams::fs { + + struct QueryRangeInfo { + u32 aes_ctr_key_type; + u32 speed_emulation_type; + u32 reserved[0x38 / sizeof(u32)]; + + void Clear() { + this->aes_ctr_key_type = 0; + this->speed_emulation_type = 0; + std::memset(this->reserved, 0, sizeof(this->reserved)); + } + + void Merge(const QueryRangeInfo &rhs) { + this->aes_ctr_key_type |= rhs.aes_ctr_key_type; + this->speed_emulation_type |= rhs.speed_emulation_type; + } + }; + + static_assert(std::is_pod::value); + static_assert(sizeof(QueryRangeInfo) == 0x40); + static_assert(sizeof(QueryRangeInfo) == sizeof(::FsRangeInfo)); + + using FileQueryRangeInfo = QueryRangeInfo; + using StorageQueryRangeInfo = QueryRangeInfo; + +} diff --git a/include/stratosphere/fs/fs_remote_filesystem.hpp b/include/stratosphere/fs/fs_remote_filesystem.hpp new file mode 100644 index 00000000..8aaee50f --- /dev/null +++ b/include/stratosphere/fs/fs_remote_filesystem.hpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" +#include "fsa/fs_ifile.hpp" +#include "fsa/fs_idirectory.hpp" +#include "fsa/fs_ifilesystem.hpp" + +namespace ams::fs { + + class RemoteFile : public fsa::IFile { + private: + std::unique_ptr<::FsFile> base_file; + public: + RemoteFile(::FsFile *f) : base_file(f) { /* ... */ } + RemoteFile(std::unique_ptr<::FsFile> f) : base_file(std::move(f)) { /* ... */ } + RemoteFile(::FsFile f) { + this->base_file = std::make_unique<::FsFile>(f); + } + + virtual ~RemoteFile() { fsFileClose(this->base_file.get()); } + public: + virtual Result ReadImpl(size_t *out, s64 offset, void *buffer, size_t size, const ReadOption &option) override final { + return fsFileRead(this->base_file.get(), offset, buffer, size, option.value, out); + } + + virtual Result GetSizeImpl(s64 *out) override final { + return fsFileGetSize(this->base_file.get(), reinterpret_cast(out)); + } + + virtual Result FlushImpl() override final { + return fsFileFlush(this->base_file.get()); + } + + virtual Result WriteImpl(s64 offset, const void *buffer, size_t size, const WriteOption &option) override final { + return fsFileWrite(this->base_file.get(), offset, buffer, size, option.value); + } + + virtual Result SetSizeImpl(s64 size) override final { + return fsFileSetSize(this->base_file.get(), size); + } + + virtual Result OperateRangeImpl(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override final { + /* TODO: How should this be handled? */ + return fs::ResultNotImplemented(); + } + }; + + class RemoteDirectory : public fsa::IDirectory { + private: + std::unique_ptr<::FsDir> base_dir; + public: + RemoteDirectory(::FsDir *d) : base_dir(d) { /* ... */ } + RemoteDirectory(std::unique_ptr<::FsDir> d) : base_dir(std::move(d)) { /* ... */ } + RemoteDirectory(::FsDir d) { + this->base_dir = std::make_unique<::FsDir>(d); + } + + virtual ~RemoteDirectory() { fsDirClose(this->base_dir.get()); } + public: + virtual Result ReadImpl(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) override final { + return fsDirRead(this->base_dir.get(), 0, reinterpret_cast(out_count), max_entries, out_entries); + } + + virtual Result GetEntryCountImpl(s64 *out) override final { + return fsDirGetEntryCount(this->base_dir.get(), reinterpret_cast(out)); + } + }; + + class RemoteFileSystem : public fsa::IFileSystem { + private: + std::unique_ptr<::FsFileSystem> base_fs; + public: + RemoteFileSystem(::FsFileSystem *fs) : base_fs(fs) { /* ... */ } + RemoteFileSystem(std::unique_ptr<::FsFileSystem> fs) : base_fs(std::move(fs)) { /* ... */ } + RemoteFileSystem(::FsFileSystem fs) { + this->base_fs = std::make_unique<::FsFileSystem>(fs); + } + + virtual ~RemoteFileSystem() { fsFsClose(this->base_fs.get()); } + public: + virtual Result CreateFileImpl(const char *path, s64 size, int flags) override final { + return fsFsCreateFile(this->base_fs.get(), path, size, flags); + } + + virtual Result DeleteFileImpl(const char *path) override final { + return fsFsDeleteFile(this->base_fs.get(), path); + } + + virtual Result CreateDirectoryImpl(const char *path) override final { + return fsFsCreateDirectory(this->base_fs.get(), path); + } + + virtual Result DeleteDirectoryImpl(const char *path) override final { + return fsFsDeleteDirectory(this->base_fs.get(), path); + } + + virtual Result DeleteDirectoryRecursivelyImpl(const char *path) override final { + return fsFsDeleteDirectoryRecursively(this->base_fs.get(), path); + } + + virtual Result RenameFileImpl(const char *old_path, const char *new_path) override final { + return fsFsRenameFile(this->base_fs.get(), old_path, new_path); + } + + virtual Result RenameDirectoryImpl(const char *old_path, const char *new_path) override final { + return fsFsRenameDirectory(this->base_fs.get(), old_path, new_path); + } + + virtual Result GetEntryTypeImpl(DirectoryEntryType *out, const char *path) override final { + static_assert(sizeof(::FsDirEntryType) == sizeof(DirectoryEntryType)); + return fsFsGetEntryType(this->base_fs.get(), path, reinterpret_cast<::FsDirEntryType *>(out)); + } + + virtual Result OpenFileImpl(std::unique_ptr *out_file, const char *path, OpenMode mode) override final { + FsFile f; + R_TRY(fsFsOpenFile(this->base_fs.get(), path, mode, &f)); + + *out_file = std::make_unique(f); + return ResultSuccess(); + } + + virtual Result OpenDirectoryImpl(std::unique_ptr *out_dir, const char *path, OpenDirectoryMode mode) override final { + FsDir d; + R_TRY(fsFsOpenDirectory(this->base_fs.get(), path, mode, &d)); + + *out_dir = std::make_unique(d); + return ResultSuccess(); + } + + virtual Result CommitImpl() override final { + return fsFsCommit(this->base_fs.get()); + } + + + virtual Result GetFreeSpaceSizeImpl(s64 *out, const char *path) { + return fsFsGetFreeSpace(this->base_fs.get(), path, reinterpret_cast(out)); + } + + virtual Result GetTotalSpaceSizeImpl(s64 *out, const char *path) { + return fsFsGetTotalSpace(this->base_fs.get(), path, reinterpret_cast(out)); + } + + virtual Result CleanDirectoryRecursivelyImpl(const char *path) { + return fsFsCleanDirectoryRecursively(this->base_fs.get(), path); + } + + virtual Result GetFileTimeStampRawImpl(FileTimeStampRaw *out, const char *path) { + static_assert(sizeof(FileTimeStampRaw) == sizeof(::FsTimeStampRaw)); + return fsFsGetFileTimeStampRaw(this->base_fs.get(), path, reinterpret_cast<::FsTimeStampRaw *>(out)); + } + + virtual Result QueryEntryImpl(char *dst, size_t dst_size, const char *src, size_t src_size, fsa::QueryType query, const char *path) { + return fsFsQueryEntry(this->base_fs.get(), dst, dst_size, src, src_size, path, static_cast(query)); + } + }; + +} diff --git a/include/stratosphere/fs/fs_remote_storage.hpp b/include/stratosphere/fs/fs_remote_storage.hpp new file mode 100644 index 00000000..07b08c94 --- /dev/null +++ b/include/stratosphere/fs/fs_remote_storage.hpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "fs_common.hpp" +#include "fs_istorage.hpp" + +namespace ams::fs { + + class RemoteStorage : public IStorage { + private: + std::unique_ptr<::FsStorage> base_storage; + public: + RemoteStorage(::FsStorage *s) : base_storage(s) { /* ... */ } + RemoteStorage(std::unique_ptr<::FsStorage> s) : base_storage(std::move(s)) { /* ... */ } + RemoteStorage(::FsStorage s) { + this->base_storage = std::make_unique<::FsStorage>(s); + } + + virtual ~RemoteStorage() { fsStorageClose(this->base_storage.get()); } + public: + virtual Result Read(s64 offset, void *buffer, size_t size) override { + return fsStorageRead(this->base_storage.get(), offset, buffer, size); + }; + + virtual Result Write(s64 offset, const void *buffer, size_t size) override { + return fsStorageWrite(this->base_storage.get(), offset, buffer, size); + }; + + virtual Result Flush() override { + return fsStorageFlush(this->base_storage.get()); + }; + + virtual Result GetSize(s64 *out_size) override { + return fsStorageGetSize(this->base_storage.get(), reinterpret_cast(out_size)); + }; + + virtual Result SetSize(s64 size) override { + return fsStorageSetSize(this->base_storage.get(), size); + }; + + virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override { + /* TODO: How to deal with this? */ + return fs::ResultUnsupportedOperation(); + }; + }; + +} diff --git a/include/stratosphere/fs/fsa/fs_idirectory.hpp b/include/stratosphere/fs/fsa/fs_idirectory.hpp new file mode 100644 index 00000000..ab3e06b4 --- /dev/null +++ b/include/stratosphere/fs/fsa/fs_idirectory.hpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ + +#pragma once +#include "../fs_common.hpp" +#include "../fs_directory.hpp" + +namespace ams::fs::fsa { + + class IDirectory { + public: + virtual ~IDirectory() { /* ... */ } + + Result Read(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) { + R_UNLESS(out_count != nullptr, fs::ResultNullptrArgument()); + if (max_entries == 0) { + *out_count = 0; + return ResultSuccess(); + } + R_UNLESS(out_entries != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(max_entries > 0, fs::ResultInvalidArgument()); + return this->ReadImpl(out_count, out_entries, max_entries); + } + + Result GetEntryCount(s64 *out) { + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetEntryCountImpl(out); + } + protected: + /* ...? */ + private: + virtual Result ReadImpl(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) = 0; + virtual Result GetEntryCountImpl(s64 *out) = 0; + }; + +} diff --git a/include/stratosphere/fs/fsa/fs_ifile.hpp b/include/stratosphere/fs/fsa/fs_ifile.hpp new file mode 100644 index 00000000..e81cae59 --- /dev/null +++ b/include/stratosphere/fs/fsa/fs_ifile.hpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include "../fs_common.hpp" +#include "../fs_file.hpp" +#include "../fs_operate_range.hpp" + +namespace ams::fs::fsa { + + class IFile { + public: + virtual ~IFile() { /* ... */ } + + Result Read(size_t *out, s64 offset, void *buffer, size_t size, const ReadOption &option) { + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + if (size == 0) { + *out = 0; + return ResultSuccess(); + } + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(offset >= 0, fs::ResultOutOfRange()); + const s64 signed_size = static_cast(size); + R_UNLESS(signed_size >= 0, fs::ResultOutOfRange()); + R_UNLESS((std::numeric_limits::max() - offset) >= signed_size, fs::ResultOutOfRange()); + return this->ReadImpl(out, offset, buffer, size, option); + } + + Result Read(size_t *out, s64 offset, void *buffer, size_t size) { + return this->Read(out, offset, buffer, size, ReadOption::None); + } + + Result GetSize(s64 *out) { + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetSizeImpl(out); + } + + Result Flush() { + return this->FlushImpl(); + } + + Result Write(s64 offset, const void *buffer, size_t size, const WriteOption &option) { + if (size == 0) { + if (option.HasFlushFlag()) { + R_TRY(this->Flush()); + } + return ResultSuccess(); + } + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(offset >= 0, fs::ResultOutOfRange()); + const s64 signed_size = static_cast(size); + R_UNLESS(signed_size >= 0, fs::ResultOutOfRange()); + R_UNLESS((std::numeric_limits::max() - offset) >= signed_size, fs::ResultOutOfRange()); + return this->WriteImpl(offset, buffer, size, option); + } + + Result SetSize(s64 size) { + R_UNLESS(size >= 0, fs::ResultOutOfRange()); + return this->SetSizeImpl(size); + } + + Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) { + return this->OperateRangeImpl(dst, dst_size, op_id, offset, size, src, src_size); + } + + Result OperateRange(OperationId op_id, s64 offset, s64 size) { + return this->OperateRangeImpl(nullptr, 0, op_id, offset, size, nullptr, 0); + } + protected: + /* ...? */ + private: + virtual Result ReadImpl(size_t *out, s64 offset, void *buffer, size_t size, const ReadOption &option) = 0; + virtual Result GetSizeImpl(s64 *out) = 0; + virtual Result FlushImpl() = 0; + virtual Result WriteImpl(s64 offset, const void *buffer, size_t size, const WriteOption &option) = 0; + virtual Result SetSizeImpl(s64 size) = 0; + virtual Result OperateRangeImpl(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) = 0; + }; + +} diff --git a/include/stratosphere/fs/fsa/fs_ifilesystem.hpp b/include/stratosphere/fs/fsa/fs_ifilesystem.hpp new file mode 100644 index 00000000..98a7a9e4 --- /dev/null +++ b/include/stratosphere/fs/fsa/fs_ifilesystem.hpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2018-2019 Atmosphère-NX + * + * 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 . + */ + +#pragma once +#include "../fs_common.hpp" +#include "../fs_filesystem.hpp" + +namespace ams::fs::fsa { + + class IFile; + class IDirectory; + + enum class QueryType { + SetArchiveBit = FsFileSystemQueryType_SetArchiveBit + }; + + class IFileSystem { + public: + virtual ~IFileSystem() { /* ... */ } + + Result CreateFile(const char *path, s64 size, int option) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(size >= 0, fs::ResultOutOfRange()); + return this->CreateFileImpl(path, size, option); + } + + Result CreateFile(const char *path, s64 size) { + return this->CreateFile(path, size, 0); + } + + Result DeleteFile(const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->DeleteFileImpl(path); + } + + Result CreateDirectory(const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->CreateDirectoryImpl(path); + } + + Result DeleteDirectory(const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->DeleteDirectoryImpl(path); + } + + Result DeleteDirectoryRecursively(const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->DeleteDirectoryRecursivelyImpl(path); + } + + Result RenameFile(const char *old_path, const char *new_path) { + R_UNLESS(old_path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(new_path != nullptr, fs::ResultInvalidPath()); + return this->RenameFileImpl(old_path, new_path); + } + + Result RenameDirectory(const char *old_path, const char *new_path) { + R_UNLESS(old_path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(new_path != nullptr, fs::ResultInvalidPath()); + return this->RenameDirectoryImpl(old_path, new_path); + } + + Result GetEntryType(DirectoryEntryType *out, const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetEntryTypeImpl(out, path); + } + + Result OpenFile(std::unique_ptr *out_file, const char *path, OpenMode mode) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out_file != nullptr, fs::ResultNullptrArgument()); + R_UNLESS((mode & OpenMode_ReadWrite) != 0, fs::ResultInvalidArgument()); + R_UNLESS((mode & ~OpenMode_All) == 0, fs::ResultInvalidArgument()); + return this->OpenFileImpl(out_file, path, mode); + } + + Result OpenDirectory(std::unique_ptr *out_dir, const char *path, OpenDirectoryMode mode) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out_dir != nullptr, fs::ResultNullptrArgument()); + R_UNLESS((mode & OpenDirectoryMode_All) != 0, fs::ResultInvalidArgument()); + R_UNLESS((mode & ~OpenDirectoryMode_All) == 0, fs::ResultInvalidArgument()); + return this->OpenDirectoryImpl(out_dir, path, mode); + } + + Result Commit() { + return this->CommitImpl(); + } + + Result GetFreeSpaceSize(s64 *out, const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetFreeSpaceSizeImpl(out, path); + } + + Result GetTotalSpaceSize(s64 *out, const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetTotalSpaceSizeImpl(out, path); + } + + Result CleanDirectoryRecursively(const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->CleanDirectoryRecursivelyImpl(path); + } + + Result GetFileTimeStampRaw(FileTimeStampRaw *out, const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + R_UNLESS(out != nullptr, fs::ResultNullptrArgument()); + return this->GetFileTimeStampRawImpl(out, path); + } + + Result QueryEntry(char *dst, size_t dst_size, const char *src, size_t src_size, QueryType query, const char *path) { + R_UNLESS(path != nullptr, fs::ResultInvalidPath()); + return this->QueryEntryImpl(dst, dst_size, src, src_size, query, path); + } + + /* These aren't accessible as commands. */ + + Result CommitProvisionally(s64 counter) { + return this->CommitProvisionallyImpl(counter); + } + + Result Rollback() { + return this->RollbackImpl(); + } + + Result Flush() { + return this->FlushImpl(); + } + + protected: + /* ...? */ + private: + virtual Result CreateFileImpl(const char *path, s64 size, int flags) = 0; + virtual Result DeleteFileImpl(const char *path) = 0; + virtual Result CreateDirectoryImpl(const char *path) = 0; + virtual Result DeleteDirectoryImpl(const char *path) = 0; + virtual Result DeleteDirectoryRecursivelyImpl(const char *path) = 0; + virtual Result RenameFileImpl(const char *old_path, const char *new_path) = 0; + virtual Result RenameDirectoryImpl(const char *old_path, const char *new_path) = 0; + virtual Result GetEntryTypeImpl(DirectoryEntryType *out, const char *path) = 0; + virtual Result OpenFileImpl(std::unique_ptr *out_file, const char *path, OpenMode mode) = 0; + virtual Result OpenDirectoryImpl(std::unique_ptr *out_dir, const char *path, OpenDirectoryMode mode) = 0; + virtual Result CommitImpl() = 0; + + virtual Result GetFreeSpaceSizeImpl(s64 *out, const char *path) { + return fs::ResultNotImplemented(); + } + + virtual Result GetTotalSpaceSizeImpl(s64 *out, const char *path) { + return fs::ResultNotImplemented(); + } + + virtual Result CleanDirectoryRecursivelyImpl(const char *path) = 0; + + virtual Result GetFileTimeStampRawImpl(FileTimeStampRaw *out, const char *path) { + return fs::ResultNotImplemented(); + } + + virtual Result QueryEntryImpl(char *dst, size_t dst_size, const char *src, size_t src_size, QueryType query, const char *path) { + return fs::ResultNotImplemented(); + } + + /* These aren't accessible as commands. */ + virtual Result CommitProvisionallyImpl(s64 counter) { + return fs::ResultNotImplemented(); + } + + virtual Result RollbackImpl() { + return fs::ResultNotImplemented(); + } + + virtual Result FlushImpl() { + return fs::ResultNotImplemented(); + } + }; + +} diff --git a/include/stratosphere/sf/cmif/sf_cmif_domain_manager.hpp b/include/stratosphere/sf/cmif/sf_cmif_domain_manager.hpp index 27d70b84..0627aed2 100644 --- a/include/stratosphere/sf/cmif/sf_cmif_domain_manager.hpp +++ b/include/stratosphere/sf/cmif/sf_cmif_domain_manager.hpp @@ -51,6 +51,8 @@ namespace ams::sf::cmif { explicit Domain(ServerDomainManager *m) : manager(m) { /* ... */ } ~Domain(); + void DestroySelf(); + virtual ServerDomainBase *GetServerDomain() override final { return static_cast(this); } @@ -116,10 +118,9 @@ namespace ams::sf::cmif { } return new (storage) Domain(this); } - - inline void FreeDomainServiceObject(DomainServiceObject *object) { - static_cast(object)->~Domain(); - this->FreeDomain(object); + public: + static void DestroyDomainServiceObject(DomainServiceObject *obj) { + static_cast(obj)->DestroySelf(); } }; diff --git a/include/stratosphere/sf/cmif/sf_cmif_domain_service_object.hpp b/include/stratosphere/sf/cmif/sf_cmif_domain_service_object.hpp index 9dbea100..77758190 100644 --- a/include/stratosphere/sf/cmif/sf_cmif_domain_service_object.hpp +++ b/include/stratosphere/sf/cmif/sf_cmif_domain_service_object.hpp @@ -103,6 +103,8 @@ namespace ams::sf::cmif { class MitmDomainServiceObject : public DomainServiceObject{}; + static_assert(sizeof(DomainServiceObject) == sizeof(MitmDomainServiceObject)); + template<> struct ServiceDispatchTraits { static_assert(std::is_base_of::value, "DomainServiceObject must derive from sf::IServiceObject"); diff --git a/include/stratosphere/sf/hipc/sf_hipc_server_domain_session_manager.hpp b/include/stratosphere/sf/hipc/sf_hipc_server_domain_session_manager.hpp index 6d5c4527..38cdd574 100644 --- a/include/stratosphere/sf/hipc/sf_hipc_server_domain_session_manager.hpp +++ b/include/stratosphere/sf/hipc/sf_hipc_server_domain_session_manager.hpp @@ -32,10 +32,6 @@ namespace ams::sf::hipc { inline cmif::DomainServiceObject *AllocateDomainServiceObject() { return cmif::ServerDomainManager::AllocateDomainServiceObject(); } - - inline void FreeDomainServiceObject(cmif::DomainServiceObject *object) { - cmif::ServerDomainManager::FreeDomainServiceObject(object); - } }; } diff --git a/source/ams/ams_exosphere_api.cpp b/source/ams/ams_exosphere_api.cpp index 9d97f2c6..f1233ec3 100644 --- a/source/ams/ams_exosphere_api.cpp +++ b/source/ams/ams_exosphere_api.cpp @@ -58,7 +58,7 @@ namespace ams::exosphere { namespace { inline Result GetRcmBugPatched(bool *out) { - u64 tmp; + u64 tmp = 0; R_TRY(spl::smc::ConvertResult(spl::smc::GetConfig(&tmp, 1, SplConfigItem_ExosphereHasRcmBugPatch))); *out = (tmp != 0); return ResultSuccess(); diff --git a/source/sf/cmif/sf_cmif_domain_manager.cpp b/source/sf/cmif/sf_cmif_domain_manager.cpp index 6be09d7c..67b9ccb8 100644 --- a/source/sf/cmif/sf_cmif_domain_manager.cpp +++ b/source/sf/cmif/sf_cmif_domain_manager.cpp @@ -31,6 +31,12 @@ namespace ams::sf::cmif { } } + void ServerDomainManager::Domain::DestroySelf() { + ServerDomainManager *manager = this->manager; + this->~Domain(); + manager->FreeDomain(this); + } + Result ServerDomainManager::Domain::ReserveIds(DomainObjectId *out_ids, size_t count) { for (size_t i = 0; i < count; i++) { Entry *entry = this->manager->entry_manager.AllocateEntry(); diff --git a/source/sf/hipc/sf_hipc_server_domain_session_manager.cpp b/source/sf/hipc/sf_hipc_server_domain_session_manager.cpp index 85c4aa41..fcf82737 100644 --- a/source/sf/hipc/sf_hipc_server_domain_session_manager.cpp +++ b/source/sf/hipc/sf_hipc_server_domain_session_manager.cpp @@ -63,7 +63,7 @@ namespace ams::sf::hipc { /* Allocate a domain. */ auto domain = this->manager->AllocateDomainServiceObject(); R_UNLESS(domain, sf::hipc::ResultOutOfDomains()); - auto domain_guard = SCOPE_GUARD { this->manager->FreeDomainServiceObject(domain); }; + auto domain_guard = SCOPE_GUARD { cmif::ServerDomainManager::DestroyDomainServiceObject(static_cast(domain)); }; cmif::DomainObjectId object_id = cmif::InvalidDomainObjectId; @@ -80,8 +80,8 @@ namespace ams::sf::hipc { /* Create new object. */ cmif::MitmDomainServiceObject *domain_ptr = static_cast(domain); - new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr(domain_ptr, [&](cmif::MitmDomainServiceObject *obj) { - this->manager->FreeDomainServiceObject(domain); + new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr(domain_ptr, [](cmif::MitmDomainServiceObject *obj) { + cmif::ServerDomainManager::DestroyDomainServiceObject(static_cast(obj)); }))); } else { /* We're not a mitm session. Reserve a new object in the domain. */ @@ -89,8 +89,8 @@ namespace ams::sf::hipc { /* Create new object. */ cmif::DomainServiceObject *domain_ptr = static_cast(domain); - new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr(domain_ptr, [&](cmif::DomainServiceObject *obj) { - this->manager->FreeDomainServiceObject(domain); + new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr(domain_ptr, [](cmif::DomainServiceObject *obj) { + cmif::ServerDomainManager::DestroyDomainServiceObject(static_cast(obj)); }))); }