Skip to content

Commit

Permalink
Add internal read write functions for LfsWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
flo12356 committed Aug 12, 2024
1 parent a268ed2 commit 9eaed59
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
40 changes: 39 additions & 1 deletion Sts1CobcSw/FileSystem/LfsWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int>
{
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<ErrorCode>(nWrittenBytes);
}


auto File::ReadInternal(void * buffer, std::size_t size) const -> Result<int>
{
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<ErrorCode>(nReadBytes);
}
}
10 changes: 3 additions & 7 deletions Sts1CobcSw/FileSystem/LfsWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <etl/string.h>

#include <array>
#include <cstddef>
#include <string_view>


Expand All @@ -19,10 +20,6 @@ namespace sts1cobcsw::fs
using Path = etl::string<LFS_NAME_MAX>;


// 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<void>;
Expand All @@ -43,9 +40,6 @@ class File

friend auto Open(std::string_view path, unsigned int flags) -> Result<File>;

// 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<typename T>
[[nodiscard]] auto Read(T * t) const -> Result<int>;
template<typename T>
Expand All @@ -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<int>;
[[nodiscard]] auto ReadInternal(void * buffer, std::size_t size) const -> Result<int>;

Path path_ = "";
unsigned int openFlags_ = 0;
Expand Down
32 changes: 2 additions & 30 deletions Sts1CobcSw/FileSystem/LfsWrapper.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <Sts1CobcSw/FileSystem/LfsWrapper.hpp>

#include <littlefs/lfs.h>

#include "Sts1CobcSw/FileSystem/ErrorsAndResult.hpp"


Expand All @@ -12,39 +10,13 @@ namespace sts1cobcsw::fs
template<typename T>
auto File::Read(T * t) const -> Result<int>
{
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<ErrorCode>(nReadBytes);
return ReadInternal(t, sizeof(T));
}


template<typename T>
auto File::Write(T const & t) -> Result<int>
{
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<ErrorCode>(nWrittenBytes);
return WriteInternal(&t, sizeof(T));
}
}

0 comments on commit 9eaed59

Please sign in to comment.