2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
#include <cerrno> // for errno
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2021-09-29 17:25:52 +00:00
|
|
|
#include <string>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
#include <fcntl.h> // for open()
|
|
|
|
#include <unistd.h> // for 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
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-03-17 00:23:44 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
|
|
|
#include "libtransmission/utils.h" // for _()
|
|
|
|
#include "libtransmission/utils-ev.h"
|
|
|
|
#include "libtransmission/watchdir-base.h"
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
namespace libtransmission
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
namespace
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
class KQueueWatchdir final : public impl::BaseWatchdir
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
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 }
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
init(evbase);
|
|
|
|
scan();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
KQueueWatchdir(KQueueWatchdir&&) = delete;
|
|
|
|
KQueueWatchdir(KQueueWatchdir const&) = delete;
|
|
|
|
KQueueWatchdir& operator=(KQueueWatchdir&&) = delete;
|
|
|
|
KQueueWatchdir& operator=(KQueueWatchdir const&) = delete;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
~KQueueWatchdir() override
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
event_.reset();
|
2022-08-10 13:34:51 +00:00
|
|
|
|
|
|
|
if (kq_ != -1)
|
|
|
|
{
|
|
|
|
close(kq_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirfd_ != -1)
|
|
|
|
{
|
|
|
|
close(dirfd_);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
private:
|
|
|
|
void init(struct event_base* evbase)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
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
|
2022-11-06 21:11:30 +00:00
|
|
|
event_.reset(event_new(evbase, kq_, EV_READ | EV_ET | EV_PERSIST, &onKqueueEvent, this));
|
|
|
|
if (!event_)
|
2022-08-10 13:34:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:11:30 +00:00
|
|
|
if (event_add(event_.get(), nullptr) == -1)
|
2022-08-10 13:34:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
static void onKqueueEvent(evutil_socket_t /*fd*/, short /*type*/, void* vself)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
static_cast<KQueueWatchdir*>(vself)->handleKqueueEvent();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
void handleKqueueEvent()
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
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();
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
int kq_ = -1;
|
|
|
|
int dirfd_ = -1;
|
2022-11-06 21:11:30 +00:00
|
|
|
libtransmission::evhelpers::event_unique_ptr event_;
|
2022-08-10 13:34:51 +00:00
|
|
|
};
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
} // namespace
|
2016-01-02 14:28:59 +00:00
|
|
|
|
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);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
2022-08-10 13:34:51 +00:00
|
|
|
|
|
|
|
} // namespace libtransmission
|