Implement the NCM sysmodule (closes #91)

* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Updated AddOnContentLocationResolver and RegisteredLocationResolver to 9.0.0

* Finished updating lr to 9.0.0

* Updated NCM to 9.0.0

* Fix libstrat includes

* Fixed application launching

* title_id_2 -> owner_tid

* Updated to new-ipc

* Change to using pure virtuals

* Title Id -> Program Id

* Fixed compilation against master

* std::scoped_lock<> -> std::scoped_lock

* Adopted R_UNLESS and R_CONVERT

* Prefix namespace to Results

* Adopt std::numeric_limits

* Fixed incorrect error handling in ReadFile

* Adopted AMS_ABORT_UNLESS

* Adopt util::GenerateUuid()

* Syntax improvements

* ncm_types: Address review

* Address more review comments

* Updated copyrights

* Address more feedback

* More feedback addressed

* More changes

* Move dispatch tables out of interface files

* Addressed remaining comments

* lr: move into libstratosphere

* ncm: Fix logic inversion

* lr: Add comments

* lr: Remove whitespace

* ncm: Start addressing feedback

* ncm: Cleanup InitializeContentManager

* lr: support client-side usage

* lr_service -> lr_api

* ncm: Begin refactoring content manager

* ncm: More content manager improvements

* ncm: Content manager mount improvements

* ldr: use lr bindings

* lr bindings usage: minor fixes

* ncm/lr: Pointer placement

* ncm: placeholder accessor cleanup

* ncm: minor fixes

* ncm: refactor rights cache

* ncm: content meta database cleanup

* ncm: move content meta database impl out of interface file

* ncm: Use const ContentMetaKey &

* ncm: fix other non-const ContentMetaKey references

* ncm: content meta database cleanup

* ncm: content storage fixes for 2.0.0

* ncm: add missing end of file newlines

* ncm: implement ContentMetaReader

* ncm: client-side api

* ncm: trim trailing spaces

* ncm: FS_MAX_PATH-1 -> fs::EntryNameLengthMax

* ncm: Use PathString and Path

* fs: implement accessor wrappers for ncm

* fs: implement user fs wrappers

* fs: add MountSdCard

* ncm: move to content manager impl

* ncm: fix up main

* kvdb: use fs::

* fs: Add wrappers needed for ncm

* ncm: use fs bindings, other refactoring

* ncm: minor fixes

* fsa: fix ReadFile without size output

* fs: add substorage, rom path tool

* ncm: fix dangling fsdev usage

* fs: fix bug in Commit

* fs: fixed incorrect mode check

* fs: implement Mount(System)Data

* ncm: don't delete hos

* results: add R_SUCCEED_IF

* ams-except-ncm: use R_SUCCEED_IF

* ncm: added comments

* ncm: fix api definitions

* ncm: use R_SUCCEED_IF

* pm: think of the savings

* ncm: employ kernel strats

* ncm: Nintendo has 5 MiB of heap. Give ourselves 4 to be safe, pending analysis

* ncm: refactor IDs, split types header into many headers

* ams.mitm: use fs bindings instead of stdio

* fs: SystemData uses SystemDataId

* ncm: improve meta-db accuracy

* ncm: inline getlatestkey

* fs: improve UnsupportedOperation results

* fs: modernize mount utils

* ams: misc fixes for merge-errors

* fs: improve unsupportedoperation results

* git subrepo pull emummc

subrepo:
  subdir:   "emummc"
  merged:   "d12dd546"
upstream:
  origin:   "https://github.com/m4xw/emuMMC"
  branch:   "develop"
  commit:   "d12dd546"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"

* util: add boundedmap

* ncm: minor style fixes

* ncm: don't unmount if mounting fails

* lr: bug fixes

* ncm: implement ncm.for-initialize + ncm.for-safemode

* lr: ncm::ProgramId::Invalid -> ncm::InvalidProgramId

* ncm: fix open directory mode on 1.0.0

* ncm: fix fs use, implement more of < 4.0.0 for-initialize/safemode

* ncm: implement packagedcontent -> content for building metadb

* ncm: fix save data flag management

* ncm: address some review suggestions (thanks @leoetlino!)

* updater: use fs bindings

* fs: implement MountCode

* fs: prefer make_unique to operator new

* ncm: implement remaining ContentMetaDatabaseBuilder functionality

Co-authored-by: Michael Scire <SciresM@gmail.com>
This commit is contained in:
Adubbz 2020-03-08 19:06:23 +11:00 committed by GitHub
parent f9403201f0
commit c7026b9094
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
254 changed files with 16876 additions and 1274 deletions

View file

@ -0,0 +1,127 @@
/*
* 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 {
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 = strnlen(bis_mount_name, MountNameLengthMax) + 2;
AMS_ABORT_UNLESS(dst_size >= needed_size);
/* Generate the name. */
auto size = std::snprintf(dst, dst_size, "%s:", bis_mount_name);
AMS_ASSERT(static_cast<size_t>(size) == needed_size - 1);
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. */
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 = PathTool::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_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();
}
}

View file

@ -0,0 +1,44 @@
/*
* 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 MountCode(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());
/* 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());
/* Register. */
return fsa::Register(name, std::move(fsa));
}
}

View file

@ -0,0 +1,74 @@
/*
* 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 {
namespace {
impl::FileSystemProxyType ConvertToFileSystemProxyType(ContentType content_type) {
switch (content_type) {
case ContentType_Control: return impl::FileSystemProxyType_Control;
case ContentType_Manual: return impl::FileSystemProxyType_Manual;
case ContentType_Meta: return impl::FileSystemProxyType_Meta;
case ContentType_Data: return impl::FileSystemProxyType_Data;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
Result MountContentImpl(const char *name, const char *path, u64 id, impl::FileSystemProxyType type) {
/* Open a filesystem using libnx bindings. */
FsFileSystem fs;
R_TRY(fsOpenFileSystemWithId(std::addressof(fs), id, static_cast<::FsFileSystemType>(type), path));
/* Allocate a new filesystem wrapper. */
auto fsa = std::make_unique<RemoteFileSystem>(fs);
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInContentA());
/* Register. */
return fsa::Register(name, std::move(fsa));
}
Result MountContent(const char *name, const char *path, u64 id, ContentType content_type) {
/* Validate the mount name. */
R_TRY(impl::CheckMountNameAllowingReserved(name));
/* Validate the path. */
R_UNLESS(path != nullptr, fs::ResultInvalidPath());
/* Mount the content. */
return MountContentImpl(name, path, id, ConvertToFileSystemProxyType(content_type));
}
}
Result MountContent(const char *name, const char *path, ContentType content_type) {
/* This API only supports mounting Meta content. */
AMS_ABORT_UNLESS(content_type == ContentType_Meta);
return MountContent(name, path, ncm::InvalidProgramId, content_type);
}
Result MountContent(const char *name, const char *path, ncm::ProgramId id, ContentType content_type) {
return MountContent(name, path, id.value, content_type);
}
Result MountContent(const char *name, const char *path, ncm::DataId id, ContentType content_type) {
return MountContent(name, path, id.value, content_type);
}
}

View file

@ -0,0 +1,93 @@
/*
* 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 {
namespace {
class ContentStorageCommonMountNameGenerator : public fsa::ICommonMountNameGenerator, public impl::Newable {
private:
const ContentStorageId id;
public:
explicit ContentStorageCommonMountNameGenerator(ContentStorageId i) : id(i) { /* ... */ }
virtual Result GenerateCommonMountName(char *dst, size_t dst_size) override {
/* Determine how much space we need. */
const size_t needed_size = strnlen(GetContentStorageMountName(id), MountNameLengthMax) + 2;
AMS_ABORT_UNLESS(dst_size >= needed_size);
/* Generate the name. */
auto size = std::snprintf(dst, dst_size, "%s:", GetContentStorageMountName(id));
AMS_ASSERT(static_cast<size_t>(size) == needed_size - 1);
return ResultSuccess();
}
};
}
const char *GetContentStorageMountName(ContentStorageId id) {
switch (id) {
case ContentStorageId::System: return impl::ContentStorageSystemMountName;
case ContentStorageId::User: return impl::ContentStorageUserMountName;
case ContentStorageId::SdCard: return impl::ContentStorageSdCardMountName;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
Result MountContentStorage(ContentStorageId id) {
return MountContentStorage(GetContentStorageMountName(id), id);
}
Result MountContentStorage(const char *name, ContentStorageId id) {
/* Validate the mount name. */
R_TRY(impl::CheckMountNameAllowingReserved(name));
/* It can take some time for the system partition to be ready (if it's on the SD card). */
/* Thus, we will retry up to 10 times, waiting one second each time. */
constexpr size_t MaxRetries = 10;
constexpr u64 RetryInterval = 1'000'000'000ul;
/* Mount the content storage, use libnx bindings. */
::FsFileSystem fs;
for (size_t i = 0; i < MaxRetries; i++) {
R_TRY_CATCH(fsOpenContentStorageFileSystem(std::addressof(fs), static_cast<::FsContentStorageId>(id))) {
R_CATCH(fs::ResultSystemPartitionNotReady) {
if (i < MaxRetries - 1) {
/* TODO: os::SleepThread */
svcSleepThread(RetryInterval);
} else {
return fs::ResultSystemPartitionNotReady();
}
}
} R_END_TRY_CATCH;
}
/* Allocate a new filesystem wrapper. */
auto fsa = std::make_unique<RemoteFileSystem>(fs);
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInContentStorageA());
/* Allocate a new mountname generator. */
auto generator = std::make_unique<ContentStorageCommonMountNameGenerator>(id);
R_UNLESS(generator != nullptr, fs::ResultAllocationFailureInContentStorageB());
/* Register. */
return fsa::Register(name, std::move(fsa), std::move(generator));
}
}

View file

@ -0,0 +1,89 @@
/*
* 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::impl {
namespace {
Result OpenDataStorageByDataId(std::unique_ptr<ams::fs::IStorage> *out, ncm::DataId data_id, ncm::StorageId storage_id) {
/* Open storage using libnx bindings. */
::FsStorage s;
R_TRY_CATCH(fsOpenDataStorageByDataId(std::addressof(s), data_id.value, static_cast<::NcmStorageId>(storage_id))) {
R_CONVERT(ncm::ResultContentMetaNotFound, fs::ResultTargetNotFound());
} R_END_TRY_CATCH;
auto storage = std::make_unique<RemoteStorage>(s);
R_UNLESS(storage != nullptr, fs::ResultAllocationFailureInDataA());
*out = std::move(storage);
return ResultSuccess();
}
Result MountDataImpl(const char *name, ncm::DataId data_id, ncm::StorageId storage_id, void *cache_buffer, size_t cache_size, bool use_cache, bool use_data_cache, bool use_path_cache) {
std::unique_ptr<fs::IStorage> storage;
R_TRY(OpenDataStorageByDataId(std::addressof(storage), data_id, storage_id));
auto fs = std::make_unique<RomFsFileSystem>();
R_UNLESS(fs != nullptr, fs::ResultAllocationFailureInDataB());
R_TRY(fs->Initialize(std::move(storage), cache_buffer, cache_size, use_cache));
return fsa::Register(name, std::move(fs), nullptr, use_data_cache, use_path_cache, false);
}
}
Result QueryMountDataCacheSize(size_t *out, ncm::DataId data_id, ncm::StorageId storage_id) {
R_UNLESS(out != nullptr, fs::ResultNullptrArgument());
std::unique_ptr<fs::IStorage> storage;
R_TRY(OpenDataStorageByDataId(std::addressof(storage), data_id, storage_id));
size_t size = 0;
R_TRY(RomFsFileSystem::GetRequiredWorkingMemorySize(std::addressof(size), storage.get()));
constexpr size_t MinimumCacheSize = 32;
*out = std::max(size, MinimumCacheSize);
return ResultSuccess();
}
Result MountData(const char *name, ncm::DataId data_id, ncm::StorageId storage_id) {
/* Validate the mount name. */
R_TRY(impl::CheckMountName(name));
return MountDataImpl(name, data_id, storage_id, nullptr, 0, false, false, false);
}
Result MountData(const char *name, ncm::DataId data_id, ncm::StorageId storage_id, void *cache_buffer, size_t cache_size) {
/* Validate the mount name. */
R_TRY(impl::CheckMountName(name));
R_UNLESS(cache_buffer != nullptr, fs::ResultNullptrArgument());
return MountDataImpl(name, data_id, storage_id, cache_buffer, cache_size, true, false, false);
}
Result MountData(const char *name, ncm::DataId data_id, ncm::StorageId storage_id, void *cache_buffer, size_t cache_size, bool use_data_cache, bool use_path_cache) {
/* Validate the mount name. */
R_TRY(impl::CheckMountName(name));
R_UNLESS(cache_buffer != nullptr, fs::ResultNullptrArgument());
return MountDataImpl(name, data_id, storage_id, cache_buffer, cache_size, true, use_data_cache, use_path_cache);
}
}

View file

