2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2015-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-10-24 22:30:31 +00:00
|
|
|
#include <cerrno> /* errno */
|
2021-09-29 17:25:52 +00:00
|
|
|
#include <string>
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <string_view>
|
2021-09-29 17:25:52 +00:00
|
|
|
#include <unordered_set>
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <fcntl.h> /* open() */
|
|
|
|
#include <unistd.h> /* close() */
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
|
|
|
|
#ifndef O_EVTONLY
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_EVTONLY O_RDONLY
|
2016-01-02 14:28:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <fmt/core.h>
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#define LIBTRANSMISSION_WATCHDIR_MODULE
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "transmission.h"
|
2022-03-17 00:23:44 +00:00
|
|
|
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "watchdir.h"
|
|
|
|
#include "watchdir-common.h"
|
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
static auto constexpr LogName = std::string_view{ "watchdir:kqueue" };
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_watchdir_kqueue
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend base;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int kq;
|
|
|
|
int dirfd;
|
|
|
|
struct event* event;
|
2021-09-29 17:25:52 +00:00
|
|
|
std::unordered_set<std::string> dir_entries;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-29 17:25:52 +00:00
|
|
|
#define BACKEND_UPCAST(b) (reinterpret_cast<tr_watchdir_kqueue*>(b))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#define KQUEUE_WATCH_MASK (NOTE_WRITE | NOTE_EXTEND)
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void tr_watchdir_kqueue_on_event(evutil_socket_t /*fd*/, short /*type*/, void* context)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const handle = static_cast<tr_watchdir_t>(context);
|
2022-03-17 00:23:44 +00:00
|
|
|
auto* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct kevent ke;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto ts = timespec{};
|
2021-09-15 00:18:09 +00:00
|
|
|
if (kevent(backend->kq, nullptr, 0, &ke, 1, &ts) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't read event: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Read directory with generic scan */
|
|
|
|
tr_watchdir_scan(handle, &backend->dir_entries);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_kqueue_free(tr_watchdir_backend* backend_base)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_kqueue* const backend = BACKEND_UPCAST(backend_base);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (backend == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(backend->base.free_func == &tr_watchdir_kqueue_free);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (backend->event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
event_del(backend->event);
|
|
|
|
event_free(backend->event);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->kq != -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
close(backend->kq);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->dirfd != -1)
|
|
|
|
{
|
|
|
|
close(backend->dirfd);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-29 17:25:52 +00:00
|
|
|
delete backend;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend* tr_watchdir_kqueue_new(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const path = tr_watchdir_get_path(handle);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct kevent ke;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-29 17:25:52 +00:00
|
|
|
auto* backend = new tr_watchdir_kqueue{};
|
2017-04-19 12:04:45 +00:00
|
|
|
backend->base.free_func = &tr_watchdir_kqueue_free;
|
|
|
|
backend->dirfd = -1;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
backend->kq = kqueue();
|
|
|
|
if (backend->kq == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Open fd for watching */
|
2022-03-17 00:23:44 +00:00
|
|
|
backend->dirfd = open(path, O_RDONLY | O_EVTONLY);
|
|
|
|
if (backend->dirfd == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Register kevent filter with kqueue descriptor */
|
|
|
|
EV_SET(&ke, backend->dirfd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, KQUEUE_WATCH_MASK, 0, NULL);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (kevent(backend->kq, &ke, 1, nullptr, 0, nullptr) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Create libevent task for event descriptor */
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((backend->event = event_new(
|
|
|
|
tr_watchdir_get_event_base(handle),
|
|
|
|
backend->kq,
|
|
|
|
EV_READ | EV_ET | EV_PERSIST,
|
|
|
|
&tr_watchdir_kqueue_on_event,
|
2021-09-15 00:18:09 +00:00
|
|
|
handle)) == nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't create event: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (event_add(backend->event, nullptr) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddNamedError(
|
|
|
|
LogName,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't add event: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Trigger one event for the initial scan */
|
|
|
|
event_active(backend->event, EV_READ, 0);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return BACKEND_DOWNCAST(backend);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_kqueue_free(BACKEND_DOWNCAST(backend));
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|