transmission/libtransmission/watchdir-win32.cc

350 lines
10 KiB
C++
Raw Normal View History

// This file Copyright © 2015-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
2022-08-10 13:34:51 +00:00
#include <array>
#include <cerrno>
#include <cstddef> // for offsetof
2022-08-10 13:34:51 +00:00
#include <process.h> // for _beginthreadex()
#include <windows.h>
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/util.h>
#include <fmt/core.h>
#define LIBTRANSMISSION_WATCHDIR_MODULE
#include "transmission.h"
#include "log.h"
#include "net.h"
#include "tr-assert.h"
#include "utils.h"
2022-08-10 13:34:51 +00:00
#include "watchdir-base.h"
2022-08-10 13:34:51 +00:00
namespace libtransmission
{
namespace
{
2022-08-10 13:34:51 +00:00
BOOL tr_get_overlapped_result_ex(
HANDLE handle,
LPOVERLAPPED overlapped,
LPDWORD bytes_transferred,
DWORD timeout,
BOOL alertable)
{
using impl_t = BOOL(WINAPI*)(HANDLE, LPOVERLAPPED, LPDWORD, DWORD, BOOL);
static impl_t real_impl = nullptr;
static bool is_real_impl_valid = false;
if (!is_real_impl_valid)
{
real_impl = (impl_t)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetOverlappedResultEx");
is_real_impl_valid = true;
}
if (real_impl != nullptr)
{
return (*real_impl)(handle, overlapped, bytes_transferred, timeout, alertable);
}
DWORD const wait_result = WaitForSingleObjectEx(handle, timeout, alertable);
if (wait_result == WAIT_FAILED)
2016-01-02 15:26:23 +00:00
{
return FALSE;
2016-01-02 15:26:23 +00:00
}
if (wait_result == WAIT_IO_COMPLETION || wait_result == WAIT_TIMEOUT)
{
SetLastError(wait_result);
return FALSE;
}
TR_ASSERT(wait_result == WAIT_OBJECT_0);
2016-01-02 15:26:23 +00:00
return GetOverlappedResult(handle, overlapped, bytes_transferred, FALSE);
}
2022-08-10 13:34:51 +00:00
class Win32Watchdir final : public impl::BaseWatchdir
{
2022-08-10 13:34:51 +00:00
public:
Win32Watchdir(
std::string_view dirname,
Callback callback,
libtransmission::TimerMaker& timer_maker,
struct event_base* event_base)
: BaseWatchdir{ dirname, std::move(callback), timer_maker }
{
init(event_base);
scan();
}
Win32Watchdir(Win32Watchdir&&) = delete;
Win32Watchdir(Win32Watchdir const&) = delete;
Win32Watchdir& operator=(Win32Watchdir&&) = delete;
Win32Watchdir& operator=(Win32Watchdir const&) = delete;
2022-08-10 13:34:51 +00:00
~Win32Watchdir() override
{
2022-08-10 13:34:51 +00:00
if (fd_ != INVALID_HANDLE_VALUE)
{
CancelIoEx(fd_, &overlapped_);
}
2022-08-10 13:34:51 +00:00
if (thread_ != nullptr)
{
2022-08-10 13:34:51 +00:00
WaitForSingleObject(thread_, INFINITE);
CloseHandle(thread_);
}
2022-08-10 13:34:51 +00:00
if (event_ != nullptr)
{
bufferevent_free(event_);
}
2022-08-10 13:34:51 +00:00
if (notify_pipe_[0] != TR_BAD_SOCKET)
{
evutil_closesocket(notify_pipe_[0]);
}
2022-08-10 13:34:51 +00:00
if (notify_pipe_[1] != TR_BAD_SOCKET)
{
2022-08-10 13:34:51 +00:00
evutil_closesocket(notify_pipe_[1]);
}
2022-08-10 13:34:51 +00:00
if (fd_ != INVALID_HANDLE_VALUE)
{
CloseHandle(fd_);
}
}
2022-08-10 13:34:51 +00:00
private:
static auto constexpr Win32WatchMask =
(FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE);
2022-08-10 13:34:51 +00:00
void init(struct event_base* event_base)
{
2022-08-10 13:34:51 +00:00
tr_net_init();
auto const path = dirname();
auto const wide_path = tr_win32_utf8_to_native(path);
if (std::empty(wide_path))
{
2022-08-10 13:34:51 +00:00
tr_logAddError(fmt::format(_("Couldn't convert '{path}' to native path"), fmt::arg("path", path)));
return;
}
2022-08-10 13:34:51 +00:00
if ((fd_ = CreateFileW(
wide_path.c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
nullptr)) == INVALID_HANDLE_VALUE)
{
2022-08-10 13:34:51 +00:00
tr_logAddError(fmt::format(_("Couldn't read '{path}'"), fmt::arg("path", path)));
return;
}
2022-08-10 13:34:51 +00:00
overlapped_.Pointer = this;
2022-08-10 13:34:51 +00:00
if (!ReadDirectoryChangesW(fd_, buffer_, sizeof(buffer_), false, Win32WatchMask, nullptr, &overlapped_, nullptr))
{
2022-08-10 13:34:51 +00:00
tr_logAddError(fmt::format(_("Couldn't read '{path}'"), fmt::arg("path", path)));
return;
}
2022-08-10 13:34:51 +00:00
if (evutil_socketpair(AF_INET, SOCK_STREAM, 0, std::data(notify_pipe_)) == -1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
2022-08-10 13:34:51 +00:00
_("Couldn't create pipe: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
2022-08-10 13:34:51 +00:00
return;
}
2022-08-10 13:34:51 +00:00
event_ = bufferevent_socket_new(event_base, notify_pipe_[0], 0);
if (event_ == nullptr)
{
2022-08-10 13:34:51 +00:00
auto const error_code = errno;
tr_logAddError(fmt::format(
2022-08-10 13:34:51 +00:00
_("Couldn't create event: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
return;
}
2022-08-10 13:34:51 +00:00
bufferevent_setwatermark(event_, EV_READ, sizeof(FILE_NOTIFY_INFORMATION), 0);
bufferevent_setcb(event_, &Win32Watchdir::onBufferEvent, nullptr, nullptr, this);
bufferevent_enable(event_, EV_READ);
thread_ = (HANDLE)_beginthreadex(nullptr, 0, Win32Watchdir::staticThreadFunc, this, 0, nullptr);
if (thread_ == nullptr)
{
2022-08-10 13:34:51 +00:00
tr_logAddError(_("Couldn't create thread"));
return;
}
}
2022-08-10 13:34:51 +00:00
static unsigned int __stdcall staticThreadFunc(void* vself)
{
2022-08-10 13:34:51 +00:00
return static_cast<Win32Watchdir*>(vself)->threadFunc();
}
2022-08-10 13:34:51 +00:00
unsigned int threadFunc()
{
2022-08-10 13:34:51 +00:00
DWORD bytes_transferred;
2022-08-10 13:34:51 +00:00
while (tr_get_overlapped_result_ex(fd_, &overlapped_, &bytes_transferred, INFINITE, FALSE))
{
PFILE_NOTIFY_INFORMATION info = (PFILE_NOTIFY_INFORMATION)buffer_;
2022-08-10 13:34:51 +00:00
while (info->NextEntryOffset != 0)
{
*((BYTE**)&info) += info->NextEntryOffset;
}
info->NextEntryOffset = bytes_transferred - ((BYTE*)info - (BYTE*)buffer_);
send(notify_pipe_[1], (char const*)buffer_, bytes_transferred, 0);
if (!ReadDirectoryChangesW(fd_, buffer_, sizeof(buffer_), FALSE, Win32WatchMask, nullptr, &overlapped_, nullptr))
{
tr_logAddError(_("Couldn't read directory changes"));
return 0;
}
}
if (GetLastError() != ERROR_OPERATION_ABORTED)
{
tr_logAddError(_("Couldn't wait for directory changes"));
}
return 0;
}
2022-08-10 13:34:51 +00:00
static void onFirstScan(evutil_socket_t /*unused*/, short /*unused*/, void* vself)
{
2022-08-10 13:34:51 +00:00
static_cast<Win32Watchdir*>(vself)->scan();
}
2022-08-10 13:34:51 +00:00
static void onBufferEvent(struct bufferevent* event, void* vself)
{
2022-08-10 13:34:51 +00:00
static_cast<Win32Watchdir*>(vself)->processBufferEvent(event);
}
2022-08-10 13:34:51 +00:00
void processBufferEvent(struct bufferevent* event)
{
2022-08-10 13:34:51 +00:00
size_t name_size = MAX_PATH * sizeof(WCHAR);
2022-08-10 13:34:51 +00:00
auto buffer = std::vector<char>{};
buffer.resize(sizeof(FILE_NOTIFY_INFORMATION) + name_size);
PFILE_NOTIFY_INFORMATION ev = (PFILE_NOTIFY_INFORMATION)std::data(buffer);
2022-08-10 13:34:51 +00:00
size_t const header_size = offsetof(FILE_NOTIFY_INFORMATION, FileName);
2022-08-10 13:34:51 +00:00
// Read the size of the struct excluding name into buf.
// Guaranteed to have at least sizeof(*ev) available
for (;;)
{
auto nread = bufferevent_read(event, ev, header_size);
if (nread == 0)
{
break;
}
2022-08-10 13:34:51 +00:00
if (nread == (size_t)-1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't read event: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
break;
}
2022-08-10 13:34:51 +00:00
if (nread != header_size)
{
tr_logAddError(fmt::format(
_("Couldn't read event: expected {expected_size}, got {actual_size}"),
fmt::arg("expected_size", header_size),
fmt::arg("actual_size", nread)));
break;
}
2022-08-10 13:34:51 +00:00
size_t const nleft = ev->NextEntryOffset - nread;
2022-08-10 13:34:51 +00:00
TR_ASSERT(ev->FileNameLength % sizeof(WCHAR) == 0);
TR_ASSERT(ev->FileNameLength > 0);
TR_ASSERT(ev->FileNameLength <= nleft);
2022-08-10 13:34:51 +00:00
if (nleft > name_size)
{
name_size = nleft;
buffer.resize(sizeof(FILE_NOTIFY_INFORMATION) + name_size);
ev = (PFILE_NOTIFY_INFORMATION)std::data(buffer);
}
// consume entire name into buffer
nread = bufferevent_read(event, &buffer[header_size], nleft);
if (nread == (size_t)-1)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't read filename: {error} ({error_code})"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
break;
}
2022-08-10 13:34:51 +00:00
if (nread != nleft)
{
tr_logAddError(fmt::format(
_("Couldn't read filename: expected {expected_size}, got {actual_size}"),
fmt::arg("expected_size", nleft),
fmt::arg("actual_size", nread)));
break;
}
2022-08-10 13:34:51 +00:00
if (ev->Action == FILE_ACTION_ADDED || ev->Action == FILE_ACTION_MODIFIED ||
ev->Action == FILE_ACTION_RENAMED_NEW_NAME)
{
if (auto const name = tr_win32_native_to_utf8({ ev->FileName, ev->FileNameLength / sizeof(WCHAR) });
!std::empty(name))
{
processFile(name);
}
}
}
}
2022-08-10 13:34:51 +00:00
HANDLE fd_ = INVALID_HANDLE_VALUE;
OVERLAPPED overlapped_ = {};
DWORD buffer_[8 * 1024 / sizeof(DWORD)];
std::array<evutil_socket_t, 2> notify_pipe_{ static_cast<evutil_socket_t>(-1), static_cast<evutil_socket_t>(-1) };
struct bufferevent* event_ = nullptr;
HANDLE thread_ = {};
};
2022-08-10 13:34:51 +00:00
} // namespace
2022-08-10 13:34:51 +00:00
std::unique_ptr<Watchdir> Watchdir::create(
std::string_view dirname,
Callback callback,
TimerMaker& timer_maker,
struct event_base* event_base)
{
return std::make_unique<Win32Watchdir>(dirname, std::move(callback), timer_maker, event_base);
}
2022-08-10 13:34:51 +00:00
} // namespace libtransmission