Merge issue-40-windows-process: Windows process and handle support (closes #40)

This commit is contained in:
faumaray committed 2026-12-03 16:11:40 +03:00
commit 62c2082fb3
7 files changed
+2754 -294

No files matched your search

+103 -1
View File
@@ -24,6 +24,19 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#else
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <sys/stat.h>
#endif
#include <algorithm>
@@ -56,6 +69,26 @@ namespace Void::Net
std::atomic<std::uint64_t> g_dl_temp_seq{0};
#if defined(_WIN32)
// Win32 paths above MAX_PATH must use the extended-length prefix, and
// forward slashes are not accepted by the wide file APIs.
[[nodiscard]] std::filesystem::path
extended_path(const std::filesystem::path& path)
{
std::error_code ec;
const auto absolute = std::filesystem::absolute(path, ec);
std::wstring text = (ec ? path : absolute).wstring();
std::replace(text.begin(), text.end(), L'/', L'\\');
if (text.rfind(L"\\\\?\\", 0) == 0 || text.size() < MAX_PATH) {
return std::filesystem::path{text};
}
if (text.rfind(L"\\\\", 0) == 0) {
return std::filesystem::path{L"\\\\?\\UNC\\" + text.substr(2)};
}
return std::filesystem::path{L"\\\\?\\" + text};
}
#endif
[[nodiscard]] bool
equal_ascii_ci(std::string_view a, std::string_view b) noexcept
{
@@ -557,13 +590,26 @@ namespace Void::Net
}
const std::filesystem::path tmp_path =
parent / std::format(".void-dl-{}-{}",
#if defined(_WIN32)
static_cast<long long>(::_getpid()),
#else
static_cast<long long>(::getpid()),
#endif
g_dl_temp_seq.fetch_add(
1, std::memory_order_relaxed));
#if defined(_WIN32)
// Create exclusively next to the destination: an existing entry
// (including a reparse point) is never followed or truncated.
const int fd = ::_wopen(
extended_path(tmp_path).c_str(),
_O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY | _O_NOINHERIT,
_S_IREAD | _S_IWRITE);
#else
const int fd = ::open(
tmp_path.c_str(),
O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW,
0644);
#endif
if (fd < 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
@@ -593,10 +639,17 @@ namespace Void::Net
{
std::size_t off = 0;
while (off < data.size()) {
#if defined(_WIN32)
const int w = ::_write(
m_fd,
reinterpret_cast<const char*>(data.data()) + off,
static_cast<unsigned int>(data.size() - off));
#else
const ssize_t w = ::write(
m_fd,
reinterpret_cast<const char*>(data.data()) + off,
data.size() - off);
#endif
if (w < 0 && errno == EINTR) {
continue;
}
@@ -616,6 +669,50 @@ namespace Void::Net
[[nodiscard]] Void::Result::Result<void> commit()
{
#if defined(_WIN32)
if (::_commit(m_fd) != 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: flush download file failed: {}",
std::strerror(errno)),
std::error_code{errno, std::generic_category()})};
}
const int raw = std::exchange(m_fd, -1);
if (::_close(raw) != 0) {
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: close download file failed: {}",
std::strerror(errno)),
std::error_code{errno, std::generic_category()})};
}
// Never replace a reparse point: a hostile workspace entry
// could redirect the completed download outside the target.
const DWORD attributes =
::GetFileAttributesW(extended_path(m_dest).c_str());
if (attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
"Net: refusing to replace a reparse-point "
"download destination")};
}
if (::MoveFileExW(extended_path(m_tmp).c_str(),
extended_path(m_dest).c_str(),
MOVEFILE_REPLACE_EXISTING |
MOVEFILE_WRITE_THROUGH) == 0) {
const DWORD error = ::GetLastError();
discard();
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: replace download file failed: "
"win32={}",
error),
std::error_code{static_cast<int>(error),
std::system_category()})};
}
#else
if (::fsync(m_fd) != 0) {
return std::unexpected{make_net_error(
NetErrorCode::io_error,
@@ -639,9 +736,10 @@ namespace Void::Net
return std::unexpected{make_net_error(
NetErrorCode::io_error,
std::format("Net: rename download file failed: {}",
ec.message()),
ec.message()),
ec)};
}
#endif
m_committed = true;
return {};
}
@@ -649,7 +747,11 @@ namespace Void::Net
void discard() noexcept
{
if (m_fd >= 0) {
#if defined(_WIN32)
(void)::_close(m_fd);
#else
(void)::close(m_fd);
#endif
m_fd = -1;
}
if (!m_committed && !m_tmp.empty()) {
+5
View File
@@ -1,5 +1,10 @@
void_add_module(Net
INTERNAL_DEPS Functional Text Time
# The HTTP server is POSIX-only. Windows builds get the client-side
# surface; a Windows server port is explicitly out of scope, and any
# server symbol left undefined fails closed at link time.
EXCLUDE_SOURCES_WIN32
HttpServer.cpp
EXTERNAL_DEPS
Boost::headers
Boost::beast