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.
This commit is contained in:
Léo Lam 2019-12-28 17:44:47 +01:00
parent e3a7922e12
commit d185bc6f09
3 changed files with 27 additions and 13 deletions

View File

@ -11,6 +11,17 @@
namespace IOS::HLE::FS 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<FileSystem> MakeFileSystem(Location location) std::unique_ptr<FileSystem> MakeFileSystem(Location location)
{ {
const std::string nand_root = const std::string nand_root =

View File

@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#ifdef _WIN32 #ifdef _WIN32
@ -111,6 +112,13 @@ struct FileStatus
u32 size; 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 FileSystem;
class FileHandle final class FileHandle final
{ {

View File

@ -15,11 +15,6 @@
namespace IOS::HLE::FS 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 std::string HostFileSystem::BuildFilename(const std::string& wii_path) const
{ {
if (wii_path.compare(0, 1, "/") == 0) 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) ResultCode HostFileSystem::CreateDirectory(Uid, Gid, const std::string& path, FileAttribute, Modes)
{ {
if (!IsValidWiiPath(path)) if (!IsValidPath(path))
return ResultCode::Invalid; return ResultCode::Invalid;
std::string name(BuildFilename(path)); 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) ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path)
{ {
if (!IsValidWiiPath(path)) if (!IsValidPath(path))
return ResultCode::Invalid; return ResultCode::Invalid;
const std::string file_name = BuildFilename(path); 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, ResultCode HostFileSystem::Rename(Uid, Gid, const std::string& old_path,
const std::string& new_path) const std::string& new_path)
{ {
if (!IsValidWiiPath(old_path)) if (!IsValidPath(old_path))
return ResultCode::Invalid; return ResultCode::Invalid;
const std::string old_name = BuildFilename(old_path); const std::string old_name = BuildFilename(old_path);
if (!IsValidWiiPath(new_path)) if (!IsValidPath(new_path))
return ResultCode::Invalid; return ResultCode::Invalid;
const std::string new_name = BuildFilename(new_path); const std::string new_name = BuildFilename(new_path);
@ -252,7 +247,7 @@ ResultCode HostFileSystem::Rename(Uid, Gid, const std::string& old_path,
Result<std::vector<std::string>> HostFileSystem::ReadDirectory(Uid, Gid, const std::string& path) Result<std::vector<std::string>> HostFileSystem::ReadDirectory(Uid, Gid, const std::string& path)
{ {
if (!IsValidWiiPath(path)) if (!IsValidPath(path))
return ResultCode::Invalid; return ResultCode::Invalid;
// the Wii uses this function to define the type (dir or file) // the Wii uses this function to define the type (dir or file)
@ -301,7 +296,7 @@ Result<Metadata> HostFileSystem::GetMetadata(Uid, Gid, const std::string& path)
metadata.gid = 0x3031; // this is also known as makercd, 01 (0x3031) for nintendo and 08 metadata.gid = 0x3031; // this is also known as makercd, 01 (0x3031) for nintendo and 08
// (0x3038) for MH3 etc // (0x3038) for MH3 etc
if (!IsValidWiiPath(path)) if (!IsValidPath(path))
return ResultCode::Invalid; return ResultCode::Invalid;
std::string file_name = BuildFilename(path); std::string file_name = BuildFilename(path);
@ -330,7 +325,7 @@ Result<Metadata> HostFileSystem::GetMetadata(Uid, Gid, const std::string& path)
ResultCode HostFileSystem::SetMetadata(Uid caller_uid, const std::string& path, Uid uid, Gid gid, ResultCode HostFileSystem::SetMetadata(Uid caller_uid, const std::string& path, Uid uid, Gid gid,
FileAttribute, Modes) FileAttribute, Modes)
{ {
if (!IsValidWiiPath(path)) if (!IsValidPath(path))
return ResultCode::Invalid; return ResultCode::Invalid;
return ResultCode::Success; return ResultCode::Success;
} }
@ -354,7 +349,7 @@ Result<NandStats> HostFileSystem::GetNandStats()
Result<DirectoryStats> HostFileSystem::GetDirectoryStats(const std::string& wii_path) Result<DirectoryStats> HostFileSystem::GetDirectoryStats(const std::string& wii_path)
{ {
if (!IsValidWiiPath(wii_path)) if (!IsValidPath(wii_path))
return ResultCode::Invalid; return ResultCode::Invalid;
DirectoryStats stats{}; DirectoryStats stats{};