2018-07-19 03:07:11 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-21 04:02:53 +02:00
|
|
|
#include <algorithm>
|
2018-07-20 06:04:51 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-19 03:07:11 +02:00
|
|
|
#include "core/file_sys/vfs_offset.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
OffsetVfsFile::OffsetVfsFile(std::shared_ptr<VfsFile> file_, size_t size_, size_t offset_,
|
2018-07-28 00:14:03 +02: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 03:07:11 +02:00
|
|
|
|
|
|
|
std::string OffsetVfsFile::GetName() const {
|
|
|
|
return name.empty() ? file->GetName() : name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OffsetVfsFile::GetSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::Resize(size_t new_size) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> OffsetVfsFile::GetContainingDirectory() const {
|
2018-07-28 00:14:03 +02:00
|
|
|
return parent;
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::IsWritable() const {
|
|
|
|
return file->IsWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::IsReadable() const {
|
|
|
|
return file->IsReadable();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OffsetVfsFile::Read(u8* data, size_t length, size_t r_offset) const {
|
|
|
|
return file->Read(data, TrimToFit(length, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OffsetVfsFile::Write(const u8* data, size_t length, size_t r_offset) {
|
|
|
|
return file->Write(data, TrimToFit(length, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<u8> OffsetVfsFile::ReadByte(size_t r_offset) const {
|
|
|
|
if (r_offset < size)
|
|
|
|
return file->ReadByte(offset + r_offset);
|
|
|
|
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> OffsetVfsFile::ReadBytes(size_t r_size, size_t r_offset) const {
|
|
|
|
return file->ReadBytes(TrimToFit(r_size, r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> OffsetVfsFile::ReadAllBytes() const {
|
|
|
|
return file->ReadBytes(size, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) {
|
|
|
|
if (r_offset < size)
|
|
|
|
return file->WriteByte(data, offset + r_offset);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-21 03:51:28 +02:00
|
|
|
size_t OffsetVfsFile::WriteBytes(const std::vector<u8>& data, size_t r_offset) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset);
|
|
|
|
}
|
|
|
|
|
2018-07-22 07:23:29 +02:00
|
|
|
bool OffsetVfsFile::Rename(std::string_view name) {
|
2018-07-19 03:07:11 +02:00
|
|
|
return file->Rename(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OffsetVfsFile::GetOffset() const {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const {
|
2018-07-21 04:02:53 +02:00
|
|
|
return std::clamp(r_size, size_t{0}, size - r_offset);
|
2018-07-19 03:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|