2021-10-15 15:41:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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/>.
|
|
|
|
****************************************************************************/
|
2022-07-26 08:16:27 +02:00
|
|
|
#include "WriteOnlyFileWithCache.h"
|
2021-10-15 15:41:16 +02:00
|
|
|
#include <coreinit/memory.h>
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <malloc.h>
|
2021-10-16 13:03:32 +02:00
|
|
|
#include <utils/StringTools.h>
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <utils/logger.h>
|
2021-10-15 15:41:16 +02:00
|
|
|
|
2021-10-16 13:03:32 +02:00
|
|
|
#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) {
|
2021-10-15 15:41:16 +02:00
|
|
|
if (!this->isOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->writeBufferSize = cacheSize;
|
2022-07-26 08:16:27 +02:00
|
|
|
this->writeBuffer = (void *) memalign(0x1000, this->writeBufferSize);
|
2021-10-15 15:41:16 +02:00
|
|
|
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) {
|
2022-07-26 08:16:27 +02:00
|
|
|
auto finalAddr = addr;
|
2021-10-16 13:03:32 +02:00
|
|
|
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) {
|
2021-10-16 17:43:51 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Write remaining %8d bytes", realWriteSize);
|
2021-10-16 13:03:32 +02:00
|
|
|
if (CFile::write(reinterpret_cast<const uint8_t *>(addr), realWriteSize) != (int32_t) realWriteSize) {
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finalWriteSize = writeSize - realWriteSize;
|
2022-07-26 08:16:27 +02:00
|
|
|
finalAddr = (uint8_t *) ((uint32_t) addr + realWriteSize);
|
2021-10-16 13:03:32 +02:00
|
|
|
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) {
|
2021-10-15 15:41:16 +02:00
|
|
|
if (!this->flush()) {
|
|
|
|
DEBUG_FUNCTION_LINE("Flush failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-10-16 13:03:32 +02:00
|
|
|
return CFile::write(reinterpret_cast<const uint8_t *>(addr), finalWriteSize);
|
2021-10-15 15:41:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-16 13:03:32 +02:00
|
|
|
auto toWrite = (int32_t) finalWriteSize;
|
2021-10-15 15:41:16 +02:00
|
|
|
if (toWrite == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:03:32 +02:00
|
|
|
auto written = (int32_t) (writeSize - finalWriteSize);
|
2021-10-15 15:41:16 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
int32_t curWrite = toWrite;
|
|
|
|
|
|
|
|
if (this->writeBufferPos + curWrite > this->writeBufferSize) {
|
|
|
|
curWrite = this->writeBufferSize - this->writeBufferPos;
|
|
|
|
}
|
2021-10-16 13:03:32 +02:00
|
|
|
OSBlockMove((void *) (((uint32_t) this->writeBuffer) + this->writeBufferPos), (void *) (finalAddr + written), curWrite, 1);
|
2021-10-15 15:41:16 +02:00
|
|
|
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) {
|
2021-10-16 13:03:32 +02:00
|
|
|
// 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);
|
|
|
|
}
|
2021-10-15 15:41:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WriteOnlyFileWithCache::read(uint8_t *ptr, size_t size) {
|
|
|
|
return -1;
|
|
|
|
}
|