2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-19 01:07:11 +00:00
|
|
|
|
2018-07-21 02:02:53 +00:00
|
|
|
#include <algorithm>
|
2018-07-20 04:04:51 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
#include "core/file_sys/vfs_offset.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2020-12-10 06:31:58 +00:00
|
|
|
OffsetVfsFile::OffsetVfsFile(VirtualFile file_, std::size_t size_, std::size_t offset_,
|
2018-07-27 22:14:03 +00:00
|
|
|
std::string name_, VirtualDir parent_)
|
|
|
|
: file(file_), offset(offset_), size(size_), name(std::move(name_)),
|
|
|
|
parent(parent_ == nullptr ? file->GetContainingDirectory() : std::move(parent_)) {}
|
2018-07-19 01:07:11 +00:00
|
|
|
|
2018-09-19 23:19:05 +00:00
|
|
|
OffsetVfsFile::~OffsetVfsFile() = default;
|
|
|
|
|
2018-07-19 01:07:11 +00:00
|
|
|
std::string OffsetVfsFile::GetName() const {
|
|
|
|
return name.empty() ? file->GetName() : name;
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::GetSize() const {
|
2018-07-19 01:07:11 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
bool OffsetVfsFile::Resize(std::size_t new_size) {
|
2018-07-19 01:07:11 +00:00
|
|
|
if (offset + new_size < file->GetSize()) {
|
|
|
|
size = new_size;
|
|
|
|
} else {
|
|
|
|
auto res = file->Resize(offset + new_size);
|
|
|
|
if (!res)
|
|
|
|
return false;
|
|
|
|
size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-10 06:31:58 +00:00
|
|
|
VirtualDir OffsetVfsFile::GetContainingDirectory() const {
|
2018-07-27 22:14:03 +00:00
|
|
|
return parent;
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::IsWritable() const {
|
|
|
|
return file->IsWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::IsReadable() const {
|
|
|
|
return file->IsReadable();
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::Read(u8* data, std::size_t length, std::size_t r_offset) const {
|
2018-07-19 01:07:11 +00:00
|
|
|
return file->Read(data, TrimToFit(length, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::Write(const u8* data, std::size_t length, std::size_t r_offset) {
|
2018-07-19 01:07:11 +00:00
|
|
|
return file->Write(data, TrimToFit(length, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<u8> OffsetVfsFile::ReadByte(std::size_t r_offset) const {
|
2020-09-22 21:31:53 +00:00
|
|
|
if (r_offset >= size) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-07-19 01:07:11 +00:00
|
|
|
|
2020-09-22 21:31:53 +00:00
|
|
|
return file->ReadByte(offset + r_offset);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::vector<u8> OffsetVfsFile::ReadBytes(std::size_t r_size, std::size_t r_offset) const {
|
2018-07-19 01:07:11 +00:00
|
|
|
return file->ReadBytes(TrimToFit(r_size, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> OffsetVfsFile::ReadAllBytes() const {
|
|
|
|
return file->ReadBytes(size, offset);
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
bool OffsetVfsFile::WriteByte(u8 data, std::size_t r_offset) {
|
2018-07-19 01:07:11 +00:00
|
|
|
if (r_offset < size)
|
|
|
|
return file->WriteByte(data, offset + r_offset);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::WriteBytes(const std::vector<u8>& data, std::size_t r_offset) {
|
2018-07-19 01:07:11 +00:00
|
|
|
return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
2021-05-02 06:34:40 +00:00
|
|
|
bool OffsetVfsFile::Rename(std::string_view new_name) {
|
|
|
|
return file->Rename(new_name);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::GetOffset() const {
|
2018-07-19 01:07:11 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t OffsetVfsFile::TrimToFit(std::size_t r_size, std::size_t r_offset) const {
|
|
|
|
return std::clamp(r_size, std::size_t{0}, size - r_offset);
|
2018-07-19 01:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|