wudd/source/fs/WriteOnlyFileWithCache.cpp
2021-10-16 17:43:51 +02:00

150 lines
5.2 KiB
C++

/****************************************************************************
* Copyright (C) 2021 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <malloc.h>
#include <coreinit/memory.h>
#include <utils/logger.h>
#include <utils/StringTools.h>
#include "WriteOnlyFileWithCache.h"
#define SPLIT_SIZE (0x80000000)
WriteOnlyFileWithCache::WriteOnlyFileWithCache(const char *path, int32_t cacheSize, bool split) : CFile(split ? std::string(path) + ".part1" : path, WriteOnly),
originalPath(path),
splitFile(split) {
if (!this->isOpen()) {
return;
}
this->writeBufferSize = cacheSize;
this->writeBuffer = (void *) memalign(0x1000, this->writeBufferSize);
if (this->writeBuffer == nullptr) {
this->close();
return;
}
this->writeBufferPos = 0;
}
WriteOnlyFileWithCache::~WriteOnlyFileWithCache() {
WriteOnlyFileWithCache::close();
free(writeBuffer);
}
bool WriteOnlyFileWithCache::flush() {
if (this->writeBufferPos > 0) {
int32_t res = CFile::write(static_cast<const uint8_t *>(this->writeBuffer), this->writeBufferPos);
if (res < 0) {
DEBUG_FUNCTION_LINE("Failed to flush cache, write failed: %d", res);
return false;
}
this->writeBufferPos = 0;
}
return true;
}
int32_t WriteOnlyFileWithCache::write(const uint8_t *addr, size_t writeSize) {
auto finalAddr = addr;
size_t finalWriteSize = writeSize;
if (splitFile) {
if (pos + writeBufferPos + finalWriteSize >= SPLIT_SIZE) {
DEBUG_FUNCTION_LINE("We need to split");
if (!flush()) {
return -2;
}
uint32_t realWriteSize = SPLIT_SIZE - pos;
if (realWriteSize > 0) {
DEBUG_FUNCTION_LINE("Write remaining %8d bytes", realWriteSize);
if (CFile::write(reinterpret_cast<const uint8_t *>(addr), realWriteSize) != (int32_t) realWriteSize) {
return -3;
}
}
finalWriteSize = writeSize - realWriteSize;
finalAddr = (uint8_t *) ((uint32_t) addr + realWriteSize);
part++;
if (!flush()) {
return -2;
}
CFile::close();
// open the next part
DEBUG_FUNCTION_LINE("Open %s", StringTools::strfmt("%s.part%d", originalPath.c_str(), part).c_str());
this->open(StringTools::strfmt("%s.part%d", originalPath.c_str(), part), WriteOnly);
}
if (finalWriteSize == 0) {
return (int32_t) writeSize;
}
}
if (finalWriteSize == this->writeBufferSize) {
if (!this->flush()) {
DEBUG_FUNCTION_LINE("Flush failed");
return -1;
}
return CFile::write(reinterpret_cast<const uint8_t *>(addr), finalWriteSize);
}
auto toWrite = (int32_t) finalWriteSize;
if (toWrite == 0) {
return 0;
}
auto written = (int32_t) (writeSize - finalWriteSize);
do {
int32_t curWrite = toWrite;
if (this->writeBufferPos + curWrite > this->writeBufferSize) {
curWrite = this->writeBufferSize - this->writeBufferPos;
}
OSBlockMove((void *) (((uint32_t) this->writeBuffer) + this->writeBufferPos), (void *) (finalAddr + written), curWrite, 1);
this->writeBufferPos += curWrite;
if (this->writeBufferPos == this->writeBufferSize) {
if (!this->flush()) {
DEBUG_FUNCTION_LINE("Flush failed");
return -2;
}
}
toWrite -= curWrite;
written += curWrite;
} while (toWrite > 0);
return written;
}
int32_t WriteOnlyFileWithCache::seek(int64_t offset, int32_t origin) {
// Hacky trick because we may need a seek.
if (origin == SEEK_SET_BASE_CLASS) {
if (splitFile) {
if ((offset / SPLIT_SIZE) + 1 != part) {
flush();
close();
part = (offset / SPLIT_SIZE) + 1;
DEBUG_FUNCTION_LINE("Open %s", StringTools::strfmt("%s.part%d", originalPath.c_str(), part).c_str());
this->open(StringTools::strfmt("%s.part%d", originalPath.c_str(), part), ReadWrite);
}
return CFile::seek(offset % SPLIT_SIZE, SEEK_SET);
}
return CFile::seek(offset, SEEK_SET);
}
return -1;
}
int32_t WriteOnlyFileWithCache::read(uint8_t *ptr, size_t size) {
return -1;
}