vfs: Add and rewite VfsRawCopy functions

This commit is contained in:
Zach Hilman 2018-09-19 21:55:47 -04:00
parent d6cbb3a3e0
commit f68e324672
2 changed files with 36 additions and 6 deletions

View File

@ -463,13 +463,41 @@ bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t
return true; return true;
} }
bool VfsRawCopy(VirtualFile src, VirtualFile dest) { bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, size_t block_size) {
if (src == nullptr || dest == nullptr) if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
return false; return false;
if (!dest->Resize(src->GetSize())) if (!dest->Resize(src->GetSize()))
return false; return false;
std::vector<u8> data = src->ReadAllBytes();
return dest->WriteBytes(data, 0) == data.size(); std::vector<u8> temp(std::min(block_size, src->GetSize()));
for (size_t i = 0; i < src->GetSize(); i += block_size) {
const auto read = std::min(block_size, src->GetSize() - i);
const auto block = src->Read(temp.data(), read, i);
if (dest->Write(temp.data(), read, i) != read)
return false;
}
return true;
}
bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, size_t block_size) {
if (src == nullptr || dest == nullptr || !src->IsReadable() || !dest->IsWritable())
return false;
for (const auto& file : src->GetFiles()) {
const auto out = dest->CreateFile(file->GetName());
if (!VfsRawCopy(file, out, block_size))
return false;
}
for (const auto& dir : src->GetSubdirectories()) {
const auto out = dest->CreateSubdirectory(dir->GetName());
if (!VfsRawCopyD(dir, out, block_size))
return false;
}
return true;
} }
VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) { VirtualDir GetOrCreateDirectoryRelative(const VirtualDir& rel, std::string_view path) {

View File

@ -316,12 +316,14 @@ public:
}; };
// Compare the two files, byte-for-byte, in increments specificed by block_size // Compare the two files, byte-for-byte, in increments specificed by block_size
bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, std::size_t block_size = 0x200); bool DeepEquals(const VirtualFile& file1, const VirtualFile& file2, size_t block_size = 0x1000);
// A method that copies the raw data between two different implementations of VirtualFile. If you // A method that copies the raw data between two different implementations of VirtualFile. If you
// are using the same implementation, it is probably better to use the Copy method in the parent // are using the same implementation, it is probably better to use the Copy method in the parent
// directory of src/dest. // directory of src/dest.
bool VfsRawCopy(VirtualFile src, VirtualFile dest); bool VfsRawCopy(const VirtualFile& src, const VirtualFile& dest, size_t block_size = 0x1000);
bool VfsRawCopyD(const VirtualDir& src, const VirtualDir& dest, size_t block_size = 0x1000);
// Checks if the directory at path relative to rel exists. If it does, returns that. If it does not // Checks if the directory at path relative to rel exists. If it does, returns that. If it does not
// it attempts to create it and returns the new dir or nullptr on failure. // it attempts to create it and returns the new dir or nullptr on failure.