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#include <ove/error.hpp>
21
22namespace ove
23{
24
32namespace fs
33{
34
42[[nodiscard]] inline Result<void> mount(const char *dev_path, const char *mount_point) noexcept
43{
44 return from_rc(ove_fs_mount(dev_path, mount_point));
45}
46
51inline void unmount(const char *mount_point)
52{
53 ove_fs_unmount(mount_point);
54}
55
62[[nodiscard]] inline Result<void> unlink(const char *path) noexcept
63{
64 return from_rc(ove_fs_unlink(path));
65}
66
74[[nodiscard]] inline Result<void> rename(const char *old_path, const char *new_path) noexcept
75{
76 return from_rc(ove_fs_rename(old_path, new_path));
77}
78
79} /* namespace fs */
80
91class File
92{
93 public:
97 File() : handle_(nullptr)
98 {
99 }
100
104 ~File() noexcept
105 {
106 close();
107 }
108
109 File(const File &) = delete;
110 File &operator=(const File &) = delete;
111
116 File(File &&other) noexcept : handle_(other.handle_)
117 {
118 other.handle_ = nullptr;
119 }
120
126 File &operator=(File &&other) noexcept
127 {
128 if (this != &other) {
129 close();
130 handle_ = other.handle_;
131 other.handle_ = nullptr;
132 }
133 return *this;
134 }
135
143 [[nodiscard]] Result<void> open(const char *path, int flags) noexcept
144 {
145 return from_rc(ove_fs_open(&handle_, path, flags));
146 }
147
157 void close() noexcept
158 {
159 if (handle_) {
160 (void)ove_fs_close(handle_);
161 handle_ = nullptr;
162 }
163 }
164
173 [[nodiscard]] Result<size_t> read(void *buf, size_t count) noexcept
174 {
175 size_t bytes_read = 0;
176 const int rc = ove_fs_read(handle_, buf, count, &bytes_read);
177 return from_rc(rc, bytes_read);
178 }
179
187 [[nodiscard]] Result<size_t> write(const void *buf, size_t count) noexcept
188 {
189 size_t bytes_written = 0;
190 const int rc = ove_fs_write(handle_, buf, count, &bytes_written);
191 return from_rc(rc, bytes_written);
192 }
193
201 [[nodiscard]] Result<void> seek(long offset, int whence) noexcept
202 {
203 return from_rc(ove_fs_seek(handle_, offset, whence));
204 }
205
210 long tell()
211 {
212 return ove_fs_tell(handle_);
213 }
214
220 [[nodiscard]] Result<size_t> size() noexcept
221 {
222 size_t out_size = 0;
223 const int rc = ove_fs_size(handle_, &out_size);
224 return from_rc(rc, out_size);
225 }
226
231 bool valid() const
232 {
233 return handle_ != nullptr;
234 }
235
240 ove_file_t handle() const
241 {
242 return handle_;
243 }
244
245 private:
246 ove_file_t handle_ = nullptr;
247};
248
258class Dir
259{
260 public:
264 Dir() : handle_(nullptr)
265 {
266 }
267
271 ~Dir() noexcept
272 {
273 close();
274 }
275
276 Dir(const Dir &) = delete;
277 Dir &operator=(const Dir &) = delete;
278
283 Dir(Dir &&other) noexcept : handle_(other.handle_)
284 {
285 other.handle_ = nullptr;
286 }
287
293 Dir &operator=(Dir &&other) noexcept
294 {
295 if (this != &other) {
296 close();
297 handle_ = other.handle_;
298 other.handle_ = nullptr;
299 }
300 return *this;
301 }
302
309 [[nodiscard]] Result<void> open(const char *path) noexcept
310 {
311 return from_rc(ove_fs_opendir(&handle_, path));
312 }
313
320 void close() noexcept
321 {
322 if (handle_) {
323 (void)ove_fs_closedir(handle_);
324 handle_ = nullptr;
325 }
326 }
327
336 [[nodiscard]] Result<bool> readdir(struct ove_dirent *entry) noexcept
337 {
338 const int rc = ove_fs_readdir(handle_, entry);
339 if (rc == OVE_OK)
340 return true;
341 if (rc == OVE_ERR_EOF)
342 return false;
343 return std::unexpected{static_cast<Error>(rc)};
344 }
345
350 bool valid() const
351 {
352 return handle_ != nullptr;
353 }
354
359 ove_dir_t handle() const
360 {
361 return handle_;
362 }
363
364 private:
365 ove_dir_t handle_ = nullptr;
366};
367
368} /* namespace ove */
369
370#endif /* CONFIG_OVE_FS */
RAII wrapper around an oveRTOS directory handle.
Definition fs.hpp:259
Result< bool > readdir(struct ove_dirent *entry) noexcept
Reads the next entry from the directory.
Definition fs.hpp:336
~Dir() noexcept
Destroys the Dir object, closing the directory if it is still open.
Definition fs.hpp:271
Dir & operator=(Dir &&other) noexcept
Move-assignment operator — closes the current directory and takes ownership.
Definition fs.hpp:293
Dir(Dir &&other) noexcept
Move constructor — transfers ownership of the directory handle.
Definition fs.hpp:283
ove_dir_t handle() const
Returns the raw oveRTOS directory handle.
Definition fs.hpp:359
void close() noexcept
Closes the directory and invalidates the handle.
Definition fs.hpp:320
bool valid() const
Returns true if the directory handle is valid.
Definition fs.hpp:350
Result< void > open(const char *path) noexcept
Opens a directory at the specified path.
Definition fs.hpp:309
Dir()
Constructs a Dir object with no open directory (invalid state).
Definition fs.hpp:264
RAII wrapper around an oveRTOS file handle.
Definition fs.hpp:92
long tell()
Returns the current file offset.
Definition fs.hpp:210
void close() noexcept
Closes the file and invalidates the handle.
Definition fs.hpp:157
Result< size_t > write(const void *buf, size_t count) noexcept
Writes bytes to the file at the current position.
Definition fs.hpp:187
bool valid() const
Returns true if the file handle is valid (file is open).
Definition fs.hpp:231
File()
Constructs a File object with no open file (invalid state).
Definition fs.hpp:97
Result< void > open(const char *path, int flags) noexcept
Opens a file at the specified path.
Definition fs.hpp:143
Result< size_t > size() noexcept
Returns the size of the file.
Definition fs.hpp:220
File & operator=(File &&other) noexcept
Move-assignment operator — closes the current file and takes ownership.
Definition fs.hpp:126
Result< size_t > read(void *buf, size_t count) noexcept
Reads bytes from the file at the current position.
Definition fs.hpp:173
~File() noexcept
Destroys the file object, closing the file if it is still open.
Definition fs.hpp:104
File(File &&other) noexcept
Move constructor — transfers ownership of the file handle.
Definition fs.hpp:116
ove_file_t handle() const
Returns the raw oveRTOS file handle.
Definition fs.hpp:240
Result< void > seek(long offset, int whence) noexcept
Repositions the file offset.
Definition fs.hpp:201
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
void unmount(const char *mount_point)
Unmounts a previously mounted filesystem.
Definition fs.hpp:51
Result< void > mount(const char *dev_path, const char *mount_point) noexcept
Mounts a filesystem at the given mount point.
Definition fs.hpp:42
Result< void > unlink(const char *path) noexcept
Deletes a file from the filesystem.
Definition fs.hpp:62
Result< void > rename(const char *old_path, const char *new_path) noexcept
Renames or moves a file or directory.
Definition fs.hpp:74
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
Error
Strong-typed mirror of substrate OVE_ERR_* codes.
Definition error.hpp:64
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.