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 <assert.h>
|
|
|
|
#include <errno.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <string.h> /* strcmp() */
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <fcntl.h> /* open() */
|
|
|
|
#include <unistd.h> /* close() */
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
|
|
|
|
#ifndef O_EVTONLY
|
2017-04-19 12:04:45 +00:00
|
|
|
#define O_EVTONLY O_RDONLY
|
2016-01-02 14:28:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#define __LIBTRANSMISSION_WATCHDIR_MODULE__
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "watchdir.h"
|
|
|
|
#include "watchdir-common.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define log_error(...) (!tr_logLevelIsActive(TR_LOG_ERROR) ? (void)0 : \
|
|
|
|
tr_logAddMessage(__FILE__, __LINE__, TR_LOG_ERROR, "watchdir:kqueue", __VA_ARGS__))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
typedef struct tr_watchdir_kqueue
|
|
|
|
{
|
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 kq;
|
|
|
|
int dirfd;
|
|
|
|
struct event* event;
|
|
|
|
tr_ptrArray dir_entries;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
tr_watchdir_kqueue;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#define BACKEND_UPCAST(b) ((tr_watchdir_kqueue*)(b))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#define KQUEUE_WATCH_MASK (NOTE_WRITE | NOTE_EXTEND)
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_kqueue_on_event(evutil_socket_t fd UNUSED, short type UNUSED, void* context)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_watchdir_t const handle = context;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_kqueue* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
|
|
|
|
struct kevent ke;
|
2017-04-20 16:02:19 +00:00
|
|
|
struct timespec const ts = { 0, 0 };
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (kevent(backend->kq, NULL, 0, &ke, 1, &ts) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to fetch kevent: %s", tr_strerror(errno));
|
|
|
|
return;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Read directory with generic scan */
|
|
|
|
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_kqueue_free(tr_watchdir_backend* backend_base)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_kqueue* const backend = BACKEND_UPCAST(backend_base);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(backend->base.free_func == &tr_watchdir_kqueue_free);
|
|
|
|
|
|
|
|
if (backend->event != NULL)
|
|
|
|
{
|
|
|
|
event_del(backend->event);
|
|
|
|
event_free(backend->event);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->kq != -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
close(backend->kq);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->dirfd != -1)
|
|
|
|
{
|
|
|
|
close(backend->dirfd);
|
|
|
|
}
|
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_kqueue_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
|
|
|
struct kevent ke;
|
|
|
|
tr_watchdir_kqueue* backend;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
backend = tr_new0(tr_watchdir_kqueue, 1);
|
|
|
|
backend->base.free_func = &tr_watchdir_kqueue_free;
|
|
|
|
backend->kq = -1;
|
|
|
|
backend->dirfd = -1;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((backend->kq = kqueue()) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to start kqueue");
|
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Open fd for watching */
|
|
|
|
if ((backend->dirfd = open(path, O_RDONLY | O_EVTONLY)) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to passively watch directory \"%s\": %s", path, tr_strerror(errno));
|
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Register kevent filter with kqueue descriptor */
|
|
|
|
EV_SET(&ke, backend->dirfd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, KQUEUE_WATCH_MASK, 0, NULL);
|
|
|
|
|
|
|
|
if (kevent(backend->kq, &ke, 1, NULL, 0, NULL) == -1)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
log_error("Failed to set directory event filter with fd %d: %s", backend->kq, tr_strerror(errno));
|
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Create libevent task for event descriptor */
|
|
|
|
if ((backend->event = event_new(tr_watchdir_get_event_base(handle), backend->kq, EV_READ | EV_ET | EV_PERSIST,
|
|
|
|
&tr_watchdir_kqueue_on_event, handle)) == NULL)
|
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));
|
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (event_add(backend->event, NULL) == -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));
|
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Trigger one event for the initial scan */
|
|
|
|
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
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_kqueue_free(BACKEND_DOWNCAST(backend));
|
|
|
|
return NULL;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|