run_processes_parallel: treat output of children as byte array
We do not want the output to be interrupted by a NUL byte, so we cannot use raw fputs. Introduce strbuf_write to avoid having long arguments in run-command.c. Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Feb 29, 2016 at 18:07 UTC
2dac9b5637abe580e8b8ea36e00dbbf28ec99af6
3 files changed
+16
-4
run-command.c
+4
-4
@@ -994,7 +994,7 @@ static void pp_cleanup(struct parallel_processes *pp)
994
* When get_next_task added messages to the buffer in its last
995
* iteration, the buffered output is non empty.
996
*/
997
- fputs(pp->buffered_output.buf, stderr);
997
+ strbuf_write(&pp->buffered_output, stderr);
998
strbuf_release(&pp->buffered_output);
999
1000
sigchain_pop_common();
@@ -1079,7 +1079,7 @@ static void pp_output(struct parallel_processes *pp)
1079
int i = pp->output_owner;
1080
if (pp->children[i].state == GIT_CP_WORKING &&
1081
pp->children[i].err.len) {
1082
- fputs(pp->children[i].err.buf, stderr);
1082
+ strbuf_write(&pp->children[i].err, stderr);
1083
strbuf_reset(&pp->children[i].err);
1084
}
1085
}
@@ -1117,11 +1117,11 @@ static int pp_collect_finished(struct parallel_processes *pp)
1117
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1118
strbuf_reset(&pp->children[i].err);
1119
} else {
1120
- fputs(pp->children[i].err.buf, stderr);
1120
+ strbuf_write(&pp->children[i].err, stderr);
1121
strbuf_reset(&pp->children[i].err);
1122
1123
/* Output all other finished child processes */
1124
- fputs(pp->buffered_output.buf, stderr);
1124
+ strbuf_write(&pp->buffered_output, stderr);
1125
strbuf_reset(&pp->buffered_output);
1126
1127
/*
strbuf.c
+6
@@ -395,6 +395,12 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
395
return cnt;
396
}
397
398
+ssize_t strbuf_write(struct strbuf *sb, FILE *f)
399
+{
400
+ return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
401
+}
402
+
403
+
404
#define STRBUF_MAXLINK (2*PATH_MAX)
405
406
int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
strbuf.h
+6
@@ -386,6 +386,12 @@ extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint
386
*/
387
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
388
389
+/**
390
+ * Write the whole content of the strbuf to the stream not stopping at
391
+ * NUL bytes.
392
+ */
393
+extern ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
394
+
395
/**
396
* Read a line from a FILE *, overwriting the existing contents
397
* of the strbuf. The second argument specifies the line