2016-01-02 14:28:59 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2015-2016 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <climits> /* NAME_MAX */
|
|
|
|
#include <cstdlib> /* realloc() */
|
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>
|
|
|
|
|
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-08-15 09:41:48 +00:00
|
|
|
#define log_error(...) \
|
|
|
|
(!tr_logLevelIsActive(TR_LOG_ERROR) ? (void)0 : \
|
|
|
|
tr_logAddMessage(__FILE__, __LINE__, TR_LOG_ERROR, "watchdir:inotify", __VA_ARGS__))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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-10 16:52:26 +00:00
|
|
|
static void tr_watchdir_inotify_on_first_scan([[maybe_unused]] evutil_socket_t fd, [[maybe_unused]] 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 nread;
|
|
|
|
size_t name_size = NAME_MAX + 1;
|
|
|
|
char* name = tr_new(char, name_size);
|
|
|
|
|
|
|
|
/* 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 */
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to read inotify event: %s", tr_strerror(errno));
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to read inotify event: expected %zu, got %zu bytes.", sizeof(ev), nread);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to read inotify name: %s", tr_strerror(errno));
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to read inotify name: expected %" PRIu32 ", got %zu bytes.", ev.len, nread);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_inotify* 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);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_inotify* backend;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
backend = tr_new0(tr_watchdir_inotify, 1);
|
|
|
|
backend->base.free_func = &tr_watchdir_inotify_free;
|
|
|
|
backend->infd = -1;
|
|
|
|
backend->inwd = -1;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((backend->infd = inotify_init()) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Unable to inotify_init: %s", tr_strerror(errno));
|
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
|
|
|
if ((backend->inwd = inotify_add_watch(backend->infd, path, INOTIFY_WATCH_MASK | IN_ONLYDIR)) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to setup watchdir \"%s\": %s (%d)", path, tr_strerror(errno), errno);
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if ((backend->event = bufferevent_socket_new(tr_watchdir_get_event_base(handle), backend->infd, 0)) == nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to create event buffer: %s", tr_strerror(errno));
|
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
|
|
|
{
|
|
|
|
log_error("Failed to perform initial scan: %s", tr_strerror(errno));
|
|
|
|
}
|
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
|
|
|
}
|