oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
fs.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#ifdef CONFIG_OVE_FS
17
18#include <ove/fs.h>
19#include <ove/types.hpp>
20
21namespace ove {
22
30namespace fs {
31
38[[nodiscard]] inline int mount(const char *dev_path,
39 const char *mount_point) {
40 return ove_fs_mount(dev_path, mount_point);
41}
42
47inline void unmount(const char *mount_point) {
48 ove_fs_unmount(mount_point);
49}
50
56[[nodiscard]] inline int unlink(const char *path) {
57 return ove_fs_unlink(path);
58}
59
66[[nodiscard]] inline int rename(const char *old_path,
67 const char *new_path) {
68 return ove_fs_rename(old_path, new_path);
69}
70
71} /* namespace fs */
72
83class File {
84public:
88 File() : handle_(nullptr) {}
89
93 ~File() { close(); }
94
95 File(const File &) = delete;
96 File &operator=(const File &) = delete;
97
102 File(File &&other) noexcept : handle_(other.handle_) {
103 other.handle_ = nullptr;
104 }
105
111 File &operator=(File &&other) noexcept {
112 if (this != &other) {
113 close();
114 handle_ = other.handle_;
115 other.handle_ = nullptr;
116 }
117 return *this;
118 }
119
126 [[nodiscard]] int open(const char *path, int flags) {
127 return ove_fs_open(&handle_, path, flags);
128 }
129
137 int close() {
138 int ret = OVE_OK;
139 if (handle_) {
140 ret = ove_fs_close(handle_);
141 handle_ = nullptr;
142 }
143 return ret;
144 }
145
153 [[nodiscard]] int read(void *buf, size_t count, size_t *bytes_read) {
154 return ove_fs_read(handle_, buf, count, bytes_read);
155 }
156
164 [[nodiscard]] int write(const void *buf, size_t count,
165 size_t *bytes_written) {
166 return ove_fs_write(handle_, buf, count, bytes_written);
167 }
168
175 [[nodiscard]] int seek(long offset, int whence) {
176 return ove_fs_seek(handle_, offset, whence);
177 }
178
183 long tell() {
184 return ove_fs_tell(handle_);
185 }
186
192 [[nodiscard]] int size(size_t *out_size) {
193 return ove_fs_size(handle_, out_size);
194 }
195
200 bool valid() const { return handle_ != nullptr; }
201
206 ove_file_t handle() const { return handle_; }
207
208private:
209 ove_file_t handle_;
210};
211
221class Dir {
222public:
226 Dir() : handle_(nullptr) {}
227
231 ~Dir() { close(); }
232
233 Dir(const Dir &) = delete;
234 Dir &operator=(const Dir &) = delete;
235
240 Dir(Dir &&other) noexcept : handle_(other.handle_) {
241 other.handle_ = nullptr;
242 }
243
249 Dir &operator=(Dir &&other) noexcept {
250 if (this != &other) {
251 close();
252 handle_ = other.handle_;
253 other.handle_ = nullptr;
254 }
255 return *this;
256 }
257
263 [[nodiscard]] int open(const char *path) {
264 return ove_fs_opendir(&handle_, path);
265 }
266
274 int close() {
275 int ret = OVE_OK;
276 if (handle_) {
277 ret = ove_fs_closedir(handle_);
278 handle_ = nullptr;
279 }
280 return ret;
281 }
282
289 [[nodiscard]] int readdir(struct ove_dirent *entry) {
290 return ove_fs_readdir(handle_, entry);
291 }
292
297 bool valid() const { return handle_ != nullptr; }
298
303 ove_dir_t handle() const { return handle_; }
304
305private:
306 ove_dir_t handle_;
307};
308
309} /* namespace ove */
310
311#endif /* CONFIG_OVE_FS */
RAII wrapper around an oveRTOS directory handle.
Definition fs.hpp:221
int close()
Closes the directory and invalidates the handle.
Definition fs.hpp:274
int open(const char *path)
Opens a directory at the specified path.
Definition fs.hpp:263
Dir & operator=(Dir &&other) noexcept
Move-assignment operator — closes the current directory and takes ownership.
Definition fs.hpp:249
int readdir(struct ove_dirent *entry)
Reads the next entry from the directory.
Definition fs.hpp:289
Dir(Dir &&other) noexcept
Move constructor — transfers ownership of the directory handle.
Definition fs.hpp:240
ove_dir_t handle() const
Returns the raw oveRTOS directory handle.
Definition fs.hpp:303
~Dir()
Destroys the Dir object, closing the directory if it is still open.
Definition fs.hpp:231
bool valid() const
Returns true if the directory handle is valid.
Definition fs.hpp:297
Dir()
Constructs a Dir object with no open directory (invalid state).
Definition fs.hpp:226
RAII wrapper around an oveRTOS file handle.
Definition fs.hpp:83
int seek(long offset, int whence)
Repositions the file offset.
Definition fs.hpp:175
int open(const char *path, int flags)
Opens a file at the specified path.
Definition fs.hpp:126
long tell()
Returns the current file offset.
Definition fs.hpp:183
int write(const void *buf, size_t count, size_t *bytes_written)
Writes bytes to the file at the current position.
Definition fs.hpp:164
int read(void *buf, size_t count, size_t *bytes_read)
Reads bytes from the file at the current position.
Definition fs.hpp:153
bool valid() const
Returns true if the file handle is valid (file is open).
Definition fs.hpp:200
int size(size_t *out_size)
Returns the size of the file.
Definition fs.hpp:192
int close()
Closes the file and invalidates the handle.
Definition fs.hpp:137
File()
Constructs a File object with no open file (invalid state).
Definition fs.hpp:88
~File()
Destroys the file object, closing the file if it is still open.
Definition fs.hpp:93
File & operator=(File &&other) noexcept
Move-assignment operator — closes the current file and takes ownership.
Definition fs.hpp:111
File(File &&other) noexcept
Move constructor — transfers ownership of the file handle.
Definition fs.hpp:102
ove_file_t handle() const
Returns the raw oveRTOS file handle.
Definition fs.hpp:206
int mount(const char *dev_path, const char *mount_point)
Mounts a filesystem at the given mount point.
Definition fs.hpp:38
void unmount(const char *mount_point)
Unmounts a previously mounted filesystem.
Definition fs.hpp:47
int rename(const char *old_path, const char *new_path)
Renames or moves a file or directory.
Definition fs.hpp:66
int unlink(const char *path)
Deletes a file from the filesystem.
Definition fs.hpp:56
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.