vfs: Add readonly layer class

Takes a VirtualDir/File and makes its API read only.
This commit is contained in:
Zach Hilman
2019-10-01 09:04:10 -04:00
parent a0702e2db3
commit 4f163cf77f
3 changed files with 256 additions and 0 deletions

View File

@@ -86,6 +86,8 @@ add_library(core STATIC
file_sys/vfs_offset.h
file_sys/vfs_real.cpp
file_sys/vfs_real.h
file_sys/vfs_ro_layer.cpp
file_sys/vfs_ro_layer.h
file_sys/vfs_static.h
file_sys/vfs_types.h
file_sys/vfs_vector.cpp

View File

@@ -0,0 +1,181 @@
// Copyright 2019 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "core/file_sys/vfs_ro_layer.h"
namespace FileSys {
ReadOnlyVfsFileLayer::ReadOnlyVfsFileLayer(VirtualFile base) : base(std::move(base)) {}
ReadOnlyVfsFileLayer::~ReadOnlyVfsFileLayer() = default;
std::string ReadOnlyVfsFileLayer::GetName() const {
return base->GetName();
}
std::size_t ReadOnlyVfsFileLayer::GetSize() const {
return base->GetSize();
}
bool ReadOnlyVfsFileLayer::Resize(std::size_t new_size) {
return false;
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsFileLayer::GetContainingDirectory() const {
// Make containing read-only to prevent escaping the layer by getting containing and then
// getting this file again.
return std::make_shared<ReadOnlyVfsDirectoryLayer>(base->GetContainingDirectory());
}
bool ReadOnlyVfsFileLayer::IsWritable() const {
return false;
}
bool ReadOnlyVfsFileLayer::IsReadable() const {
return base->IsReadable();
}
std::size_t ReadOnlyVfsFileLayer::Read(u8* data, std::size_t length, std::size_t offset) const {
return base->Read(data, length, offset);
}
std::size_t ReadOnlyVfsFileLayer::Write(const u8* data, std::size_t length, std::size_t offset) {
return 0;
}
bool ReadOnlyVfsFileLayer::Rename(std::string_view name) {
return false;
}
std::string ReadOnlyVfsFileLayer::GetFullPath() const {
return base->GetFullPath();
}
ReadOnlyVfsDirectoryLayer::ReadOnlyVfsDirectoryLayer(VirtualDir base) : base(std::move(base)) {}
ReadOnlyVfsDirectoryLayer::~ReadOnlyVfsDirectoryLayer() = default;
std::vector<std::shared_ptr<VfsFile>> ReadOnlyVfsDirectoryLayer::GetFiles() const {
std::vector<VirtualFile> out;
const auto in = base->GetFiles();
std::transform(in.begin(), in.end(), std::back_inserter(out),
[](const VirtualFile& i) { return std::make_shared<ReadOnlyVfsFileLayer>(i); });
return out;
}
std::vector<std::shared_ptr<VfsDirectory>> ReadOnlyVfsDirectoryLayer::GetSubdirectories() const {
std::vector<VirtualDir> out;
const auto in = base->GetSubdirectories();
std::transform(in.begin(), in.end(), std::back_inserter(out), [](const VirtualDir& i) {
return std::make_shared<ReadOnlyVfsDirectoryLayer>(i);
});
return out;
}
std::string ReadOnlyVfsDirectoryLayer::GetName() const {
return base->GetName();
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::GetParentDirectory() const {
return std::make_shared<ReadOnlyVfsDirectoryLayer>(base->GetParentDirectory());
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::GetFileRelative(std::string_view path) const {
return std::make_shared<ReadOnlyVfsFileLayer>(base->GetFileRelative(path));
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::GetFileAbsolute(std::string_view path) const {
return std::make_shared<ReadOnlyVfsFileLayer>(base->GetFileAbsolute(path));
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::GetDirectoryRelative(
std::string_view path) const {
return std::make_shared<ReadOnlyVfsDirectoryLayer>(base->GetDirectoryRelative(path));
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::GetDirectoryAbsolute(
std::string_view path) const {
return std::make_shared<ReadOnlyVfsDirectoryLayer>(base->GetDirectoryAbsolute(path));
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::GetFile(std::string_view name) const {
return std::make_shared<ReadOnlyVfsFileLayer>(base->GetFile(name));
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::GetSubdirectory(
std::string_view name) const {
return std::make_shared<ReadOnlyVfsDirectoryLayer>(base->GetSubdirectory(name));
}
bool ReadOnlyVfsDirectoryLayer::IsRoot() const {
return base->IsRoot();
}
std::size_t ReadOnlyVfsDirectoryLayer::GetSize() const {
return base->GetSize();
}
bool ReadOnlyVfsDirectoryLayer::Copy(std::string_view src, std::string_view dest) {
return false;
}
std::string ReadOnlyVfsDirectoryLayer::GetFullPath() const {
return base->GetFullPath();
}
bool ReadOnlyVfsDirectoryLayer::IsWritable() const {
return false;
}
bool ReadOnlyVfsDirectoryLayer::IsReadable() const {
return base->IsReadable();
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::CreateSubdirectory(std::string_view name) {
return nullptr;
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::CreateFile(std::string_view name) {
return nullptr;
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::CreateFileAbsolute(std::string_view path) {
return nullptr;
}
std::shared_ptr<VfsFile> ReadOnlyVfsDirectoryLayer::CreateFileRelative(std::string_view path) {
return nullptr;
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::CreateDirectoryAbsolute(
std::string_view path) {
return nullptr;
}
std::shared_ptr<VfsDirectory> ReadOnlyVfsDirectoryLayer::CreateDirectoryRelative(
std::string_view path) {
return nullptr;
}
bool ReadOnlyVfsDirectoryLayer::DeleteSubdirectory(std::string_view name) {
return false;
}
bool ReadOnlyVfsDirectoryLayer::DeleteSubdirectoryRecursive(std::string_view name) {
return false;
}
bool ReadOnlyVfsDirectoryLayer::CleanSubdirectoryRecursive(std::string_view name) {
return false;
}
bool ReadOnlyVfsDirectoryLayer::DeleteFile(std::string_view name) {
return false;
}
bool ReadOnlyVfsDirectoryLayer::Rename(std::string_view name) {
return false;
}
} // namespace FileSys

View File

@@ -0,0 +1,73 @@
// Copyright 2019 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include "core/file_sys/vfs.h"
namespace FileSys {
// Class that wraps a VfsFile making it read-only
class ReadOnlyVfsFileLayer : public VfsFile {
public:
explicit ReadOnlyVfsFileLayer(VirtualFile base);
~ReadOnlyVfsFileLayer() override;
std::string GetName() const override;
std::size_t GetSize() const override;
bool Resize(std::size_t new_size) override;
std::shared_ptr<VfsDirectory> GetContainingDirectory() const override;
bool IsWritable() const override;
bool IsReadable() const override;
std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override;
std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override;
bool Rename(std::string_view name) override;
std::string GetFullPath() const override;
private:
VirtualFile base;
};
// Class that wraps a VfsDirectory making it and its children read only.
class ReadOnlyVfsDirectoryLayer : public ReadOnlyVfsDirectory {
public:
explicit ReadOnlyVfsDirectoryLayer(VirtualDir base);
~ReadOnlyVfsDirectoryLayer() override;
std::vector<std::shared_ptr<VfsFile>> GetFiles() const override;
std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() const override;
std::string GetName() const override;
std::shared_ptr<VfsDirectory> GetParentDirectory() const override;
std::shared_ptr<VfsFile> GetFileRelative(std::string_view path) const override;
std::shared_ptr<VfsFile> GetFileAbsolute(std::string_view path) const override;
std::shared_ptr<VfsDirectory> GetDirectoryRelative(std::string_view path) const override;
std::shared_ptr<VfsDirectory> GetDirectoryAbsolute(std::string_view path) const override;
std::shared_ptr<VfsFile> GetFile(std::string_view name) const override;
std::shared_ptr<VfsDirectory> GetSubdirectory(std::string_view name) const override;
bool IsRoot() const override;
std::size_t GetSize() const override;
bool Copy(std::string_view src, std::string_view dest) override;
std::string GetFullPath() const override;
bool IsWritable() const override;
bool IsReadable() const override;
std::shared_ptr<VfsDirectory> CreateSubdirectory(std::string_view name) override;
std::shared_ptr<VfsFile> CreateFile(std::string_view name) override;
std::shared_ptr<VfsFile> CreateFileAbsolute(std::string_view path) override;
std::shared_ptr<VfsFile> CreateFileRelative(std::string_view path) override;
std::shared_ptr<VfsDirectory> CreateDirectoryAbsolute(std::string_view path) override;
std::shared_ptr<VfsDirectory> CreateDirectoryRelative(std::string_view path) override;
bool DeleteSubdirectory(std::string_view name) override;
bool DeleteSubdirectoryRecursive(std::string_view name) override;
bool CleanSubdirectoryRecursive(std::string_view name) override;
bool DeleteFile(std::string_view name) override;
bool Rename(std::string_view name) override;
private:
VirtualDir base;
};
} // namespace FileSys