160 lines
5.2 KiB
C++
160 lines
5.2 KiB
C++
#include "FilesystemMock.h"
|
|
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
|
|
#include "fsfw/serialize/SerializeIF.h"
|
|
|
|
ReturnValue_t FilesystemMock::feedFile(const std::string& filename, std::ifstream& file) {
|
|
if (not std::filesystem::exists(filename)) {
|
|
return returnvalue::FAILED;
|
|
}
|
|
size_t fileSize = std::filesystem::file_size(filename);
|
|
FileOpParams params(filename.c_str(), fileSize);
|
|
std::vector<uint8_t> rawData(fileSize);
|
|
file.read(reinterpret_cast<char*>(rawData.data()), static_cast<unsigned int>(rawData.size()));
|
|
createOrAddToFile(filename.data(), 0, rawData.data(), rawData.size());
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::writeToFile(const char* path, size_t offset, const uint8_t* data,
|
|
size_t size) {
|
|
createOrAddToFile(path, offset, data, size);
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::readFromFile(const char* path, size_t offset, size_t size,
|
|
uint8_t* buffer, size_t& readSize, size_t maxSize) {
|
|
const std::string filename(path);
|
|
const auto iter = fileMap.find(filename);
|
|
if (iter == fileMap.end()) {
|
|
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
|
}
|
|
auto& [fileSegQueue, fileRaw] = iter->second;
|
|
size_t readLen = size;
|
|
if (offset + size > fileRaw.size()) {
|
|
if (offset > fileRaw.size()) {
|
|
return returnvalue::OK;
|
|
}
|
|
readLen = fileRaw.size() - offset;
|
|
}
|
|
if (readSize + readLen > maxSize) {
|
|
return SerializeIF::STREAM_TOO_SHORT;
|
|
}
|
|
// std::copy(info.fileRaw.data() + offset, info.fileRaw.data() + offset + readLen, *buffer);
|
|
std::memcpy(buffer, fileRaw.data() + offset, readLen);
|
|
readSize = readLen;
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::createFile(const char* path, const uint8_t* data, size_t size) {
|
|
createOrAddToFile(path, 0, data, size);
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::removeFile(const char* path, FileSystemArgsIF* args) {
|
|
std::string filename(path);
|
|
auto iter = fileMap.find(filename);
|
|
if (iter == fileMap.end()) {
|
|
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
|
} else {
|
|
fileMap.erase(iter);
|
|
return returnvalue::OK;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::createDirectory(const char* path, bool createParentDirs,
|
|
FileSystemArgsIF* args) {
|
|
std::string dirPath = path;
|
|
dirMap[dirPath].createCallCount++;
|
|
dirMap[dirPath].wihParentDir.push(createParentDirs);
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::removeDirectory(const char* path, bool deleteRecurively,
|
|
FileSystemArgsIF* args) {
|
|
std::string dirPath = path;
|
|
dirMap[dirPath].delCallCount++;
|
|
dirMap[dirPath].recursiveDeletion.push(deleteRecurively);
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::rename(const char* oldPath, const char* newPath,
|
|
FileSystemArgsIF* args) {
|
|
renameQueue.emplace(oldPath, newPath);
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
void FilesystemMock::createOrAddToFile(const char* path, size_t offset, const uint8_t* data,
|
|
size_t dataSize) {
|
|
std::string filename(path);
|
|
auto iter = fileMap.find(filename);
|
|
if (iter == fileMap.end()) {
|
|
FileSegmentQueue queue;
|
|
if (dataSize > 0) {
|
|
queue.emplace(filename, offset, data, dataSize);
|
|
}
|
|
FileInfo info;
|
|
info.fileSegQueue = queue;
|
|
if (data != nullptr) {
|
|
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
|
}
|
|
fileMap.emplace(filename, info);
|
|
} else {
|
|
FileInfo& info = iter->second;
|
|
info.fileSegQueue.emplace(filename, offset, data, dataSize);
|
|
if (data == nullptr) {
|
|
return;
|
|
}
|
|
// Easiest case: append data to the end
|
|
if (offset == info.fileRaw.size()) {
|
|
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
|
} else {
|
|
size_t totalNewLen = offset + dataSize;
|
|
if (totalNewLen > info.fileRaw.size()) {
|
|
info.fileRaw.resize(offset + dataSize);
|
|
}
|
|
std::copy(data, data + dataSize, info.fileRaw.begin() + static_cast<unsigned int>(offset));
|
|
}
|
|
}
|
|
}
|
|
|
|
void FilesystemMock::reset() {
|
|
fileMap.clear();
|
|
dirMap.clear();
|
|
std::queue<RenameInfo> empty;
|
|
std::swap(renameQueue, empty);
|
|
}
|
|
|
|
bool FilesystemMock::fileExists(const char* path, FileSystemArgsIF* args) {
|
|
std::string filename(path);
|
|
auto iter = fileMap.find(filename);
|
|
if (iter == fileMap.end()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::truncateFile(const char* path, FileSystemArgsIF* args) {
|
|
truncateCalledOnFile = path;
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
|
|
size_t& baseNameLen) {
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t FilesystemMock::isDirectory(const char* path, bool& isDirectory) { return false; }
|
|
|
|
ReturnValue_t FilesystemMock::getFileSize(const char* path, uint64_t& fileSize,
|
|
FileSystemArgsIF* args) {
|
|
std::string filename(path);
|
|
auto iter = fileMap.find(filename);
|
|
if (iter != fileMap.end()) {
|
|
fileSize = iter->second.fileRaw.size();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|