Error out on --eval if stdout write fails (#1444)

Since --eval only has one job - to actually *print* something to stdout,
we should fail completely if that's not possible due to an external
error, instead of just not printing anything.  One particular case is
redirecting stdout to a file on a file system that's full (generating
the ENOSPC error).

In order to be able to detect such errors, we need to flush the stream
buffer first; that's because normally, if stdout is not referring to an
interactive device (a terminal), it is block-buffered, as opposed to
line-buffered.
This commit is contained in:
Michal Domonkos 2021-03-01 12:45:10 +01:00 committed by Panu Matilainen
parent 9747a6af01
commit 95a83332ac
2 changed files with 15 additions and 0 deletions

View File

@ -133,6 +133,10 @@ static void rpmcliAllArgCallback( poptContext con,
if (rpmExpandMacros(NULL, arg, &val, 0) < 0)
exit(EXIT_FAILURE);
fprintf(stdout, "%s\n", val);
if (fflush(stdout) == EOF) {
perror(_("Error writing to stdout"));
exit(EXIT_FAILURE);
}
free(val);
}
break;

View File

@ -967,3 +967,14 @@ xxx
error: bad
])
AT_CLEANUP
AT_SETUP([no space left on stdout])
AT_KEYWORDS([macros])
AT_CHECK([
runroot rpm --eval 1 >/dev/full
],
[1],
[],
[Error writing to stdout: No space left on device
])
AT_CLEANUP