Skip to content

Commit 9ac13ec

Browse files
Nicolas PitreJunio C Hamano
Nicolas Pitre
authored and
Junio C Hamano
committed
atomic write for sideband remote messages
It has been a few times that I ended up with such a confusing display: |remote: Generating pack... |remote: Done counting 17 objects. |remote: Result has 9 objects. |remote: Deltifying 9 objects. |remote: 100% (9/9) done |remote: Unpacking 9 objects |Total 9, written 9 (delta 8), reused 0 (delta 0) | 100% (9/9) done The confusion can be avoided in most cases by writing the remote message in one go to prevent interleacing with local messages. The buffer declaration has been moved inside recv_sideband() to avoid extra string copies. Signed-off-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0503f9c commit 9ac13ec

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

builtin-archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int run_remote_archiver(const char *remote, int argc,
7575
die("git-archive: expected a flush");
7676

7777
/* Now, start reading from fd[0] and spit it out to stdout */
78-
rv = recv_sideband("archive", fd[0], 1, 2, buf, sizeof(buf));
78+
rv = recv_sideband("archive", fd[0], 1, 2);
7979
close(fd[0]);
8080
rv |= finish_connect(pid);
8181

fetch-clone.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
115115
die("%s: unable to fork off sideband demultiplexer", me);
116116
if (!side_pid) {
117117
/* subprocess */
118-
char buf[LARGE_PACKET_MAX];
119-
120118
close(fd[0]);
121119
if (xd[0] != xd[1])
122120
close(xd[1]);
123-
if (recv_sideband(me, xd[0], fd[1], 2, buf, sizeof(buf)))
121+
if (recv_sideband(me, xd[0], fd[1], 2))
124122
exit(1);
125123
exit(0);
126124
}

sideband.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,40 @@
1111
* stream, aka "verbose"). A message over band #3 is a signal that
1212
* the remote died unexpectedly. A flush() concludes the stream.
1313
*/
14-
int recv_sideband(const char *me, int in_stream, int out, int err, char *buf, int bufsz)
14+
int recv_sideband(const char *me, int in_stream, int out, int err)
1515
{
16+
char buf[7 + LARGE_PACKET_MAX + 1];
17+
strcpy(buf, "remote:");
1618
while (1) {
17-
int len = packet_read_line(in_stream, buf, bufsz);
19+
int band, len;
20+
len = packet_read_line(in_stream, buf+7, LARGE_PACKET_MAX);
1821
if (len == 0)
1922
break;
2023
if (len < 1) {
2124
len = sprintf(buf, "%s: protocol error: no band designator\n", me);
2225
safe_write(err, buf, len);
2326
return SIDEBAND_PROTOCOL_ERROR;
2427
}
28+
band = buf[7] & 0xff;
2529
len--;
26-
switch (buf[0] & 0xFF) {
30+
switch (band) {
2731
case 3:
28-
safe_write(err, "remote: ", 8);
29-
safe_write(err, buf+1, len);
30-
safe_write(err, "\n", 1);
32+
buf[7] = ' ';
33+
buf[8+len] = '\n';
34+
safe_write(err, buf, 8+len+1);
3135
return SIDEBAND_REMOTE_ERROR;
3236
case 2:
33-
safe_write(err, "remote: ", 8);
34-
safe_write(err, buf+1, len);
37+
buf[7] = ' ';
38+
safe_write(err, buf, 8+len);
3539
continue;
3640
case 1:
37-
safe_write(out, buf+1, len);
41+
safe_write(out, buf+8, len);
3842
continue;
3943
default:
40-
len = sprintf(buf + 1,
44+
len = sprintf(buf,
4145
"%s: protocol error: bad band #%d\n",
42-
me, buf[0] & 0xFF);
43-
safe_write(err, buf+1, len);
46+
me, band);
47+
safe_write(err, buf, len);
4448
return SIDEBAND_PROTOCOL_ERROR;
4549
}
4650
}

sideband.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define DEFAULT_PACKET_MAX 1000
88
#define LARGE_PACKET_MAX 65520
99

10-
int recv_sideband(const char *me, int in_stream, int out, int err, char *, int);
10+
int recv_sideband(const char *me, int in_stream, int out, int err);
1111
ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max);
1212

1313
#endif

0 commit comments

Comments
 (0)