@ -0,0 +1,578 @@
/*
* 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>
namespace ams::fs {
s64 HierarchicalRomFileTable::QueryDirectoryEntryStorageSize(u32 count) {
const size_t real_count = count + ReservedDirectoryCount;
return DirectoryEntryMapTable::QueryKeyValueStorageSize(real_count) + real_count * (RomPathTool::MaxPathLength + 1) * sizeof(RomPathChar);
}
s64 HierarchicalRomFileTable::QueryDirectoryEntryBucketStorageSize(s64 count) {
return DirectoryEntryMapTable::QueryBucketStorageSize(count);
}
s64 HierarchicalRomFileTable::QueryFileEntryStorageSize(u32 count) {
return FileEntryMapTable::QueryKeyValueStorageSize(count) + count * (RomPathTool::MaxPathLength + 1) * sizeof(RomPathChar);
}
s64 HierarchicalRomFileTable::QueryFileEntryBucketStorageSize(s64 count) {
return FileEntryMapTable::QueryBucketStorageSize(count);
}
Result HierarchicalRomFileTable::Format(SubStorage dir_bucket, SubStorage file_bucket) {
s64 dir_bucket_size;
R_TRY(dir_bucket.GetSize(std::addressof(dir_bucket_size)));
R_TRY(DirectoryEntryMapTable::Format(dir_bucket, DirectoryEntryMapTable::QueryBucketCount(dir_bucket_size)));
s64 file_bucket_size;
R_TRY(file_bucket.GetSize(std::addressof(file_bucket_size)));
R_TRY(FileEntryMapTable::Format(file_bucket, FileEntryMapTable::QueryBucketCount(file_bucket_size)));
return ResultSuccess();
}
HierarchicalRomFileTable::HierarchicalRomFileTable() { /* ... */ }
Result HierarchicalRomFileTable::Initialize(SubStorage dir_bucket, SubStorage dir_entry, SubStorage file_bucket, SubStorage file_entry) {
s64 dir_bucket_size;
R_TRY(dir_bucket.GetSize(std::addressof(dir_bucket_size)));
R_TRY(this->dir_table.Initialize(dir_bucket, DirectoryEntryMapTable::QueryBucketCount(dir_bucket_size), dir_entry));
s64 file_bucket_size;
R_TRY(file_bucket.GetSize(std::addressof(file_bucket_size)));
R_TRY(this->file_table.Initialize(file_bucket, FileEntryMapTable::QueryBucketCount(file_bucket_size), file_entry));
return ResultSuccess();
}
void HierarchicalRomFileTable::Finalize() {
this->dir_table.Finalize();
this->file_table.Finalize();
}
Result HierarchicalRomFileTable::CreateRootDirectory() {
Position root_pos = RootPosition;
EntryKey root_key = {};
root_key.key.parent = root_pos;
RomPathTool::InitializeRomEntryName(std::addressof(root_key.name));
RomDirectoryEntry root_entry = {
.next = InvalidPosition,
.dir = InvalidPosition,
.file = InvalidPosition,
};
return this->dir_table.Add(std::addressof(root_pos), root_key, root_entry);
}
Result HierarchicalRomFileTable::CreateDirectory(RomDirectoryId *out, const RomPathChar *path, const DirectoryInfo &info) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey new_key = {};
R_TRY(this->FindDirectoryRecursive(std::addressof(new_key), std::addressof(parent_entry), path));
R_TRY(this->CheckSameEntryExists(new_key, fs::ResultDbmAlreadyExists()));
RomDirectoryEntry new_entry = {
.next = InvalidPosition,
.dir = InvalidPosition,
.file = InvalidPosition,
};
Position new_pos = 0;
R_TRY_CATCH(this->dir_table.Add(std::addressof(new_pos), new_key, new_entry)) {
R_CONVERT(fs::ResultDbmKeyFull, fs::ResultDbmDirectoryEntryFull())
} R_END_TRY_CATCH;
*out = ConvertToDirectoryId(new_pos);
if (parent_entry.dir == InvalidPosition) {
parent_entry.dir = new_pos;
R_TRY(this->dir_table.SetByPosition(new_key.key.parent, parent_entry));
} else {
Position cur_pos = parent_entry.dir;
while (true) {
RomEntryKey cur_key = {};
RomDirectoryEntry cur_entry = {};
R_TRY(this->dir_table.GetByPosition(std::addressof(cur_key), std::addressof(cur_entry), cur_pos));
if (cur_entry.next == InvalidPosition) {
cur_entry.next = new_pos;
R_TRY(this->dir_table.SetByPosition(cur_pos, cur_entry));
break;
}
cur_pos = cur_entry.next;
}
}
return ResultSuccess();
}
Result HierarchicalRomFileTable::CreateFile(RomFileId *out, const RomPathChar *path, const FileInfo &info) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey new_key = {};
R_TRY(this->FindFileRecursive(std::addressof(new_key), std::addressof(parent_entry), path));
R_TRY(this->CheckSameEntryExists(new_key, fs::ResultDbmAlreadyExists()));
RomFileEntry new_entry = {
.next = InvalidPosition,
.info = info,
};
Position new_pos = 0;
R_TRY_CATCH(this->file_table.Add(std::addressof(new_pos), new_key, new_entry)) {
R_CONVERT(fs::ResultDbmKeyFull, fs::ResultDbmFileEntryFull())
} R_END_TRY_CATCH;
*out = ConvertToFileId(new_pos);
if (parent_entry.file == InvalidPosition) {
parent_entry.file = new_pos;
R_TRY(this->dir_table.SetByPosition(new_key.key.parent, parent_entry));
} else {
Position cur_pos = parent_entry.file;
while (true) {
RomEntryKey cur_key = {};
RomFileEntry cur_entry = {};
R_TRY(this->file_table.GetByPosition(std::addressof(cur_key), std::addressof(cur_entry), cur_pos));
if (cur_entry.next == InvalidPosition) {
cur_entry.next = new_pos;
R_TRY(this->file_table.SetByPosition(cur_pos, cur_entry));
break;
}
cur_pos = cur_entry.next;
}
}
return ResultSuccess();
}
Result HierarchicalRomFileTable::ConvertPathToDirectoryId(RomDirectoryId *out, const RomPathChar *path) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey key = {};
R_TRY(this->FindDirectoryRecursive(std::addressof(key), std::addressof(parent_entry), path));
Position pos = 0;
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(pos), std::addressof(entry), key));
*out = ConvertToDirectoryId(pos);
return ResultSuccess();
}
Result HierarchicalRomFileTable::ConvertPathToFileId(RomFileId *out, const RomPathChar *path) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey key = {};
R_TRY(this->FindDirectoryRecursive(std::addressof(key), std::addressof(parent_entry), path));
Position pos = 0;
RomFileEntry entry = {};
R_TRY(this->GetFileEntry(std::addressof(pos), std::addressof(entry), key));
*out = ConvertToFileId(pos);
return ResultSuccess();
}
Result HierarchicalRomFileTable::GetDirectoryInformation(DirectoryInfo *out, const RomPathChar *path) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey key = {};
R_TRY(this->FindDirectoryRecursive(std::addressof(key), std::addressof(parent_entry), path));
return this->GetDirectoryInformation(out, key);
}
Result HierarchicalRomFileTable::GetDirectoryInformation(DirectoryInfo *out, RomDirectoryId id) {
AMS_ASSERT(out != nullptr);
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(entry), id));
return ResultSuccess();
}
Result HierarchicalRomFileTable::OpenFile(FileInfo *out, const RomPathChar *path) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey key = {};
R_TRY(this->FindFileRecursive(std::addressof(key), std::addressof(parent_entry), path));
return this->OpenFile(out, key);
}
Result HierarchicalRomFileTable::OpenFile(FileInfo *out, RomFileId id) {
AMS_ASSERT(out != nullptr);
RomFileEntry entry = {};
R_TRY(this->GetFileEntry(std::addressof(entry), id));
*out = entry.info;
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindOpen(FindPosition *out, const RomPathChar *path) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(path != nullptr);
RomDirectoryEntry parent_entry = {};
EntryKey key = {};
R_TRY(this->FindDirectoryRecursive(std::addressof(key), std::addressof(parent_entry), path));
return this->FindOpen(out, key);
}
Result HierarchicalRomFileTable::FindOpen(FindPosition *out, RomDirectoryId id) {
AMS_ASSERT(out != nullptr);
out->next_dir = InvalidPosition;
out->next_file = InvalidPosition;
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(entry), id));
out->next_dir = entry.dir;
out->next_file = entry.file;
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindNextDirectory(RomPathChar *out, FindPosition *find, size_t length) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(find != nullptr);
AMS_ASSERT(length > RomPathTool::MaxPathLength);
R_UNLESS(find->next_dir != InvalidPosition, fs::ResultDbmFindFinished());
RomEntryKey key = {};
RomDirectoryEntry entry = {};
size_t aux_size = 0;
R_TRY(this->dir_table.GetByPosition(std::addressof(key), std::addressof(entry), out, std::addressof(aux_size), find->next_dir));
AMS_ASSERT(aux_size / sizeof(RomPathChar) <= RomPathTool::MaxPathLength);
out[aux_size / sizeof(RomPathChar)] = RomStringTraits::NullTerminator;
find->next_dir = entry.next;
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindNextFile(RomPathChar *out, FindPosition *find, size_t length) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(find != nullptr);
AMS_ASSERT(length > RomPathTool::MaxPathLength);
R_UNLESS(find->next_file != InvalidPosition, fs::ResultDbmFindFinished());
RomEntryKey key = {};
RomFileEntry entry = {};
size_t aux_size = 0;
R_TRY(this->file_table.GetByPosition(std::addressof(key), std::addressof(entry), out, std::addressof(aux_size), find->next_file));
AMS_ASSERT(aux_size / sizeof(RomPathChar) <= RomPathTool::MaxPathLength);
out[aux_size / sizeof(RomPathChar)] = RomStringTraits::NullTerminator;
find->next_file = entry.next;
return ResultSuccess();
}
Result HierarchicalRomFileTable::QueryRomFileSystemSize(s64 *out_dir_entry_size, s64 *out_file_entry_size) {
AMS_ASSERT(out_dir_entry_size != nullptr);
AMS_ASSERT(out_file_entry_size != nullptr);
*out_dir_entry_size = this->dir_table.GetTotalEntrySize();
*out_file_entry_size = this->file_table.GetTotalEntrySize();
return ResultSuccess();
}
Result HierarchicalRomFileTable::GetGrandParent(Position *out_pos, EntryKey *out_dir_key, RomDirectoryEntry *out_dir_entry, Position pos, RomPathTool::RomEntryName name, const RomPathChar *path) {
AMS_ASSERT(out_pos != nullptr);
AMS_ASSERT(out_dir_key != nullptr);
AMS_ASSERT(out_dir_entry != nullptr);
AMS_ASSERT(path != nullptr);
RomEntryKey gp_key = {};
RomDirectoryEntry gp_entry = {};
R_TRY(this->dir_table.GetByPosition(std::addressof(gp_key), std::addressof(gp_entry), pos));
out_dir_key->key.parent = gp_key.parent;
R_TRY(RomPathTool::GetParentDirectoryName(std::addressof(out_dir_key->name), name, path));
R_TRY(this->GetDirectoryEntry(out_pos, out_dir_entry, *out_dir_key));
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindParentDirectoryRecursive(Position *out_pos, EntryKey *out_dir_key, RomDirectoryEntry *out_dir_entry, RomPathTool::PathParser *parser, const RomPathChar *path) {
AMS_ASSERT(out_pos != nullptr);
AMS_ASSERT(out_dir_key != nullptr);
AMS_ASSERT(out_dir_entry != nullptr);
AMS_ASSERT(parser != nullptr);
AMS_ASSERT(path != nullptr);
Position dir_pos = RootPosition;
EntryKey dir_key = {};
RomDirectoryEntry dir_entry = {};
dir_key.key.parent = RootPosition;
R_TRY(parser->GetNextDirectoryName(std::addressof(dir_key.name)));
R_TRY(this->GetDirectoryEntry(std::addressof(dir_pos), std::addressof(dir_entry), dir_key));
Position parent_pos = dir_pos;
while (!parser->IsFinished()) {
EntryKey old_key = dir_key;
R_TRY(parser->GetNextDirectoryName(std::addressof(dir_key.name)));
if (RomPathTool::IsCurrentDirectory(dir_key.name)) {
dir_key = old_key;
continue;
} else if (RomPathTool::IsParentDirectory(dir_key.name)) {
R_UNLESS(parent_pos != RootPosition, fs::ResultDirectoryUnobtainable());
R_TRY(this->GetGrandParent(std::addressof(parent_pos), std::addressof(dir_key), std::addressof(dir_entry), dir_key.key.parent, dir_key.name, path));
} else {
dir_key.key.parent = parent_pos;
R_TRY_CATCH(this->GetDirectoryEntry(std::addressof(dir_pos), std::addressof(dir_entry), dir_key)) {
R_CONVERT(fs::ResultDbmInvalidOperation, fs::ResultDbmNotFound())
} R_END_TRY_CATCH;
parent_pos = dir_pos;
}
}
*out_pos = parent_pos;
*out_dir_key = dir_key;
*out_dir_entry = dir_entry;
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindPathRecursive(EntryKey *out_key, RomDirectoryEntry *out_dir_entry, bool is_dir, const RomPathChar *path) {
AMS_ASSERT(out_key != nullptr);
AMS_ASSERT(out_dir_entry != nullptr);
AMS_ASSERT(path != nullptr);
RomPathTool::PathParser parser;
R_TRY(parser.Initialize(path));
EntryKey parent_key = {};
Position parent_pos = 0;
R_TRY(this->FindParentDirectoryRecursive(std::addressof(parent_pos), std::addressof(parent_key), out_dir_entry, std::addressof(parser), path));
if (is_dir) {
RomPathTool::RomEntryName name = {};
R_TRY(parser.GetAsDirectoryName(std::addressof(name)));
if (RomPathTool::IsCurrentDirectory(name)) {
*out_key = parent_key;
if (out_key->key.parent != RootPosition) {
Position pos = 0;
R_TRY(this->GetGrandParent(std::addressof(pos), std::addressof(parent_key), out_dir_entry, out_key->key.parent, out_key->name, path));
}
} else if (RomPathTool::IsParentDirectory(name)) {
R_UNLESS(parent_pos != RootPosition, fs::ResultDirectoryUnobtainable());
Position pos = 0;
RomDirectoryEntry cur_entry = {};
R_TRY(this->GetGrandParent(std::addressof(pos), out_key, std::addressof(cur_entry), parent_key.key.parent, parent_key.name, path));
if (out_key->key.parent != RootPosition) {
R_TRY(this->GetGrandParent(std::addressof(pos), std::addressof(parent_key), out_dir_entry, out_key->key.parent, out_key->name, path));
}
} else {
out_key->name = name;
out_key->key.parent = (out_key->name.length > 0) ? parent_pos : RootPosition;
}
} else {
{
RomPathTool::RomEntryName name = {};
R_TRY(parser.GetAsDirectoryName(std::addressof(name)));
R_UNLESS(!RomPathTool::IsParentDirectory(name) || parent_pos != RootPosition, fs::ResultDirectoryUnobtainable());
}
R_UNLESS(!parser.IsDirectoryPath(), fs::ResultDbmInvalidOperation());
out_key->key.parent = parent_pos;
R_TRY(parser.GetAsFileName(std::addressof(out_key->name)));
}
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindDirectoryRecursive(EntryKey *out_key, RomDirectoryEntry *out_dir_entry, const RomPathChar *path) {
AMS_ASSERT(out_key != nullptr);
AMS_ASSERT(out_dir_entry != nullptr);
AMS_ASSERT(path != nullptr);
return this->FindPathRecursive(out_key, out_dir_entry, true, path);
}
Result HierarchicalRomFileTable::FindFileRecursive(EntryKey *out_key, RomDirectoryEntry *out_dir_entry, const RomPathChar *path) {
AMS_ASSERT(out_key != nullptr);
AMS_ASSERT(out_dir_entry != nullptr);
AMS_ASSERT(path != nullptr);
return this->FindPathRecursive(out_key, out_dir_entry, false, path);
}
Result HierarchicalRomFileTable::CheckSameEntryExists(const EntryKey &key, Result if_exists) {
/* Check dir */
{
Position pos = InvalidPosition;
RomDirectoryEntry entry = {};
const Result get_res = this->dir_table.Get(std::addressof(pos), std::addressof(entry), key);
if (!fs::ResultDbmKeyNotFound::Includes(get_res)) {
R_TRY(get_res);
return if_exists;
}
}
/* Check file */
{
Position pos = InvalidPosition;
RomFileEntry entry = {};
const Result get_res = this->file_table.Get(std::addressof(pos), std::addressof(entry), key);
if (!fs::ResultDbmKeyNotFound::Includes(get_res)) {
R_TRY(get_res);
return if_exists;
}
}
return ResultSuccess();
}
Result HierarchicalRomFileTable::GetDirectoryEntry(Position *out_pos, RomDirectoryEntry *out_entry, const EntryKey &key) {
AMS_ASSERT(out_pos != nullptr);
AMS_ASSERT(out_entry != nullptr);
const Result dir_res = this->dir_table.Get(out_pos, out_entry, key);
R_UNLESS(R_FAILED(dir_res), dir_res);
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(dir_res), dir_res);
Position pos = 0;
RomFileEntry entry = {};
const Result file_res = this->file_table.Get(std::addressof(pos), std::addressof(entry), key);
R_UNLESS(R_FAILED(file_res), fs::ResultDbmInvalidOperation());
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(file_res), fs::ResultDbmDirectoryNotFound());
return file_res;
}
Result HierarchicalRomFileTable::GetDirectoryEntry(RomDirectoryEntry *out_entry, RomDirectoryId id) {
AMS_ASSERT(out_entry != nullptr);
Position pos = ConvertToPosition(id);
RomEntryKey key = {};
const Result dir_res = this->dir_table.GetByPosition(std::addressof(key), out_entry, pos);
R_UNLESS(R_FAILED(dir_res), dir_res);
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(dir_res), dir_res);
RomFileEntry entry = {};
const Result file_res = this->file_table.GetByPosition(std::addressof(key), std::addressof(entry), pos);
R_UNLESS(R_FAILED(file_res), fs::ResultDbmInvalidOperation());
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(file_res), fs::ResultDbmDirectoryNotFound());
return file_res;
}
Result HierarchicalRomFileTable::GetFileEntry(Position *out_pos, RomFileEntry *out_entry, const EntryKey &key) {
AMS_ASSERT(out_pos != nullptr);
AMS_ASSERT(out_entry != nullptr);
const Result file_res = this->file_table.Get(out_pos, out_entry, key);
R_UNLESS(R_FAILED(file_res), file_res);
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(file_res), file_res);
Position pos = 0;
RomDirectoryEntry entry = {};
const Result dir_res = this->dir_table.Get(std::addressof(pos), std::addressof(entry), key);
R_UNLESS(R_FAILED(dir_res), fs::ResultDbmInvalidOperation());
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(dir_res), fs::ResultDbmFileNotFound());
return dir_res;
}
Result HierarchicalRomFileTable::GetFileEntry(RomFileEntry *out_entry, RomFileId id) {
AMS_ASSERT(out_entry != nullptr);
Position pos = ConvertToPosition(id);
RomEntryKey key = {};
const Result file_res = this->file_table.GetByPosition(std::addressof(key), out_entry, pos);
R_UNLESS(R_FAILED(file_res), file_res);
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(file_res), file_res);
RomDirectoryEntry entry = {};
const Result dir_res = this->dir_table.GetByPosition(std::addressof(key), std::addressof(entry), pos);
R_UNLESS(R_FAILED(dir_res), fs::ResultDbmInvalidOperation());
R_UNLESS(!fs::ResultDbmKeyNotFound::Includes(dir_res), fs::ResultDbmFileNotFound());
return dir_res;
}
Result HierarchicalRomFileTable::GetDirectoryInformation(DirectoryInfo *out, const EntryKey &key) {
AMS_ASSERT(out != nullptr);
Position pos = 0;
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(pos), std::addressof(entry), key));
return ResultSuccess();
}
Result HierarchicalRomFileTable::OpenFile(FileInfo *out, const EntryKey &key) {
AMS_ASSERT(out != nullptr);
Position pos = 0;
RomFileEntry entry = {};
R_TRY(this->GetFileEntry(std::addressof(pos), std::addressof(entry), key));
*out = entry.info;
return ResultSuccess();
}
Result HierarchicalRomFileTable::FindOpen(FindPosition *out, const EntryKey &key) {
AMS_ASSERT(out != nullptr);
out->next_dir = InvalidPosition;
out->next_file = InvalidPosition;
Position pos = 0;
RomDirectoryEntry entry = {};
R_TRY(this->GetDirectoryEntry(std::addressof(pos), std::addressof(entry), key));
out->next_dir = entry.dir;
out->next_file = entry.file;
return ResultSuccess();
}
}

