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