8
8
#if (KERNEL_VERSION(5, 9, 0) <= LINUX_VERSION_CODE) && ! __ANDROID_API__
9
9
10
10
#include " wtr/watcher.hpp"
11
- #include < atomic>
12
11
#include < cassert>
13
12
#include < cerrno>
14
13
#include < climits>
15
14
#include < cstdio>
16
15
#include < cstring>
17
16
#include < fcntl.h>
18
- #include < filesystem>
19
17
#include < functional>
20
18
#include < optional>
21
19
#include < sys/epoll.h>
@@ -58,7 +56,7 @@ struct sysres {
58
56
};
59
57
60
58
inline auto do_mark =
61
- [](char const * const dirpath, int fa_fd, auto const & cb) noexcept -> bool
59
+ [](char const * const dirpath, int fa_fd, auto const & cb) -> bool
62
60
{
63
61
auto do_error = [&]() -> bool
64
62
{ return adapter::do_error (" w/sys/not_watched@" , dirpath, cb); };
@@ -81,9 +79,8 @@ inline auto do_mark =
81
79
fcntl(fa_fd, F_SETFL, O_NONBLOCK);
82
80
fcntl(ep_fd, F_SETFL, O_NONBLOCK);
83
81
*/
84
- inline auto make_sysres (
85
- char const * const base_path,
86
- ::wtr::watcher::event::callback const & cb) noexcept -> sysres
82
+ inline auto make_sysres =
83
+ [](char const * const base_path, auto const & cb) -> sysres
87
84
{
88
85
auto do_error = [&](auto && msg, int fa_fd, int ep_fd = -1 ) -> sysres
89
86
{
@@ -112,9 +109,9 @@ inline auto make_sysres(
112
109
.ke {.fd = fa_fd},
113
110
.ep {.fd = ep_fd},
114
111
};
115
- }
112
+ };
116
113
117
- inline auto close_sysres (sysres& sr) noexcept -> bool
114
+ inline auto close_sysres (sysres& sr) -> bool
118
115
{
119
116
sr.ok = false ;
120
117
auto ok = close (sr.ke .fd ) == 0 && close (sr.ep .fd ) == 0 ;
@@ -152,8 +149,7 @@ inline auto close_sysres(sysres& sr) noexcept -> bool
152
149
character string to the event's directory entry
153
150
after the file handle to the directory.
154
151
Confusing, right? */
155
- inline auto pathof (fanotify_event_metadata const * const mtd) noexcept
156
- -> std::filesystem::path
152
+ inline auto pathof (fanotify_event_metadata const * const mtd) -> std::string
157
153
{
158
154
constexpr size_t path_ulim = PATH_MAX - sizeof (' \0 ' );
159
155
auto dir_info = (fanotify_event_info_fid*)(mtd + 1 );
@@ -189,20 +185,19 @@ inline auto pathof(fanotify_event_metadata const* const mtd) noexcept
189
185
if (file_name && not_selfdir) snprintf (beg, end, " /%s" , file_name);
190
186
}
191
187
}
188
+
192
189
return {path_buf};
193
190
}
194
191
195
- inline auto
196
- peek (fanotify_event_metadata const * const mtd, size_t read_len) noexcept
192
+ inline auto peek (fanotify_event_metadata const * const mtd, size_t read_len)
197
193
-> fanotify_event_metadata const *
198
194
{
199
195
auto evlen = mtd->event_len ;
200
196
auto next = (fanotify_event_metadata*)((char *)mtd + evlen);
201
197
return FAN_EVENT_OK (next, read_len - evlen) ? next : nullptr ;
202
198
}
203
199
204
- inline auto
205
- parse_event (fanotify_event_metadata const * m, size_t read_len) noexcept
200
+ inline auto parse_event (fanotify_event_metadata const * m, size_t read_len)
206
201
-> ::wtr::watcher::event
207
202
{
208
203
using ev = ::wtr::watcher::event;
@@ -229,10 +224,8 @@ parse_event(fanotify_event_metadata const* m, size_t read_len) noexcept
229
224
Most of the other code is
230
225
a layer of translation
231
226
between us and the kernel. */
232
- inline auto send (
233
- ::wtr::watcher::event const & ev,
234
- sysres& sr,
235
- ::wtr::watcher::event::callback const & cb) noexcept -> bool
227
+ inline auto send =
228
+ [](::wtr::watcher::event const & ev, sysres& sr, auto const & cb) -> bool
236
229
{
237
230
using e = ::wtr::watcher::event;
238
231
using ev_pt = enum e::path_type;
@@ -242,7 +235,7 @@ inline auto send(
242
235
? do_mark (pn.c_str (), sr.ke .fd , cb)
243
236
: true ;
244
237
return (cb (ev), update_ok);
245
- }
238
+ };
246
239
247
240
/* Read some events from what fanotify gives
248
241
us. Sends (the valid) events to the user's
@@ -265,17 +258,15 @@ inline auto send(
265
258
The `metadata->vers` field may differ between
266
259
kernel versions, so we check it against the
267
260
version we were compiled with. */
268
- inline auto do_event_recv (
269
- std::filesystem::path const & base_path,
270
- ::wtr::watcher::event::callback const & cb,
271
- sysres& sr) noexcept -> bool
261
+ inline auto do_ev_recv =
262
+ [](char const * const base_path, auto const & cb, sysres& sr) -> bool
272
263
{
273
- auto do_error = [&](auto && msg) noexcept -> bool
274
- { return adapter::do_error (msg, base_path. c_str () , cb); };
275
- auto do_warn = [&](auto && msg) noexcept -> bool { return ! do_error (msg); };
276
- auto ev_info = [](fanotify_event_metadata* m) noexcept
264
+ auto do_error = [&](auto && msg) -> bool
265
+ { return adapter::do_error (msg, base_path, cb); };
266
+ auto do_warn = [&](auto && msg) -> bool { return ! do_error (msg); };
267
+ auto ev_info = [](fanotify_event_metadata* m)
277
268
{ return (fanotify_event_info_fid*)(m + 1 ); };
278
- auto ev_has_dirname = [&](fanotify_event_metadata* m) noexcept -> bool
269
+ auto ev_has_dirname = [&](fanotify_event_metadata* m) -> bool
279
270
{ return ev_info (m)->hdr .info_type == FAN_EVENT_INFO_TYPE_DFID_NAME; };
280
271
281
272
unsigned ev_c = 0 ;
@@ -290,24 +281,22 @@ inline auto do_event_recv(
290
281
FAN_EVENT_OK (mtd, read_len);
291
282
mtd = FAN_EVENT_NEXT (mtd, read_len))
292
283
if (ev_c++ > sr.ke .c_ulim )
293
- return do_error (" e/sys/bad_count @" );
284
+ return do_error (" e/sys/ev_lin @" );
294
285
else if (mtd->vers != FANOTIFY_METADATA_VERSION)
295
286
return do_error (" e/sys/kernel_version@" );
296
287
else if (mtd->fd != FAN_NOFD)
297
288
return do_warn (" w/sys/bad_fd@" );
298
289
else if (mtd->mask & FAN_Q_OVERFLOW)
299
- return do_warn (" w/sys/overflow @" );
290
+ return do_warn (" w/sys/ev_lim @" );
300
291
else if (! ev_has_dirname (mtd))
301
292
return do_warn (" w/sys/bad_info@" );
302
293
else
303
294
send (parse_event (mtd, read_len), sr, cb);
304
295
return true ;
305
- }
296
+ };
306
297
307
- inline auto watch (
308
- char const * const path,
309
- ::wtr::watcher::event::callback const & cb,
310
- std::atomic<bool >& is_living) noexcept -> bool
298
+ inline auto watch =
299
+ [](char const * const path, auto const & cb, auto const & is_living) -> bool
311
300
{
312
301
auto sr = make_sysres (path, cb);
313
302
auto do_error = [&](auto && msg) -> bool
@@ -322,12 +311,12 @@ inline auto watch(
322
311
else if (ep_c > 0 ) [[likely]]
323
312
for (int n = 0 ; n < ep_c; n++)
324
313
if (sr.ep .interests [n].data .fd == sr.ke .fd ) [[likely]]
325
- if (! do_event_recv (path, cb, sr)) [[unlikely]]
326
- return do_error (" e/self/event_recv @" );
314
+ if (! do_ev_recv (path, cb, sr)) [[unlikely]]
315
+ return do_error (" e/self/ev_recv @" );
327
316
}
328
317
329
318
return close_sysres (sr);
330
- }
319
+ };
331
320
332
321
} /* namespace detail::wtr::watcher::adapter::fanotify */
333
322
0 commit comments