event/InotifyEvent: new class wrapping inotify

Replaces class InotifySource.
This commit is contained in:
Max Kellermann
2022-06-30 11:27:13 +02:00
parent ff4cf6c6d1
commit 0f4bf5569a
12 changed files with 301 additions and 230 deletions

View File

@@ -133,8 +133,6 @@ if enable_inotify
'run_inotify',
'run_inotify.cxx',
'ShutdownHandler.cxx',
'../src/db/update/InotifyDomain.cxx',
'../src/db/update/InotifySource.cxx',
include_directories: inc,
dependencies: [
log_dep,

View File

@@ -18,7 +18,7 @@
*/
#include "ShutdownHandler.hxx"
#include "db/update/InotifySource.hxx"
#include "event/InotifyEvent.hxx"
#include "event/Loop.hxx"
#include "Log.hxx"
@@ -33,18 +33,23 @@ static constexpr unsigned IN_MASK =
IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
|IN_MOVE|IN_MOVE_SELF;
static void
my_inotify_callback([[maybe_unused]] int wd, unsigned mask,
const char *name, [[maybe_unused]] void *ctx)
{
printf("mask=0x%x name='%s'\n", mask, name);
}
struct Instance {
struct Instance final : InotifyHandler {
EventLoop event_loop;
const ShutdownHandler shutdown_handler{event_loop};
InotifySource source{event_loop, my_inotify_callback, nullptr};
InotifyEvent inotify_event{event_loop, *this};
std::exception_ptr error;
/* virtual methods from class InotifyHandler */
void OnInotify(int, unsigned mask, const char *name) override {
printf("mask=0x%x name='%s'\n", mask, name);
}
void OnInotifyError(std::exception_ptr _error) noexcept override {
error = std::move(_error);
event_loop.Break();
}
};
int main(int argc, char **argv)
@@ -60,10 +65,13 @@ try {
Instance instance;
instance.source.Add(path, IN_MASK);
instance.inotify_event.AddWatch(path, IN_MASK);
instance.event_loop.Run();
if (instance.error)
std::rethrow_exception(instance.error);
return EXIT_SUCCESS;
} catch (...) {
LogError(std::current_exception());