From 89713c588955a8d9a687e4b0351e0684ed51e241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Wed, 7 Mar 2018 18:36:04 +0100 Subject: [PATCH] IOS/FS: Add CreateFullPath helper Analogous to File::CreateFullPath, but for the Wii filesystem so this ensures that directories and files receive proper attributes. (This function is technically not part of the FS sysmodule but it's in an internal FS library that is reused in several IOS sysmodules.) --- Source/Core/Core/IOS/FS/FileSystem.cpp | 25 +++++++++++++++++++++++++ Source/Core/Core/IOS/FS/FileSystem.h | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/Source/Core/Core/IOS/FS/FileSystem.cpp b/Source/Core/Core/IOS/FS/FileSystem.cpp index f2beb7d0c8..c6db165002 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.cpp +++ b/Source/Core/Core/IOS/FS/FileSystem.cpp @@ -61,4 +61,29 @@ void FileSystem::Init() if (Delete(0, 0, "/tmp") == ResultCode::Success) CreateDirectory(0, 0, "/tmp", 0, Mode::ReadWrite, Mode::ReadWrite, Mode::ReadWrite); } + +ResultCode FileSystem::CreateFullPath(Uid uid, Gid gid, const std::string& path, + FileAttribute attribute, Mode owner, Mode group, Mode other) +{ + std::string::size_type position = 1; + while (true) + { + position = path.find('/', position); + if (position == std::string::npos) + return ResultCode::Success; + + const std::string subpath = path.substr(0, position); + const Result metadata = GetMetadata(uid, gid, subpath); + if (!metadata && metadata.Error() != ResultCode::NotFound) + return metadata.Error(); + if (metadata && metadata->is_file) + return ResultCode::Invalid; + + const ResultCode result = CreateDirectory(uid, gid, subpath, attribute, owner, group, other); + if (result != ResultCode::Success && result != ResultCode::AlreadyExists) + return result; + + ++position; + } +} } // namespace IOS::HLE::FS diff --git a/Source/Core/Core/IOS/FS/FileSystem.h b/Source/Core/Core/IOS/FS/FileSystem.h index eb14dc6673..42edb14cdf 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.h +++ b/Source/Core/Core/IOS/FS/FileSystem.h @@ -157,6 +157,11 @@ public: FileAttribute attribute, Mode owner_mode, Mode group_mode, Mode other_mode) = 0; + /// Create any parent directories for a path with the specified metadata. + /// Example: "/a/b" to create directory /a; "/a/b/" to create directories /a and /a/b + ResultCode CreateFullPath(Uid caller_uid, Gid caller_gid, const std::string& path, + FileAttribute attribute, Mode ownerm, Mode group, Mode other); + /// Delete a file or directory with the specified path. virtual ResultCode Delete(Uid caller_uid, Gid caller_gid, const std::string& path) = 0; /// Rename a file or directory with the specified path.