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
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <unordered_set>
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/util.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-04-09 14:58:02 +00:00
|
|
|
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "error-types.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-04-09 14:58:02 +00:00
|
|
|
#include "tr-strbuf.h"
|
2016-01-02 14:28:59 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "watchdir.h"
|
|
|
|
#include "watchdir-common.h"
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2016-01-02 14:28:59 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
/* Non-static and mutable for unit tests */
|
|
|
|
auto tr_watchdir_retry_limit = size_t{ 3 };
|
|
|
|
auto tr_watchdir_retry_start_interval = timeval{ 1, 0 };
|
|
|
|
auto tr_watchdir_retry_max_interval = timeval{ 10, 0 };
|
|
|
|
|
|
|
|
class tr_watchdir_retry
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
public:
|
|
|
|
tr_watchdir_retry(tr_watchdir_retry const&) = delete;
|
|
|
|
tr_watchdir_retry& operator=(tr_watchdir_retry const&) = delete;
|
|
|
|
|
|
|
|
tr_watchdir_retry(tr_watchdir_t handle_in, struct event_base* base, std::string_view name_in)
|
|
|
|
: handle_{ handle_in }
|
|
|
|
, name_{ name_in }
|
|
|
|
, timer_{ evtimer_new(base, onRetryTimer, this) }
|
|
|
|
{
|
|
|
|
restart();
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
~tr_watchdir_retry()
|
|
|
|
{
|
|
|
|
evtimer_del(timer_);
|
|
|
|
event_free(timer_);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
void restart()
|
|
|
|
{
|
|
|
|
evtimer_del(timer_);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
counter_ = 0U;
|
|
|
|
interval_ = tr_watchdir_retry_start_interval;
|
2020-08-27 23:41:26 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
evtimer_add(timer_, &interval_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bump()
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
evtimer_del(timer_);
|
|
|
|
|
|
|
|
if (++counter_ >= tr_watchdir_retry_limit)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
// keep doubling the interval, but clamp at max_interval
|
|
|
|
evutil_timeradd(&interval_, &interval_, &interval_);
|
|
|
|
if (evutil_timercmp(&interval_, &tr_watchdir_retry_max_interval, >))
|
|
|
|
{
|
|
|
|
interval_ = tr_watchdir_retry_max_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
evtimer_add(timer_, &interval_);
|
|
|
|
return true;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
[[nodiscard]] auto const& name() const noexcept
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
private:
|
|
|
|
static void onRetryTimer(evutil_socket_t /*fd*/, short /*type*/, void* self);
|
|
|
|
|
|
|
|
tr_watchdir_t handle_ = nullptr;
|
|
|
|
std::string name_;
|
|
|
|
size_t counter_ = 0U;
|
|
|
|
struct event* const timer_;
|
|
|
|
struct timeval interval_ = tr_watchdir_retry_start_interval;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: notify / kqueue / win32 / generic should subclass from tr_watchdir
|
|
|
|
|
|
|
|
struct tr_watchdir
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
public:
|
|
|
|
tr_watchdir(
|
|
|
|
std::string_view path,
|
|
|
|
event_base* event_base,
|
|
|
|
tr_watchdir_cb callback,
|
|
|
|
void* callback_user_data,
|
|
|
|
bool force_generic = false)
|
|
|
|
: path_{ path }
|
|
|
|
, event_base_{ event_base }
|
|
|
|
, callback_{ callback }
|
|
|
|
, callback_user_data_{ callback_user_data }
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
// TODO: backends should be subclasses
|
|
|
|
if (!force_generic && (backend_ == nullptr))
|
|
|
|
{
|
|
|
|
#if defined(WITH_INOTIFY)
|
|
|
|
backend_ = tr_watchdir_inotify_new(this);
|
|
|
|
#elif defined(WITH_KQUEUE)
|
|
|
|
backend_ = tr_watchdir_kqueue_new(this);
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
backend_ = tr_watchdir_win32_new(this);
|
|
|
|
#endif
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (backend_ == nullptr)
|
|
|
|
{
|
|
|
|
backend_ = tr_watchdir_generic_new(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~tr_watchdir()
|
|
|
|
{
|
|
|
|
if (backend_ != nullptr)
|
|
|
|
{
|
|
|
|
backend_->free_func(backend_);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
tr_watchdir(tr_watchdir const&) = delete;
|
|
|
|
tr_watchdir& operator=(tr_watchdir const&) = delete;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
[[nodiscard]] constexpr auto const& path() const noexcept
|
|
|
|
{
|
|
|
|
return path_;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
[[nodiscard]] constexpr auto* backend() noexcept
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
return backend_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
[[nodiscard]] constexpr auto* eventBase() noexcept
|
|
|
|
{
|
|
|
|
return event_base_;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
tr_watchdir_status invoke(char const* name)
|
|
|
|
{
|
|
|
|
/* File may be gone while we're retrying */
|
|
|
|
if (!is_regular_file(path(), name))
|
|
|
|
{
|
|
|
|
return TR_WATCHDIR_IGNORE;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
auto const ret = (*callback_)(this, name, callback_user_data_);
|
|
|
|
TR_ASSERT(ret == TR_WATCHDIR_ACCEPT || ret == TR_WATCHDIR_IGNORE || ret == TR_WATCHDIR_RETRY);
|
|
|
|
tr_logAddDebug(fmt::format("Callback decided to {:s} file '{:s}'", statusToString(ret), name));
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
void erase(std::string_view name)
|
|
|
|
{
|
|
|
|
active_retries_.erase(std::string{ name });
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
void scan(std::unordered_set<std::string>* dir_entries)
|
|
|
|
{
|
|
|
|
auto new_dir_entries = std::unordered_set<std::string>{};
|
|
|
|
tr_error* error = nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
auto const dir = tr_sys_dir_open(path().c_str(), &error);
|
|
|
|
if (dir == TR_BAD_SYS_DIR)
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path()),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
tr_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
char const* name = nullptr;
|
|
|
|
while ((name = tr_sys_dir_read_name(dir, &error)) != nullptr)
|
|
|
|
{
|
|
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (dir_entries != nullptr)
|
|
|
|
{
|
|
|
|
auto const namestr = std::string(name);
|
|
|
|
new_dir_entries.insert(namestr);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (dir_entries->count(namestr) != 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
process(name);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path()),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
tr_error_free(error);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
tr_sys_dir_close(dir);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (dir_entries != nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
*dir_entries = new_dir_entries;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
void process(char const* name_cstr)
|
|
|
|
{
|
|
|
|
auto& retries = active_retries_;
|
|
|
|
auto name = std::string{ name_cstr };
|
|
|
|
auto it = retries.find(name);
|
|
|
|
if (it != std::end(retries)) // if we already have it, restart it
|
|
|
|
{
|
|
|
|
it->second->restart();
|
|
|
|
return;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (invoke(name_cstr) != TR_WATCHDIR_RETRY)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
retries.try_emplace(name, std::make_unique<tr_watchdir_retry>(this, event_base_, name));
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
private:
|
|
|
|
static bool is_regular_file(std::string_view dir, std::string_view name)
|
|
|
|
{
|
|
|
|
auto const path = tr_pathbuf{ dir, '/', name };
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
auto path_info = tr_sys_path_info{};
|
|
|
|
tr_error* error = nullptr;
|
|
|
|
bool const ret = tr_sys_path_get_info(path, 0, &path_info, &error) && (path_info.type == TR_SYS_PATH_IS_FILE);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
if (!TR_ERROR_IS_ENOENT(error->code))
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Skipping '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
tr_error_free(error);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
static constexpr std::string_view statusToString(tr_watchdir_status status)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case TR_WATCHDIR_ACCEPT:
|
|
|
|
return "accept"sv;
|
|
|
|
|
|
|
|
case TR_WATCHDIR_IGNORE:
|
|
|
|
return "ignore"sv;
|
|
|
|
|
|
|
|
case TR_WATCHDIR_RETRY:
|
|
|
|
return "retry"sv;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "???"sv;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
std::string const path_;
|
|
|
|
struct event_base* const event_base_;
|
|
|
|
tr_watchdir_backend* backend_ = nullptr;
|
|
|
|
tr_watchdir_cb const callback_;
|
|
|
|
void* const callback_user_data_;
|
|
|
|
std::map<std::string /*name*/, std::unique_ptr<tr_watchdir_retry>> active_retries_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
void tr_watchdir_retry::onRetryTimer(evutil_socket_t /*fd*/, short /*type*/, void* vself)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
TR_ASSERT(vself != nullptr);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
auto* const retry = static_cast<tr_watchdir_retry*>(vself);
|
|
|
|
auto const handle = retry->handle_;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
if (handle->invoke(retry->name_.c_str()) == TR_WATCHDIR_RETRY)
|
|
|
|
{
|
|
|
|
if (retry->bump())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_logAddWarn(fmt::format(_("Couldn't add torrent file '{path}'"), fmt::arg("path", retry->name())));
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
handle->erase(retry->name());
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_watchdir_t tr_watchdir_new(
|
2021-11-15 04:49:56 +00:00
|
|
|
std::string_view path,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_watchdir_cb callback,
|
|
|
|
void* callback_user_data,
|
|
|
|
struct event_base* event_base,
|
|
|
|
bool force_generic)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
return new tr_watchdir{ path, event_base, callback, callback_user_data, force_generic };
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_watchdir_free(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
delete handle;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_watchdir_get_path(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
return handle->path().c_str();
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend* tr_watchdir_get_backend(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
return handle->backend();
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct event_base* tr_watchdir_get_event_base(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(handle != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-04-09 14:58:02 +00:00
|
|
|
return handle->eventBase();
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_watchdir_process(tr_watchdir_t handle, char const* name)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
handle->process(name);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 17:25:52 +00:00
|
|
|
void tr_watchdir_scan(tr_watchdir_t handle, std::unordered_set<std::string>* dir_entries)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-04-09 14:58:02 +00:00
|
|
|
handle->scan(dir_entries);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|