From 398444e67635c158a20301d1a32a1c45bfdd4056 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 22 Jul 2018 01:23:29 -0400 Subject: [PATCH 1/2] file_util, vfs: Use std::string_view where applicable Avoids unnecessary construction of std::string instances where applicable. --- src/common/file_util.cpp | 84 +++++++------ src/common/file_util.h | 13 +- src/core/file_sys/vfs.cpp | 111 ++++++++++++------ src/core/file_sys/vfs.h | 50 ++++---- src/core/file_sys/vfs_offset.cpp | 2 +- src/core/file_sys/vfs_offset.h | 5 +- src/core/file_sys/vfs_real.cpp | 57 ++++++--- src/core/file_sys/vfs_real.h | 14 ++- .../hle/service/filesystem/filesystem.cpp | 2 +- src/core/loader/loader.cpp | 3 +- 10 files changed, 209 insertions(+), 132 deletions(-) diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 47ac8368e5..13fc31bfc2 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -802,66 +802,80 @@ void SplitFilename83(const std::string& filename, std::array& short_nam } } -std::vector SplitPathComponents(const std::string& filename) { - auto copy(filename); +std::vector SplitPathComponents(std::string_view filename) { + std::string copy(filename); std::replace(copy.begin(), copy.end(), '\\', '/'); std::vector out; - std::stringstream stream(filename); + std::stringstream stream(copy); std::string item; - while (std::getline(stream, item, '/')) + while (std::getline(stream, item, '/')) { out.push_back(std::move(item)); + } return out; } -std::string GetParentPath(const std::string& path) { - auto out = path; - const auto name_bck_index = out.find_last_of('\\'); - const auto name_fwd_index = out.find_last_of('/'); +std::string_view GetParentPath(std::string_view path) { + const auto name_bck_index = path.rfind('\\'); + const auto name_fwd_index = path.rfind('/'); size_t name_index; - if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos) - name_index = std::min(name_bck_index, name_fwd_index); - else - name_index = std::max(name_bck_index, name_fwd_index); - return out.erase(name_index); + if (name_bck_index == std::string_view::npos || name_fwd_index == std::string_view::npos) { + name_index = std::min(name_bck_index, name_fwd_index); + } else { + name_index = std::max(name_bck_index, name_fwd_index); + } + + return path.substr(0, name_index); } -std::string GetPathWithoutTop(std::string path) { - if (path.empty()) - return ""; - while (path[0] == '\\' || path[0] == '/') { - path = path.substr(1); - if (path.empty()) - return ""; +std::string_view GetPathWithoutTop(std::string_view path) { + if (path.empty()) { + return path; } - const auto name_bck_index = path.find_first_of('\\'); - const auto name_fwd_index = path.find_first_of('/'); + + while (path[0] == '\\' || path[0] == '/') { + path.remove_suffix(1); + if (path.empty()) { + return path; + } + } + + const auto name_bck_index = path.find('\\'); + const auto name_fwd_index = path.find('/'); return path.substr(std::min(name_bck_index, name_fwd_index) + 1); } -std::string GetFilename(std::string path) { - std::replace(path.begin(), path.end(), '\\', '/'); - auto name_index = path.find_last_of('/'); - if (name_index == std::string::npos) - return ""; +std::string_view GetFilename(std::string_view path) { + const auto name_index = path.find_last_of("\\/"); + + if (name_index == std::string_view::npos) { + return {}; + } + return path.substr(name_index + 1); } -std::string GetExtensionFromFilename(const std::string& name) { - size_t index = name.find_last_of('.'); - if (index == std::string::npos) - return ""; +std::string_view GetExtensionFromFilename(std::string_view name) { + const size_t index = name.rfind('.'); + + if (index == std::string_view::npos) { + return {}; + } return name.substr(index + 1); } -std::string RemoveTrailingSlash(const std::string& path) { - if (path.empty()) +std::string_view RemoveTrailingSlash(std::string_view path) { + if (path.empty()) { return path; - if (path.back() == '\\' || path.back() == '/') - return path.substr(0, path.size() - 1); + } + + if (path.back() == '\\' || path.back() == '/') { + path.remove_suffix(1); + return path; + } return path; } diff --git a/src/common/file_util.h b/src/common/file_util.h index 090907c033..abfa79eaea 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "common/common_types.h" @@ -151,22 +152,22 @@ void SplitFilename83(const std::string& filename, std::array& short_nam // Splits the path on '/' or '\' and put the components into a vector // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } -std::vector SplitPathComponents(const std::string& filename); +std::vector SplitPathComponents(std::string_view filename); // Gets all of the text up to the last '/' or '\' in the path. -std::string GetParentPath(const std::string& path); +std::string_view GetParentPath(std::string_view path); // Gets all of the text after the first '/' or '\' in the path. -std::string GetPathWithoutTop(std::string path); +std::string_view GetPathWithoutTop(std::string_view path); // Gets the filename of the path -std::string GetFilename(std::string path); +std::string_view GetFilename(std::string_view path); // Gets the extension of the filename -std::string GetExtensionFromFilename(const std::string& name); +std::string_view GetExtensionFromFilename(std::string_view name); // Removes the final '/' or '\' if one exists -std::string RemoveTrailingSlash(const std::string& path); +std::string_view RemoveTrailingSlash(std::string_view path); // Creates a new vector containing indices [first, last) from the original. template diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index 3f690f12a6..b99a4fd5be 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp @@ -13,7 +13,7 @@ namespace FileSys { VfsFile::~VfsFile() = default; std::string VfsFile::GetExtension() const { - return FileUtil::GetExtensionFromFilename(GetName()); + return std::string(FileUtil::GetExtensionFromFilename(GetName())); } VfsDirectory::~VfsDirectory() = default; @@ -46,64 +46,80 @@ size_t VfsFile::WriteBytes(const std::vector& data, size_t offset) { return Write(data.data(), data.size(), offset); } -std::shared_ptr VfsDirectory::GetFileRelative(const std::string& path) const { +std::shared_ptr VfsDirectory::GetFileRelative(std::string_view path) const { auto vec = FileUtil::SplitPathComponents(path); vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), vec.end()); - if (vec.empty()) + if (vec.empty()) { return nullptr; - if (vec.size() == 1) + } + + if (vec.size() == 1) { return GetFile(vec[0]); + } + auto dir = GetSubdirectory(vec[0]); for (size_t component = 1; component < vec.size() - 1; ++component) { - if (dir == nullptr) + if (dir == nullptr) { return nullptr; + } + dir = dir->GetSubdirectory(vec[component]); } - if (dir == nullptr) + + if (dir == nullptr) { return nullptr; + } + return dir->GetFile(vec.back()); } -std::shared_ptr VfsDirectory::GetFileAbsolute(const std::string& path) const { - if (IsRoot()) +std::shared_ptr VfsDirectory::GetFileAbsolute(std::string_view path) const { + if (IsRoot()) { return GetFileRelative(path); + } return GetParentDirectory()->GetFileAbsolute(path); } -std::shared_ptr VfsDirectory::GetDirectoryRelative(const std::string& path) const { +std::shared_ptr VfsDirectory::GetDirectoryRelative(std::string_view path) const { auto vec = FileUtil::SplitPathComponents(path); vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), vec.end()); - if (vec.empty()) + if (vec.empty()) { // TODO(DarkLordZach): Return this directory if path is '/' or similar. Can't currently // because of const-ness return nullptr; + } + auto dir = GetSubdirectory(vec[0]); for (size_t component = 1; component < vec.size(); ++component) { - if (dir == nullptr) + if (dir == nullptr) { return nullptr; + } + dir = dir->GetSubdirectory(vec[component]); } + return dir; } -std::shared_ptr VfsDirectory::GetDirectoryAbsolute(const std::string& path) const { - if (IsRoot()) +std::shared_ptr VfsDirectory::GetDirectoryAbsolute(std::string_view path) const { + if (IsRoot()) { return GetDirectoryRelative(path); + } return GetParentDirectory()->GetDirectoryAbsolute(path); } -std::shared_ptr VfsDirectory::GetFile(const std::string& name) const { +std::shared_ptr VfsDirectory::GetFile(std::string_view name) const { const auto& files = GetFiles(); const auto iter = std::find_if(files.begin(), files.end(), [&name](const auto& file1) { return name == file1->GetName(); }); return iter == files.end() ? nullptr : *iter; } -std::shared_ptr VfsDirectory::GetSubdirectory(const std::string& name) const { +std::shared_ptr VfsDirectory::GetSubdirectory(std::string_view name) const { const auto& subs = GetSubdirectories(); const auto iter = std::find_if(subs.begin(), subs.end(), [&name](const auto& file1) { return name == file1->GetName(); }); @@ -128,77 +144,96 @@ size_t VfsDirectory::GetSize() const { return file_total + subdir_total; } -std::shared_ptr VfsDirectory::CreateFileRelative(const std::string& path) { +std::shared_ptr VfsDirectory::CreateFileRelative(std::string_view path) { auto vec = FileUtil::SplitPathComponents(path); vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), vec.end()); - if (vec.empty()) + if (vec.empty()) { return nullptr; - if (vec.size() == 1) + } + + if (vec.size() == 1) { return CreateFile(vec[0]); + } + auto dir = GetSubdirectory(vec[0]); if (dir == nullptr) { dir = CreateSubdirectory(vec[0]); - if (dir == nullptr) + if (dir == nullptr) { return nullptr; + } } return dir->CreateFileRelative(FileUtil::GetPathWithoutTop(path)); } -std::shared_ptr VfsDirectory::CreateFileAbsolute(const std::string& path) { - if (IsRoot()) +std::shared_ptr VfsDirectory::CreateFileAbsolute(std::string_view path) { + if (IsRoot()) { return CreateFileRelative(path); + } + return GetParentDirectory()->CreateFileAbsolute(path); } -std::shared_ptr VfsDirectory::CreateDirectoryRelative(const std::string& path) { +std::shared_ptr VfsDirectory::CreateDirectoryRelative(std::string_view path) { auto vec = FileUtil::SplitPathComponents(path); vec.erase(std::remove_if(vec.begin(), vec.end(), [](const auto& str) { return str.empty(); }), vec.end()); - if (vec.empty()) + if (vec.empty()) { return nullptr; - if (vec.size() == 1) + } + + if (vec.size() == 1) { return CreateSubdirectory(vec[0]); + } + auto dir = GetSubdirectory(vec[0]); if (dir == nullptr) { dir = CreateSubdirectory(vec[0]); - if (dir == nullptr) + if (dir == nullptr) { return nullptr; + } } + return dir->CreateDirectoryRelative(FileUtil::GetPathWithoutTop(path)); } -std::shared_ptr VfsDirectory::CreateDirectoryAbsolute(const std::string& path) { - if (IsRoot()) +std::shared_ptr VfsDirectory::CreateDirectoryAbsolute(std::string_view path) { + if (IsRoot()) { return CreateDirectoryRelative(path); + } + return GetParentDirectory()->CreateDirectoryAbsolute(path); } -bool VfsDirectory::DeleteSubdirectoryRecursive(const std::string& name) { +bool VfsDirectory::DeleteSubdirectoryRecursive(std::string_view name) { auto dir = GetSubdirectory(name); - if (dir == nullptr) + if (dir == nullptr) { return false; + } bool success = true; for (const auto& file : dir->GetFiles()) { - if (!DeleteFile(file->GetName())) + if (!DeleteFile(file->GetName())) { success = false; + } } for (const auto& sdir : dir->GetSubdirectories()) { - if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) + if (!dir->DeleteSubdirectoryRecursive(sdir->GetName())) { success = false; + } } return success; } -bool VfsDirectory::Copy(const std::string& src, const std::string& dest) { +bool VfsDirectory::Copy(std::string_view src, std::string_view dest) { const auto f1 = GetFile(src); auto f2 = CreateFile(dest); - if (f1 == nullptr || f2 == nullptr) + if (f1 == nullptr || f2 == nullptr) { return false; + } if (!f2->Resize(f1->GetSize())) { DeleteFile(dest); @@ -216,23 +251,23 @@ bool ReadOnlyVfsDirectory::IsReadable() const { return true; } -std::shared_ptr ReadOnlyVfsDirectory::CreateSubdirectory(const std::string& name) { +std::shared_ptr ReadOnlyVfsDirectory::CreateSubdirectory(std::string_view name) { return nullptr; } -std::shared_ptr ReadOnlyVfsDirectory::CreateFile(const std::string& name) { +std::shared_ptr ReadOnlyVfsDirectory::CreateFile(std::string_view name) { return nullptr; } -bool ReadOnlyVfsDirectory::DeleteSubdirectory(const std::string& name) { +bool ReadOnlyVfsDirectory::DeleteSubdirectory(std::string_view name) { return false; } -bool ReadOnlyVfsDirectory::DeleteFile(const std::string& name) { +bool ReadOnlyVfsDirectory::DeleteFile(std::string_view name) { return false; } -bool ReadOnlyVfsDirectory::Rename(const std::string& name) { +bool ReadOnlyVfsDirectory::Rename(std::string_view name) { return false; } } // namespace FileSys diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index db3c77eac9..529c6c952d 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -6,11 +6,11 @@ #include #include +#include #include #include #include "boost/optional.hpp" #include "common/common_types.h" -#include "common/file_util.h" namespace FileSys { struct VfsFile; @@ -112,7 +112,7 @@ struct VfsFile : NonCopyable { } // Renames the file to name. Returns whether or not the operation was successsful. - virtual bool Rename(const std::string& name) = 0; + virtual bool Rename(std::string_view name) = 0; }; // A class representing a directory in an abstract filesystem. @@ -121,27 +121,27 @@ struct VfsDirectory : NonCopyable { // Retrives the file located at path as if the current directory was root. Returns nullptr if // not found. - virtual std::shared_ptr GetFileRelative(const std::string& path) const; + virtual std::shared_ptr GetFileRelative(std::string_view path) const; // Calls GetFileRelative(path) on the root of the current directory. - virtual std::shared_ptr GetFileAbsolute(const std::string& path) const; + virtual std::shared_ptr GetFileAbsolute(std::string_view path) const; // Retrives the directory located at path as if the current directory was root. Returns nullptr // if not found. - virtual std::shared_ptr GetDirectoryRelative(const std::string& path) const; + virtual std::shared_ptr GetDirectoryRelative(std::string_view path) const; // Calls GetDirectoryRelative(path) on the root of the current directory. - virtual std::shared_ptr GetDirectoryAbsolute(const std::string& path) const; + virtual std::shared_ptr GetDirectoryAbsolute(std::string_view path) const; // Returns a vector containing all of the files in this directory. virtual std::vector> GetFiles() const = 0; // Returns the file with filename matching name. Returns nullptr if directory dosen't have a // file with name. - virtual std::shared_ptr GetFile(const std::string& name) const; + virtual std::shared_ptr GetFile(std::string_view name) const; // Returns a vector containing all of the subdirectories in this directory. virtual std::vector> GetSubdirectories() const = 0; // Returns the directory with name matching name. Returns nullptr if directory dosen't have a // directory with name. - virtual std::shared_ptr GetSubdirectory(const std::string& name) const; + virtual std::shared_ptr GetSubdirectory(std::string_view name) const; // Returns whether or not the directory can be written to. virtual bool IsWritable() const = 0; @@ -161,49 +161,49 @@ struct VfsDirectory : NonCopyable { // Creates a new subdirectory with name name. Returns a pointer to the new directory or nullptr // if the operation failed. - virtual std::shared_ptr CreateSubdirectory(const std::string& name) = 0; + virtual std::shared_ptr CreateSubdirectory(std::string_view name) = 0; // Creates a new file with name name. Returns a pointer to the new file or nullptr if the // operation failed. - virtual std::shared_ptr CreateFile(const std::string& name) = 0; + virtual std::shared_ptr CreateFile(std::string_view name) = 0; // Creates a new file at the path relative to this directory. Also creates directories if // they do not exist and is supported by this implementation. Returns nullptr on any failure. - virtual std::shared_ptr CreateFileRelative(const std::string& path); + virtual std::shared_ptr CreateFileRelative(std::string_view path); // Creates a new file at the path relative to root of this directory. Also creates directories // if they do not exist and is supported by this implementation. Returns nullptr on any failure. - virtual std::shared_ptr CreateFileAbsolute(const std::string& path); + virtual std::shared_ptr CreateFileAbsolute(std::string_view path); // Creates a new directory at the path relative to this directory. Also creates directories if // they do not exist and is supported by this implementation. Returns nullptr on any failure. - virtual std::shared_ptr CreateDirectoryRelative(const std::string& path); + virtual std::shared_ptr CreateDirectoryRelative(std::string_view path); // Creates a new directory at the path relative to root of this directory. Also creates // directories if they do not exist and is supported by this implementation. Returns nullptr on // any failure. - virtual std::shared_ptr CreateDirectoryAbsolute(const std::string& path); + virtual std::shared_ptr CreateDirectoryAbsolute(std::string_view path); // Deletes the subdirectory with name and returns true on success. - virtual bool DeleteSubdirectory(const std::string& name) = 0; + virtual bool DeleteSubdirectory(std::string_view name) = 0; // Deletes all subdirectories and files of subdirectory with name recirsively and then deletes // the subdirectory. Returns true on success. - virtual bool DeleteSubdirectoryRecursive(const std::string& name); + virtual bool DeleteSubdirectoryRecursive(std::string_view name); // Returnes whether or not the file with name name was deleted successfully. - virtual bool DeleteFile(const std::string& name) = 0; + virtual bool DeleteFile(std::string_view name) = 0; // Returns whether or not this directory was renamed to name. - virtual bool Rename(const std::string& name) = 0; + virtual bool Rename(std::string_view name) = 0; // Returns whether or not the file with name src was successfully copied to a new file with name // dest. - virtual bool Copy(const std::string& src, const std::string& dest); + virtual bool Copy(std::string_view src, std::string_view dest); // Interprets the file with name file instead as a directory of type directory. // The directory must have a constructor that takes a single argument of type // std::shared_ptr. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a // subdirectory in one call. template - bool InterpretAsDirectory(const std::string& file) { + bool InterpretAsDirectory(std::string_view file) { auto file_p = GetFile(file); if (file_p == nullptr) return false; @@ -221,10 +221,10 @@ protected: struct ReadOnlyVfsDirectory : public VfsDirectory { bool IsWritable() const override; bool IsReadable() const override; - std::shared_ptr CreateSubdirectory(const std::string& name) override; - std::shared_ptr CreateFile(const std::string& name) override; - bool DeleteSubdirectory(const std::string& name) override; - bool DeleteFile(const std::string& name) override; - bool Rename(const std::string& name) override; + std::shared_ptr CreateSubdirectory(std::string_view name) override; + std::shared_ptr CreateFile(std::string_view name) override; + bool DeleteSubdirectory(std::string_view name) override; + bool DeleteFile(std::string_view name) override; + bool Rename(std::string_view name) override; }; } // namespace FileSys diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp index 217e022356..a40331cefd 100644 --- a/src/core/file_sys/vfs_offset.cpp +++ b/src/core/file_sys/vfs_offset.cpp @@ -80,7 +80,7 @@ size_t OffsetVfsFile::WriteBytes(const std::vector& data, size_t r_offset) { return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset); } -bool OffsetVfsFile::Rename(const std::string& name) { +bool OffsetVfsFile::Rename(std::string_view name) { return file->Rename(name); } diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h index ded4827f5e..4f471e3ba4 100644 --- a/src/core/file_sys/vfs_offset.h +++ b/src/core/file_sys/vfs_offset.h @@ -4,6 +4,9 @@ #pragma once +#include +#include + #include "core/file_sys/vfs.h" namespace FileSys { @@ -30,7 +33,7 @@ struct OffsetVfsFile : public VfsFile { bool WriteByte(u8 data, size_t offset) override; size_t WriteBytes(const std::vector& data, size_t offset) override; - bool Rename(const std::string& name) override; + bool Rename(std::string_view name) override; size_t GetOffset() const; diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp index 27fd464ae4..095fec77e5 100644 --- a/src/core/file_sys/vfs_real.cpp +++ b/src/core/file_sys/vfs_real.cpp @@ -72,12 +72,15 @@ size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) { return backing.WriteBytes(data, length); } -bool RealVfsFile::Rename(const std::string& name) { - const auto out = FileUtil::Rename(GetName(), name); - path = parent_path + DIR_SEP + name; +bool RealVfsFile::Rename(std::string_view name) { + std::string name_str(name.begin(), name.end()); + const auto out = FileUtil::Rename(GetName(), name_str); + + path = (parent_path + DIR_SEP).append(name); path_components = parent_components; - path_components.push_back(name); + path_components.push_back(std::move(name_str)); backing = FileUtil::IOFile(path, PermissionsToCharArray(perms).c_str()); + return out; } @@ -135,36 +138,54 @@ std::shared_ptr RealVfsDirectory::GetParentDirectory() const { return std::make_shared(parent_path, perms); } -std::shared_ptr RealVfsDirectory::CreateSubdirectory(const std::string& name) { - if (!FileUtil::CreateDir(path + DIR_SEP + name)) +std::shared_ptr RealVfsDirectory::CreateSubdirectory(std::string_view name) { + const std::string subdir_path = (path + DIR_SEP).append(name); + + if (!FileUtil::CreateDir(subdir_path)) { return nullptr; - subdirectories.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + } + + subdirectories.emplace_back(std::make_shared(subdir_path, perms)); return subdirectories.back(); } -std::shared_ptr RealVfsDirectory::CreateFile(const std::string& name) { - if (!FileUtil::CreateEmptyFile(path + DIR_SEP + name)) +std::shared_ptr RealVfsDirectory::CreateFile(std::string_view name) { + const std::string file_path = (path + DIR_SEP).append(name); + + if (!FileUtil::CreateEmptyFile(file_path)) { return nullptr; - files.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + } + + files.emplace_back(std::make_shared(file_path, perms)); return files.back(); } -bool RealVfsDirectory::DeleteSubdirectory(const std::string& name) { - return FileUtil::DeleteDirRecursively(path + DIR_SEP + name); +bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) { + const std::string subdir_path = (path + DIR_SEP).append(name); + + return FileUtil::DeleteDirRecursively(subdir_path); } -bool RealVfsDirectory::DeleteFile(const std::string& name) { - auto file = GetFile(name); - if (file == nullptr) +bool RealVfsDirectory::DeleteFile(std::string_view name) { + const auto file = GetFile(name); + + if (file == nullptr) { return false; + } + files.erase(std::find(files.begin(), files.end(), file)); + auto real_file = std::static_pointer_cast(file); real_file->Close(); - return FileUtil::Delete(path + DIR_SEP + name); + + const std::string file_path = (path + DIR_SEP).append(name); + return FileUtil::Delete(file_path); } -bool RealVfsDirectory::Rename(const std::string& name) { - return FileUtil::Rename(path, parent_path + DIR_SEP + name); +bool RealVfsDirectory::Rename(std::string_view name) { + const std::string new_name = (parent_path + DIR_SEP).append(name); + + return FileUtil::Rename(path, new_name); } bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { diff --git a/src/core/file_sys/vfs_real.h b/src/core/file_sys/vfs_real.h index 5b765a5525..2151211c93 100644 --- a/src/core/file_sys/vfs_real.h +++ b/src/core/file_sys/vfs_real.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "common/file_util.h" #include "core/file_sys/mode.h" #include "core/file_sys/vfs.h" @@ -24,7 +26,7 @@ struct RealVfsFile : public VfsFile { bool IsReadable() const override; size_t Read(u8* data, size_t length, size_t offset) const override; size_t Write(const u8* data, size_t length, size_t offset) override; - bool Rename(const std::string& name) override; + bool Rename(std::string_view name) override; private: bool Close(); @@ -47,11 +49,11 @@ struct RealVfsDirectory : public VfsDirectory { bool IsReadable() const override; std::string GetName() const override; std::shared_ptr GetParentDirectory() const override; - std::shared_ptr CreateSubdirectory(const std::string& name) override; - std::shared_ptr CreateFile(const std::string& name) override; - bool DeleteSubdirectory(const std::string& name) override; - bool DeleteFile(const std::string& name) override; - bool Rename(const std::string& name) override; + std::shared_ptr CreateSubdirectory(std::string_view name) override; + std::shared_ptr CreateFile(std::string_view name) override; + bool DeleteSubdirectory(std::string_view name) override; + bool DeleteFile(std::string_view name) override; + bool Rename(std::string_view name) override; protected: bool ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) override; diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp index 671e0b8d0e..dbfe06cbc8 100644 --- a/src/core/hle/service/filesystem/filesystem.cpp +++ b/src/core/hle/service/filesystem/filesystem.cpp @@ -24,7 +24,7 @@ namespace Service::FileSystem { constexpr u64 EMULATED_SD_REPORTED_SIZE = 32000000000; static FileSys::VirtualDir GetDirectoryRelativeWrapped(FileSys::VirtualDir base, - const std::string& dir_name) { + std::string_view dir_name) { if (dir_name.empty() || dir_name == "." || dir_name == "/" || dir_name == "\\") return base; diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index e70f376777..4cbd9e2859 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -49,7 +49,8 @@ FileType GuessFromFilename(const std::string& name) { if (name == "main") return FileType::DeconstructedRomDirectory; - const std::string extension = Common::ToLower(FileUtil::GetExtensionFromFilename(name)); + const std::string extension = + Common::ToLower(std::string(FileUtil::GetExtensionFromFilename(name))); if (extension == "elf") return FileType::ELF; From 0081252d317af44b3e810d7f31fdd3fb03b8e23c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 22 Jul 2018 02:54:07 -0400 Subject: [PATCH 2/2] vfs: Correct file_p variable usage within InterpretAsDirectory() ReplaceFileWithSubdirectory() takes a VirtualFile and a VirtualDir, but it was being passed a string as one of its arguments. The only reason this never caused issues is because this template isn't instantiated anywhere yet. This corrects an issue before it occurs. --- src/core/file_sys/vfs.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index 529c6c952d..4a13b8378e 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -205,9 +205,12 @@ struct VfsDirectory : NonCopyable { template bool InterpretAsDirectory(std::string_view file) { auto file_p = GetFile(file); - if (file_p == nullptr) + + if (file_p == nullptr) { return false; - return ReplaceFileWithSubdirectory(file, std::make_shared(file_p)); + } + + return ReplaceFileWithSubdirectory(file_p, std::make_shared(file_p)); } protected: