Move sideband server side support into reusable form.
The server side support; this is just the very low level, and the caller needs to know which band it wants to send things out. Signed-off-by: Junio C Hamano <junkio@cox.net> (cherry picked from b786552b67878c7780c50def4c069d46dc54efbe commit)
Junio C Hamano committed
Sep 10, 2006 at 03:20 UTC
958c24b1b8f463bca857f45c41a2f8198e345c2f
3 files changed
+40
-37
sideband.c
+26
@@ -46,3 +46,29 @@ int recv_sideband(const char *me, int in_stream, int out, int err, char *buf, in
46
}
47
return 0;
48
}
49
+
50
+/*
51
+ * fd is connected to the remote side; send the sideband data
52
+ * over multiplexed packet stream.
53
+ */
54
+ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max)
55
+{
56
+ ssize_t ssz = sz;
57
+ const char *p = data;
58
+
59
+ while (sz) {
60
+ unsigned n;
61
+ char hdr[5];
62
+
63
+ n = sz;
64
+ if (packet_max - 5 < n)
65
+ n = packet_max - 5;
66
+ sprintf(hdr, "%04x", n + 5);
67
+ hdr[4] = band;
68
+ safe_write(fd, hdr, 5);
69
+ safe_write(fd, p, n);
70
+ p += n;
71
+ sz -= n;
72
+ }
73
+ return ssz;
74
+}
sideband.h
+1
@@ -7,5 +7,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
+ssize_t send_sideband(int fd, int band, const char *data, ssize_t sz, int packet_max);
11
12
#endif
upload-pack.c
+13
-37
@@ -4,6 +4,7 @@
4
#include "cache.h"
5
#include "refs.h"
6
#include "pkt-line.h"
7
+#include "sideband.h"
8
#include "tag.h"
9
#include "object.h"
10
#include "commit.h"
@@ -33,45 +34,19 @@ static int strip(char *line, int len)
34
return len;
35
}
36
36
-#define PACKET_MAX 1000
37
static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
38
{
39
- ssize_t ssz;
40
- const char *p;
41
-
42
- if (!data) {
43
- if (!use_sideband)
44
- return 0;
45
- packet_flush(1);
46
- }
47
-
48
- if (!use_sideband) {
49
- if (fd == 3)
50
- /* emergency quit */
51
- fd = 2;
52
- if (fd == 2) {
53
- xwrite(fd, data, sz);
54
- return sz;
55
- }
56
- return safe_write(fd, data, sz);
57
- }
58
- p = data;
59
- ssz = sz;
60
- while (sz) {
61
- unsigned n;
62
- char hdr[5];
63
-
64
- n = sz;
65
- if (PACKET_MAX - 5 < n)
66
- n = PACKET_MAX - 5;
67
- sprintf(hdr, "%04x", n + 5);
68
- hdr[4] = fd;
69
- safe_write(1, hdr, 5);
70
- safe_write(1, p, n);
71
- p += n;
72
- sz -= n;
39
+ if (use_sideband)
40
+ return send_sideband(1, fd, data, sz, DEFAULT_PACKET_MAX);
41
+
42
+ if (fd == 3)
43
+ /* emergency quit */
44
+ fd = 2;
45
+ if (fd == 2) {
46
+ xwrite(fd, data, sz);
47
+ return sz;
48
}
74
- return ssz;
49
+ return safe_write(fd, data, sz);
50
}
51
52
static void create_pack_file(void)
@@ -308,7 +283,8 @@ static void create_pack_file(void)
283
goto fail;
284
fprintf(stderr, "flushed.\n");
285
}
311
- send_client_data(1, NULL, 0);
286
+ if (use_sideband)
287
+ packet_flush(1);
288
return;
289
}
290
fail: