1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-23 12:22:08 +00:00
Atmosphere/libraries/libstratosphere/source/fs/fs_bis.cpp
Michael Scire 7ca83c9d3b libstrat: enable -Wextra, -Werror
This caught an embarrassingly large number of bugs.
2021-10-06 15:20:48 -07:00

130 lines
4.9 KiB
C++

/*
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "fsa/fs_mount_utils.hpp"
namespace ams::fs {
namespace {
class BisCommonMountNameGenerator : public fsa::ICommonMountNameGenerator, public impl::Newable {
private:
const BisPartitionId id;
public:
explicit BisCommonMountNameGenerator(BisPartitionId i) : id(i) { /* ... */ }
virtual Result GenerateCommonMountName(char *dst, size_t dst_size) override {
/* Determine how much space we need. */
const char *bis_mount_name = GetBisMountName(this->id);
const size_t needed_size = util::Strnlen(bis_mount_name, MountNameLengthMax) + 2;
AMS_ABORT_UNLESS(dst_size >= needed_size);
/* Generate the name. */
const auto size = util::SNPrintf(dst, dst_size, "%s:", bis_mount_name);
AMS_ASSERT(static_cast<size_t>(size) == needed_size - 1);
AMS_UNUSED(size);
return ResultSuccess();
}
};
}
namespace impl {
Result MountBisImpl(const char *name, BisPartitionId id, const char *root_path) {
/* Validate the mount name. */
R_TRY(impl::CheckMountNameAllowingReserved(name));
/* Open the partition. This uses libnx bindings. */
/* NOTE: Nintendo ignores the root_path here. */
AMS_UNUSED(root_path);
FsFileSystem fs;
R_TRY(fsOpenBisFileSystem(std::addressof(fs), static_cast<::FsBisPartitionId>(id), ""));
/* Allocate a new mountname generator. */
auto generator = std::make_unique<BisCommonMountNameGenerator>(id);
R_UNLESS(generator != nullptr, fs::ResultAllocationFailureInBisA());
/* Allocate a new filesystem wrapper. */
auto fsa = std::make_unique<RemoteFileSystem>(fs);
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInBisB());
/* Register. */
return fsa::Register(name, std::move(fsa), std::move(generator));
}
Result SetBisRootForHostImpl(BisPartitionId id, const char *root_path) {
/* Ensure the path isn't too long. */
size_t len = strnlen(root_path, fs::EntryNameLengthMax + 1);
R_UNLESS(len <= fs::EntryNameLengthMax, fs::ResultTooLongPath());
fssrv::sf::Path sf_path;
if (len > 0) {
const bool ending_sep = PathNormalizer::IsSeparator(root_path[len - 1]);
FspPathPrintf(std::addressof(sf_path), "%s%s", root_path, ending_sep ? "" : "/");
} else {
sf_path.str[0] = '\x00';
}
/* TODO: Libnx binding for fsSetBisRootForHost */
AMS_UNUSED(id);
AMS_ABORT();
}
}
const char *GetBisMountName(BisPartitionId id) {
switch (id) {
case BisPartitionId::CalibrationFile: return impl::BisCalibrationFilePartitionMountName;
case BisPartitionId::SafeMode: return impl::BisSafeModePartitionMountName;
case BisPartitionId::User: return impl::BisUserPartitionMountName;
case BisPartitionId::System: return impl::BisSystemPartitionMountName;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
Result MountBis(BisPartitionId id, const char *root_path) {
return impl::MountBisImpl(GetBisMountName(id), id, root_path);
}
Result MountBis(const char *name, BisPartitionId id) {
return impl::MountBisImpl(name, id, nullptr);
}
void SetBisRootForHost(BisPartitionId id, const char *root_path) {
R_ABORT_UNLESS(impl::SetBisRootForHostImpl(id, root_path));
}
Result OpenBisPartition(std::unique_ptr<fs::IStorage> *out, BisPartitionId id) {
/* Open the partition. This uses libnx bindings. */
FsStorage s;
R_TRY(fsOpenBisStorage(std::addressof(s), static_cast<::FsBisPartitionId>(id)));
/* Allocate a new storage wrapper. */
auto storage = std::make_unique<RemoteStorage>(s);
R_UNLESS(storage != nullptr, fs::ResultAllocationFailureInBisC());
*out = std::move(storage);
return ResultSuccess();
}
Result InvalidateBisCache() {
/* TODO: Libnx binding for this command. */
AMS_ABORT();
}
}