fs: Replace Mode enum by OpenMode enum

This commit is contained in:
FearlessTobi
2024-01-18 21:31:41 +01:00
committed by Liam
parent 0f9288e38d
commit cc09c265e1
21 changed files with 195 additions and 188 deletions

View File

@ -5,7 +5,6 @@
#include <numeric>
#include <string>
#include "common/fs/path_util.h"
#include "core/file_sys/mode.h"
#include "core/file_sys/vfs/vfs.h"
namespace FileSys {
@ -36,12 +35,12 @@ VfsEntryType VfsFilesystem::GetEntryType(std::string_view path_) const {
return VfsEntryType::None;
}
VirtualFile VfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
VirtualFile VfsFilesystem::OpenFile(std::string_view path_, OpenMode perms) {
const auto path = Common::FS::SanitizePath(path_);
return root->GetFileRelative(path);
}
VirtualFile VfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
VirtualFile VfsFilesystem::CreateFile(std::string_view path_, OpenMode perms) {
const auto path = Common::FS::SanitizePath(path_);
return root->CreateFileRelative(path);
}
@ -54,17 +53,17 @@ VirtualFile VfsFilesystem::CopyFile(std::string_view old_path_, std::string_view
if (Common::FS::GetParentPath(old_path) == Common::FS::GetParentPath(new_path)) {
if (!root->Copy(Common::FS::GetFilename(old_path), Common::FS::GetFilename(new_path)))
return nullptr;
return OpenFile(new_path, Mode::ReadWrite);
return OpenFile(new_path, OpenMode::ReadWrite);
}
// Do it using RawCopy. Non-default impls are encouraged to optimize this.
const auto old_file = OpenFile(old_path, Mode::Read);
const auto old_file = OpenFile(old_path, OpenMode::Read);
if (old_file == nullptr)
return nullptr;
auto new_file = OpenFile(new_path, Mode::Read);
auto new_file = OpenFile(new_path, OpenMode::Read);
if (new_file != nullptr)
return nullptr;
new_file = CreateFile(new_path, Mode::Write);
new_file = CreateFile(new_path, OpenMode::Write);
if (new_file == nullptr)
return nullptr;
if (!VfsRawCopy(old_file, new_file))
@ -87,18 +86,18 @@ VirtualFile VfsFilesystem::MoveFile(std::string_view old_path, std::string_view
bool VfsFilesystem::DeleteFile(std::string_view path_) {
const auto path = Common::FS::SanitizePath(path_);
auto parent = OpenDirectory(Common::FS::GetParentPath(path), Mode::Write);
auto parent = OpenDirectory(Common::FS::GetParentPath(path), OpenMode::Write);
if (parent == nullptr)
return false;
return parent->DeleteFile(Common::FS::GetFilename(path));
}
VirtualDir VfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
VirtualDir VfsFilesystem::OpenDirectory(std::string_view path_, OpenMode perms) {
const auto path = Common::FS::SanitizePath(path_);
return root->GetDirectoryRelative(path);
}
VirtualDir VfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
VirtualDir VfsFilesystem::CreateDirectory(std::string_view path_, OpenMode perms) {
const auto path = Common::FS::SanitizePath(path_);
return root->CreateDirectoryRelative(path);
}
@ -108,13 +107,13 @@ VirtualDir VfsFilesystem::CopyDirectory(std::string_view old_path_, std::string_
const auto new_path = Common::FS::SanitizePath(new_path_);
// Non-default impls are highly encouraged to provide a more optimized version of this.
auto old_dir = OpenDirectory(old_path, Mode::Read);
auto old_dir = OpenDirectory(old_path, OpenMode::Read);
if (old_dir == nullptr)
return nullptr;
auto new_dir = OpenDirectory(new_path, Mode::Read);
auto new_dir = OpenDirectory(new_path, OpenMode::Read);
if (new_dir != nullptr)
return nullptr;
new_dir = CreateDirectory(new_path, Mode::Write);
new_dir = CreateDirectory(new_path, OpenMode::Write);
if (new_dir == nullptr)
return nullptr;
@ -149,7 +148,7 @@ VirtualDir VfsFilesystem::MoveDirectory(std::string_view old_path, std::string_v
bool VfsFilesystem::DeleteDirectory(std::string_view path_) {
const auto path = Common::FS::SanitizePath(path_);
auto parent = OpenDirectory(Common::FS::GetParentPath(path), Mode::Write);
auto parent = OpenDirectory(Common::FS::GetParentPath(path), OpenMode::Write);
if (parent == nullptr)
return false;
return parent->DeleteSubdirectoryRecursive(Common::FS::GetFilename(path));

View File

@ -13,12 +13,11 @@
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "core/file_sys/fs_filesystem.h"
#include "core/file_sys/vfs/vfs_types.h"
namespace FileSys {
enum class Mode : u32;
// An enumeration representing what can be at the end of a path in a VfsFilesystem
enum class VfsEntryType {
None,
@ -49,9 +48,9 @@ public:
virtual VfsEntryType GetEntryType(std::string_view path) const;
// Opens the file with path relative to root. If it doesn't exist, returns nullptr.
virtual VirtualFile OpenFile(std::string_view path, Mode perms);
virtual VirtualFile OpenFile(std::string_view path, OpenMode perms);
// Creates a new, empty file at path
virtual VirtualFile CreateFile(std::string_view path, Mode perms);
virtual VirtualFile CreateFile(std::string_view path, OpenMode perms);
// Copies the file from old_path to new_path, returning the new file on success and nullptr on
// failure.
virtual VirtualFile CopyFile(std::string_view old_path, std::string_view new_path);
@ -62,9 +61,9 @@ public:
virtual bool DeleteFile(std::string_view path);
// Opens the directory with path relative to root. If it doesn't exist, returns nullptr.
virtual VirtualDir OpenDirectory(std::string_view path, Mode perms);
virtual VirtualDir OpenDirectory(std::string_view path, OpenMode perms);
// Creates a new, empty directory at path
virtual VirtualDir CreateDirectory(std::string_view path, Mode perms);
virtual VirtualDir CreateDirectory(std::string_view path, OpenMode perms);
// Copies the directory from old_path to new_path, returning the new directory on success and
// nullptr on failure.
virtual VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path);

View File

@ -28,16 +28,14 @@ namespace {
constexpr size_t MaxOpenFiles = 512;
constexpr FS::FileAccessMode ModeFlagsToFileAccessMode(Mode mode) {
constexpr FS::FileAccessMode ModeFlagsToFileAccessMode(OpenMode mode) {
switch (mode) {
case Mode::Read:
case OpenMode::Read:
return FS::FileAccessMode::Read;
case Mode::Write:
case Mode::ReadWrite:
case Mode::Append:
case Mode::ReadAppend:
case Mode::WriteAppend:
case Mode::All:
case OpenMode::Write:
case OpenMode::ReadWrite:
case OpenMode::AllowAppend:
case OpenMode::All:
return FS::FileAccessMode::ReadWrite;
default:
return {};
@ -74,7 +72,7 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
}
VirtualFile RealVfsFilesystem::OpenFileFromEntry(std::string_view path_, std::optional<u64> size,
Mode perms) {
OpenMode perms) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
std::scoped_lock lk{list_lock};
@ -98,11 +96,11 @@ VirtualFile RealVfsFilesystem::OpenFileFromEntry(std::string_view path_, std::op
return file;
}
VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, OpenMode perms) {
return OpenFileFromEntry(path_, {}, perms);
}
VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, OpenMode perms) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
{
std::scoped_lock lk{list_lock};
@ -145,7 +143,7 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_
if (!FS::RenameFile(old_path, new_path)) {
return nullptr;
}
return OpenFile(new_path, Mode::ReadWrite);
return OpenFile(new_path, OpenMode::ReadWrite);
}
bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
@ -157,12 +155,12 @@ bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
return FS::RemoveFile(path);
}
VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, OpenMode perms) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
}
VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, OpenMode perms) {
const auto path = FS::SanitizePath(path_, FS::DirectorySeparator::PlatformDefault);
if (!FS::CreateDirs(path)) {
return nullptr;
@ -184,7 +182,7 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
if (!FS::RenameDir(old_path, new_path)) {
return nullptr;
}
return OpenDirectory(new_path, Mode::ReadWrite);
return OpenDirectory(new_path, OpenMode::ReadWrite);
}
bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
@ -193,7 +191,7 @@ bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
}
std::unique_lock<std::mutex> RealVfsFilesystem::RefreshReference(const std::string& path,
Mode perms,
OpenMode perms,
FileReference& reference) {
std::unique_lock lk{list_lock};
@ -266,7 +264,7 @@ void RealVfsFilesystem::RemoveReferenceFromListLocked(FileReference& reference)
}
RealVfsFile::RealVfsFile(RealVfsFilesystem& base_, std::unique_ptr<FileReference> reference_,
const std::string& path_, Mode perms_, std::optional<u64> size_)
const std::string& path_, OpenMode perms_, std::optional<u64> size_)
: base(base_), reference(std::move(reference_)), path(path_),
parent_path(FS::GetParentPath(path_)), path_components(FS::SplitPathComponentsCopy(path_)),
size(size_), perms(perms_) {}
@ -298,11 +296,11 @@ VirtualDir RealVfsFile::GetContainingDirectory() const {
}
bool RealVfsFile::IsWritable() const {
return True(perms & Mode::Write);
return True(perms & OpenMode::Write);
}
bool RealVfsFile::IsReadable() const {
return True(perms & Mode::Read);
return True(perms & OpenMode::Read);
}
std::size_t RealVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
@ -331,7 +329,7 @@ bool RealVfsFile::Rename(std::string_view name) {
template <>
std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>() const {
if (perms == Mode::Append) {
if (perms == OpenMode::AllowAppend) {
return {};
}
@ -353,7 +351,7 @@ std::vector<VirtualFile> RealVfsDirectory::IterateEntries<RealVfsFile, VfsFile>(
template <>
std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDirectory>() const {
if (perms == Mode::Append) {
if (perms == OpenMode::AllowAppend) {
return {};
}
@ -373,10 +371,11 @@ std::vector<VirtualDir> RealVfsDirectory::IterateEntries<RealVfsDirectory, VfsDi
return out;
}
RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_, Mode perms_)
RealVfsDirectory::RealVfsDirectory(RealVfsFilesystem& base_, const std::string& path_,
OpenMode perms_)
: base(base_), path(FS::RemoveTrailingSlash(path_)), parent_path(FS::GetParentPath(path)),
path_components(FS::SplitPathComponentsCopy(path)), perms(perms_) {
if (!FS::Exists(path) && True(perms & Mode::Write)) {
if (!FS::Exists(path) && True(perms & OpenMode::Write)) {
void(FS::CreateDirs(path));
}
}
@ -456,11 +455,11 @@ std::vector<VirtualDir> RealVfsDirectory::GetSubdirectories() const {
}
bool RealVfsDirectory::IsWritable() const {
return True(perms & Mode::Write);
return True(perms & OpenMode::Write);
}
bool RealVfsDirectory::IsReadable() const {
return True(perms & Mode::Read);
return True(perms & OpenMode::Read);
}
std::string RealVfsDirectory::GetName() const {
@ -507,7 +506,7 @@ std::string RealVfsDirectory::GetFullPath() const {
}
std::map<std::string, VfsEntryType, std::less<>> RealVfsDirectory::GetEntries() const {
if (perms == Mode::Append) {
if (perms == OpenMode::AllowAppend) {
return {};
}

View File

@ -8,7 +8,7 @@
#include <optional>
#include <string_view>
#include "common/intrusive_list.h"
#include "core/file_sys/mode.h"
#include "core/file_sys/fs_filesystem.h"
#include "core/file_sys/vfs/vfs.h"
namespace Common::FS {
@ -33,13 +33,14 @@ public:
bool IsReadable() const override;
bool IsWritable() const override;
VfsEntryType GetEntryType(std::string_view path) const override;
VirtualFile OpenFile(std::string_view path, Mode perms = Mode::Read) override;
VirtualFile CreateFile(std::string_view path, Mode perms = Mode::ReadWrite) override;
VirtualFile OpenFile(std::string_view path, OpenMode perms = OpenMode::Read) override;
VirtualFile CreateFile(std::string_view path, OpenMode perms = OpenMode::ReadWrite) override;
VirtualFile CopyFile(std::string_view old_path, std::string_view new_path) override;
VirtualFile MoveFile(std::string_view old_path, std::string_view new_path) override;
bool DeleteFile(std::string_view path) override;
VirtualDir OpenDirectory(std::string_view path, Mode perms = Mode::Read) override;
VirtualDir CreateDirectory(std::string_view path, Mode perms = Mode::ReadWrite) override;
VirtualDir OpenDirectory(std::string_view path, OpenMode perms = OpenMode::Read) override;
VirtualDir CreateDirectory(std::string_view path,
OpenMode perms = OpenMode::ReadWrite) override;
VirtualDir CopyDirectory(std::string_view old_path, std::string_view new_path) override;
VirtualDir MoveDirectory(std::string_view old_path, std::string_view new_path) override;
bool DeleteDirectory(std::string_view path) override;
@ -54,14 +55,14 @@ private:
private:
friend class RealVfsFile;
std::unique_lock<std::mutex> RefreshReference(const std::string& path, Mode perms,
std::unique_lock<std::mutex> RefreshReference(const std::string& path, OpenMode perms,
FileReference& reference);
void DropReference(std::unique_ptr<FileReference>&& reference);
private:
friend class RealVfsDirectory;
VirtualFile OpenFileFromEntry(std::string_view path, std::optional<u64> size,
Mode perms = Mode::Read);
OpenMode perms = OpenMode::Read);
private:
void EvictSingleReferenceLocked();
@ -89,7 +90,8 @@ public:
private:
RealVfsFile(RealVfsFilesystem& base, std::unique_ptr<FileReference> reference,
const std::string& path, Mode perms = Mode::Read, std::optional<u64> size = {});
const std::string& path, OpenMode perms = OpenMode::Read,
std::optional<u64> size = {});
RealVfsFilesystem& base;
std::unique_ptr<FileReference> reference;
@ -97,7 +99,7 @@ private:
std::string parent_path;
std::vector<std::string> path_components;
std::optional<u64> size;
Mode perms;
OpenMode perms;
};
// An implementation of VfsDirectory that represents a directory on the user's computer.
@ -130,7 +132,8 @@ public:
std::map<std::string, VfsEntryType, std::less<>> GetEntries() const override;
private:
RealVfsDirectory(RealVfsFilesystem& base, const std::string& path, Mode perms = Mode::Read);
RealVfsDirectory(RealVfsFilesystem& base, const std::string& path,
OpenMode perms = OpenMode::Read);
template <typename T, typename R>
std::vector<std::shared_ptr<R>> IterateEntries() const;
@ -139,7 +142,7 @@ private:
std::string path;
std::string parent_path;
std::vector<std::string> path_components;
Mode perms;
OpenMode perms;
};
} // namespace FileSys