mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-19 09:25:08 -04:00
fs: update + consolidate path normalization logic
This commit is contained in:
parent
5ef93778f6
commit
32803d9920
22 changed files with 1007 additions and 463 deletions
|
@ -18,51 +18,36 @@
|
|||
namespace ams::fssrv {
|
||||
|
||||
Result PathNormalizer::Normalize(const char **out_path, Buffer *out_buf, const char *path, bool preserve_unc, bool preserve_tail_sep, bool has_mount_name) {
|
||||
/* Check pre-conditions. */
|
||||
AMS_ASSERT(out_path != nullptr);
|
||||
AMS_ASSERT(out_buf != nullptr);
|
||||
|
||||
/* Clear output. */
|
||||
*out_path = nullptr;
|
||||
*out_buf = Buffer();
|
||||
|
||||
/* Find start of path. */
|
||||
const char *path_start = path;
|
||||
if (has_mount_name) {
|
||||
while (path_start < path + fs::MountNameLengthMax + 1) {
|
||||
if (fssystem::PathTool::IsNullTerminator(*path_start)) {
|
||||
break;
|
||||
} else if (fssystem::PathTool::IsDriveSeparator(*(path_start++))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
R_UNLESS(path < path_start - 1, fs::ResultInvalidPath());
|
||||
R_UNLESS(fssystem::PathTool::IsDriveSeparator(*(path_start - 1)), fs::ResultInvalidPath());
|
||||
}
|
||||
|
||||
/* Check if we're normalized. */
|
||||
bool normalized = false;
|
||||
R_TRY(fssystem::PathTool::IsNormalized(&normalized, path_start));
|
||||
R_TRY(fs::PathNormalizer::IsNormalized(std::addressof(normalized), path, preserve_unc, has_mount_name));
|
||||
|
||||
if (normalized) {
|
||||
/* If we're already normalized, no allocation is needed. */
|
||||
*out_path = path;
|
||||
} else {
|
||||
/* Allocate a new buffer. */
|
||||
auto buffer = std::make_unique<char[]>(fs::EntryNameLengthMax + 1);
|
||||
auto buffer = fs::impl::MakeUnique<char[]>(fs::EntryNameLengthMax + 1);
|
||||
R_UNLESS(buffer != nullptr, fs::ResultAllocationFailureInPathNormalizer());
|
||||
|
||||
/* Copy in mount name. */
|
||||
const size_t mount_name_len = path_start - path;
|
||||
std::memcpy(buffer.get(), path, mount_name_len);
|
||||
|
||||
/* Generate normalized path. */
|
||||
size_t normalized_len = 0;
|
||||
R_TRY(fssystem::PathTool::Normalize(buffer.get() + mount_name_len, &normalized_len, path_start, fs::EntryNameLengthMax + 1 - mount_name_len, preserve_unc));
|
||||
R_TRY(fs::PathNormalizer::Normalize(buffer.get(), std::addressof(normalized_len), path, fs::EntryNameLengthMax + 1, preserve_unc, has_mount_name));
|
||||
|
||||
/* Preserve the tail separator, if we should. */
|
||||
if (preserve_tail_sep) {
|
||||
if (fssystem::PathTool::IsSeparator(path[strnlen(path, fs::EntryNameLengthMax) - 1])) {
|
||||
/* Nintendo doesn't actually validate this. */
|
||||
R_UNLESS(mount_name_len + normalized_len < fs::EntryNameLengthMax, fs::ResultTooLongPath());
|
||||
buffer[mount_name_len + normalized_len] = fssystem::StringTraits::DirectorySeparator;
|
||||
buffer[mount_name_len + normalized_len + 1] = fssystem::StringTraits::NullTerminator;
|
||||
if (fs::PathNormalizer::IsSeparator(path[strnlen(path, fs::EntryNameLengthMax) - 1]) && !fs::PathNormalizer::IsSeparator(buffer[normalized_len - 1])) {
|
||||
AMS_ASSERT(normalized_len < fs::EntryNameLengthMax);
|
||||
buffer[normalized_len] = fs::StringTraits::DirectorySeparator;
|
||||
buffer[normalized_len + 1] = fs::StringTraits::NullTerminator;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue