diff --git a/Sts1CobcSw/FileSystem/LfsWrapper.cpp b/Sts1CobcSw/FileSystem/LfsWrapper.cpp index ad0f8004..943f485b 100644 --- a/Sts1CobcSw/FileSystem/LfsWrapper.cpp +++ b/Sts1CobcSw/FileSystem/LfsWrapper.cpp @@ -6,7 +6,7 @@ namespace sts1cobcsw::fs { -lfs_t lfs{}; +auto lfs = lfs_t{}; // FIXME: For some reason this allocates 1024 bytes on the heap. With LFS_NO_MALLOC defined, it @@ -141,4 +141,42 @@ auto File::MoveConstructFrom(File * other) noexcept -> void other->openFlags_ = 0; other->lfsFile_ = {}; } + + +auto File::WriteInternal(void const * buffer, std::size_t size) -> Result +{ + if(not isOpen_) + { + return ErrorCode::fileNotOpen; + } + if((openFlags_ & LFS_O_WRONLY) == 0U) + { + return ErrorCode::unsupportedOperation; + } + auto nWrittenBytes = lfs_file_write(&lfs, &lfsFile_, buffer, size); + if(nWrittenBytes >= 0) + { + return nWrittenBytes; + } + return static_cast(nWrittenBytes); +} + + +auto File::ReadInternal(void * buffer, std::size_t size) const -> Result +{ + if(not isOpen_) + { + return ErrorCode::fileNotOpen; + } + if((openFlags_ & LFS_O_RDONLY) == 0U) + { + return ErrorCode::unsupportedOperation; + } + auto nReadBytes = lfs_file_read(&lfs, &lfsFile_, buffer, size); + if(nReadBytes >= 0) + { + return nReadBytes; + } + return static_cast(nReadBytes); +} } diff --git a/Sts1CobcSw/FileSystem/LfsWrapper.hpp b/Sts1CobcSw/FileSystem/LfsWrapper.hpp index abcf1f8b..7bb8d975 100644 --- a/Sts1CobcSw/FileSystem/LfsWrapper.hpp +++ b/Sts1CobcSw/FileSystem/LfsWrapper.hpp @@ -10,6 +10,7 @@ #include #include +#include #include @@ -19,10 +20,6 @@ namespace sts1cobcsw::fs using Path = etl::string; -// TODO: Get rid of this global variable or at least hide it in an internal namespace -extern lfs_t lfs; - - class File; [[nodiscard]] auto Mount() -> Result; @@ -43,9 +40,6 @@ class File friend auto Open(std::string_view path, unsigned int flags) -> Result; - // TODO: Read() and Write() should be implemented like ReadFrom() and WriteTo() in Fram.hpp, - // including forwarding to functions in an internal namespace. This way we can move lfs back - // into the .cpp file. template [[nodiscard]] auto Read(T * t) const -> Result; template @@ -58,6 +52,8 @@ class File // Only allow creation of File class through friend function Open() File() = default; auto MoveConstructFrom(File * other) noexcept -> void; + [[nodiscard]] auto WriteInternal(void const * buffer, std::size_t size) -> Result; + [[nodiscard]] auto ReadInternal(void * buffer, std::size_t size) const -> Result; Path path_ = ""; unsigned int openFlags_ = 0; diff --git a/Sts1CobcSw/FileSystem/LfsWrapper.ipp b/Sts1CobcSw/FileSystem/LfsWrapper.ipp index 1e9187b7..53932d70 100644 --- a/Sts1CobcSw/FileSystem/LfsWrapper.ipp +++ b/Sts1CobcSw/FileSystem/LfsWrapper.ipp @@ -2,8 +2,6 @@ #include -#include - #include "Sts1CobcSw/FileSystem/ErrorsAndResult.hpp" @@ -12,39 +10,13 @@ namespace sts1cobcsw::fs template auto File::Read(T * t) const -> Result { - if(not isOpen_) - { - return ErrorCode::fileNotOpen; - } - if(not(openFlags_ & LFS_O_RDONLY)) - { - return ErrorCode::unsupportedOperation; - } - auto nReadBytes = lfs_file_read(&lfs, &lfsFile_, t, sizeof(T)); - if(nReadBytes >= 0) - { - return nReadBytes; - } - return static_cast(nReadBytes); + return ReadInternal(t, sizeof(T)); } template auto File::Write(T const & t) -> Result { - if(not isOpen_) - { - return ErrorCode::fileNotOpen; - } - if(not(openFlags_ & LFS_O_WRONLY)) - { - return ErrorCode::unsupportedOperation; - } - auto nWrittenBytes = lfs_file_write(&lfs, &lfsFile_, &t, sizeof(T)); - if(nWrittenBytes >= 0) - { - return nWrittenBytes; - } - return static_cast(nWrittenBytes); + return WriteInternal(&t, sizeof(T)); } }