From d185bc6f0927b7b08a8f28a96066f8d7a72617e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sat, 28 Dec 2019 17:44:47 +0100 Subject: [PATCH] IOS/FS: Move path validity check functions They will be used in more places than just HostBackend/FS.cpp. Also fix the check and make it accurate while we're at it. --- Source/Core/Core/IOS/FS/FileSystem.cpp | 11 +++++++++++ Source/Core/Core/IOS/FS/FileSystem.h | 8 ++++++++ Source/Core/Core/IOS/FS/HostBackend/FS.cpp | 21 ++++++++------------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Source/Core/Core/IOS/FS/FileSystem.cpp b/Source/Core/Core/IOS/FS/FileSystem.cpp index 3fa652fb22..11806f313c 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.cpp +++ b/Source/Core/Core/IOS/FS/FileSystem.cpp @@ -11,6 +11,17 @@ namespace IOS::HLE::FS { +bool IsValidPath(std::string_view path) +{ + return path == "/" || IsValidNonRootPath(path); +} + +bool IsValidNonRootPath(std::string_view path) +{ + return path.length() > 1 && path.length() <= MaxPathLength && path[0] == '/' && + path.back() != '/'; +} + std::unique_ptr MakeFileSystem(Location location) { const std::string nand_root = diff --git a/Source/Core/Core/IOS/FS/FileSystem.h b/Source/Core/Core/IOS/FS/FileSystem.h index 6266ba6979..5bed4349d0 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.h +++ b/Source/Core/Core/IOS/FS/FileSystem.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -111,6 +112,13 @@ struct FileStatus u32 size; }; +/// The maximum number of characters a path can have. +constexpr size_t MaxPathLength = 64; + +/// Returns whether a Wii path is valid. +bool IsValidPath(std::string_view path); +bool IsValidNonRootPath(std::string_view path); + class FileSystem; class FileHandle final { diff --git a/Source/Core/Core/IOS/FS/HostBackend/FS.cpp b/Source/Core/Core/IOS/FS/HostBackend/FS.cpp index 345800f5a5..65f42e228e 100644 --- a/Source/Core/Core/IOS/FS/HostBackend/FS.cpp +++ b/Source/Core/Core/IOS/FS/HostBackend/FS.cpp @@ -15,11 +15,6 @@ namespace IOS::HLE::FS { -static bool IsValidWiiPath(const std::string& path) -{ - return path.compare(0, 1, "/") == 0; -} - std::string HostFileSystem::BuildFilename(const std::string& wii_path) const { if (wii_path.compare(0, 1, "/") == 0) @@ -185,7 +180,7 @@ ResultCode HostFileSystem::CreateFile(Uid, Gid, const std::string& path, FileAtt ResultCode HostFileSystem::CreateDirectory(Uid, Gid, const std::string& path, FileAttribute, Modes) { - if (!IsValidWiiPath(path)) + if (!IsValidPath(path)) return ResultCode::Invalid; std::string name(BuildFilename(path)); @@ -199,7 +194,7 @@ ResultCode HostFileSystem::CreateDirectory(Uid, Gid, const std::string& path, Fi ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path) { - if (!IsValidWiiPath(path)) + if (!IsValidPath(path)) return ResultCode::Invalid; const std::string file_name = BuildFilename(path); @@ -216,11 +211,11 @@ ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path) ResultCode HostFileSystem::Rename(Uid, Gid, const std::string& old_path, const std::string& new_path) { - if (!IsValidWiiPath(old_path)) + if (!IsValidPath(old_path)) return ResultCode::Invalid; const std::string old_name = BuildFilename(old_path); - if (!IsValidWiiPath(new_path)) + if (!IsValidPath(new_path)) return ResultCode::Invalid; const std::string new_name = BuildFilename(new_path); @@ -252,7 +247,7 @@ ResultCode HostFileSystem::Rename(Uid, Gid, const std::string& old_path, Result> HostFileSystem::ReadDirectory(Uid, Gid, const std::string& path) { - if (!IsValidWiiPath(path)) + if (!IsValidPath(path)) return ResultCode::Invalid; // the Wii uses this function to define the type (dir or file) @@ -301,7 +296,7 @@ Result HostFileSystem::GetMetadata(Uid, Gid, const std::string& path) metadata.gid = 0x3031; // this is also known as makercd, 01 (0x3031) for nintendo and 08 // (0x3038) for MH3 etc - if (!IsValidWiiPath(path)) + if (!IsValidPath(path)) return ResultCode::Invalid; std::string file_name = BuildFilename(path); @@ -330,7 +325,7 @@ Result HostFileSystem::GetMetadata(Uid, Gid, const std::string& path) ResultCode HostFileSystem::SetMetadata(Uid caller_uid, const std::string& path, Uid uid, Gid gid, FileAttribute, Modes) { - if (!IsValidWiiPath(path)) + if (!IsValidPath(path)) return ResultCode::Invalid; return ResultCode::Success; } @@ -354,7 +349,7 @@ Result HostFileSystem::GetNandStats() Result HostFileSystem::GetDirectoryStats(const std::string& wii_path) { - if (!IsValidWiiPath(wii_path)) + if (!IsValidPath(wii_path)) return ResultCode::Invalid; DirectoryStats stats{};