fs: update + consolidate path normalization logic

This commit is contained in:
Michael Scire 2020-12-06 19:56:45 -08:00
parent 5ef93778f6
commit 32803d9920
22 changed files with 1007 additions and 463 deletions

View file

@ -40,7 +40,6 @@
#include <stratosphere/fs/fs_speed_emulation.hpp>
#include <stratosphere/fs/impl/fs_common_mount_name.hpp>
#include <stratosphere/fs/fs_mount.hpp>
#include <stratosphere/fs/fs_path_tool.hpp>
#include <stratosphere/fs/fs_path_utils.hpp>
#include <stratosphere/fs/fs_filesystem_utils.hpp>
#include <stratosphere/fs/fs_romfs_filesystem.hpp>

View file

@ -1,86 +0,0 @@
/*
* 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/fs/fs_common.hpp>
#include <stratosphere/fssrv/sf/fssrv_sf_path.hpp>
namespace ams::fs {
namespace StringTraits {
constexpr inline char DirectorySeparator = '/';
constexpr inline char DriveSeparator = ':';
constexpr inline char Dot = '.';
constexpr inline char NullTerminator = '\x00';
constexpr inline char AlternateDirectorySeparator = '\\';
}
class PathTool {
public:
static constexpr const char RootPath[] = "/";
public:
static constexpr inline bool IsAlternateSeparator(char c) {
return c == StringTraits::AlternateDirectorySeparator;
}
static constexpr inline bool IsSeparator(char c) {
return c == StringTraits::DirectorySeparator;
}
static constexpr inline bool IsAnySeparator(char c) {
return IsSeparator(c) || IsAlternateSeparator(c);
}
static constexpr inline bool IsNullTerminator(char c) {
return c == StringTraits::NullTerminator;
}
static constexpr inline bool IsDot(char c) {
return c == StringTraits::Dot;
}
static constexpr inline bool IsWindowsDriveCharacter(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
static constexpr inline bool IsDriveSeparator(char c) {
return c == StringTraits::DriveSeparator;
}
static constexpr inline bool IsWindowsAbsolutePath(const char *p) {
return IsWindowsDriveCharacter(p[0]) && IsDriveSeparator(p[1]);
}
static constexpr inline bool IsUnc(const char *p) {
return (IsSeparator(p[0]) && IsSeparator(p[1])) || (IsAlternateSeparator(p[0]) && IsAlternateSeparator(p[1]));
}
static constexpr inline bool IsCurrentDirectory(const char *p) {
return IsDot(p[0]) && (IsSeparator(p[1]) || IsNullTerminator(p[1]));
}
static constexpr inline bool IsParentDirectory(const char *p) {
return IsDot(p[0]) && IsDot(p[1]) && (IsSeparator(p[2]) || IsNullTerminator(p[2]));
}
static Result Normalize(char *out, size_t *out_len, const char *src, size_t max_out_size, bool unc_preserved = false);
static Result IsNormalized(bool *out, const char *path);
static bool IsSubPath(const char *lhs, const char *rhs);
};
}

View file

@ -19,15 +19,59 @@
namespace ams::fs {
namespace StringTraits {
constexpr inline char DirectorySeparator = '/';
constexpr inline char DriveSeparator = ':';
constexpr inline char Dot = '.';
constexpr inline char NullTerminator = '\x00';
constexpr inline char AlternateDirectorySeparator = '\\';
}
/* Windows path utilities. */
constexpr inline bool IsWindowsDrive(const char *path) {
AMS_ASSERT(path != nullptr);
const char c = path[0];
return (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) && path[1] == StringTraits::DriveSeparator;
}
constexpr inline bool IsUnc(const char *path) {
return (path[0] == StringTraits::DirectorySeparator && path[1] == StringTraits::DirectorySeparator) ||
(path[0] == StringTraits::AlternateDirectorySeparator && path[1] == StringTraits::AlternateDirectorySeparator);
}
constexpr inline s64 GetWindowsPathSkipLength(const char *path) {
if (IsWindowsDrive(path)) {
return 2;
}
if (IsUnc(path)) {
for (s64 i = 2; path[i] != StringTraits::NullTerminator; ++i) {
if (path[i] == '$' || path[i] == ':') {
return i + 1;
}
}
}
return 0;
}
/* Path utilities. */
inline void Replace(char *dst, size_t dst_size, char old_char, char new_char) {
for (char *cur = dst; cur < dst + dst_size && *cur != '\x00'; cur++) {
AMS_ASSERT(dst != nullptr);
for (char *cur = dst; cur < dst + dst_size && *cur != StringTraits::NullTerminator; ++cur) {
if (*cur == old_char) {
*cur = new_char;
}
}
}
Result FspPathPrintf(fssrv::sf::FspPath *dst, const char *format, ...) __attribute__((format(printf, 2, 3)));
inline Result FspPathPrintf(fssrv::sf::FspPath *dst, const char *format, ...) {
AMS_ASSERT(dst != nullptr);
/* Format the path. */
std::va_list va_list;
va_start(va_list, format);
@ -43,6 +87,37 @@ namespace ams::fs {
return ResultSuccess();
}
Result VerifyPath(const char *path, size_t max_path_len, size_t max_name_len);
Result VerifyPath(const char *path, int max_path_len, int max_name_len);
bool IsSubPath(const char *lhs, const char *rhs);
/* Path normalization. */
class PathNormalizer {
public:
static constexpr const char RootPath[] = "/";
public:
static constexpr inline bool IsSeparator(char c) {
return c == StringTraits::DirectorySeparator;
}
static constexpr inline bool IsAnySeparator(char c) {
return c == StringTraits::DirectorySeparator || c == StringTraits::AlternateDirectorySeparator;
}
static constexpr inline bool IsNullTerminator(char c) {
return c == StringTraits::NullTerminator;
}
static constexpr inline bool IsCurrentDirectory(const char *p) {
return p[0] == StringTraits::Dot && (IsSeparator(p[1]) || IsNullTerminator(p[1]));
}
static constexpr inline bool IsParentDirectory(const char *p) {
return p[0] == StringTraits::Dot && p[1] == StringTraits::Dot && (IsSeparator(p[2]) || IsNullTerminator(p[2]));
}
static Result Normalize(char *out, size_t *out_len, const char *src, size_t max_out_size, bool unc_preserved = false, bool has_mount_name = false);
static Result IsNormalized(bool *out, const char *path, bool unc_preserved = false, bool has_mount_name = false);
};
}

View file

@ -20,7 +20,6 @@
#include <stratosphere/fs/fsa/fs_idirectory.hpp>
#include <stratosphere/fs/fsa/fs_ifilesystem.hpp>
#include <stratosphere/fs/fs_query_range.hpp>
#include <stratosphere/fs/fs_path_tool.hpp>
#include <stratosphere/fs/fs_path_utils.hpp>
namespace ams::fs {
@ -100,12 +99,12 @@ namespace ams::fs {
out_path->str[sizeof(out_path->str) - 1] = '\x00';
/* Replace directory separators. */
Replace(out_path->str, sizeof(out_path->str) - 1, StringTraits::AlternateDirectorySeparator, StringTraits::DirectorySeparator);
fs::Replace(out_path->str, sizeof(out_path->str) - 1, StringTraits::AlternateDirectorySeparator, StringTraits::DirectorySeparator);
/* Get lengths. */
const auto mount_name_len = PathTool::IsWindowsAbsolutePath(path) ? 2 : 0;
const auto rel_path = out_path->str + mount_name_len;
const auto max_len = fs::EntryNameLengthMax - mount_name_len;
const auto skip_len = fs::GetWindowsPathSkipLength(path);
const auto rel_path = out_path->str + skip_len;
const auto max_len = fs::EntryNameLengthMax - skip_len;
return VerifyPath(rel_path, max_len, max_len);
}
public:

View file

@ -31,7 +31,7 @@ namespace ams::fssrv {
Option_AcceptEmpty = BIT(4),
};
private:
using Buffer = std::unique_ptr<char[]>;
using Buffer = std::unique_ptr<char[], fs::impl::Deleter>;
private:
Buffer buffer;
const char *path;
@ -39,8 +39,9 @@ namespace ams::fssrv {
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:
/* TODO: Remove non-option constructor. */
explicit PathNormalizer(const char *p) : buffer(), path(nullptr), result(ResultSuccess()) {
this->result = Normalize(&this->path, &this->buffer, p, false, false, false);
this->result = Normalize(std::addressof(this->path), std::addressof(this->buffer), p, false, false, false);
}
PathNormalizer(const char *p, u32 option) : buffer(), path(nullptr), result(ResultSuccess()) {
@ -50,15 +51,15 @@ namespace ams::fssrv {
const bool preserve_unc = (option & Option_PreserveUnc);
const bool preserve_tail_sep = (option & Option_PreserveTailSeparator);
const bool has_mount_name = (option & Option_HasMountName);
this->result = Normalize(&this->path, &this->buffer, p, preserve_unc, preserve_tail_sep, has_mount_name);
this->result = Normalize(std::addressof(this->path), std::addressof(this->buffer), p, preserve_unc, preserve_tail_sep, has_mount_name);
}
}
inline Result GetResult() const {
Result GetResult() const {
return this->result;
}
inline const char * GetPath() const {
const char *GetPath() const {
return this->path;
}
};

View file

@ -21,7 +21,6 @@
#include <stratosphere/fssystem/fssystem_external_code.hpp>
#include <stratosphere/fssystem/fssystem_partition_file_system.hpp>
#include <stratosphere/fssystem/fssystem_partition_file_system_meta.hpp>
#include <stratosphere/fssystem/fssystem_path_tool.hpp>
#include <stratosphere/fssystem/fssystem_thread_priority_changer.hpp>
#include <stratosphere/fssystem/fssystem_aes_ctr_storage.hpp>
#include <stratosphere/fssystem/fssystem_aes_xts_storage.hpp>

View file

@ -1,26 +0,0 @@
/*
* 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 "../fs/fs_common.hpp"
#include "../fs/fs_path_tool.hpp"
namespace ams::fssystem {
namespace StringTraits = ::ams::fs::StringTraits;
using PathTool = ::ams::fs::PathTool;
}

View file

@ -14,11 +14,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../fs/fs_common.hpp"
#include "../fs/fs_file.hpp"
#include "../fs/fs_directory.hpp"
#include "../fs/fs_filesystem.hpp"
#include "fssystem_path_tool.hpp"
#include <stratosphere/fs/fs_common.hpp>
#include <stratosphere/fs/fs_file.hpp>
#include <stratosphere/fs/fs_directory.hpp>
#include <stratosphere/fs/fs_filesystem.hpp>
#include <stratosphere/fs/fs_path_utils.hpp>
namespace ams::fssystem {
@ -70,7 +70,7 @@ namespace ams::fssystem {
}
/* Restore parent path. */
work_path[parent_len] = StringTraits::NullTerminator;
work_path[parent_len] = fs::StringTraits::NullTerminator;
}
return ResultSuccess();
@ -91,13 +91,13 @@ namespace ams::fssystem {
/* Copy root path in, add a / if necessary. */
std::memcpy(work_path, root_path, root_path_len);
if (!PathTool::IsSeparator(work_path[root_path_len - 1])) {
work_path[root_path_len++] = StringTraits::DirectorySeparator;
if (!fs::PathNormalizer::IsSeparator(work_path[root_path_len - 1])) {
work_path[root_path_len++] = fs::StringTraits::DirectorySeparator;
}
/* Make sure the result path is still valid. */
R_UNLESS(root_path_len <= fs::EntryNameLengthMax, fs::ResultTooLongPath());
work_path[root_path_len] = StringTraits::NullTerminator;
work_path[root_path_len] = fs::StringTraits::NullTerminator;
return impl::IterateDirectoryRecursivelyImpl(fs, work_path, work_path_size, dir_ent_buf, on_enter_dir, on_exit_dir, on_file);
}
@ -111,7 +111,7 @@ namespace ams::fssystem {
template<typename OnEnterDir, typename OnExitDir, typename OnFile>
Result IterateDirectoryRecursively(fs::fsa::IFileSystem *fs, OnEnterDir on_enter_dir, OnExitDir on_exit_dir, OnFile on_file) {
return IterateDirectoryRecursively(fs, PathTool::RootPath, on_enter_dir, on_exit_dir, on_file);
return IterateDirectoryRecursively(fs, fs::PathNormalizer::RootPath, on_enter_dir, on_exit_dir, on_file);
}
/* TODO: Cleanup API */