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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-03-08 08:06:23 +00:00
|
|
|
#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>
|
|
|
|
#include <stratosphere/fs/fs_query_range.hpp>
|
|
|
|
#include <stratosphere/fs/fs_path_utils.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
class RemoteFile : public fsa::IFile, public impl::Newable {
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
::FsFile m_base_file;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
RemoteFile(const ::FsFile &f) : m_base_file(f) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
virtual ~RemoteFile() { fsFileClose(std::addressof(m_base_file)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
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 {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileRead(std::addressof(m_base_file), offset, buffer, size, option._value, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetSize(s64 *out) override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileGetSize(std::addressof(m_base_file), out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoFlush() override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileFlush(std::addressof(m_base_file));
|
2019-12-09 11:57:37 +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 {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileWrite(std::addressof(m_base_file), offset, buffer, size, option._value);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoSetSize(s64 size) override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileSetSize(std::addressof(m_base_file), size);
|
2019-12-09 11:57:37 +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 {
|
2021-10-07 00:58:42 +00:00
|
|
|
AMS_UNUSED(src, src_size);
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
R_UNLESS(op_id == OperationId::QueryRange, fs::ResultUnsupportedOperationInFileServiceObjectAdapterA());
|
|
|
|
R_UNLESS(dst_size == sizeof(FileQueryRangeInfo), fs::ResultInvalidSize());
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFileOperateRange(std::addressof(m_base_file), static_cast<::FsOperationId>(op_id), offset, size, reinterpret_cast<::FsRangeInfo *>(dst));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return sf::cmif::DomainObjectId{serviceGetObjectId(const_cast<::Service *>(std::addressof(m_base_file.s)))};
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
class RemoteDirectory : public fsa::IDirectory, public impl::Newable {
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
::FsDir m_base_dir;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
RemoteDirectory(const ::FsDir &d) : m_base_dir(d) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
virtual ~RemoteDirectory() { fsDirClose(std::addressof(m_base_dir)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoRead(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsDirRead(std::addressof(m_base_dir), out_count, max_entries, out_entries);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetEntryCount(s64 *out) override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsDirGetEntryCount(std::addressof(m_base_dir), out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
|
2021-10-10 07:14:06 +00:00
|
|
|
return sf::cmif::DomainObjectId{serviceGetObjectId(const_cast<::Service *>(std::addressof(m_base_dir.s)))};
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
class RemoteFileSystem : public fsa::IFileSystem, public impl::Newable {
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
::FsFileSystem m_base_fs;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
RemoteFileSystem(const ::FsFileSystem &fs) : m_base_fs(fs) { /* ... */ }
|
2020-03-08 08:06:23 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
virtual ~RemoteFileSystem() { fsFsClose(std::addressof(m_base_fs)); }
|
2020-03-08 08:06:23 +00:00
|
|
|
private:
|
|
|
|
Result GetPathForServiceObject(fssrv::sf::Path *out_path, const char *path) {
|
|
|
|
/* Copy and null terminate. */
|
|
|
|
std::strncpy(out_path->str, path, sizeof(out_path->str) - 1);
|
|
|
|
out_path->str[sizeof(out_path->str) - 1] = '\x00';
|
|
|
|
|
|
|
|
/* Replace directory separators. */
|
2020-12-07 03:56:45 +00:00
|
|
|
fs::Replace(out_path->str, sizeof(out_path->str) - 1, StringTraits::AlternateDirectorySeparator, StringTraits::DirectorySeparator);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
/* Get lengths. */
|
2020-12-07 03:56:45 +00:00
|
|
|
const auto skip_len = fs::GetWindowsPathSkipLength(path);
|
|
|
|
const auto rel_path = out_path->str + skip_len;
|
|
|
|
const auto max_len = fs::EntryNameLengthMax - skip_len;
|
2020-03-08 08:06:23 +00:00
|
|
|
return VerifyPath(rel_path, max_len, max_len);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCreateFile(const char *path, s64 size, int flags) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsCreateFile(std::addressof(m_base_fs), sf_path.str, size, flags);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoDeleteFile(const char *path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsDeleteFile(std::addressof(m_base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCreateDirectory(const char *path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsCreateDirectory(std::addressof(m_base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoDeleteDirectory(const char *path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsDeleteDirectory(std::addressof(m_base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoDeleteDirectoryRecursively(const char *path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsDeleteDirectoryRecursively(std::addressof(m_base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoRenameFile(const char *old_path, const char *new_path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path old_sf_path;
|
|
|
|
fssrv::sf::Path new_sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(old_sf_path), old_path));
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(new_sf_path), new_path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsRenameFile(std::addressof(m_base_fs), old_sf_path.str, new_sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoRenameDirectory(const char *old_path, const char *new_path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path old_sf_path;
|
|
|
|
fssrv::sf::Path new_sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(old_sf_path), old_path));
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(new_sf_path), new_path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsRenameDirectory(std::addressof(m_base_fs), old_sf_path.str, new_sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetEntryType(DirectoryEntryType *out, const char *path) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
static_assert(sizeof(::FsDirEntryType) == sizeof(DirectoryEntryType));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsGetEntryType(std::addressof(m_base_fs), sf_path.str, reinterpret_cast<::FsDirEntryType *>(out));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoOpenFile(std::unique_ptr<fsa::IFile> *out_file, const char *path, OpenMode mode) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
FsFile f;
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(fsFsOpenFile(std::addressof(m_base_fs), sf_path.str, mode, std::addressof(f)));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
auto file = std::make_unique<RemoteFile>(f);
|
|
|
|
R_UNLESS(file != nullptr, fs::ResultAllocationFailureInNew());
|
|
|
|
|
|
|
|
*out_file = std::move(file);
|
2019-12-09 11:57:37 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoOpenDirectory(std::unique_ptr<fsa::IDirectory> *out_dir, const char *path, OpenDirectoryMode mode) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
FsDir d;
|
2021-10-10 07:14:06 +00:00
|
|
|
R_TRY(fsFsOpenDirectory(std::addressof(m_base_fs), sf_path.str, mode, std::addressof(d)));
|
2020-03-08 08:06:23 +00:00
|
|
|
|
|
|
|
auto dir = std::make_unique<RemoteDirectory>(d);
|
|
|
|
R_UNLESS(dir != nullptr, fs::ResultAllocationFailureInNew());
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
*out_dir = std::move(dir);
|
2019-12-09 11:57:37 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCommit() override final {
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsCommit(std::addressof(m_base_fs));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetFreeSpaceSize(s64 *out, const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsGetFreeSpace(std::addressof(m_base_fs), sf_path.str, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetTotalSpaceSize(s64 *out, const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsGetTotalSpace(std::addressof(m_base_fs), sf_path.str, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoCleanDirectoryRecursively(const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsCleanDirectoryRecursively(std::addressof(m_base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoGetFileTimeStampRaw(FileTimeStampRaw *out, const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2019-12-09 11:57:37 +00:00
|
|
|
static_assert(sizeof(FileTimeStampRaw) == sizeof(::FsTimeStampRaw));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsGetFileTimeStampRaw(std::addressof(m_base_fs), sf_path.str, reinterpret_cast<::FsTimeStampRaw *>(out));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 11:05:06 +00:00
|
|
|
virtual Result DoQueryEntry(char *dst, size_t dst_size, const char *src, size_t src_size, fsa::QueryId query, const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
2021-10-10 07:14:06 +00:00
|
|
|
return fsFsQueryEntry(std::addressof(m_base_fs), dst, dst_size, src, src_size, sf_path.str, static_cast<FsFileSystemQueryId>(query));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|