Check for IO errors after running a command
This is trying to implement the strict IO error checks that Jim Meyering suggested, but explicitly limits it to just regular files. If a pipe gets closed on us, we shouldn't complain about it. If the subcommand already returned an error, that takes precedence (and we assume that the subcommand already printed out any relevant messages relating to it) Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Linus Torvalds committed
Jun 24, 2007 at 10:29 UTC
0f157315a1020fce76fe2c5a703e40684b9b1699
1 file changed
+20
-1
git.c
+20
-1
@@ -224,6 +224,8 @@ struct cmd_struct {
224
225
static int run_command(struct cmd_struct *p, int argc, const char **argv)
226
{
227
+ int status;
228
+ struct stat st;
229
const char *prefix;
230
231
prefix = NULL;
@@ -237,7 +239,24 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
239
}
240
trace_argv_printf(argv, argc, "trace: built-in: git");
241
240
- return p->fn(argc, argv, prefix);
242
+ status = p->fn(argc, argv, prefix);
243
+ if (status)
244
+ return status;
245
+
246
+ /* Somebody closed stdout? */
247
+ if (fstat(fileno(stdout), &st))
248
+ return 0;
249
+ /* Ignore write errors for pipes and sockets.. */
250
+ if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
251
+ return 0;
252
+
253
+ /* Check for ENOSPC and EIO errors.. */
254
+ if (ferror(stdout))
255
+ die("write failure on standard output");
256
+ if (fflush(stdout) || fclose(stdout))
257
+ die("write failure on standard output: %s", strerror(errno));
258
+
259
+ return 0;
260
}
261
262
static void handle_internal_command(int argc, const char **argv)