aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/fd.cc
blob: 6d5e54f8578eb4f8193d8e30ed860e2afcbff786 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * This source code is released into the public domain.
 */

module;

#include <fcntl.h>
#include <unistd.h>

#include <coroutine>
#include <expected>
#include <format>
#include <stdexcept>
#include <system_error>

module nihil.posix;

import nihil.error;
import nihil.monad;

namespace nihil {

fd::fd() noexcept = default;

fd::fd(int fileno) noexcept
	: m_fileno(fileno)
{
}

fd::~fd()
{
	if (*this)
		std::ignore = this->close();
}

fd::fd(fd &&other) noexcept
	: m_fileno(std::exchange(other.m_fileno, invalid_fileno))
{
}

auto fd::operator=(this fd &self, fd &&other) noexcept -> fd &
{
	if (&self != &other)
		self.m_fileno = std::exchange(other.m_fileno, invalid_fileno);
	return self;
}

fd::operator bool(this fd const &self) noexcept
{
	return self.m_fileno != invalid_fileno;
}

auto fd::close(this fd &self) -> std::expected<void, error>
{
	auto const ret = ::close(self.get());
	self.m_fileno = invalid_fileno;

	if (ret == 0)
		return {};

	return std::unexpected(error(std::errc(errno)));
}

auto fd::get(this fd const &self) -> int
{
	if (self)
		return self.m_fileno;
	throw std::logic_error("Attempt to call get() on invalid fd");
}

auto fd::release(this fd &&self) -> int
{
	if (self)
		return std::exchange(self.m_fileno, invalid_fileno);
	throw std::logic_error("Attempt to release an invalid fd");
}

auto dup(fd const &self) -> std::expected<fd, error>
{
	auto const newfd = ::dup(self.get());
	if (newfd != -1)
		return newfd;

	return std::unexpected(error(std::errc(errno)));
}

auto dup(fd const &self, int newfd) -> std::expected<fd, error>
{
	auto const ret = ::dup2(self.get(), newfd);
	if (ret != -1)
		return newfd;

	return std::unexpected(error(std::errc(errno)));
}

auto raw_dup(fd const &self) -> std::expected<int, error>
{
	auto const newfd = ::dup(self.get());
	if (newfd != -1)
		return newfd;

	return std::unexpected(error(std::errc(errno)));
}

auto raw_dup(fd const &self, int newfd) -> std::expected<int, error>
{
	auto const ret = ::dup2(self.get(), newfd);
	if (ret != -1)
		return newfd;

	return std::unexpected(error(std::errc(errno)));
}

auto getflags(fd const &self) -> std::expected<int, error>
{
	auto const flags = ::fcntl(self.get(), F_GETFL);
	if (flags != -1)
		return flags;

	return std::unexpected(error(std::errc(errno)));
}

auto replaceflags(fd &self, int newflags) -> std::expected<void, error>
{
	auto const ret = ::fcntl(self.get(), F_SETFL, newflags);
	if (ret == 0)
		return {};

	return std::unexpected(error(std::errc(errno)));
}

auto setflags(fd &self, int newflags) -> std::expected<int, error>
{
	auto flags = co_await getflags(self);

	flags |= newflags;
	co_await replaceflags(self, flags);

	co_return flags;
}

auto clearflags(fd &self, int clrflags) -> std::expected<int, error>
{
	auto flags = co_await getflags(self);

	flags &= ~clrflags;
	co_await replaceflags(self, flags);

	co_return flags;
}

auto getfdflags(fd const &self) -> std::expected<int, error>
{
	auto const flags = ::fcntl(self.get(), F_GETFD);
	if (flags != -1)
		return flags;

	return std::unexpected(error(std::errc(errno)));
}

auto replacefdflags(fd &self, int newflags) -> std::expected<void, error>
{
	auto const ret = ::fcntl(self.get(), F_SETFD, newflags);
	if (ret != -1)
		return {};

	return std::unexpected(error(std::errc(errno)));
}

auto setfdflags(fd &self, int newflags) -> std::expected<int, error>
{
	auto flags = co_await getfdflags(self);

	flags |= newflags;
	co_await replacefdflags(self, flags);

	co_return flags;
}

auto clearfdflags(fd &self, int clrflags) -> std::expected<int, error>
{
	auto flags = co_await getfdflags(self);

	flags &= ~clrflags;
	co_await replacefdflags(self, flags);

	co_return flags;
}

auto pipe() -> std::expected<std::pair<fd, fd>, error>
{
	auto fds = std::array<int, 2>{};

	if (auto const ret = ::pipe(fds.data()); ret != 0)
		return std::unexpected(error(std::errc(errno)));

	return {{fd(fds[0]), fd(fds[1])}};
}

auto fd::write(this fd &self, std::span<std::byte const> buffer)
	-> std::expected<std::size_t, error>
{
	auto const ret = ::write(self.get(), buffer.data(), buffer.size());
	if (ret >= 0)
		return ret;

	return std::unexpected(error(std::errc(errno)));
}

auto fd::read(this fd &self, std::span<std::byte> buffer)
	-> std::expected<std::span<std::byte>, error>
{
	auto const ret = ::read(self.get(), buffer.data(), buffer.size());
	if (ret >= 0)
		return buffer.subspan(0, ret);

	return std::unexpected(error(std::errc(errno)));
}

} // namespace nihil