diff options
author | Lioncash <mathew1800@gmail.com> | 2018-07-22 07:23:29 +0200 |
---|---|---|
committer | Lioncash <mathew1800@gmail.com> | 2018-07-22 09:22:21 +0200 |
commit | 398444e67635c158a20301d1a32a1c45bfdd4056 (patch) | |
tree | db638ebe09199ae64b69f5911136effa9756571f /src/core/file_sys | |
parent | Merge pull request #764 from lioncash/move (diff) | |
download | yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.gz yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.bz2 yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.lz yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.xz yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.tar.zst yuzu-398444e67635c158a20301d1a32a1c45bfdd4056.zip |
Diffstat (limited to 'src/core/file_sys')
-rw-r--r-- | src/core/file_sys/vfs.cpp | 111 | ||||
-rw-r--r-- | src/core/file_sys/vfs.h | 50 | ||||
-rw-r--r-- | src/core/file_sys/vfs_offset.cpp | 2 | ||||
-rw-r--r-- | src/core/file_sys/vfs_offset.h | 5 | ||||
-rw-r--r-- | src/core/file_sys/vfs_real.cpp | 57 | ||||
-rw-r--r-- | src/core/file_sys/vfs_real.h | 14 |
6 files changed, 150 insertions, 89 deletions
diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index 3f690f12a..b99a4fd5b 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<u8>& data, size_t offset) { return Write(data.data(), data.size(), offset); } -std::shared_ptr<VfsFile> VfsDirectory::GetFileRelative(const std::string& path) const { +std::shared_ptr<VfsFile> 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<VfsFile> VfsDirectory::GetFileAbsolute(const std::string& path) const { - if (IsRoot()) +std::shared_ptr<VfsFile> VfsDirectory::GetFileAbsolute(std::string_view path) const { + if (IsRoot()) { return GetFileRelative(path); + } return GetParentDirectory()->GetFileAbsolute(path); } -std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryRelative(const std::string& path) const { +std::shared_ptr<VfsDirectory> 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> VfsDirectory::GetDirectoryAbsolute(const std::string& path) const { - if (IsRoot()) +std::shared_ptr<VfsDirectory> VfsDirectory::GetDirectoryAbsolute(std::string_view path) const { + if (IsRoot()) { return GetDirectoryRelative(path); + } return GetParentDirectory()->GetDirectoryAbsolute(path); } -std::shared_ptr<VfsFile> VfsDirectory::GetFile(const std::string& name) const { +std::shared_ptr<VfsFile> 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> VfsDirectory::GetSubdirectory(const std::string& name) const { +std::shared_ptr<VfsDirectory> 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<VfsFile> VfsDirectory::CreateFileRelative(const std::string& path) { +std::shared_ptr<VfsFile> 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<VfsFile> VfsDirectory::CreateFileAbsolute(const std::string& path) { - if (IsRoot()) +std::shared_ptr<VfsFile> VfsDirectory::CreateFileAbsolute(std::string_view path) { + if (IsRoot()) { return CreateFileRelative(path); + } + return GetParentDirectory()->CreateFileAbsolute(path); } -std::shared_ptr<VfsDirectory> VfsDirectory::CreateDirectoryRelative(const std::string& path) { +std::shared_ptr<VfsDirectory> 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> VfsDirectory::CreateDirectoryAbsolute(const std::string& path) { - if (IsRoot()) +std::shared_ptr<VfsDirectory> 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<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(const std::string& name) { +std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectory::CreateSubdirectory(std::string_view name) { return nullptr; } -std::shared_ptr<VfsFile> ReadOnlyVfsDirectory::CreateFile(const std::string& name) { +std::shared_ptr<VfsFile> 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 db3c77eac..529c6c952 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -6,11 +6,11 @@ #include <memory> #include <string> +#include <string_view> #include <type_traits> #include <vector> #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<VfsFile> GetFileRelative(const std::string& path) const; + virtual std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const; // Calls GetFileRelative(path) on the root of the current directory. - virtual std::shared_ptr<VfsFile> GetFileAbsolute(const std::string& path) const; + virtual std::shared_ptr<VfsFile> 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<VfsDirectory> GetDirectoryRelative(const std::string& path) const; + virtual std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const; // Calls GetDirectoryRelative(path) on the root of the current directory. - virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(const std::string& path) const; + virtual std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(std::string_view path) const; // Returns a vector containing all of the files in this directory. virtual std::vector<std::shared_ptr<VfsFile>> 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<VfsFile> GetFile(const std::string& name) const; + virtual std::shared_ptr<VfsFile> GetFile(std::string_view name) const; // Returns a vector containing all of the subdirectories in this directory. virtual std::vector<std::shared_ptr<VfsDirectory>> 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<VfsDirectory> GetSubdirectory(const std::string& name) const; + virtual std::shared_ptr<VfsDirectory> 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<VfsDirectory> CreateSubdirectory(const std::string& name) = 0; + virtual std::shared_ptr<VfsDirectory> 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<VfsFile> CreateFile(const std::string& name) = 0; + virtual std::shared_ptr<VfsFile> 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<VfsFile> CreateFileRelative(const std::string& path); + virtual std::shared_ptr<VfsFile> 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<VfsFile> CreateFileAbsolute(const std::string& path); + virtual std::shared_ptr<VfsFile> 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<VfsDirectory> CreateDirectoryRelative(const std::string& path); + virtual std::shared_ptr<VfsDirectory> 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<VfsDirectory> CreateDirectoryAbsolute(const std::string& path); + virtual std::shared_ptr<VfsDirectory> 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<VfsFile>. Allows to reinterpret container files (i.e NCA, zip, XCI, etc) as a // subdirectory in one call. template <typename Directory> - 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<VfsDirectory> CreateSubdirectory(const std::string& name) override; - std::shared_ptr<VfsFile> 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<VfsDirectory> CreateSubdirectory(std::string_view name) override; + std::shared_ptr<VfsFile> 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 217e02235..a40331cef 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<u8>& 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 ded4827f5..4f471e3ba 100644 --- a/src/core/file_sys/vfs_offset.h +++ b/src/core/file_sys/vfs_offset.h @@ -4,6 +4,9 @@ #pragma once +#include <memory> +#include <string_view> + #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<u8>& 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 27fd464ae..095fec77e 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<VfsDirectory> RealVfsDirectory::GetParentDirectory() const { return std::make_shared<RealVfsDirectory>(parent_path, perms); } -std::shared_ptr<VfsDirectory> RealVfsDirectory::CreateSubdirectory(const std::string& name) { - if (!FileUtil::CreateDir(path + DIR_SEP + name)) +std::shared_ptr<VfsDirectory> 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<RealVfsDirectory>(path + DIR_SEP + name, perms)); + } + + subdirectories.emplace_back(std::make_shared<RealVfsDirectory>(subdir_path, perms)); return subdirectories.back(); } -std::shared_ptr<VfsFile> RealVfsDirectory::CreateFile(const std::string& name) { - if (!FileUtil::CreateEmptyFile(path + DIR_SEP + name)) +std::shared_ptr<VfsFile> 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<RealVfsFile>(path + DIR_SEP + name, perms)); + } + + files.emplace_back(std::make_shared<RealVfsFile>(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<RealVfsFile>(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 5b765a552..2151211c9 100644 --- a/src/core/file_sys/vfs_real.h +++ b/src/core/file_sys/vfs_real.h @@ -4,6 +4,8 @@ #pragma once +#include <string_view> + #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<VfsDirectory> GetParentDirectory() const override; - std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) override; - std::shared_ptr<VfsFile> 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<VfsDirectory> CreateSubdirectory(std::string_view name) override; + std::shared_ptr<VfsFile> 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; |