Move sideband client side support into reusable form.
This moves the receiver side of the sideband support from fetch-clone.c to sideband.c and its header file, so that archiver protocol can use it. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 10, 2006 at 01:06 UTC
49a52b1d1fb120f52de3a67f1e4e5ae81512ab81
4 files changed
+66
-29
Makefile
+2
-2
@@ -233,7 +233,7 @@ XDIFF_LIB=xdiff/lib.a
233
234
LIB_H = \
235
blob.h cache.h commit.h csum-file.h delta.h \
236
- diff.h object.h pack.h pkt-line.h quote.h refs.h \
236
+ diff.h object.h pack.h pkt-line.h quote.h refs.h sideband.h \
237
run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
238
tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h
239
@@ -245,7 +245,7 @@ DIFF_OBJS = \
245
LIB_OBJS = \
246
blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
247
date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
248
- object.o pack-check.o patch-delta.o path.o pkt-line.o \
248
+ object.o pack-check.o patch-delta.o path.o pkt-line.o sideband.o \
249
quote.o read-cache.o refs.o run-command.o dir.o object-refs.o \
250
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
251
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
fetch-clone.c
+5
-27
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "exec_cmd.h"
3
#include "pkt-line.h"
4
+#include "sideband.h"
5
#include <sys/wait.h>
6
#include <sys/time.h>
7
@@ -114,36 +115,13 @@ 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[DEFAULT_PACKET_MAX];
119
+
120
close(fd[0]);
121
if (xd[0] != xd[1])
122
close(xd[1]);
120
- while (1) {
121
- char buf[1024];
122
- int len = packet_read_line(xd[0], buf, sizeof(buf));
123
- if (len == 0)
124
- break;
125
- if (len < 1)
126
- die("%s: protocol error: no band designator",
127
- me);
128
- len--;
129
- switch (buf[0] & 0xFF) {
130
- case 3:
131
- safe_write(2, "remote: ", 8);
132
- safe_write(2, buf+1, len);
133
- safe_write(2, "\n", 1);
134
- exit(1);
135
- case 2:
136
- safe_write(2, "remote: ", 8);
137
- safe_write(2, buf+1, len);
138
- continue;
139
- case 1:
140
- safe_write(fd[1], buf+1, len);
141
- continue;
142
- default:
143
- die("%s: protocol error: bad band #%d",
144
- me, (buf[0] & 0xFF));
145
- }
146
- }
123
+ if (recv_sideband(me, xd[0], fd[1], 2, buf, sizeof(buf)))
124
+ exit(1);
125
exit(0);
126
}
127
close(xd[0]);
sideband.c
new
+48
@@ -0,0 +1,48 @@
1
+#include "pkt-line.h"
2
+#include "sideband.h"
3
+
4
+/*
5
+ * Receive multiplexed output stream over git native protocol.
6
+ * in_stream is the input stream from the remote, which carries data
7
+ * in pkt_line format with band designator. Demultiplex it into out
8
+ * and err and return error appropriately. Band #1 carries the
9
+ * primary payload. Things coming over band #2 is not necessarily
10
+ * error; they are usually informative message on the standard error
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)
15
+{
16
+ while (1) {
17
+ int len = packet_read_line(in_stream, buf, bufsz);
18
+ if (len == 0)
19
+ break;
20
+ if (len < 1) {
21
+ len = sprintf(buf, "%s: protocol error: no band designator\n", me);
22
+ safe_write(err, buf, len);
23
+ return SIDEBAND_PROTOCOL_ERROR;
24
+ }
25
+ len--;
26
+ switch (buf[0] & 0xFF) {
27
+ case 3:
28
+ safe_write(err, "remote: ", 8);
29
+ safe_write(err, buf+1, len);
30
+ safe_write(err, "\n", 1);
31
+ return SIDEBAND_REMOTE_ERROR;
32
+ case 2:
33
+ safe_write(err, "remote: ", 8);
34
+ safe_write(err, buf+1, len);
35
+ continue;
36
+ case 1:
37
+ safe_write(out, buf+1, len);
38
+ continue;
39
+ default:
40
+ len = sprintf(buf + 1,
41
+ "%s: protocol error: bad band #%d\n",
42
+ me, buf[0] & 0xFF);
43
+ safe_write(err, buf+1, len);
44
+ return SIDEBAND_PROTOCOL_ERROR;
45
+ }
46
+ }
47
+ return 0;
48
+}
sideband.h
new
+11
@@ -0,0 +1,11 @@
1
+#ifndef SIDEBAND_H
2
+#define SIDEBAND_H
3
+
4
+#define SIDEBAND_PROTOCOL_ERROR -2
5
+#define SIDEBAND_REMOTE_ERROR -1
6
+
7
+#define DEFAULT_PACKET_MAX 1000
8
+
9
+int recv_sideband(const char *me, int in_stream, int out, int err, char *, int);
10
+
11
+#endif