1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-24 04:42:11 +00:00
Atmosphere/libraries/libstratosphere/source/fssystem/fssystem_subdirectory_filesystem.cpp

83 lines
3.4 KiB
C++
Raw Normal View History

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