Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
File.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_FILE_HPP
18#define TOOLBOX_IO_FILE_HPP
19
20#include <toolbox/io/Buffer.hpp>
21#include <toolbox/io/Handle.hpp>
22#include <toolbox/sys/Error.hpp>
23#include <toolbox/contrib/libutil.h>
24
25#include <fcntl.h>
26#include <stdio.h>
27#include <sys/stat.h>
28
29namespace toolbox {
30namespace os {
31
33inline FileHandle open(const char* path, int flags, mode_t mode, std::error_code& ec) noexcept
34{
35 const auto fd = ::open(path, flags, mode);
36 if (fd < 0) {
38 }
39 return fd;
40}
41
43inline FileHandle open(const char* path, int flags, mode_t mode)
44{
45 const auto fd = ::open(path, flags, mode);
46 if (fd < 0) {
47 throw std::system_error{make_error(errno), "open"};
48 }
49 return fd;
50}
51
53inline FileHandle open(const char* path, int flags, std::error_code& ec) noexcept
54{
55 const auto fd = ::open(path, flags);
56 if (fd < 0) {
58 }
59 return fd;
60}
61
63inline FileHandle open(const char* path, int flags)
64{
65 const auto fd = ::open(path, flags);
66 if (fd < 0) {
67 throw std::system_error{make_error(errno), "open"};
68 }
69 return fd;
70}
71
72inline void rename(const char* oldpath, const char* newpath, std::error_code& ec)
73{
74 const auto ret = ::rename(oldpath, newpath);
75 if (ret < 0) {
77 }
78}
79
80inline void rename(const char* oldpath, const char* newpath)
81{
82 const auto ret = ::rename(oldpath, newpath);
83 if (ret < 0) {
84 throw std::system_error{make_error(errno), "rename"};
85 }
86}
87
88inline int remove(const char* path)
89{
90 const auto ret = ::remove(path);
91 if (ret < 0) {
92 throw std::system_error{make_error(errno), "remove"};
93 }
94 return ret;
95}
96
97inline int remove(const char* path, std::error_code& ec) noexcept
98{
99 const auto ret = ::remove(path);
100 if (ret < 0) {
102 }
103 return ret;
104}
105
107inline std::pair<FileHandle, FileHandle> pipe2(int flags, std::error_code& ec) noexcept
108{
109 int pipefd[2];
110 if (::pipe2(pipefd, flags) < 0) {
112 }
113 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
114}
115
117inline std::pair<FileHandle, FileHandle> pipe2(int flags)
118{
119 int pipefd[2];
120 if (::pipe2(pipefd, flags) < 0) {
121 throw std::system_error{make_error(errno), "pipe2"};
122 }
123 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
124}
125
127inline void fstat(int fd, struct stat& statbuf, std::error_code& ec) noexcept
128{
129 const auto ret = ::fstat(fd, &statbuf);
130 if (ret < 0) {
132 }
133}
134
136inline void fstat(int fd, struct stat& statbuf)
137{
138 const auto ret = ::fstat(fd, &statbuf);
139 if (ret < 0) {
140 throw std::system_error{make_error(errno), "fstat"};
141 }
142}
143
145inline void ftruncate(int fd, off_t length, std::error_code& ec) noexcept
146{
147 const auto ret = ::ftruncate(fd, length);
148 if (ret < 0) {
150 }
151}
152
154inline void ftruncate(int fd, off_t length)
155{
156 const auto ret = ::ftruncate(fd, length);
157 if (ret < 0) {
158 throw std::system_error{make_error(errno), "ftruncate"};
159 }
160}
161
163inline ssize_t read(int fd, void* buf, std::size_t len, std::error_code& ec) noexcept
164{
165 const auto ret = ::read(fd, buf, len);
166 if (ret < 0) {
168 }
169 return ret;
170}
171
173inline std::size_t read(int fd, void* buf, std::size_t len)
174{
175 const auto ret = ::read(fd, buf, len);
176 if (ret < 0) {
177 throw std::system_error{make_error(errno), "read"};
178 }
179 return ret;
180}
181
183inline ssize_t read(int fd, MutableBuffer buf, std::error_code& ec) noexcept
184{
185 return read(fd, static_cast<void*>(buf.data()), buffer_size(buf), ec);
186}
187
189inline std::size_t read(int fd, MutableBuffer buf) noexcept
190{
191 return read(fd, static_cast<void*>(buf.data()), buffer_size(buf));
192}
193
195inline ssize_t write(int fd, const void* buf, std::size_t len, std::error_code& ec) noexcept
196{
197 const auto ret = ::write(fd, buf, len);
198 if (ret < 0) {
200 }
201 return ret;
202}
203
205inline std::size_t write(int fd, const void* buf, std::size_t len)
206{
207 const auto ret = ::write(fd, buf, len);
208 if (ret < 0) {
209 throw std::system_error{make_error(errno), "write"};
210 }
211 return ret;
212}
213
215inline ssize_t write(int fd, ConstBuffer buf, std::error_code& ec) noexcept
216{
217 return write(fd, static_cast<const void*>(buf.data()), buffer_size(buf), ec);
218}
219
221inline std::size_t write(int fd, ConstBuffer buf)
222{
223 return write(fd, static_cast<const void*>(buf.data()), buffer_size(buf));
224}
225
227inline int fcntl(int fd, int cmd, std::error_code& ec) noexcept
228{
229 const auto ret = ::fcntl(fd, cmd);
230 if (ret < 0) {
232 }
233 return ret;
234}
235
237template <typename ArgT>
238inline int fcntl(int fd, int cmd, ArgT arg, std::error_code& ec) noexcept
239{
240 const auto ret = ::fcntl(fd, cmd, arg);
241 if (ret < 0) {
243 }
244 return ret;
245}
246
248inline int fcntl(int fd, int cmd)
249{
250 const auto ret = ::fcntl(fd, cmd);
251 if (ret < 0) {
252 throw std::system_error{make_error(errno), "fcntl"};
253 }
254 return ret;
255}
256
258template <typename ArgT>
259inline int fcntl(int fd, int cmd, ArgT arg)
260{
261 const auto ret = ::fcntl(fd, cmd, arg);
262 if (ret < 0) {
263 throw std::system_error{make_error(errno), "fcntl"};
264 }
265 return ret;
266}
267
268} // namespace os
269inline namespace io {
270
272inline std::size_t file_size(int fd)
273{
274 struct stat st; // NOLINT(hicpp-member-init)
275 os::fstat(fd, st);
276 return st.st_size;
277}
278
281{
282 mode_t mode{umask(0)};
283 umask(mode);
284 return mode;
285}
286
287inline void set_non_block(int fd, std::error_code& ec) noexcept
288{
290}
291
292inline void set_non_block(int fd)
293{
295}
296
297// Reliably open and lock a file
298inline FileHandle flopen(const char* path, int flags, mode_t mode, std::error_code& ec)
299{
300 // flopen comes from library 'libbsd'
301 const auto fd = ::flopen(path, flags, mode);
302 if (fd < 0) {
304 }
305 return fd;
306}
307
308// Reliably open and lock a file
309inline FileHandle flopen(const char* path, int flags, mode_t mode)
310{
311 // flopen comes from library 'libbsd'
312 const auto fd = ::flopen(path, flags, mode);
313 if (fd < 0) {
314 throw std::system_error{make_error(errno), "flopen"};
315 }
316 return fd;
317}
318
319
320} // namespace io
321} // namespace toolbox
322
323#endif // TOOLBOX_IO_FILE_HPP
std::size_t file_size(int fd)
Get file size.
Definition File.hpp:272
boost::asio::const_buffer ConstBuffer
Definition Buffer.hpp:28
boost::asio::mutable_buffer MutableBuffer
Definition Buffer.hpp:29
void set_non_block(int fd, std::error_code &ec) noexcept
Definition File.hpp:287
FileHandle flopen(const char *path, int flags, mode_t mode, std::error_code &ec)
Definition File.hpp:298
mode_t file_mode() noexcept
Get current file mode.
Definition File.hpp:280
void ftruncate(int fd, off_t length, std::error_code &ec) noexcept
Truncate a file to a specified length.
Definition File.hpp:145
ssize_t read(int fd, void *buf, std::size_t len, std::error_code &ec) noexcept
Read from a file descriptor.
Definition File.hpp:163
FileHandle open(const char *path, int flags, mode_t mode, std::error_code &ec) noexcept
Open and possibly create a file.
Definition File.hpp:33
ssize_t write(int fd, const void *buf, std::size_t len, std::error_code &ec) noexcept
Write to a file descriptor.
Definition File.hpp:195
std::pair< FileHandle, FileHandle > pipe2(int flags, std::error_code &ec) noexcept
Create pipe.
Definition File.hpp:107
int fcntl(int fd, int cmd, std::error_code &ec) noexcept
File control.
Definition File.hpp:227
void fstat(int fd, struct stat &statbuf, std::error_code &ec) noexcept
Get file status.
Definition File.hpp:127
int remove(const char *path)
Definition File.hpp:88
void rename(const char *oldpath, const char *newpath, std::error_code &ec)
Definition File.hpp:72
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr auto bind() noexcept
Definition Slot.hpp:97