2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 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_tool.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:
|
2020-03-08 08:06:23 +00:00
|
|
|
::FsFile base_file;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
RemoteFile(const ::FsFile &f) : base_file(f) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual ~RemoteFile() { fsFileClose(std::addressof(this->base_file)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
virtual Result ReadImpl(size_t *out, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFileRead(std::addressof(this->base_file), offset, buffer, size, option.value, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetSizeImpl(s64 *out) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFileGetSize(std::addressof(this->base_file), out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result FlushImpl() override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFileFlush(std::addressof(this->base_file));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result WriteImpl(s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFileWrite(std::addressof(this->base_file), offset, buffer, size, option.value);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result SetSizeImpl(s64 size) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFileSetSize(std::addressof(this->base_file), size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result OperateRangeImpl(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-08 08:06:23 +00:00
|
|
|
R_UNLESS(op_id == OperationId::QueryRange, fs::ResultUnsupportedOperationInFileServiceObjectAdapterA());
|
|
|
|
R_UNLESS(dst_size == sizeof(FileQueryRangeInfo), fs::ResultInvalidSize());
|
|
|
|
|
|
|
|
return fsFileOperateRange(std::addressof(this->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 {
|
2020-03-08 08:06:23 +00:00
|
|
|
return sf::cmif::DomainObjectId{serviceGetObjectId(const_cast<::Service *>(std::addressof(this->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:
|
2020-03-08 08:06:23 +00:00
|
|
|
::FsDir base_dir;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
RemoteDirectory(const ::FsDir &d) : base_dir(d) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
virtual ~RemoteDirectory() { fsDirClose(std::addressof(this->base_dir)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
virtual Result ReadImpl(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsDirRead(std::addressof(this->base_dir), out_count, max_entries, out_entries);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetEntryCountImpl(s64 *out) override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsDirGetEntryCount(std::addressof(this->base_dir), out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
|
2020-03-08 08:06:23 +00:00
|
|
|
return sf::cmif::DomainObjectId{serviceGetObjectId(const_cast<::Service *>(std::addressof(this->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:
|
2020-03-08 08:06:23 +00:00
|
|
|
::FsFileSystem base_fs;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
RemoteFileSystem(const ::FsFileSystem &fs) : base_fs(fs) { /* ... */ }
|
|
|
|
|
|
|
|
virtual ~RemoteFileSystem() { fsFsClose(std::addressof(this->base_fs)); }
|
|
|
|
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. */
|
|
|
|
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. */
|
|
|
|
const auto mount_name_len = PathTool::IsWindowsAbsolutePath(path) ? 2 : 0;
|
|
|
|
const auto rel_path = out_path->str + mount_name_len;
|
|
|
|
const auto max_len = fs::EntryNameLengthMax - mount_name_len;
|
|
|
|
return VerifyPath(rel_path, max_len, max_len);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
virtual Result CreateFileImpl(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));
|
|
|
|
return fsFsCreateFile(std::addressof(this->base_fs), sf_path.str, size, flags);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result DeleteFileImpl(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));
|
|
|
|
return fsFsDeleteFile(std::addressof(this->base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result CreateDirectoryImpl(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));
|
|
|
|
return fsFsCreateDirectory(std::addressof(this->base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result DeleteDirectoryImpl(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));
|
|
|
|
return fsFsDeleteDirectory(std::addressof(this->base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result DeleteDirectoryRecursivelyImpl(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));
|
|
|
|
return fsFsDeleteDirectoryRecursively(std::addressof(this->base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result RenameFileImpl(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));
|
|
|
|
return fsFsRenameFile(std::addressof(this->base_fs), old_sf_path.str, new_sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result RenameDirectoryImpl(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));
|
|
|
|
return fsFsRenameDirectory(std::addressof(this->base_fs), old_sf_path.str, new_sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetEntryTypeImpl(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));
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFsGetEntryType(std::addressof(this->base_fs), sf_path.str, reinterpret_cast<::FsDirEntryType *>(out));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result OpenFileImpl(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;
|
2020-03-08 08:06:23 +00:00
|
|
|
R_TRY(fsFsOpenFile(std::addressof(this->base_fs), sf_path.str, mode, &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();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result OpenDirectoryImpl(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;
|
2020-03-08 08:06:23 +00:00
|
|
|
R_TRY(fsFsOpenDirectory(std::addressof(this->base_fs), sf_path.str, mode, &d));
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result CommitImpl() override final {
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFsCommit(std::addressof(this->base_fs));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual Result GetFreeSpaceSizeImpl(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));
|
|
|
|
return fsFsGetFreeSpace(std::addressof(this->base_fs), sf_path.str, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetTotalSpaceSizeImpl(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));
|
|
|
|
return fsFsGetTotalSpace(std::addressof(this->base_fs), sf_path.str, out);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result CleanDirectoryRecursivelyImpl(const char *path) {
|
2020-03-08 08:06:23 +00:00
|
|
|
fssrv::sf::Path sf_path;
|
|
|
|
R_TRY(GetPathForServiceObject(std::addressof(sf_path), path));
|
|
|
|
return fsFsCleanDirectoryRecursively(std::addressof(this->base_fs), sf_path.str);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result GetFileTimeStampRawImpl(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));
|
2020-03-08 08:06:23 +00:00
|
|
|
return fsFsGetFileTimeStampRaw(std::addressof(this->base_fs), sf_path.str, reinterpret_cast<::FsTimeStampRaw *>(out));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Result QueryEntryImpl(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));
|
|
|
|
return fsFsQueryEntry(std::addressof(this->base_fs), dst, dst_size, src, src_size, sf_path.str, static_cast<FsFileSystemQueryId>(query));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|