View file

@ -0,0 +1,195 @@
/*
* 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>
namespace ams::fs::RomPathTool {
Result PathParser::Initialize(const RomPathChar *path) {
AMS_ASSERT(path != nullptr);
/* Require paths start with a separator, and skip repeated separators. */
R_UNLESS(IsSeparator(path[0]), fs::ResultDbmInvalidPathFormat());
while (IsSeparator(path[1])) {
path++;
}
this->prev_path_start = path;
this->prev_path_end = path;
this->next_path = path + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
}
return ResultSuccess();
}
void PathParser::Finalize() {
this->prev_path_start = nullptr;
this->prev_path_end = nullptr;
this->next_path = nullptr;
this->finished = false;
}
bool PathParser::IsFinished() const {
return this->finished;
}
bool PathParser::IsDirectoryPath() const {
AMS_ASSERT(this->next_path != nullptr);
if (IsNullTerminator(this->next_path[0]) && IsSeparator(this->next_path[-1])) {
return true;
}
if (IsCurrentDirectory(this->next_path)) {
return true;
}
if (IsParentDirectory(this->next_path)) {
return true;
}
return false;
}
Result PathParser::GetAsDirectoryName(RomEntryName *out) const {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
const size_t len = this->prev_path_end - this->prev_path_start;
R_UNLESS(len <= MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
out->length = len;
out->path = this->prev_path_start;
return ResultSuccess();
}
Result PathParser::GetAsFileName(RomEntryName *out) const {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
const size_t len = this->prev_path_end - this->prev_path_start;
R_UNLESS(len <= MaxPathLength, fs::ResultDbmFileNameTooLong());
out->length = len;
out->path = this->prev_path_start;
return ResultSuccess();
}
Result PathParser::GetNextDirectoryName(RomEntryName *out) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(this->prev_path_start != nullptr);
AMS_ASSERT(this->prev_path_end != nullptr);
AMS_ASSERT(this->next_path != nullptr);
/* Set the current path to output. */
out->length = this->prev_path_end - this->prev_path_start;
out->path = this->prev_path_start;
/* Parse the next path. */
this->prev_path_start = this->next_path;
const RomPathChar *cur = this->next_path;
for (size_t name_len = 0; true; name_len++) {
if (IsSeparator(cur[name_len])) {
R_UNLESS(name_len < MaxPathLength, fs::ResultDbmDirectoryNameTooLong());
this->prev_path_end = cur + name_len;
this->next_path = this->prev_path_end + 1;
while (IsSeparator(this->next_path[0])) {
this->next_path++;
}
if (IsNullTerminator(this->next_path[0])) {
this->finished = true;
}
break;
}
if (IsNullTerminator(cur[name_len])) {
this->finished = true;
this->prev_path_end = this->next_path = cur + name_len;
break;
}
}
return ResultSuccess();
}
Result GetParentDirectoryName(RomEntryName *out, const RomEntryName &cur, const RomPathChar *p) {
AMS_ASSERT(out != nullptr);
AMS_ASSERT(p != nullptr);
const RomPathChar *start = cur.path;
const RomPathChar *end = cur.path + cur.length - 1;
s32 depth = 1;
if (IsParentDirectory(cur)) {
depth++;
}
if (cur.path > p) {
size_t len = 0;
const RomPathChar *head = cur.path - 1;
while (head >= p) {
if (IsSeparator(*head)) {
if (IsCurrentDirectory(head + 1, len)) {
depth++;
}
if (IsParentDirectory(head + 1, len)) {
depth += 2;
}
if (depth == 0) {
start = head + 1;
break;
}
while (IsSeparator(*head)) {
head--;
}
end = head;
len = 0;
depth--;
}
len++;
head--;
}
R_UNLESS(depth == 0, fs::ResultDirectoryUnobtainable());
if (head == p) {
start = p + 1;
}
}
if (end <= p) {
out->path = p;
out->length = 0;
} else {
out->path = start;
out->length = end - start + 1;
}
return ResultSuccess();
}
}

View file

@ -0,0 +1,36 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs::impl {
constexpr inline size_t FilePathHashSize = 4;
struct FilePathHash : public Newable {
u8 data[FilePathHashSize];
};
static_assert(std::is_pod<FilePathHash>::value);
inline bool operator==(const FilePathHash &lhs, const FilePathHash &rhs) {
return std::memcmp(lhs.data, rhs.data, FilePathHashSize) == 0;
}
inline bool operator!=(const FilePathHash &lhs, const FilePathHash &rhs) {
return !(lhs == rhs);
}
}

View file

@ -18,13 +18,13 @@
namespace ams::fs {
Result FileStorage::UpdateSize() {
R_UNLESS(this->size == InvalidSize, ResultSuccess());
R_SUCCEED_IF(this->size != InvalidSize);
return this->base_file->GetSize(&this->size);
}
Result FileStorage::Read(s64 offset, void *buffer, size_t size) {
/* Immediately succeed if there's nothing to read. */
R_UNLESS(size > 0, ResultSuccess());
R_SUCCEED_IF(size == 0);
/* Validate buffer. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
@ -41,7 +41,7 @@ namespace ams::fs {
Result FileStorage::Write(s64 offset, const void *buffer, size_t size) {
/* Immediately succeed if there's nothing to write. */
R_UNLESS(size > 0, ResultSuccess());
R_SUCCEED_IF(size == 0);
/* Validate buffer. */
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
@ -86,7 +86,7 @@ namespace ams::fs {
R_UNLESS(IStorage::IsOffsetAndSizeValid(offset, size), fs::ResultOutOfRange());
return this->base_file->OperateRange(dst, dst_size, op_id, offset, size, src, src_size);
default:
return fs::ResultUnsupportedOperation();
return fs::ResultUnsupportedOperationInFileStorageA();
}
}

View file

@ -0,0 +1,87 @@
/*
* 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 {
namespace {
const char *GetGameCardMountNameSuffix(GameCardPartition which) {
switch (which) {
case GameCardPartition::Update: return impl::GameCardFileSystemMountNameUpdateSuffix;
case GameCardPartition::Normal: return impl::GameCardFileSystemMountNameNormalSuffix;
case GameCardPartition::Secure: return impl::GameCardFileSystemMountNameSecureSuffix;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
class GameCardCommonMountNameGenerator : public fsa::ICommonMountNameGenerator, public impl::Newable {
private:
const GameCardHandle handle;
const GameCardPartition partition;
public:
explicit GameCardCommonMountNameGenerator(GameCardHandle h, GameCardPartition p) : handle(h), partition(p) { /* ... */ }
virtual Result GenerateCommonMountName(char *dst, size_t dst_size) override {
/* Determine how much space we need. */
const size_t needed_size = strnlen(impl::GameCardFileSystemMountName, MountNameLengthMax) + strnlen(GetGameCardMountNameSuffix(this->partition), MountNameLengthMax) + sizeof(GameCardHandle) * 2 + 2;
AMS_ABORT_UNLESS(dst_size >= needed_size);
/* Generate the name. */
auto size = std::snprintf(dst, dst_size, "%s%s%08x:", impl::GameCardFileSystemMountName, GetGameCardMountNameSuffix(this->partition), this->handle);
AMS_ASSERT(static_cast<size_t>(size) == needed_size - 1);
return ResultSuccess();
}
};
}
Result GetGameCardHandle(GameCardHandle *out) {
/* TODO: fs::DeviceOperator */
/* Open a DeviceOperator. */
::FsDeviceOperator d;
R_TRY(fsOpenDeviceOperator(std::addressof(d)));
ON_SCOPE_EXIT { fsDeviceOperatorClose(std::addressof(d)); };
/* Get the handle. */
static_assert(sizeof(GameCardHandle) == sizeof(::FsGameCardHandle));
return fsDeviceOperatorGetGameCardHandle(std::addressof(d), reinterpret_cast<::FsGameCardHandle *>(out));
}
Result MountGameCardPartition(const char *name, GameCardHandle handle, GameCardPartition partition) {
/* Validate the mount name. */
R_TRY(impl::CheckMountNameAllowingReserved(name));
/* Open gamecard filesystem. This uses libnx bindings. */
::FsFileSystem fs;
const ::FsGameCardHandle _hnd = {handle};
R_TRY(fsOpenGameCardFileSystem(std::addressof(fs), std::addressof(_hnd), static_cast<::FsGameCardPartition>(partition)));
/* Allocate a new filesystem wrapper. */
auto fsa = std::make_unique<RemoteFileSystem>(fs);
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInGameCardC());
/* Allocate a new mountname generator. */
auto generator = std::make_unique<GameCardCommonMountNameGenerator>(handle, partition);
R_UNLESS(generator != nullptr, fs::ResultAllocationFailureInGameCardD());
/* Register. */
return fsa::Register(name, std::move(fsa), std::move(generator));
}
}

