Skip to content
Permalink
Browse files

Merge pull request #8208 from lioncash/fmt

Core/IOS: Use fmt where applicable
  • Loading branch information...
leoetlino committed Jun 28, 2019
2 parents 769ba43 + 980085e commit 6fd435fdffdbde75807186cb92b34ee3f2b5718b
@@ -16,6 +16,7 @@
#include <utility>
#include <vector>

#include <fmt/format.h>
#include <mbedtls/sha1.h>

#include "Common/Assert.h"
@@ -320,7 +321,7 @@ std::string TMDReader::GetGameID() const
if (all_printable)
return std::string(game_id, sizeof(game_id));

return StringFromFormat("%016" PRIx64, GetTitleId());
return fmt::format("{:016x}", GetTitleId());
}

std::string TMDReader::GetGameTDBID() const
@@ -334,7 +335,7 @@ std::string TMDReader::GetGameTDBID() const
if (all_printable)
return std::string(begin, end);

return StringFromFormat("%016" PRIx64, GetTitleId());
return fmt::format("{:016x}", GetTitleId());
}

u16 TMDReader::GetNumContents() const
@@ -547,7 +548,7 @@ struct SharedContentMap::Entry
std::array<u8, 20> sha1;
};

static const std::string CONTENT_MAP_PATH = "/shared1/content.map";
constexpr char CONTENT_MAP_PATH[] = "/shared1/content.map";
SharedContentMap::SharedContentMap(std::shared_ptr<HLE::FS::FileSystem> fs) : m_fs{fs}
{
static_assert(sizeof(Entry) == 28, "SharedContentMap::Entry has the wrong size");
@@ -572,7 +573,7 @@ SharedContentMap::GetFilenameFromSHA1(const std::array<u8, 20>& sha1) const
return {};

const std::string id_string(it->id.begin(), it->id.end());
return StringFromFormat("/shared1/%s.app", id_string.c_str());
return fmt::format("/shared1/{}.app", id_string);
}

std::vector<std::array<u8, 20>> SharedContentMap::GetHashes() const
@@ -591,14 +592,14 @@ std::string SharedContentMap::AddSharedContent(const std::array<u8, 20>& sha1)
if (filename)
return *filename;

const std::string id = StringFromFormat("%08x", m_last_id);
const std::string id = fmt::format("{:08x}", m_last_id);
Entry entry;
std::copy(id.cbegin(), id.cend(), entry.id.begin());
entry.sha1 = sha1;
m_entries.push_back(entry);

WriteEntries();
filename = StringFromFormat("/shared1/%s.app", id.c_str());
filename = fmt::format("/shared1/{}.app", id);
m_last_id++;
return *filename;
}
@@ -640,7 +641,7 @@ static std::pair<u32, u64> ReadUidSysEntry(const HLE::FS::FileHandle& file)
return {Common::swap32(uid), Common::swap64(title_id)};
}

static const std::string UID_MAP_PATH = "/sys/uid.sys";
constexpr char UID_MAP_PATH[] = "/sys/uid.sys";
UIDSys::UIDSys(std::shared_ptr<HLE::FS::FileSystem> fs) : m_fs{fs}
{
if (const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, UID_MAP_PATH, HLE::FS::Mode::Read))
@@ -12,6 +12,8 @@
#include <unordered_set>
#include <vector>

#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
@@ -86,8 +88,8 @@ static std::vector<u64> GetTitlesInTitleOrImport(FS::FileSystem* fs, const std::
if (!IsValidPartOfTitleID(title_type))
continue;

const auto title_entries =
fs->ReadDirectory(PID_KERNEL, PID_KERNEL, titles_dir + '/' + title_type);
const std::string title_dir = fmt::format("{}/{}", titles_dir, title_type);
const auto title_entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, title_dir);
if (!title_entries)
continue;

@@ -96,8 +98,10 @@ static std::vector<u64> GetTitlesInTitleOrImport(FS::FileSystem* fs, const std::
if (!IsValidPartOfTitleID(title_identifier))
continue;
if (!fs->ReadDirectory(PID_KERNEL, PID_KERNEL,
titles_dir + '/' + title_type + '/' + title_identifier))
fmt::format("{}/{}", title_dir, title_identifier)))
{
continue;
}

