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