From 41ffd65e8a65ace23c97c99bb7782b641d887e20 Mon Sep 17 00:00:00 2001 From: Rijwal Sangey Date: Tue, 7 Jul 2026 20:21:37 -0700 Subject: [PATCH 1/3] log fs refactor --- firmware/logfs/CMakeLists.txt | 29 ++++-- firmware/logfs/srcpp/logfs.cpp | 1 + firmware/logfs/srcpp/logfs.hpp | 171 +++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 firmware/logfs/srcpp/logfs.cpp create mode 100644 firmware/logfs/srcpp/logfs.hpp diff --git a/firmware/logfs/CMakeLists.txt b/firmware/logfs/CMakeLists.txt index 606c804962..3630cc6189 100644 --- a/firmware/logfs/CMakeLists.txt +++ b/firmware/logfs/CMakeLists.txt @@ -1,9 +1,13 @@ set(LOGFS_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src" + "${CMAKE_CURRENT_SOURCE_DIR}/srcpp" ) -file(GLOB_RECURSE LOGFS_SRCS +file(GLOB_RECURSE LOGFS_C_SRCS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c" ) +file(GLOB_RECURSE LOGFS_CPP_SRCS CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/srcpp/*.cpp" +) message("") message("⚙️ Configuring LogFS") @@ -16,7 +20,7 @@ if (SKBUILD) find_package(Python3 REQUIRED COMPONENTS Interpreter Development) find_package(pybind11 CONFIG REQUIRED) - pybind11_add_module(logfs_src ${LOGFS_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/python/bindings.cpp) + pybind11_add_module(logfs_src ${LOGFS_CPP_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/python/bindings.cpp) target_include_directories(logfs_src PUBLIC "${LOGFS_INCLUDE_DIRS}") if (MSVC) @@ -42,9 +46,19 @@ else () if (NOT "${EMBEDDED_CMAKE_INCLUDED}" STREQUAL "TRUE") message(FATAL_ERROR "❌ embedded.cmake must be included before logfs is subdirectoried") endif () + embedded_library( + "logfs_cm5" + "${LOGFS_C_SRCS}" + "${LOGFS_INCLUDE_DIRS}" + "cm7" + TRUE + ) + target_compile_options(logfs_cm5 PRIVATE + -O3 + ) embedded_library( "logfs_cm7" - "${LOGFS_SRCS}" + "${LOGFS_CPP_SRCS}" "${LOGFS_INCLUDE_DIRS}" "cm7" TRUE @@ -56,8 +70,12 @@ else () find_package(Python3 REQUIRED COMPONENTS Interpreter Development) find_package(pybind11 CONFIG) - # Create pybind11 module. - pybind11_add_module(logfs_src ${LOGFS_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/python/bindings.cpp) + # Create pybind11 module, imma make this js CPP when rewrite is done. + pybind11_add_module(logfs_src + ${LOGFS_C_SRCS} + ${LOGFS_CPP_SRCS} + ${CMAKE_CURRENT_SOURCE_DIR}/python/bindings.cpp + ) target_include_directories(logfs_src PUBLIC "${LOGFS_INCLUDE_DIRS}") set(LOGFS_SRC_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/python") @@ -83,4 +101,3 @@ else () DEPENDS logfs_src) endif () endif () - diff --git a/firmware/logfs/srcpp/logfs.cpp b/firmware/logfs/srcpp/logfs.cpp new file mode 100644 index 0000000000..037a6091d7 --- /dev/null +++ b/firmware/logfs/srcpp/logfs.cpp @@ -0,0 +1 @@ +#include "logfs.hpp" diff --git a/firmware/logfs/srcpp/logfs.hpp b/firmware/logfs/srcpp/logfs.hpp new file mode 100644 index 0000000000..7341ce2040 --- /dev/null +++ b/firmware/logfs/srcpp/logfs.hpp @@ -0,0 +1,171 @@ +#pragma once +#include +#include +#include + +constexpr uint16_t LOGFS_ORIGIN = 0; // Filesystem starts at address zero +constexpr uint32_t LOGFS_INVALID_BLOCK = 0xFFFFFFFF; // Indicates invalid block +constexpr uint16_t LOGFS_PATH_BYTES = 128; // Max bytes to allocate for a path +constexpr uint16_t LOGFS_PAIR_SIZE = 2; + +/* Enum definitions */ +enum class LogFsErr : int16_t +{ + LOGFS_ERR_OK = 0, // No error + LOGFS_ERR_IO = -2, // Error during I/O operation + LOGFS_ERR_CORRUPT = -3, // File system was corrupted (bad CRC) + LOGFS_ERR_INVALID_ARG = -4, // Invalid argument + LOGFS_ERR_INVALID_PATH = -5, // Invalid path + LOGFS_ERR_UNMOUNTED = -6, // Filesystem hasn't been successfully mounted + LOGFS_ERR_NOMEM = -7, // Filesystem is full (no more memory) + LOGFS_ERR_NOT_OPEN = -8, // File hasn't been opened + LOGFS_ERR_RD_ONLY = -9, // File is read only, and a write was attempted + LOGFS_ERR_WR_ONLY = -10, // File is write only, and a read was attempted + LOGFS_ERR_DNE = -11, // File does not exist + LOGFS_ERR_NO_MORE_FILES = -12, // Traversed all files on the filesystem +}; + +enum class LogFsOpenFlags : uint8_t +{ + LOGFS_OPEN_RD_ONLY = 0x01, + LOGFS_OPEN_WR_ONLY = 0x02, + LOGFS_OPEN_RD_WR = 0x03, + LOGFS_OPEN_CREATE = 0x10, +}; + +enum class LogFsReadFlags : uint8_t +{ + LOGFS_READ_END, // Read from end of file + LOGFS_READ_ITER, // Read next N bytes of file +}; + +/* Config classes and structs */ +class LogFsBlockDevice +{ + public: + // Read a block at a given address. + virtual LogFsErr read(uint32_t block, std::span buf) = 0; + // Program a block at a given address. Will be erased beforehand. + virtual LogFsErr write(uint32_t block, std::span buf) = 0; + virtual ~LogFsBlockDevice() = default; + + struct LogFsCfg + { + // Size of a block, in bytes. + uint32_t block_size; + // Number of contiguous blocks available to the filesystem. + uint32_t block_count; + // 'this' ptr and the I/O functions. + LogFsBlockDevice* device; + // Pointer to cache buffer (must be 1 block in size). + std::span cache; + // Number of write cycles before blocks are evicted and replaced. + uint32_t write_cycles; + // If the entire filesystem should be marked as read-only. + bool rd_only; + }; +}; + +struct LogFsFileCfg +{ + std::string_view path; // File path string + std::span cache; // Pointer to cache buffer (must be 1 block in size) +}; + +/* Block definitions */ +struct LogFsPairHeader +{ + uint32_t crc; // Checksum must be first word in block + uint8_t seq_num; // Sequence number (used to calculate most recent version of the pair) + uint32_t write_cycles; // Number of times this pair has been written to + uint32_t replacement_addr; // Replacement address if evicted (invalid address if this block is still valid) +}; + +struct LogFsBlock_File +{ + LogFsPairHeader pair_hdr; // Pair data (pairs guarantee power loss resilience, must be first) + uint32_t next_file_addr; // Address of the next file block + uint32_t metadata_addr; // Address of the file's metadata block + uint32_t head_data_addr; // Address of file's newest data block + uint32_t prev_head_addr; // Address of file's previous head (for redundnacy, if head is corrupted) + uint32_t num_data_blocks; // Number of data blocks of this file + char path[1]; // File path string +}; + +struct LogFsBlock_Metadata +{ + LogFsPairHeader pair_hdr; // Pair data (pairs guarantee power loss resilience, must be first) + uint32_t num_bytes; // Number of data bytes in this block + uint8_t data; // First data byte, used to get pointer to the actual data bytes +}; + +struct LogFsBlock_Data +{ + uint32_t crc; // Checksum must be first word in block + uint32_t prev_data_addr; // Previous data block address + uint32_t num_bytes; // Number of data bytes in this block + uint8_t data; // First data byte, used to get pointer to the actual data bytes +}; + +struct LogFsPair +{ + uint32_t addrs[LOGFS_PAIR_SIZE]; // The 2 block addresses in the pair + uint8_t seq_num; // Sequence number (used to calculate most recent version of the pair) + bool seq_num_on_disk; // Whether or not the sequence number has been written to disk +}; + +struct LogFsCache +{ + uint32_t cached_addr; // Address of block in the cache + std::span buf; // Pointer to cache buffer (must be 1 block in size) +}; + +struct LogFsFile +{ + // File config info. + LogFsCache cache; // Each file has its own cache + LogFsBlock_Data* cache_data; // Convenience pointer to cache, as a data block + char path[LOGFS_PATH_BYTES]; // File path string + uint32_t flags; + + // File state variables. + bool is_open; // If the file is open + LogFsPair file_pair; // The pair on disk representing this file + LogFsPair metadata_pair; // The pair on disk holding file metadata + uint32_t head_data_addr; // Address of file's newest data block + + // State for iterating reads. + uint32_t read_iter_init; // If the read iterator has been initialized + uint32_t read_iter_data_byte; // Number of bytes read from current block + uint32_t read_iter_data_addr; // Current data block being read from +}; + +struct LogFsPath +{ + char path[LOGFS_PATH_BYTES]; // Path string + uint32_t file_addr; // Block for this file's info + uint32_t next_file_addr; // Block for next file's info +}; + +struct LogFs +{ + // Filesystem config. + const LogFsBlockDevice::LogFsCfg *cfg; // Pointer to config struct + + // Addresses to keep track of disk usage. + LogFsBlockDevice::LogFsCfg root_file; // Reserved empty root file, created during a format operation + uint32_t head_file_addr; // Address of newest file + uint32_t head_addr; // Smallest available block, to be written to next + + // Misc. state variables. + uint32_t eff_block_size_bytes; // How many bytes + int max_path_len_bytes; // Max path length that can be stored in a block, + bool mounted; // If filesystem has been mounted + bool out_of_memory; // If the filesystem has run out of memory + + // Utility pointers to filesystem cache. + LogFsPairHeader *cache_pair_hdr; // Pair header in filesystem cache (used for pair read/write ops) + LogFsBlock_File *cache_file; // File block pointer to the filesystem cache (for convenience) + LogFsBlock_Metadata *cache_metadata; // Metadata block pointer to the filesystem cache (for convenience) + LogFsBlock_Data *cache_data; // Data block pointer to the filesystem cache (for convenience) +}; From 169e5a0cadcec569947406c4f6467330c9c0668e Mon Sep 17 00:00:00 2001 From: Rijwal Sangey Date: Wed, 8 Jul 2026 14:04:55 -0700 Subject: [PATCH 2/3] Quick changes and rewrites --- firmware/logfs/CMakeLists.txt | 8 +- firmware/logfs/src/logfs.c | 2 +- firmware/logfs/srcpp/logfs.cpp | 43 ++++++ firmware/logfs/srcpp/logfs.hpp | 246 ++++++++++++++++----------------- 4 files changed, 170 insertions(+), 129 deletions(-) diff --git a/firmware/logfs/CMakeLists.txt b/firmware/logfs/CMakeLists.txt index 3630cc6189..992600e347 100644 --- a/firmware/logfs/CMakeLists.txt +++ b/firmware/logfs/CMakeLists.txt @@ -47,23 +47,23 @@ else () message(FATAL_ERROR "❌ embedded.cmake must be included before logfs is subdirectoried") endif () embedded_library( - "logfs_cm5" + "logfs_C" "${LOGFS_C_SRCS}" "${LOGFS_INCLUDE_DIRS}" "cm7" TRUE ) - target_compile_options(logfs_cm5 PRIVATE + target_compile_options(logfs_C PRIVATE -O3 ) embedded_library( - "logfs_cm7" + "logfs_CXX" "${LOGFS_CPP_SRCS}" "${LOGFS_INCLUDE_DIRS}" "cm7" TRUE ) - target_compile_options(logfs_cm7 PRIVATE + target_compile_options(logfs_CXX PRIVATE -O3 ) elseif ("${TARGET}" STREQUAL "test") diff --git a/firmware/logfs/src/logfs.c b/firmware/logfs/src/logfs.c index d51122662f..7c8a80815c 100644 --- a/firmware/logfs/src/logfs.c +++ b/firmware/logfs/src/logfs.c @@ -372,7 +372,7 @@ LogFsErr logfs_write(LogFs *fs, LogFsFile *file, const void *buf, uint32_t size) return LOGFS_ERR_OK; } -LogFsErr logfs_read( +LogFsErr( LogFs *fs, LogFsFile *file, void *buf, diff --git a/firmware/logfs/srcpp/logfs.cpp b/firmware/logfs/srcpp/logfs.cpp index 037a6091d7..20a487be5f 100644 --- a/firmware/logfs/srcpp/logfs.cpp +++ b/firmware/logfs/srcpp/logfs.cpp @@ -1 +1,44 @@ #include "logfs.hpp" + +#include +#include + +namespace LogFs +{ + +FileSystem::FileSystem(const BlockDevice::Cfg& cfg_in) + : cfg(&cfg_in) +{ + assert(cfg != nullptr); + assert(cfg->block_size > sizeof(BlockFile)); + assert(cfg->block_size > sizeof(BlockMetadata)); + assert(cfg->block_size > sizeof(BlockData)); + + eff_block_size_bytes = cfg->block_size - (sizeof(BlockData) - 1); + max_path_len_bytes = std::min( + static_cast(cfg->block_size - sizeof(BlockFile)), + static_cast(PATH_BYTES) + ); +} + +ErrorCode FileSystem::mount() +{ + return ErrorCode::NOT_OPEN; //bs for now +} + +ErrorCode FileSystem::format() +{ + return ErrorCode::NOT_OPEN; +} + +ErrorCode FileSystem::open(File&, const FileCfg&, uint32_t) +{ + return ErrorCode::NOT_OPEN; +} + +ErrorCode FileSystem::close(File&) +{ + return ErrorCode::NOT_OPEN; +} + +} // namespace LogFs \ No newline at end of file diff --git a/firmware/logfs/srcpp/logfs.hpp b/firmware/logfs/srcpp/logfs.hpp index 7341ce2040..ea54017182 100644 --- a/firmware/logfs/srcpp/logfs.hpp +++ b/firmware/logfs/srcpp/logfs.hpp @@ -1,171 +1,169 @@ #pragma once + +#include #include #include #include -constexpr uint16_t LOGFS_ORIGIN = 0; // Filesystem starts at address zero -constexpr uint32_t LOGFS_INVALID_BLOCK = 0xFFFFFFFF; // Indicates invalid block -constexpr uint16_t LOGFS_PATH_BYTES = 128; // Max bytes to allocate for a path -constexpr uint16_t LOGFS_PAIR_SIZE = 2; +namespace LogFs +{ + +inline constexpr uint16_t ORIGIN = 0; +inline constexpr uint32_t INVALID_BLOCK = 0xFFFFFFFFu; +inline constexpr uint16_t PATH_BYTES = 128; +inline constexpr uint16_t PAIR_SIZE = 2; -/* Enum definitions */ -enum class LogFsErr : int16_t +enum class ErrorCode : int16_t { - LOGFS_ERR_OK = 0, // No error - LOGFS_ERR_IO = -2, // Error during I/O operation - LOGFS_ERR_CORRUPT = -3, // File system was corrupted (bad CRC) - LOGFS_ERR_INVALID_ARG = -4, // Invalid argument - LOGFS_ERR_INVALID_PATH = -5, // Invalid path - LOGFS_ERR_UNMOUNTED = -6, // Filesystem hasn't been successfully mounted - LOGFS_ERR_NOMEM = -7, // Filesystem is full (no more memory) - LOGFS_ERR_NOT_OPEN = -8, // File hasn't been opened - LOGFS_ERR_RD_ONLY = -9, // File is read only, and a write was attempted - LOGFS_ERR_WR_ONLY = -10, // File is write only, and a read was attempted - LOGFS_ERR_DNE = -11, // File does not exist - LOGFS_ERR_NO_MORE_FILES = -12, // Traversed all files on the filesystem + OK = 0, + IO = -2, + CORRUPT = -3, + INVALID_ARG = -4, + INVALID_PATH = -5, + UNMOUNTED = -6, + NOMEM = -7, + NOT_OPEN = -8, + RD_ONLY = -9, + WR_ONLY = -10, + DNE = -11, + NO_MORE_FILES = -12, }; -enum class LogFsOpenFlags : uint8_t +enum class OpenFlags : uint8_t { - LOGFS_OPEN_RD_ONLY = 0x01, - LOGFS_OPEN_WR_ONLY = 0x02, - LOGFS_OPEN_RD_WR = 0x03, - LOGFS_OPEN_CREATE = 0x10, + RD_ONLY = 0x01, + WR_ONLY = 0x02, + RD_WR = 0x03, + CREATE = 0x10, }; -enum class LogFsReadFlags : uint8_t +enum class ReadFlags : uint8_t { - LOGFS_READ_END, // Read from end of file - LOGFS_READ_ITER, // Read next N bytes of file + END, + ITER, }; -/* Config classes and structs */ -class LogFsBlockDevice +struct PairHeader { - public: - // Read a block at a given address. - virtual LogFsErr read(uint32_t block, std::span buf) = 0; - // Program a block at a given address. Will be erased beforehand. - virtual LogFsErr write(uint32_t block, std::span buf) = 0; - virtual ~LogFsBlockDevice() = default; - - struct LogFsCfg - { - // Size of a block, in bytes. - uint32_t block_size; - // Number of contiguous blocks available to the filesystem. - uint32_t block_count; - // 'this' ptr and the I/O functions. - LogFsBlockDevice* device; - // Pointer to cache buffer (must be 1 block in size). - std::span cache; - // Number of write cycles before blocks are evicted and replaced. - uint32_t write_cycles; - // If the entire filesystem should be marked as read-only. - bool rd_only; - }; + uint32_t crc = 0; + uint8_t seq_num = 0; + uint32_t write_cycles = 0; + uint32_t replacement_addr = INVALID_BLOCK; }; -struct LogFsFileCfg +struct BlockFile { - std::string_view path; // File path string - std::span cache; // Pointer to cache buffer (must be 1 block in size) + PairHeader pair_hdr{}; + uint32_t next_file_addr = INVALID_BLOCK; + uint32_t metadata_addr = INVALID_BLOCK; + uint32_t head_data_addr = INVALID_BLOCK; + uint32_t prev_head_addr = INVALID_BLOCK; + uint32_t num_data_blocks = 0; + char path[1] = {}; }; -/* Block definitions */ -struct LogFsPairHeader +struct BlockMetadata { - uint32_t crc; // Checksum must be first word in block - uint8_t seq_num; // Sequence number (used to calculate most recent version of the pair) - uint32_t write_cycles; // Number of times this pair has been written to - uint32_t replacement_addr; // Replacement address if evicted (invalid address if this block is still valid) + PairHeader pair_hdr{}; + uint32_t num_bytes = 0; + uint8_t data = 0; }; -struct LogFsBlock_File +struct BlockData { - LogFsPairHeader pair_hdr; // Pair data (pairs guarantee power loss resilience, must be first) - uint32_t next_file_addr; // Address of the next file block - uint32_t metadata_addr; // Address of the file's metadata block - uint32_t head_data_addr; // Address of file's newest data block - uint32_t prev_head_addr; // Address of file's previous head (for redundnacy, if head is corrupted) - uint32_t num_data_blocks; // Number of data blocks of this file - char path[1]; // File path string + uint32_t crc = 0; + uint32_t prev_data_addr = INVALID_BLOCK; + uint32_t num_bytes = 0; + uint8_t data = 0; }; -struct LogFsBlock_Metadata +struct Pair { - LogFsPairHeader pair_hdr; // Pair data (pairs guarantee power loss resilience, must be first) - uint32_t num_bytes; // Number of data bytes in this block - uint8_t data; // First data byte, used to get pointer to the actual data bytes + uint32_t addrs[PAIR_SIZE]; + uint8_t seq_num = 0; + bool seq_num_on_disk = false; }; -struct LogFsBlock_Data +struct Cache { - uint32_t crc; // Checksum must be first word in block - uint32_t prev_data_addr; // Previous data block address - uint32_t num_bytes; // Number of data bytes in this block - uint8_t data; // First data byte, used to get pointer to the actual data bytes + uint32_t cached_addr = INVALID_BLOCK; + std::span buf{}; }; -struct LogFsPair +struct FileCfg { - uint32_t addrs[LOGFS_PAIR_SIZE]; // The 2 block addresses in the pair - uint8_t seq_num; // Sequence number (used to calculate most recent version of the pair) - bool seq_num_on_disk; // Whether or not the sequence number has been written to disk + std::string_view path{}; + std::span cache{}; }; -struct LogFsCache +class BlockDevice { - uint32_t cached_addr; // Address of block in the cache - std::span buf; // Pointer to cache buffer (must be 1 block in size) +public: + struct Cfg + { + uint32_t block_size = 0; + uint32_t block_count = 0; + BlockDevice* device = nullptr; + std::span cache{}; + uint32_t write_cycles = 0; + bool rd_only = false; + }; + + virtual ~BlockDevice() = default; + virtual ErrorCode read(uint32_t block, std::span buf) = 0; + virtual ErrorCode write(uint32_t block, std::span buf) = 0; }; -struct LogFsFile +class File { - // File config info. - LogFsCache cache; // Each file has its own cache - LogFsBlock_Data* cache_data; // Convenience pointer to cache, as a data block - char path[LOGFS_PATH_BYTES]; // File path string - uint32_t flags; - - // File state variables. - bool is_open; // If the file is open - LogFsPair file_pair; // The pair on disk representing this file - LogFsPair metadata_pair; // The pair on disk holding file metadata - uint32_t head_data_addr; // Address of file's newest data block - - // State for iterating reads. - uint32_t read_iter_init; // If the read iterator has been initialized - uint32_t read_iter_data_byte; // Number of bytes read from current block - uint32_t read_iter_data_addr; // Current data block being read from +public: + File() = default; + + Cache cache{}; + BlockData* cache_data = nullptr; + char path[PATH_BYTES] = {}; + uint32_t flags = 0; + + bool is_open = false; + Pair file_pair{}; + Pair metadata_pair{}; + uint32_t head_data_addr = INVALID_BLOCK; + + uint32_t read_iter_init = 0; + uint32_t read_iter_data_byte = 0; + uint32_t read_iter_data_addr = INVALID_BLOCK; }; -struct LogFsPath +class FileSystem { - char path[LOGFS_PATH_BYTES]; // Path string - uint32_t file_addr; // Block for this file's info - uint32_t next_file_addr; // Block for next file's info +public: + explicit FileSystem(const BlockDevice::Cfg& cfg); + + ErrorCode mount(); + ErrorCode format(); + ErrorCode open(File& file, const FileCfg& cfg, uint32_t flags); + ErrorCode close(File& file); + +private: + const BlockDevice::Cfg* cfg = nullptr; + File root_file{}; + uint32_t head_file_addr = INVALID_BLOCK; + uint32_t head_addr = 0; + uint32_t eff_block_size_bytes = 0; + int max_path_len_bytes = 0; + bool mounted = false; + bool out_of_memory = false; + PairHeader* cache_pair_hdr = nullptr; + BlockFile* cache_file = nullptr; + BlockMetadata* cache_metadata = nullptr; + BlockData* cache_data = nullptr; }; -struct LogFs +struct Path { - // Filesystem config. - const LogFsBlockDevice::LogFsCfg *cfg; // Pointer to config struct - - // Addresses to keep track of disk usage. - LogFsBlockDevice::LogFsCfg root_file; // Reserved empty root file, created during a format operation - uint32_t head_file_addr; // Address of newest file - uint32_t head_addr; // Smallest available block, to be written to next - - // Misc. state variables. - uint32_t eff_block_size_bytes; // How many bytes - int max_path_len_bytes; // Max path length that can be stored in a block, - bool mounted; // If filesystem has been mounted - bool out_of_memory; // If the filesystem has run out of memory - - // Utility pointers to filesystem cache. - LogFsPairHeader *cache_pair_hdr; // Pair header in filesystem cache (used for pair read/write ops) - LogFsBlock_File *cache_file; // File block pointer to the filesystem cache (for convenience) - LogFsBlock_Metadata *cache_metadata; // Metadata block pointer to the filesystem cache (for convenience) - LogFsBlock_Data *cache_data; // Data block pointer to the filesystem cache (for convenience) + char path[PATH_BYTES] = {}; + uint32_t file_addr = INVALID_BLOCK; + uint32_t next_file_addr = INVALID_BLOCK; }; + +} // namespace LogFs From 0b5385461e27ac167c5b03a1257458d1609e111d Mon Sep 17 00:00:00 2001 From: Rijwal Sangey Date: Sat, 11 Jul 2026 16:42:23 -0700 Subject: [PATCH 3/3] random --- firmware/logfs/src/logfs.c | 2 +- firmware/logfs/srcpp/logfs.cpp | 19 +++++++++++++++++-- firmware/logfs/srcpp/logfs.hpp | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/firmware/logfs/src/logfs.c b/firmware/logfs/src/logfs.c index 7c8a80815c..d51122662f 100644 --- a/firmware/logfs/src/logfs.c +++ b/firmware/logfs/src/logfs.c @@ -372,7 +372,7 @@ LogFsErr logfs_write(LogFs *fs, LogFsFile *file, const void *buf, uint32_t size) return LOGFS_ERR_OK; } -LogFsErr( +LogFsErr logfs_read( LogFs *fs, LogFsFile *file, void *buf, diff --git a/firmware/logfs/srcpp/logfs.cpp b/firmware/logfs/srcpp/logfs.cpp index 20a487be5f..ee8eaf7ae4 100644 --- a/firmware/logfs/srcpp/logfs.cpp +++ b/firmware/logfs/srcpp/logfs.cpp @@ -2,11 +2,26 @@ #include #include +#include namespace LogFs { -FileSystem::FileSystem(const BlockDevice::Cfg& cfg_in) +[[maybe_unused]] static void initFile(File& file, const FileCfg& cfg, const OpenFlags flags) +{ + file.cache.cached_addr = INVALID_BLOCK; + file.cache.buf = cfg.cache; + file.cache_data = reinterpret_cast(cfg.cache.data()); + file.is_open = false; + file.flags = static_cast(flags); + + const auto path_len = std::min(cfg.path.size(), static_cast(PATH_BYTES - 1)); + std::memcpy(file.path, cfg.path.data(), path_len); + file.path[path_len] = '\0'; +} + +// File System +LogFs::FileSystem::FileSystem(const BlockDevice::Cfg& cfg_in) : cfg(&cfg_in) { assert(cfg != nullptr); @@ -41,4 +56,4 @@ ErrorCode FileSystem::close(File&) return ErrorCode::NOT_OPEN; } -} // namespace LogFs \ No newline at end of file +} // namespace LogFs diff --git a/firmware/logfs/srcpp/logfs.hpp b/firmware/logfs/srcpp/logfs.hpp index ea54017182..9412e5a224 100644 --- a/firmware/logfs/srcpp/logfs.hpp +++ b/firmware/logfs/srcpp/logfs.hpp @@ -117,7 +117,7 @@ class BlockDevice class File { public: - File() = default; + File(){}; Cache cache{}; BlockData* cache_data = nullptr;