forked from microsoft/vscode-remote-try-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinotify.hpp
292 lines (248 loc) · 6.86 KB
/
inotify.hpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef INOTIFY_ASIO_INOTIFY_HPP
#define INOTIFY_ASIO_INOTIFY_HPP
#include <boost/asio/io_context.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/post.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <string_view>
#include <sys/inotify.h>
namespace inotify_asio
{
using watch_descriptor = std::uint32_t;
using mask_type = std::uint32_t;
using cookie_type = std::uint32_t;
const static std::size_t min_buffer_size = sizeof(struct inotify_event) + NAME_MAX + 1;
class watch_item
{
public:
watch_item(int wd, int fd)
: wd_(wd)
, fd_(fd)
{}
watch_item(watch_item &&other) noexcept
: wd_(std::exchange(other.wd_, -1))
, fd_(std::exchange(other.fd_, -1))
{}
~watch_item() noexcept
{
::inotify_rm_watch(fd_, wd_);
}
int fd() const
{
return fd_;
}
int wd() const
{
return wd_;
}
void forget()
{
wd_ = -1;
}
private:
int wd_;
int fd_;
};
class event
{
public:
event() = default;
explicit event(const struct inotify_event* ev)
: wd_(ev->wd),
mask_(ev->mask),
cookie_(ev->cookie),
name_(ev->name, ev->len ? ev->len - 1 : 0)
{}
int wd() const
{
return wd_;
}
mask_type mask() const
{
return mask_;
}
cookie_type cookie() const
{
return cookie_;
}
std::string name() const
{
return name_;
}
private:
uint32_t wd_;
uint32_t mask_;
uint32_t cookie_;
std::string name_;
};
template <typename Executor>
class basic_inotify
{
public:
typedef Executor executor_type;
explicit basic_inotify(const Executor &ex)
: desc_(ex)
{
int fd = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd < 0)
{
throw boost::system::system_error(
errno,
boost::system::system_category(),
"inotify::inotify");
}
desc_.assign(fd);
}
template <typename ExecutionContext>
explicit basic_inotify(
ExecutionContext &context,
std::enable_if_t<std::is_convertible<ExecutionContext &, boost::asio::execution_context &>::value, int> * = nullptr)
: basic_inotify(context.get_executor())
{
}
executor_type get_executor()
{
return desc_.get_executor();
}
watch_item add(
const std::string &pathname,
mask_type mask,
boost::system::error_code &ec)
{
int wd = ::inotify_add_watch(
desc_.native_handle(),
pathname.c_str(),
mask);
if (wd < 0)
{
ec.assign(errno, boost::system::system_category());
}
else
{
ec.clear();
}
return {wd, desc_.native_handle()};
}
watch_item add(const std::string &pathname, mask_type mask)
{
boost::system::error_code ec;
auto ret = add(pathname, mask, ec);
if (ec)
{
throw boost::system::system_error(ec, "inotify::add");
}
return ret;
}
event watch(boost::system::error_code &ec)
{
if (buffer_.size() == 0)
{
std::size_t bytes = desc_.read_some(
buffer_.prepare(min_buffer_size),
ec);
if (ec)
{
return {};
}
buffer_.commit(bytes);
}
return extract_event(buffer_);
}
event watch()
{
boost::system::error_code ec;
auto ret = watch(ec);
if (ec)
{
throw boost::system::system_error(ec, "inotify::watch");
}
return ret;
}
template <typename CompletionToken>
auto async_watch(CompletionToken &&token)
{
auto initiation = [](auto &&completion_handler,
boost::asio::posix::stream_descriptor& desc,
boost::beast::flat_buffer& buffer)
{
struct watch_op : boost::asio::coroutine
{
watch_op(
boost::asio::posix::stream_descriptor &desc,
boost::beast::flat_buffer& buffer,
std::decay_t<decltype(completion_handler)> handler,
bool need_read)
: desc_(desc)
, buffer_(buffer)
, handler_(std::move(handler))
, need_read_(need_read)
{
(*this)();
}
boost::asio::posix::stream_descriptor &desc_;
boost::beast::flat_buffer& buffer_;
std::decay_t<decltype(completion_handler)> handler_;
const bool need_read_;
#include <boost/asio/yield.hpp>
void operator()(
boost::system::error_code ec = {},
std::size_t bytes_transferred = 0)
{
reenter(this)
{
if (need_read_)
{
yield desc_.async_read_some(
buffer_.prepare(min_buffer_size),
std::move(*this));
if (ec)
{
handler_(ec, event{});
yield return;
}
buffer_.commit(bytes_transferred);
}
else
{
yield boost::asio::post(
desc_.get_executor(),
std::move(*this));
}
handler_(ec, extract_event(buffer_));
yield return;
}
}
#include <boost/asio/unyield.hpp>
};
watch_op
{
desc,
buffer,
completion_handler,
buffer.size() == 0
};
};
return boost::asio::async_initiate<
CompletionToken,
void(boost::system::error_code, event)>(
initiation, token, std::ref(desc_), buffer_);
}
private:
static event extract_event(boost::beast::flat_buffer &buffer)
{
const struct inotify_event *evbuf =
static_cast<const struct inotify_event *>(
buffer.data().data());
event ev(evbuf);
buffer.consume(sizeof(struct inotify_event) + evbuf->len);
return ev;
}
boost::asio::posix::stream_descriptor desc_;
boost::beast::flat_buffer buffer_;
};
typedef basic_inotify<boost::asio::any_io_executor> inotify;
}
#endif // INOTIFY_ASIO_INOTIFY_HPP