24#include <etl/string.h>
27#include <zephyr/fs/fs.h>
28#include <zephyr/kernel.h>
36 static constexpr size_t SECTOR_SIZE = CONFIG_EMMC_SECTOR_SIZE;
42 file(
const etl::istring &path) : m_file{}
44 if (path.size() >= CONFIG_STORAGE_MAX_PATH_LEN) {
48 struct fs_dirent entry;
50 int rc = fs_stat(path.c_str(), &entry);
51 if (rc < 0 || entry.type != FS_DIR_ENTRY_FILE) {
55 fs_file_t_init(&m_file);
57 rc = fs_open(&m_file, path.c_str(), FS_O_READ);
66 struct fs_file_t m_file;
75 fs_dir_t_init(&m_dir);
77 if (path.size() >= CONFIG_STORAGE_MAX_PATH_LEN) {
81 int rc = fs_opendir(&m_dir, path.c_str());
90 struct fs_dir_t m_dir;
124 truncate(
const etl::istring &path,
size_t len);
127 mkdir(
const etl::istring &path);
130 rm(
const etl::istring &path);
133 rmdir(
const etl::istring &path);
136 write(
const etl::istring &path,
const uint8_t *b,
size_t len);
139 write(
const etl::istring &path,
const uint8_t *b,
size_t len,
size_t offset);
142 ls(
const etl::istring &path, etl::istring &res);
148 is_file(
const etl::istring &path);
151 is_dir(
const etl::istring &path);
154 du(
const etl::istring &path);
157 read(
const etl::istring &path, uint8_t *b,
size_t len,
size_t offset);
163 read(
file &f, uint8_t *b,
size_t len);
166 write_raw(
const uint8_t *b,
size_t start_sector,
size_t len);
169 read_raw(uint8_t *b,
size_t start_sector,
size_t sectors);
196 error_msg{
"Directory deletion did not complete",
"strgdeld",
229 error_msg{
"Failed to (un)mount storage device",
"strgmnt", err})
269 etl::string<CONFIG_STORAGE_MAX_PATH_LEN> m_pname;
270 mutable struct k_mutex m_mtx;
278 rm_priv(
const etl::istring &path);
A class representing error messages in the SatNOGS-COMMS system.
severity
Severity levels of exceptions.
@ MINOR
Failure having minimal impact.
@ MAJOR
Failure causing minor mission degradation.
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
delete_dir_exception(const char *file_name, int line)
delete_file_exception(const char *file_name, int line)
directory(const etl::istring &path)
file(const etl::istring &path)
fs_exception(const char *file_name, int line)
max_path_exception(const char *file_name, int line)
mount_exception(const char *file_name, int line, int err)
not_ready_exception(const char *file_name, int line)
open_dir_exception(const char *file_name, int line)
read_exception(const char *file_name, int line)
size_t read(const etl::istring &path, uint8_t *b, size_t len, size_t offset)
Reads from a file starting from specific offset.
void ls(const etl::istring &path, etl::istring &res)
Lists the contents of a directory.
void truncate(const etl::istring &path, size_t len)
Truncate a file by removing bytes from its end.
uint64_t du(const etl::istring &path)
Calculate the disk usage of a file or directory likewise the 'du' command of Linux.
size_t read_raw(uint8_t *b, size_t start_sector, size_t sectors)
Reads directly raw sectors from the eMMC, bypassing any filesystem.
bool enabled() const
Checks if the storage subsystem is enabled.
void rmdir(const etl::istring &path)
Removes a directory and all its contents.
float utilization()
Get eMMC storage utilization of the partition used by the MCU.
void mkdir(const etl::istring &path)
Creates a directory at the specified path.
bool ready() const
Checks if the storage subsystem is ready to accept filesystem operations for the MCU side.
void operator=(storage const &)=delete
size_t write_raw(const uint8_t *b, size_t start_sector, size_t len)
Writes raw bytes on the eMMC.
static storage & get_instance()
lib::emmc::dir get_dir() const
Get the direction of the eMMC.
size_t write(const etl::istring &path, const uint8_t *b, size_t len)
Writes data to a file.
size_t sectors_num() const
Returns the number of total sectors of the eMMC.
void erase_sectors(size_t start_sector, size_t sectors)
Erase a specific number of sectors.
void enable(bool en)
Enable or disable the storage subsystem.
bool is_file(const etl::istring &path)
Check if a path is a file.
bool mounted() const
Checks if the storage subsystem is mounted.
void rm(const etl::istring &path)
Removes a file from the filesystem.
bool is_dir(const etl::istring &path)
Check if a path is a directory.
static constexpr size_t SECTOR_SIZE
void set_dir(lib::emmc::dir d)
Set the direction of the eMMC.
storage(storage const &)=delete
#define ESTORAGE
Error on storage.