Merge pull request #11833 from NixOS/fix-11830

Fix #11830
This commit is contained in:
John Ericson 2024-11-07 16:08:55 -05:00 committed by GitHub
commit ff456f6713
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 51 additions and 21 deletions

View file

@ -543,7 +543,7 @@ public:
auto state(state_.lock()); auto state(state_.lock());
if (!state->active) return {}; if (!state->active) return {};
std::cerr << fmt("\r\e[K%s ", msg); std::cerr << fmt("\r\e[K%s ", msg);
auto s = trim(readLine(STDIN_FILENO, true)); auto s = trim(readLine(getStandardInput(), true));
if (s.size() != 1) return {}; if (s.size() != 1) return {};
draw(*state); draw(*state);
return s[0]; return s[0];

View file

@ -106,8 +106,25 @@ void drainFD(
#endif #endif
); );
/**
* Get [Standard Input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin))
*/
[[gnu::always_inline]] [[gnu::always_inline]]
inline Descriptor getStandardOut() { inline Descriptor getStandardInput()
{
#ifndef _WIN32
return STDIN_FILENO;
#else
return GetStdHandle(STD_INPUT_HANDLE);
#endif
}
/**
* Get [Standard Output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout))
*/
[[gnu::always_inline]]
inline Descriptor getStandardOutput()
{
#ifndef _WIN32 #ifndef _WIN32
return STDOUT_FILENO; return STDOUT_FILENO;
#else #else
@ -115,6 +132,19 @@ inline Descriptor getStandardOut() {
#endif #endif
} }
/**
* Get [Standard Error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))
*/
[[gnu::always_inline]]
inline Descriptor getStandardError()
{
#ifndef _WIN32
return STDERR_FILENO;
#else
return GetStdHandle(STD_ERROR_HANDLE);
#endif
}
/** /**
* Automatic cleanup of resources. * Automatic cleanup of resources.
*/ */

View file

@ -38,7 +38,7 @@ void Logger::warn(const std::string & msg)
void Logger::writeToStdout(std::string_view s) void Logger::writeToStdout(std::string_view s)
{ {
Descriptor standard_out = getStandardOut(); Descriptor standard_out = getStandardOutput();
writeFull(standard_out, s); writeFull(standard_out, s);
writeFull(standard_out, "\n"); writeFull(standard_out, "\n");
} }
@ -118,11 +118,7 @@ void writeToStderr(std::string_view s)
{ {
try { try {
writeFull( writeFull(
#ifdef _WIN32 getStandardError(),
GetStdHandle(STD_ERROR_HANDLE),
#else
STDERR_FILENO,
#endif
s, false); s, false);
} catch (SystemError & e) { } catch (SystemError & e) {
/* Ignore failing writes to stderr. We need to ignore write /* Ignore failing writes to stderr. We need to ignore write

View file

@ -61,7 +61,7 @@ void writeFull(HANDLE handle, std::string_view s, bool allowInterrupts)
} }
std::string readLine(HANDLE handle) std::string readLine(HANDLE handle, bool eofOk)
{ {
std::string s; std::string s;
while (1) { while (1) {
@ -71,8 +71,12 @@ std::string readLine(HANDLE handle)
DWORD rd; DWORD rd;
if (!ReadFile(handle, &ch, 1, &rd, NULL)) { if (!ReadFile(handle, &ch, 1, &rd, NULL)) {
throw WinError("reading a line"); throw WinError("reading a line");
} else if (rd == 0) } else if (rd == 0) {
if (eofOk)
return s;
else
throw EndOfFile("unexpected EOF reading a line"); throw EndOfFile("unexpected EOF reading a line");
}
else { else {
if (ch == '\n') return s; if (ch == '\n') return s;
s += ch; s += ch;

View file

@ -694,7 +694,7 @@ static void opDump(Strings opFlags, Strings opArgs)
if (!opFlags.empty()) throw UsageError("unknown flag"); if (!opFlags.empty()) throw UsageError("unknown flag");
if (opArgs.size() != 1) throw UsageError("only one argument allowed"); if (opArgs.size() != 1) throw UsageError("only one argument allowed");
FdSink sink(getStandardOut()); FdSink sink(getStandardOutput());
std::string path = *opArgs.begin(); std::string path = *opArgs.begin();
dumpPath(path, sink); dumpPath(path, sink);
sink.flush(); sink.flush();
@ -722,7 +722,7 @@ static void opExport(Strings opFlags, Strings opArgs)
for (auto & i : opArgs) for (auto & i : opArgs)
paths.insert(store->followLinksToStorePath(i)); paths.insert(store->followLinksToStorePath(i));
FdSink sink(getStandardOut()); FdSink sink(getStandardOutput());
store->exportPaths(paths, sink); store->exportPaths(paths, sink);
sink.flush(); sink.flush();
} }
@ -835,7 +835,7 @@ static void opServe(Strings opFlags, Strings opArgs)
if (!opArgs.empty()) throw UsageError("no arguments expected"); if (!opArgs.empty()) throw UsageError("no arguments expected");
FdSource in(STDIN_FILENO); FdSource in(STDIN_FILENO);
FdSink out(getStandardOut()); FdSink out(getStandardOutput());
/* Exchange the greeting. */ /* Exchange the greeting. */
ServeProto::Version clientVersion = ServeProto::Version clientVersion =

View file

@ -16,7 +16,7 @@ struct MixCat : virtual Args
throw Error("path '%1%' is not a regular file", path); throw Error("path '%1%' is not a regular file", path);
stopProgressBar(); stopProgressBar();
writeFull(getStandardOut(), accessor->readFile(CanonPath(path))); writeFull(getStandardOutput(), accessor->readFile(CanonPath(path)));
} }
}; };

View file

@ -20,7 +20,7 @@ struct CmdDumpPath : StorePathCommand
void run(ref<Store> store, const StorePath & storePath) override void run(ref<Store> store, const StorePath & storePath) override
{ {
FdSink sink(getStandardOut()); FdSink sink(getStandardOutput());
store->narFromPath(storePath, sink); store->narFromPath(storePath, sink);
sink.flush(); sink.flush();
} }
@ -55,7 +55,7 @@ struct CmdDumpPath2 : Command
void run() override void run() override
{ {
FdSink sink(getStandardOut()); FdSink sink(getStandardOutput());
dumpPath(path, sink); dumpPath(path, sink);
sink.flush(); sink.flush();
} }

View file

@ -115,7 +115,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
else if (raw) { else if (raw) {
stopProgressBar(); stopProgressBar();
writeFull(getStandardOut(), *state->coerceToString(noPos, *v, context, "while generating the eval command output")); writeFull(getStandardOutput(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
} }
else if (json) { else if (json) {

View file

@ -57,7 +57,7 @@ struct CmdLog : InstallableCommand
if (!log) continue; if (!log) continue;
stopProgressBar(); stopProgressBar();
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri()); printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
writeFull(getStandardOut(), *log); writeFull(getStandardOutput(), *log);
return; return;
} }

View file

@ -177,7 +177,7 @@ struct CmdKeyGenerateSecret : Command
throw UsageError("required argument '--key-name' is missing"); throw UsageError("required argument '--key-name' is missing");
stopProgressBar(); stopProgressBar();
writeFull(getStandardOut(), SecretKey::generate(*keyName).to_string()); writeFull(getStandardOutput(), SecretKey::generate(*keyName).to_string());
} }
}; };
@ -199,7 +199,7 @@ struct CmdKeyConvertSecretToPublic : Command
{ {
SecretKey secretKey(drainFD(STDIN_FILENO)); SecretKey secretKey(drainFD(STDIN_FILENO));
stopProgressBar(); stopProgressBar();
writeFull(getStandardOut(), secretKey.toPublicKey().to_string()); writeFull(getStandardOutput(), secretKey.toPublicKey().to_string());
} }
}; };