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