View file

@ -0,0 +1,83 @@
/*
* 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>
namespace ams::fs {
namespace {
bool g_used_default_allocator;
void *DefaultAllocate(size_t size) {
g_used_default_allocator = true;
return std::malloc(size);
}
void DefaultDeallocate(void *ptr, size_t size) {
std::free(ptr);
}
os::Mutex g_lock;
AllocateFunction g_allocate_func = DefaultAllocate;
DeallocateFunction g_deallocate_func = DefaultDeallocate;
constexpr size_t RequiredAlignment = alignof(u64);
Result SetAllocatorImpl(AllocateFunction allocator, DeallocateFunction deallocator) {
/* Ensure SetAllocator is used correctly. */
R_UNLESS(g_allocate_func == DefaultAllocate, fs::ResultAllocatorAlreadyRegistered());
R_UNLESS(g_deallocate_func == DefaultDeallocate, fs::ResultAllocatorAlreadyRegistered());
R_UNLESS(allocator != nullptr, fs::ResultNullptrArgument());
R_UNLESS(deallocator != nullptr, fs::ResultNullptrArgument());
R_UNLESS(!g_used_default_allocator, fs::ResultDefaultAllocatorUsed());
/* Set allocators. */
g_allocate_func = allocator;
g_deallocate_func = deallocator;
return ResultSuccess();
}
}
void SetAllocator(AllocateFunction allocator, DeallocateFunction deallocator) {
R_ABORT_UNLESS(SetAllocatorImpl(allocator, deallocator));
}
namespace impl {
void *Allocate(size_t size) {
void *ptr;
{
std::scoped_lock lk(g_lock);
ptr = g_allocate_func(size);
if (!util::IsAligned(reinterpret_cast<uintptr_t>(ptr), RequiredAlignment)) {
R_ABORT_UNLESS(fs::ResultAllocatorAlignmentViolation());
}
}
return ptr;
}
void Deallocate(void *ptr, size_t size) {
if (ptr == nullptr) {
return;
}
std::scoped_lock lk(g_lock);
g_deallocate_func(ptr, size);
}
}
}

View file

