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-03-17 00:23:44 +00:00
|
|
|
#include <cstddef> /* offsetof */
|
|
|
|
#include <cstdlib> /* realloc() */
|
2016-01-02 14:28:59 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <process.h> /* _beginthreadex() */
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#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"
|
|
|
|
#include "log.h"
|
|
|
|
#include "net.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_win32
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend base;
|
|
|
|
|
|
|
|
HANDLE fd;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD buffer[8 * 1024 / sizeof(DWORD)];
|
|
|
|
evutil_socket_t notify_pipe[2];
|
|
|
|
struct bufferevent* event;
|
|
|
|
HANDLE thread;
|
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_win32*)(b))
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
#define WIN32_WATCH_MASK (FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE)
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static BOOL tr_get_overlapped_result_ex(
|
|
|
|
HANDLE handle,
|
|
|
|
LPOVERLAPPED overlapped,
|
|
|
|
LPDWORD bytes_transferred,
|
|
|
|
DWORD timeout,
|
2017-04-19 12:04:45 +00:00
|
|
|
BOOL alertable)
|
2016-01-02 15:23:38 +00:00
|
|
|
{
|
2021-10-06 14:26:07 +00:00
|
|
|
using impl_t = BOOL(WINAPI*)(HANDLE, LPOVERLAPPED, LPDWORD, DWORD, BOOL);
|
2016-01-02 15:23:38 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
static impl_t real_impl = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool is_real_impl_valid = false;
|
2016-01-02 15:23:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!is_real_impl_valid)
|
2016-01-02 15:23:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
real_impl = (impl_t)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetOverlappedResultEx");
|
|
|
|
is_real_impl_valid = true;
|
2016-01-02 15:23:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (real_impl != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
return (*real_impl)(handle, overlapped, bytes_transferred, timeout, alertable);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 15:23:38 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const wait_result = WaitForSingleObjectEx(handle, timeout, alertable);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (wait_result == WAIT_FAILED)
|
2016-01-02 15:26:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return FALSE;
|
2016-01-02 15:26:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wait_result == WAIT_IO_COMPLETION || wait_result == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
SetLastError(wait_result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(wait_result == WAIT_OBJECT_0);
|
2016-01-02 15:26:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return GetOverlappedResult(handle, overlapped, bytes_transferred, FALSE);
|
2016-01-02 15:23:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static unsigned int __stdcall tr_watchdir_win32_thread(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);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_win32* const backend = BACKEND_UPCAST(tr_watchdir_get_backend(handle));
|
|
|
|
DWORD bytes_transferred;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (tr_get_overlapped_result_ex(backend->fd, &backend->overlapped, &bytes_transferred, INFINITE, FALSE))
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
PFILE_NOTIFY_INFORMATION info = (PFILE_NOTIFY_INFORMATION)backend->buffer;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (info->NextEntryOffset != 0)
|
|
|
|
{
|
|
|
|
*((BYTE**)&info) += info->NextEntryOffset;
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
info->NextEntryOffset = bytes_transferred - ((BYTE*)info - (BYTE*)backend->buffer);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
send(backend->notify_pipe[1], (char const*)backend->buffer, bytes_transferred, 0);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!ReadDirectoryChangesW(
|
|
|
|
backend->fd,
|
|
|
|
backend->buffer,
|
|
|
|
sizeof(backend->buffer),
|
|
|
|
FALSE,
|
|
|
|
WIN32_WATCH_MASK,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
&backend->overlapped,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr))
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(_("Couldn't read directory changes"));
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (GetLastError() != ERROR_OPERATION_ABORTED)
|
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(_("Couldn't wait for directory changes"));
|
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 0;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void tr_watchdir_win32_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_win32_on_event(struct bufferevent* event, 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);
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t nread;
|
|
|
|
size_t name_size = MAX_PATH * sizeof(WCHAR);
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* buffer = static_cast<char*>(tr_malloc(sizeof(FILE_NOTIFY_INFORMATION) + name_size));
|
2017-04-19 12:04:45 +00:00
|
|
|
PFILE_NOTIFY_INFORMATION ev = (PFILE_NOTIFY_INFORMATION)buffer;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const header_size = offsetof(FILE_NOTIFY_INFORMATION, FileName);
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
while ((nread = bufferevent_read(event, ev, header_size)) != 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(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("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 != header_size)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't read event: expected {expected_size}, got {actual_size}"),
|
|
|
|
fmt::arg("expected_size", header_size),
|
|
|
|
fmt::arg("actual_size", nread)));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const nleft = ev->NextEntryOffset - nread;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ev->FileNameLength % sizeof(WCHAR) == 0);
|
|
|
|
TR_ASSERT(ev->FileNameLength > 0);
|
|
|
|
TR_ASSERT(ev->FileNameLength <= nleft);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (nleft > name_size)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
name_size = nleft;
|
2021-09-12 17:41:49 +00:00
|
|
|
buffer = static_cast<char*>(tr_realloc(buffer, sizeof(FILE_NOTIFY_INFORMATION) + name_size));
|
2017-04-19 12:04:45 +00:00
|
|
|
ev = (PFILE_NOTIFY_INFORMATION)buffer;
|
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, buffer + header_size, nleft)) == (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(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("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 != nleft)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't read filename: expected {expected_size}, got {actual_size}"),
|
|
|
|
fmt::arg("expected_size", nleft),
|
|
|
|
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
|
|
|
if (ev->Action == FILE_ACTION_ADDED || ev->Action == FILE_ACTION_MODIFIED || ev->Action == FILE_ACTION_RENAMED_NEW_NAME)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* name = tr_win32_native_to_utf8(ev->FileName, ev->FileNameLength / sizeof(WCHAR));
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (name != nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_process(handle, name);
|
|
|
|
tr_free(name);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(buffer);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_watchdir_win32_free(tr_watchdir_backend* backend_base)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_win32* 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_win32_free);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->fd != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CancelIoEx(backend->fd, &backend->overlapped);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (backend->thread != nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
WaitForSingleObject(backend->thread, INFINITE);
|
|
|
|
CloseHandle(backend->thread);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (backend->event != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
bufferevent_free(backend->event);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->notify_pipe[0] != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
evutil_closesocket(backend->notify_pipe[0]);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->notify_pipe[1] != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
evutil_closesocket(backend->notify_pipe[1]);
|
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (backend->fd != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(backend->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(backend);
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_backend* tr_watchdir_win32_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
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
auto* const backend = tr_new0(tr_watchdir_win32, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
backend->base.free_func = &tr_watchdir_win32_free;
|
|
|
|
backend->fd = INVALID_HANDLE_VALUE;
|
|
|
|
backend->notify_pipe[0] = backend->notify_pipe[1] = TR_BAD_SOCKET;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-03-17 00:23:44 +00:00
|
|
|
wchar_t* wide_path = tr_win32_utf8_to_native(path, -1);
|
|
|
|
if (wide_path == nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't convert '{path}' to native path"), fmt::arg("path", path)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((backend->fd = CreateFileW(
|
|
|
|
wide_path,
|
|
|
|
FILE_LIST_DIRECTORY,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr)) == INVALID_HANDLE_VALUE)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't read '{path}'"), fmt::arg("path", path)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_path);
|
2021-09-15 00:18:09 +00:00
|
|
|
wide_path = nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
backend->overlapped.Pointer = handle;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (!ReadDirectoryChangesW(
|
|
|
|
backend->fd,
|
|
|
|
backend->buffer,
|
|
|
|
sizeof(backend->buffer),
|
|
|
|
FALSE,
|
|
|
|
WIN32_WATCH_MASK,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
&backend->overlapped,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr))
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't read '{path}'"), fmt::arg("path", path)));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (evutil_socketpair(AF_INET, SOCK_STREAM, 0, backend->notify_pipe) == -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(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't create pipe: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2017-04-19 12:04:45 +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->notify_pipe[0], 0)) == 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(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't create 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
|
|
|
goto fail;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bufferevent_setwatermark(backend->event, EV_READ, sizeof(FILE_NOTIFY_INFORMATION), 0);
|
2021-09-15 00:18:09 +00:00
|
|
|
bufferevent_setcb(backend->event, &tr_watchdir_win32_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
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if ((backend->thread = (HANDLE)_beginthreadex(nullptr, 0, &tr_watchdir_win32_thread, handle, 0, nullptr)) == nullptr)
|
2016-01-02 14:28:59 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddError(_("Couldn't create thread"));
|
2017-04-19 12:04:45 +00:00
|
|
|
goto fail;
|
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_win32_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_logAddError(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("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
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_win32_free(BACKEND_DOWNCAST(backend));
|
|
|
|
tr_free(wide_path);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|