2018-08-09 19:46:41 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-09-19 21:00:44 -05:00
|
|
|
#include "common/assert.h"
|
2018-08-09 19:46:41 -05:00
|
|
|
#include "core/file_sys/vfs_concat.h"
|
2018-09-25 16:26:09 -05:00
|
|
|
#include "core/file_sys/vfs_static.h"
|
2018-08-09 19:46:41 -05:00
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2020-07-12 03:31:01 -05:00
|
|
|
static bool VerifyConcatenationMapContinuity(const std::multimap<u64, VirtualFile>& map) {
|
2018-09-23 20:50:16 -05:00
|
|
|
const auto last_valid = --map.end();
|
|
|
|
for (auto iter = map.begin(); iter != last_valid;) {
|
2018-09-19 21:00:44 -05:00
|
|
|
const auto old = iter++;
|
|
|
|
if (old->first + old->second->GetSize() != iter->first) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map.begin()->first == 0;
|
|
|
|
}
|
|
|
|
|
2018-08-09 22:10:32 -05:00
|
|
|
ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name)
|
|
|
|
: name(std::move(name)) {
|
2018-09-15 08:21:06 -05:00
|
|
|
std::size_t next_offset = 0;
|
2018-08-09 19:46:41 -05:00
|
|
|
for (const auto& file : files_) {
|
2020-07-12 03:31:01 -05:00
|
|
|
files.emplace(next_offset, file);
|
2018-08-09 19:46:41 -05:00
|
|
|
next_offset += file->GetSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 03:31:01 -05:00
|
|
|
ConcatenatedVfsFile::ConcatenatedVfsFile(std::multimap<u64, VirtualFile> files_, std::string name)
|
2018-09-19 21:00:44 -05:00
|
|
|
: files(std::move(files_)), name(std::move(name)) {
|
2018-09-23 20:50:16 -05:00
|
|
|
ASSERT(VerifyConcatenationMapContinuity(files));
|
2018-09-19 21:00:44 -05:00
|
|
|
}
|
2018-09-19 18:19:05 -05:00
|
|
|
|
2018-09-23 20:50:16 -05:00
|
|
|
ConcatenatedVfsFile::~ConcatenatedVfsFile() = default;
|
|
|
|
|
2018-09-25 16:38:16 -05:00
|
|
|
VirtualFile ConcatenatedVfsFile::MakeConcatenatedFile(std::vector<VirtualFile> files,
|
|
|
|
std::string name) {
|
|
|
|
if (files.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (files.size() == 1)
|
|
|
|
return files[0];
|
|
|
|
|
|
|
|
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile ConcatenatedVfsFile::MakeConcatenatedFile(u8 filler_byte,
|
2020-07-12 03:31:01 -05:00
|
|
|
std::multimap<u64, VirtualFile> files,
|
2018-09-25 16:38:16 -05:00
|
|
|
std::string name) {
|
|
|
|
if (files.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (files.size() == 1)
|
|
|
|
return files.begin()->second;
|
|
|
|
|
|
|
|
const auto last_valid = --files.end();
|
|
|
|
for (auto iter = files.begin(); iter != last_valid;) {
|
|
|
|
const auto old = iter++;
|
|
|
|
if (old->first + old->second->GetSize() != iter->first) {
|
|
|
|
files.emplace(old->first + old->second->GetSize(),
|
|
|
|
std::make_shared<StaticVfsFile>(filler_byte, iter->first - old->first -
|
|
|
|
old->second->GetSize()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the map starts at offset 0 (start of file), otherwise pad to fill.
|
|
|
|
if (files.begin()->first != 0)
|
|
|
|
files.emplace(0, std::make_shared<StaticVfsFile>(filler_byte, files.begin()->first));
|
|
|
|
|
|
|
|
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:46:41 -05:00
|
|
|
std::string ConcatenatedVfsFile::GetName() const {
|
|
|
|
if (files.empty())
|
|
|
|
return "";
|
|
|
|
if (!name.empty())
|
|
|
|
return name;
|
|
|
|
return files.begin()->second->GetName();
|
|
|
|
}
|
|
|
|
|
2018-09-15 08:21:06 -05:00
|
|
|
std::size_t ConcatenatedVfsFile::GetSize() const {
|
2018-08-09 19:46:41 -05:00
|
|
|
if (files.empty())
|
|
|
|
return 0;
|
|
|
|
return files.rbegin()->first + files.rbegin()->second->GetSize();
|
|
|
|
}
|
|
|
|
|
2018-09-15 08:21:06 -05:00
|
|
|
bool ConcatenatedVfsFile::Resize(std::size_t new_size) {
|
2018-08-09 19:46:41 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VfsDirectory> ConcatenatedVfsFile::GetContainingDirectory() const {
|
|
|
|
if (files.empty())
|
|
|
|
return nullptr;
|
|
|
|
return files.begin()->second->GetContainingDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConcatenatedVfsFile::IsWritable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConcatenatedVfsFile::IsReadable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-15 08:21:06 -05:00
|
|
|
std::size_t ConcatenatedVfsFile::Read(u8* data, std::size_t length, std::size_t offset) const {
|
2018-09-23 20:50:16 -05:00
|
|
|
auto entry = --files.end();
|
2018-08-09 19:46:41 -05:00
|
|
|
for (auto iter = files.begin(); iter != files.end(); ++iter) {
|
|
|
|
if (iter->first > offset) {
|
2018-09-23 20:50:16 -05:00
|
|
|
entry = --iter;
|
2018-08-09 19:46:41 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 20:50:16 -05:00
|
|
|
if (entry->first + entry->second->GetSize() <= offset)
|
2018-08-09 19:46:41 -05:00
|
|
|
return 0;
|
|
|
|
|
2018-09-19 21:00:44 -05:00
|
|
|
const auto read_in =
|
2018-09-23 20:50:16 -05:00
|
|
|
std::min<u64>(entry->first + entry->second->GetSize() - offset, entry->second->GetSize());
|
2018-09-19 21:00:44 -05:00
|
|
|
if (length > read_in) {
|
2018-09-23 20:50:16 -05:00
|
|
|
return entry->second->Read(data, read_in, offset - entry->first) +
|
2018-09-19 21:00:44 -05:00
|
|
|
Read(data + read_in, length - read_in, offset + read_in);
|
2018-08-09 19:46:41 -05:00
|
|
|
}
|
|
|
|
|
2018-09-23 20:50:16 -05:00
|
|
|
return entry->second->Read(data, std::min<u64>(read_in, length), offset - entry->first);
|
2018-08-09 19:46:41 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 08:21:06 -05:00
|
|
|
std::size_t ConcatenatedVfsFile::Write(const u8* data, std::size_t length, std::size_t offset) {
|
2018-08-09 19:46:41 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConcatenatedVfsFile::Rename(std::string_view name) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-19 21:00:44 -05:00
|
|
|
|
2018-08-09 19:46:41 -05:00
|
|
|
} // namespace FileSys
|