diff --git a/src/libfetchers/git-lfs-fetch.cc b/src/libfetchers/git-lfs-fetch.cc index 936976e55..f1982c314 100644 --- a/src/libfetchers/git-lfs-fetch.cc +++ b/src/libfetchers/git-lfs-fetch.cc @@ -209,7 +209,7 @@ std::vector Fetch::fetchUrls(const std::vector & pointe auto url = api.endpoint + "/objects/batch"; const auto & authHeader = api.authHeader; FileTransferRequest request(parseURL(url)); - request.method = HttpMethod::POST; + request.method = HttpMethod::Post; Headers headers; if (authHeader.has_value()) headers.push_back({"Authorization", *authHeader}); diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index 3f0b4f5cb..5acad485c 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -386,17 +386,17 @@ struct curlFileTransfer : public FileTransfer if (settings.downloadSpeed.get() > 0) 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); - if (request.method == HttpMethod::DELETE) + if (request.method == HttpMethod::Delete) curl_easy_setopt(req, CURLOPT_CUSTOMREQUEST, "DELETE"); 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_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_INFILESIZE_LARGE, (curl_off_t) request.data->sizeHint); } else { diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index 555da12ea..ef6ae92a4 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -120,7 +120,7 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path) try { FileTransferRequest request(makeRequest(path)); - request.method = HttpMethod::HEAD; + request.method = HttpMethod::Head; getFileTransfer()->download(request); return true; } catch (FileTransferError & e) { @@ -141,7 +141,7 @@ void HttpBinaryCacheStore::upload( std::optional headers) { auto req = makeRequest(path); - req.method = HttpMethod::PUT; + req.method = HttpMethod::Put; if (headers) { req.headers.reserve(req.headers.size() + headers->size()); diff --git a/src/libstore/include/nix/store/filetransfer.hh b/src/libstore/include/nix/store/filetransfer.hh index 6419a686e..0523f79c7 100644 --- a/src/libstore/include/nix/store/filetransfer.hh +++ b/src/libstore/include/nix/store/filetransfer.hh @@ -87,11 +87,11 @@ extern const unsigned int RETRY_TIME_MS_DEFAULT; * HTTP methods supported by FileTransfer. */ enum struct HttpMethod { - GET, - PUT, - HEAD, - POST, - DELETE, + Get, + Put, + Head, + Post, + Delete, }; /** @@ -110,7 +110,7 @@ struct FileTransferRequest VerbatimURL uri; Headers headers; std::string expectedETag; - HttpMethod method = HttpMethod::GET; + HttpMethod method = HttpMethod::Get; size_t tries = fileTransferSettings.tries; unsigned int baseRetryTimeMs = RETRY_TIME_MS_DEFAULT; ActivityId parentAct; @@ -164,14 +164,14 @@ struct FileTransferRequest std::string verb() const { switch (method) { - case HttpMethod::HEAD: - case HttpMethod::GET: + case HttpMethod::Head: + case HttpMethod::Get: return "download"; - case HttpMethod::PUT: - case HttpMethod::POST: + case HttpMethod::Put: + case HttpMethod::Post: assert(data); return "upload"; - case HttpMethod::DELETE: + case HttpMethod::Delete: return "delet"; } unreachable(); diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index bc7f53f4d..5dfc7f11d 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -295,7 +295,7 @@ std::string S3BinaryCacheStore::createMultipartUpload( url.query["uploads"] = ""; req.uri = VerbatimURL(url); - req.method = HttpMethod::POST; + req.method = HttpMethod::Post; StringSource payload{std::string_view("")}; req.data = {payload}; req.mimeType = mimeType; @@ -325,7 +325,7 @@ S3BinaryCacheStore::uploadPart(std::string_view key, std::string_view uploadId, } auto req = makeRequest(key); - req.method = HttpMethod::PUT; + req.method = HttpMethod::Put; req.setupForS3(); auto url = req.uri.parsed(); @@ -355,7 +355,7 @@ void S3BinaryCacheStore::abortMultipartUpload(std::string_view key, std::string_ auto url = req.uri.parsed(); url.query["uploadId"] = uploadId; req.uri = VerbatimURL(url); - req.method = HttpMethod::DELETE; + req.method = HttpMethod::Delete; getFileTransfer()->enqueueFileTransfer(req).get(); } catch (...) { @@ -372,7 +372,7 @@ void S3BinaryCacheStore::completeMultipartUpload( auto url = req.uri.parsed(); url.query["uploadId"] = uploadId; req.uri = VerbatimURL(url); - req.method = HttpMethod::POST; + req.method = HttpMethod::Post; std::string xml = ""; for (const auto & [idx, etag] : enumerate(partEtags)) { diff --git a/src/libutil/include/nix/util/terminal.hh b/src/libutil/include/nix/util/terminal.hh index fa71e074e..5e35cbb95 100644 --- a/src/libutil/include/nix/util/terminal.hh +++ b/src/libutil/include/nix/util/terminal.hh @@ -4,7 +4,15 @@ #include #include +#include "nix/util/file-descriptor.hh" + namespace nix { + +/** + * Determine whether \param fd is a terminal. + */ +bool isTTY(Descriptor fd); + /** * Determine whether ANSI escape sequences are appropriate for the * present output. diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc index fe22146ab..401ce1604 100644 --- a/src/libutil/terminal.cc +++ b/src/libutil/terminal.cc @@ -64,6 +64,16 @@ inline std::pair charWidthUTF8Helper(std::string_view s) namespace nix { +bool isTTY(Descriptor fd) +{ +#ifndef _WIN32 + return isatty(fd); +#else + DWORD mode; + return GetConsoleMode(fd, &mode); +#endif +} + bool isTTY() { static const bool tty = isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb" diff --git a/src/nix/cat.cc b/src/nix/cat.cc index 1284b50fd..bf58bb492 100644 --- a/src/nix/cat.cc +++ b/src/nix/cat.cc @@ -75,7 +75,7 @@ struct CmdCatNar : StoreCommand, MixCat void run(ref store) override { - AutoCloseFD fd = open(narPath.c_str(), O_RDONLY); + AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY)); if (!fd) throw SysError("opening NAR file '%s'", narPath); auto source = FdSource{fd.get()}; diff --git a/src/nix/dump-path.cc b/src/nix/dump-path.cc index f375b0ac8..62fd89877 100644 --- a/src/nix/dump-path.cc +++ b/src/nix/dump-path.cc @@ -1,13 +1,14 @@ #include "nix/cmd/command.hh" #include "nix/store/store-api.hh" #include "nix/util/archive.hh" +#include "nix/util/terminal.hh" using namespace nix; static FdSink getNarSink() { auto fd = getStandardOutput(); - if (isatty(fd)) + if (isTTY(fd)) throw UsageError("refusing to write NAR to a terminal"); return FdSink(std::move(fd)); } diff --git a/src/nix/ls.cc b/src/nix/ls.cc index 82721222e..9bf3c5996 100644 --- a/src/nix/ls.cc +++ b/src/nix/ls.cc @@ -145,7 +145,7 @@ struct CmdLsNar : Command, MixLs void run() override { - AutoCloseFD fd = open(narPath.c_str(), O_RDONLY); + AutoCloseFD fd = toDescriptor(open(narPath.c_str(), O_RDONLY)); if (!fd) throw SysError("opening NAR file '%s'", narPath); auto source = FdSource{fd.get()};