]>
Commit | Line | Data |
---|---|---|
6eb996b5 DB |
1 | #include "cache.h" |
2 | #include "transport.h" | |
ae4efe19 | 3 | #include "quote.h" |
6eb996b5 DB |
4 | #include "run-command.h" |
5 | #include "commit.h" | |
6 | #include "diff.h" | |
7 | #include "revision.h" | |
ef08ef9e | 8 | #include "quote.h" |
72ff8943 | 9 | #include "remote.h" |
73b49a75 | 10 | #include "string-list.h" |
7851b1e6 | 11 | #include "thread-utils.h" |
7851b1e6 | 12 | |
bf3c523c IL |
13 | static int debug; |
14 | ||
9cba13ca | 15 | struct helper_data { |
6eb996b5 DB |
16 | const char *name; |
17 | struct child_process *helper; | |
292ce46b | 18 | FILE *out; |
ef08ef9e | 19 | unsigned fetch : 1, |
a24a32dd | 20 | import : 1, |
73b49a75 | 21 | export : 1, |
ae4efe19 | 22 | option : 1, |
fa8c097c IL |
23 | push : 1, |
24 | connect : 1, | |
25 | no_disconnect_req : 1; | |
a515ebe9 SR |
26 | char *export_marks; |
27 | char *import_marks; | |
72ff8943 DB |
28 | /* These go from remote name (as in "list") to private name */ |
29 | struct refspec *refspecs; | |
30 | int refspec_nr; | |
61b075bd IL |
31 | /* Transport options for fetch-pack/send-pack (should one of |
32 | * those be invoked). | |
33 | */ | |
34 | struct git_transport_options transport_options; | |
6eb996b5 DB |
35 | }; |
36 | ||
bf3c523c IL |
37 | static void sendline(struct helper_data *helper, struct strbuf *buffer) |
38 | { | |
39 | if (debug) | |
40 | fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf); | |
41 | if (write_in_full(helper->helper->in, buffer->buf, buffer->len) | |
42 | != buffer->len) | |
43 | die_errno("Full write to remote helper failed"); | |
44 | } | |
45 | ||
fa8c097c | 46 | static int recvline_fh(FILE *helper, struct strbuf *buffer) |
bf3c523c IL |
47 | { |
48 | strbuf_reset(buffer); | |
49 | if (debug) | |
50 | fprintf(stderr, "Debug: Remote helper: Waiting...\n"); | |
fa8c097c | 51 | if (strbuf_getline(buffer, helper, '\n') == EOF) { |
bf3c523c IL |
52 | if (debug) |
53 | fprintf(stderr, "Debug: Remote helper quit.\n"); | |
54 | exit(128); | |
55 | } | |
56 | ||
57 | if (debug) | |
58 | fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf); | |
59 | return 0; | |
60 | } | |
61 | ||
fa8c097c IL |
62 | static int recvline(struct helper_data *helper, struct strbuf *buffer) |
63 | { | |
64 | return recvline_fh(helper->out, buffer); | |
65 | } | |
66 | ||
bf3c523c IL |
67 | static void xchgline(struct helper_data *helper, struct strbuf *buffer) |
68 | { | |
69 | sendline(helper, buffer); | |
70 | recvline(helper, buffer); | |
71 | } | |
72 | ||
73 | static void write_constant(int fd, const char *str) | |
74 | { | |
75 | if (debug) | |
76 | fprintf(stderr, "Debug: Remote helper: -> %s", str); | |
77 | if (write_in_full(fd, str, strlen(str)) != strlen(str)) | |
78 | die_errno("Full write to remote helper failed"); | |
79 | } | |
80 | ||
c2e86add | 81 | static const char *remove_ext_force(const char *url) |
25d5cc48 IL |
82 | { |
83 | if (url) { | |
84 | const char *colon = strchr(url, ':'); | |
85 | if (colon && colon[1] == ':') | |
86 | return colon + 2; | |
87 | } | |
88 | return url; | |
89 | } | |
90 | ||
fa8c097c IL |
91 | static void do_take_over(struct transport *transport) |
92 | { | |
93 | struct helper_data *data; | |
94 | data = (struct helper_data *)transport->data; | |
95 | transport_take_over(transport, data->helper); | |
96 | fclose(data->out); | |
97 | free(data); | |
98 | } | |
99 | ||
6eb996b5 DB |
100 | static struct child_process *get_helper(struct transport *transport) |
101 | { | |
102 | struct helper_data *data = transport->data; | |
103 | struct strbuf buf = STRBUF_INIT; | |
104 | struct child_process *helper; | |
72ff8943 DB |
105 | const char **refspecs = NULL; |
106 | int refspec_nr = 0; | |
107 | int refspec_alloc = 0; | |
61b075bd | 108 | int duped; |
6b02de3b | 109 | int code; |
e1735872 DI |
110 | char git_dir_buf[sizeof(GIT_DIR_ENVIRONMENT) + PATH_MAX + 1]; |
111 | const char *helper_env[] = { | |
112 | git_dir_buf, | |
113 | NULL | |
114 | }; | |
115 | ||
6eb996b5 DB |
116 | |
117 | if (data->helper) | |
118 | return data->helper; | |
119 | ||
120 | helper = xcalloc(1, sizeof(*helper)); | |
121 | helper->in = -1; | |
122 | helper->out = -1; | |
123 | helper->err = 0; | |
124 | helper->argv = xcalloc(4, sizeof(*helper->argv)); | |
6b02de3b | 125 | strbuf_addf(&buf, "git-remote-%s", data->name); |
6eb996b5 DB |
126 | helper->argv[0] = strbuf_detach(&buf, NULL); |
127 | helper->argv[1] = transport->remote->name; | |
25d5cc48 | 128 | helper->argv[2] = remove_ext_force(transport->url); |
6b02de3b IL |
129 | helper->git_cmd = 0; |
130 | helper->silent_exec_failure = 1; | |
e1735872 DI |
131 | |
132 | snprintf(git_dir_buf, sizeof(git_dir_buf), "%s=%s", GIT_DIR_ENVIRONMENT, get_git_dir()); | |
133 | helper->env = helper_env; | |
134 | ||
6b02de3b IL |
135 | code = start_command(helper); |
136 | if (code < 0 && errno == ENOENT) | |
137 | die("Unable to find remote helper for '%s'", data->name); | |
138 | else if (code != 0) | |
139 | exit(code); | |
140 | ||
6eb996b5 | 141 | data->helper = helper; |
fa8c097c | 142 | data->no_disconnect_req = 0; |
6eb996b5 | 143 | |
61b075bd IL |
144 | /* |
145 | * Open the output as FILE* so strbuf_getline() can be used. | |
146 | * Do this with duped fd because fclose() will close the fd, | |
147 | * and stuff like taking over will require the fd to remain. | |
61b075bd IL |
148 | */ |
149 | duped = dup(helper->out); | |
150 | if (duped < 0) | |
151 | die_errno("Can't dup helper output fd"); | |
152 | data->out = xfdopen(duped, "r"); | |
153 | ||
bf3c523c | 154 | write_constant(helper->in, "capabilities\n"); |
2d14d65c | 155 | |
6eb996b5 | 156 | while (1) { |
28ed5b35 IL |
157 | const char *capname; |
158 | int mandatory = 0; | |
bf3c523c | 159 | recvline(data, &buf); |
6eb996b5 DB |
160 | |
161 | if (!*buf.buf) | |
162 | break; | |
28ed5b35 IL |
163 | |
164 | if (*buf.buf == '*') { | |
165 | capname = buf.buf + 1; | |
166 | mandatory = 1; | |
167 | } else | |
168 | capname = buf.buf; | |
169 | ||
bf3c523c | 170 | if (debug) |
28ed5b35 IL |
171 | fprintf(stderr, "Debug: Got cap %s\n", capname); |
172 | if (!strcmp(capname, "fetch")) | |
6eb996b5 | 173 | data->fetch = 1; |
28ed5b35 | 174 | else if (!strcmp(capname, "option")) |
ef08ef9e | 175 | data->option = 1; |
28ed5b35 | 176 | else if (!strcmp(capname, "push")) |
ae4efe19 | 177 | data->push = 1; |
28ed5b35 | 178 | else if (!strcmp(capname, "import")) |
e65e91ed | 179 | data->import = 1; |
73b49a75 SR |
180 | else if (!strcmp(capname, "export")) |
181 | data->export = 1; | |
28ed5b35 | 182 | else if (!data->refspecs && !prefixcmp(capname, "refspec ")) { |
72ff8943 DB |
183 | ALLOC_GROW(refspecs, |
184 | refspec_nr + 1, | |
185 | refspec_alloc); | |
c28cce55 | 186 | refspecs[refspec_nr++] = xstrdup(capname + strlen("refspec ")); |
fa8c097c IL |
187 | } else if (!strcmp(capname, "connect")) { |
188 | data->connect = 1; | |
a515ebe9 SR |
189 | } else if (!prefixcmp(capname, "export-marks ")) { |
190 | struct strbuf arg = STRBUF_INIT; | |
191 | strbuf_addstr(&arg, "--export-marks="); | |
192 | strbuf_addstr(&arg, capname + strlen("export-marks ")); | |
193 | data->export_marks = strbuf_detach(&arg, NULL); | |
194 | } else if (!prefixcmp(capname, "import-marks")) { | |
195 | struct strbuf arg = STRBUF_INIT; | |
196 | strbuf_addstr(&arg, "--import-marks="); | |
197 | strbuf_addstr(&arg, capname + strlen("import-marks ")); | |
198 | data->import_marks = strbuf_detach(&arg, NULL); | |
28ed5b35 | 199 | } else if (mandatory) { |
9517e6b8 | 200 | die("Unknown mandatory capability %s. This remote " |
28ed5b35 IL |
201 | "helper probably needs newer version of Git.\n", |
202 | capname); | |
72ff8943 DB |
203 | } |
204 | } | |
205 | if (refspecs) { | |
206 | int i; | |
207 | data->refspec_nr = refspec_nr; | |
208 | data->refspecs = parse_fetch_refspec(refspec_nr, refspecs); | |
209 | for (i = 0; i < refspec_nr; i++) { | |
210 | free((char *)refspecs[i]); | |
211 | } | |
212 | free(refspecs); | |
6eb996b5 | 213 | } |
b962dbdc | 214 | strbuf_release(&buf); |
bf3c523c IL |
215 | if (debug) |
216 | fprintf(stderr, "Debug: Capabilities complete.\n"); | |
6eb996b5 DB |
217 | return data->helper; |
218 | } | |
219 | ||
220 | static int disconnect_helper(struct transport *transport) | |
221 | { | |
222 | struct helper_data *data = transport->data; | |
bf3c523c | 223 | struct strbuf buf = STRBUF_INIT; |
cc567322 | 224 | int res = 0; |
bf3c523c | 225 | |
6eb996b5 | 226 | if (data->helper) { |
bf3c523c IL |
227 | if (debug) |
228 | fprintf(stderr, "Debug: Disconnecting.\n"); | |
fa8c097c IL |
229 | if (!data->no_disconnect_req) { |
230 | strbuf_addf(&buf, "\n"); | |
231 | sendline(data, &buf); | |
232 | } | |
6eb996b5 | 233 | close(data->helper->in); |
61b075bd | 234 | close(data->helper->out); |
292ce46b | 235 | fclose(data->out); |
cc567322 | 236 | res = finish_command(data->helper); |
6eb996b5 DB |
237 | free((char *)data->helper->argv[0]); |
238 | free(data->helper->argv); | |
239 | free(data->helper); | |
240 | data->helper = NULL; | |
241 | } | |
cc567322 | 242 | return res; |
6eb996b5 DB |
243 | } |
244 | ||
ef08ef9e SP |
245 | static const char *unsupported_options[] = { |
246 | TRANS_OPT_UPLOADPACK, | |
247 | TRANS_OPT_RECEIVEPACK, | |
248 | TRANS_OPT_THIN, | |
249 | TRANS_OPT_KEEP | |
250 | }; | |
251 | static const char *boolean_options[] = { | |
252 | TRANS_OPT_THIN, | |
253 | TRANS_OPT_KEEP, | |
254 | TRANS_OPT_FOLLOWTAGS | |
255 | }; | |
256 | ||
257 | static int set_helper_option(struct transport *transport, | |
258 | const char *name, const char *value) | |
259 | { | |
260 | struct helper_data *data = transport->data; | |
ef08ef9e SP |
261 | struct strbuf buf = STRBUF_INIT; |
262 | int i, ret, is_bool = 0; | |
263 | ||
bf3c523c IL |
264 | get_helper(transport); |
265 | ||
ef08ef9e SP |
266 | if (!data->option) |
267 | return 1; | |
268 | ||
269 | for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) { | |
270 | if (!strcmp(name, unsupported_options[i])) | |
271 | return 1; | |
272 | } | |
273 | ||
274 | for (i = 0; i < ARRAY_SIZE(boolean_options); i++) { | |
275 | if (!strcmp(name, boolean_options[i])) { | |
276 | is_bool = 1; | |
277 | break; | |
278 | } | |
279 | } | |
280 | ||
281 | strbuf_addf(&buf, "option %s ", name); | |
282 | if (is_bool) | |
283 | strbuf_addstr(&buf, value ? "true" : "false"); | |
284 | else | |
285 | quote_c_style(value, &buf, NULL, 0); | |
286 | strbuf_addch(&buf, '\n'); | |
287 | ||
bf3c523c | 288 | xchgline(data, &buf); |
ef08ef9e SP |
289 | |
290 | if (!strcmp(buf.buf, "ok")) | |
291 | ret = 0; | |
292 | else if (!prefixcmp(buf.buf, "error")) { | |
293 | ret = -1; | |
294 | } else if (!strcmp(buf.buf, "unsupported")) | |
295 | ret = 1; | |
296 | else { | |
297 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
298 | ret = 1; | |
299 | } | |
300 | strbuf_release(&buf); | |
301 | return ret; | |
302 | } | |
303 | ||
304 | static void standard_options(struct transport *t) | |
305 | { | |
306 | char buf[16]; | |
307 | int n; | |
308 | int v = t->verbose; | |
ef08ef9e | 309 | |
d01b3c02 | 310 | set_helper_option(t, "progress", t->progress ? "true" : "false"); |
ef08ef9e SP |
311 | |
312 | n = snprintf(buf, sizeof(buf), "%d", v + 1); | |
313 | if (n >= sizeof(buf)) | |
314 | die("impossibly large verbosity value"); | |
315 | set_helper_option(t, "verbosity", buf); | |
316 | } | |
317 | ||
f2a37151 DB |
318 | static int release_helper(struct transport *transport) |
319 | { | |
cc567322 | 320 | int res = 0; |
72ff8943 DB |
321 | struct helper_data *data = transport->data; |
322 | free_refspec(data->refspec_nr, data->refspecs); | |
323 | data->refspecs = NULL; | |
cc567322 | 324 | res = disconnect_helper(transport); |
f2a37151 | 325 | free(transport->data); |
cc567322 | 326 | return res; |
f2a37151 DB |
327 | } |
328 | ||
6eb996b5 | 329 | static int fetch_with_fetch(struct transport *transport, |
37148311 | 330 | int nr_heads, struct ref **to_fetch) |
6eb996b5 | 331 | { |
292ce46b | 332 | struct helper_data *data = transport->data; |
6eb996b5 DB |
333 | int i; |
334 | struct strbuf buf = STRBUF_INIT; | |
335 | ||
ef08ef9e SP |
336 | standard_options(transport); |
337 | ||
6eb996b5 | 338 | for (i = 0; i < nr_heads; i++) { |
1088261f | 339 | const struct ref *posn = to_fetch[i]; |
6eb996b5 DB |
340 | if (posn->status & REF_STATUS_UPTODATE) |
341 | continue; | |
2d14d65c DB |
342 | |
343 | strbuf_addf(&buf, "fetch %s %s\n", | |
344 | sha1_to_hex(posn->old_sha1), posn->name); | |
292ce46b | 345 | } |
2d14d65c | 346 | |
292ce46b | 347 | strbuf_addch(&buf, '\n'); |
bf3c523c | 348 | sendline(data, &buf); |
292ce46b SP |
349 | |
350 | while (1) { | |
bf3c523c | 351 | recvline(data, &buf); |
292ce46b SP |
352 | |
353 | if (!prefixcmp(buf.buf, "lock ")) { | |
354 | const char *name = buf.buf + 5; | |
355 | if (transport->pack_lockfile) | |
356 | warning("%s also locked %s", data->name, name); | |
357 | else | |
358 | transport->pack_lockfile = xstrdup(name); | |
359 | } | |
360 | else if (!buf.len) | |
361 | break; | |
362 | else | |
363 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
6eb996b5 | 364 | } |
292ce46b | 365 | strbuf_release(&buf); |
6eb996b5 DB |
366 | return 0; |
367 | } | |
368 | ||
e65e91ed DB |
369 | static int get_importer(struct transport *transport, struct child_process *fastimport) |
370 | { | |
371 | struct child_process *helper = get_helper(transport); | |
372 | memset(fastimport, 0, sizeof(*fastimport)); | |
373 | fastimport->in = helper->out; | |
374 | fastimport->argv = xcalloc(5, sizeof(*fastimport->argv)); | |
375 | fastimport->argv[0] = "fast-import"; | |
376 | fastimport->argv[1] = "--quiet"; | |
377 | ||
378 | fastimport->git_cmd = 1; | |
379 | return start_command(fastimport); | |
380 | } | |
381 | ||
73b49a75 SR |
382 | static int get_exporter(struct transport *transport, |
383 | struct child_process *fastexport, | |
73b49a75 SR |
384 | struct string_list *revlist_args) |
385 | { | |
a515ebe9 | 386 | struct helper_data *data = transport->data; |
73b49a75 SR |
387 | struct child_process *helper = get_helper(transport); |
388 | int argc = 0, i; | |
389 | memset(fastexport, 0, sizeof(*fastexport)); | |
390 | ||
391 | /* we need to duplicate helper->in because we want to use it after | |
392 | * fastexport is done with it. */ | |
393 | fastexport->out = dup(helper->in); | |
1f25c504 | 394 | fastexport->argv = xcalloc(5 + revlist_args->nr, sizeof(*fastexport->argv)); |
73b49a75 | 395 | fastexport->argv[argc++] = "fast-export"; |
1f25c504 | 396 | fastexport->argv[argc++] = "--use-done-feature"; |
a515ebe9 SR |
397 | if (data->export_marks) |
398 | fastexport->argv[argc++] = data->export_marks; | |
399 | if (data->import_marks) | |
400 | fastexport->argv[argc++] = data->import_marks; | |
73b49a75 SR |
401 | |
402 | for (i = 0; i < revlist_args->nr; i++) | |
403 | fastexport->argv[argc++] = revlist_args->items[i].string; | |
404 | ||
405 | fastexport->git_cmd = 1; | |
406 | return start_command(fastexport); | |
407 | } | |
408 | ||
e65e91ed DB |
409 | static int fetch_with_import(struct transport *transport, |
410 | int nr_heads, struct ref **to_fetch) | |
411 | { | |
412 | struct child_process fastimport; | |
72ff8943 | 413 | struct helper_data *data = transport->data; |
e65e91ed DB |
414 | int i; |
415 | struct ref *posn; | |
416 | struct strbuf buf = STRBUF_INIT; | |
417 | ||
bf3c523c IL |
418 | get_helper(transport); |
419 | ||
e65e91ed DB |
420 | if (get_importer(transport, &fastimport)) |
421 | die("Couldn't run fast-import"); | |
422 | ||
423 | for (i = 0; i < nr_heads; i++) { | |
424 | posn = to_fetch[i]; | |
425 | if (posn->status & REF_STATUS_UPTODATE) | |
426 | continue; | |
427 | ||
428 | strbuf_addf(&buf, "import %s\n", posn->name); | |
bf3c523c | 429 | sendline(data, &buf); |
e65e91ed DB |
430 | strbuf_reset(&buf); |
431 | } | |
9504bc9d SR |
432 | |
433 | write_constant(data->helper->in, "\n"); | |
434 | ||
cc567322 SR |
435 | if (finish_command(&fastimport)) |
436 | die("Error while running fast-import"); | |
b962dbdc SR |
437 | free(fastimport.argv); |
438 | fastimport.argv = NULL; | |
e65e91ed DB |
439 | |
440 | for (i = 0; i < nr_heads; i++) { | |
72ff8943 | 441 | char *private; |
e65e91ed DB |
442 | posn = to_fetch[i]; |
443 | if (posn->status & REF_STATUS_UPTODATE) | |
444 | continue; | |
72ff8943 DB |
445 | if (data->refspecs) |
446 | private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name); | |
447 | else | |
c28cce55 | 448 | private = xstrdup(posn->name); |
d51b720f MH |
449 | if (private) { |
450 | read_ref(private, posn->old_sha1); | |
451 | free(private); | |
452 | } | |
e65e91ed | 453 | } |
b962dbdc | 454 | strbuf_release(&buf); |
e65e91ed DB |
455 | return 0; |
456 | } | |
457 | ||
fa8c097c IL |
458 | static int process_connect_service(struct transport *transport, |
459 | const char *name, const char *exec) | |
460 | { | |
461 | struct helper_data *data = transport->data; | |
462 | struct strbuf cmdbuf = STRBUF_INIT; | |
463 | struct child_process *helper; | |
464 | int r, duped, ret = 0; | |
465 | FILE *input; | |
466 | ||
467 | helper = get_helper(transport); | |
468 | ||
469 | /* | |
470 | * Yes, dup the pipe another time, as we need unbuffered version | |
471 | * of input pipe as FILE*. fclose() closes the underlying fd and | |
472 | * stream buffering only can be changed before first I/O operation | |
473 | * on it. | |
474 | */ | |
475 | duped = dup(helper->out); | |
476 | if (duped < 0) | |
477 | die_errno("Can't dup helper output fd"); | |
478 | input = xfdopen(duped, "r"); | |
479 | setvbuf(input, NULL, _IONBF, 0); | |
480 | ||
481 | /* | |
482 | * Handle --upload-pack and friends. This is fire and forget... | |
483 | * just warn if it fails. | |
484 | */ | |
485 | if (strcmp(name, exec)) { | |
486 | r = set_helper_option(transport, "servpath", exec); | |
487 | if (r > 0) | |
488 | warning("Setting remote service path not supported by protocol."); | |
489 | else if (r < 0) | |
490 | warning("Invalid remote service path."); | |
491 | } | |
492 | ||
493 | if (data->connect) | |
494 | strbuf_addf(&cmdbuf, "connect %s\n", name); | |
495 | else | |
496 | goto exit; | |
497 | ||
498 | sendline(data, &cmdbuf); | |
499 | recvline_fh(input, &cmdbuf); | |
500 | if (!strcmp(cmdbuf.buf, "")) { | |
501 | data->no_disconnect_req = 1; | |
502 | if (debug) | |
503 | fprintf(stderr, "Debug: Smart transport connection " | |
504 | "ready.\n"); | |
505 | ret = 1; | |
506 | } else if (!strcmp(cmdbuf.buf, "fallback")) { | |
507 | if (debug) | |
508 | fprintf(stderr, "Debug: Falling back to dumb " | |
509 | "transport.\n"); | |
510 | } else | |
511 | die("Unknown response to connect: %s", | |
512 | cmdbuf.buf); | |
513 | ||
514 | exit: | |
515 | fclose(input); | |
516 | return ret; | |
517 | } | |
518 | ||
519 | static int process_connect(struct transport *transport, | |
520 | int for_push) | |
521 | { | |
522 | struct helper_data *data = transport->data; | |
523 | const char *name; | |
524 | const char *exec; | |
525 | ||
526 | name = for_push ? "git-receive-pack" : "git-upload-pack"; | |
527 | if (for_push) | |
528 | exec = data->transport_options.receivepack; | |
529 | else | |
530 | exec = data->transport_options.uploadpack; | |
531 | ||
532 | return process_connect_service(transport, name, exec); | |
533 | } | |
534 | ||
b236752a IL |
535 | static int connect_helper(struct transport *transport, const char *name, |
536 | const char *exec, int fd[2]) | |
537 | { | |
538 | struct helper_data *data = transport->data; | |
539 | ||
540 | /* Get_helper so connect is inited. */ | |
541 | get_helper(transport); | |
542 | if (!data->connect) | |
543 | die("Operation not supported by protocol."); | |
544 | ||
545 | if (!process_connect_service(transport, name, exec)) | |
546 | die("Can't connect to subservice %s.", name); | |
547 | ||
548 | fd[0] = data->helper->out; | |
549 | fd[1] = data->helper->in; | |
550 | return 0; | |
551 | } | |
552 | ||
6eb996b5 | 553 | static int fetch(struct transport *transport, |
37148311 | 554 | int nr_heads, struct ref **to_fetch) |
6eb996b5 DB |
555 | { |
556 | struct helper_data *data = transport->data; | |
557 | int i, count; | |
558 | ||
fa8c097c IL |
559 | if (process_connect(transport, 0)) { |
560 | do_take_over(transport); | |
561 | return transport->fetch(transport, nr_heads, to_fetch); | |
562 | } | |
563 | ||
6eb996b5 DB |
564 | count = 0; |
565 | for (i = 0; i < nr_heads; i++) | |
566 | if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) | |
567 | count++; | |
568 | ||
569 | if (!count) | |
570 | return 0; | |
571 | ||
572 | if (data->fetch) | |
573 | return fetch_with_fetch(transport, nr_heads, to_fetch); | |
574 | ||
e65e91ed DB |
575 | if (data->import) |
576 | return fetch_with_import(transport, nr_heads, to_fetch); | |
577 | ||
6eb996b5 DB |
578 | return -1; |
579 | } | |
580 | ||
d2e73c6f SR |
581 | static void push_update_ref_status(struct strbuf *buf, |
582 | struct ref **ref, | |
583 | struct ref *remote_refs) | |
584 | { | |
585 | char *refname, *msg; | |
586 | int status; | |
587 | ||
588 | if (!prefixcmp(buf->buf, "ok ")) { | |
589 | status = REF_STATUS_OK; | |
590 | refname = buf->buf + 3; | |
591 | } else if (!prefixcmp(buf->buf, "error ")) { | |
592 | status = REF_STATUS_REMOTE_REJECT; | |
593 | refname = buf->buf + 6; | |
594 | } else | |
595 | die("expected ok/error, helper said '%s'\n", buf->buf); | |
596 | ||
597 | msg = strchr(refname, ' '); | |
598 | if (msg) { | |
599 | struct strbuf msg_buf = STRBUF_INIT; | |
600 | const char *end; | |
601 | ||
602 | *msg++ = '\0'; | |
603 | if (!unquote_c_style(&msg_buf, msg, &end)) | |
604 | msg = strbuf_detach(&msg_buf, NULL); | |
605 | else | |
606 | msg = xstrdup(msg); | |
607 | strbuf_release(&msg_buf); | |
608 | ||
609 | if (!strcmp(msg, "no match")) { | |
610 | status = REF_STATUS_NONE; | |
611 | free(msg); | |
612 | msg = NULL; | |
613 | } | |
614 | else if (!strcmp(msg, "up to date")) { | |
615 | status = REF_STATUS_UPTODATE; | |
616 | free(msg); | |
617 | msg = NULL; | |
618 | } | |
619 | else if (!strcmp(msg, "non-fast forward")) { | |
620 | status = REF_STATUS_REJECT_NONFASTFORWARD; | |
621 | free(msg); | |
622 | msg = NULL; | |
623 | } | |
624 | } | |
625 | ||
626 | if (*ref) | |
627 | *ref = find_ref_by_name(*ref, refname); | |
628 | if (!*ref) | |
629 | *ref = find_ref_by_name(remote_refs, refname); | |
630 | if (!*ref) { | |
631 | warning("helper reported unexpected status of %s", refname); | |
632 | return; | |
633 | } | |
634 | ||
635 | if ((*ref)->status != REF_STATUS_NONE) { | |
636 | /* | |
637 | * Earlier, the ref was marked not to be pushed, so ignore the ref | |
638 | * status reported by the remote helper if the latter is 'no match'. | |
639 | */ | |
640 | if (status == REF_STATUS_NONE) | |
641 | return; | |
642 | } | |
643 | ||
644 | (*ref)->status = status; | |
645 | (*ref)->remote_status = msg; | |
646 | } | |
647 | ||
648 | static void push_update_refs_status(struct helper_data *data, | |
649 | struct ref *remote_refs) | |
650 | { | |
651 | struct strbuf buf = STRBUF_INIT; | |
652 | struct ref *ref = remote_refs; | |
653 | for (;;) { | |
654 | recvline(data, &buf); | |
655 | if (!buf.len) | |
656 | break; | |
657 | ||
658 | push_update_ref_status(&buf, &ref, remote_refs); | |
659 | } | |
660 | strbuf_release(&buf); | |
661 | } | |
662 | ||
73b49a75 | 663 | static int push_refs_with_push(struct transport *transport, |
ae4efe19 SP |
664 | struct ref *remote_refs, int flags) |
665 | { | |
666 | int force_all = flags & TRANSPORT_PUSH_FORCE; | |
667 | int mirror = flags & TRANSPORT_PUSH_MIRROR; | |
668 | struct helper_data *data = transport->data; | |
669 | struct strbuf buf = STRBUF_INIT; | |
ae4efe19 SP |
670 | struct ref *ref; |
671 | ||
c0aa335c | 672 | get_helper(transport); |
ae4efe19 SP |
673 | if (!data->push) |
674 | return 1; | |
675 | ||
676 | for (ref = remote_refs; ref; ref = ref->next) { | |
20e8b465 | 677 | if (!ref->peer_ref && !mirror) |
ae4efe19 SP |
678 | continue; |
679 | ||
20e8b465 TRC |
680 | /* Check for statuses set by set_ref_status_for_push() */ |
681 | switch (ref->status) { | |
682 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
683 | case REF_STATUS_UPTODATE: | |
ae4efe19 | 684 | continue; |
20e8b465 TRC |
685 | default: |
686 | ; /* do nothing */ | |
ae4efe19 SP |
687 | } |
688 | ||
689 | if (force_all) | |
690 | ref->force = 1; | |
691 | ||
692 | strbuf_addstr(&buf, "push "); | |
693 | if (!ref->deletion) { | |
694 | if (ref->force) | |
695 | strbuf_addch(&buf, '+'); | |
696 | if (ref->peer_ref) | |
697 | strbuf_addstr(&buf, ref->peer_ref->name); | |
698 | else | |
699 | strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1)); | |
700 | } | |
701 | strbuf_addch(&buf, ':'); | |
702 | strbuf_addstr(&buf, ref->name); | |
703 | strbuf_addch(&buf, '\n'); | |
704 | } | |
d8f67d20 CB |
705 | if (buf.len == 0) |
706 | return 0; | |
ae4efe19 | 707 | |
ae4efe19 SP |
708 | standard_options(transport); |
709 | ||
710 | if (flags & TRANSPORT_PUSH_DRY_RUN) { | |
711 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
712 | die("helper %s does not support dry-run", data->name); | |
713 | } | |
714 | ||
715 | strbuf_addch(&buf, '\n'); | |
bf3c523c | 716 | sendline(data, &buf); |
ae4efe19 | 717 | strbuf_release(&buf); |
d2e73c6f SR |
718 | |
719 | push_update_refs_status(data, remote_refs); | |
ae4efe19 SP |
720 | return 0; |
721 | } | |
722 | ||
73b49a75 SR |
723 | static int push_refs_with_export(struct transport *transport, |
724 | struct ref *remote_refs, int flags) | |
725 | { | |
726 | struct ref *ref; | |
727 | struct child_process *helper, exporter; | |
728 | struct helper_data *data = transport->data; | |
183113a5 | 729 | struct string_list revlist_args = STRING_LIST_INIT_NODUP; |
73b49a75 SR |
730 | struct strbuf buf = STRBUF_INIT; |
731 | ||
732 | helper = get_helper(transport); | |
733 | ||
734 | write_constant(helper->in, "export\n"); | |
735 | ||
73b49a75 SR |
736 | strbuf_reset(&buf); |
737 | ||
738 | for (ref = remote_refs; ref; ref = ref->next) { | |
739 | char *private; | |
740 | unsigned char sha1[20]; | |
741 | ||
742 | if (!data->refspecs) | |
743 | continue; | |
744 | private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name); | |
745 | if (private && !get_sha1(private, sha1)) { | |
746 | strbuf_addf(&buf, "^%s", private); | |
1d2f80fa | 747 | string_list_append(&revlist_args, strbuf_detach(&buf, NULL)); |
73b49a75 | 748 | } |
2faa1527 | 749 | free(private); |
73b49a75 | 750 | |
105fe3e4 SR |
751 | if (ref->deletion) { |
752 | die("remote-helpers do not support ref deletion"); | |
753 | } | |
754 | ||
3ea7d094 JK |
755 | if (ref->peer_ref) |
756 | string_list_append(&revlist_args, ref->peer_ref->name); | |
73b49a75 SR |
757 | |
758 | } | |
759 | ||
a515ebe9 | 760 | if (get_exporter(transport, &exporter, &revlist_args)) |
73b49a75 SR |
761 | die("Couldn't run fast-export"); |
762 | ||
cc567322 SR |
763 | if (finish_command(&exporter)) |
764 | die("Error while running fast-export"); | |
6c8151a3 | 765 | push_update_refs_status(data, remote_refs); |
73b49a75 SR |
766 | return 0; |
767 | } | |
768 | ||
769 | static int push_refs(struct transport *transport, | |
770 | struct ref *remote_refs, int flags) | |
771 | { | |
772 | struct helper_data *data = transport->data; | |
773 | ||
774 | if (process_connect(transport, 1)) { | |
775 | do_take_over(transport); | |
776 | return transport->push_refs(transport, remote_refs, flags); | |
777 | } | |
778 | ||
779 | if (!remote_refs) { | |
780 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" | |
781 | "Perhaps you should specify a branch such as 'master'.\n"); | |
782 | return 0; | |
783 | } | |
784 | ||
785 | if (data->push) | |
786 | return push_refs_with_push(transport, remote_refs, flags); | |
787 | ||
788 | if (data->export) | |
789 | return push_refs_with_export(transport, remote_refs, flags); | |
790 | ||
791 | return -1; | |
792 | } | |
793 | ||
794 | ||
f8ec9167 DB |
795 | static int has_attribute(const char *attrs, const char *attr) { |
796 | int len; | |
797 | if (!attrs) | |
798 | return 0; | |
799 | ||
800 | len = strlen(attr); | |
801 | for (;;) { | |
802 | const char *space = strchrnul(attrs, ' '); | |
803 | if (len == space - attrs && !strncmp(attrs, attr, len)) | |
804 | return 1; | |
805 | if (!*space) | |
806 | return 0; | |
807 | attrs = space + 1; | |
808 | } | |
809 | } | |
810 | ||
6eb996b5 DB |
811 | static struct ref *get_refs_list(struct transport *transport, int for_push) |
812 | { | |
292ce46b | 813 | struct helper_data *data = transport->data; |
6eb996b5 DB |
814 | struct child_process *helper; |
815 | struct ref *ret = NULL; | |
816 | struct ref **tail = &ret; | |
817 | struct ref *posn; | |
818 | struct strbuf buf = STRBUF_INIT; | |
6eb996b5 DB |
819 | |
820 | helper = get_helper(transport); | |
2d14d65c | 821 | |
fa8c097c IL |
822 | if (process_connect(transport, for_push)) { |
823 | do_take_over(transport); | |
824 | return transport->get_refs_list(transport, for_push); | |
825 | } | |
826 | ||
ae4efe19 SP |
827 | if (data->push && for_push) |
828 | write_str_in_full(helper->in, "list for-push\n"); | |
829 | else | |
830 | write_str_in_full(helper->in, "list\n"); | |
6eb996b5 | 831 | |
6eb996b5 DB |
832 | while (1) { |
833 | char *eov, *eon; | |
bf3c523c | 834 | recvline(data, &buf); |
6eb996b5 DB |
835 | |
836 | if (!*buf.buf) | |
837 | break; | |
838 | ||
839 | eov = strchr(buf.buf, ' '); | |
840 | if (!eov) | |
841 | die("Malformed response in ref list: %s", buf.buf); | |
842 | eon = strchr(eov + 1, ' '); | |
843 | *eov = '\0'; | |
844 | if (eon) | |
845 | *eon = '\0'; | |
846 | *tail = alloc_ref(eov + 1); | |
847 | if (buf.buf[0] == '@') | |
848 | (*tail)->symref = xstrdup(buf.buf + 1); | |
849 | else if (buf.buf[0] != '?') | |
850 | get_sha1_hex(buf.buf, (*tail)->old_sha1); | |
f8ec9167 DB |
851 | if (eon) { |
852 | if (has_attribute(eon + 1, "unchanged")) { | |
853 | (*tail)->status |= REF_STATUS_UPTODATE; | |
854 | read_ref((*tail)->name, (*tail)->old_sha1); | |
855 | } | |
856 | } | |
6eb996b5 DB |
857 | tail = &((*tail)->next); |
858 | } | |
bf3c523c IL |
859 | if (debug) |
860 | fprintf(stderr, "Debug: Read ref listing.\n"); | |
6eb996b5 DB |
861 | strbuf_release(&buf); |
862 | ||
863 | for (posn = ret; posn; posn = posn->next) | |
864 | resolve_remote_symref(posn, ret); | |
865 | ||
866 | return ret; | |
867 | } | |
868 | ||
c9e388bb | 869 | int transport_helper_init(struct transport *transport, const char *name) |
6eb996b5 DB |
870 | { |
871 | struct helper_data *data = xcalloc(sizeof(*data), 1); | |
c9e388bb | 872 | data->name = name; |
6eb996b5 | 873 | |
bf3c523c IL |
874 | if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) |
875 | debug = 1; | |
876 | ||
6eb996b5 | 877 | transport->data = data; |
ef08ef9e | 878 | transport->set_option = set_helper_option; |
6eb996b5 DB |
879 | transport->get_refs_list = get_refs_list; |
880 | transport->fetch = fetch; | |
ae4efe19 | 881 | transport->push_refs = push_refs; |
f2a37151 | 882 | transport->disconnect = release_helper; |
b236752a | 883 | transport->connect = connect_helper; |
61b075bd | 884 | transport->smart_options = &(data->transport_options); |
6eb996b5 DB |
885 | return 0; |
886 | } | |
419f37db IL |
887 | |
888 | /* | |
889 | * Linux pipes can buffer 65536 bytes at once (and most platforms can | |
890 | * buffer less), so attempt reads and writes with up to that size. | |
891 | */ | |
892 | #define BUFFERSIZE 65536 | |
893 | /* This should be enough to hold debugging message. */ | |
894 | #define PBUFFERSIZE 8192 | |
895 | ||
896 | /* Print bidirectional transfer loop debug message. */ | |
897 | static void transfer_debug(const char *fmt, ...) | |
898 | { | |
899 | va_list args; | |
900 | char msgbuf[PBUFFERSIZE]; | |
901 | static int debug_enabled = -1; | |
902 | ||
903 | if (debug_enabled < 0) | |
904 | debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0; | |
905 | if (!debug_enabled) | |
906 | return; | |
907 | ||
908 | va_start(args, fmt); | |
909 | vsnprintf(msgbuf, PBUFFERSIZE, fmt, args); | |
910 | va_end(args); | |
911 | fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf); | |
912 | } | |
913 | ||
914 | /* Stream state: More data may be coming in this direction. */ | |
915 | #define SSTATE_TRANSFERING 0 | |
916 | /* | |
917 | * Stream state: No more data coming in this direction, flushing rest of | |
918 | * data. | |
919 | */ | |
920 | #define SSTATE_FLUSHING 1 | |
921 | /* Stream state: Transfer in this direction finished. */ | |
922 | #define SSTATE_FINISHED 2 | |
923 | ||
924 | #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING) | |
925 | #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING) | |
926 | #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING) | |
927 | ||
928 | /* Unidirectional transfer. */ | |
929 | struct unidirectional_transfer { | |
930 | /* Source */ | |
931 | int src; | |
932 | /* Destination */ | |
933 | int dest; | |
934 | /* Is source socket? */ | |
935 | int src_is_sock; | |
936 | /* Is destination socket? */ | |
937 | int dest_is_sock; | |
938 | /* Transfer state (TRANSFERING/FLUSHING/FINISHED) */ | |
939 | int state; | |
940 | /* Buffer. */ | |
941 | char buf[BUFFERSIZE]; | |
942 | /* Buffer used. */ | |
943 | size_t bufuse; | |
944 | /* Name of source. */ | |
945 | const char *src_name; | |
946 | /* Name of destination. */ | |
947 | const char *dest_name; | |
948 | }; | |
949 | ||
950 | /* Closes the target (for writing) if transfer has finished. */ | |
951 | static void udt_close_if_finished(struct unidirectional_transfer *t) | |
952 | { | |
953 | if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) { | |
954 | t->state = SSTATE_FINISHED; | |
955 | if (t->dest_is_sock) | |
956 | shutdown(t->dest, SHUT_WR); | |
957 | else | |
958 | close(t->dest); | |
959 | transfer_debug("Closed %s.", t->dest_name); | |
960 | } | |
961 | } | |
962 | ||
963 | /* | |
964 | * Tries to read read data from source into buffer. If buffer is full, | |
965 | * no data is read. Returns 0 on success, -1 on error. | |
966 | */ | |
967 | static int udt_do_read(struct unidirectional_transfer *t) | |
968 | { | |
969 | ssize_t bytes; | |
970 | ||
971 | if (t->bufuse == BUFFERSIZE) | |
972 | return 0; /* No space for more. */ | |
973 | ||
974 | transfer_debug("%s is readable", t->src_name); | |
975 | bytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse); | |
976 | if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN && | |
977 | errno != EINTR) { | |
978 | error("read(%s) failed: %s", t->src_name, strerror(errno)); | |
979 | return -1; | |
980 | } else if (bytes == 0) { | |
981 | transfer_debug("%s EOF (with %i bytes in buffer)", | |
982 | t->src_name, t->bufuse); | |
983 | t->state = SSTATE_FLUSHING; | |
984 | } else if (bytes > 0) { | |
985 | t->bufuse += bytes; | |
986 | transfer_debug("Read %i bytes from %s (buffer now at %i)", | |
987 | (int)bytes, t->src_name, (int)t->bufuse); | |
988 | } | |
989 | return 0; | |
990 | } | |
991 | ||
992 | /* Tries to write data from buffer into destination. If buffer is empty, | |
993 | * no data is written. Returns 0 on success, -1 on error. | |
994 | */ | |
995 | static int udt_do_write(struct unidirectional_transfer *t) | |
996 | { | |
803dbdb9 | 997 | ssize_t bytes; |
419f37db IL |
998 | |
999 | if (t->bufuse == 0) | |
1000 | return 0; /* Nothing to write. */ | |
1001 | ||
1002 | transfer_debug("%s is writable", t->dest_name); | |
1003 | bytes = write(t->dest, t->buf, t->bufuse); | |
1004 | if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN && | |
1005 | errno != EINTR) { | |
1006 | error("write(%s) failed: %s", t->dest_name, strerror(errno)); | |
1007 | return -1; | |
1008 | } else if (bytes > 0) { | |
1009 | t->bufuse -= bytes; | |
1010 | if (t->bufuse) | |
1011 | memmove(t->buf, t->buf + bytes, t->bufuse); | |
1012 | transfer_debug("Wrote %i bytes to %s (buffer now at %i)", | |
1013 | (int)bytes, t->dest_name, (int)t->bufuse); | |
1014 | } | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | ||
1019 | /* State of bidirectional transfer loop. */ | |
1020 | struct bidirectional_transfer_state { | |
1021 | /* Direction from program to git. */ | |
1022 | struct unidirectional_transfer ptg; | |
1023 | /* Direction from git to program. */ | |
1024 | struct unidirectional_transfer gtp; | |
1025 | }; | |
1026 | ||
1027 | static void *udt_copy_task_routine(void *udt) | |
1028 | { | |
1029 | struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt; | |
1030 | while (t->state != SSTATE_FINISHED) { | |
1031 | if (STATE_NEEDS_READING(t->state)) | |
1032 | if (udt_do_read(t)) | |
1033 | return NULL; | |
1034 | if (STATE_NEEDS_WRITING(t->state)) | |
1035 | if (udt_do_write(t)) | |
1036 | return NULL; | |
1037 | if (STATE_NEEDS_CLOSING(t->state)) | |
1038 | udt_close_if_finished(t); | |
1039 | } | |
1040 | return udt; /* Just some non-NULL value. */ | |
1041 | } | |
1042 | ||
1043 | #ifndef NO_PTHREADS | |
1044 | ||
1045 | /* | |
1046 | * Join thread, with apporiate errors on failure. Name is name for the | |
1047 | * thread (for error messages). Returns 0 on success, 1 on failure. | |
1048 | */ | |
1049 | static int tloop_join(pthread_t thread, const char *name) | |
1050 | { | |
1051 | int err; | |
1052 | void *tret; | |
1053 | err = pthread_join(thread, &tret); | |
1054 | if (!tret) { | |
1055 | error("%s thread failed", name); | |
1056 | return 1; | |
1057 | } | |
1058 | if (err) { | |
1059 | error("%s thread failed to join: %s", name, strerror(err)); | |
1060 | return 1; | |
1061 | } | |
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1067 | * -1 on failure. | |
1068 | */ | |
1069 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1070 | { | |
1071 | pthread_t gtp_thread; | |
1072 | pthread_t ptg_thread; | |
1073 | int err; | |
1074 | int ret = 0; | |
1075 | err = pthread_create(>p_thread, NULL, udt_copy_task_routine, | |
1076 | &s->gtp); | |
1077 | if (err) | |
1078 | die("Can't start thread for copying data: %s", strerror(err)); | |
1079 | err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine, | |
1080 | &s->ptg); | |
1081 | if (err) | |
1082 | die("Can't start thread for copying data: %s", strerror(err)); | |
1083 | ||
1084 | ret |= tloop_join(gtp_thread, "Git to program copy"); | |
1085 | ret |= tloop_join(ptg_thread, "Program to git copy"); | |
1086 | return ret; | |
1087 | } | |
1088 | #else | |
1089 | ||
1090 | /* Close the source and target (for writing) for transfer. */ | |
1091 | static void udt_kill_transfer(struct unidirectional_transfer *t) | |
1092 | { | |
1093 | t->state = SSTATE_FINISHED; | |
1094 | /* | |
1095 | * Socket read end left open isn't a disaster if nobody | |
1096 | * attempts to read from it (mingw compat headers do not | |
1097 | * have SHUT_RD)... | |
1098 | * | |
1099 | * We can't fully close the socket since otherwise gtp | |
1100 | * task would first close the socket it sends data to | |
1101 | * while closing the ptg file descriptors. | |
1102 | */ | |
1103 | if (!t->src_is_sock) | |
1104 | close(t->src); | |
1105 | if (t->dest_is_sock) | |
1106 | shutdown(t->dest, SHUT_WR); | |
1107 | else | |
1108 | close(t->dest); | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * Join process, with apporiate errors on failure. Name is name for the | |
1113 | * process (for error messages). Returns 0 on success, 1 on failure. | |
1114 | */ | |
1115 | static int tloop_join(pid_t pid, const char *name) | |
1116 | { | |
1117 | int tret; | |
1118 | if (waitpid(pid, &tret, 0) < 0) { | |
1119 | error("%s process failed to wait: %s", name, strerror(errno)); | |
1120 | return 1; | |
1121 | } | |
1122 | if (!WIFEXITED(tret) || WEXITSTATUS(tret)) { | |
1123 | error("%s process failed", name); | |
1124 | return 1; | |
1125 | } | |
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1131 | * -1 on failure. | |
1132 | */ | |
1133 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1134 | { | |
1135 | pid_t pid1, pid2; | |
1136 | int ret = 0; | |
1137 | ||
1138 | /* Fork thread #1: git to program. */ | |
1139 | pid1 = fork(); | |
1140 | if (pid1 < 0) | |
1141 | die_errno("Can't start thread for copying data"); | |
1142 | else if (pid1 == 0) { | |
1143 | udt_kill_transfer(&s->ptg); | |
1144 | exit(udt_copy_task_routine(&s->gtp) ? 0 : 1); | |
1145 | } | |
1146 | ||
1147 | /* Fork thread #2: program to git. */ | |
1148 | pid2 = fork(); | |
1149 | if (pid2 < 0) | |
1150 | die_errno("Can't start thread for copying data"); | |
1151 | else if (pid2 == 0) { | |
1152 | udt_kill_transfer(&s->gtp); | |
1153 | exit(udt_copy_task_routine(&s->ptg) ? 0 : 1); | |
1154 | } | |
1155 | ||
1156 | /* | |
1157 | * Close both streams in parent as to not interfere with | |
1158 | * end of file detection and wait for both tasks to finish. | |
1159 | */ | |
1160 | udt_kill_transfer(&s->gtp); | |
1161 | udt_kill_transfer(&s->ptg); | |
1162 | ret |= tloop_join(pid1, "Git to program copy"); | |
1163 | ret |= tloop_join(pid2, "Program to git copy"); | |
1164 | return ret; | |
1165 | } | |
1166 | #endif | |
1167 | ||
1168 | /* | |
1169 | * Copies data from stdin to output and from input to stdout simultaneously. | |
1170 | * Additionally filtering through given filter. If filter is NULL, uses | |
1171 | * identity filter. | |
1172 | */ | |
1173 | int bidirectional_transfer_loop(int input, int output) | |
1174 | { | |
1175 | struct bidirectional_transfer_state state; | |
1176 | ||
1177 | /* Fill the state fields. */ | |
1178 | state.ptg.src = input; | |
1179 | state.ptg.dest = 1; | |
1180 | state.ptg.src_is_sock = (input == output); | |
1181 | state.ptg.dest_is_sock = 0; | |
1182 | state.ptg.state = SSTATE_TRANSFERING; | |
1183 | state.ptg.bufuse = 0; | |
1184 | state.ptg.src_name = "remote input"; | |
1185 | state.ptg.dest_name = "stdout"; | |
1186 | ||
1187 | state.gtp.src = 0; | |
1188 | state.gtp.dest = output; | |
1189 | state.gtp.src_is_sock = 0; | |
1190 | state.gtp.dest_is_sock = (input == output); | |
1191 | state.gtp.state = SSTATE_TRANSFERING; | |
1192 | state.gtp.bufuse = 0; | |
1193 | state.gtp.src_name = "stdin"; | |
1194 | state.gtp.dest_name = "remote output"; | |
1195 | ||
1196 | return tloop_spawnwait_tasks(&state); | |
1197 | } |