@ -25,7 +25,7 @@ namespace ams::fs {
const char c = *(cur++);
/* If terminated, we're done. */
R_UNLESS(c != StringTraits::NullTerminator, ResultSuccess());
R_SUCCEED_IF(PathTool::IsNullTerminator(c));
/* TODO: Nintendo converts the path from utf-8 to utf-32, one character at a time. */
/* We should do this. */

View file

@ -0,0 +1,31 @@
/*
* 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 <stratosphere/fs/fs_rights_id.hpp>
namespace ams::fs {
Result GetRightsId(RightsId *out, const char *path) {
static_assert(sizeof(RightsId) == sizeof(::FsRightsId));
return fsGetRightsIdByPath(path, reinterpret_cast<::FsRightsId *>(out));
}
Result GetRightsId(RightsId *out, u8 *out_key_generation, const char *path) {
static_assert(sizeof(RightsId) == sizeof(::FsRightsId));
return fsGetRightsIdAndKeyGenerationByPath(path, out_key_generation, reinterpret_cast<::FsRightsId *>(out));
}
}

View file

@ -0,0 +1,536 @@
/*
* 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>
namespace ams::fs {
namespace {
Result ConvertNcaCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultNcaCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultInvalidNcaFileSystemType, fs::ResultInvalidRomNcaFileSystemType())
R_CONVERT(fs::ResultInvalidAcidFileSize, fs::ResultInvalidRomAcidFileSize())
R_CONVERT(fs::ResultInvalidAcidSize, fs::ResultInvalidRomAcidSize())
R_CONVERT(fs::ResultInvalidAcid, fs::ResultInvalidRomAcid())
R_CONVERT(fs::ResultAcidVerificationFailed, fs::ResultRomAcidVerificationFailed())
R_CONVERT(fs::ResultInvalidNcaSignature, fs::ResultInvalidRomNcaSignature())
R_CONVERT(fs::ResultNcaHeaderSignature1VerificationFailed, fs::ResultRomNcaHeaderSignature1VerificationFailed())
R_CONVERT(fs::ResultNcaHeaderSignature2VerificationFailed, fs::ResultRomNcaHeaderSignature2VerificationFailed())
R_CONVERT(fs::ResultNcaFsHeaderHashVerificationFailed, fs::ResultRomNcaFsHeaderHashVerificationFailed())
R_CONVERT(fs::ResultInvalidNcaKeyIndex, fs::ResultInvalidRomNcaKeyIndex())
R_CONVERT(fs::ResultInvalidNcaFsHeaderHashType, fs::ResultInvalidRomNcaFsHeaderHashType())
R_CONVERT(fs::ResultInvalidNcaFsHeaderEncryptionType, fs::ResultInvalidRomNcaFsHeaderEncryptionType())
R_CONVERT(fs::ResultInvalidHierarchicalSha256BlockSize, fs::ResultInvalidRomHierarchicalSha256BlockSize())
R_CONVERT(fs::ResultInvalidHierarchicalSha256LayerCount, fs::ResultInvalidRomHierarchicalSha256LayerCount())
R_CONVERT(fs::ResultHierarchicalSha256BaseStorageTooLarge, fs::ResultRomHierarchicalSha256BaseStorageTooLarge())
R_CONVERT(fs::ResultHierarchicalSha256HashVerificationFailed, fs::ResultRomHierarchicalSha256HashVerificationFailed())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultNcaCorrupted();
}
Result ConvertIntegrityVerificationStorageCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultIntegrityVerificationStorageCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultIncorrectIntegrityVerificationMagic, fs::ResultIncorrectRomIntegrityVerificationMagic())
R_CONVERT(fs::ResultInvalidZeroHash, fs::ResultInvalidRomZeroHash())
R_CONVERT(fs::ResultNonRealDataVerificationFailed, fs::ResultRomNonRealDataVerificationFailed())
R_CONVERT(fs::ResultInvalidHierarchicalIntegrityVerificationLayerCount, fs::ResultInvalidRomHierarchicalIntegrityVerificationLayerCount())
R_CONVERT(fs::ResultClearedRealDataVerificationFailed, fs::ResultClearedRomRealDataVerificationFailed())
R_CONVERT(fs::ResultUnclearedRealDataVerificationFailed, fs::ResultUnclearedRomRealDataVerificationFailed())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultIntegrityVerificationStorageCorrupted();
}
Result ConvertBuiltInStorageCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultBuiltInStorageCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultGptHeaderVerificationFailed, fs::ResultRomGptHeaderVerificationFailed())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultBuiltInStorageCorrupted();
}
Result ConvertPartitionFileSystemCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultPartitionFileSystemCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultInvalidSha256PartitionHashTarget, fs::ResultInvalidRomSha256PartitionHashTarget())
R_CONVERT(fs::ResultSha256PartitionHashVerificationFailed, fs::ResultRomSha256PartitionHashVerificationFailed())
R_CONVERT(fs::ResultPartitionSignatureVerificationFailed, fs::ResultRomPartitionSignatureVerificationFailed())
R_CONVERT(fs::ResultSha256PartitionSignatureVerificationFailed, fs::ResultRomSha256PartitionSignatureVerificationFailed())
R_CONVERT(fs::ResultInvalidPartitionEntryOffset, fs::ResultInvalidRomPartitionEntryOffset())
R_CONVERT(fs::ResultInvalidSha256PartitionMetaDataSize, fs::ResultInvalidRomSha256PartitionMetaDataSize())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultPartitionFileSystemCorrupted();
}
Result ConvertFatFileSystemCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultFatFileSystemCorrupted::Includes(res));
return res;
}
Result ConvertHostFileSystemCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultHostFileSystemCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultHostEntryCorrupted, fs::ResultRomHostEntryCorrupted())
R_CONVERT(fs::ResultHostFileDataCorrupted, fs::ResultRomHostFileDataCorrupted())
R_CONVERT(fs::ResultHostFileCorrupted, fs::ResultRomHostFileCorrupted())
R_CONVERT(fs::ResultInvalidHostHandle, fs::ResultInvalidRomHostHandle())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultHostFileSystemCorrupted();
}
Result ConvertDatabaseCorruptedResult(Result res) {
AMS_ASSERT(fs::ResultDatabaseCorrupted::Includes(res));
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultInvalidAllocationTableBlock, fs::ResultInvalidRomAllocationTableBlock())
R_CONVERT(fs::ResultInvalidKeyValueListElementIndex, fs::ResultInvalidRomKeyValueListElementIndex())
R_CATCH_ALL() { /* ... */ }
} R_END_TRY_CATCH;
AMS_ASSERT(false);
return fs::ResultDatabaseCorrupted();
}
Result ConvertRomFsResult(Result res) {
R_TRY_CATCH(res) {
R_CONVERT(fs::ResultUnsupportedVersion, fs::ResultUnsupportedRomVersion())
R_CONVERT(fs::ResultNcaCorrupted, ConvertNcaCorruptedResult(res))
R_CONVERT(fs::ResultIntegrityVerificationStorageCorrupted, ConvertIntegrityVerificationStorageCorruptedResult(res))
R_CONVERT(fs::ResultBuiltInStorageCorrupted, ConvertBuiltInStorageCorruptedResult(res))
R_CONVERT(fs::ResultPartitionFileSystemCorrupted, ConvertPartitionFileSystemCorruptedResult(res))
R_CONVERT(fs::ResultFatFileSystemCorrupted, ConvertFatFileSystemCorruptedResult(res))
R_CONVERT(fs::ResultHostFileSystemCorrupted, ConvertHostFileSystemCorruptedResult(res))
R_CONVERT(fs::ResultDatabaseCorrupted, ConvertDatabaseCorruptedResult(res))
R_CONVERT(fs::ResultNotFound, fs::ResultPathNotFound())
R_CONVERT(fs::ResultPermissionDenied, fs::ResultTargetLocked())
R_CONVERT(fs::ResultIncompatiblePath, fs::ResultPathNotFound())
} R_END_TRY_CATCH;
return ResultSuccess();
}
Result ReadFile(IStorage *storage, s64 offset, void *buffer, size_t size) {
AMS_ASSERT(storage != nullptr);
AMS_ASSERT(offset >= 0);
AMS_ASSERT(buffer != nullptr || size == 0);
return ConvertRomFsResult(storage->Read(offset, buffer, size));
}
Result ReadFileHeader(IStorage *storage, RomFileSystemInformation *out) {
AMS_ASSERT(storage != nullptr);
AMS_ASSERT(out != nullptr);
return ReadFile(storage, 0, out, sizeof(*out));
}
constexpr size_t CalculateRequiredWorkingMemorySize(const RomFileSystemInformation &header) {
const size_t needed_size = header.directory_bucket_size + header.directory_entry_size + header.file_bucket_size + header.file_entry_size;
return util::AlignUp(needed_size, 8);
}
class RomFsFile : public fsa::IFile, public impl::Newable {
private:
RomFsFileSystem *parent;
s64 start;
s64 end;
public:
RomFsFile(RomFsFileSystem *p, s64 s, s64 e) : parent(p), start(s), end(e) { /* ... */ }
virtual ~RomFsFile() { /* ... */ }
Result VerifyArguments(size_t *out, s64 offset, void *buf, size_t size, const fs::ReadOption &option) {
R_TRY(DryRead(out, offset, size, option, fs::OpenMode_Read));
AMS_ASSERT(this->GetStorage() != nullptr);
AMS_ASSERT(offset >= 0);
AMS_ASSERT(buf != nullptr || size == 0);
return ResultSuccess();
}
Result ConvertResult(Result res) const {
return ConvertRomFsResult(res);
}
s64 GetOffset() const {
return this->start;
}
s64 GetSize() const {
return this->end - this->start;
}
IStorage *GetStorage() {
return this->parent->GetBaseStorage();
}
public:
virtual Result ReadImpl(size_t *out, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) override {
size_t read_size = 0;
R_TRY(this->VerifyArguments(std::addressof(read_size), offset, buffer, size, option));
R_TRY(this->ConvertResult(this->GetStorage()->Read(offset + this->start, buffer, size)));
*out = read_size;
return ResultSuccess();
}
virtual Result GetSizeImpl(s64 *out) override {
*out = this->GetSize();
return ResultSuccess();
}
virtual Result FlushImpl() override {
return ResultSuccess();
}
virtual Result WriteImpl(s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) override {
return fs::ResultUnsupportedOperationInRomFsFileA();
}
virtual Result SetSizeImpl(s64 size) override {
return fs::ResultUnsupportedOperationInRomFsFileA();
}
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 {
switch (op_id) {
case OperationId::InvalidateCache:
case OperationId::QueryRange:
{
R_UNLESS(offset >= 0, fs::ResultOutOfRange());
R_UNLESS(this->GetSize() >= 0, fs::ResultOutOfRange());
}
default:
return fs::ResultUnsupportedOperationInRomFsFileB();
}
}
public:
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
AMS_ABORT();
}
};
class RomFsDirectory : public fsa::IDirectory, public impl::Newable {
private:
using FindPosition = RomFsFileSystem::RomFileTable::FindPosition;
private:
RomFsFileSystem *parent;
FindPosition current_find;
FindPosition first_find;
fs::OpenDirectoryMode mode;
public:
RomFsDirectory(RomFsFileSystem *p, const FindPosition &f, fs::OpenDirectoryMode m) : parent(p), current_find(f), first_find(f), mode(m) { /* ... */ }
virtual ~RomFsDirectory() override { /* ... */ }
public:
virtual Result ReadImpl(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) {
return this->ReadImpl(out_count, std::addressof(this->current_find), out_entries, max_entries);
}
virtual Result GetEntryCountImpl(s64 *out) {
FindPosition find = this->first_find;
return this->ReadImpl(out, std::addressof(find), nullptr, 0);
}
private:
Result ReadImpl(s64 *out_count, FindPosition *find, DirectoryEntry *out_entries, s64 max_entries) {
AMS_ASSERT(out_count != nullptr);
AMS_ASSERT(find != nullptr);
constexpr size_t NameBufferSize = fs::EntryNameLengthMax + 1;
char *name_buf = static_cast<char *>(::ams::fs::impl::Allocate(NameBufferSize));
R_UNLESS(name_buf != nullptr, fs::ResultAllocationFailureInRomFsFileSystemE());
ON_SCOPE_EXIT { ::ams::fs::impl::Deallocate(name_buf, NameBufferSize); };
s32 i = 0;
if (this->mode & fs::OpenDirectoryMode_Directory) {
while (i < max_entries || out_entries == nullptr) {
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextDirectory(name_buf, find, NameBufferSize)) {
R_CATCH(fs::ResultDbmFindFinished) { break; }
} R_END_TRY_CATCH;
if (out_entries) {
R_UNLESS(strnlen(name_buf, NameBufferSize) < NameBufferSize, fs::ResultTooLongPath());
strncpy(out_entries[i].name, name_buf, fs::EntryNameLengthMax);
out_entries[i].name[fs::EntryNameLengthMax] = '\x00';
out_entries[i].type = fs::DirectoryEntryType_Directory;
out_entries[i].file_size = 0;
}
i++;
}
}
if (this->mode & fs::OpenDirectoryMode_File) {
while (i < max_entries || out_entries == nullptr) {
auto file_pos = find->next_file;
R_TRY_CATCH(this->parent->GetRomFileTable()->FindNextFile(name_buf, find, NameBufferSize)) {
R_CATCH(fs::ResultDbmFindFinished) { break; }
} R_END_TRY_CATCH;
if (out_entries) {
R_UNLESS(strnlen(name_buf, NameBufferSize) < NameBufferSize, fs::ResultTooLongPath());
strncpy(out_entries[i].name, name_buf, fs::EntryNameLengthMax);
out_entries[i].name[fs::EntryNameLengthMax] = '\x00';
out_entries[i].type = fs::DirectoryEntryType_File;
RomFsFileSystem::RomFileTable::FileInfo file_info;
R_TRY(this->parent->GetRomFileTable()->OpenFile(std::addressof(file_info), this->parent->GetRomFileTable()->ConvertToFileId(file_pos)));
out_entries[i].file_size = file_info.size.Get();
}
i++;
}
}
*out_count = i;
return ResultSuccess();
}
public:
virtual sf::cmif::DomainObjectId GetDomainObjectId() const override {
AMS_ABORT();
}
};
}
RomFsFileSystem::RomFsFileSystem() : base_storage() {
/* ... */
}
RomFsFileSystem::~RomFsFileSystem() {
/* ... */
}
Result RomFsFileSystem::GetRequiredWorkingMemorySize(size_t *out, IStorage *storage) {
RomFileSystemInformation header;
R_TRY(ReadFileHeader(storage, std::addressof(header)));
*out = CalculateRequiredWorkingMemorySize(header);
return ResultSuccess();
}
Result RomFsFileSystem::Initialize(IStorage *base, void *work, size_t work_size, bool use_cache) {
AMS_ABORT_UNLESS(!use_cache || work != nullptr);
AMS_ABORT_UNLESS(base != nullptr);
/* Read the header. */
RomFileSystemInformation header;
R_TRY(ReadFileHeader(base, std::addressof(header)));
/* Set up our storages. */
if (use_cache) {
const size_t needed_size = CalculateRequiredWorkingMemorySize(header);
R_UNLESS(work_size >= needed_size, fs::ResultPreconditionViolation());
u8 *buf = static_cast<u8 *>(work);
auto dir_bucket_buf = buf; buf += header.directory_bucket_size;
auto dir_entry_buf = buf; buf += header.directory_entry_size;
auto file_bucket_buf = buf; buf += header.file_bucket_size;
auto file_entry_buf = buf; buf += header.file_entry_size;
R_TRY(ReadFile(base, header.directory_bucket_offset, dir_bucket_buf, header.directory_bucket_size));
R_TRY(ReadFile(base, header.directory_entry_offset, dir_entry_buf, header.directory_entry_size));
R_TRY(ReadFile(base, header.file_bucket_offset, file_bucket_buf, header.file_bucket_size));
R_TRY(ReadFile(base, header.file_entry_offset, file_entry_buf, header.file_entry_size));
this->dir_bucket_storage.reset(new MemoryStorage(dir_bucket_buf, header.directory_bucket_size));
this->dir_entry_storage.reset(new MemoryStorage(dir_entry_buf, header.directory_entry_size));
this->file_bucket_storage.reset(new MemoryStorage(file_bucket_buf, header.file_bucket_size));
this->file_entry_storage.reset(new MemoryStorage(file_entry_buf, header.file_entry_size));
} else {
this->dir_bucket_storage.reset(new SubStorage(base, header.directory_bucket_offset, header.directory_bucket_size));
this->dir_entry_storage.reset(new SubStorage(base, header.directory_entry_offset, header.directory_entry_size));
this->file_bucket_storage.reset(new SubStorage(base, header.file_bucket_offset, header.file_bucket_size));
this->file_entry_storage.reset(new SubStorage(base, header.file_entry_offset, header.file_entry_size));
}
/* Ensure we allocated storages successfully. */
R_UNLESS(this->dir_bucket_storage != nullptr, fs::ResultAllocationFailureInRomFsFileSystemA());
R_UNLESS(this->dir_entry_storage != nullptr, fs::ResultAllocationFailureInRomFsFileSystemA());
R_UNLESS(this->file_bucket_storage != nullptr, fs::ResultAllocationFailureInRomFsFileSystemA());
R_UNLESS(this->file_entry_storage != nullptr, fs::ResultAllocationFailureInRomFsFileSystemA());
/* Initialize the rom table. */
{
SubStorage db(this->dir_bucket_storage.get(), 0, header.directory_bucket_size);
SubStorage de(this->dir_entry_storage.get(), 0, header.directory_entry_size);
SubStorage fb(this->file_bucket_storage.get(), 0, header.file_bucket_size);
SubStorage fe(this->file_entry_storage.get(), 0, header.file_entry_size);
R_TRY(this->rom_file_table.Initialize(db, de, fb, fe));
}
/* Set members. */
this->entry_size = header.body_offset;
this->base_storage = base;
return ResultSuccess();
}
Result RomFsFileSystem::Initialize(std::unique_ptr<IStorage>&& base, void *work, size_t work_size, bool use_cache) {
this->unique_storage = std::move(base);
return this->Initialize(this->unique_storage.get(), work, work_size, use_cache);
}
Result RomFsFileSystem::GetFileInfo(RomFileTable::FileInfo *out, const char *path) {
R_TRY_CATCH(this->rom_file_table.OpenFile(out, path)) {
R_CONVERT(fs::ResultDbmNotFound, fs::ResultPathNotFound());
R_CONVERT(fs::ResultDbmInvalidOperation, fs::ResultPathNotFound());
} R_END_TRY_CATCH;
return ResultSuccess();
}
IStorage *RomFsFileSystem::GetBaseStorage() {
return this->base_storage;
}
RomFsFileSystem::RomFileTable *RomFsFileSystem::GetRomFileTable() {
return std::addressof(this->rom_file_table);
}
Result RomFsFileSystem::GetFileBaseOffset(s64 *out, const char *path) {
AMS_ABORT_UNLESS(out != nullptr);
AMS_ABORT_UNLESS(path != nullptr);
RomFileTable::FileInfo info;
R_TRY(this->GetFileInfo(std::addressof(info), path));
*out = this->entry_size + info.offset.Get();
return ResultSuccess();
}
Result RomFsFileSystem::CreateFileImpl(const char *path, s64 size, int flags) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::DeleteFileImpl(const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::CreateDirectoryImpl(const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::DeleteDirectoryImpl(const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::DeleteDirectoryRecursivelyImpl(const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::RenameFileImpl(const char *old_path, const char *new_path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::RenameDirectoryImpl(const char *old_path, const char *new_path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::GetEntryTypeImpl(fs::DirectoryEntryType *out, const char *path) {
RomDirectoryInfo dir_info;
R_TRY_CATCH(this->rom_file_table.GetDirectoryInformation(std::addressof(dir_info), path)) {
R_CONVERT(fs::ResultDbmNotFound, fs::ResultPathNotFound())
R_CATCH(fs::ResultDbmInvalidOperation) {
RomFileTable::FileInfo file_info;
R_TRY(this->GetFileInfo(std::addressof(file_info), path));
*out = fs::DirectoryEntryType_File;
return ResultSuccess();
}
} R_END_TRY_CATCH;
*out = fs::DirectoryEntryType_Directory;
return ResultSuccess();
}
Result RomFsFileSystem::OpenFileImpl(std::unique_ptr<fs::fsa::IFile> *out_file, const char *path, fs::OpenMode mode) {
AMS_ASSERT(out_file != nullptr);
AMS_ASSERT(path != nullptr);
R_UNLESS((mode & fs::OpenMode_All) == fs::OpenMode_Read, fs::ResultInvalidArgument());
RomFileTable::FileInfo file_info;
R_TRY(this->GetFileInfo(std::addressof(file_info), path));
auto file = std::make_unique<RomFsFile>(this, this->entry_size + file_info.offset.Get(), this->entry_size + file_info.offset.Get() + file_info.size.Get());
R_UNLESS(file != nullptr, fs::ResultAllocationFailureInRomFsFileSystemB());
*out_file = std::move(file);
return ResultSuccess();
}
Result RomFsFileSystem::OpenDirectoryImpl(std::unique_ptr<fs::fsa::IDirectory> *out_dir, const char *path, fs::OpenDirectoryMode mode) {
AMS_ASSERT(out_dir != nullptr);
AMS_ASSERT(path != nullptr);
RomFileTable::FindPosition find;
R_TRY_CATCH(this->rom_file_table.FindOpen(std::addressof(find), path)) {
R_CONVERT(fs::ResultDbmNotFound, fs::ResultPathNotFound())
R_CONVERT(fs::ResultDbmInvalidOperation, fs::ResultPathNotFound())
} R_END_TRY_CATCH;
auto dir = std::make_unique<RomFsDirectory>(this, find, mode);
R_UNLESS(dir != nullptr, fs::ResultAllocationFailureInRomFsFileSystemC());
*out_dir = std::move(dir);
return ResultSuccess();
}
Result RomFsFileSystem::CommitImpl() {
return ResultSuccess();
}
Result RomFsFileSystem::GetFreeSpaceSizeImpl(s64 *out, const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemC();
}
Result RomFsFileSystem::GetTotalSpaceSizeImpl(s64 *out, const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemC();
}
Result RomFsFileSystem::CleanDirectoryRecursivelyImpl(const char *path) {
return fs::ResultUnsupportedOperationInRomFsFileSystemA();
}
Result RomFsFileSystem::CommitProvisionallyImpl(s64 counter) {
return fs::ResultUnsupportedOperationInRomFsFileSystemB();
}
Result RomFsFileSystem::RollbackImpl() {
return ResultSuccess();
}
}

View file

@ -0,0 +1,119 @@
/*
* 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 {
namespace impl {
Result ReadSaveDataFileSystemExtraData(SaveDataExtraData *out, SaveDataId id) {
return fsReadSaveDataFileSystemExtraData(out, sizeof(*out), id);
}
Result ReadSaveDataFileSystemExtraData(SaveDataExtraData *out, SaveDataSpaceId space_id, SaveDataId id) {
return fsReadSaveDataFileSystemExtraDataBySaveDataSpaceId(out, sizeof(*out), static_cast<::FsSaveDataSpaceId>(space_id), id);
}
Result WriteSaveDataFileSystemExtraData(SaveDataSpaceId space_id, SaveDataId id, const SaveDataExtraData &extra_data) {
return fsWriteSaveDataFileSystemExtraData(std::addressof(extra_data), sizeof(extra_data), static_cast<::FsSaveDataSpaceId>(space_id), id);
}
}
void DisableAutoSaveDataCreation() {
/* Use libnx binding. */
R_ABORT_UNLESS(fsDisableAutoSaveDataCreation());
}
Result CreateSystemSaveData(SaveDataSpaceId space_id, SystemSaveDataId save_id, UserId user_id, u64 owner_id, s64 size, s64 journal_size, u32 flags) {
const auto attribute = SaveDataAttribute::Make(ncm::InvalidProgramId, SaveDataType::System, user_id, save_id);
const SaveDataCreationInfo info = {
.size = size,
.journal_size = journal_size,
.block_size = DefaultSaveDataBlockSize,
.owner_id = owner_id,
.flags = flags,
.space_id = space_id,
.pseudo = false,
};
static_assert(sizeof(SaveDataAttribute) == sizeof(::FsSaveDataAttribute));
static_assert(sizeof(SaveDataCreationInfo) == sizeof(::FsSaveDataCreationInfo));
return fsCreateSaveDataFileSystemBySystemSaveDataId(reinterpret_cast<const ::FsSaveDataAttribute *>(std::addressof(attribute)), reinterpret_cast<const ::FsSaveDataCreationInfo *>(std::addressof(info)));
}
Result CreateSystemSaveData(SystemSaveDataId save_id, s64 size, s64 journal_size, u32 flags) {
return CreateSystemSaveData(SaveDataSpaceId::System, save_id, InvalidUserId, 0, size, journal_size, flags);
}
Result CreateSystemSaveData(SystemSaveDataId save_id, u64 owner_id, s64 size, s64 journal_size, u32 flags) {
return CreateSystemSaveData(SaveDataSpaceId::System, save_id, InvalidUserId, owner_id, size, journal_size, flags);
}
Result CreateSystemSaveData(SaveDataSpaceId space_id, SystemSaveDataId save_id, u64 owner_id, s64 size, s64 journal_size, u32 flags) {
return CreateSystemSaveData(space_id, save_id, InvalidUserId, owner_id, size, journal_size, flags);
}
Result CreateSystemSaveData(SystemSaveDataId save_id, UserId user_id, s64 size, s64 journal_size, u32 flags) {
return CreateSystemSaveData(SaveDataSpaceId::System, save_id, user_id, 0, size, journal_size, flags);
}
Result CreateSystemSaveData(SystemSaveDataId save_id, UserId user_id, u64 owner_id, s64 size, s64 journal_size, u32 flags) {
return CreateSystemSaveData(SaveDataSpaceId::System, save_id, user_id, owner_id, size, journal_size, flags);
}
Result DeleteSaveData(SaveDataId id) {
/* TODO: Libnx binding for DeleteSaveDataFileSystem */
AMS_ABORT();
}
Result DeleteSaveData(SaveDataSpaceId space_id, SaveDataId id) {
return fsDeleteSaveDataFileSystemBySaveDataSpaceId(static_cast<::FsSaveDataSpaceId>(space_id), id);
}
Result DeleteSystemSaveData(SaveDataSpaceId space_id, SystemSaveDataId id, UserId user_id) {
const auto attribute = SaveDataAttribute::Make(ncm::InvalidProgramId, SaveDataType::System, user_id, id);
/* TODO: Libnx binding for DeleteSaveDataFileSystemBySaveDataAttribute */
AMS_UNUSED(attribute);
AMS_ABORT();
}
Result GetSaveDataFlags(u32 *out, SaveDataId id) {
SaveDataExtraData extra_data;
R_TRY(impl::ReadSaveDataFileSystemExtraData(std::addressof(extra_data), id));
*out = extra_data.flags;
return ResultSuccess();
}
Result GetSaveDataFlags(u32 *out, SaveDataSpaceId space_id, SaveDataId id) {
SaveDataExtraData extra_data;
R_TRY(impl::ReadSaveDataFileSystemExtraData(std::addressof(extra_data), space_id, id));
*out = extra_data.flags;
return ResultSuccess();
}
Result SetSaveDataFlags(SaveDataId id, SaveDataSpaceId space_id, u32 flags) {
SaveDataExtraData extra_data;
R_TRY(impl::ReadSaveDataFileSystemExtraData(std::addressof(extra_data), space_id, id));
extra_data.flags = flags;
return impl::WriteSaveDataFileSystemExtraData(space_id, id, extra_data);
}
}

View file

@ -0,0 +1,60 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs {
template<typename T>
class ScopedSetter {
NON_COPYABLE(ScopedSetter);
private:
T *ptr;
T value;
public:
constexpr ALWAYS_INLINE ScopedSetter(T &p, T v) : ptr(std::addressof(p)), value(v) { /* ... */ }
ALWAYS_INLINE ~ScopedSetter() {
if (this->ptr) {
*this->ptr = this->value;
}
}
ALWAYS_INLINE ScopedSetter(ScopedSetter &&rhs) {
this->ptr = rhs.ptr;
this->value = rhs.value;
rhs.Reset();
}
ALWAYS_INLINE ScopedSetter &operator=(ScopedSetter &&rhs) {
this->ptr = rhs.ptr;
this->value = rhs.value;
rhs.Reset();
return *this;
}
ALWAYS_INLINE void Set(T v) { this->value = v; }
private:
ALWAYS_INLINE void Reset() {
this->ptr = nullptr;
}
};
template<typename T>
ALWAYS_INLINE ScopedSetter<T> MakeScopedSetter(T &p, T v) {
return ScopedSetter<T>(p, v);
}
}

View file

@ -0,0 +1,37 @@
/*
* 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 MountSdCard(const char *name) {
/* Validate the mount name. */
R_TRY(impl::CheckMountName(name));
/* 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::ResultAllocationFailureInSdCardA());
/* Register. */
return fsa::Register(name, std::move(fsa));
}
}

View file

@ -0,0 +1,48 @@
/*
* 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_filesystem_accessor.hpp"
#include "fsa/fs_mount_utils.hpp"
namespace ams::fs {
bool IsSignedSystemPartitionOnSdCardValid(const char *system_root_path) {
/* Get the accessor for the system filesystem. */
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_ABORT_UNLESS(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), system_root_path));
char is_valid;
R_TRY_CATCH(accessor->QueryEntry(std::addressof(is_valid), 1, nullptr, 0, fsa::QueryId::IsSignedSystemPartitionOnSdCardValid, "/")) {
/* If querying isn't supported, then the partition isn't valid. */
R_CATCH(fs::ResultUnsupportedOperation) { is_valid = false; }
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
return is_valid;
}
bool IsSignedSystemPartitionOnSdCardValidDeprecated() {
/* Ensure we only call with correct version. */
auto version = hos::GetVersion();
AMS_ABORT_UNLESS(hos::Version_400 <= version && version < hos::Version_800);
/* Check that the partition is valid. */
bool is_valid;
R_ABORT_UNLESS(fsIsSignedSystemPartitionOnSdCardValid(std::addressof(is_valid)));
return is_valid;
}
}

