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-03-17 00:23:44 +00:00
|
|
|
#include <string_view>
|
2016-01-02 14:28:59 +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"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_watchdir_inotify
|
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 infd;
|
|
|
|
int inwd;
|
|
|
|
struct bufferevent* event;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define BACKEND_UPCAST(b) ((tr_watchdir_inotify*)(b))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#define INOTIFY_WATCH_MASK (IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE)
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void tr_watchdir_inotify_on_first_scan(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);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_watchdir_scan(handle, nullptr);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_inotify_on_event(struct bufferevent* event, void* context)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(context != nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const handle = static_cast<tr_watchdir_t>(context);
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_watchdir_inotify const* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
struct inotify_event ev;
|
|
|
|
size_t name_size = NAME_MAX + 1;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* name = tr_new(char, name_size);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* Read the size of the struct excluding name into buf. Guaranteed to have at
|
2017-04-21 07:40:57 +00:00
|
|
|
least sizeof(ev) available */
|
2021-10-19 16:09:38 +00:00
|
|
|
auto nread = size_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
while ((nread = bufferevent_read(event, &ev, sizeof(ev))) != 0)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (nread == (size_t)-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(
|
|
|
|
_("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
|
|
|
break;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (nread != sizeof(ev))
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
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)));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ev.wd == backend->inwd);
|
|
|
|
TR_ASSERT((ev.mask & INOTIFY_WATCH_MASK) != 0);
|
|
|
|
TR_ASSERT(ev.len > 0);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ev.len > name_size)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
name_size = ev.len;
|
|
|
|
name = tr_renew(char, name, name_size);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Consume entire name into buffer */
|
|
|
|
if ((nread = bufferevent_read(event, name, ev.len)) == (size_t)-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(
|
|
|
|
_("Couldn't read filename: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (nread != ev.len)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
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)));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_process(handle, name);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(name);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_inotify_free(tr_watchdir_backend* backend_base)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* 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_inotify_free);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (backend->event != nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bufferevent_disable(backend->event, EV_READ);
|
|
|
|
bufferevent_free(backend->event);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->infd != -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->inwd != -1)
|
|
|
|
{
|
|
|
|
inotify_rm_watch(backend->infd, backend->inwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(backend->infd);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(backend);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend* tr_watchdir_inotify_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);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-10-19 16:09:38 +00:00
|
|
|
auto* const backend = tr_new0(tr_watchdir_inotify, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
backend->base.free_func = &tr_watchdir_inotify_free;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
backend->infd = inotify_init();
|
|
|
|
if (backend->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(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
backend->inwd = inotify_add_watch(backend->infd, path, INOTIFY_WATCH_MASK | IN_ONLYDIR);
|
|
|
|
if (backend->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(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
backend->event = bufferevent_socket_new(tr_watchdir_get_event_base(handle), backend->infd, 0);
|
|
|
|
if (backend->event == nullptr)
|
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(
|
|
|
|
_("Couldn't watch '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", path),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Guarantees at least the sizeof an inotify event will be available in the
|
|
|
|
event buffer */
|
|
|
|
bufferevent_setwatermark(backend->event, EV_READ, sizeof(struct inotify_event), 0);
|
2021-09-15 00:18:09 +00:00
|
|
|
bufferevent_setcb(backend->event, &tr_watchdir_inotify_on_event, nullptr, nullptr, handle);
|
2017-04-19 12:04:45 +00:00
|
|
|
bufferevent_enable(backend->event, EV_READ);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Perform an initial scan on the directory */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (event_base_once(
|
|
|
|
tr_watchdir_get_event_base(handle),
|
|
|
|
-1,
|
|
|
|
EV_TIMEOUT,
|
|
|
|
&tr_watchdir_inotify_on_first_scan,
|
|
|
|
handle,
|
|
|
|
nullptr) == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 00:23:44 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't scan '{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
|
|
|
}
|
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
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
FAIL:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_inotify_free(BACKEND_DOWNCAST(backend));
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|