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 <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Oct 11, 2006 at 11:49 UTC
9ac13ec941933c32849c2284b5d79ef608023a56
4 files changed
+19
-17
builtin-archive.c
+1
-1
@@ -75,7 +75,7 @@ static int run_remote_archiver(const char *remote, int argc,
75
die("git-archive: expected a flush");
76
77
/* 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);
79
close(fd[0]);
80
rv |= finish_connect(pid);
81
fetch-clone.c
+1
-3
@@ -115,12 +115,10 @@ static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
115
die("%s: unable to fork off sideband demultiplexer", me);
116
if (!side_pid) {
117
/* subprocess */
118
- char buf[LARGE_PACKET_MAX];
119
-
118
close(fd[0]);
119
if (xd[0] != xd[1])
120
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))
122
exit(1);
123
exit(0);
124
}
sideband.c
+16
-12
@@ -11,10 +11,13 @@
11
* stream, aka "verbose"). A message over band #3 is a signal that
12
* the remote died unexpectedly. A flush() concludes the stream.
13
*/
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)
15
{
16
+ char buf[7 + LARGE_PACKET_MAX + 1];
17
+ strcpy(buf, "remote:");
18
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);
21
if (len == 0)
22
break;
23
if (len < 1) {
@@ -22,25 +25,26 @@ int recv_sideband(const char *me, int in_stream, int out, int err, char *buf, in
25
safe_write(err, buf, len);
26
return SIDEBAND_PROTOCOL_ERROR;
27
}
28
+ band = buf[7] & 0xff;
29
len--;
26
- switch (buf[0] & 0xFF) {
30
+ switch (band) {
31
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);
35
return SIDEBAND_REMOTE_ERROR;
36
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);
39
continue;
40
case 1:
37
- safe_write(out, buf+1, len);
41
+ safe_write(out, buf+8, len);
42
continue;
43
default:
40
- len = sprintf(buf + 1,
44
+ len = sprintf(buf,
45
"%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);
48
return SIDEBAND_PROTOCOL_ERROR;
49
}
50
}
sideband.h
+1
-1
@@ -7,7 +7,7 @@
7
#define DEFAULT_PACKET_MAX 1000
8
#define LARGE_PACKET_MAX 65520
9
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);
11
ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max);
12
13
#endif