mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-02 23:59:49 -04:00
loader: refactor to use fs bindings
This commit is contained in:
parent
4c5e980e07
commit
237b513408
30 changed files with 821 additions and 650 deletions
40
libraries/libstratosphere/source/fs/fs_application.cpp
Normal file
40
libraries/libstratosphere/source/fs/fs_application.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 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 {
|
||||
|
||||
Result MountApplicationPackage(const char *name, const char *common_path) {
|
||||
/* Validate the mount name. */
|
||||
R_TRY(impl::CheckMountName(name));
|
||||
|
||||
/* Validate the path. */
|
||||
R_UNLESS(common_path != nullptr, fs::ResultInvalidPath());
|
||||
|
||||
/* Open a filesystem using libnx bindings. */
|
||||
FsFileSystem fs;
|
||||
R_TRY(fsOpenFileSystemWithId(std::addressof(fs), ncm::InvalidProgramId.value, static_cast<::FsFileSystemType>(impl::FileSystemProxyType_Package), common_path));
|
||||
|
||||
/* Allocate a new filesystem wrapper. */
|
||||
auto fsa = std::make_unique<RemoteFileSystem>(fs);
|
||||
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInApplicationA());
|
||||
|
||||
/* Register. */
|
||||
return fsa::Register(name, std::move(fsa));
|
||||
}
|
||||
|
||||
}
|
|
@ -18,6 +18,261 @@
|
|||
|
||||
namespace ams::fs {
|
||||
|
||||
namespace {
|
||||
|
||||
Result OpenCodeFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out, const char *path, ncm::ProgramId program_id) {
|
||||
/* Print a path suitable for the remote service. */
|
||||
fssrv::sf::Path sf_path;
|
||||
R_TRY(FspPathPrintf(std::addressof(sf_path), "%s", path));
|
||||
|
||||
/* Open the filesystem using libnx bindings. */
|
||||
::FsFileSystem fs;
|
||||
R_TRY(fsldrOpenCodeFileSystem(program_id.value, sf_path.str, std::addressof(fs)));
|
||||
|
||||
/* Allocate a new filesystem wrapper. */
|
||||
auto fsa = std::make_unique<RemoteFileSystem>(fs);
|
||||
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
|
||||
*out = std::move(fsa);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result OpenPackageFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out, const char *common_path) {
|
||||
/* Open a filesystem using libnx bindings. */
|
||||
FsFileSystem fs;
|
||||
R_TRY(fsOpenFileSystemWithId(std::addressof(fs), ncm::InvalidProgramId.value, static_cast<::FsFileSystemType>(impl::FileSystemProxyType_Package), common_path));
|
||||
|
||||
/* Allocate a new filesystem wrapper. */
|
||||
auto fsa = std::make_unique<RemoteFileSystem>(fs);
|
||||
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
|
||||
*out = std::move(fsa);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result OpenSdCardCodeFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out, ncm::ProgramId program_id) {
|
||||
/* Ensure we don't access the SD card too early. */
|
||||
R_UNLESS(cfg::IsSdCardInitialized(), fs::ResultSdCardNotPresent());
|
||||
|
||||
/* Print a path to the program's package. */
|
||||
fssrv::sf::Path sf_path;
|
||||
R_TRY(FspPathPrintf(std::addressof(sf_path), "%s:/atmosphere/contents/%016lx/exefs.nsp", impl::SdCardFileSystemMountName, program_id.value));
|
||||
|
||||
return OpenPackageFileSystemImpl(out, sf_path.str);
|
||||
}
|
||||
|
||||
Result OpenSdCardCodeOrCodeFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out, const char *path, ncm::ProgramId program_id) {
|
||||
/* If we can open an sd card code fs, use it. */
|
||||
R_SUCCEED_IF(R_SUCCEEDED(OpenSdCardCodeFileSystemImpl(out, program_id)));
|
||||
|
||||
/* Otherwise, fall back to a normal code fs. */
|
||||
return OpenCodeFileSystemImpl(out, path, program_id);
|
||||
}
|
||||
|
||||
Result OpenHblCodeFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out) {
|
||||
/* Get the HBL path. */
|
||||
const char *hbl_path = cfg::GetHblPath();
|
||||
|
||||
/* Print a path to the hbl package. */
|
||||
fssrv::sf::Path sf_path;
|
||||
R_TRY(FspPathPrintf(std::addressof(sf_path), "%s:/%s", impl::SdCardFileSystemMountName, hbl_path[0] == '/' ? hbl_path + 1 : hbl_path));
|
||||
|
||||
return OpenPackageFileSystemImpl(out, sf_path.str);
|
||||
}
|
||||
|
||||
Result OpenSdCardFileSystemImpl(std::unique_ptr<fsa::IFileSystem> *out) {
|
||||
/* Open the SD card. This uses libnx bindings. */
|
||||
FsFileSystem fs;
|
||||
R_TRY(fsOpenSdCardFileSystem(std::addressof(fs)));
|
||||
|
||||
/* Allocate a new filesystem wrapper. */
|
||||
auto fsa = std::make_unique<RemoteFileSystem>(fs);
|
||||
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
|
||||
*out = std::move(fsa);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
class OpenFileOnlyFileSystem : public fsa::IFileSystem, public impl::Newable {
|
||||
private:
|
||||
virtual Result CommitImpl() override final {
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
virtual Result OpenDirectoryImpl(std::unique_ptr<fsa::IDirectory> *out_dir, const char *path, OpenDirectoryMode mode) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result GetEntryTypeImpl(DirectoryEntryType *out, const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result CreateFileImpl(const char *path, s64 size, int flags) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result DeleteFileImpl(const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result CreateDirectoryImpl(const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result DeleteDirectoryImpl(const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result DeleteDirectoryRecursivelyImpl(const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result RenameFileImpl(const char *old_path, const char *new_path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result RenameDirectoryImpl(const char *old_path, const char *new_path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result CleanDirectoryRecursivelyImpl(const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result GetFreeSpaceSizeImpl(s64 *out, const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result GetTotalSpaceSizeImpl(s64 *out, const char *path) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
|
||||
virtual Result CommitProvisionallyImpl(s64 counter) override final {
|
||||
return fs::ResultUnsupportedOperation();
|
||||
}
|
||||
};
|
||||
|
||||
class SdCardRedirectionCodeFileSystem : public OpenFileOnlyFileSystem {
|
||||
private:
|
||||
std::optional<ReadOnlyFileSystem> sd_content_fs;
|
||||
ReadOnlyFileSystem code_fs;
|
||||
bool is_redirect;
|
||||
public:
|
||||
SdCardRedirectionCodeFileSystem(std::unique_ptr<fsa::IFileSystem> &&code, ncm::ProgramId program_id, bool redirect) : code_fs(std::move(code)), is_redirect(redirect) {
|
||||
if (!cfg::IsSdCardInitialized()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Open an SD card filesystem. */
|
||||
std::unique_ptr<fsa::IFileSystem> sd_fs;
|
||||
if (R_FAILED(OpenSdCardFileSystemImpl(std::addressof(sd_fs)))) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Create a redirection filesystem to the relevant content folder. */
|
||||
char path[fs::EntryNameLengthMax + 1];
|
||||
std::snprintf(path, sizeof(path), "/atmosphere/contents/%016lx/exefs", program_id.value);
|
||||
|
||||
auto subdir_fs = std::make_unique<fssystem::SubDirectoryFileSystem>(std::move(sd_fs), path);
|
||||
if (subdir_fs == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
sd_content_fs.emplace(std::move(subdir_fs));
|
||||
}
|
||||
private:
|
||||
bool IsFileStubbed(const char *path) {
|
||||
/* If we don't have an sd content fs, nothing is stubbed. */
|
||||
if (!this->sd_content_fs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Create a path representing the stub. */
|
||||
char stub_path[fs::EntryNameLengthMax + 1];
|
||||
std::snprintf(stub_path, sizeof(stub_path), "%s.stub", path);
|
||||
|
||||
/* Query whether we have the file. */
|
||||
bool has_file;
|
||||
if (R_FAILED(fssystem::HasFile(std::addressof(has_file), std::addressof(*this->sd_content_fs), stub_path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return has_file;
|
||||
}
|
||||
|
||||
virtual Result OpenFileImpl(std::unique_ptr<fsa::IFile> *out_file, const char *path, OpenMode mode) override final {
|
||||
/* Only allow opening files with mode = read. */
|
||||
R_UNLESS((mode & fs::OpenMode_All) == fs::OpenMode_Read, fs::ResultInvalidArgument());
|
||||
|
||||
/* If we support redirection, we'd like to prefer a file from the sd card. */
|
||||
if (this->is_redirect) {
|
||||
R_SUCCEED_IF(R_SUCCEEDED(this->sd_content_fs->OpenFile(out_file, path, mode)));
|
||||
}
|
||||
|
||||
/* Otherwise, check if the file is stubbed. */
|
||||
R_UNLESS(!this->IsFileStubbed(path), fs::ResultPathNotFound());
|
||||
|
||||
/* Open a file from the base code fs. */
|
||||
return this->code_fs.OpenFile(out_file, path, mode);
|
||||
}
|
||||
};
|
||||
|
||||
class AtmosphereCodeFileSystem : public OpenFileOnlyFileSystem {
|
||||
private:
|
||||
std::optional<SdCardRedirectionCodeFileSystem> code_fs;
|
||||
std::optional<ReadOnlyFileSystem> hbl_fs;
|
||||
ncm::ProgramId program_id;
|
||||
bool initialized;
|
||||
public:
|
||||
AtmosphereCodeFileSystem() : initialized(false) { /* ... */ }
|
||||
|
||||
Result Initialize(const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) {
|
||||
AMS_ABORT_UNLESS(!this->initialized);
|
||||
|
||||
/* If we're hbl, we need to open a hbl fs. */
|
||||
if (is_hbl) {
|
||||
std::unique_ptr<fsa::IFileSystem> fsa;
|
||||
R_TRY(OpenHblCodeFileSystemImpl(std::addressof(fsa)));
|
||||
this->hbl_fs.emplace(std::move(fsa));
|
||||
}
|
||||
|
||||
/* Open the code filesystem. */
|
||||
std::unique_ptr<fsa::IFileSystem> fsa;
|
||||
R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id));
|
||||
this->code_fs.emplace(std::move(fsa), program_id, is_specific);
|
||||
|
||||
this->initialized = true;
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
private:
|
||||
virtual Result OpenFileImpl(std::unique_ptr<fsa::IFile> *out_file, const char *path, OpenMode mode) override final {
|
||||
/* Ensure that we're initialized. */
|
||||
R_UNLESS(this->initialized, fs::ResultNotInitialized());
|
||||
|
||||
/* Only allow opening files with mode = read. */
|
||||
R_UNLESS((mode & fs::OpenMode_All) == fs::OpenMode_Read, fs::ResultInvalidArgument());
|
||||
|
||||
/* First, check if there's an external code. */
|
||||
{
|
||||
fsa::IFileSystem *ecs = fssystem::GetExternalCodeFileSystem(this->program_id);
|
||||
if (ecs != nullptr) {
|
||||
return ecs->OpenFile(out_file, path, mode);
|
||||
}
|
||||
}
|
||||
|
||||
/* If we're hbl, open from the hbl fs. */
|
||||
if (this->hbl_fs) {
|
||||
return this->hbl_fs->OpenFile(out_file, path, mode);
|
||||
}
|
||||
|
||||
/* If we're not hbl, fall back to our code filesystem. */
|
||||
return this->code_fs->OpenFile(out_file, path, mode);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Result MountCode(const char *name, const char *path, ncm::ProgramId program_id) {
|
||||
/* Validate the mount name. */
|
||||
R_TRY(impl::CheckMountName(name));
|
||||
|
@ -25,20 +280,49 @@ namespace ams::fs {
|
|||
/* Validate the path isn't null. */
|
||||
R_UNLESS(path != nullptr, fs::ResultInvalidPath());
|
||||
|
||||
/* Print a path suitable for the remove service. */
|
||||
fssrv::sf::Path sf_path;
|
||||
R_TRY(FspPathPrintf(std::addressof(sf_path), "%s", path));
|
||||
|
||||
/* Open the filesystem using libnx bindings. */
|
||||
::FsFileSystem fs;
|
||||
R_TRY(fsldrOpenCodeFileSystem(program_id.value, sf_path.str, std::addressof(fs)));
|
||||
|
||||
/* Allocate a new filesystem wrapper. */
|
||||
auto fsa = std::make_unique<RemoteFileSystem>(fs);
|
||||
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
/* Open the code file system. */
|
||||
std::unique_ptr<fsa::IFileSystem> fsa;
|
||||
R_TRY(OpenCodeFileSystemImpl(std::addressof(fsa), path, program_id));
|
||||
|
||||
/* Register. */
|
||||
return fsa::Register(name, std::move(fsa));
|
||||
}
|
||||
|
||||
Result MountCodeForAtmosphereWithRedirection(const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) {
|
||||
/* Validate the mount name. */
|
||||
R_TRY(impl::CheckMountName(name));
|
||||
|
||||
/* Validate the path isn't null. */
|
||||
R_UNLESS(path != nullptr, fs::ResultInvalidPath());
|
||||
|
||||
/* Create an AtmosphereCodeFileSystem. */
|
||||
auto ams_code_fs = std::make_unique<AtmosphereCodeFileSystem>();
|
||||
R_UNLESS(ams_code_fs != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
|
||||
/* Initialize the code file system. */
|
||||
R_TRY(ams_code_fs->Initialize(path, program_id, is_hbl, is_specific));
|
||||
|
||||
/* Register. */
|
||||
return fsa::Register(name, std::move(ams_code_fs));
|
||||
}
|
||||
|
||||
Result MountCodeForAtmosphere(const char *name, const char *path, ncm::ProgramId program_id) {
|
||||
/* Validate the mount name. */
|
||||
R_TRY(impl::CheckMountName(name));
|
||||
|
||||
/* Validate the path isn't null. */
|
||||
R_UNLESS(path != nullptr, fs::ResultInvalidPath());
|
||||
|
||||
/* Open the code file system. */
|
||||
std::unique_ptr<fsa::IFileSystem> fsa;
|
||||
R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id));
|
||||
|
||||
/* Create a wrapper fs. */
|
||||
auto wrap_fsa = std::make_unique<SdCardRedirectionCodeFileSystem>(std::move(fsa), program_id, false);
|
||||
R_UNLESS(wrap_fsa != nullptr, fs::ResultAllocationFailureInCodeA());
|
||||
|
||||
/* Register. */
|
||||
return fsa::Register(name, std::move(wrap_fsa));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,26 +19,6 @@
|
|||
|
||||
namespace ams::fs {
|
||||
|
||||
namespace {
|
||||
|
||||
Result HasEntry(bool *out, const char *path, fs::DirectoryEntryType type) {
|
||||
/* Set out to false initially. */
|
||||
*out = false;
|
||||
|
||||
/* Try to get the entry type. */
|
||||
fs::DirectoryEntryType entry_type;
|
||||
R_TRY_CATCH(fs::GetEntryType(std::addressof(entry_type), path)) {
|
||||
/* If the path doesn't exist, nothing has gone wrong. */
|
||||
R_CONVERT(fs::ResultPathNotFound, ResultSuccess());
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
/* We succeeded. */
|
||||
*out = entry_type == type;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Result EnsureDirectoryRecursively(const char *path) {
|
||||
/* Get the filesystem accessor and sub path. */
|
||||
impl::FileSystemAccessor *accessor;
|
||||
|
@ -60,11 +40,21 @@ namespace ams::fs {
|
|||
}
|
||||
|
||||
Result HasFile(bool *out, const char *path) {
|
||||
return HasEntry(out, path, fs::DirectoryEntryType_File);
|
||||
/* Get the filesystem accessor and sub path. */
|
||||
impl::FileSystemAccessor *accessor;
|
||||
const char *sub_path;
|
||||
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
|
||||
|
||||
return fssystem::HasFile(out, accessor->GetRawFileSystemUnsafe(), sub_path);
|
||||
}
|
||||
|
||||
Result HasDirectory(bool *out, const char *path) {
|
||||
return HasEntry(out, path, fs::DirectoryEntryType_Directory);
|
||||
/* Get the filesystem accessor and sub path. */
|
||||
impl::FileSystemAccessor *accessor;
|
||||
const char *sub_path;
|
||||
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
|
||||
|
||||
return fssystem::HasDirectory(out, accessor->GetRawFileSystemUnsafe(), sub_path);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue