Add sideband status report to git-archive protocol
Using the refactored sideband code from existing upload-pack protocol, this lets the error condition and status output sent from the remote process to be shown locally. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 10, 2006 at 03:33 UTC
23d6d112c004d4242f9dbd8161f79ccdeb47bde8
2 files changed
+92
-6
builtin-archive.c
+3
-3
@@ -10,6 +10,7 @@
10
#include "tree-walk.h"
11
#include "exec_cmd.h"
12
#include "pkt-line.h"
13
+#include "sideband.h"
14
15
static const char archive_usage[] = \
16
"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
@@ -29,7 +30,7 @@ struct archiver archivers[] = {
30
static int run_remote_archiver(const char *remote, int argc,
31
const char **argv)
32
{
32
- char *url, buf[1024];
33
+ char *url, buf[LARGE_PACKET_MAX];
34
int fd[2], i, len, rv;
35
pid_t pid;
36
const char *exec = "git-upload-archive";
@@ -74,8 +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 */
77
- rv = copy_fd(fd[0], 1);
78
-
78
+ rv = recv_sideband("archive", fd[0], 1, 2, buf, sizeof(buf));
79
close(fd[0]);
80
rv |= finish_connect(pid);
81
builtin-upload-archive.c
+89
-3
@@ -6,12 +6,18 @@
6
#include "builtin.h"
7
#include "archive.h"
8
#include "pkt-line.h"
9
+#include "sideband.h"
10
+#include <sys/wait.h>
11
+#include <sys/poll.h>
12
13
static const char upload_archive_usage[] =
14
"git-upload-archive <repo>";
15
16
+static const char deadchild[] =
17
+"git-upload-archive: archiver died with error";
18
14
-int cmd_upload_archive(int argc, const char **argv, const char *prefix)
19
+
20
+static int run_upload_archive(int argc, const char **argv, const char *prefix)
21
{
22
struct archiver ar;
23
const char *sent_argv[MAX_ARGS];
@@ -64,9 +70,89 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
70
parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
71
parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
72
73
+ return ar.write_archive(&ar.args);
74
+}
75
+
76
+int cmd_upload_archive(int argc, const char **argv, const char *prefix)
77
+{
78
+ pid_t writer;
79
+ int fd1[2], fd2[2];
80
+ /*
81
+ * Set up sideband subprocess.
82
+ *
83
+ * We (parent) monitor and read from child, sending its fd#1 and fd#2
84
+ * multiplexed out to our fd#1. If the child dies, we tell the other
85
+ * end over channel #3.
86
+ */
87
+ if (pipe(fd1) < 0 || pipe(fd2) < 0) {
88
+ int err = errno;
89
+ packet_write(1, "NACK pipe failed on the remote side\n");
90
+ die("upload-archive: %s", strerror(err));
91
+ }
92
+ writer = fork();
93
+ if (writer < 0) {
94
+ int err = errno;
95
+ packet_write(1, "NACK fork failed on the remote side\n");
96
+ die("upload-archive: %s", strerror(err));
97
+ }
98
+ if (!writer) {
99
+ /* child - connect fd#1 and fd#2 to the pipe */
100
+ dup2(fd1[1], 1);
101
+ dup2(fd2[1], 2);
102
+ close(fd1[1]); close(fd2[1]);
103
+ close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
104
+
105
+ exit(run_upload_archive(argc, argv, prefix));
106
+ }
107
+
108
+ /* parent - read from child, multiplex and send out to fd#1 */
109
+ close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
110
packet_write(1, "ACK\n");
111
packet_flush(1);
112
70
- return ar.write_archive(&ar.args);
71
-}
113
+ while (1) {
114
+ struct pollfd pfd[2];
115
+ char buf[16384];
116
+ ssize_t sz;
117
+ pid_t pid;
118
+ int status;
119
+
120
+ pfd[0].fd = fd1[0];
121
+ pfd[0].events = POLLIN;
122
+ pfd[1].fd = fd2[0];
123
+ pfd[1].events = POLLIN;
124
+ if (poll(pfd, 2, -1) < 0) {
125
+ if (errno != EINTR) {
126
+ error("poll failed resuming: %s",
127
+ strerror(errno));
128
+ sleep(1);
129
+ }
130
+ continue;
131
+ }
132
+ if (pfd[0].revents & (POLLIN|POLLHUP)) {
133
+ /* Data stream ready */
134
+ sz = read(pfd[0].fd, buf, sizeof(buf));
135
+ send_sideband(1, 1, buf, sz, LARGE_PACKET_MAX);
136
+ }
137
+ if (pfd[1].revents & (POLLIN|POLLHUP)) {
138
+ /* Status stream ready */
139
+ sz = read(pfd[1].fd, buf, sizeof(buf));
140
+ send_sideband(1, 2, buf, sz, LARGE_PACKET_MAX);
141
+ }
142
143
+ if (((pfd[0].revents | pfd[1].revents) & POLLHUP) == 0)
144
+ continue;
145
+ /* did it die? */
146
+ pid = waitpid(writer, &status, WNOHANG);
147
+ if (!pid) {
148
+ fprintf(stderr, "Hmph, HUP?\n");
149
+ continue;
150
+ }
151
+ if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
152
+ send_sideband(1, 3, deadchild, strlen(deadchild),
153
+ LARGE_PACKET_MAX);
154
+ packet_flush(1);
155
+ break;
156
+ }
157
+ return 0;
158
+}