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
24#include <toolbox/contrib/libutil.h>
25
26#include <fcntl.h>
27#include <stdio.h>
28#include <sys/stat.h>
29
30namespace toolbox {
31namespace os {
32
34inline FileHandle open(const char* path, int flags, mode_t mode, std::error_code& ec) noexcept
35{
36 const auto fd = ::open(path, flags, mode);
37 if (fd < 0) {
39 }
40 return fd;
41}
42
44inline FileHandle open(const char* path, int flags, mode_t mode)
45{
46 const auto fd = ::open(path, flags, mode);
47 if (fd < 0) {
48 throw std::system_error{make_error(errno), "open"};
49 }
50 return fd;
51}
52
54inline FileHandle open(const char* path, int flags, std::error_code& ec) noexcept
55{
56 const auto fd = ::open(path, flags);
57 if (fd < 0) {
59 }
60 return fd;
61}
62
64inline FileHandle open(const char* path, int flags)
65{
66 const auto fd = ::open(path, flags);
67 if (fd < 0) {
68 throw std::system_error{make_error(errno), "open"};
69 }
70 return fd;
71}
72
73inline void rename(const char* oldpath, const char* newpath, std::error_code& ec)
74{
75 const auto ret = ::rename(oldpath, newpath);
76 if (ret < 0) {
78 }
79}
80
81inline void rename(const char* oldpath, const char* newpath)
82{
83 const auto ret = ::rename(oldpath, newpath);
84 if (ret < 0) {
85 throw std::system_error{make_error(errno), "rename"};
86 }
87}
88
89inline int remove(const char* path)
90{
91 const auto ret = ::remove(path);
92 if (ret < 0) {
93 throw std::system_error{make_error(errno), "remove"};
94 }
95 return ret;
96}
97
98inline int remove(const char* path, std::error_code& ec) noexcept
99{
100 const auto ret = ::remove(path);
101 if (ret < 0) {
103 }
104 return ret;
105}
106
108inline std::pair<FileHandle, FileHandle> pipe2(int flags, std::error_code& ec) noexcept
109{
110 int pipefd[2];
111 if (::pipe2(pipefd, flags) < 0) {
113 }
114 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
115}
116
118inline std::pair<FileHandle, FileHandle> pipe2(int flags)
119{
120 int pipefd[2];
121 if (::pipe2(pipefd, flags) < 0) {
122 throw std::system_error{make_error(errno), "pipe2"};
123 }
124 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
125}
126
128inline void fstat(int fd, struct stat& statbuf, std::error_code& ec) noexcept
129{
130 const auto ret = ::fstat(fd, &statbuf);
131 if (ret < 0) {
133 }
134}
135
137inline void fstat(int fd, struct stat& statbuf)
138{
139 const auto ret = ::fstat(fd, &statbuf);
140 if (ret < 0) {
141 throw std::system_error{make_error(errno), "fstat"};
142 }
143}
144
146inline void ftruncate(int fd, off_t length, std::error_code& ec) noexcept
147{
148 const auto ret = ::ftruncate(fd, length);
149 if (ret < 0) {
151 }
152}
153
155inline void ftruncate(int fd, off_t length)
156{
157 const auto ret = ::ftruncate(fd, length);
158 if (ret < 0) {
159 throw std::system_error{make_error(errno), "ftruncate"};
160 }
161}
162
164inline ssize_t read(int fd, void* buf, std::size_t len, std::error_code& ec) noexcept
165{
166 const auto ret = ::read(fd, buf, len);
167 if (ret < 0) {
169 }
170 return ret;
171}
172
174inline std::size_t read(int fd, void* buf, std::size_t len)
175{
176 const auto ret = ::read(fd, buf, len);
177 if (ret < 0) {
178 throw std::system_error{make_error(errno), "read"};
179 }
180 return ret;
181}
182
184inline ssize_t read(int fd, MutableBuffer buf, std::error_code& ec) noexcept
185{
186 return read(fd, static_cast<void*>(buf.data()), buffer_size(buf), ec);
187}
188
190inline std::size_t read(int fd, MutableBuffer buf) noexcept
191{
192 return read(fd, static_cast<void*>(buf.data()), buffer_size(buf));
193}
194
196inline ssize_t write(int fd, const void* buf, std::size_t len, std::error_code& ec) noexcept
197{
198 const auto ret = ::write(fd, buf, len);
199 if (ret < 0) {
201 }
202 return ret;
203}
204
206inline std::size_t write(int fd, const void* buf, std::size_t len)
207{
208 const auto ret = ::write(fd, buf, len);
209 if (ret < 0) {
210 throw std::system_error{make_error(errno), "write"};
211 }
212 return ret;
213}
214
216inline ssize_t write(int fd, ConstBuffer buf, std::error_code& ec) noexcept
217{
218 return write(fd, static_cast<const void*>(buf.data()), buffer_size(buf), ec);
219}
220
222inline std::size_t write(int fd, ConstBuffer buf)
223{
224 return write(fd, static_cast<const void*>(buf.data()), buffer_size(buf));
225}
226
228inline int fcntl(int fd, int cmd, std::error_code& ec) noexcept
229{
230 const auto ret = ::fcntl(fd, cmd);
231 if (ret < 0) {
233 }
234 return ret;
235}
236
238template <typename ArgT>
239inline int fcntl(int fd, int cmd, ArgT arg, std::error_code& ec) noexcept
240{
241 const auto ret = ::fcntl(fd, cmd, arg);
242 if (ret < 0) {
244 }
245 return ret;
246}
247
249inline int fcntl(int fd, int cmd)
250{
251 const auto ret = ::fcntl(fd, cmd);
252 if (ret < 0) {
253 throw std::system_error{make_error(errno), "fcntl"};
254 }
255 return ret;
256}
257
259template <typename ArgT>
260inline int fcntl(int fd, int cmd, ArgT arg)
261{
262 const auto ret = ::fcntl(fd, cmd, arg);
263 if (ret < 0) {
264 throw std::system_error{make_error(errno), "fcntl"};
265 }
266 return ret;
267}
268
269} // namespace os
270inline namespace io {
271
273inline std::size_t file_size(int fd)
274{
275 struct stat st;
276 os::fstat(fd, st);
277 return st.st_size;
278}
279
282{
283 mode_t mode{umask(0)};
284 umask(mode);
285 return mode;
286}
287
288inline void set_non_block(int fd, std::error_code& ec) noexcept
289{
291}
292
293inline void set_non_block(int fd)
294{
296}
297
298// Reliably open and lock a file
299inline FileHandle flopen(const char* path, int flags, mode_t mode, std::error_code& ec)
300{
301 // flopen comes from library 'libbsd'
302 const auto fd = ::flopen(path, flags, mode);
303 if (fd < 0) {
305 }
306 return fd;
307}
308
309// Reliably open and lock a file
310inline FileHandle flopen(const char* path, int flags, mode_t mode)
311{
312 // flopen comes from library 'libbsd'
313 const auto fd = ::flopen(path, flags, mode);
314 if (fd < 0) {
315 throw std::system_error{make_error(errno), "flopen"};
316 }
317 return fd;
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:273
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:288
FileHandle flopen(const char *path, int flags, mode_t mode, std::error_code &ec)
Definition File.hpp:299
mode_t file_mode() noexcept
Get current file mode.
Definition File.hpp:281
void ftruncate(int fd, off_t length, std::error_code &ec) noexcept
Truncate a file to a specified length.
Definition File.hpp:146
ssize_t read(int fd, void *buf, std::size_t len, std::error_code &ec) noexcept
Read from a file descriptor.
Definition File.hpp:164
FileHandle open(const char *path, int flags, mode_t mode, std::error_code &ec) noexcept
Open and possibly create a file.
Definition File.hpp:34
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:196
std::pair< FileHandle, FileHandle > pipe2(int flags, std::error_code &ec) noexcept
Create pipe.
Definition File.hpp:108
int fcntl(int fd, int cmd, std::error_code &ec) noexcept
File control.
Definition File.hpp:228
void fstat(int fd, struct stat &statbuf, std::error_code &ec) noexcept
Get file status.
Definition File.hpp:128
int remove(const char *path)
Definition File.hpp:89
void rename(const char *oldpath, const char *newpath, std::error_code &ec)
Definition File.hpp:73
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr auto bind() noexcept
Definition Slot.hpp:97