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 1/2] 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. From fb1d075330b75b6763d66448e24612eeee73a72d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 11 Mar 2018 18:28:36 +0100 Subject: [PATCH 2/2] IOS/NCD: Migrate to new filesystem interface A followup for the migration work started in 8317a66 --- Source/Core/Core/IOS/Network/NCD/Manage.cpp | 5 +- .../Core/IOS/Network/NCD/WiiNetConfig.cpp | 48 +++++++++---------- .../Core/Core/IOS/Network/NCD/WiiNetConfig.h | 12 +++-- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Source/Core/Core/IOS/Network/NCD/Manage.cpp b/Source/Core/Core/IOS/Network/NCD/Manage.cpp index 410c3bc863..9d6cdd62a4 100644 --- a/Source/Core/Core/IOS/Network/NCD/Manage.cpp +++ b/Source/Core/Core/IOS/Network/NCD/Manage.cpp @@ -21,6 +21,7 @@ namespace Device { NetNCDManage::NetNCDManage(Kernel& ios, const std::string& device_name) : Device(ios, device_name) { + config.ReadConfig(ios.GetFS().get()); } IPCCommandResult NetNCDManage::IOCtlV(const IOCtlVRequest& request) @@ -51,14 +52,14 @@ IPCCommandResult NetNCDManage::IOCtlV(const IOCtlVRequest& request) case IOCTLV_NCD_READCONFIG: INFO_LOG(IOS_NET, "NET_NCD_MANAGE: IOCTLV_NCD_READCONFIG"); - config.ReadConfig(); + config.ReadConfig(m_ios.GetFS().get()); config.WriteToMem(request.io_vectors.at(0).address); break; case IOCTLV_NCD_WRITECONFIG: INFO_LOG(IOS_NET, "NET_NCD_MANAGE: IOCTLV_NCD_WRITECONFIG"); config.ReadFromMem(request.in_vectors.at(0).address); - config.WriteConfig(); + config.WriteConfig(m_ios.GetFS().get()); break; case IOCTLV_NCD_GETLINKSTATUS: diff --git a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp index 97a04a3b13..80e06289ad 100644 --- a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp +++ b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp @@ -8,10 +8,10 @@ #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" -#include "Common/File.h" -#include "Common/FileUtil.h" #include "Common/Logging/Log.h" #include "Core/HW/Memmap.h" +#include "Core/IOS/FS/FileSystem.h" +#include "Core/IOS/IOS.h" namespace IOS { @@ -19,36 +19,34 @@ namespace HLE { namespace Net { -WiiNetConfig::WiiNetConfig() -{ - m_path = File::GetUserPath(D_SESSION_WIIROOT_IDX) + "/" WII_SYSCONF_DIR "/net/02/config.dat"; - ReadConfig(); -} +static const std::string CONFIG_PATH = "/shared2/sys/net/02/config.dat"; -void WiiNetConfig::ReadConfig() -{ - if (!File::IOFile(m_path, "rb").ReadBytes(&m_data, sizeof(m_data))) - ResetConfig(); -} +WiiNetConfig::WiiNetConfig() = default; -void WiiNetConfig::WriteConfig() const +void WiiNetConfig::ReadConfig(FS::FileSystem* fs) { - if (!File::Exists(m_path)) { - if (!File::CreateFullPath(File::GetUserPath(D_SESSION_WIIROOT_IDX) + "/" WII_SYSCONF_DIR - "/net/02/")) - { - ERROR_LOG(IOS_NET, "Failed to create directory for network config file"); - } + const auto file = fs->OpenFile(PID_NCD, PID_NCD, CONFIG_PATH, FS::Mode::Read); + if (file && file->Read(&m_data, 1)) + return; } - - File::IOFile(m_path, "wb").WriteBytes(&m_data, sizeof(m_data)); + ResetConfig(fs); } -void WiiNetConfig::ResetConfig() +void WiiNetConfig::WriteConfig(FS::FileSystem* fs) const { - if (File::Exists(m_path)) - File::Delete(m_path); + fs->CreateFullPath(PID_NCD, PID_NCD, CONFIG_PATH, 0, FS::Mode::ReadWrite, FS::Mode::ReadWrite, + FS::Mode::ReadWrite); + fs->CreateFile(PID_NCD, PID_NCD, CONFIG_PATH, 0, FS::Mode::ReadWrite, FS::Mode::ReadWrite, + FS::Mode::ReadWrite); + const auto file = fs->OpenFile(PID_NCD, PID_NCD, CONFIG_PATH, FS::Mode::Write); + if (!file || !file->Write(&m_data, 1)) + ERROR_LOG(IOS_NET, "Failed to write config"); +} + +void WiiNetConfig::ResetConfig(FS::FileSystem* fs) +{ + fs->Delete(PID_NCD, PID_NCD, CONFIG_PATH); memset(&m_data, 0, sizeof(m_data)); m_data.connType = ConfigData::IF_WIRED; @@ -56,7 +54,7 @@ void WiiNetConfig::ResetConfig() ConnectionSettings::WIRED_IF | ConnectionSettings::DNS_DHCP | ConnectionSettings::IP_DHCP | ConnectionSettings::CONNECTION_TEST_OK | ConnectionSettings::CONNECTION_SELECTED; - WriteConfig(); + WriteConfig(fs); } void WiiNetConfig::WriteToMem(const u32 address) const diff --git a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.h b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.h index c1d8619991..ab14451aa2 100644 --- a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.h +++ b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.h @@ -11,6 +11,11 @@ namespace IOS { namespace HLE { +namespace FS +{ +class FileSystem; +} + namespace Net { #pragma pack(push, 1) @@ -105,9 +110,9 @@ class WiiNetConfig final public: WiiNetConfig(); - void ReadConfig(); - void WriteConfig() const; - void ResetConfig(); + void ReadConfig(FS::FileSystem* fs); + void WriteConfig(FS::FileSystem* fs) const; + void ResetConfig(FS::FileSystem* fs); void WriteToMem(u32 address) const; void ReadFromMem(u32 address); @@ -135,7 +140,6 @@ private: }; #pragma pack(pop) - std::string m_path; ConfigData m_data; }; } // namespace Net