2022-08-10 11:09:07 +02:00
|
|
|
#include "FilesystemMock.h"
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
#include <cstring>
|
2022-08-17 16:47:46 +02:00
|
|
|
#include <filesystem>
|
|
|
|
|
2022-08-10 17:03:23 +02:00
|
|
|
#include "fsfw/serialize/SerializeIF.h"
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::feedFile(const std::string& filename, std::ifstream& file) {
|
2022-08-17 16:47:46 +02:00
|
|
|
if (not std::filesystem::exists(filename)) {
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-08-17 16:47:46 +02:00
|
|
|
}
|
|
|
|
size_t fileSize = std::filesystem::file_size(filename);
|
|
|
|
FileOpParams params(filename.c_str(), fileSize);
|
|
|
|
std::vector<uint8_t> rawData(fileSize);
|
2024-05-24 14:29:42 +02:00
|
|
|
file.read(reinterpret_cast<char*>(rawData.data()), static_cast<unsigned int>(rawData.size()));
|
|
|
|
createOrAddToFile(filename.data(), 0, rawData.data(), rawData.size());
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-17 16:47:46 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::writeToFile(const char* path, size_t offset, const uint8_t* data,
|
|
|
|
size_t size) {
|
|
|
|
createOrAddToFile(path, offset, data, size);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
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);
|
2022-08-10 17:03:23 +02:00
|
|
|
if (iter == fileMap.end()) {
|
|
|
|
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
2024-05-24 14:29:42 +02:00
|
|
|
}
|
|
|
|
auto& [fileSegQueue, fileRaw] = iter->second;
|
|
|
|
size_t readLen = size;
|
|
|
|
if (offset + size > fileRaw.size()) {
|
|
|
|
if (offset > fileRaw.size()) {
|
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
2024-05-24 14:29:42 +02:00
|
|
|
readLen = fileRaw.size() - offset;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
2024-05-24 14:29:42 +02:00
|
|
|
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;
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::createFile(const char* path, const uint8_t* data, size_t size) {
|
|
|
|
createOrAddToFile(path, 0, data, size);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::removeFile(const char* path, FileSystemArgsIF* args) {
|
2022-08-10 17:03:23 +02:00
|
|
|
std::string filename(path);
|
|
|
|
auto iter = fileMap.find(filename);
|
|
|
|
if (iter == fileMap.end()) {
|
|
|
|
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
|
|
|
} else {
|
|
|
|
fileMap.erase(iter);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::createDirectory(const char* path, bool createParentDirs,
|
|
|
|
FileSystemArgsIF* args) {
|
|
|
|
std::string dirPath = path;
|
2022-08-10 17:03:23 +02:00
|
|
|
dirMap[dirPath].createCallCount++;
|
|
|
|
dirMap[dirPath].wihParentDir.push(createParentDirs);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::removeDirectory(const char* path, bool deleteRecurively,
|
|
|
|
FileSystemArgsIF* args) {
|
|
|
|
std::string dirPath = path;
|
2022-08-10 17:03:23 +02:00
|
|
|
dirMap[dirPath].delCallCount++;
|
|
|
|
dirMap[dirPath].recursiveDeletion.push(deleteRecurively);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::rename(const char* oldPath, const char* newPath,
|
|
|
|
FileSystemArgsIF* args) {
|
2023-08-03 17:43:03 +02:00
|
|
|
renameQueue.emplace(oldPath, newPath);
|
2022-08-22 16:35:53 +02:00
|
|
|
return returnvalue::OK;
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
void FilesystemMock::createOrAddToFile(const char* path, size_t offset, const uint8_t* data,
|
|
|
|
size_t dataSize) {
|
|
|
|
std::string filename(path);
|
2022-08-10 15:03:53 +02:00
|
|
|
auto iter = fileMap.find(filename);
|
|
|
|
if (iter == fileMap.end()) {
|
|
|
|
FileSegmentQueue queue;
|
2024-05-24 14:29:42 +02:00
|
|
|
if (dataSize > 0) {
|
|
|
|
queue.emplace(filename, offset, data, dataSize);
|
2022-08-17 16:47:46 +02:00
|
|
|
}
|
2022-08-10 15:03:53 +02:00
|
|
|
FileInfo info;
|
|
|
|
info.fileSegQueue = queue;
|
2022-08-10 17:03:23 +02:00
|
|
|
if (data != nullptr) {
|
2024-05-24 14:29:42 +02:00
|
|
|
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
2022-08-10 17:03:23 +02:00
|
|
|
}
|
2022-08-10 15:03:53 +02:00
|
|
|
fileMap.emplace(filename, info);
|
|
|
|
} else {
|
2024-05-24 14:29:42 +02:00
|
|
|
FileInfo& info = iter->second;
|
|
|
|
info.fileSegQueue.emplace(filename, offset, data, dataSize);
|
2022-08-10 17:03:23 +02:00
|
|
|
if (data == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-10 15:03:53 +02:00
|
|
|
// Easiest case: append data to the end
|
2024-05-24 14:29:42 +02:00
|
|
|
if (offset == info.fileRaw.size()) {
|
|
|
|
info.fileRaw.insert(info.fileRaw.end(), data, data + dataSize);
|
2022-08-10 15:03:53 +02:00
|
|
|
} else {
|
2024-05-24 14:29:42 +02:00
|
|
|
size_t totalNewLen = offset + dataSize;
|
2022-08-10 17:03:23 +02:00
|
|
|
if (totalNewLen > info.fileRaw.size()) {
|
2024-05-24 14:29:42 +02:00
|
|
|
info.fileRaw.resize(offset + dataSize);
|
2022-08-10 15:03:53 +02:00
|
|
|
}
|
2024-05-24 14:29:42 +02:00
|
|
|
std::copy(data, data + dataSize, info.fileRaw.begin() + static_cast<unsigned int>(offset));
|
2022-08-10 15:03:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 17:03:23 +02:00
|
|
|
|
|
|
|
void FilesystemMock::reset() {
|
|
|
|
fileMap.clear();
|
|
|
|
dirMap.clear();
|
2022-08-17 16:47:46 +02:00
|
|
|
std::queue<RenameInfo> empty;
|
|
|
|
std::swap(renameQueue, empty);
|
2022-08-10 11:09:07 +02:00
|
|
|
}
|
2022-09-05 17:42:56 +02:00
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
bool FilesystemMock::fileExists(const char* path, FileSystemArgsIF* args) {
|
|
|
|
std::string filename(path);
|
2022-09-05 17:42:56 +02:00
|
|
|
auto iter = fileMap.find(filename);
|
|
|
|
if (iter == fileMap.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::truncateFile(const char* path, FileSystemArgsIF* args) {
|
|
|
|
truncateCalledOnFile = path;
|
2022-09-05 17:42:56 +02:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
2023-02-24 16:49:23 +01:00
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
|
|
|
|
size_t& baseNameLen) {
|
2023-02-24 16:49:23 +01:00
|
|
|
return returnvalue::OK;
|
|
|
|
}
|
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::isDirectory(const char* path, bool& isDirectory) { return false; }
|
2023-07-17 16:40:44 +02:00
|
|
|
|
2024-05-24 14:29:42 +02:00
|
|
|
ReturnValue_t FilesystemMock::getFileSize(const char* path, uint64_t& fileSize,
|
|
|
|
FileSystemArgsIF* args) {
|
|
|
|
std::string filename(path);
|
2023-07-17 16:40:44 +02:00
|
|
|
auto iter = fileMap.find(filename);
|
2023-08-03 17:43:03 +02:00
|
|
|
if (iter != fileMap.end()) {
|
2023-07-17 16:40:44 +02:00
|
|
|
fileSize = iter->second.fileRaw.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|