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());
if (!state->active) return {};
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 {};
draw(*state);
return s[0];

View file

@ -106,8 +106,25 @@ void drainFD(
#endif
);
/**
* Get [Standard Input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin))
*/
[[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
return STDOUT_FILENO;
#else
@ -115,6 +132,19 @@ inline Descriptor getStandardOut() {
#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.
*/

View file

@ -38,7 +38,7 @@ void Logger::warn(const std::string & msg)
void Logger::writeToStdout(std::string_view s)
{
Descriptor standard_out = getStandardOut();
Descriptor standard_out = getStandardOutput();
writeFull(standard_out, s);
writeFull(standard_out, "\n");
}
@ -118,11 +118,7 @@ void writeToStderr(std::string_view s)
{
try {
writeFull(
#ifdef _WIN32
GetStdHandle(STD_ERROR_HANDLE),
#else
STDERR_FILENO,
#endif
getStandardError(),
s, false);
} catch (SystemError & e) {
/* 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;
while (1) {
@ -71,8 +71,12 @@ std::string readLine(HANDLE handle)
DWORD rd;
if (!ReadFile(handle, &ch, 1, &rd, NULL)) {
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");
}
else {
if (ch == '\n') return s;
s += ch;

View file

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

View file

@ -16,7 +16,7 @@ struct MixCat : virtual Args
throw Error("path '%1%' is not a regular file", path);
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
{
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
store->narFromPath(storePath, sink);
sink.flush();
}
@ -55,7 +55,7 @@ struct CmdDumpPath2 : Command
void run() override
{
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
dumpPath(path, sink);
sink.flush();
}

View file

@ -115,7 +115,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
else if (raw) {
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) {

View file

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

View file

@ -177,7 +177,7 @@ struct CmdKeyGenerateSecret : Command
throw UsageError("required argument '--key-name' is missing");
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));
stopProgressBar();
writeFull(getStandardOut(), secretKey.toPublicKey().to_string());
writeFull(getStandardOutput(), secretKey.toPublicKey().to_string());
}
};