View file

@ -0,0 +1,33 @@
/*
* 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 QueryMountSystemDataCacheSize(size_t *out, ncm::SystemDataId data_id) {
return impl::QueryMountDataCacheSize(out, data_id, ncm::StorageId::BuiltInSystem);
}
Result MountSystemData(const char *name, ncm::SystemDataId data_id) {
return impl::MountData(name, data_id, ncm::StorageId::BuiltInSystem);
}
Result MountSystemData(const char *name, ncm::SystemDataId data_id, void *cache_buffer, size_t cache_size) {
return impl::MountData(name, data_id, ncm::StorageId::BuiltInSystem, cache_buffer, cache_size);
}
}

View file

@ -0,0 +1,61 @@
/*
* 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 {
namespace {
Result MountSystemSaveDataImpl(const char *name, SaveDataSpaceId space_id, SystemSaveDataId id, UserId user_id, SaveDataType type) {
/* Validate the mount name. */
R_TRY(impl::CheckMountName(name));
/* Create the attribute. */
const auto attribute = SaveDataAttribute::Make(ncm::InvalidProgramId, type, user_id, id);
static_assert(sizeof(attribute) == sizeof(::FsSaveDataAttribute));
/* Open the filesystem, use libnx bindings. */
::FsFileSystem fs;
R_TRY(fsOpenSaveDataFileSystemBySystemSaveDataId(std::addressof(fs), static_cast<::FsSaveDataSpaceId>(space_id), reinterpret_cast<const ::FsSaveDataAttribute *>(std::addressof(attribute))));
/* Allocate a new filesystem wrapper. */
auto fsa = std::make_unique<RemoteFileSystem>(fs);
R_UNLESS(fsa != nullptr, fs::ResultAllocationFailureInSystemSaveDataA());
/* Register. */
return fsa::Register(name, std::move(fsa));
}
}
Result MountSystemSaveData(const char *name, SystemSaveDataId id) {
return MountSystemSaveData(name, id, InvalidUserId);
}
Result MountSystemSaveData(const char *name, SaveDataSpaceId space_id, SystemSaveDataId id) {
return MountSystemSaveData(name, space_id, id, InvalidUserId);
}
Result MountSystemSaveData(const char *name, SystemSaveDataId id, UserId user_id) {
return MountSystemSaveData(name, SaveDataSpaceId::System, id, user_id);
}
Result MountSystemSaveData(const char *name, SaveDataSpaceId space_id, SystemSaveDataId id, UserId user_id) {
return MountSystemSaveDataImpl(name, space_id, id, user_id, SaveDataType::System);
}
}

View file

@ -0,0 +1,39 @@
/*
* 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 "fs_directory_accessor.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs::impl {
DirectoryAccessor::DirectoryAccessor(std::unique_ptr<fsa::IDirectory>&& d, FileSystemAccessor &p) : impl(std::move(d)), parent(p) {
/* ... */
}
DirectoryAccessor::~DirectoryAccessor() {
this->impl.reset();
this->parent.NotifyCloseDirectory(this);
}
Result DirectoryAccessor::Read(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries) {
return this->impl->Read(out_count, out_entries, max_entries);
}
Result DirectoryAccessor::GetEntryCount(s64 *out) {
return this->impl->GetEntryCount(out);
}
}

View file

@ -0,0 +1,38 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs::impl {
class FileSystemAccessor;
class DirectoryAccessor : public util::IntrusiveListBaseNode<DirectoryAccessor>, public Newable {
NON_COPYABLE(DirectoryAccessor);
private:
std::unique_ptr<fsa::IDirectory> impl;
FileSystemAccessor &parent;
public:
DirectoryAccessor(std::unique_ptr<fsa::IDirectory>&& d, FileSystemAccessor &p);
~DirectoryAccessor();
Result Read(s64 *out_count, DirectoryEntry *out_entries, s64 max_entries);
Result GetEntryCount(s64 *out);
FileSystemAccessor &GetParent() const { return this->parent; }
};
}

View file

@ -0,0 +1,123 @@
/*
* 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 "../fs_scoped_setter.hpp"
#include "../fs_file_path_hash.hpp"
#include "fs_file_accessor.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs::impl {
FileAccessor::FileAccessor(std::unique_ptr<fsa::IFile>&& f, FileSystemAccessor *p, OpenMode mode)
: impl(std::move(f)), parent(p), write_state(WriteState::None), write_result(ResultSuccess()), open_mode(mode)
{
/* ... */
}
FileAccessor::~FileAccessor() {
/* Ensure that all files are flushed. */
if (R_FAILED(this->write_result)) {
AMS_ABORT_UNLESS(this->write_state != WriteState::NeedsFlush);
}
this->impl.reset();
if (this->parent != nullptr) {
this->parent->NotifyCloseFile(this);
}
}
Result FileAccessor::ReadWithCacheAccessLog(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option, bool use_path_cache, bool use_data_cache) {
AMS_ABORT();
}
Result FileAccessor::ReadWithoutCacheAccessLog(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option) {
return this->impl->Read(out, offset, buf, size, option);
}
Result FileAccessor::Read(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option) {
/* Fail after a write fails. */
R_TRY(this->write_result);
/* TODO: Logging. */
/* TODO: Support cache. */
const bool use_path_cache = this->parent != nullptr && this->file_path_hash != nullptr;
const bool use_data_cache = /* TODO */false && this->parent != nullptr && this->parent->IsFileDataCacheAttachable();
if (use_path_cache && use_data_cache && false) {
/* TODO */
return this->ReadWithCacheAccessLog(out, offset, buf, size, option, use_path_cache, use_data_cache);
} else {
return this->ReadWithoutCacheAccessLog(out, offset, buf, size, option);
}
}
Result FileAccessor::Write(s64 offset, const void *buf, size_t size, const WriteOption &option) {
/* Fail after a write fails. */
R_TRY(this->write_result);
auto setter = MakeScopedSetter(this->write_state, WriteState::Failed);
if (this->file_path_hash != nullptr && /* TODO */ false) {
/* TODO */
AMS_ABORT();
} else {
R_TRY(this->UpdateLastResult(this->impl->Write(offset, buf, size, option)));
}
setter.Set(option.HasFlushFlag() ? WriteState::None : WriteState::NeedsFlush);
return ResultSuccess();
}
Result FileAccessor::Flush() {
/* Fail after a write fails. */
R_TRY(this->write_result);
auto setter = MakeScopedSetter(this->write_state, WriteState::Failed);
R_TRY(this->UpdateLastResult(this->impl->Flush()));
setter.Set(WriteState::None);
return ResultSuccess();
}
Result FileAccessor::SetSize(s64 size) {
/* Fail after a write fails. */
R_TRY(this->write_result);
const WriteState old_write_state = this->write_state;
auto setter = MakeScopedSetter(this->write_state, WriteState::Failed);
R_TRY(this->UpdateLastResult(this->impl->SetSize(size)));
if (this->file_path_hash != nullptr) {
/* TODO: invalidate path cache */
}
setter.Set(old_write_state);
return ResultSuccess();
}
Result FileAccessor::GetSize(s64 *out) {
/* Fail after a write fails. */
R_TRY(this->write_result);
return this->impl->GetSize(out);
}
Result FileAccessor::OperateRange(void *dst, size_t dst_size, OperationId operation, s64 offset, s64 size, const void *src, size_t src_size) {
return this->impl->OperateRange(dst, dst_size, operation, offset, size, src, src_size);
}
}

View file

@ -0,0 +1,68 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs::impl {
struct FilePathHash;
class FileSystemAccessor;
enum class WriteState {
None,
NeedsFlush,
Failed,
};
class FileAccessor : public util::IntrusiveListBaseNode<FileAccessor>, public Newable {
NON_COPYABLE(FileAccessor);
private:
std::unique_ptr<fsa::IFile> impl;
FileSystemAccessor * const parent;
WriteState write_state;
Result write_result;
const OpenMode open_mode;
std::unique_ptr<FilePathHash> file_path_hash;
s32 path_hash_index;
public:
FileAccessor(std::unique_ptr<fsa::IFile>&& f, FileSystemAccessor *p, OpenMode mode);
~FileAccessor();
Result Read(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option);
Result Write(s64 offset, const void *buf, size_t size, const WriteOption &option);
Result Flush();
Result SetSize(s64 size);
Result GetSize(s64 *out);
Result OperateRange(void *dst, size_t dst_size, OperationId operation, s64 offset, s64 size, const void *src, size_t src_size);
OpenMode GetOpenMode() const { return this->open_mode; }
WriteState GetWriteState() const { return this->write_state; }
FileSystemAccessor *GetParent() const { return this->parent; }
void SetFilePathHash(std::unique_ptr<FilePathHash>&& file_path_hash, s32 index);
Result ReadWithoutCacheAccessLog(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option);
private:
Result ReadWithCacheAccessLog(size_t *out, s64 offset, void *buf, size_t size, const ReadOption &option, bool use_path_cache, bool use_data_cache);
ALWAYS_INLINE Result UpdateLastResult(Result r) {
if (!fs::ResultNotEnoughFreeSpace::Includes(r)) {
this->write_result = r;
}
return r;
}
};
}

View file

