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-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <climits> /* NAME_MAX */
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2022-08-10 13:34:51 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <unistd.h> /* close() */
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_WATCHDIR_MODULE
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2022-08-10 13:34:51 +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"
|
2022-08-10 13:34:51 +00:00
|
|
|
#include "tr-strbuf.h"
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "utils.h"
|
2022-08-10 13:34:51 +00:00
|
|
|
#include "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
|
|
|
|
{
|
|
|
|
class INotifyWatchdir final : public impl::BaseWatchdir
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static auto constexpr InotifyWatchMask = uint32_t{ IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE };
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
public:
|
|
|
|
INotifyWatchdir(std::string_view dirname, Callback callback, TimerMaker& timer_maker, event_base* evbase)
|
|
|
|
: BaseWatchdir{ dirname, std::move(callback), timer_maker }
|
|
|
|
{
|
|
|
|
init(evbase);
|
|
|
|
scan();
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
INotifyWatchdir(INotifyWatchdir&&) = delete;
|
|
|
|
INotifyWatchdir(INotifyWatchdir const&) = delete;
|
|
|
|
INotifyWatchdir& operator=(INotifyWatchdir&&) = delete;
|
|
|
|
INotifyWatchdir& operator=(INotifyWatchdir const&) = delete;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
~INotifyWatchdir() override
|
|
|
|
{
|
|
|
|
if (event_ != nullptr)
|
|
|
|
{
|
|
|
|
bufferevent_disable(event_, EV_READ);
|
|
|
|
bufferevent_free(event_);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
if (infd_ != -1)
|
|
|
|
{
|
|
|
|
if (inwd_ != -1)
|
|
|
|
{
|
|
|
|
inotify_rm_watch(infd_, inwd_);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
close(infd_);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
infd_ = inotify_init();
|
|
|
|
if (infd_ == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-08-10 13:34:51 +00:00
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", dirname()),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2022-08-10 13:34:51 +00:00
|
|
|
return;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
inwd_ = inotify_add_watch(infd_, tr_pathbuf{ dirname() }, InotifyWatchMask | IN_ONLYDIR);
|
|
|
|
if (inwd_ == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-08-10 13:34:51 +00:00
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", dirname()),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2022-08-10 13:34:51 +00:00
|
|
|
return;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
event_ = bufferevent_socket_new(evbase, infd_, 0);
|
|
|
|
if (event_ == nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-08-10 13:34:51 +00:00
|
|
|
_("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;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
// guarantees at least the sizeof an inotify event will be available in the event buffer
|
|
|
|
bufferevent_setwatermark(event_, EV_READ, sizeof(struct inotify_event), 0);
|
|
|
|
bufferevent_setcb(event_, onInotifyEvent, nullptr, nullptr, this);
|
|
|
|
bufferevent_enable(event_, EV_READ);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
static void onInotifyEvent(struct bufferevent* event, void* vself)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
static_cast<INotifyWatchdir*>(vself)->handleInotifyEvent(event);
|
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 handleInotifyEvent(struct bufferevent* event)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-09-23 05:51:15 +00:00
|
|
|
auto ev = inotify_event{};
|
2022-08-10 13:34:51 +00:00
|
|
|
auto name = std::string{};
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
// Read the size of the struct excluding name into buf.
|
|
|
|
// Guaranteed to have at least sizeof(ev) available.
|
|
|
|
auto nread = size_t{};
|
|
|
|
while ((nread = bufferevent_read(event, &ev, sizeof(ev))) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
if (nread == (size_t)-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)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread != sizeof(ev))
|
|
|
|
{
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't read event: expected {expected_size}, got {actual_size}"),
|
|
|
|
fmt::arg("expected_size", sizeof(ev)),
|
|
|
|
fmt::arg("actual_size", nread)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT(ev.wd == inwd_);
|
|
|
|
TR_ASSERT((ev.mask & InotifyWatchMask) != 0);
|
|
|
|
TR_ASSERT(ev.len > 0);
|
|
|
|
|
|
|
|
// consume entire name into buffer
|
|
|
|
name.resize(ev.len);
|
|
|
|
nread = bufferevent_read(event, std::data(name), ev.len);
|
|
|
|
if (nread == static_cast<size_t>(-1))
|
|
|
|
{
|
|
|
|
auto const error_code = errno;
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't read filename: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread != ev.len)
|
|
|
|
{
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't read filename: expected {expected_size}, got {actual_size}"),
|
|
|
|
fmt::arg("expected_size", sizeof(ev)),
|
|
|
|
fmt::arg("actual_size", nread)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: `name` may have extra trailing zeroes from inotify;
|
|
|
|
// pass the c_str() so that processFile gets the right strlen
|
|
|
|
processFile(name.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
int infd_ = -1;
|
|
|
|
int inwd_ = -1;
|
|
|
|
struct bufferevent* event_ = nullptr;
|
|
|
|
};
|
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,
|
|
|
|
libtransmission::TimerMaker& timer_maker,
|
|
|
|
event_base* evbase)
|
|
|
|
{
|
|
|
|
return std::make_unique<INotifyWatchdir>(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
|