1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-19 08:49:35 +01:00

treewide: Fix MinGW build

Several bugs to squash:

- Apparently DELETE is an already used macro with Win32. We can avoid it
  by using Camel case instead (slightly hacky but also fits the naming
  convention better)

- Gets rid of the raw usage of isatty. Added an isTTY impl to abstract over
  the raw API.
This commit is contained in:
Sergei Zimmerman 2025-11-18 04:30:57 +03:00
parent f8141a2c26
commit 8165419a0c
No known key found for this signature in database
10 changed files with 44 additions and 25 deletions

View file

@ -209,7 +209,7 @@ std::vector<nlohmann::json> Fetch::fetchUrls(const std::vector<Pointer> & pointe
auto url = api.endpoint + "/objects/batch"; auto url = api.endpoint + "/objects/batch";
const auto & authHeader = api.authHeader; const auto & authHeader = api.authHeader;
FileTransferRequest request(parseURL(url)); FileTransferRequest request(parseURL(url));
request.method = HttpMethod::POST; request.method = HttpMethod::Post;
Headers headers; Headers headers;
if (authHeader.has_value()) if (authHeader.has_value())
headers.push_back({"Authorization", *authHeader}); headers.push_back({"Authorization", *authHeader});

View file

@ -386,17 +386,17 @@ struct curlFileTransfer : public FileTransfer
if (settings.downloadSpeed.get() > 0) if (settings.downloadSpeed.get() > 0)
curl_easy_setopt(req, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) (settings.downloadSpeed.get() * 1024)); curl_easy_setopt(req, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) (settings.downloadSpeed.get() * 1024));
if (request.method == HttpMethod::HEAD) if (request.method == HttpMethod::Head)
curl_easy_setopt(req, CURLOPT_NOBODY, 1); curl_easy_setopt(req, CURLOPT_NOBODY, 1);
if (request.method == HttpMethod::DELETE) if (request.method == HttpMethod::Delete)
curl_easy_setopt(req, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_easy_setopt(req, CURLOPT_CUSTOMREQUEST, "DELETE");
if (request.data) { if (request.data) {
if (request.method == HttpMethod::POST) { if (request.method == HttpMethod::Post) {
curl_easy_setopt(req, CURLOPT_POST, 1L); curl_easy_setopt(req, CURLOPT_POST, 1L);
curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) request.data->sizeHint); curl_easy_setopt(req, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) request.data->sizeHint);
} else if (request.method == HttpMethod::PUT) { } else if (request.method == HttpMethod::Put) {
curl_easy_setopt(req, CURLOPT_UPLOAD, 1L); curl_easy_setopt(req, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(req, CURLOPT_INFILESIZE_LARGE, (curl_off_t) request.data->sizeHint); curl_easy_setopt(req, CURLOPT_INFILESIZE_LARGE, (curl_off_t) request.data->sizeHint);
} else { } else {

View file

@ -120,7 +120,7 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
try { try {
FileTransferRequest request(makeRequest(path)); FileTransferRequest request(makeRequest(path));
request.method = HttpMethod::HEAD; request.method = HttpMethod::Head;
getFileTransfer()->download(request); getFileTransfer()->download(request);
return true; return true;
} catch (FileTransferError & e) { } catch (FileTransferError & e) {
@ -141,7 +141,7 @@ void HttpBinaryCacheStore::upload(
std::optional<Headers> headers) std::optional<Headers> headers)
{ {
auto req = makeRequest(path); auto req = makeRequest(path);
req.method = HttpMethod::PUT; req.method = HttpMethod::Put;
if (headers) { if (headers) {
req.headers.reserve(req.headers.size() + headers->size()); req.headers.reserve(req.headers.size() + headers->size());

View file

@ -87,11 +87,11 @@ extern const unsigned int RETRY_TIME_MS_DEFAULT;
* HTTP methods supported by FileTransfer. * HTTP methods supported by FileTransfer.
*/ */
enum struct HttpMethod { enum struct HttpMethod {
GET, Get,
PUT, Put,
HEAD, Head,
POST, Post,
DELETE, Delete,
}; };
/** /**
@ -110,7 +110,7 @@ struct FileTransferRequest
VerbatimURL uri; VerbatimURL uri;
Headers headers; Headers headers;
std::string expectedETag; std::string expectedETag;
HttpMethod method = HttpMethod::GET; HttpMethod method = HttpMethod::Get;
size_t tries = fileTransferSettings.tries; size_t tries = fileTransferSettings.tries;
unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT; unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT;
ActivityId parentAct; ActivityId parentAct;
@ -164,14 +164,14 @@ struct FileTransferRequest
std::string verb() const std::string verb() const
{ {
switch (method) { switch (method) {
case HttpMethod::HEAD: case HttpMethod::Head:
case HttpMethod::GET: case HttpMethod::Get:
return "download"; return "download";
case HttpMethod::PUT: case HttpMethod::Put:
case HttpMethod::POST: case HttpMethod::Post:
assert(data); assert(data);
return "upload"; return "upload";
case HttpMethod::DELETE: case HttpMethod::Delete:
return "delet"; return "delet";
} }
unreachable(); unreachable();

View file

@ -295,7 +295,7 @@ std::string S3BinaryCacheStore::createMultipartUpload(
url.query["uploads"] = ""; url.query["uploads"] = "";
req.uri = VerbatimURL(url); req.uri = VerbatimURL(url);
req.method = HttpMethod::POST; req.method = HttpMethod::Post;
StringSource payload{std::string_view("")}; StringSource payload{std::string_view("")};
req.data = {payload}; req.data = {payload};
req.mimeType = mimeType; req.mimeType = mimeType;
@ -325,7 +325,7 @@ S3BinaryCacheStore::uploadPart(std::string_view key, std::string_view uploadId,
} }
auto req = makeRequest(key); auto req = makeRequest(key);
req.method = HttpMethod::PUT; req.method = HttpMethod::Put;
req.setupForS3(); req.setupForS3();
auto url = req.uri.parsed(); auto url = req.uri.parsed();
@ -355,7 +355,7 @@ void S3BinaryCacheStore::abortMultipartUpload(std::string_view key, std::string_
auto url = req.uri.parsed(); auto url = req.uri.parsed();
url.query["uploadId"] = uploadId; url.query["uploadId"] = uploadId;
req.uri = VerbatimURL(url); req.uri = VerbatimURL(url);
req.method = HttpMethod::DELETE; req.method = HttpMethod::Delete;
getFileTransfer()->enqueueFileTransfer(req).get(); getFileTransfer()->enqueueFileTransfer(req).get();
} catch (...) { } catch (...) {
@ -372,7 +372,7 @@ void S3BinaryCacheStore::completeMultipartUpload(
auto url = req.uri.parsed(); auto url = req.uri.parsed();
url.query["uploadId"] = uploadId; url.query["uploadId"] = uploadId;
req.uri = VerbatimURL(url); req.uri = VerbatimURL(url);
req.method = HttpMethod::POST; req.method = HttpMethod::Post;
std::string xml = "<CompleteMultipartUpload>"; std::string xml = "<CompleteMultipartUpload>";
for (const auto & [idx, etag] : enumerate(partEtags)) { for (const auto & [idx, etag] : enumerate(partEtags)) {

View file

@ -4,7 +4,15 @@
#include <limits> #include <limits>
#include <string> #include <string>
#include "nix/util/file-descriptor.hh"
namespace nix { namespace nix {
/**
* Determine whether \param fd is a terminal.
*/
bool isTTY(Descriptor fd);
/** /**
* Determine whether ANSI escape sequences are appropriate for the * Determine whether ANSI escape sequences are appropriate for the
* present output. * present output.

View file

@ -64,6 +64,16 @@ inline std::pair<int, size_t> charWidthUTF8Helper(std::string_view s)
namespace nix { namespace nix {
bool isTTY(Descriptor fd)
{
#ifndef _WIN32
return isatty(fd);
#else
DWORD mode;
return GetConsoleMode(fd, &mode);
#endif
}
bool isTTY() bool isTTY()
{ {
static const bool tty = isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb" static const bool tty = isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"

View file

@ -75,7 +75,7 @@ struct CmdCatNar : StoreCommand, MixCat
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
AutoCloseFD fd = open(narPath.c_str(), O_RDONLY); AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY));
if (!fd) if (!fd)
throw SysError("opening NAR file '%s'", narPath); throw SysError("opening NAR file '%s'", narPath);
auto source = FdSource{fd.get()}; auto source = FdSource{fd.get()};

View file

@ -1,13 +1,14 @@
#include "nix/cmd/command.hh" #include "nix/cmd/command.hh"
#include "nix/store/store-api.hh" #include "nix/store/store-api.hh"
#include "nix/util/archive.hh" #include "nix/util/archive.hh"
#include "nix/util/terminal.hh"
using namespace nix; using namespace nix;
static FdSink getNarSink() static FdSink getNarSink()
{ {
auto fd = getStandardOutput(); auto fd = getStandardOutput();
if (isatty(fd)) if (isTTY(fd))
throw UsageError("refusing to write NAR to a terminal"); throw UsageError("refusing to write NAR to a terminal");
return FdSink(std::move(fd)); return FdSink(std::move(fd));
} }

View file

@ -145,7 +145,7 @@ struct CmdLsNar : Command, MixLs
void run() override void run() override
{ {
AutoCloseFD fd = open(narPath.c_str(), O_RDONLY); AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY));
if (!fd) if (!fd)
throw SysError("opening NAR file '%s'", narPath); throw SysError("opening NAR file '%s'", narPath);
auto source = FdSource{fd.get()}; auto source = FdSource{fd.get()};