upload-pack: avoid sending an incomplete pack upon failure
When the repository on the remote side is corrupted, rev-list spawned from upload-pack would die with error, but pack-objects that reads from the rev-list happily created a packfile that can be unpacked by the downloader. When this happens, the resulting packfile is not corrupted and unpacks cleanly, but the list of the objects contained in it is not what the protocol exchange computed. This update makes upload-pack to monitor its subprocesses, and when either of them dies with error, sends an incomplete pack data to the downloader to cause it to fail. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jun 20, 2006 at 18:26 UTC
b1c71b72815cb82a8bad14020a047320b88a04eb
1 file changed
+163
-14
upload-pack.c
+163
-14
@@ -5,6 +5,9 @@
5
#include "object.h"
6
#include "commit.h"
7
#include "exec_cmd.h"
8
+#include <signal.h>
9
+#include <sys/poll.h>
10
+#include <sys/wait.h>
11
12
static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
13
@@ -33,17 +36,20 @@ static int strip(char *line, int len)
36
37
static void create_pack_file(void)
38
{
36
- int fd[2];
37
- pid_t pid;
39
+ /* Pipes between rev-list to pack-objects and pack-objects to us. */
40
+ int lp_pipe[2], pu_pipe[2];
41
+ pid_t pid_rev_list, pid_pack_objects;
42
int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
43
+ char data[8193];
44
+ int buffered = -1;
45
40
- if (pipe(fd) < 0)
46
+ if (pipe(lp_pipe) < 0)
47
die("git-upload-pack: unable to create pipe");
42
- pid = fork();
43
- if (pid < 0)
48
+ pid_rev_list = fork();
49
+ if (pid_rev_list < 0)
50
die("git-upload-pack: unable to fork git-rev-list");
51
46
- if (!pid) {
52
+ if (!pid_rev_list) {
53
int i;
54
int args;
55
const char **argv;
@@ -60,10 +66,10 @@ static void create_pack_file(void)
66
argv = (const char **) p;
67
buf = xmalloc(args * 45);
68
63
- dup2(fd[1], 1);
69
+ dup2(lp_pipe[1], 1);
70
close(0);
65
- close(fd[0]);
66
- close(fd[1]);
71
+ close(lp_pipe[0]);
72
+ close(lp_pipe[1]);
73
*p++ = "rev-list";
74
*p++ = use_thin_pack ? "--objects-edge" : "--objects";
75
if (create_full_pack || MAX_NEEDS <= nr_needs)
@@ -86,11 +92,154 @@ static void create_pack_file(void)
92
execv_git_cmd(argv);
93
die("git-upload-pack: unable to exec git-rev-list");
94
}
89
- dup2(fd[0], 0);
90
- close(fd[0]);
91
- close(fd[1]);
92
- execl_git_cmd("pack-objects", "--stdout", NULL);
93
- die("git-upload-pack: unable to exec git-pack-objects");
95
+
96
+ if (pipe(pu_pipe) < 0)
97
+ die("git-upload-pack: unable to create pipe");
98
+ pid_pack_objects = fork();
99
+ if (pid_pack_objects < 0) {
100
+ /* daemon sets things up to ignore TERM */
101
+ kill(pid_rev_list, SIGKILL);
102
+ die("git-upload-pack: unable to fork git-pack-objects");
103
+ }
104
+ if (!pid_pack_objects) {
105
+ dup2(lp_pipe[0], 0);
106
+ dup2(pu_pipe[1], 1);
107
+
108
+ close(lp_pipe[0]);
109
+ close(lp_pipe[1]);
110
+ close(pu_pipe[0]);
111
+ close(pu_pipe[1]);
112
+ execl_git_cmd("pack-objects", "--stdout", NULL);
113
+ kill(pid_rev_list, SIGKILL);
114
+ die("git-upload-pack: unable to exec git-pack-objects");
115
+ }
116
+
117
+ close(lp_pipe[0]);
118
+ close(lp_pipe[1]);
119
+
120
+ /* We read from pu_pipe[0] to capture the pack data.
121
+ */
122
+ close(pu_pipe[1]);
123
+
124
+ while (1) {
125
+ const char *who;
126
+ struct pollfd pfd[2];
127
+ pid_t pid;
128
+ int status;
129
+ ssize_t sz;
130
+ int pu, pollsize;
131
+
132
+ pollsize = 0;
133
+ pu = -1;
134
+
135
+ if (0 <= pu_pipe[0]) {
136
+ pfd[pollsize].fd = pu_pipe[0];
137
+ pfd[pollsize].events = POLLIN;
138
+ pu = pollsize;
139
+ pollsize++;
140
+ }
141
+
142
+ if (pollsize) {
143
+ if (poll(pfd, pollsize, -1) < 0) {
144
+ if (errno != EINTR) {
145
+ error("poll failed, resuming: %s",
146
+ strerror(errno));
147
+ sleep(1);
148
+ }
149
+ continue;
150
+ }
151
+ if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
152
+ /* Data ready; we keep the last byte
153
+ * to ourselves in case we detect
154
+ * broken rev-list, so that we can
155
+ * leave the stream corrupted. This
156
+ * is unfortunate -- unpack-objects
157
+ * would happily accept a valid pack
158
+ * data with trailing garbage, so
159
+ * appending garbage after we pass all
160
+ * the pack data is not good enough to
161
+ * signal breakage to downstream.
162
+ */
163
+ char *cp = data;
164
+ ssize_t outsz = 0;
165
+ if (0 <= buffered) {
166
+ *cp++ = buffered;
167
+ outsz++;
168
+ }
169
+ sz = read(pu_pipe[0], cp,
170
+ sizeof(data) - outsz);
171
+ if (0 < sz)
172
+ ;
173
+ else if (sz == 0) {
174
+ close(pu_pipe[0]);
175
+ pu_pipe[0] = -1;
176
+ }
177
+ else
178
+ goto fail;
179
+ sz += outsz;
180
+ if (1 < sz) {
181
+ buffered = data[sz-1] & 0xFF;
182
+ sz--;
183
+ }
184
+ else
185
+ buffered = -1;
186
+ sz = xwrite(1, data, sz);
187
+ if (sz < 0)
188
+ goto fail;
189
+ }
190
+ }
191
+
192
+ /* See if the children are still there */
193
+ if (pid_rev_list || pid_pack_objects) {
194
+ pid = waitpid(-1, &status, WNOHANG);
195
+ if (!pid)
196
+ continue;
197
+ who = ((pid == pid_rev_list) ? "git-rev-list" :
198
+ (pid == pid_pack_objects) ? "git-pack-objects" :
199
+ NULL);
200
+ if (!who) {
201
+ if (pid < 0) {
202
+ error("git-upload-pack: %s",
203
+ strerror(errno));
204
+ goto fail;
205
+ }
206
+ error("git-upload-pack: we weren't "
207
+ "waiting for %d", pid);
208
+ continue;
209
+ }
210
+ if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
211
+ error("git-upload-pack: %s died with error.",
212
+ who);
213
+ goto fail;
214
+ }
215
+ if (pid == pid_rev_list)
216
+ pid_rev_list = 0;
217
+ if (pid == pid_pack_objects)
218
+ pid_pack_objects = 0;
219
+ if (pid_rev_list || pid_pack_objects)
220
+ continue;
221
+ }
222
+
223
+ /* both died happily */
224
+ if (pollsize)
225
+ continue;
226
+
227
+ /* flush the data */
228
+ if (0 <= buffered) {
229
+ data[0] = buffered;
230
+ sz = xwrite(1, data, 1);
231
+ if (sz < 0)
232
+ goto fail;
233
+ fprintf(stderr, "flushed.\n");
234
+ }
235
+ return;
236
+ }
237
+ fail:
238
+ if (pid_pack_objects)
239
+ kill(pid_pack_objects, SIGKILL);
240
+ if (pid_rev_list)
241
+ kill(pid_rev_list, SIGKILL);
242
+ die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
243
}
244
245
static int got_sha1(char *hex, unsigned char *sha1)