transmission/libtransmission/watchdir-kqueue.cc

176 lines
4.9 KiB
C++
Raw Normal View History

// This file Copyright © 2015-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
2022-08-10 13:34:51 +00:00
#include <cerrno> // for errno
2022-08-17 16:08:36 +00:00
#include <memory>
#include <string>
2022-08-17 16:08:36 +00:00
#include <utility>
2022-08-10 13:34:51 +00:00
#include <fcntl.h> // for open()
#include <unistd.h> // for close()
#include <sys/types.h>
#include <sys/event.h>
#ifndef O_EVTONLY
#define O_EVTONLY O_RDONLY
#endif
#include <event2/event.h>
#include <fmt/core.h>
#define LIBTRANSMISSION_WATCHDIR_MODULE
#include "transmission.h"
#include "log.h"
2022-08-10 13:34:51 +00:00
#include "tr-strbuf.h"
#include "utils.h" // for _()
#include "watchdir-base.h"
2022-08-10 13:34:51 +00:00
namespace libtransmission
{
2022-08-10 13:34:51 +00:00
namespace
{
2022-08-10 13:34:51 +00:00
class KQueueWatchdir final : public impl::BaseWatchdir
{
2022-08-10 13:34:51 +00:00
public:
KQueueWatchdir(std::string_view dirname, Callback callback, libtransmission::TimerMaker& timer_maker, event_base* evbase)
: BaseWatchdir{ dirname, std::move(callback), timer_maker }
{
2022-08-10 13:34:51 +00:00
init(evbase);
scan();
}
2022-08-10 13:34:51 +00:00
KQueueWatchdir(KQueueWatchdir&&) = delete;
KQueueWatchdir(KQueueWatchdir const&) = delete;
KQueueWatchdir& operator=(KQueueWatchdir&&) = delete;
KQueueWatchdir& operator=(KQueueWatchdir const&) = delete;
2022-08-10 13:34:51 +00:00
~KQueueWatchdir() override
{
2022-08-10 13:34:51 +00:00
if (event_ != nullptr)
{
event_del(event_);
event_free(event_);
}
if (kq_ != -1)
{
close(kq_);
}
if (dirfd_ != -1)
{
close(dirfd_);
}
}
2022-08-10 13:34:51 +00:00
private:
void init(struct event_base* evbase)
{
2022-08-10 13:34:51 +00:00
kq_ = kqueue();
if (kq_ == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't watch '{path}': {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
// open fd for watching
auto const szdirname = tr_pathbuf{ dirname() };
dirfd_ = open(szdirname, O_RDONLY | O_EVTONLY);
if (dirfd_ == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't watch '{path}': {error} ({error_code})"),
fmt::arg("path", dirname()),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
// register kevent filter with kqueue descriptor
struct kevent ke;
static auto constexpr KqueueWatchMask = (NOTE_WRITE | NOTE_EXTEND);
EV_SET(&ke, dirfd_, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, KqueueWatchMask, 0, NULL);
if (kevent(kq_, &ke, 1, nullptr, 0, nullptr) == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't watch '{path}': {error} ({error_code})"),
fmt::arg("path", dirname()),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
// create libevent task for event descriptor
event_ = event_new(evbase, kq_, EV_READ | EV_ET | EV_PERSIST, &onKqueueEvent, this);
if (event_ == nullptr)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't create event: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
if (event_add(event_, nullptr) == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't add event: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
}
2022-08-10 13:34:51 +00:00
static void onKqueueEvent(evutil_socket_t /*fd*/, short /*type*/, void* vself)
{
2022-08-10 13:34:51 +00:00
static_cast<KQueueWatchdir*>(vself)->handleKqueueEvent();
}
2022-08-10 13:34:51 +00:00
void handleKqueueEvent()
{
2022-08-10 13:34:51 +00:00
struct kevent ke;
auto ts = timespec{};
if (kevent(kq_, nullptr, 0, &ke, 1, &ts) == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't read event: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
scan();
}
2022-08-10 13:34:51 +00:00
int kq_ = -1;
int dirfd_ = -1;
struct event* event_ = nullptr;
};
2022-08-10 13:34:51 +00:00
} // namespace
2022-08-10 13:34:51 +00:00
std::unique_ptr<Watchdir> Watchdir::create(
std::string_view dirname,
Callback callback,
TimerMaker& timer_maker,
event_base* evbase)
{
return std::make_unique<KQueueWatchdir>(dirname, std::move(callback), timer_maker, evbase);
}
2022-08-10 13:34:51 +00:00
} // namespace libtransmission