const u32 type = std::stoul(title_type, nullptr, 16);
const u32 identifier = std::stoul(title_identifier, nullptr, 16);
@@ -150,7 +154,8 @@ std::vector<u64> ES::GetTitlesWithTickets() const
for (const std::string& file_name : *sub_entries)
{
const std::string name_without_ext = file_name.substr(0, 8);
if (fs->ReadDirectory(PID_KERNEL, PID_KERNEL, "/ticket/" + title_type + '/' + file_name) ||
if (fs->ReadDirectory(PID_KERNEL, PID_KERNEL,
fmt::format("/ticket/{}/{}", title_type, file_name)) ||
!IsValidPartOfTitleID(name_without_ext) || name_without_ext + ".tik" != file_name)
{
continue;
@@ -299,13 +304,13 @@ bool ES::FinishImport(const IOS::ES::TMDReader& tmd)
// Remove everything not listed in the TMD.
std::unordered_set<std::string> expected_entries = {"title.tmd"};
for (const auto& content_info : tmd.GetContents())
expected_entries.insert(StringFromFormat("%08x.app", content_info.id));
expected_entries.insert(fmt::format("{:08x}.app", content_info.id));
const auto entries = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, import_content_dir);
if (!entries)
return false;
for (const std::string& name : *entries)
{
const std::string absolute_path = import_content_dir + '/' + name;
const std::string absolute_path = fmt::format("{}/{}", import_content_dir, name);
// There should not be any directory in there. Remove it.
if (fs->ReadDirectory(PID_KERNEL, PID_KERNEL, absolute_path))
fs->Delete(PID_KERNEL, PID_KERNEL, absolute_path);
@@ -333,7 +338,8 @@ bool ES::WriteImportTMD(const IOS::ES::TMDReader& tmd)
return false;
}

const std::string dest = Common::GetImportTitlePath(tmd.GetTitleId()) + "/content/title.tmd";
const std::string dest =
fmt::format("{}/content/title.tmd", Common::GetImportTitlePath(tmd.GetTitleId()));
return fs->Rename(PID_KERNEL, PID_KERNEL, tmd_path, dest) == FS::ResultCode::Success;
}

@@ -365,7 +371,7 @@ std::string ES::GetContentPath(const u64 title_id, const IOS::ES::Content& conte
{
if (content.IsShared())
return content_map.GetFilenameFromSHA1(content.sha1).value_or("");
return Common::GetTitleContentPath(title_id) + StringFromFormat("/%08x.app", content.id);
return fmt::format("{}/{:08x}.app", Common::GetTitleContentPath(title_id), content.id);
}

std::string ES::GetContentPath(const u64 title_id, const IOS::ES::Content& content) const
@@ -10,12 +10,12 @@
#include <utility>
#include <vector>

#include <fmt/format.h>
#include <mbedtls/sha1.h>

#include "Common/Align.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Core/CommonTitles.h"
#include "Core/HW/Memmap.h"
#include "Core/IOS/ES/Formats.h"
@@ -350,7 +350,7 @@ static bool CheckIfContentHashMatches(const std::vector<u8>& content, const IOS:

static std::string GetImportContentPath(u64 title_id, u32 content_id)
{
return Common::GetImportTitlePath(title_id) + StringFromFormat("/content/%08x.app", content_id);
return fmt::format("{}/content/{:08x}.app", Common::GetImportTitlePath(title_id), content_id);
}

ReturnCode ES::ImportContentEnd(Context& context, u32 content_fd)
@@ -438,8 +438,7 @@ static bool HasAllRequiredContents(IOS::HLE::Kernel& ios, const IOS::ES::TMDRead

// Note: the import hasn't been finalised yet, so the whole title directory
// is still in /import, not /title.
const std::string path =
Common::GetImportTitlePath(title_id) + StringFromFormat("/content/%08x.app", content.id);
const std::string path = GetImportContentPath(title_id, content.id);
return ios.GetFS()->GetMetadata(PID_KERNEL, PID_KERNEL, path).Succeeded();
});
}
@@ -567,7 +566,7 @@ ReturnCode ES::DeleteTicket(const u8* ticket_view)

// Delete the ticket directory if it is now empty.
const std::string ticket_parent_dir =
StringFromFormat("/ticket/%08x", static_cast<u32>(title_id >> 32));
fmt::format("/ticket/{:08x}", static_cast<u32>(title_id >> 32));
const auto ticket_parent_dir_entries =
fs->ReadDirectory(PID_KERNEL, PID_KERNEL, ticket_parent_dir);
if (ticket_parent_dir_entries && ticket_parent_dir_entries->empty())
@@ -625,9 +624,9 @@ ReturnCode ES::DeleteContent(u64 title_id, u32 content_id) const
if (!tmd.FindContentById(content_id, &content))
return ES_EINVAL;

return FS::ConvertResult(m_ios.GetFS()->Delete(PID_KERNEL, PID_KERNEL,
Common::GetTitleContentPath(title_id) +
StringFromFormat("/%08x.app", content_id)));
const std::string path =
fmt::format("{}/{:08x}.app", Common::GetTitleContentPath(title_id), content_id);
return FS::ConvertResult(m_ios.GetFS()->Delete(PID_KERNEL, PID_KERNEL, path));
}

