2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2010-05-30 23:30:43 +02:00
|
|
|
|
2013-10-18 08:56:25 +02:00
|
|
|
#include "ShutdownHandler.hxx"
|
2022-06-30 11:27:13 +02:00
|
|
|
#include "event/InotifyEvent.hxx"
|
2013-01-10 19:13:00 +01:00
|
|
|
#include "event/Loop.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2010-05-30 23:30:43 +02:00
|
|
|
|
2017-12-19 10:56:23 +01:00
|
|
|
#include <exception>
|
|
|
|
|
2010-05-30 23:30:43 +02:00
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
2013-10-20 13:51:55 +02:00
|
|
|
static constexpr unsigned IN_MASK =
|
2010-05-30 23:30:43 +02:00
|
|
|
#ifdef IN_ONLYDIR
|
2013-10-20 13:51:55 +02:00
|
|
|
IN_ONLYDIR|
|
2010-05-30 23:30:43 +02:00
|
|
|
#endif
|
2013-10-20 13:51:55 +02:00
|
|
|
IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
|
|
|
|
|IN_MOVE|IN_MOVE_SELF;
|
2010-05-30 23:30:43 +02:00
|
|
|
|
2022-06-30 11:27:13 +02:00
|
|
|
struct Instance final : InotifyHandler {
|
2022-06-30 11:56:19 +02:00
|
|
|
EventLoop event_loop;
|
|
|
|
const ShutdownHandler shutdown_handler{event_loop};
|
|
|
|
|
2022-06-30 11:27:13 +02:00
|
|
|
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();
|
|
|
|
}
|
2022-06-30 11:56:19 +02:00
|
|
|
};
|
|
|
|
|
2010-05-30 23:30:43 +02:00
|
|
|
int main(int argc, char **argv)
|
2016-10-28 10:23:05 +02:00
|
|
|
try {
|
2010-05-30 23:30:43 +02:00
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2013-12-24 14:44:08 +01:00
|
|
|
fprintf(stderr, "Usage: run_inotify PATH\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-05-30 23:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
path = argv[1];
|
|
|
|
|
2022-06-30 11:56:19 +02:00
|
|
|
Instance instance;
|
2013-01-15 18:18:02 +01:00
|
|
|
|
2022-06-30 11:27:13 +02:00
|
|
|
instance.inotify_event.AddWatch(path, IN_MASK);
|
2010-05-30 23:30:43 +02:00
|
|
|
|
2022-06-30 11:56:19 +02:00
|
|
|
instance.event_loop.Run();
|
2010-05-30 23:30:43 +02:00
|
|
|
|
2022-06-30 11:27:13 +02:00
|
|
|
if (instance.error)
|
|
|
|
std::rethrow_exception(instance.error);
|
|
|
|
|
2013-12-24 14:44:08 +01:00
|
|
|
return EXIT_SUCCESS;
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2016-10-28 10:23:05 +02:00
|
|
|
return EXIT_FAILURE;
|
2010-05-30 23:30:43 +02:00
|
|
|
}
|