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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.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"
|
|
|
|
#include "ptrarray.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:generic", __VA_ARGS__))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
typedef struct tr_watchdir_generic
|
|
|
|
{
|
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
|
|
|
struct event* event;
|
|
|
|
tr_ptrArray dir_entries;
|
2021-08-15 09:41:48 +00:00
|
|
|
} tr_watchdir_generic;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define BACKEND_UPCAST(b) ((tr_watchdir_generic*)(b))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
/* Non-static and mutable for unit tests. default to 10 sec. */
|
|
|
|
auto tr_watchdir_generic_interval = timeval{ 10, 0 };
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void tr_watchdir_generic_on_event(evutil_socket_t fd, short type, void* context)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(fd);
|
|
|
|
TR_UNUSED(type);
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const handle = static_cast<tr_watchdir_t>(context);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_generic* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_scan(handle, &backend->dir_entries);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_generic_free(tr_watchdir_backend* backend_base)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_generic* 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_generic_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
|
|
|
event_del(backend->event);
|
|
|
|
event_free(backend->event);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayDestruct(&backend->dir_entries, &tr_free);
|
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_generic_new(tr_watchdir_t handle)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_generic* backend;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
backend = tr_new0(tr_watchdir_generic, 1);
|
|
|
|
backend->base.free_func = &tr_watchdir_generic_free;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((backend
|
|
|
|
->event = event_new(tr_watchdir_get_event_base(handle), -1, EV_PERSIST, &tr_watchdir_generic_on_event, handle)) ==
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to create event: %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 (event_add(backend->event, &tr_watchdir_generic_interval) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to add event: %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
|
|
|
/* Run initial scan on startup */
|
|
|
|
event_active(backend->event, EV_READ, 0);
|
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_generic_free(BACKEND_DOWNCAST(backend));
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|