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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2021-10-01 02:21:08 +00:00
|
|
|
#include <stratosphere/fs/fs_common.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::fssrv {
|
|
|
|
|
|
|
|
/* This is in fssrv::detail in official code. */
|
|
|
|
/* TODO: Consider moving to ::impl? */
|
|
|
|
|
|
|
|
class PathNormalizer {
|
|
|
|
public:
|
|
|
|
enum Option : u32 {
|
|
|
|
Option_None = BIT(0),
|
|
|
|
Option_PreserveUnc = BIT(1),
|
|
|
|
Option_PreserveTailSeparator = BIT(2),
|
|
|
|
Option_HasMountName = BIT(3),
|
|
|
|
Option_AcceptEmpty = BIT(4),
|
|
|
|
};
|
|
|
|
private:
|
2020-12-07 03:56:45 +00:00
|
|
|
using Buffer = std::unique_ptr<char[], fs::impl::Deleter>;
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
Buffer m_buffer;
|
|
|
|
const char *m_path;
|
|
|
|
Result m_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
|
|
|
static Result Normalize(const char **out_path, Buffer *out_buf, const char *path, bool preserve_unc, bool preserve_tail_sep, bool has_mount_name);
|
|
|
|
public:
|
2020-12-07 03:56:45 +00:00
|
|
|
/* TODO: Remove non-option constructor. */
|
2021-10-10 07:14:06 +00:00
|
|
|
explicit PathNormalizer(const char *p) : m_buffer(), m_path(nullptr), m_result(ResultSuccess()) {
|
|
|
|
m_result = Normalize(std::addressof(m_path), std::addressof(m_buffer), p, false, false, false);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
PathNormalizer(const char *p, u32 option) : m_buffer(), m_path(nullptr), m_result(ResultSuccess()) {
|
2019-12-09 11:57:37 +00:00
|
|
|
if ((option & Option_AcceptEmpty) && p[0] == '\x00') {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_path = p;
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
|
|
|
const bool preserve_unc = (option & Option_PreserveUnc);
|
|
|
|
const bool preserve_tail_sep = (option & Option_PreserveTailSeparator);
|
|
|
|
const bool has_mount_name = (option & Option_HasMountName);
|
2021-10-10 07:14:06 +00:00
|
|
|
m_result = Normalize(std::addressof(m_path), std::addressof(m_buffer), p, preserve_unc, preserve_tail_sep, has_mount_name);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:56:45 +00:00
|
|
|
Result GetResult() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 03:56:45 +00:00
|
|
|
const char *GetPath() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_path;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|