IPCCommandResult ES::DeleteContent(const IOCtlVRequest& request)
@@ -6,8 +6,10 @@

#include <algorithm>
#include <cstring>
#include <string_view>

#include <fmt/format.h>

#include "Common/Assert.h"
#include "Common/ChunkFile.h"
#include "Common/StringUtil.h"
#include "Common/Swap.h"
@@ -45,16 +47,19 @@ void FS::DoState(PointerWrap& p)
p.Do(m_dirty_cache);
}

static void LogResult(const std::string& command, ResultCode code)
template <typename... Args>
static void LogResult(ResultCode code, std::string_view format, Args&&... args)
{
const std::string command = fmt::format(format, std::forward<Args>(args)...);
GENERIC_LOG(LogTypes::IOS_FS, (code == ResultCode::Success ? LogTypes::LINFO : LogTypes::LERROR),
"%s: result %d", command.c_str(), ConvertResult(code));
}

template <typename T>
static void LogResult(const std::string& command, const Result<T>& result)
template <typename T, typename... Args>
static void LogResult(const Result<T>& result, std::string_view format, Args&&... args)
{
LogResult(command, result.Succeeded() ? ResultCode::Success : result.Error());
const auto result_code = result.Succeeded() ? ResultCode::Success : result.Error();
LogResult(result_code, format, std::forward<Args>(args)...);
}

enum class FileLookupMode
@@ -108,7 +113,7 @@ IPCCommandResult FS::Open(const OpenRequest& request)

auto backend_fd = m_ios.GetFS()->OpenFile(request.uid, request.gid, request.path,
static_cast<Mode>(request.flags & 3));
LogResult(StringFromFormat("OpenFile(%s)", request.path.c_str()), backend_fd);
LogResult(backend_fd, "OpenFile({})", request.path);
if (!backend_fd)
return GetFSReply(ConvertResult(backend_fd.Error()), ticks);

@@ -132,7 +137,7 @@ IPCCommandResult FS::Close(u32 fd)
ticks += SUPERBLOCK_WRITE_TICKS;

const ResultCode result = m_ios.GetFS()->Close(m_fd_map[fd].fs_fd);
LogResult(StringFromFormat("Close(%s)", m_fd_map[fd].name.data()), result);
LogResult(result, "Close({})", m_fd_map[fd].name.data());
m_fd_map.erase(fd);
if (result != ResultCode::Success)
return GetFSReply(ConvertResult(result));
@@ -227,9 +232,7 @@ IPCCommandResult FS::Read(const ReadWriteRequest& request)

const Result<u32> result = m_ios.GetFS()->ReadBytesFromFile(
handle.fs_fd, Memory::GetPointer(request.buffer), request.size);
LogResult(
StringFromFormat("Read(%s, 0x%08x, %u)", handle.name.data(), request.buffer, request.size),
result);
LogResult(result, "Read({}, 0x{:08x}, {})", handle.name.data(), request.buffer, request.size);
if (!result)
return GetFSReply(ConvertResult(result.Error()));

@@ -247,9 +250,7 @@ IPCCommandResult FS::Write(const ReadWriteRequest& request)

const Result<u32> result = m_ios.GetFS()->WriteBytesToFile(
handle.fs_fd, Memory::GetPointer(request.buffer), request.size);
LogResult(
StringFromFormat("Write(%s, 0x%08x, %u)", handle.name.data(), request.buffer, request.size),
result);
LogResult(result, "Write({}, 0x{:08x}, {})", handle.name.data(), request.buffer, request.size);
if (!result)
return GetFSReply(ConvertResult(result.Error()));

@@ -264,9 +265,7 @@ IPCCommandResult FS::Seek(const SeekRequest& request)

const Result<u32> result =
m_ios.GetFS()->SeekFile(handle.fs_fd, request.offset, IOS::HLE::FS::SeekMode(request.mode));
LogResult(
StringFromFormat("Seek(%s, 0x%08x, %u)", handle.name.data(), request.offset, request.mode),
result);
LogResult(result, "Seek({}, 0x{:08x}, {})", handle.name.data(), request.offset, request.mode);
if (!result)
return GetFSReply(ConvertResult(result.Error()));
return GetFSReply(*result);
@@ -378,7 +377,7 @@ IPCCommandResult FS::GetStats(const Handle& handle, const IOCtlRequest& request)
return GetFSReply(ConvertResult(ResultCode::Invalid));

const Result<NandStats> stats = m_ios.GetFS()->GetNandStats();
LogResult("GetNandStats", stats);
LogResult(stats, "GetNandStats");
if (!stats)
return GetDefaultReply(ConvertResult(stats.Error()));

