C++ is so weird

This commit is contained in:
Robin Müller 2024-09-06 16:09:17 +02:00
parent d7124edb5a
commit c21185e1cb
Signed by: muellerr
GPG Key ID: A649FB78196E3849
5 changed files with 63 additions and 52 deletions

View File

@ -307,7 +307,6 @@ ReturnValue_t cfdp::DestHandler::startTransaction(const MetadataPduReader& reade
// so there is no need to create a file or truncate an existing file // so there is no need to create a file or truncate an existing file
if (destNameSize > 0 and sourceNameSize > 0) { if (destNameSize > 0 and sourceNameSize > 0) {
transactionParams.metadataOnly = false; transactionParams.metadataOnly = false;
FilesystemParams fparams(transactionParams.destName.data());
// handling to allow only specifying target directory. Example: // handling to allow only specifying target directory. Example:
// Source path /test/hello.txt, dest path /tmp -> dest path /tmp/hello.txt // Source path /test/hello.txt, dest path /tmp -> dest path /tmp/hello.txt
bool isDirectory = false; bool isDirectory = false;
@ -318,15 +317,15 @@ ReturnValue_t cfdp::DestHandler::startTransaction(const MetadataPduReader& reade
return result; return result;
} }
} }
if (destParams.user.vfs.fileExists(fparams)) { if (destParams.user.vfs.fileExists(transactionParams.destName.data())) {
result = destParams.user.vfs.truncateFile(fparams); result = destParams.user.vfs.truncateFile(transactionParams.destName.data());
if (result != returnvalue::OK) { if (result != returnvalue::OK) {
fileErrorHandler(events::FILESTORE_ERROR, result, "file truncation error"); fileErrorHandler(events::FILESTORE_ERROR, result, "file truncation error");
return FAILED; return FAILED;
// TODO: Relevant for filestore rejection error? // TODO: Relevant for filestore rejection error?
} }
} else { } else {
result = destParams.user.vfs.createFile(fparams); result = destParams.user.vfs.createFile(transactionParams.destName.data());
if (result != OK) { if (result != OK) {
fileErrorHandler(events::FILESTORE_ERROR, result, "file creation error"); fileErrorHandler(events::FILESTORE_ERROR, result, "file creation error");
return FAILED; return FAILED;
@ -413,10 +412,9 @@ ReturnValue_t cfdp::DestHandler::tryBuildingAbsoluteDestName(size_t destNameSize
// A path may only have a maximum of 256 characters in CFDP, so this buffer should be sufficient // A path may only have a maximum of 256 characters in CFDP, so this buffer should be sufficient
// for all use-cases. // for all use-cases.
char baseNameBuf[512]{}; char baseNameBuf[512]{};
FilesystemParams fparamsSrc(transactionParams.sourceName.data());
size_t baseNameLen = 0; size_t baseNameLen = 0;
ReturnValue_t result = destParams.user.vfs.getBaseFilename(fparamsSrc, baseNameBuf, ReturnValue_t result = destParams.user.vfs.getBaseFilename(
sizeof(baseNameBuf), baseNameLen); transactionParams.sourceName.data(), baseNameBuf, sizeof(baseNameBuf), baseNameLen);
if (result != returnvalue::OK or baseNameLen == 0) { if (result != returnvalue::OK or baseNameLen == 0) {
fileErrorHandler(events::FILENAME_TOO_LARGE_ERROR, 0, "error retrieving source base name"); fileErrorHandler(events::FILENAME_TOO_LARGE_ERROR, 0, "error retrieving source base name");
return FAILED; return FAILED;

View File

@ -169,8 +169,7 @@ ReturnValue_t cfdp::SourceHandler::transactionStart(PutRequest& putRequest, Remo
// operation is safe. // operation is safe.
transactionParams.sourceName[transactionParams.sourceNameSize] = '\0'; transactionParams.sourceName[transactionParams.sourceNameSize] = '\0';
transactionParams.destName[transactionParams.destNameSize] = '\0'; transactionParams.destName[transactionParams.destNameSize] = '\0';
FilesystemParams params(transactionParams.sourceName.data()); if (!sourceParams.user.vfs.fileExists(transactionParams.sourceName.data())) {
if (!sourceParams.user.vfs.fileExists(params)) {
return FILE_DOES_NOT_EXIST; return FILE_DOES_NOT_EXIST;
} }
if (cfg.maxFileSegmentLen > fileBuf.size() or cfg.maxFileSegmentLen == 0) { if (cfg.maxFileSegmentLen > fileBuf.size() or cfg.maxFileSegmentLen == 0) {
@ -206,7 +205,7 @@ ReturnValue_t cfdp::SourceHandler::transactionStart(PutRequest& putRequest, Remo
} }
step = TransactionStep::IDLE; step = TransactionStep::IDLE;
uint64_t fileSize = 0; uint64_t fileSize = 0;
sourceParams.user.vfs.getFileSize(params, fileSize); sourceParams.user.vfs.getFileSize(transactionParams.sourceName.data(), fileSize);
transactionParams.pduConf.largeFile = false; transactionParams.pduConf.largeFile = false;
if (fileSize > UINT32_MAX) { if (fileSize > UINT32_MAX) {
transactionParams.pduConf.largeFile = true; transactionParams.pduConf.largeFile = true;

View File

@ -71,21 +71,27 @@ class HasFileSystemIF {
} }
// Get the base filename without the full directory path // Get the base filename without the full directory path
virtual ReturnValue_t getBaseFilename(FilesystemParams params, char* nameBuf, size_t maxLen, virtual ReturnValue_t getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
size_t& baseNameLen) = 0; size_t& baseNameLen) = 0;
virtual ReturnValue_t isDirectory(const char* path, bool& isDirectory) = 0; virtual ReturnValue_t isDirectory(const char* path, bool& isDirectory) = 0;
virtual ReturnValue_t getFileSize(FilesystemParams params, uint64_t& fileSize) = 0; virtual ReturnValue_t getFileSize(const char* path, uint64_t& fileSize,
FileSystemArgsIF* args) = 0;
virtual ReturnValue_t getFileSize(const char* path, uint64_t& fileSize) {
return getFileSize(path, fileSize, nullptr);
}
virtual bool fileExists(FilesystemParams params) = 0; virtual bool fileExists(const char* path, FileSystemArgsIF* args) = 0;
virtual bool fileExists(const char* path) { return fileExists(path, nullptr); }
/** /**
* Truncate a file, deleting its contents and setting its size to 0 accordingly. * Truncate a file, deleting its contents and setting its size to 0 accordingly.
* @param params * @param params
* @return * @return
*/ */
virtual ReturnValue_t truncateFile(FilesystemParams params) = 0; virtual ReturnValue_t truncateFile(const char* path, FileSystemArgsIF* args) = 0;
virtual ReturnValue_t truncateFile(const char* path) { return truncateFile(path, nullptr); }
/** /**
* @brief Generic function to write to a file. * @brief Generic function to write to a file.
@ -132,10 +138,8 @@ class HasFileSystemIF {
* @param args Any other arguments which an implementation might require * @param args Any other arguments which an implementation might require
* @return * @return
*/ */
virtual ReturnValue_t createFile(FilesystemParams params) { virtual ReturnValue_t createFile(const char* path) { return createFile(path, nullptr, 0); }
return createFile(params, nullptr, 0); virtual ReturnValue_t createFile(const char* path, const uint8_t* data, size_t size) = 0;
}
virtual ReturnValue_t createFile(FilesystemParams params, const uint8_t* data, size_t size) = 0;
/** /**
* @brief Generic function to delete a file. * @brief Generic function to delete a file.
@ -155,9 +159,13 @@ class HasFileSystemIF {
* @param args Any other arguments which an implementation might require * @param args Any other arguments which an implementation might require
* @return * @return
*/ */
virtual ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) = 0; virtual ReturnValue_t createDirectory(const char* path, bool createParentDirs,
virtual ReturnValue_t createDirectory(FilesystemParams params) { FileSystemArgsIF* args) = 0;
return createDirectory(params, false); virtual ReturnValue_t createDirectory(const char* path, FileSystemArgsIF* args) {
return createDirectory(path, false, args);
}
virtual ReturnValue_t createDirectory(const char* path) {
return createDirectory(path, false, nullptr);
} }
/** /**
@ -165,9 +173,10 @@ class HasFileSystemIF {
* @param repositoryPath * @param repositoryPath
* @param args Any other arguments which an implementation might require * @param args Any other arguments which an implementation might require
*/ */
virtual ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) = 0; virtual ReturnValue_t removeDirectory(const char* path, bool deleteRecurively,
virtual ReturnValue_t removeDirectory(FilesystemParams params) { FileSystemArgsIF* args) = 0;
return removeDirectory(params, false); virtual ReturnValue_t removeDirectory(const char* path, FileSystemArgsIF* args) {
return removeDirectory(path, false, nullptr);
} }
virtual ReturnValue_t rename(const char* oldPath, const char* newPath) { virtual ReturnValue_t rename(const char* oldPath, const char* newPath) {

View File

@ -55,12 +55,11 @@ ReturnValue_t HostFilesystem::readFromFile(FileOpParams params, uint8_t **buffer
return returnvalue::OK; return returnvalue::OK;
} }
ReturnValue_t HostFilesystem::createFile(FilesystemParams params, const uint8_t *data, ReturnValue_t HostFilesystem::createFile(const char *path_, const uint8_t *data, size_t size) {
size_t size) { if (path_ == nullptr) {
if (params.path == nullptr) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
path path(params.path); std::filesystem::path path(path_);
std::error_code e; std::error_code e;
if (exists(path, e)) { if (exists(path, e)) {
return HasFileSystemIF::FILE_ALREADY_EXISTS; return HasFileSystemIF::FILE_ALREADY_EXISTS;
@ -87,11 +86,12 @@ ReturnValue_t HostFilesystem::removeFile(const char *path_, FileSystemArgsIF *ar
return HasFileSystemIF::GENERIC_FILE_ERROR; return HasFileSystemIF::GENERIC_FILE_ERROR;
} }
ReturnValue_t HostFilesystem::createDirectory(FilesystemParams params, bool createParentDirs) { ReturnValue_t HostFilesystem::createDirectory(const char *path, bool createParentDirs,
if (params.path == nullptr) { FileSystemArgsIF *args) {
if (path == nullptr) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
path dirPath(params.path); std::filesystem::path dirPath(path);
std::error_code e; std::error_code e;
if (exists(dirPath, e)) { if (exists(dirPath, e)) {
@ -110,11 +110,12 @@ ReturnValue_t HostFilesystem::createDirectory(FilesystemParams params, bool crea
return HasFileSystemIF::GENERIC_DIR_ERROR; return HasFileSystemIF::GENERIC_DIR_ERROR;
} }
ReturnValue_t HostFilesystem::removeDirectory(FilesystemParams params, bool deleteRecurively) { ReturnValue_t HostFilesystem::removeDirectory(const char *path, bool deleteRecurively,
if (params.path == nullptr) { FileSystemArgsIF *args) {
if (path == nullptr) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
path dirPath(params.path); std::filesystem::path dirPath(path);
std::error_code e; std::error_code e;
if (not exists(dirPath, e)) { if (not exists(dirPath, e)) {
return HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST; return HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST;
@ -153,14 +154,14 @@ ReturnValue_t HostFilesystem::rename(const char *oldPath_, const char *newPath_,
return returnvalue::OK; return returnvalue::OK;
} }
bool HostFilesystem::fileExists(FilesystemParams params) { bool HostFilesystem::fileExists(const char *path_, FileSystemArgsIF *args) {
path path(params.path); std::filesystem::path path(path_);
std::error_code e; std::error_code e;
return filesystem::exists(path, e); return filesystem::exists(path, e);
} }
ReturnValue_t HostFilesystem::truncateFile(FilesystemParams params) { ReturnValue_t HostFilesystem::truncateFile(const char *path_, FileSystemArgsIF *args) {
path path(params.path); std::filesystem::path path(path_);
std::error_code e; std::error_code e;
if (not filesystem::exists(path, e)) { if (not filesystem::exists(path, e)) {
return FILE_DOES_NOT_EXIST; return FILE_DOES_NOT_EXIST;
@ -175,14 +176,14 @@ ReturnValue_t HostFilesystem::isDirectory(const char *path, bool &isDirectory) {
return returnvalue::OK; return returnvalue::OK;
} }
ReturnValue_t HostFilesystem::getBaseFilename(FilesystemParams params, char *nameBuf, size_t maxLen, ReturnValue_t HostFilesystem::getBaseFilename(const char *path, char *nameBuf, size_t maxLen,
size_t &baseNameLen) { size_t &baseNameLen) {
std::string path(params.path); std::string pathAsStr(path);
size_t lastPos = path.find_last_of("/\\"); size_t lastPos = pathAsStr.find_last_of("/\\");
if (lastPos == std::string::npos) { if (lastPos == std::string::npos) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
std::string baseName = path.substr(path.find_last_of("/\\") + 1); std::string baseName = pathAsStr.substr(pathAsStr.find_last_of("/\\") + 1);
if (baseName.size() + 1 > maxLen) { if (baseName.size() + 1 > maxLen) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
@ -192,10 +193,11 @@ ReturnValue_t HostFilesystem::getBaseFilename(FilesystemParams params, char *nam
return returnvalue::OK; return returnvalue::OK;
} }
ReturnValue_t HostFilesystem::getFileSize(FilesystemParams params, uint64_t &fileSize) { ReturnValue_t HostFilesystem::getFileSize(const char *path, uint64_t &fileSize,
if (!fileExists(params)) { FileSystemArgsIF *args) {
if (!fileExists(path, args)) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST; return HasFileSystemIF::FILE_DOES_NOT_EXIST;
} }
fileSize = std::filesystem::file_size(params.path); fileSize = std::filesystem::file_size(path);
return returnvalue::OK; return returnvalue::OK;
} }

View File

@ -9,19 +9,22 @@ class HostFilesystem : public HasFileSystemIF {
public: public:
HostFilesystem(); HostFilesystem();
ReturnValue_t getBaseFilename(FilesystemParams params, char *nameBuf, size_t maxLen, ReturnValue_t getBaseFilename(const char *path, char *nameBuf, size_t maxLen,
size_t &baseNameLen) override; size_t &baseNameLen) override;
virtual ReturnValue_t getFileSize(FilesystemParams params, uint64_t &fileSize) override; virtual ReturnValue_t getFileSize(const char *path, uint64_t &fileSize,
FileSystemArgsIF *args) override;
ReturnValue_t isDirectory(const char *path, bool &isDirectory) override; ReturnValue_t isDirectory(const char *path, bool &isDirectory) override;
bool fileExists(FilesystemParams params) override; bool fileExists(const char *path, FileSystemArgsIF *args) override;
ReturnValue_t truncateFile(FilesystemParams params) override; ReturnValue_t truncateFile(const char *path, FileSystemArgsIF *args) override;
ReturnValue_t writeToFile(FileOpParams params, const uint8_t *data) override; ReturnValue_t writeToFile(FileOpParams params, const uint8_t *data) override;
ReturnValue_t readFromFile(FileOpParams fileOpInfo, uint8_t **buffer, size_t &readSize, ReturnValue_t readFromFile(FileOpParams fileOpInfo, uint8_t **buffer, size_t &readSize,
size_t maxSize) override; size_t maxSize) override;
ReturnValue_t createFile(FilesystemParams params, const uint8_t *data, size_t size) override; ReturnValue_t createFile(const char *path, const uint8_t *data, size_t size) override;
ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override; ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override;
ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override; ReturnValue_t createDirectory(const char *path, bool createParentDirs,
ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override; FileSystemArgsIF *args) override;
ReturnValue_t removeDirectory(const char *path, bool deleteRecurively,
FileSystemArgsIF *args) override;
ReturnValue_t rename(const char *oldPath, const char *newPath, FileSystemArgsIF *args) override; ReturnValue_t rename(const char *oldPath, const char *newPath, FileSystemArgsIF *args) override;
std::error_code errorCode; std::error_code errorCode;