@ -0,0 +1,243 @@
/*
* 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 "fs_file_accessor.hpp"
#include "fs_directory_accessor.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs::impl {
namespace {
template<typename List, typename Iter>
void Remove(List &list, Iter *desired) {
for (auto it = list.cbegin(); it != list.cend(); it++) {
if (it.operator->() == desired) {
list.erase(it);
return;
}
}
/* This should never happen. */
AMS_ABORT();
}
Result ValidatePath(const char *mount_name, const char *path) {
const size_t mount_name_len = strnlen(mount_name, MountNameLengthMax);
const size_t path_len = strnlen(path, EntryNameLengthMax);
R_UNLESS(mount_name_len + 1 + path_len <= EntryNameLengthMax, fs::ResultTooLongPath());
return ResultSuccess();
}
Result ValidateMountName(const char *name) {
R_UNLESS(name[0] != 0, fs::ResultInvalidMountName());
R_UNLESS(strnlen(name, sizeof(MountName)) < sizeof(MountName), fs::ResultInvalidMountName());
return ResultSuccess();
}
template<typename List>
Result ValidateNoOpenWriteModeFiles(List &list) {
for (auto it = list.cbegin(); it != list.cend(); it++) {
R_UNLESS((it->GetOpenMode() & OpenMode_Write) == 0, fs::ResultWriteModeFileNotClosed());
}
return ResultSuccess();
}
}
FileSystemAccessor::FileSystemAccessor(const char *n, std::unique_ptr<fsa::IFileSystem> &&fs, std::unique_ptr<fsa::ICommonMountNameGenerator> &&generator)
: impl(std::move(fs)), mount_name_generator(std::move(generator)),
access_log_enabled(false), data_cache_attachable(false), path_cache_attachable(false), path_cache_attached(false), multi_commit_supported(false)
{
R_ABORT_UNLESS(ValidateMountName(n));
std::strncpy(this->name.str, n, MountNameLengthMax);
this->name.str[MountNameLengthMax] = 0;
}
FileSystemAccessor::~FileSystemAccessor() {
std::scoped_lock lk(this->open_list_lock);
/* TODO: Iterate over list entries. */
if (!this->open_file_list.empty()) { R_ABORT_UNLESS(fs::ResultFileNotClosed()); }
if (!this->open_dir_list.empty()) { R_ABORT_UNLESS(fs::ResultDirectoryNotClosed()); }
if (this->path_cache_attached) {
/* TODO: Invalidate path cache */
}
}
Result FileSystemAccessor::GetCommonMountName(char *dst, size_t dst_size) const {
R_UNLESS(this->mount_name_generator != nullptr, fs::ResultPreconditionViolation());
return this->mount_name_generator->GenerateCommonMountName(dst, dst_size);
}
std::shared_ptr<fssrv::impl::FileSystemInterfaceAdapter> FileSystemAccessor::GetMultiCommitTarget() {
if (this->multi_commit_supported) {
/* TODO: Support multi commit. */
AMS_ABORT();
}
return nullptr;
}
void FileSystemAccessor::NotifyCloseFile(FileAccessor *f) {
std::scoped_lock lk(this->open_list_lock);
Remove(this->open_file_list, f);
}
void FileSystemAccessor::NotifyCloseDirectory(DirectoryAccessor *d) {
std::scoped_lock lk(this->open_list_lock);
Remove(this->open_dir_list, d);
}
Result FileSystemAccessor::CreateFile(const char *path, s64 size, int option) {
R_TRY(ValidatePath(this->name.str, path));
if (this->path_cache_attached) {
/* TODO: Path cache */
R_TRY(this->impl->CreateFile(path, size, option));
} else {
R_TRY(this->impl->CreateFile(path, size, option));
}
return ResultSuccess();
}
Result FileSystemAccessor::DeleteFile(const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->DeleteFile(path);
}
Result FileSystemAccessor::CreateDirectory(const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->CreateDirectory(path);
}
Result FileSystemAccessor::DeleteDirectory(const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->DeleteDirectory(path);
}
Result FileSystemAccessor::DeleteDirectoryRecursively(const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->DeleteDirectoryRecursively(path);
}
Result FileSystemAccessor::RenameFile(const char *old_path, const char *new_path) {
R_TRY(ValidatePath(this->name.str, old_path));
R_TRY(ValidatePath(this->name.str, new_path));
if (this->path_cache_attached) {
/* TODO: Path cache */
R_TRY(this->impl->RenameFile(old_path, new_path));
} else {
R_TRY(this->impl->RenameFile(old_path, new_path));
}
return ResultSuccess();
}
Result FileSystemAccessor::RenameDirectory(const char *old_path, const char *new_path) {
R_TRY(ValidatePath(this->name.str, old_path));
R_TRY(ValidatePath(this->name.str, new_path));
if (this->path_cache_attached) {
/* TODO: Path cache */
R_TRY(this->impl->RenameDirectory(old_path, new_path));
} else {
R_TRY(this->impl->RenameDirectory(old_path, new_path));
}
return ResultSuccess();
}
Result FileSystemAccessor::GetEntryType(DirectoryEntryType *out, const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->GetEntryType(out, path);
}
Result FileSystemAccessor::OpenFile(std::unique_ptr<FileAccessor> *out_file, const char *path, OpenMode mode) {
R_TRY(ValidatePath(this->name.str, path));
std::unique_ptr<fsa::IFile> file;
R_TRY(this->impl->OpenFile(std::addressof(file), path, mode));
auto accessor = new FileAccessor(std::move(file), this, mode);
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailureInFileSystemAccessorA());
{
std::scoped_lock lk(this->open_list_lock);
this->open_file_list.push_back(*accessor);
}
if (this->path_cache_attached) {
if (mode & OpenMode_Append) {
/* TODO: Append Path cache */
} else {
/* TODO: Non-append path cache */
}
}
out_file->reset(accessor);
return ResultSuccess();
}
Result FileSystemAccessor::OpenDirectory(std::unique_ptr<DirectoryAccessor> *out_dir, const char *path, OpenDirectoryMode mode) {
R_TRY(ValidatePath(this->name.str, path));
std::unique_ptr<fsa::IDirectory> dir;
R_TRY(this->impl->OpenDirectory(std::addressof(dir), path, mode));
auto accessor = new DirectoryAccessor(std::move(dir), *this);
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailureInFileSystemAccessorB());
{
std::scoped_lock lk(this->open_list_lock);
this->open_dir_list.push_back(*accessor);
}
out_dir->reset(accessor);
return ResultSuccess();
}
Result FileSystemAccessor::Commit() {
{
std::scoped_lock lk(this->open_list_lock);
R_ABORT_UNLESS(ValidateNoOpenWriteModeFiles(this->open_file_list));
}
return this->impl->Commit();
}
Result FileSystemAccessor::GetFreeSpaceSize(s64 *out, const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->GetFreeSpaceSize(out, path);
}
Result FileSystemAccessor::GetTotalSpaceSize(s64 *out, const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->GetTotalSpaceSize(out, path);
}
Result FileSystemAccessor::CleanDirectoryRecursively(const char *path) {
R_TRY(ValidatePath(this->name.str, path));
return this->impl->CleanDirectoryRecursively(path);
}
Result FileSystemAccessor::GetFileTimeStampRaw(FileTimeStampRaw *out, const char *path) {
return this->impl->GetFileTimeStampRaw(out, path);
}
Result FileSystemAccessor::QueryEntry(char *dst, size_t dst_size, const char *src, size_t src_size, fsa::QueryId query, const char *path) {
return this->impl->QueryEntry(dst, dst_size, src, src_size, query, path);
}
}

View file

@ -0,0 +1,94 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include <stratosphere/fssrv/fssrv_interface_adapters.hpp>
#include "fs_mount_name.hpp"
namespace ams::fs::impl {
class FileAccessor;
class DirectoryAccessor;
class FileSystemAccessor : public util::IntrusiveListBaseNode<FileSystemAccessor>, public Newable {
NON_COPYABLE(FileSystemAccessor);
friend class FileAccessor;
friend class DirectoryAccessor;
private:
using FileList = util::IntrusiveListBaseTraits<FileAccessor>::ListType;
using DirList = util::IntrusiveListBaseTraits<DirectoryAccessor>::ListType;
private:
MountName name;
std::unique_ptr<fsa::IFileSystem> impl;
FileList open_file_list;
DirList open_dir_list;
os::Mutex open_list_lock;
std::unique_ptr<fsa::ICommonMountNameGenerator> mount_name_generator;
bool access_log_enabled;
bool data_cache_attachable;
bool path_cache_attachable;
bool path_cache_attached;
bool multi_commit_supported;
public:
FileSystemAccessor(const char *name, std::unique_ptr<fsa::IFileSystem> &&fs, std::unique_ptr<fsa::ICommonMountNameGenerator> &&generator = nullptr);
virtual ~FileSystemAccessor();
Result CreateFile(const char *path, s64 size, int option);
Result DeleteFile(const char *path);
Result CreateDirectory(const char *path);
Result DeleteDirectory(const char *path);
Result DeleteDirectoryRecursively(const char *path);
Result RenameFile(const char *old_path, const char *new_path);
Result RenameDirectory(const char *old_path, const char *new_path);
Result GetEntryType(DirectoryEntryType *out, const char *path);
Result OpenFile(std::unique_ptr<FileAccessor> *out_file, const char *path, OpenMode mode);
Result OpenDirectory(std::unique_ptr<DirectoryAccessor> *out_dir, const char *path, OpenDirectoryMode mode);
Result Commit();
Result GetFreeSpaceSize(s64 *out, const char *path);
Result GetTotalSpaceSize(s64 *out, const char *path);
Result CleanDirectoryRecursively(const char *path);
Result GetFileTimeStampRaw(FileTimeStampRaw *out, const char *path);
Result QueryEntry(char *dst, size_t dst_size, const char *src, size_t src_size, fsa::QueryId query, const char *path);
const char *GetName() const { return this->name.str; }
Result GetCommonMountName(char *dst, size_t dst_size) const;
void SetAccessLogEnabled(bool en) { this->access_log_enabled = en; }
void SetFileDataCacheAttachable(bool en) { this->data_cache_attachable = en; }
void SetPathBasedFileDataCacheAttachable(bool en) { this->path_cache_attachable = en; }
void SetMultiCommitSupported(bool en) { this->multi_commit_supported = en; }
bool IsAccessLogEnabled() const { return this->access_log_enabled; }
bool IsFileDataCacheAttachable() const { return this->data_cache_attachable; }
bool IsPathBasedFileDataCacheAttachable() const { return this->path_cache_attachable; }
void AttachPathBasedFileDataCache() {
if (this->IsPathBasedFileDataCacheAttachable()) {
this->path_cache_attached = true;
}
}
void DetachPathBasedFileDataCache() {
this->path_cache_attached = false;
}
std::shared_ptr<fssrv::impl::FileSystemInterfaceAdapter> GetMultiCommitTarget();
private:
void NotifyCloseFile(FileAccessor *f);
void NotifyCloseDirectory(DirectoryAccessor *d);
};
}

View file

@ -0,0 +1,26 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs {
struct MountName {
char str[MountNameLengthMax + 1];
};
static_assert(std::is_pod<MountName>::value);
}

View file

@ -0,0 +1,75 @@
/*
* 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 "fs_mount_table.hpp"
namespace ams::fs::impl {
namespace {
bool MatchesName(const FileSystemAccessor &accessor, const char *name) {
return std::strncmp(accessor.GetName(), name, sizeof(MountName)) == 0;
}
}
bool MountTable::CanAcceptMountName(const char *name) {
for (const auto &fs : this->fs_list) {
if (MatchesName(fs, name)) {
return false;
}
}
return true;
}
Result MountTable::Mount(std::unique_ptr<FileSystemAccessor> &&fs) {
std::scoped_lock lk(this->mutex);
R_UNLESS(this->CanAcceptMountName(fs->GetName()), fs::ResultMountNameAlreadyExists());
this->fs_list.push_back(*fs.release());
return ResultSuccess();
}
Result MountTable::Find(FileSystemAccessor **out, const char *name) {
std::scoped_lock lk(this->mutex);
for (auto &fs : this->fs_list) {
if (MatchesName(fs, name)) {
*out = std::addressof(fs);
return ResultSuccess();
}
}
return fs::ResultNotMounted();
}
void MountTable::Unmount(const char *name) {
std::scoped_lock lk(this->mutex);
for (auto it = this->fs_list.cbegin(); it != this->fs_list.cend(); it++) {
if (MatchesName(*it, name)) {
auto p = std::addressof(*it);
this->fs_list.erase(it);
delete p;
return;
}
}
R_ABORT_UNLESS(fs::ResultNotMounted());
}
}

View 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "fs_filesystem_accessor.hpp"
namespace ams::fs::impl {
class MountTable : public Newable {
NON_COPYABLE(MountTable);
NON_MOVEABLE(MountTable);
private:
using FileSystemList = util::IntrusiveListBaseTraits<FileSystemAccessor>::ListType;
private:
FileSystemList fs_list;
os::Mutex mutex;
public:
constexpr MountTable() : fs_list(), mutex() { /* ... */ }
private:
bool CanAcceptMountName(const char *name);
public:
Result Mount(std::unique_ptr<FileSystemAccessor> &&fs);
Result Find(FileSystemAccessor **out, const char *name);
void Unmount(const char *name);
};
}

View file

