2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::fssystem {
|
|
|
|
|
|
|
|
SubDirectoryFileSystem::SubDirectoryFileSystem(std::shared_ptr<fs::fsa::IFileSystem> fs, const char *bp, bool unc)
|
|
|
|
: PathResolutionFileSystem(fs, unc)
|
|
|
|
{
|
2021-10-10 07:14:06 +00:00
|
|
|
m_base_path = nullptr;
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(this->Initialize(bp));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SubDirectoryFileSystem::SubDirectoryFileSystem(std::unique_ptr<fs::fsa::IFileSystem> fs, const char *bp, bool unc)
|
|
|
|
: PathResolutionFileSystem(std::move(fs), unc)
|
|
|
|
{
|
2021-10-10 07:14:06 +00:00
|
|
|
m_base_path = nullptr;
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(this->Initialize(bp));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SubDirectoryFileSystem::~SubDirectoryFileSystem() {
|
2021-10-10 07:14:06 +00:00
|
|
|
if (m_base_path != nullptr) {
|
|
|
|
fs::impl::Deallocate(m_base_path, m_base_path_len);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result SubDirectoryFileSystem::Initialize(const char *bp) {
|
|
|
|
/* Make sure the path isn't too long. */
|
|
|
|
R_UNLESS(strnlen(bp, fs::EntryNameLengthMax + 1) <= fs::EntryNameLengthMax, fs::ResultTooLongPath());
|
|
|
|
|
|
|
|
/* Normalize the path. */
|
|
|
|
char normalized_path[fs::EntryNameLengthMax + 2];
|
|
|
|
size_t normalized_path_len;
|
2020-12-07 03:56:45 +00:00
|
|
|
R_TRY(fs::PathNormalizer::Normalize(normalized_path, std::addressof(normalized_path_len), bp, sizeof(normalized_path), this->IsUncPreserved()));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Ensure terminating '/' */
|
2020-12-07 03:56:45 +00:00
|
|
|
if (!fs::PathNormalizer::IsSeparator(normalized_path[normalized_path_len - 1])) {
|
|
|
|
AMS_ASSERT(normalized_path_len + 2 < sizeof(normalized_path));
|
|
|
|
normalized_path[normalized_path_len] = fs::StringTraits::DirectorySeparator;
|
|
|
|
normalized_path[normalized_path_len + 1] = fs::StringTraits::NullTerminator;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-12-07 03:56:45 +00:00
|
|
|
++normalized_path_len;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate new path. */
|
2021-10-10 07:14:06 +00:00
|
|
|
m_base_path_len = normalized_path_len + 1;
|
|
|
|
m_base_path = static_cast<char *>(fs::impl::Allocate(m_base_path_len));
|
|
|
|
R_UNLESS(m_base_path != nullptr, fs::ResultAllocationFailureInSubDirectoryFileSystem());
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Copy path in. */
|
2021-10-10 07:14:06 +00:00
|
|
|
std::memcpy(m_base_path, normalized_path, normalized_path_len);
|
|
|
|
m_base_path[normalized_path_len] = fs::StringTraits::NullTerminator;
|
2019-12-09 11:57:37 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result SubDirectoryFileSystem::ResolveFullPath(char *out, size_t out_size, const char *relative_path) {
|
|
|
|
/* Ensure path will fit. */
|
2021-10-10 07:14:06 +00:00
|
|
|
R_UNLESS(m_base_path_len + strnlen(relative_path, fs::EntryNameLengthMax + 1) <= out_size, fs::ResultTooLongPath());
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Copy base path. */
|
2021-10-10 07:14:06 +00:00
|
|
|
std::memcpy(out, m_base_path, m_base_path_len);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Normalize it. */
|
2021-10-10 07:14:06 +00:00
|
|
|
const size_t prefix_size = m_base_path_len - 2;
|
2019-12-09 11:57:37 +00:00
|
|
|
size_t normalized_len;
|
2020-12-07 03:56:45 +00:00
|
|
|
return fs::PathNormalizer::Normalize(out + prefix_size, std::addressof(normalized_len), relative_path, out_size - prefix_size, this->IsUncPreserved(), false);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|