Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
file_handle.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
7
8#include <cstdint>
9#include <utility>
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/byte.hpp"
13#include "arene/base/filesystem/error_code.hpp"
14#include "arene/base/result.hpp"
15#include "arene/base/span.hpp"
16// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
17
18// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
19
20namespace arene {
21namespace base {
22namespace filesystem {
23
24// parasoft-begin-suppress AUTOSAR-A12_1_5-a-2 "Delegating constructors would not reduce duplication"
25/// @brief An RAII wrapper around a POSIX file handle with unique-ownership semantics.
26class file_handle {
27 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "Using an int for compatibility with OS functions"
28 /// @brief The OS file handle type
29 using os_file_handle = int;
30 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
31
32 public:
33 // parasoft-begin-suppress AUTOSAR-M11_0_1-a-2 "False positive: this is not 'member data', it is a public property"
34 /// @brief An invalid handle value
35 static constexpr os_file_handle invalid_handle_value{-1};
36 // parasoft-end-suppress AUTOSAR-M11_0_1-a-2
37
38 /// @brief Default construct with an invalid value
39 /// @post <c> get_fd() == invalid_handle_value </c>
40 file_handle() noexcept
41 : fd_(invalid_handle_value) {}
42
43 /// @brief Take ownership of the specified handle
44 /// @param file_descriptor The file handle to own
45 /// @post <c> get_fd() == file_description </c>
46 explicit file_handle(os_file_handle const file_descriptor) noexcept
47 : fd_(file_descriptor) {}
48
49 /// @brief Close the handle if there is one
50 /// @post If <c> valid() == true </c>, the file descriptor will be closed using underlying OS mechanisms.
51 ~file_handle();
52
53 /// @brief Transfer ownership of the handle from @c other to @c *this.
54 /// @param other the other @c file_handle to take ownership from
55 file_handle(file_handle&& other) noexcept
56 : fd_(std::exchange(other.fd_, invalid_handle_value)) {}
57
58 /// @brief Take ownership of the file handle from @c other.
59 /// @param other the other @c file_handle to take ownership from
60 /// @return @c *this
61 /// @post If @c *this held a file handle before assignment, it is closed.
62 /// @post @c get_fd() will return the value previously held by @c other
63 auto operator=(file_handle&& other) noexcept -> file_handle& {
64 file_handle temp{std::move(other)};
65 std::swap(fd_, temp.fd_);
66 return *this;
67 }
68
69 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
70 /// @brief Not copyable
71 file_handle(file_handle const&) = delete;
72 /// @brief Not copyable
73 auto operator=(file_handle const&) -> file_handle& = delete;
74 // parasoft-end-suppress CERT_C-EXP37-a-3
75
76 /// @brief Get the wrapped handle.
77 /// @return the handle value.
78 auto get_fd() const noexcept -> os_file_handle { return fd_; }
79
80 /// @brief Release ownership of the handle, if held.
81 /// @post <c> get_fd() == invalid_handle_value </c>.
82 void release() noexcept { fd_ = invalid_handle_value; }
83
84 /// @brief Check if the handle currently holds a file descriptor
85 /// @return true if <c> get_fd() != invalid_handle_value </c>.
86 /// @return false otherwise.
87 auto valid() const noexcept -> bool { return fd_ != invalid_handle_value; }
88
89 /// @brief Read from the file handle, starting at the specified position, into the provided buffer
90 /// @param buffer A view onto a buffer to write the read bytes into into. The read will never be larger then
91 /// the provided span.
92 /// @param position The position in the file to read from
93 /// @return result<span<const byte>, error_code> A @c result holding either:
94 /// - A sub-span of @c buffer that is a view onto to the actual data read.
95 /// - An @c error_code indicating the reason for failure to read.
96 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
97 auto read_at(span<byte> buffer, std::uint64_t const position) const noexcept -> result<span<byte>, error_code>;
98
99 /// @brief Write data from the provided buffer to the file referenced by the handle, starting at the specified
100 /// position in the file.
101 /// @param data The data to write
102 /// @param position The position in the file to write at
103 /// @return result<span<const byte>, error_code> A @c result holding either:
104 /// - A sub-span of @c data that refers to the remaining unwritten data for a successful write, if any.
105 /// If all data was written, this span will be empty.
106 /// - An @c error_code indicating the reason for failure to write.
107 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
108 auto write_at(span<byte const> data, std::uint64_t const position) const noexcept
109 -> result<span<byte const>, error_code>;
110
111 /// @brief Read from the file handle, starting at the current sequential access position, into the provided buffer.
112 /// Updates the sequential access position to the end of the read data.
113 /// @param buffer The buffer to read into. The read will never be larger then the provided buffer.
114 /// @return result<span<byte>, error_code> A @c result holding either:
115 /// - A sub-span of @c buffer that is a view onto to the actual data read.
116 /// - An @c error_code indicating the reason for failure to read.
117 auto sequential_read(span<byte> buffer) const noexcept -> result<span<byte>, error_code>;
118
119 /// @brief Write data from the provided buffer to the file referenced by the handle, starting at the current
120 /// sequential access position. Updates the sequential access position to the end of the written data.
121 /// @param data The data to write
122 /// @return result<span<const byte>, error_code> A @c result holding either:
123 /// - A sub-span of @c data that refers to the remaining unwritten data for a successful write, if any.
124 /// If all data was written, this span will be empty.
125 /// - An @c error_code indicating the reason for failure to write.
126 auto sequential_write(span<byte const> data) const noexcept -> result<span<byte const>, error_code>;
127
128 /// @brief Move the sequential access position to the specified offset from the start of the file.
129 /// @param position The position in the file to use for subsequent sequential access
130 /// @return result<void, error_code> A @c result that is either:
131 /// - An empty value for success
132 /// - An @c error_code indicating the reason for failure if the seek failed.
133 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
134 auto sequential_seek(std::uint64_t const position) const noexcept -> result<void, error_code>;
135
136 /// @brief Flush the file handle
137 /// @return result<void, error_code> A @c result that is either:
138 /// - An empty value for success
139 /// - An @c error_code indicating the reason for failure if the flush failed.
140 auto flush() const noexcept -> result<void, error_code>;
141
142 /// @brief Close the file handle, if there is one.
143 /// @return result<void, error_code> A @c result that is either:
144 /// - An empty value for success.
145 /// - An @c error_code indicating the reason for failure if the handle could not be closed.
146 auto close() noexcept -> result<void, error_code>;
147
148 /// @brief Get the size of the open file
149 /// @return A @c result holding the file size on success, or an @c error_code
150 /// indicating the reason for failure.
151 auto size() const noexcept -> result<std::uint64_t, error_code>;
152
153 private:
154 /// @brief The stored handle
155 os_file_handle fd_;
156};
157// parasoft-end-suppress AUTOSAR-A12_1_5-a-2
158
159} // namespace filesystem
160} // namespace base
161} // namespace arene
162
163#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FILESYSTEM_FILE_HANDLE_HPP_
Definition directory_handle.cpp:39
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10