@ -0,0 +1,168 @@
/*
* 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 "fs_filesystem_accessor.hpp"
#include "fs_mount_utils.hpp"
#include "fs_user_mount_table.hpp"
namespace ams::fs::impl {
namespace {
const char *FindMountNameDriveSeparator(const char *path) {
for (const char *cur = path; cur < path + MountNameLengthMax + 1; cur++) {
if (PathTool::IsDriveSeparator(*cur)) {
return cur;
} else if (PathTool::IsNullTerminator(*cur)) {
break;
}
}
return nullptr;
}
Result GetMountNameAndSubPath(MountName *out_mount_name, const char **out_sub_path, const char *path) {
/* Handle the Host-path case. */
if (PathTool::IsWindowsAbsolutePath(path) || PathTool::IsUnc(path)) {
std::strncpy(out_mount_name->str, HostRootFileSystemMountName, MountNameLengthMax);
out_mount_name->str[MountNameLengthMax] = '\x00';
return ResultSuccess();
}
/* Locate the drive separator. */
const char *drive_separator = FindMountNameDriveSeparator(path);
R_UNLESS(drive_separator != nullptr, fs::ResultInvalidMountName());
/* Ensure the mount name isn't too long. */
const size_t len = drive_separator - path;
R_UNLESS(len <= MountNameLengthMax, fs::ResultInvalidMountName());
/* Ensure the result sub-path is valid. */
const char *sub_path = drive_separator + 1;
R_UNLESS(!PathTool::IsNullTerminator(sub_path[0]), fs::ResultInvalidMountName());
R_UNLESS(PathTool::IsAnySeparator(sub_path[0]), fs::ResultInvalidPathFormat());
/* Set output. */
std::memcpy(out_mount_name->str, path, len);
out_mount_name->str[len] = StringTraits::NullTerminator;
*out_sub_path = sub_path;
return ResultSuccess();
}
}
bool IsValidMountName(const char *name) {
if (PathTool::IsNullTerminator(*name)) {
return false;
}
if (PathTool::IsWindowsDriveCharacter(name[0]) && PathTool::IsNullTerminator(name[1])) {
return false;
}
size_t len = 0;
for (const char *cur = name; !PathTool::IsNullTerminator(*cur); cur++) {
if (PathTool::IsDriveSeparator(*cur) || PathTool::IsSeparator(*cur)) {
return false;
}
if ((++len) > MountNameLengthMax) {
return false;
}
}
/* TODO: N validates that the mount name decodes via utf-8 here. */
return true;
}
bool IsWindowsDrive(const char *name) {
return PathTool::IsWindowsAbsolutePath(name);
}
bool IsReservedMountName(const char *name) {
return name[0] == ReservedMountNamePrefixCharacter;
}
Result CheckMountName(const char *name) {
R_TRY(CheckMountNameAllowingReserved(name));
R_UNLESS(!impl::IsReservedMountName(name), fs::ResultInvalidMountName());
return ResultSuccess();
}
Result CheckMountNameAllowingReserved(const char *name) {
R_UNLESS(name != nullptr, fs::ResultInvalidMountName());
R_UNLESS(impl::IsValidMountName(name), fs::ResultInvalidMountName());
return ResultSuccess();
}
Result FindFileSystem(FileSystemAccessor **out_accessor, const char **out_sub_path, const char *path) {
R_UNLESS(out_accessor != nullptr, fs::ResultUnexpectedInFindFileSystemA());
R_UNLESS(out_sub_path != nullptr, fs::ResultUnexpectedInFindFileSystemA());
R_UNLESS(path != nullptr, fs::ResultNullptrArgument());
R_UNLESS(strncmp(path, HostRootFileSystemMountName, strnlen(HostRootFileSystemMountName, sizeof(MountName))) != 0, fs::ResultNotMounted());
MountName mount_name;
R_TRY(GetMountNameAndSubPath(std::addressof(mount_name), out_sub_path, path));
return impl::Find(out_accessor, mount_name.str);
}
}
namespace ams::fs {
namespace {
Result UnmountImpl(const char *name) {
impl::FileSystemAccessor *accessor;
R_TRY(impl::Find(std::addressof(accessor), name));
if (accessor->IsFileDataCacheAttachable()) {
/* TODO: Data cache purge */
}
impl::Unregister(name);
return ResultSuccess();
}
}
Result ConvertToFsCommonPath(char *dst, size_t dst_size, const char *src) {
/* Ensure neither argument is nullptr. */
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
R_UNLESS(src != nullptr, fs::ResultNullptrArgument());
/* Get the mount name and sub path for the path. */
MountName mount_name;
const char *sub_path;
R_TRY(impl::GetMountNameAndSubPath(std::addressof(mount_name), std::addressof(sub_path), src));
impl::FileSystemAccessor *accessor;
R_TRY(impl::Find(std::addressof(accessor), mount_name.str));
R_TRY(accessor->GetCommonMountName(dst, dst_size));
const auto mount_name_len = strnlen(dst, dst_size);
const auto common_path_len = std::snprintf(dst + mount_name_len, dst_size - mount_name_len, "%s", sub_path);
R_UNLESS(static_cast<size_t>(common_path_len) < dst_size - mount_name_len, fs::ResultTooLongPath());
return ResultSuccess();
}
void Unmount(const char *mount_name) {
R_ABORT_UNLESS(UnmountImpl(mount_name));
}
}

View file

@ -0,0 +1,30 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs::impl {
class FileSystemAccessor;
Result FindFileSystem(FileSystemAccessor **out_accessor, const char **out_sub_path, const char *path);
bool IsWindowsDrive(const char *name);
bool IsReservedMountName(const char *name);
Result CheckMountName(const char *name);
Result CheckMountNameAllowingReserved(const char *name);
}

View file

@ -0,0 +1,51 @@
/*
* 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 "fs_filesystem_accessor.hpp"
#include "fs_user_mount_table.hpp"
namespace ams::fs::fsa {
Result Register(const char *name, std::unique_ptr<IFileSystem> &&fs) {
auto accessor = std::make_unique<impl::FileSystemAccessor>(name, std::move(fs));
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailureInRegisterA());
return impl::Register(std::move(accessor));
}
Result Register(const char *name, std::unique_ptr<IFileSystem> &&fs, std::unique_ptr<ICommonMountNameGenerator> &&generator) {
auto accessor = std::make_unique<impl::FileSystemAccessor>(name, std::move(fs), std::move(generator));
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailureInRegisterB());
return impl::Register(std::move(accessor));
}
Result Register(const char *name, std::unique_ptr<IFileSystem> &&fs, std::unique_ptr<ICommonMountNameGenerator> &&generator, bool use_data_cache, bool use_path_cache, bool support_multi_commit) {
auto accessor = std::make_unique<impl::FileSystemAccessor>(name, std::move(fs), std::move(generator));
R_UNLESS(accessor != nullptr, fs::ResultAllocationFailureInRegisterB());
accessor->SetFileDataCacheAttachable(use_data_cache);
accessor->SetPathBasedFileDataCacheAttachable(use_path_cache);
accessor->SetMultiCommitSupported(support_multi_commit);
return impl::Register(std::move(accessor));
}
void Unregister(const char *name) {
impl::Unregister(name);
}
}

View file

@ -0,0 +1,42 @@
/*
* 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 "fs_directory_accessor.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs {
namespace {
ALWAYS_INLINE impl::DirectoryAccessor *Get(DirectoryHandle handle) {
return reinterpret_cast<impl::DirectoryAccessor *>(handle.handle);
}
}
Result ReadDirectory(s64 *out_count, DirectoryEntry *out_entries, DirectoryHandle handle, s64 max_entries) {
return Get(handle)->Read(out_count, out_entries, max_entries);
}
Result GetDirectoryEntryCount(s64 *out, DirectoryHandle handle) {
return Get(handle)->GetEntryCount(out);
}
void CloseDirectory(DirectoryHandle handle) {
delete Get(handle);
}
}

View file

@ -0,0 +1,77 @@
/*
* 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 "fs_file_accessor.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs {
namespace {
ALWAYS_INLINE impl::FileAccessor *Get(FileHandle handle) {
return reinterpret_cast<impl::FileAccessor *>(handle.handle);
}
}
Result ReadFile(FileHandle handle, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) {
size_t read_size;
R_TRY(ReadFile(std::addressof(read_size), handle, offset, buffer, size, option));
R_UNLESS(read_size == size, fs::ResultOutOfRange());
return ResultSuccess();
}
Result ReadFile(FileHandle handle, s64 offset, void *buffer, size_t size) {
return ReadFile(handle, offset, buffer, size, ReadOption());
}
Result ReadFile(size_t *out, FileHandle handle, s64 offset, void *buffer, size_t size, const fs::ReadOption &option) {
return Get(handle)->Read(out, offset, buffer, size, option);
}
Result ReadFile(size_t *out, FileHandle handle, s64 offset, void *buffer, size_t size) {
return ReadFile(out, handle, offset, buffer, size, ReadOption());
}
Result GetFileSize(s64 *out, FileHandle handle) {
return Get(handle)->GetSize(out);
}
Result FlushFile(FileHandle handle) {
return Get(handle)->Flush();
}
Result WriteFile(FileHandle handle, s64 offset, const void *buffer, size_t size, const fs::WriteOption &option) {
return Get(handle)->Write(offset, buffer, size, option);
}
Result SetFileSize(FileHandle handle, s64 size) {
return Get(handle)->SetSize(size);
}
int GetFileOpenMode(FileHandle handle) {
return Get(handle)->GetOpenMode();
}
void CloseFile(FileHandle handle) {
delete Get(handle);
}
Result QueryRange(QueryRangeInfo *out, FileHandle handle, s64 offset, s64 size) {
return Get(handle)->OperateRange(out, sizeof(*out), OperationId::QueryRange, offset, size, nullptr, 0);
}
}

View file

@ -0,0 +1,199 @@
/*
* 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 "fs_filesystem_accessor.hpp"
#include "fs_file_accessor.hpp"
#include "fs_directory_accessor.hpp"
#include "fs_mount_utils.hpp"
#include "fs_user_mount_table.hpp"
namespace ams::fs {
Result CreateFile(const char *path, s64 size) {
return CreateFile(path, size, 0);
}
Result CreateFile(const char* path, s64 size, int option) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->CreateFile(sub_path, size, option);
}
Result DeleteFile(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->DeleteFile(sub_path);
}
Result CreateDirectory(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->CreateDirectory(sub_path);
}
Result DeleteDirectory(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->DeleteDirectory(sub_path);
}
Result DeleteDirectoryRecursively(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->DeleteDirectoryRecursively(sub_path);
}
Result RenameFile(const char *old_path, const char *new_path) {
impl::FileSystemAccessor *old_accessor;
impl::FileSystemAccessor *new_accessor;
const char *old_sub_path;
const char *new_sub_path;
R_TRY(impl::FindFileSystem(std::addressof(old_accessor), std::addressof(old_sub_path), old_path));
R_TRY(impl::FindFileSystem(std::addressof(new_accessor), std::addressof(new_sub_path), new_path));
R_UNLESS(old_accessor == new_accessor, fs::ResultRenameToOtherFileSystem());
return old_accessor->RenameFile(old_sub_path, new_sub_path);
}
Result RenameDirectory(const char *old_path, const char *new_path) {
impl::FileSystemAccessor *old_accessor;
impl::FileSystemAccessor *new_accessor;
const char *old_sub_path;
const char *new_sub_path;
R_TRY(impl::FindFileSystem(std::addressof(old_accessor), std::addressof(old_sub_path), old_path));
R_TRY(impl::FindFileSystem(std::addressof(new_accessor), std::addressof(new_sub_path), new_path));
R_UNLESS(old_accessor == new_accessor, fs::ResultRenameToOtherFileSystem());
return old_accessor->RenameDirectory(old_sub_path, new_sub_path);
}
Result GetEntryType(DirectoryEntryType *out, const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->GetEntryType(out, sub_path);
}
Result OpenFile(FileHandle *out_file, const char *path, int mode) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
R_UNLESS(out_file != nullptr, fs::ResultNullptrArgument());
std::unique_ptr<impl::FileAccessor> file_accessor;
R_TRY(accessor->OpenFile(std::addressof(file_accessor), sub_path, static_cast<OpenMode>(mode)));
out_file->handle = file_accessor.release();
return ResultSuccess();
}
Result OpenDirectory(DirectoryHandle *out_dir, const char *path, int mode) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
R_UNLESS(out_dir != nullptr, fs::ResultNullptrArgument());
std::unique_ptr<impl::DirectoryAccessor> dir_accessor;
R_TRY(accessor->OpenDirectory(std::addressof(dir_accessor), sub_path, static_cast<OpenDirectoryMode>(mode)));
out_dir->handle = dir_accessor.release();
return ResultSuccess();
}
Result CleanDirectoryRecursively(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->CleanDirectoryRecursively(sub_path);
}
Result GetFreeSpaceSize(s64 *out, const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->GetFreeSpaceSize(out, sub_path);
}
Result GetTotalSpaceSize(s64 *out, const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->GetTotalSpaceSize(out, sub_path);
}
Result SetConcatenationFileAttribute(const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->QueryEntry(nullptr, 0, nullptr, 0, fsa::QueryId::SetConcatenationFileAttribute, sub_path);
}
Result GetFileTimeStampRaw(FileTimeStampRaw *out, const char *path) {
impl::FileSystemAccessor *accessor;
const char *sub_path;
R_TRY(impl::FindFileSystem(std::addressof(accessor), std::addressof(sub_path), path));
return accessor->GetFileTimeStampRaw(out, sub_path);
}
Result OpenFile(FileHandle *out, std::unique_ptr<fsa::IFile> &&file, int mode) {
R_UNLESS(out != nullptr, fs::ResultNullptrArgument());
auto file_accessor = std::make_unique<impl::FileAccessor>(std::move(file), nullptr, static_cast<OpenMode>(mode));
R_UNLESS(file_accessor != nullptr, fs::ResultAllocationFailureInNew());
out->handle = file_accessor.release();
return ResultSuccess();
}
namespace {
Result CommitImpl(const char *path) {
impl::FileSystemAccessor *accessor;
R_TRY(impl::Find(std::addressof(accessor), path));
return accessor->Commit();
}
}
Result Commit(const char *path) {
return CommitImpl(path);
}
Result CommitSaveData(const char *path) {
return CommitImpl(path);
}
}

View file

@ -0,0 +1,41 @@
/*
* 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 "fs_user_mount_table.hpp"
#include "fs_mount_table.hpp"
#include "fs_filesystem_accessor.hpp"
namespace ams::fs::impl {
namespace {
MountTable g_mount_table;
}
Result Register(std::unique_ptr<FileSystemAccessor> &&fs) {
return g_mount_table.Mount(std::move(fs));
}
Result Find(FileSystemAccessor **out, const char *name) {
return g_mount_table.Find(out, name);
}
void Unregister(const char *name) {
g_mount_table.Unmount(name);
}
}

View file

@ -0,0 +1,27 @@
/*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::fs::impl {
class FileSystemAccessor;
Result Register(std::unique_ptr<FileSystemAccessor> &&fs);
Result Find(FileSystemAccessor **out, const char *name);
void Unregister(const char *name);
}