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 <fcntl.h>
25#include <stdio.h>
26#include <sys/stat.h>
27
28namespace toolbox {
29namespace os {
30
32inline FileHandle open(const char* path, int flags, mode_t mode, std::error_code& ec) noexcept
33{
34 const auto fd = ::open(path, flags, mode);
35 if (fd < 0) {
37 }
38 return fd;
39}
40
42inline FileHandle open(const char* path, int flags, mode_t mode)
43{
44 const auto fd = ::open(path, flags, mode);
45 if (fd < 0) {
46 throw std::system_error{make_error(errno), "open"};
47 }
48 return fd;
49}
50
52inline FileHandle open(const char* path, int flags, std::error_code& ec) noexcept
53{
54 const auto fd = ::open(path, flags);
55 if (fd < 0) {
57 }
58 return fd;
59}
60
62inline FileHandle open(const char* path, int flags)
63{
64 const auto fd = ::open(path, flags);
65 if (fd < 0) {
66 throw std::system_error{make_error(errno), "open"};
67 }
68 return fd;
69}
70
71inline int remove(const char* path)
72{
73 const auto ret = ::remove(path);
74 if (ret < 0) {
75 throw std::system_error{make_error(errno), "remove"};
76 }
77 return ret;
78}
79
80inline int remove(const char* path, std::error_code& ec) noexcept
81{
82 const auto ret = ::remove(path);
83 if (ret < 0) {
85 }
86 return ret;
87}
88
90inline std::pair<FileHandle, FileHandle> pipe2(int flags, std::error_code& ec) noexcept
91{
92 int pipefd[2];
93 if (::pipe2(pipefd, flags) < 0) {
95 }
96 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
97}
98
100inline std::pair<FileHandle, FileHandle> pipe2(int flags)
101{
102 int pipefd[2];
103 if (::pipe2(pipefd, flags) < 0) {
104 throw std::system_error{make_error(errno), "pipe2"};
105 }
106 return {FileHandle{pipefd[0]}, FileHandle{pipefd[1]}};
107}
108
110inline void fstat(int fd, struct stat& statbuf, std::error_code& ec) noexcept
111{
112 const auto ret = ::fstat(fd, &statbuf);
113 if (ret < 0) {
115 }
116}
117
119inline void fstat(int fd, struct stat& statbuf)
120{
121 const auto ret = ::fstat(fd, &statbuf);
122 if (ret < 0) {
123 throw std::system_error{make_error(errno), "fstat"};
124 }
125}
126
128inline void ftruncate(int fd, off_t length, std::error_code& ec) noexcept
129{
130 const auto ret = ::ftruncate(fd, length);
131 if (ret < 0) {
133 }
134}
135
137inline void ftruncate(int fd, off_t length)
138{
139 const auto ret = ::ftruncate(fd, length);
140 if (ret < 0) {
141 throw std::system_error{make_error(errno), "ftruncate"};
142 }
143}
144
146inline ssize_t read(int fd, void* buf, std::size_t len, std::error_code& ec) noexcept
147{
148 const auto ret = ::read(fd, buf, len);
149 if (ret < 0) {
151 }
152 return ret;
153}
154
156inline std::size_t read(int fd, void* buf, std::size_t len)
157{
158 const auto ret = ::read(fd, buf, len);
159 if (ret < 0) {
160 throw std::system_error{make_error(errno), "read"};
161 }
162 return ret;
163}
164
166inline ssize_t read(int fd, MutableBuffer buf, std::error_code& ec) noexcept
167{
168 return read(fd, buffer_cast<void*>(buf), buffer_size(buf), ec);
169}
170
172inline std::size_t read(int fd, MutableBuffer buf) noexcept
173{
174 return read(fd, buffer_cast<void*>(buf), buffer_size(buf));
175}
176
178inline ssize_t write(int fd, const void* buf, std::size_t len, std::error_code& ec) noexcept
179{
180 const auto ret = ::write(fd, buf, len);
181 if (ret < 0) {
183 }
184 return ret;
185}
186
188inline std::size_t write(int fd, const void* buf, std::size_t len)
189{
190 const auto ret = ::write(fd, buf, len);
191 if (ret < 0) {
192 throw std::system_error{make_error(errno), "write"};
193 }
194 return ret;
195}
196
198inline ssize_t write(int fd, ConstBuffer buf, std::error_code& ec) noexcept
199{
200 return write(fd, buffer_cast<const void*>(buf), buffer_size(buf), ec);
201}
202
204inline std::size_t write(int fd, ConstBuffer buf)
205{
206 return write(fd, buffer_cast<const void*>(buf), buffer_size(buf));
207}
208
210inline int fcntl(int fd, int cmd, std::error_code& ec) noexcept
211{
212 const auto ret = ::fcntl(fd, cmd);
213 if (ret < 0) {
215 }
216 return ret;
217}
218
220template <typename ArgT>
221inline int fcntl(int fd, int cmd, ArgT arg, std::error_code& ec) noexcept
222{
223 const auto ret = ::fcntl(fd, cmd, arg);
224 if (ret < 0) {
226 }
227 return ret;
228}
229
231inline int fcntl(int fd, int cmd)
232{
233 const auto ret = ::fcntl(fd, cmd);
234 if (ret < 0) {
235 throw std::system_error{make_error(errno), "fcntl"};
236 }
237 return ret;
238}
239
241template <typename ArgT>
242inline int fcntl(int fd, int cmd, ArgT arg)
243{
244 const auto ret = ::fcntl(fd, cmd, arg);
245 if (ret < 0) {
246 throw std::system_error{make_error(errno), "fcntl"};
247 }
248 return ret;
249}
250
251} // namespace os
252inline namespace io {
253
255inline std::size_t file_size(int fd)
256{
257 struct stat st; // NOLINT(hicpp-member-init)
258 os::fstat(fd, st);
259 return st.st_size;
260}
261
264{
265 mode_t mode{umask(0)};
266 umask(mode);
267 return mode;
268}
269
270inline void set_non_block(int fd, std::error_code& ec) noexcept
271{
273}
274
275inline void set_non_block(int fd)
276{
278}
279
280} // namespace io
281} // namespace toolbox
282
283#endif // TOOLBOX_IO_FILE_HPP
std::size_t file_size(int fd)
Get file size.
Definition File.hpp:255
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:270
mode_t file_mode() noexcept
Get current file mode.
Definition File.hpp:263
void ftruncate(int fd, off_t length, std::error_code &ec) noexcept
Truncate a file to a specified length.
Definition File.hpp:128
ssize_t read(int fd, void *buf, std::size_t len, std::error_code &ec) noexcept
Read from a file descriptor.
Definition File.hpp:146
FileHandle open(const char *path, int flags, mode_t mode, std::error_code &ec) noexcept
Open and possibly create a file.
Definition File.hpp:32
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:178
std::pair< FileHandle, FileHandle > pipe2(int flags, std::error_code &ec) noexcept
Create pipe.
Definition File.hpp:90
int fcntl(int fd, int cmd, std::error_code &ec) noexcept
File control.
Definition File.hpp:210
void fstat(int fd, struct stat &statbuf, std::error_code &ec) noexcept
Get file status.
Definition File.hpp:110
int remove(const char *path)
Definition File.hpp:71
std::error_code make_error(int err) noexcept
Definition Error.hpp:25
constexpr auto bind() noexcept
Definition Slot.hpp:92