Git 2.23.3
[git] / transport-helper.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "quote.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "remote.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
11 #include "sigchain.h"
12 #include "argv-array.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "transport-internal.h"
16 #include "protocol.h"
17
18 static int debug;
19
20 struct helper_data {
21         const char *name;
22         struct child_process *helper;
23         FILE *out;
24         unsigned fetch : 1,
25                 import : 1,
26                 bidi_import : 1,
27                 export : 1,
28                 option : 1,
29                 push : 1,
30                 connect : 1,
31                 stateless_connect : 1,
32                 signed_tags : 1,
33                 check_connectivity : 1,
34                 no_disconnect_req : 1,
35                 no_private_update : 1;
36         char *export_marks;
37         char *import_marks;
38         /* These go from remote name (as in "list") to private name */
39         struct refspec rs;
40         /* Transport options for fetch-pack/send-pack (should one of
41          * those be invoked).
42          */
43         struct git_transport_options transport_options;
44 };
45
46 static void sendline(struct helper_data *helper, struct strbuf *buffer)
47 {
48         if (debug)
49                 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
50         if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
51                 die_errno(_("full write to remote helper failed"));
52 }
53
54 static int recvline_fh(FILE *helper, struct strbuf *buffer)
55 {
56         strbuf_reset(buffer);
57         if (debug)
58                 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
59         if (strbuf_getline(buffer, helper) == EOF) {
60                 if (debug)
61                         fprintf(stderr, "Debug: Remote helper quit.\n");
62                 return 1;
63         }
64
65         if (debug)
66                 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
67         return 0;
68 }
69
70 static int recvline(struct helper_data *helper, struct strbuf *buffer)
71 {
72         return recvline_fh(helper->out, buffer);
73 }
74
75 static void write_constant(int fd, const char *str)
76 {
77         if (debug)
78                 fprintf(stderr, "Debug: Remote helper: -> %s", str);
79         if (write_in_full(fd, str, strlen(str)) < 0)
80                 die_errno(_("full write to remote helper failed"));
81 }
82
83 static const char *remove_ext_force(const char *url)
84 {
85         if (url) {
86                 const char *colon = strchr(url, ':');
87                 if (colon && colon[1] == ':')
88                         return colon + 2;
89         }
90         return url;
91 }
92
93 static void do_take_over(struct transport *transport)
94 {
95         struct helper_data *data;
96         data = (struct helper_data *)transport->data;
97         transport_take_over(transport, data->helper);
98         fclose(data->out);
99         free(data);
100 }
101
102 static void standard_options(struct transport *t);
103
104 static struct child_process *get_helper(struct transport *transport)
105 {
106         struct helper_data *data = transport->data;
107         struct strbuf buf = STRBUF_INIT;
108         struct child_process *helper;
109         int duped;
110         int code;
111
112         if (data->helper)
113                 return data->helper;
114
115         helper = xmalloc(sizeof(*helper));
116         child_process_init(helper);
117         helper->in = -1;
118         helper->out = -1;
119         helper->err = 0;
120         argv_array_pushf(&helper->args, "git-remote-%s", data->name);
121         argv_array_push(&helper->args, transport->remote->name);
122         argv_array_push(&helper->args, remove_ext_force(transport->url));
123         helper->git_cmd = 0;
124         helper->silent_exec_failure = 1;
125
126         if (have_git_dir())
127                 argv_array_pushf(&helper->env_array, "%s=%s",
128                                  GIT_DIR_ENVIRONMENT, get_git_dir());
129
130         helper->trace2_child_class = helper->args.argv[0]; /* "remote-<name>" */
131
132         code = start_command(helper);
133         if (code < 0 && errno == ENOENT)
134                 die(_("unable to find remote helper for '%s'"), data->name);
135         else if (code != 0)
136                 exit(code);
137
138         data->helper = helper;
139         data->no_disconnect_req = 0;
140         refspec_init(&data->rs, REFSPEC_FETCH);
141
142         /*
143          * Open the output as FILE* so strbuf_getline_*() family of
144          * functions can be used.
145          * Do this with duped fd because fclose() will close the fd,
146          * and stuff like taking over will require the fd to remain.
147          */
148         duped = dup(helper->out);
149         if (duped < 0)
150                 die_errno(_("can't dup helper output fd"));
151         data->out = xfdopen(duped, "r");
152
153         write_constant(helper->in, "capabilities\n");
154
155         while (1) {
156                 const char *capname, *arg;
157                 int mandatory = 0;
158                 if (recvline(data, &buf))
159                         exit(128);
160
161                 if (!*buf.buf)
162                         break;
163
164                 if (*buf.buf == '*') {
165                         capname = buf.buf + 1;
166                         mandatory = 1;
167                 } else
168                         capname = buf.buf;
169
170                 if (debug)
171                         fprintf(stderr, "Debug: Got cap %s\n", capname);
172                 if (!strcmp(capname, "fetch"))
173                         data->fetch = 1;
174                 else if (!strcmp(capname, "option"))
175                         data->option = 1;
176                 else if (!strcmp(capname, "push"))
177                         data->push = 1;
178                 else if (!strcmp(capname, "import"))
179                         data->import = 1;
180                 else if (!strcmp(capname, "bidi-import"))
181                         data->bidi_import = 1;
182                 else if (!strcmp(capname, "export"))
183                         data->export = 1;
184                 else if (!strcmp(capname, "check-connectivity"))
185                         data->check_connectivity = 1;
186                 else if (skip_prefix(capname, "refspec ", &arg)) {
187                         refspec_append(&data->rs, arg);
188                 } else if (!strcmp(capname, "connect")) {
189                         data->connect = 1;
190                 } else if (!strcmp(capname, "stateless-connect")) {
191                         data->stateless_connect = 1;
192                 } else if (!strcmp(capname, "signed-tags")) {
193                         data->signed_tags = 1;
194                 } else if (skip_prefix(capname, "export-marks ", &arg)) {
195                         data->export_marks = xstrdup(arg);
196                 } else if (skip_prefix(capname, "import-marks ", &arg)) {
197                         data->import_marks = xstrdup(arg);
198                 } else if (starts_with(capname, "no-private-update")) {
199                         data->no_private_update = 1;
200                 } else if (mandatory) {
201                         die(_("unknown mandatory capability %s; this remote "
202                               "helper probably needs newer version of Git"),
203                             capname);
204                 }
205         }
206         if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
207                 warning(_("this remote helper should implement refspec capability"));
208         }
209         strbuf_release(&buf);
210         if (debug)
211                 fprintf(stderr, "Debug: Capabilities complete.\n");
212         standard_options(transport);
213         return data->helper;
214 }
215
216 static int disconnect_helper(struct transport *transport)
217 {
218         struct helper_data *data = transport->data;
219         int res = 0;
220
221         if (data->helper) {
222                 if (debug)
223                         fprintf(stderr, "Debug: Disconnecting.\n");
224                 if (!data->no_disconnect_req) {
225                         /*
226                          * Ignore write errors; there's nothing we can do,
227                          * since we're about to close the pipe anyway. And the
228                          * most likely error is EPIPE due to the helper dying
229                          * to report an error itself.
230                          */
231                         sigchain_push(SIGPIPE, SIG_IGN);
232                         xwrite(data->helper->in, "\n", 1);
233                         sigchain_pop(SIGPIPE);
234                 }
235                 close(data->helper->in);
236                 close(data->helper->out);
237                 fclose(data->out);
238                 res = finish_command(data->helper);
239                 FREE_AND_NULL(data->helper);
240         }
241         return res;
242 }
243
244 static const char *unsupported_options[] = {
245         TRANS_OPT_UPLOADPACK,
246         TRANS_OPT_RECEIVEPACK,
247         TRANS_OPT_THIN,
248         TRANS_OPT_KEEP
249         };
250
251 static const char *boolean_options[] = {
252         TRANS_OPT_THIN,
253         TRANS_OPT_KEEP,
254         TRANS_OPT_FOLLOWTAGS,
255         TRANS_OPT_DEEPEN_RELATIVE
256         };
257
258 static int strbuf_set_helper_option(struct helper_data *data,
259                                     struct strbuf *buf)
260 {
261         int ret;
262
263         sendline(data, buf);
264         if (recvline(data, buf))
265                 exit(128);
266
267         if (!strcmp(buf->buf, "ok"))
268                 ret = 0;
269         else if (starts_with(buf->buf, "error"))
270                 ret = -1;
271         else if (!strcmp(buf->buf, "unsupported"))
272                 ret = 1;
273         else {
274                 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
275                 ret = 1;
276         }
277         return ret;
278 }
279
280 static int string_list_set_helper_option(struct helper_data *data,
281                                          const char *name,
282                                          struct string_list *list)
283 {
284         struct strbuf buf = STRBUF_INIT;
285         int i, ret = 0;
286
287         for (i = 0; i < list->nr; i++) {
288                 strbuf_addf(&buf, "option %s ", name);
289                 quote_c_style(list->items[i].string, &buf, NULL, 0);
290                 strbuf_addch(&buf, '\n');
291
292                 if ((ret = strbuf_set_helper_option(data, &buf)))
293                         break;
294                 strbuf_reset(&buf);
295         }
296         strbuf_release(&buf);
297         return ret;
298 }
299
300 static int set_helper_option(struct transport *transport,
301                           const char *name, const char *value)
302 {
303         struct helper_data *data = transport->data;
304         struct strbuf buf = STRBUF_INIT;
305         int i, ret, is_bool = 0;
306
307         get_helper(transport);
308
309         if (!data->option)
310                 return 1;
311
312         if (!strcmp(name, "deepen-not"))
313                 return string_list_set_helper_option(data, name,
314                                                      (struct string_list *)value);
315
316         for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
317                 if (!strcmp(name, unsupported_options[i]))
318                         return 1;
319         }
320
321         for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
322                 if (!strcmp(name, boolean_options[i])) {
323                         is_bool = 1;
324                         break;
325                 }
326         }
327
328         strbuf_addf(&buf, "option %s ", name);
329         if (is_bool)
330                 strbuf_addstr(&buf, value ? "true" : "false");
331         else
332                 quote_c_style(value, &buf, NULL, 0);
333         strbuf_addch(&buf, '\n');
334
335         ret = strbuf_set_helper_option(data, &buf);
336         strbuf_release(&buf);
337         return ret;
338 }
339
340 static void standard_options(struct transport *t)
341 {
342         char buf[16];
343         int v = t->verbose;
344
345         set_helper_option(t, "progress", t->progress ? "true" : "false");
346
347         xsnprintf(buf, sizeof(buf), "%d", v + 1);
348         set_helper_option(t, "verbosity", buf);
349
350         switch (t->family) {
351         case TRANSPORT_FAMILY_ALL:
352                 /*
353                  * this is already the default,
354                  * do not break old remote helpers by setting "all" here
355                  */
356                 break;
357         case TRANSPORT_FAMILY_IPV4:
358                 set_helper_option(t, "family", "ipv4");
359                 break;
360         case TRANSPORT_FAMILY_IPV6:
361                 set_helper_option(t, "family", "ipv6");
362                 break;
363         }
364 }
365
366 static int release_helper(struct transport *transport)
367 {
368         int res = 0;
369         struct helper_data *data = transport->data;
370         refspec_clear(&data->rs);
371         res = disconnect_helper(transport);
372         free(transport->data);
373         return res;
374 }
375
376 static int fetch_with_fetch(struct transport *transport,
377                             int nr_heads, struct ref **to_fetch)
378 {
379         struct helper_data *data = transport->data;
380         int i;
381         struct strbuf buf = STRBUF_INIT;
382
383         for (i = 0; i < nr_heads; i++) {
384                 const struct ref *posn = to_fetch[i];
385                 if (posn->status & REF_STATUS_UPTODATE)
386                         continue;
387
388                 strbuf_addf(&buf, "fetch %s %s\n",
389                             oid_to_hex(&posn->old_oid),
390                             posn->symref ? posn->symref : posn->name);
391         }
392
393         strbuf_addch(&buf, '\n');
394         sendline(data, &buf);
395
396         while (1) {
397                 if (recvline(data, &buf))
398                         exit(128);
399
400                 if (starts_with(buf.buf, "lock ")) {
401                         const char *name = buf.buf + 5;
402                         if (transport->pack_lockfile)
403                                 warning(_("%s also locked %s"), data->name, name);
404                         else
405                                 transport->pack_lockfile = xstrdup(name);
406                 }
407                 else if (data->check_connectivity &&
408                          data->transport_options.check_self_contained_and_connected &&
409                          !strcmp(buf.buf, "connectivity-ok"))
410                         data->transport_options.self_contained_and_connected = 1;
411                 else if (!buf.len)
412                         break;
413                 else
414                         warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
415         }
416         strbuf_release(&buf);
417         return 0;
418 }
419
420 static int get_importer(struct transport *transport, struct child_process *fastimport)
421 {
422         struct child_process *helper = get_helper(transport);
423         struct helper_data *data = transport->data;
424         int cat_blob_fd, code;
425         child_process_init(fastimport);
426         fastimport->in = xdup(helper->out);
427         argv_array_push(&fastimport->args, "fast-import");
428         argv_array_push(&fastimport->args, "--allow-unsafe-features");
429         argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
430
431         if (data->bidi_import) {
432                 cat_blob_fd = xdup(helper->in);
433                 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
434         }
435         fastimport->git_cmd = 1;
436
437         code = start_command(fastimport);
438         return code;
439 }
440
441 static int get_exporter(struct transport *transport,
442                         struct child_process *fastexport,
443                         struct string_list *revlist_args)
444 {
445         struct helper_data *data = transport->data;
446         struct child_process *helper = get_helper(transport);
447         int i;
448
449         child_process_init(fastexport);
450
451         /* we need to duplicate helper->in because we want to use it after
452          * fastexport is done with it. */
453         fastexport->out = dup(helper->in);
454         argv_array_push(&fastexport->args, "fast-export");
455         argv_array_push(&fastexport->args, "--use-done-feature");
456         argv_array_push(&fastexport->args, data->signed_tags ?
457                 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
458         if (data->export_marks)
459                 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
460         if (data->import_marks)
461                 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
462
463         for (i = 0; i < revlist_args->nr; i++)
464                 argv_array_push(&fastexport->args, revlist_args->items[i].string);
465
466         fastexport->git_cmd = 1;
467         return start_command(fastexport);
468 }
469
470 static int fetch_with_import(struct transport *transport,
471                              int nr_heads, struct ref **to_fetch)
472 {
473         struct child_process fastimport;
474         struct helper_data *data = transport->data;
475         int i;
476         struct ref *posn;
477         struct strbuf buf = STRBUF_INIT;
478
479         get_helper(transport);
480
481         if (get_importer(transport, &fastimport))
482                 die(_("couldn't run fast-import"));
483
484         for (i = 0; i < nr_heads; i++) {
485                 posn = to_fetch[i];
486                 if (posn->status & REF_STATUS_UPTODATE)
487                         continue;
488
489                 strbuf_addf(&buf, "import %s\n",
490                             posn->symref ? posn->symref : posn->name);
491                 sendline(data, &buf);
492                 strbuf_reset(&buf);
493         }
494
495         write_constant(data->helper->in, "\n");
496         /*
497          * remote-helpers that advertise the bidi-import capability are required to
498          * buffer the complete batch of import commands until this newline before
499          * sending data to fast-import.
500          * These helpers read back data from fast-import on their stdin, which could
501          * be mixed with import commands, otherwise.
502          */
503
504         if (finish_command(&fastimport))
505                 die(_("error while running fast-import"));
506
507         /*
508          * The fast-import stream of a remote helper that advertises
509          * the "refspec" capability writes to the refs named after the
510          * right hand side of the first refspec matching each ref we
511          * were fetching.
512          *
513          * (If no "refspec" capability was specified, for historical
514          * reasons we default to the equivalent of *:*.)
515          *
516          * Store the result in to_fetch[i].old_sha1.  Callers such
517          * as "git fetch" can use the value to write feedback to the
518          * terminal, populate FETCH_HEAD, and determine what new value
519          * should be written to peer_ref if the update is a
520          * fast-forward or this is a forced update.
521          */
522         for (i = 0; i < nr_heads; i++) {
523                 char *private, *name;
524                 posn = to_fetch[i];
525                 if (posn->status & REF_STATUS_UPTODATE)
526                         continue;
527                 name = posn->symref ? posn->symref : posn->name;
528                 if (data->rs.nr)
529                         private = apply_refspecs(&data->rs, name);
530                 else
531                         private = xstrdup(name);
532                 if (private) {
533                         if (read_ref(private, &posn->old_oid) < 0)
534                                 die(_("could not read ref %s"), private);
535                         free(private);
536                 }
537         }
538         strbuf_release(&buf);
539         return 0;
540 }
541
542 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
543 {
544         struct helper_data *data = transport->data;
545         int ret = 0;
546         int duped;
547         FILE *input;
548         struct child_process *helper;
549
550         helper = get_helper(transport);
551
552         /*
553          * Yes, dup the pipe another time, as we need unbuffered version
554          * of input pipe as FILE*. fclose() closes the underlying fd and
555          * stream buffering only can be changed before first I/O operation
556          * on it.
557          */
558         duped = dup(helper->out);
559         if (duped < 0)
560                 die_errno(_("can't dup helper output fd"));
561         input = xfdopen(duped, "r");
562         setvbuf(input, NULL, _IONBF, 0);
563
564         sendline(data, cmdbuf);
565         if (recvline_fh(input, cmdbuf))
566                 exit(128);
567
568         if (!strcmp(cmdbuf->buf, "")) {
569                 data->no_disconnect_req = 1;
570                 if (debug)
571                         fprintf(stderr, "Debug: Smart transport connection "
572                                 "ready.\n");
573                 ret = 1;
574         } else if (!strcmp(cmdbuf->buf, "fallback")) {
575                 if (debug)
576                         fprintf(stderr, "Debug: Falling back to dumb "
577                                 "transport.\n");
578         } else {
579                 die(_("unknown response to connect: %s"),
580                     cmdbuf->buf);
581         }
582
583         fclose(input);
584         return ret;
585 }
586
587 static int process_connect_service(struct transport *transport,
588                                    const char *name, const char *exec)
589 {
590         struct helper_data *data = transport->data;
591         struct strbuf cmdbuf = STRBUF_INIT;
592         int ret = 0;
593
594         /*
595          * Handle --upload-pack and friends. This is fire and forget...
596          * just warn if it fails.
597          */
598         if (strcmp(name, exec)) {
599                 int r = set_helper_option(transport, "servpath", exec);
600                 if (r > 0)
601                         warning(_("setting remote service path not supported by protocol"));
602                 else if (r < 0)
603                         warning(_("invalid remote service path"));
604         }
605
606         if (data->connect) {
607                 strbuf_addf(&cmdbuf, "connect %s\n", name);
608                 ret = run_connect(transport, &cmdbuf);
609         } else if (data->stateless_connect &&
610                    (get_protocol_version_config() == protocol_v2) &&
611                    !strcmp("git-upload-pack", name)) {
612                 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
613                 ret = run_connect(transport, &cmdbuf);
614                 if (ret)
615                         transport->stateless_rpc = 1;
616         }
617
618         strbuf_release(&cmdbuf);
619         return ret;
620 }
621
622 static int process_connect(struct transport *transport,
623                                      int for_push)
624 {
625         struct helper_data *data = transport->data;
626         const char *name;
627         const char *exec;
628
629         name = for_push ? "git-receive-pack" : "git-upload-pack";
630         if (for_push)
631                 exec = data->transport_options.receivepack;
632         else
633                 exec = data->transport_options.uploadpack;
634
635         return process_connect_service(transport, name, exec);
636 }
637
638 static int connect_helper(struct transport *transport, const char *name,
639                    const char *exec, int fd[2])
640 {
641         struct helper_data *data = transport->data;
642
643         /* Get_helper so connect is inited. */
644         get_helper(transport);
645         if (!data->connect)
646                 die(_("operation not supported by protocol"));
647
648         if (!process_connect_service(transport, name, exec))
649                 die(_("can't connect to subservice %s"), name);
650
651         fd[0] = data->helper->out;
652         fd[1] = data->helper->in;
653         return 0;
654 }
655
656 static int fetch(struct transport *transport,
657                  int nr_heads, struct ref **to_fetch)
658 {
659         struct helper_data *data = transport->data;
660         int i, count;
661
662         if (process_connect(transport, 0)) {
663                 do_take_over(transport);
664                 return transport->vtable->fetch(transport, nr_heads, to_fetch);
665         }
666
667         count = 0;
668         for (i = 0; i < nr_heads; i++)
669                 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
670                         count++;
671
672         if (!count)
673                 return 0;
674
675         if (data->check_connectivity &&
676             data->transport_options.check_self_contained_and_connected)
677                 set_helper_option(transport, "check-connectivity", "true");
678
679         if (transport->cloning)
680                 set_helper_option(transport, "cloning", "true");
681
682         if (data->transport_options.update_shallow)
683                 set_helper_option(transport, "update-shallow", "true");
684
685         if (data->transport_options.filter_options.choice) {
686                 struct strbuf expanded_filter_spec = STRBUF_INIT;
687                 expand_list_objects_filter_spec(
688                         &data->transport_options.filter_options,
689                         &expanded_filter_spec);
690                 set_helper_option(transport, "filter",
691                                   expanded_filter_spec.buf);
692                 strbuf_release(&expanded_filter_spec);
693         }
694
695         if (data->transport_options.negotiation_tips)
696                 warning("Ignoring --negotiation-tip because the protocol does not support it.");
697
698         if (data->fetch)
699                 return fetch_with_fetch(transport, nr_heads, to_fetch);
700
701         if (data->import)
702                 return fetch_with_import(transport, nr_heads, to_fetch);
703
704         return -1;
705 }
706
707 static int push_update_ref_status(struct strbuf *buf,
708                                    struct ref **ref,
709                                    struct ref *remote_refs)
710 {
711         char *refname, *msg;
712         int status, forced = 0;
713
714         if (starts_with(buf->buf, "ok ")) {
715                 status = REF_STATUS_OK;
716                 refname = buf->buf + 3;
717         } else if (starts_with(buf->buf, "error ")) {
718                 status = REF_STATUS_REMOTE_REJECT;
719                 refname = buf->buf + 6;
720         } else
721                 die(_("expected ok/error, helper said '%s'"), buf->buf);
722
723         msg = strchr(refname, ' ');
724         if (msg) {
725                 struct strbuf msg_buf = STRBUF_INIT;
726                 const char *end;
727
728                 *msg++ = '\0';
729                 if (!unquote_c_style(&msg_buf, msg, &end))
730                         msg = strbuf_detach(&msg_buf, NULL);
731                 else
732                         msg = xstrdup(msg);
733                 strbuf_release(&msg_buf);
734
735                 if (!strcmp(msg, "no match")) {
736                         status = REF_STATUS_NONE;
737                         FREE_AND_NULL(msg);
738                 }
739                 else if (!strcmp(msg, "up to date")) {
740                         status = REF_STATUS_UPTODATE;
741                         FREE_AND_NULL(msg);
742                 }
743                 else if (!strcmp(msg, "non-fast forward")) {
744                         status = REF_STATUS_REJECT_NONFASTFORWARD;
745                         FREE_AND_NULL(msg);
746                 }
747                 else if (!strcmp(msg, "already exists")) {
748                         status = REF_STATUS_REJECT_ALREADY_EXISTS;
749                         FREE_AND_NULL(msg);
750                 }
751                 else if (!strcmp(msg, "fetch first")) {
752                         status = REF_STATUS_REJECT_FETCH_FIRST;
753                         FREE_AND_NULL(msg);
754                 }
755                 else if (!strcmp(msg, "needs force")) {
756                         status = REF_STATUS_REJECT_NEEDS_FORCE;
757                         FREE_AND_NULL(msg);
758                 }
759                 else if (!strcmp(msg, "stale info")) {
760                         status = REF_STATUS_REJECT_STALE;
761                         FREE_AND_NULL(msg);
762                 }
763                 else if (!strcmp(msg, "forced update")) {
764                         forced = 1;
765                         FREE_AND_NULL(msg);
766                 }
767         }
768
769         if (*ref)
770                 *ref = find_ref_by_name(*ref, refname);
771         if (!*ref)
772                 *ref = find_ref_by_name(remote_refs, refname);
773         if (!*ref) {
774                 warning(_("helper reported unexpected status of %s"), refname);
775                 return 1;
776         }
777
778         if ((*ref)->status != REF_STATUS_NONE) {
779                 /*
780                  * Earlier, the ref was marked not to be pushed, so ignore the ref
781                  * status reported by the remote helper if the latter is 'no match'.
782                  */
783                 if (status == REF_STATUS_NONE)
784                         return 1;
785         }
786
787         (*ref)->status = status;
788         (*ref)->forced_update |= forced;
789         (*ref)->remote_status = msg;
790         return !(status == REF_STATUS_OK);
791 }
792
793 static int push_update_refs_status(struct helper_data *data,
794                                     struct ref *remote_refs,
795                                     int flags)
796 {
797         struct strbuf buf = STRBUF_INIT;
798         struct ref *ref = remote_refs;
799         int ret = 0;
800
801         for (;;) {
802                 char *private;
803
804                 if (recvline(data, &buf)) {
805                         ret = 1;
806                         break;
807                 }
808
809                 if (!buf.len)
810                         break;
811
812                 if (push_update_ref_status(&buf, &ref, remote_refs))
813                         continue;
814
815                 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
816                         continue;
817
818                 /* propagate back the update to the remote namespace */
819                 private = apply_refspecs(&data->rs, ref->name);
820                 if (!private)
821                         continue;
822                 update_ref("update by helper", private, &ref->new_oid, NULL,
823                            0, 0);
824                 free(private);
825         }
826         strbuf_release(&buf);
827         return ret;
828 }
829
830 static void set_common_push_options(struct transport *transport,
831                                    const char *name, int flags)
832 {
833         if (flags & TRANSPORT_PUSH_DRY_RUN) {
834                 if (set_helper_option(transport, "dry-run", "true") != 0)
835                         die(_("helper %s does not support dry-run"), name);
836         } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
837                 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
838                         die(_("helper %s does not support --signed"), name);
839         } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
840                 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
841                         die(_("helper %s does not support --signed=if-asked"), name);
842         }
843
844         if (flags & TRANSPORT_PUSH_OPTIONS) {
845                 struct string_list_item *item;
846                 for_each_string_list_item(item, transport->push_options)
847                         if (set_helper_option(transport, "push-option", item->string) != 0)
848                                 die(_("helper %s does not support 'push-option'"), name);
849         }
850 }
851
852 static int push_refs_with_push(struct transport *transport,
853                                struct ref *remote_refs, int flags)
854 {
855         int force_all = flags & TRANSPORT_PUSH_FORCE;
856         int mirror = flags & TRANSPORT_PUSH_MIRROR;
857         int atomic = flags & TRANSPORT_PUSH_ATOMIC;
858         struct helper_data *data = transport->data;
859         struct strbuf buf = STRBUF_INIT;
860         struct ref *ref;
861         struct string_list cas_options = STRING_LIST_INIT_DUP;
862         struct string_list_item *cas_option;
863
864         get_helper(transport);
865         if (!data->push)
866                 return 1;
867
868         for (ref = remote_refs; ref; ref = ref->next) {
869                 if (!ref->peer_ref && !mirror)
870                         continue;
871
872                 /* Check for statuses set by set_ref_status_for_push() */
873                 switch (ref->status) {
874                 case REF_STATUS_REJECT_NONFASTFORWARD:
875                 case REF_STATUS_REJECT_STALE:
876                 case REF_STATUS_REJECT_ALREADY_EXISTS:
877                         if (atomic) {
878                                 string_list_clear(&cas_options, 0);
879                                 return 0;
880                         } else
881                                 continue;
882                 case REF_STATUS_UPTODATE:
883                         continue;
884                 default:
885                         ; /* do nothing */
886                 }
887
888                 if (force_all)
889                         ref->force = 1;
890
891                 strbuf_addstr(&buf, "push ");
892                 if (!ref->deletion) {
893                         if (ref->force)
894                                 strbuf_addch(&buf, '+');
895                         if (ref->peer_ref)
896                                 strbuf_addstr(&buf, ref->peer_ref->name);
897                         else
898                                 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
899                 }
900                 strbuf_addch(&buf, ':');
901                 strbuf_addstr(&buf, ref->name);
902                 strbuf_addch(&buf, '\n');
903
904                 /*
905                  * The "--force-with-lease" options without explicit
906                  * values to expect have already been expanded into
907                  * the ref->old_oid_expect[] field; we can ignore
908                  * transport->smart_options->cas altogether and instead
909                  * can enumerate them from the refs.
910                  */
911                 if (ref->expect_old_sha1) {
912                         struct strbuf cas = STRBUF_INIT;
913                         strbuf_addf(&cas, "%s:%s",
914                                     ref->name, oid_to_hex(&ref->old_oid_expect));
915                         string_list_append_nodup(&cas_options,
916                                                  strbuf_detach(&cas, NULL));
917                 }
918         }
919         if (buf.len == 0) {
920                 string_list_clear(&cas_options, 0);
921                 return 0;
922         }
923
924         for_each_string_list_item(cas_option, &cas_options)
925                 set_helper_option(transport, "cas", cas_option->string);
926         set_common_push_options(transport, data->name, flags);
927
928         strbuf_addch(&buf, '\n');
929         sendline(data, &buf);
930         strbuf_release(&buf);
931         string_list_clear(&cas_options, 0);
932
933         return push_update_refs_status(data, remote_refs, flags);
934 }
935
936 static int push_refs_with_export(struct transport *transport,
937                 struct ref *remote_refs, int flags)
938 {
939         struct ref *ref;
940         struct child_process *helper, exporter;
941         struct helper_data *data = transport->data;
942         struct string_list revlist_args = STRING_LIST_INIT_DUP;
943         struct strbuf buf = STRBUF_INIT;
944
945         if (!data->rs.nr)
946                 die(_("remote-helper doesn't support push; refspec needed"));
947
948         set_common_push_options(transport, data->name, flags);
949         if (flags & TRANSPORT_PUSH_FORCE) {
950                 if (set_helper_option(transport, "force", "true") != 0)
951                         warning(_("helper %s does not support 'force'"), data->name);
952         }
953
954         helper = get_helper(transport);
955
956         write_constant(helper->in, "export\n");
957
958         for (ref = remote_refs; ref; ref = ref->next) {
959                 char *private;
960                 struct object_id oid;
961
962                 private = apply_refspecs(&data->rs, ref->name);
963                 if (private && !get_oid(private, &oid)) {
964                         strbuf_addf(&buf, "^%s", private);
965                         string_list_append_nodup(&revlist_args,
966                                                  strbuf_detach(&buf, NULL));
967                         oidcpy(&ref->old_oid, &oid);
968                 }
969                 free(private);
970
971                 if (ref->peer_ref) {
972                         if (strcmp(ref->name, ref->peer_ref->name)) {
973                                 if (!ref->deletion) {
974                                         const char *name;
975                                         int flag;
976
977                                         /* Follow symbolic refs (mainly for HEAD). */
978                                         name = resolve_ref_unsafe(ref->peer_ref->name,
979                                                                   RESOLVE_REF_READING,
980                                                                   &oid, &flag);
981                                         if (!name || !(flag & REF_ISSYMREF))
982                                                 name = ref->peer_ref->name;
983
984                                         strbuf_addf(&buf, "%s:%s", name, ref->name);
985                                 } else
986                                         strbuf_addf(&buf, ":%s", ref->name);
987
988                                 string_list_append(&revlist_args, "--refspec");
989                                 string_list_append(&revlist_args, buf.buf);
990                                 strbuf_release(&buf);
991                         }
992                         if (!ref->deletion)
993                                 string_list_append(&revlist_args, ref->peer_ref->name);
994                 }
995         }
996
997         if (get_exporter(transport, &exporter, &revlist_args))
998                 die(_("couldn't run fast-export"));
999
1000         string_list_clear(&revlist_args, 1);
1001
1002         if (finish_command(&exporter))
1003                 die(_("error while running fast-export"));
1004         if (push_update_refs_status(data, remote_refs, flags))
1005                 return 1;
1006
1007         if (data->export_marks) {
1008                 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1009                 rename(buf.buf, data->export_marks);
1010                 strbuf_release(&buf);
1011         }
1012
1013         return 0;
1014 }
1015
1016 static int push_refs(struct transport *transport,
1017                 struct ref *remote_refs, int flags)
1018 {
1019         struct helper_data *data = transport->data;
1020
1021         if (process_connect(transport, 1)) {
1022                 do_take_over(transport);
1023                 return transport->vtable->push_refs(transport, remote_refs, flags);
1024         }
1025
1026         if (!remote_refs) {
1027                 fprintf(stderr,
1028                         _("No refs in common and none specified; doing nothing.\n"
1029                           "Perhaps you should specify a branch such as 'master'.\n"));
1030                 return 0;
1031         }
1032
1033         if (data->push)
1034                 return push_refs_with_push(transport, remote_refs, flags);
1035
1036         if (data->export)
1037                 return push_refs_with_export(transport, remote_refs, flags);
1038
1039         return -1;
1040 }
1041
1042
1043 static int has_attribute(const char *attrs, const char *attr)
1044 {
1045         int len;
1046         if (!attrs)
1047                 return 0;
1048
1049         len = strlen(attr);
1050         for (;;) {
1051                 const char *space = strchrnul(attrs, ' ');
1052                 if (len == space - attrs && !strncmp(attrs, attr, len))
1053                         return 1;
1054                 if (!*space)
1055                         return 0;
1056                 attrs = space + 1;
1057         }
1058 }
1059
1060 static struct ref *get_refs_list(struct transport *transport, int for_push,
1061                                  const struct argv_array *ref_prefixes)
1062 {
1063         struct helper_data *data = transport->data;
1064         struct child_process *helper;
1065         struct ref *ret = NULL;
1066         struct ref **tail = &ret;
1067         struct ref *posn;
1068         struct strbuf buf = STRBUF_INIT;
1069
1070         helper = get_helper(transport);
1071
1072         if (process_connect(transport, for_push)) {
1073                 do_take_over(transport);
1074                 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1075         }
1076
1077         if (data->push && for_push)
1078                 write_str_in_full(helper->in, "list for-push\n");
1079         else
1080                 write_str_in_full(helper->in, "list\n");
1081
1082         while (1) {
1083                 char *eov, *eon;
1084                 if (recvline(data, &buf))
1085                         exit(128);
1086
1087                 if (!*buf.buf)
1088                         break;
1089
1090                 eov = strchr(buf.buf, ' ');
1091                 if (!eov)
1092                         die(_("malformed response in ref list: %s"), buf.buf);
1093                 eon = strchr(eov + 1, ' ');
1094                 *eov = '\0';
1095                 if (eon)
1096                         *eon = '\0';
1097                 *tail = alloc_ref(eov + 1);
1098                 if (buf.buf[0] == '@')
1099                         (*tail)->symref = xstrdup(buf.buf + 1);
1100                 else if (buf.buf[0] != '?')
1101                         get_oid_hex(buf.buf, &(*tail)->old_oid);
1102                 if (eon) {
1103                         if (has_attribute(eon + 1, "unchanged")) {
1104                                 (*tail)->status |= REF_STATUS_UPTODATE;
1105                                 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1106                                         die(_("could not read ref %s"),
1107                                             (*tail)->name);
1108                         }
1109                 }
1110                 tail = &((*tail)->next);
1111         }
1112         if (debug)
1113                 fprintf(stderr, "Debug: Read ref listing.\n");
1114         strbuf_release(&buf);
1115
1116         for (posn = ret; posn; posn = posn->next)
1117                 resolve_remote_symref(posn, ret);
1118
1119         return ret;
1120 }
1121
1122 static struct transport_vtable vtable = {
1123         0,
1124         set_helper_option,
1125         get_refs_list,
1126         fetch,
1127         push_refs,
1128         connect_helper,
1129         release_helper
1130 };
1131
1132 int transport_helper_init(struct transport *transport, const char *name)
1133 {
1134         struct helper_data *data = xcalloc(1, sizeof(*data));
1135         data->name = name;
1136
1137         transport_check_allowed(name);
1138
1139         if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1140                 debug = 1;
1141
1142         transport->data = data;
1143         transport->vtable = &vtable;
1144         transport->smart_options = &(data->transport_options);
1145         return 0;
1146 }
1147
1148 /*
1149  * Linux pipes can buffer 65536 bytes at once (and most platforms can
1150  * buffer less), so attempt reads and writes with up to that size.
1151  */
1152 #define BUFFERSIZE 65536
1153 /* This should be enough to hold debugging message. */
1154 #define PBUFFERSIZE 8192
1155
1156 /* Print bidirectional transfer loop debug message. */
1157 __attribute__((format (printf, 1, 2)))
1158 static void transfer_debug(const char *fmt, ...)
1159 {
1160         /*
1161          * NEEDSWORK: This function is sometimes used from multiple threads, and
1162          * we end up using debug_enabled racily. That "should not matter" since
1163          * we always write the same value, but it's still wrong. This function
1164          * is listed in .tsan-suppressions for the time being.
1165          */
1166
1167         va_list args;
1168         char msgbuf[PBUFFERSIZE];
1169         static int debug_enabled = -1;
1170
1171         if (debug_enabled < 0)
1172                 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1173         if (!debug_enabled)
1174                 return;
1175
1176         va_start(args, fmt);
1177         vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1178         va_end(args);
1179         fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1180 }
1181
1182 /* Stream state: More data may be coming in this direction. */
1183 #define SSTATE_TRANSFERRING 0
1184 /*
1185  * Stream state: No more data coming in this direction, flushing rest of
1186  * data.
1187  */
1188 #define SSTATE_FLUSHING 1
1189 /* Stream state: Transfer in this direction finished. */
1190 #define SSTATE_FINISHED 2
1191
1192 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1193 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1194 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1195
1196 /* Unidirectional transfer. */
1197 struct unidirectional_transfer {
1198         /* Source */
1199         int src;
1200         /* Destination */
1201         int dest;
1202         /* Is source socket? */
1203         int src_is_sock;
1204         /* Is destination socket? */
1205         int dest_is_sock;
1206         /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1207         int state;
1208         /* Buffer. */
1209         char buf[BUFFERSIZE];
1210         /* Buffer used. */
1211         size_t bufuse;
1212         /* Name of source. */
1213         const char *src_name;
1214         /* Name of destination. */
1215         const char *dest_name;
1216 };
1217
1218 /* Closes the target (for writing) if transfer has finished. */
1219 static void udt_close_if_finished(struct unidirectional_transfer *t)
1220 {
1221         if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1222                 t->state = SSTATE_FINISHED;
1223                 if (t->dest_is_sock)
1224                         shutdown(t->dest, SHUT_WR);
1225                 else
1226                         close(t->dest);
1227                 transfer_debug("Closed %s.", t->dest_name);
1228         }
1229 }
1230
1231 /*
1232  * Tries to read data from source into buffer. If buffer is full,
1233  * no data is read. Returns 0 on success, -1 on error.
1234  */
1235 static int udt_do_read(struct unidirectional_transfer *t)
1236 {
1237         ssize_t bytes;
1238
1239         if (t->bufuse == BUFFERSIZE)
1240                 return 0;       /* No space for more. */
1241
1242         transfer_debug("%s is readable", t->src_name);
1243         bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1244         if (bytes < 0) {
1245                 error_errno(_("read(%s) failed"), t->src_name);
1246                 return -1;
1247         } else if (bytes == 0) {
1248                 transfer_debug("%s EOF (with %i bytes in buffer)",
1249                         t->src_name, (int)t->bufuse);
1250                 t->state = SSTATE_FLUSHING;
1251         } else if (bytes > 0) {
1252                 t->bufuse += bytes;
1253                 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1254                         (int)bytes, t->src_name, (int)t->bufuse);
1255         }
1256         return 0;
1257 }
1258
1259 /* Tries to write data from buffer into destination. If buffer is empty,
1260  * no data is written. Returns 0 on success, -1 on error.
1261  */
1262 static int udt_do_write(struct unidirectional_transfer *t)
1263 {
1264         ssize_t bytes;
1265
1266         if (t->bufuse == 0)
1267                 return 0;       /* Nothing to write. */
1268
1269         transfer_debug("%s is writable", t->dest_name);
1270         bytes = xwrite(t->dest, t->buf, t->bufuse);
1271         if (bytes < 0) {
1272                 error_errno(_("write(%s) failed"), t->dest_name);
1273                 return -1;
1274         } else if (bytes > 0) {
1275                 t->bufuse -= bytes;
1276                 if (t->bufuse)
1277                         memmove(t->buf, t->buf + bytes, t->bufuse);
1278                 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1279                         (int)bytes, t->dest_name, (int)t->bufuse);
1280         }
1281         return 0;
1282 }
1283
1284
1285 /* State of bidirectional transfer loop. */
1286 struct bidirectional_transfer_state {
1287         /* Direction from program to git. */
1288         struct unidirectional_transfer ptg;
1289         /* Direction from git to program. */
1290         struct unidirectional_transfer gtp;
1291 };
1292
1293 static void *udt_copy_task_routine(void *udt)
1294 {
1295         struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1296         while (t->state != SSTATE_FINISHED) {
1297                 if (STATE_NEEDS_READING(t->state))
1298                         if (udt_do_read(t))
1299                                 return NULL;
1300                 if (STATE_NEEDS_WRITING(t->state))
1301                         if (udt_do_write(t))
1302                                 return NULL;
1303                 if (STATE_NEEDS_CLOSING(t->state))
1304                         udt_close_if_finished(t);
1305         }
1306         return udt;     /* Just some non-NULL value. */
1307 }
1308
1309 #ifndef NO_PTHREADS
1310
1311 /*
1312  * Join thread, with appropriate errors on failure. Name is name for the
1313  * thread (for error messages). Returns 0 on success, 1 on failure.
1314  */
1315 static int tloop_join(pthread_t thread, const char *name)
1316 {
1317         int err;
1318         void *tret;
1319         err = pthread_join(thread, &tret);
1320         if (!tret) {
1321                 error(_("%s thread failed"), name);
1322                 return 1;
1323         }
1324         if (err) {
1325                 error(_("%s thread failed to join: %s"), name, strerror(err));
1326                 return 1;
1327         }
1328         return 0;
1329 }
1330
1331 /*
1332  * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1333  * -1 on failure.
1334  */
1335 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1336 {
1337         pthread_t gtp_thread;
1338         pthread_t ptg_thread;
1339         int err;
1340         int ret = 0;
1341         err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1342                 &s->gtp);
1343         if (err)
1344                 die(_("can't start thread for copying data: %s"), strerror(err));
1345         err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1346                 &s->ptg);
1347         if (err)
1348                 die(_("can't start thread for copying data: %s"), strerror(err));
1349
1350         ret |= tloop_join(gtp_thread, "Git to program copy");
1351         ret |= tloop_join(ptg_thread, "Program to git copy");
1352         return ret;
1353 }
1354 #else
1355
1356 /* Close the source and target (for writing) for transfer. */
1357 static void udt_kill_transfer(struct unidirectional_transfer *t)
1358 {
1359         t->state = SSTATE_FINISHED;
1360         /*
1361          * Socket read end left open isn't a disaster if nobody
1362          * attempts to read from it (mingw compat headers do not
1363          * have SHUT_RD)...
1364          *
1365          * We can't fully close the socket since otherwise gtp
1366          * task would first close the socket it sends data to
1367          * while closing the ptg file descriptors.
1368          */
1369         if (!t->src_is_sock)
1370                 close(t->src);
1371         if (t->dest_is_sock)
1372                 shutdown(t->dest, SHUT_WR);
1373         else
1374                 close(t->dest);
1375 }
1376
1377 /*
1378  * Join process, with appropriate errors on failure. Name is name for the
1379  * process (for error messages). Returns 0 on success, 1 on failure.
1380  */
1381 static int tloop_join(pid_t pid, const char *name)
1382 {
1383         int tret;
1384         if (waitpid(pid, &tret, 0) < 0) {
1385                 error_errno(_("%s process failed to wait"), name);
1386                 return 1;
1387         }
1388         if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1389                 error(_("%s process failed"), name);
1390                 return 1;
1391         }
1392         return 0;
1393 }
1394
1395 /*
1396  * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1397  * -1 on failure.
1398  */
1399 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1400 {
1401         pid_t pid1, pid2;
1402         int ret = 0;
1403
1404         /* Fork thread #1: git to program. */
1405         pid1 = fork();
1406         if (pid1 < 0)
1407                 die_errno(_("can't start thread for copying data"));
1408         else if (pid1 == 0) {
1409                 udt_kill_transfer(&s->ptg);
1410                 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1411         }
1412
1413         /* Fork thread #2: program to git. */
1414         pid2 = fork();
1415         if (pid2 < 0)
1416                 die_errno(_("can't start thread for copying data"));
1417         else if (pid2 == 0) {
1418                 udt_kill_transfer(&s->gtp);
1419                 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1420         }
1421
1422         /*
1423          * Close both streams in parent as to not interfere with
1424          * end of file detection and wait for both tasks to finish.
1425          */
1426         udt_kill_transfer(&s->gtp);
1427         udt_kill_transfer(&s->ptg);
1428         ret |= tloop_join(pid1, "Git to program copy");
1429         ret |= tloop_join(pid2, "Program to git copy");
1430         return ret;
1431 }
1432 #endif
1433
1434 /*
1435  * Copies data from stdin to output and from input to stdout simultaneously.
1436  * Additionally filtering through given filter. If filter is NULL, uses
1437  * identity filter.
1438  */
1439 int bidirectional_transfer_loop(int input, int output)
1440 {
1441         struct bidirectional_transfer_state state;
1442
1443         /* Fill the state fields. */
1444         state.ptg.src = input;
1445         state.ptg.dest = 1;
1446         state.ptg.src_is_sock = (input == output);
1447         state.ptg.dest_is_sock = 0;
1448         state.ptg.state = SSTATE_TRANSFERRING;
1449         state.ptg.bufuse = 0;
1450         state.ptg.src_name = "remote input";
1451         state.ptg.dest_name = "stdout";
1452
1453         state.gtp.src = 0;
1454         state.gtp.dest = output;
1455         state.gtp.src_is_sock = 0;
1456         state.gtp.dest_is_sock = (input == output);
1457         state.gtp.state = SSTATE_TRANSFERRING;
1458         state.gtp.bufuse = 0;
1459         state.gtp.src_name = "stdin";
1460         state.gtp.dest_name = "remote output";
1461
1462         return tloop_spawnwait_tasks(&state);
1463 }