@@ -402,7 +401,7 @@ IPCCommandResult FS::CreateDirectory(const Handle& handle, const IOCtlRequest& r

const ResultCode result = m_ios.GetFS()->CreateDirectory(handle.uid, handle.gid, params->path,
params->attribute, params->modes);
LogResult(StringFromFormat("CreateDirectory(%s)", params->path), result);
LogResult(result, "CreateDirectory({})", params->path);
return GetReplyForSuperblockOperation(result);
}

@@ -438,7 +437,7 @@ IPCCommandResult FS::ReadDirectory(const Handle& handle, const IOCtlVRequest& re
const std::string directory = Memory::GetString(request.in_vectors[0].address, 64);
const Result<std::vector<std::string>> list =
m_ios.GetFS()->ReadDirectory(handle.uid, handle.gid, directory);
LogResult(StringFromFormat("ReadDirectory(%s)", directory.c_str()), list);
LogResult(list, "ReadDirectory({})", directory);
if (!list)
return GetFSReply(ConvertResult(list.Error()));

@@ -468,7 +467,7 @@ IPCCommandResult FS::SetAttribute(const Handle& handle, const IOCtlRequest& requ

const ResultCode result = m_ios.GetFS()->SetMetadata(
handle.uid, params->path, params->uid, params->gid, params->attribute, params->modes);
LogResult(StringFromFormat("SetMetadata(%s)", params->path), result);
LogResult(result, "SetMetadata({})", params->path);
return GetReplyForSuperblockOperation(result);
}

@@ -480,7 +479,7 @@ IPCCommandResult FS::GetAttribute(const Handle& handle, const IOCtlRequest& requ
const std::string path = Memory::GetString(request.buffer_in, 64);
const u64 ticks = EstimateFileLookupTicks(path, FileLookupMode::Split);
const Result<Metadata> metadata = m_ios.GetFS()->GetMetadata(handle.uid, handle.gid, path);
LogResult(StringFromFormat("GetMetadata(%s)", path.c_str()), metadata);
LogResult(metadata, "GetMetadata({})", path);
if (!metadata)
return GetFSReply(ConvertResult(metadata.Error()), ticks);

@@ -503,7 +502,7 @@ IPCCommandResult FS::DeleteFile(const Handle& handle, const IOCtlRequest& reques

const std::string path = Memory::GetString(request.buffer_in, 64);
const ResultCode result = m_ios.GetFS()->Delete(handle.uid, handle.gid, path);
LogResult(StringFromFormat("Delete(%s)", path.c_str()), result);
LogResult(result, "Delete({})", path);
return GetReplyForSuperblockOperation(result);
}

@@ -515,7 +514,7 @@ IPCCommandResult FS::RenameFile(const Handle& handle, const IOCtlRequest& reques
const std::string old_path = Memory::GetString(request.buffer_in, 64);
const std::string new_path = Memory::GetString(request.buffer_in + 64, 64);
const ResultCode result = m_ios.GetFS()->Rename(handle.uid, handle.gid, old_path, new_path);
LogResult(StringFromFormat("Rename(%s, %s)", old_path.c_str(), new_path.c_str()), result);
LogResult(result, "Rename({}, {})", old_path, new_path);
return GetReplyForSuperblockOperation(result);
}

@@ -527,7 +526,7 @@ IPCCommandResult FS::CreateFile(const Handle& handle, const IOCtlRequest& reques

const ResultCode result = m_ios.GetFS()->CreateFile(handle.uid, handle.gid, params->path,
params->attribute, params->modes);
LogResult(StringFromFormat("CreateFile(%s)", params->path), result);
LogResult(result, "CreateFile({})", params->path);
return GetReplyForSuperblockOperation(result);
}

@@ -548,7 +547,7 @@ IPCCommandResult FS::GetFileStats(const Handle& handle, const IOCtlRequest& requ
return GetFSReply(ConvertResult(ResultCode::Invalid));

const Result<FileStatus> status = m_ios.GetFS()->GetFileStatus(handle.fs_fd);
LogResult(StringFromFormat("GetFileStatus(%s)", handle.name.data()), status);
LogResult(status, "GetFileStatus({})", handle.name.data());
if (!status)
return GetDefaultReply(ConvertResult(status.Error()));

@@ -569,7 +568,7 @@ IPCCommandResult FS::GetUsage(const Handle& handle, const IOCtlVRequest& request

const std::string directory = Memory::GetString(request.in_vectors[0].address, 64);
const Result<DirectoryStats> stats = m_ios.GetFS()->GetDirectoryStats(directory);
LogResult(StringFromFormat("GetDirectoryStats(%s)", directory.c_str()), stats);
LogResult(stats, "GetDirectoryStats({})", directory);
if (!stats)
return GetFSReply(ConvertResult(stats.Error()));

0 comments on commit 6fd435f

Please sign in to comment.
You can’t perform that action at this time.